Skip to content

Commit 2b25e79

Browse files
committed
factor event code resolving out into evdev.util
1 parent 6462123 commit 2b25e79

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

evdev/device.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44

5-
from evdev import _input, ecodes
5+
from evdev import _input, ecodes, util
66
from evdev.events import InputEvent
77

88

@@ -81,23 +81,7 @@ def capabilities(self, verbose=False):
8181
'''
8282

8383
if verbose:
84-
cap = {}
85-
for type, codes in self._capabilities.items():
86-
type_name = ecodes.EV[type]
87-
88-
# ecodes.keys are a combination of KEY_ and BTN_ codes
89-
if type == ecodes.EV_KEY:
90-
code_names = ecodes.keys
91-
else:
92-
code_names = getattr(ecodes, type_name.split('_')[-1])
93-
94-
codes_res = []
95-
for i in codes:
96-
l = [(code_names[i], i) if i in code_names else ('?', i)]
97-
codes_res.append(l)
98-
99-
cap[(type_name, type)] = codes_res
100-
return cap
84+
return dict(util.resolve_ecodes(self._capabilities))
10185
else:
10286
return self._capabilities
10387

evdev/util.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import stat
55
import glob
66

7+
from evdev import ecodes
78
from evdev.events import event_factory
89

910

@@ -47,4 +48,30 @@ def categorize(event):
4748
return event
4849

4950

50-
__all__ = list_devices, is_device, categorize
51+
def resolve_ecodes(typecodemap, unknown='?'):
52+
'''
53+
Resolve event codes and types to their verbose names:
54+
{ 1 : [272, 273, 274] } =>
55+
{ ('EV_KEY', 1) : [('BTN_MOUSE', 272),
56+
('BTN_RIGHT', 273),
57+
('BTN_MIDDLE', 273)] }
58+
'''
59+
60+
for type, codes in typecodemap.items():
61+
type_name = ecodes.EV[type]
62+
63+
# ecodes.keys are a combination of KEY_ and BTN_ codes
64+
if type == ecodes.EV_KEY:
65+
code_names = ecodes.keys
66+
else:
67+
code_names = getattr(ecodes, type_name.split('_')[-1])
68+
69+
codes_res = []
70+
for i in codes:
71+
l = [(code_names[i], i) if i in code_names else (unknown, i)]
72+
codes_res.append(l)
73+
74+
yield (type_name, type), codes_res
75+
76+
77+
__all__ = list_devices, is_device, categorize, resolve_ecodes

0 commit comments

Comments
 (0)