File: make-event-names.py

package info (click to toggle)
rust-evdev-rs 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: python: 211; makefile: 4; sh: 3
file content (274 lines) | stat: -rwxr-xr-x 6,846 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
# Parses linux/input.h scanning for #define KEY_FOO 134
# Prints Rust source header files that can be used for
# mapping and lookup tables.
#
# The original version of this file is in libevdev
#

import re
import sys


class Bits(object):
    pass


prefixes = [
    "EV_",
    "REL_",
    "ABS_",
    "KEY_",
    "BTN_",
    "LED_",
    "SND_",
    "MSC_",
    "SW_",
    "FF_",
    "SYN_",
    "REP_",
    "INPUT_PROP_",
    "BUS_"
]

prefix_additional = {
    "key": ["btn"]
}

blacklist = [
    "EV_VERSION",
    "BTN_MISC",
    "BTN_MOUSE",
    "BTN_JOYSTICK",
    "BTN_GAMEPAD",
    "BTN_DIGI",
    "BTN_WHEEL",
    "BTN_TRIGGER_HAPPY",
]

btn_additional = [
    [0, "BTN_A"],
    [0, "BTN_B"],
    [0, "BTN_X"],
    [0, "BTN_Y"],
]

event_names = [
    "REL_",
    "ABS_",
    "KEY_",
    "BTN_",
    "LED_",
    "SND_",
    "MSC_",
    "SW_",
    "FF_",
    "SYN_",
    "REP_",
]


def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()


def get_enum_name(prefix):
    if prefix == "ev":
        return "EventType"
    elif prefix == "input_prop":
        return "InputProp"
    elif prefix == "bus":
        return "BusType"
    else:
        return "EV_" + prefix.upper()


def print_enums(bits, prefix):

    if not hasattr(bits, prefix):
        return

    enum_name = get_enum_name(prefix)
    associated_names = []

    print("#[allow(non_camel_case_types)]")
    print('#[cfg_attr(feature = "serde", derive(Serialize), derive(Deserialize))]')
    print("#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]")
    print("pub enum %s {" % enum_name)
    for val, names in list(getattr(bits, prefix).items()):
        # Note(ndesh): We use EV_MAX as proxy to write the UNKnown event
        if names[0] == "EV_MAX":
            print("    EV_UNK,")
        print("    %s = %s," % (names[0], val))
        if len(names) > 1:
            associated_names.extend([(names[0], names[1:])])
    if prefix == "key":
        for val, names in list(getattr(bits, "btn").items()):
            print("    %s = %s," % (names[0], val))
            if len(names) > 1:
                associated_names.extend([(names[0], names[1:])])
    print("}")
    print("")

    if len(associated_names) > 0:
        print("impl %s {" % enum_name)
        for orig, names in associated_names:
            for name in names:
                print("    pub const %s: %s = %s::%s;" %
                      (name, enum_name, enum_name, orig))
        print("}")
        print("")


def print_enums_convert_fn(bits, prefix):
    if prefix == "ev":
        fn_name = "EventType"
    elif prefix == "input_prop":
        fn_name = "InputProp"
    elif prefix == "bus":
        fn_name = "BusType"
    else:
        fn_name = "EV_" + prefix.upper()

    if not hasattr(bits, prefix):
        return

    print("pub const fn %s(code: u32) -> Option<%s> {" %
          ("int_to_" + convert(fn_name), fn_name))
    print("    match code {")
    for val, names in list(getattr(bits, prefix).items()):
        # Note(ndesh): We use EV_MAX as proxy to write the UNKnown event
        if names[0] == "EV_MAX":
            print("        c if c < 31 => Some(EventType::EV_UNK),")
        print("        %s => Some(%s::%s)," % (val, fn_name, names[0]))
    if prefix == "key":
        for val, names in list(getattr(bits, "btn").items()):
            print("        %s => Some(%s::%s)," % (val, fn_name, names[0]))
    print("        _ => None,")
    print("    }")
    print("}")
    print("")


def print_enums_fromstr(bits, prefix):

    if not hasattr(bits, prefix):
        return

    enum_name = get_enum_name(prefix)

    print('impl std::str::FromStr for %s {' % enum_name)
    print('    type Err = ();')
    print('    fn from_str(s: &str) -> Result<Self, Self::Err> {')
    print('        match s {')

    for p in (prefix, *prefix_additional.get(prefix, ())):
        for _val, names in list(getattr(bits, p).items()):
            name = names[0]
            print('            "%s" => Ok(%s::%s),' % (name, enum_name, name))
    print('            _ => Err(()),')
    print('        }')
    print('    }')
    print('}')
    print('')


def print_event_code(bits, prefix):
    if not hasattr(bits, prefix):
        return

    print("#[allow(non_camel_case_types)]")
    print('#[cfg_attr(feature = "serde", derive(Serialize), derive(Deserialize))]')
    print("#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]")
    print("pub enum EventCode {")
    for val, [name] in list(getattr(bits, prefix).items()):
        if name[3:]+"_" in event_names:
            print("    %s(%s)," % (name, name))
        elif name == "EV_FF_STATUS":
            print("    EV_FF_STATUS(EV_FF),")
        else:
            # Note(ndesh): We use EV_MAX as proxy to write the UNKnown event
            if name == "EV_MAX":
                print("    EV_UNK { event_type: u32, event_code: u32 },")
            print("    %s," % (name))
    if prefix == "key":
        for val, names in list(getattr(bits, "btn").items()):
            for name in names:
                print("    %s = %s," % (name, val))
    print("}")
    print("")


def print_mapping_table(bits):
    for prefix in prefixes:
        if prefix == "BTN_":
            continue
        print_enums(bits, prefix[:-1].lower())
        print_enums_convert_fn(bits, prefix[:-1].lower())
        print_enums_fromstr(bits, prefix[:-1].lower())
        if prefix == "EV_":
            print_event_code(bits, prefix[:-1].lower())


def parse_define(bits, line):
    m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
    if m is None:
        return

    name = m.group(1)

    if name in blacklist:
        return

    try:
        value = int(m.group(2), 0)
    except ValueError:
        return

    for prefix in prefixes:
        if not name.startswith(prefix):
            continue

        attrname = prefix[:-1].lower()

        if not hasattr(bits, attrname):
            setattr(bits, attrname, {})
        b = getattr(bits, attrname)
        if value in b:
            b[value].append(name)
        else:
            b[value] = [name]


def parse(fp):
    bits = Bits()

    lines = fp.readlines()
    for line in lines:
        if not line.startswith("#define"):
            continue
        parse_define(bits, line)

    return bits


def usage(prog):
    print("Usage: {} <files>".format(prog))


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        usage(sys.argv[0])
        sys.exit(2)

    print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
    print("")
    print('#[cfg(feature = "serde")]')
    print("use serde::{Deserialize, Serialize};")
    print("")

    for arg in sys.argv[1:]:
        with open(arg) as f:
            bits = parse(f)
            print_mapping_table(bits)