summaryrefslogtreecommitdiff
path: root/parse-glxinfo.py
blob: 843c9fb654ca856f0780279f7665e5bf964d72a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
#
# Copyright 2013 Ilia Mirkin.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import sys
import json
import subprocess

__author__ = "Ilia Mirkin <imirkin@alum.mit.edu>"


class GLInfo(object):
    def __init__(self, lines):
        self.profile = "compat"
        if "core profile" in lines[0]:
            self.profile = "core"
        if "ES profile" in lines[0]:
            self.profile = "es"
        for i, line in enumerate(lines):
            if "language version" in line:
                self.glsl = line.split(":")[-1].strip()
            elif "version" in line:
                version = line.split(":")[-1].strip()
                if " " in version:
                    version, tail = version.split(" ", 1)
                    if tail.startswith("("):
                        tail = tail.split(")", 1)[-1]
                    self.library = tail.strip()
                self.gl = version
            elif "extensions" in line:
                break
        else:
            assert False, "Extension list not found"

        self.extensions = []
        self.limits = {}

        for i in range(i + 1, len(lines)):
            line = lines[i]
            if not line:
                continue
            if line.startswith("  "):
                line = line.strip()
                if line:
                    self.extensions.append(line)
            elif "limits" in line:
                break
            else:
                assert False, "Unknown line in extension list: %r" % line
        else:
            if self.profile != "es":
                assert False, "Limit list not found"
            else:
                return

        for i in range(i + 1, len(lines)):
            line = lines[i]
            if not line:
                continue
            if not line.startswith("  "):
                assert False, "Unknown line in limit list"
            if " = " not in line:
                continue
            name, value = line.split(" = ", 1)
            self.limits[name.strip()] = value.strip()

if len(sys.argv) > 1:
    glxinfo = open(sys.argv[1], "rb").read()
else:
    glxinfo = subprocess.check_output(["glxinfo", "-l", "-s"])
infos = []

lines = []
vendor = None
renderer = None
for line in glxinfo.splitlines():
    line = line.decode("utf-8")
    if line.startswith("OpenGL vendor"):
        vendor = line.split(":")[-1].strip()
        continue
    if line.startswith("OpenGL renderer"):
        renderer = line.split(":")[-1].strip()
        continue
    # We hit the GLX visuals list. No more.
    if "GLX Visuals" in line:
        infos.append(GLInfo(lines))
        break
    if "OpenGL" in line:
        # The first version string line
        if not lines and "version string" in line:
            lines.append(line)
            continue
        # A new version string line. Must be the start of the compat profile
        elif "version string" in line and "shading" not in line:
            infos.append(GLInfo(lines))
            lines = [line]
            continue
    # Another line after the version string
    if lines:
        lines.append(line)

print("register({0}, {1}, {2})".format(
    json.dumps(vendor),
    json.dumps(renderer),
    json.dumps(infos, default=lambda x: x.__dict__, indent=4, sort_keys=True)))