1local duplicates = {
2 EV_VERSION=true,
3 BTN_MISC=true,
4 BTN_MOUSE=true,
5 BTN_JOYSTICK=true,
6 BTN_GAMEPAD=true,
7 BTN_DIGI=true,
8 BTN_WHEEL=true,
9 BTN_TRIGGER_HAPPY=true,
10 SW_MAX=true,
11 REP_MAX=true,
12 FF_STATUS_MAX=true,
13}
14
15local skip = {
16 BTN=true,
17 EV=true,
18 INPUT_PROP=true,
19 MT_TOOL=true,
20}
21
22local prefixes = {
23 'EV',
24 'REL',
25 'ABS',
26 'KEY',
27 'BTN',
28 'LED',
29 'SND',
30 'MSC',
31 'SW',
32 'FF',
33 'SYN',
34 'REP',
35 'INPUT_PROP',
36 'MT_TOOL',
37}
38
39local bits = {}
40for _, prefix in ipairs(prefixes) do
41 bits[prefix] = {}
42end
43bits.EV.map = {}
44
45local function lookuptable(prefix)
46 local entries = {}
47 for _, name in ipairs(bits[prefix]) do
48 table.insert(entries, name)
49 end
50 local max = prefix..'_MAX'
51 if duplicates[max] then
52 table.insert(entries, max)
53 end
54 table.sort(entries)
55 for _, name in ipairs(entries) do
56 io.write(string.format(' { .name = "%s", .value = %s },\n', name, name))
57 end
58end
59
60for i = 1, #arg do
61 for line in io.lines(arg[i]) do
62 local name, value = line:match('^#define%s+([%w_]+)%s+(%dx?%x*)')
63 value = tonumber(value)
64 if name and not duplicates[name] then
65 for prefix, b in pairs(bits) do
66 if name:sub(1, #prefix + 1) == prefix..'_' then
67 if b.map then
68 b.map[value] = name
69 b.map[name] = value
70 end
71 table.insert(b, name)
72 end
73 end
74 end
75 end
76end
77
78io.write[[
79/* THIS FILE IS GENERATED, DO NOT EDIT */
80
81#ifndef EVENT_NAMES_H
82#define EVENT_NAMES_H
83
84]]
85for _, prefix in ipairs(prefixes) do
86 if prefix ~= 'BTN' then
87 io.write(string.format('static const char * const %s_map[%s_MAX + 1] = {\n', prefix:lower(), prefix))
88 for _, name in ipairs(bits[prefix]) do
89 io.write(string.format(' [%s] = "%s",\n', name, name))
90 end
91 if prefix == 'KEY' then
92 for _, name in ipairs(bits.BTN) do
93 io.write(string.format(' [%s] = "%s",\n', name, name))
94 end
95 end
96 io.write[[
97};
98
99]]
100 end
101end
102for _, name in ipairs{'BTN_A', 'BTN_B', 'BTN_X', 'BTN_Y'} do
103 table.insert(bits.BTN, name)
104end
105io.write[[
106static const char * const * const event_type_map[EV_MAX + 1] = {
107]]
108for _, prefix in ipairs(prefixes) do
109 if not skip[prefix] then
110 print(string.format(' [EV_%s] = %s_map,', prefix, prefix:lower()))
111 end
112end
113io.write[[
114};
115
116#if __clang__
117#pragma clang diagnostic push
118#pragma clang diagnostic ignored "-Winitializer-overrides"
119#elif __GNUC__
120#pragma GCC diagnostic push
121#pragma GCC diagnostic ignored "-Woverride-init"
122#endif
123static const int ev_max[EV_MAX + 1] = {
124]]
125for i = 0, bits.EV.map.EV_MAX do
126 local name = bits.EV.map[i]
127 if name and bits[name:sub(4)] then
128 io.write(string.format(' %s_MAX,\n', name:sub(4)))
129 else
130 io.write(' -1,\n')
131 end
132end
133io.write[[
134};
135#if __clang__
136#pragma clang diagnostic pop /* "-Winitializer-overrides" */
137#elif __GNUC__
138#pragma GCC diagnostic pop /* "-Woverride-init" */
139#endif
140
141struct name_entry {
142 const char *name;
143 unsigned int value;
144};
145
146static const struct name_entry tool_type_names[] = {
147]]
148lookuptable('MT_TOOL')
149io.write[[
150};
151
152static const struct name_entry ev_names[] = {
153]]
154lookuptable('EV')
155io.write[[
156};
157
158static const struct name_entry code_names[] = {
159]]
160lookuptable('ABS')
161lookuptable('BTN')
162lookuptable('FF')
163lookuptable('KEY')
164lookuptable('LED')
165lookuptable('MSC')
166lookuptable('REL')
167lookuptable('REP')
168lookuptable('SND')
169lookuptable('SW')
170lookuptable('SYN')
171io.write[[
172};
173
174static const struct name_entry prop_names[] = {
175]]
176lookuptable('INPUT_PROP')
177io.write[[
178};
179
180#endif /* EVENT_NAMES_H */
181]]