Skip to content

Commit d269505

Browse files
committed
Merge ioctl_EVIOCGKEYS and get_sw_led_snd into one function
1 parent 96ffac8 commit d269505

2 files changed

Lines changed: 31 additions & 47 deletions

File tree

‎evdev/device.py‎

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

33
import os
44
import fcntl
5+
56
from select import select
67
from collections import namedtuple
78

@@ -214,7 +215,7 @@ def leds(self, verbose=False):
214215
[('LED_NUML', 0), ('LED_CAPSL', 1), ('LED_MISC', 8), ('LED_MAIL', 9)]
215216
216217
'''
217-
leds = _input.get_sw_led_snd(self.fd, ecodes.EV_LED)
218+
leds = _input.ioctl_EVIOCG_bits(self.fd, ecodes.EV_LED)
218219
if verbose:
219220
return [(ecodes.LED[l] if l in ecodes.LED else '?', l) for l in leds]
220221

@@ -362,7 +363,7 @@ def active_keys(self, verbose=False):
362363
[('KEY_ESC', 1), ('KEY_LEFTSHIFT', 42)]
363364
364365
'''
365-
active_keys = _input.ioctl_EVIOCGKEY(self.fd)
366+
active_keys = _input.ioctl_EVIOCG_bits(self.fd, ecodes.EV_KEY)
366367
if verbose:
367368
return [(ecodes.KEY[k] if k in ecodes.KEY else '?', k) for k in active_keys]
368369

‎evdev/input.c‎

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -314,62 +314,46 @@ ioctl_EVIOCGRAB(PyObject *self, PyObject *args)
314314

315315

316316
static PyObject *
317-
ioctl_EVIOCGKEY(PyObject *self, PyObject *args)
317+
ioctl_EVIOCG_bits(PyObject *self, PyObject *args)
318318
{
319-
int fd, ret, key;
320-
char keys_bitmask[KEY_MAX/8 + 1];
321-
PyObject* res = PyList_New(0);
322-
323-
ret = PyArg_ParseTuple(args, "i", &fd);
324-
if (!ret) return NULL;
325-
326-
memset(&keys_bitmask, 0, sizeof(keys_bitmask));
327-
ret = ioctl(fd, EVIOCGKEY(sizeof(keys_bitmask)), keys_bitmask);
328-
if (ret < 0) {
329-
PyErr_SetFromErrno(PyExc_IOError);
330-
return NULL;
331-
}
332-
333-
for (key = 0; key < KEY_MAX; key++) {
334-
if (test_bit(keys_bitmask, key)) {
335-
PyList_Append(res, Py_BuildValue("i", key));
336-
}
337-
}
338-
339-
return res;
340-
}
341-
342-
343-
// todo: this function needs a better name
344-
static PyObject *
345-
get_sw_led_snd(PyObject *self, PyObject *args)
346-
{
347-
int i, max, fd, evtype, ret;
348-
PyObject* res = PyList_New(0);
319+
int max, fd, evtype, ret;
349320

350321
ret = PyArg_ParseTuple(args, "ii", &fd, &evtype);
351322
if (!ret) return NULL;
352323

353-
if (evtype == EV_LED)
354-
max = LED_MAX;
355-
else if (evtype == EV_SW)
356-
max = SW_MAX;
357-
else if (evtype == EV_SND)
358-
max = SND_MAX;
359-
else
324+
switch (evtype) {
325+
case EV_LED:
326+
max = LED_MAX; break;
327+
case EV_SND:
328+
max = SND_MAX; break;
329+
case EV_KEY:
330+
max = KEY_MAX; break;
331+
case EV_SW:
332+
max = SW_MAX; break;
333+
default:
360334
return NULL;
335+
}
361336

362337
char bytes[(max+7)/8];
363338
memset(bytes, 0, sizeof bytes);
364339

365-
if (evtype == EV_LED)
340+
switch (evtype) {
341+
case EV_LED:
366342
ret = ioctl(fd, EVIOCGLED(sizeof(bytes)), &bytes);
367-
else if (evtype == EV_SW)
368-
ret = ioctl(fd, EVIOCGSW(sizeof(bytes)), &bytes);
369-
else if (evtype == EV_SND)
343+
break;
344+
case EV_SND:
370345
ret = ioctl(fd, EVIOCGSND(sizeof(bytes)), &bytes);
346+
break;
347+
case EV_KEY:
348+
ret = ioctl(fd, EVIOCGKEY(sizeof(bytes)), &bytes);
349+
break;
350+
case EV_SW:
351+
ret = ioctl(fd, EVIOCGSW(sizeof(bytes)), &bytes);
352+
break;
353+
}
371354

372-
for (i=0 ; i<max ; i++) {
355+
PyObject* res = PyList_New(0);
356+
for (int i=0; i<max; i++) {
373357
if (test_bit(bytes, i)) {
374358
PyList_Append(res, Py_BuildValue("i", i));
375359
}
@@ -468,9 +452,8 @@ static PyMethodDef MethodTable[] = {
468452
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS},
469453
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS},
470454
{ "ioctl_EVIOCGRAB", ioctl_EVIOCGRAB, METH_VARARGS},
471-
{ "ioctl_EVIOCGKEY", ioctl_EVIOCGKEY, METH_VARARGS, "get global key state" },
472455
{ "ioctl_EVIOCGEFFECTS", ioctl_EVIOCGEFFECTS, METH_VARARGS, "fetch the number of effects the device can keep in its memory." },
473-
{ "get_sw_led_snd", get_sw_led_snd, METH_VARARGS},
456+
{ "ioctl_EVIOCG_bits", ioctl_EVIOCG_bits, METH_VARARGS, "get state of KEY|LED|SND|SW"},
474457
{ "device_read", device_read, METH_VARARGS, "read an input event from a device" },
475458
{ "device_read_many", device_read_many, METH_VARARGS, "read all available input events from a device" },
476459
{ "upload_effect", upload_effect, METH_VARARGS, "" },

0 commit comments

Comments
 (0)