forked from gvalkov/python-evdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenecodes.py
More file actions
executable file
·103 lines (81 loc) · 2.23 KB
/
genecodes.py
File metadata and controls
executable file
·103 lines (81 loc) · 2.23 KB
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
#!/usr/bin/env python
# -*- coding: utf-8; -*-
'''
Generate a Python extension module that exports macros from
/usr/include/linux/input.h and /usr/include/linux/uinput.h
'''
import os, sys, re
template = r'''
#include <Python.h>
#include <linux/input.h>
#include <linux/uinput.h>
/* Automatically generated by evdev.genecodes */
/* Generated on %(uname)s */
#define MODULE_NAME "_ecodes"
#define MODULE_HELP "linux/input.h and linux/uinput.h macros"
static PyMethodDef MethodTable[] = {
{ NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
MODULE_NAME,
MODULE_HELP,
-1, /* m_size */
MethodTable, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
static PyObject *
moduleinit(void)
{
#if PY_MAJOR_VERSION >= 3
PyObject* m = PyModule_Create(&moduledef);
#else
PyObject* m = Py_InitModule3(MODULE_NAME, MethodTable, MODULE_HELP);
#endif
if (m == NULL) return NULL;
/* input.h constants */
%(input)s
/* uinput.h constants */
%(uinput)s
return m;
}
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit__ecodes(void)
{
return moduleinit();
}
#else
PyMODINIT_FUNC
init_ecodes(void)
{
moduleinit();
}
#endif
'''
inputh = '/usr/include/linux/input.h' if len(sys.argv) == 1 else sys.argv[1]
uinputh = '/usr/include/linux/uinput.h' if len(sys.argv) < 3 else sys.argv[2]
inputh_re = r'#define +((?:KEY|ABS|REL|SW|MSC|LED|BTN|REP|SND|ID|EV|BUS|SYN|FF)_\w+)'
inputh_re = re.compile(inputh_re)
uinputh_re = r'#define +((?:EV|UI_FF)_\w+)'
uinputh_re = re.compile(uinputh_re)
for fn in inputh, uinputh:
if not os.path.exists(fn):
print('no such file: %s' % inputh)
sys.exit(1)
def getmacros(fn, regex):
for line in open(fn):
macro = regex.search(line)
if macro:
yield ' PyModule_AddIntMacro(m, %s);' % macro.group(1)
uname = list(os.uname()); del uname[1]
uname = ' '.join(uname)
input_macros = os.linesep.join(getmacros(inputh, inputh_re))
uinput_macros = os.linesep.join(getmacros(uinputh, uinputh_re))
ctx = {'uname': uname, 'input': input_macros, 'uinput': uinput_macros}
print(template % ctx)