| 1 |
# pylint: disable=undefined-variable |
| 2 |
""" |
| 3 |
This modules exposes the integer constants defined in ``linux/input.h`` and |
| 4 |
``linux/input-event-codes.h``. |
| 5 |
|
| 6 |
Exposed constants:: |
| 7 |
|
| 8 |
KEY, ABS, REL, SW, MSC, LED, BTN, REP, SND, ID, EV, |
| 9 |
BUS, SYN, FF, FF_STATUS, INPUT_PROP |
| 10 |
|
| 11 |
This module also provides reverse and forward mappings of the names and values |
| 12 |
of the above mentioned constants:: |
| 13 |
|
| 14 |
>>> evdev.ecodes.KEY_A |
| 15 |
30 |
| 16 |
|
| 17 |
>>> evdev.ecodes.ecodes['KEY_A'] |
| 18 |
30 |
| 19 |
|
| 20 |
>>> evdev.ecodes.KEY[30] |
| 21 |
'KEY_A' |
| 22 |
|
| 23 |
>>> evdev.ecodes.REL[0] |
| 24 |
'REL_X' |
| 25 |
|
| 26 |
>>> evdev.ecodes.EV[evdev.ecodes.EV_KEY] |
| 27 |
'EV_KEY' |
| 28 |
|
| 29 |
>>> evdev.ecodes.bytype[evdev.ecodes.EV_REL][0] |
| 30 |
'REL_X' |
| 31 |
|
| 32 |
Keep in mind that values in reverse mappings may point to one or more event |
| 33 |
codes. For example:: |
| 34 |
|
| 35 |
>>> evdev.ecodes.FF[80] |
| 36 |
('FF_EFFECT_MIN', 'FF_RUMBLE') |
| 37 |
|
| 38 |
>>> evdev.ecodes.FF[81] |
| 39 |
'FF_PERIODIC' |
| 40 |
""" |
| 41 |
|
| 42 |
from inspect import getmembers |
| 43 |
|
| 44 |
from . import _ecodes |
| 45 |
|
| 46 |
#: Mapping of names to values. |
| 47 |
ecodes = {} |
| 48 |
|
| 49 |
prefixes = "KEY ABS REL SW MSC LED BTN REP SND ID EV BUS SYN FF_STATUS FF INPUT_PROP UI_FF".split() |
| 50 |
prev_prefix = "" |
| 51 |
g = globals() |
| 52 |
|
| 53 |
# eg. code: 'REL_Z', val: 2 |
| 54 |
for code, val in getmembers(_ecodes): |
| 55 |
for prefix in prefixes: # eg. 'REL' |
| 56 |
if code.startswith(prefix): |
| 57 |
ecodes[code] = val |
| 58 |
# FF_STATUS codes should not appear in the FF reverse mapping |
| 59 |
if not code.startswith(prev_prefix): |
| 60 |
d = g.setdefault(prefix, {}) |
| 61 |
# codes that share the same value will be added to a list. eg: |
| 62 |
# >>> ecodes.FF_STATUS |
| 63 |
# {0: 'FF_STATUS_STOPPED', 1: ['FF_STATUS_MAX', 'FF_STATUS_PLAYING']} |
| 64 |
if val in d: |
| 65 |
if isinstance(d[val], list): |
| 66 |
d[val].append(code) |
| 67 |
else: |
| 68 |
d[val] = [d[val], code] |
| 69 |
else: |
| 70 |
d[val] = code |
| 71 |
|
| 72 |
prev_prefix = prefix |
| 73 |
|
| 74 |
|
| 75 |
# Convert lists to tuples. |
| 76 |
k, v = None, None |
| 77 |
for prefix in prefixes: |
| 78 |
for k, v in g[prefix].items(): |
| 79 |
if isinstance(v, list): |
| 80 |
g[prefix][k] = tuple(v) |
| 81 |
|
| 82 |
|
| 83 |
#: Keys are a combination of all BTN and KEY codes. |
| 84 |
keys = {} |
| 85 |
keys.update(BTN) |
| 86 |
keys.update(KEY) |
| 87 |
|
| 88 |
# make keys safe to use for the default list of uinput device |
| 89 |
# capabilities |
| 90 |
del keys[_ecodes.KEY_MAX] |
| 91 |
del keys[_ecodes.KEY_CNT] |
| 92 |
|
| 93 |
#: Mapping of event types to other value/name mappings. |
| 94 |
bytype = { |
| 95 |
_ecodes.EV_KEY: keys, |
| 96 |
_ecodes.EV_ABS: ABS, |
| 97 |
_ecodes.EV_REL: REL, |
| 98 |
_ecodes.EV_SW: SW, |
| 99 |
_ecodes.EV_MSC: MSC, |
| 100 |
_ecodes.EV_LED: LED, |
| 101 |
_ecodes.EV_REP: REP, |
| 102 |
_ecodes.EV_SND: SND, |
| 103 |
_ecodes.EV_SYN: SYN, |
| 104 |
_ecodes.EV_FF: FF, |
| 105 |
_ecodes.EV_FF_STATUS: FF_STATUS, |
| 106 |
} |
| 107 |
|
| 108 |
from evdev._ecodes import * |
| 109 |
|
| 110 |
# cheaper than whitelisting in an __all__ |
| 111 |
del code, val, prefix, getmembers, g, d, k, v, prefixes, prev_prefix |