forked from phillipberndt/fakexrandr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_skeleton.py
executable file
·72 lines (62 loc) · 2.18 KB
/
make_skeleton.py
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
#!/usr/bin/env python
# encoding: utf-8
import os
import re
import sys
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix='.c') as temp:
temp.write("#include <X11/extensions/Xrandr.h>")
temp.flush()
output = os.popen("gcc -E '{}'".format(temp.name)).read()
ccode = open("libXrandr.c").read()
print("""
/* This file was automatically generated by ./make_skeleton.py */
#include <X11/extensions/Xrandr.h>
#include <X11/Xlib.h>
""")
functions = re.findall(r"(?m)^(\w+(?:\s*\*+)?)\s*(XRR\w+)\s*\(([^)]+)\);",
output)
for function in functions:
rettype, name, parameters = function
parameter_array = re.split("\s*,\s*", parameters)
call = []
actions = []
warning = ""
for x in parameter_array:
x = x.split()
param = x[-1].replace("*", "")
call.append(param)
if param != x[-1]:
if x[0] in ("RRCrtc", "RROutput"):
warning = ("\033[31mWarning\033[0m: In {name}: parameter "
"{param} unhandled").format( name=name, param=" ".join(x))
continue
if x[0] in ("RRCrtc", "RROutput"):
actions.append(
" {param} = {param} & ~XID_SPLIT_MASK;".format(param=param))
if re.search("(?<!_){}".format(name), ccode):
print(("static {ret} (*_{fn})({par_def});\n".format(
ret=rettype, par_def=", ".join(parameter_array), fn=name)))
continue
if warning:
print >> sys.stderr, warning
if actions:
actions.append("")
returnv = "return " if rettype.lower() != "void" else ""
print(("static {ret} (*_{fn})({par_def});\n"
"{ret} {fn}({par_def}) {{\n"
"{actions}"
"{returnv}_{fn}({par_call});\n"
"}}\n\n").format(
ret=rettype,
fn=name,
returnv=returnv,
actions="\n".join(actions),
par_def=", ".join(parameter_array),
par_call=", ".join(call)
))
defns = []
for function in functions:
defns.append("_{fn} = dlsym(xrandr_lib, \"{fn}\")".format(fn=function[1]))
print("#define FUNCTION_POINTER_INITIALIZATIONS {defns}".format(
defns="; ".join(defns)))