Skip to content

Commit a0bf03d

Browse files
committed
map all input_absinfo fields completely into AbsInfo
- rename AbsData to AbsInfo - use input_absinfo's comment for AbsInfo's docstring
1 parent 6940533 commit a0bf03d

7 files changed

Lines changed: 80 additions & 50 deletions

File tree

evdev/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Gather everything into a convenient namespace
44

5-
from evdev.device import DeviceInfo, InputDevice, AbsData
5+
from evdev.device import DeviceInfo, InputDevice, AbsInfo
66
from evdev.events import InputEvent, KeyEvent, RelEvent, SynEvent, AbsEvent, event_factory
77
from evdev.uinput import UInput, UInputError
88
from evdev.util import list_devices, categorize, resolve_ecodes

evdev/device.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,46 @@ def __eq__(self, o):
3131
and self.version == o.version
3232

3333

34+
_AbsInfo = namedtuple('AbsInfo',
35+
['value', 'min', 'max', 'fuzz', 'flat', 'resolution'])
3436

35-
_AbsData = namedtuple('AbsData',
36-
['min', 'max', 'fuzz', 'flat'])
37-
38-
class AbsData(_AbsData):
37+
class AbsInfo(_AbsInfo):
3938
'''
40-
A ``namedtuple`` for storing absolut axis information:
39+
A ``namedtuple`` for storing absolut axis information -
40+
corresponds to the ``input_absinfo`` c struct:
41+
42+
- value
43+
Latest reported value for the axis.
44+
45+
- min
46+
Specifies minimum value for the axis.
47+
48+
- max
49+
Specifies maximum value for the axis.
50+
51+
- fuzz
52+
Specifies fuzz value that is used to filter noise from the
53+
event stream.
4154
42-
- min - minimal value for an absolute axis
43-
- max - maximum value for an absolute axis
44-
- fuzz - noise tolerance (+- value)
45-
- flat - size of the center flat position
55+
- flat
56+
Values that are within this value will be discarded by joydev
57+
interface and reported as 0 instead.
58+
59+
- resolution
60+
Specifies resolution for the values reported for the axis.
61+
Resolution for main axes (``ABS_X, ABS_Y, ABS_Z``) is reported
62+
in units per millimeter (units/mm), resolution for rotational
63+
axes (``ABS_RX, ABS_RY, ABS_RZ``) is reported in units per
64+
radian.
65+
66+
.. note: The input core does not clamp reported values to the
67+
``[minimum, maximum]`` limits, such task is left to userspace.
4668
'''
4769
pass
4870

71+
def __str__(self):
72+
return 'val {}, min {}, max {}, fuzz {}, flat {}, res {}'.format(*self)
73+
4974

