Skip to content

Commit 94e139e

Browse files
committed
add device.leds and _input.get_sw_led_snd
1 parent 16d9ca6 commit 94e139e

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

evdev/device.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def capabilities(self, verbose=False, absinfo=True):
141141
{ ('EV_KEY', 1) : [('BTN_MOUSE', 272), ('BTN_RIGHT', 273), ('BTN_MIDDLE', 273)],
142142
('EV_REL', 2) : [('REL_X', 0), ('REL_Y', 0), ('REL_HWHEEL', 6), ('REL_WHEEL', 8)] }
143143
144-
Unknown codes or types will be resolved to '?'.
144+
Unknown codes or types will be resolved to ``'?'``.
145145
146146
If ``absinfo`` is ``True``, the list of capabilities will also
147147
include absolute axis information (``absmin``, ``absmax``,
@@ -162,8 +162,26 @@ def capabilities(self, verbose=False, absinfo=True):
162162
else:
163163
return self._capabilities(absinfo)
164164

165+
def leds(self, verbose=False):
166+
'''
167+
Returns currently set LED keys. Example::
168+
169+
[0, 1, 8, 9]
170+
171+
If ``verbose`` is ``True``, event codes will be resolved to
172+
their names. Unknown codes will be resolved to ``'?'``. Example::
173+
174+
[('LED_NUML', 0), ('LED_CAPSL', 1), ('LED_MISC', 8), ('LED_MAIL', 9)]
175+
176+
'''
177+
leds = _input.get_sw_led_snd(self.fd, ecodes.EV_LED)
178+
if verbose:
179+
return [(ecodes.LED[l] if l in ecodes.LED else '?', l) for l in leds]
180+
181+
return leds
182+
165183
def __eq__(self, o):
166-
''' Two devices are considered equal if their :data:`info` attributes are equal. '''
184+
'''Two devices are considered equal if their :data:`info` attributes are equal.'''
167185
return self.info == o.info
168186

169187
def __str__(self):

evdev/input.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,44 @@ ioctl_EVIOCGRAB(PyObject *self, PyObject *args)
293293
}
294294

295295

296+
// todo: this function needs a better name
297+
static PyObject *
298+
get_sw_led_snd(PyObject *self, PyObject *args)
299+
{
300+
int i, max, fd, evtype, ret;
301+
PyObject* res = PyList_New(0);
302+
303+
ret = PyArg_ParseTuple(args, "ii", &fd, &evtype);
304+
if (!ret) return NULL;
305+
306+
if (evtype == EV_LED)
307+
max = LED_MAX;
308+
else if (evtype == EV_SW)
309+
max = SW_MAX;
310+
else if (evtype == EV_SND)
311+
max = SND_MAX;
312+
313+
char* bits = (char*) malloc(max/8);
314+
memset(bits, 0, sizeof(bits));
315+
316+
if (evtype == EV_LED)
317+
ret = ioctl(fd, EVIOCGLED(sizeof(bits)), bits);
318+
else if (evtype == EV_SW)
319+
ret = ioctl(fd, EVIOCGSW(sizeof(bits)), bits);
320+
else if (evtype == EV_SND)
321+
ret = ioctl(fd, EVIOCGSND(sizeof(bits)), bits);
322+
323+
for (i=0 ; i<max ; i++) {
324+
if (test_bit(bits, i)) {
325+
PyList_Append(res, Py_BuildValue("i", i));
326+
}
327+
}
328+
329+
free(bits);
330+
return res;
331+
}
332+
333+
296334
static PyMethodDef MethodTable[] = {
297335
{ "unpack", event_unpack, METH_VARARGS, "unpack a single input event" },
298336
{ "ioctl_devinfo", ioctl_devinfo, METH_VARARGS, "fetch input device info" },
@@ -301,6 +339,7 @@ static PyMethodDef MethodTable[] = {
301339
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS},
302340
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS},
303341
{ "ioctl_EVIOCGRAB", ioctl_EVIOCGRAB, METH_VARARGS},
342+
{ "get_sw_led_snd", get_sw_led_snd, METH_VARARGS},
304343
{ "device_read", device_read, METH_VARARGS, "read an input event from a device" },
305344
{ "device_read_many", device_read_many, METH_VARARGS, "read all available input events from a device" },
306345

0 commit comments

Comments
 (0)