5075
class InputDevice(object):
5176
'''
@@ -80,15 +105,15 @@ def __init__(self, dev):
80105
#: The raw dictionary of device capabilities - see `:func:capabilities()`
81106
self._rawcapabilities = info_res[6]
82107

83-
def _capabilities(self, absdata=True):
108+
def _capabilities(self, absinfo=True):
84109
res = {}
85110
for etype, ecodes in self._rawcapabilities.items():
86111
for code in ecodes:
87112
l = res.setdefault(etype, [])
88113
if isinstance(code, tuple):
89-
if absdata:
90-
a = code[1] # (0, 0, 255, 0)
91-
i = AbsData(min=a[1], max=a[2], fuzz=a[3], flat=a[4])
114+
if absinfo:
115+
a = code[1] # (0, 0, 0, 255, 0, 0)
116+
i = AbsInfo(*a)
92117
l.append((code[0], i))
93118
else:
94119
l.append(code[0])
@@ -97,7 +122,7 @@ def _capabilities(self, absdata=True):
97122

98123
return res
99124

100-
def capabilities(self, verbose=False, absdata=True):
125+
def capabilities(self, verbose=False, absinfo=True):
101126
'''
102127
Returns the event types that this device supports as a a mapping of
103128
supported event types to lists of handled event codes. Example::
@@ -113,24 +138,24 @@ def capabilities(self, verbose=False, absdata=True):
113138
114139
Unknown codes or types will be resolved to '?'.
115140
116-
If ``absdata`` is ``True``, the list of capabilities will also
141+
If ``absinfo`` is ``True``, the list of capabilities will also
117142
include absolute axis information (``absmin``, ``absmax``,
118143
``absfuzz``, ``absflat``) in the following form::
119144
120-
{ 3 : [ (0, AbsData(min=0, max=255, fuzz=0, flat=0)),
121-
(1, AbsData(min=0, max=255, fuzz=0, flat=0)) ]}
145+
{ 3 : [ (0, AbsInfo(min=0, max=255, fuzz=0, flat=0)),
146+
(1, AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}
122147
123148
Combined with ``verbose`` the above becomes::
124149
125-
{ ('EV_ABS', 3) : [ (('ABS_X', 0), AbsData(min=0, max=255, fuzz=0, flat=0)),
126-
(('ABS_Y', 1), AbsData(min=0, max=255, fuzz=0, flat=0)) ]}
150+
{ ('EV_ABS', 3) : [ (('ABS_X', 0), AbsInfo(min=0, max=255, fuzz=0, flat=0)),
151+
(('ABS_Y', 1), AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}
127152
128153
'''
129154

130155
if verbose:
131-
return dict(util.resolve_ecodes(self._capabilities(absdata)))
156+
return dict(util.resolve_ecodes(self._capabilities(absinfo)))
132157
else:
133-
return self._capabilities(absdata)
158+
return self._capabilities(absinfo)
134159

135160
def __eq__(self, o):
136161
''' Two devices are considered equal if their :data:`info` attributes are equal. '''

evdev/input.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ event_unpack(PyObject *self, PyObject *args)
130130
static PyObject *
131131
ioctl_capabilities(PyObject *self, PyObject *args)
132132
{
133-
int abs_bits[6] = {0};
134133
int fd, ev_type, ev_code;
135134
char ev_bits[EV_MAX/8], code_bits[KEY_MAX/8];
135+
struct input_absinfo absinfo;
136136

137137
int ret = PyArg_ParseTuple(args, "i", &fd);
138138
if (!ret) return NULL;
@@ -146,7 +146,7 @@ ioctl_capabilities(PyObject *self, PyObject *args)
146146
PyObject* capabilities = PyDict_New();
147147
PyObject* eventcodes = NULL;
148148
PyObject* capability = NULL;
149-
PyObject* absdata = NULL;
149+
PyObject* absinfo = NULL;
150150
PyObject* absitem = NULL;
151151

152152
memset(&ev_bits, 0, sizeof(ev_bits));
@@ -168,13 +168,18 @@ ioctl_capabilities(PyObject *self, PyObject *args)
168168
if (test_bit(code_bits, ev_code)) {
169169
// Get abs{min,max,fuzz,flat} values for ABS_* event codes
170170
if (ev_type == EV_ABS) {
171-
memset(&abs_bits, 0, sizeof(abs_bits));
172-
ioctl(_fd, EVIOCGABS(ev_code), abs_bits);
173-
174-
absdata = Py_BuildValue("(iiiii)", abs_bits[0], abs_bits[1], abs_bits[2],
175-
abs_bits[3], abs_bits[4], abs_bits[5]);
176-
177-
absitem = Py_BuildValue("(OO)", PyLong_FromLong(ev_code), absdata);
171+
memset(&absinfo, 0, sizeof(absinfo));
172+
ioctl(_fd, EVIOCGABS(ev_code), &absinfo);
173+
174+
absinfo = Py_BuildValue("(iiiiii)",
175+
absinfo.value,
176+
absinfo.minimum,
177+
absinfo.maximum,
178+
absinfo.fuzz,
179+
absinfo.flat,
180+
absinfo.resolution);
181+
182+
absitem = Py_BuildValue("(OO)", PyLong_FromLong(ev_code), absinfo);
178183

179184
// absitem -> tuple(ABS_X, (0, 255, 0, 0))
180185
PyList_Append(eventcodes, absitem);

evdev/uinput.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ uinput_create(PyObject *self, PyObject *args) {
4848
int fd, len, i, abscode;
4949
__u16 vendor, product, version, bustype;
5050

51-
PyObject *absdata = NULL, *item = NULL;
51+
PyObject *absinfo = NULL, *item = NULL;
5252

5353
struct uinput_user_dev uidev;
5454
const char* name;
5555

5656
int ret = PyArg_ParseTuple(args, "ishhhhO", &fd, &name, &vendor,
57-
&product, &version, &bustype, &absdata);
57+
&product, &version, &bustype, &absinfo);
5858
if (!ret) return NULL;
5959

6060
memset(&uidev, 0, sizeof(uidev));
@@ -64,10 +64,10 @@ uinput_create(PyObject *self, PyObject *args) {
6464
uidev.id.version = version;
6565
uidev.id.bustype = bustype;
6666

67-
len = PyList_Size(absdata);
67+
len = PyList_Size(absinfo);
6868
for (i=0; i<len; i++) {
6969
// item -> (ABS_X, 0, 255, 0, 0)
70-
item = PyList_GetItem(absdata, i);
70+
item = PyList_GetItem(absinfo, i);
7171
abscode = (int)PyLong_AsLong(PyList_GetItem(item, 0));
7272

7373
uidev.absmin[abscode] = PyLong_AsLong(PyList_GetItem(item, 1));

evdev/uinput.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self,
6161

6262
# the min, max, fuzz and flat values for the absolute axis for
6363
# a given code
64-
absdata = []
64+
absinfo = []
6565

6666
self._verify()
6767

@@ -72,17 +72,17 @@ def __init__(self,
7272
for etype, codes in events.items():
7373
for code in codes:
7474
# handle max,min,fuzz,flat
75-
if isinstance(code, (tuple, list, device.AbsData)):
75+
if isinstance(code, (tuple, list, device.AbsInfo)):
7676
# flatten (ABS_Y, (0,255,0,0)) to (ABS_Y,0,255,0,0)
7777
f = [code[0]] ; f += code[1]
78-
absdata.append(f)
78+
absinfo.append(f)
7979
code = code[0]
8080

8181
#:todo: a lot of unnecessary packing/unpacking
8282
_uinput.enable(self.fd, etype, code)
8383

8484
# create uinput device
85-
_uinput.create(self.fd, name, vendor, product, version, bustype, absdata)
85+
_uinput.create(self.fd, name, vendor, product, version, bustype, absinfo)
8686

8787
#: an :class:`InputDevice <evdev.device.InputDevice>` instance
8888
# to the fake input device
@@ -164,9 +164,9 @@ def syn(self):
164164

165165
_uinput.write(self.fd, ecodes.EV_SYN, ecodes.SYN_REPORT, 0)
166166

167-
def capabilities(self, verbose=False, absdata=True):
167+
def capabilities(self, verbose=False, absinfo=True):
168168
'''See :func:`capabilities <evdev.device.InputDevice.capabilities>`.'''
169-
return self.device.capabilities(verbose, absdata)
169+
return self.device.capabilities(verbose, absinfo)
170170

171171
def _verify(self):
172172
''' Verify that an uinput device exists and is readable and writable

evdev/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ def resolve_ecodes(typecodemap, unknown='?'):
6161
{ ('EV_KEY', 1) : [('BTN_MOUSE', 272), ('BTN_RIGHT', 273), ('BTN_MIDDLE', 274)] }
6262
6363
If the typecodemap contains absolute axis info (wrapped in
64-
instances of `AbsData <evdev.device.AbsData>`) the result would
64+
instances of `AbsInfo <evdev.device.AbsInfo>`) the result would
6565
look like::
6666
67-
resove_ecodes({ 3 : [(0, AbsData(...))] })
68-
{ ('EV_ABS', 3L): [(('ABS_X', 0L), AbsData(...))] }
67+
resove_ecodes({ 3 : [(0, AbsInfo(...))] })
68+
{ ('EV_ABS', 3L): [(('ABS_X', 0L), AbsInfo(...))] }
6969
'''
7070

7171
for etype, codes in typecodemap.items():
@@ -79,7 +79,7 @@ def resolve_ecodes(typecodemap, unknown='?'):
7979

8080
res = []
8181
for i in codes:
82-
# elements with AbsData(), eg { 3 : [(0, AbsData(...)), (1, AbsData(...))] }
82+
# elements with AbsInfo(), eg { 3 : [(0, AbsInfo(...)), (1, AbsInfo(...))] }
8383
if isinstance(i, tuple):
8484
l = ((code_names[i[0]], i[0]), i[1]) if i[0] in code_names \
8585
else ((unknown, i[0]), i[1])

tests/test_uinput.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ def test_abs_values(c):
6161
c['events'] = {
6262
e.EV_KEY : [e.KEY_A, e.KEY_B],
6363
e.EV_ABS : [(e.ABS_X, (0, 255, 0, 0)),
64-
(e.ABS_Y, device.AbsData(0, 255, 5, 10))],
64+
(e.ABS_Y, device.AbsInfo(0, 255, 5, 10))],
6565
}
6666

6767
with uinput.UInput(**c) as ui:
6868
c = ui.capabilities()
69-
assert c[e.EV_ABS][0] == (0L, device.AbsData(min=0, max=255, fuzz=0, flat=0))
70-
assert c[e.EV_ABS][1] == (1L, device.AbsData(min=0, max=255, fuzz=5, flat=10))
69+
assert c[e.EV_ABS][0] == (0L, device.AbsInfo(min=0, max=255, fuzz=0, flat=0))
70+
assert c[e.EV_ABS][1] == (1L, device.AbsInfo(min=0, max=255, fuzz=5, flat=10))
7171

7272
c = ui.capabilities(verbose=True)
73-
assert c[('EV_ABS', 3)][0] == (('ABS_X', 0L), device.AbsData(min=0, max=255, fuzz=0, flat=0))
73+
assert c[('EV_ABS', 3)][0] == (('ABS_X', 0L), device.AbsInfo(min=0, max=255, fuzz=0, flat=0))
7474

75-
c = ui.capabilities(verbose=False, absdata=False)
75+
c = ui.capabilities(verbose=False, absinfo=False)
7676
assert c[e.EV_ABS] == list((0, 1))
7777

7878
def test_write(c):

0 commit comments

Comments
 (0)