Skip to content

Commit 809f95f

Browse files
committed
Expose get/set absinfo ioctl commands
Changing AbsInfo data in kernel is useful for setting min/max calibration for other programs and flatness/fuzz settings are useful for kernel side filtering of input events
1 parent 91710cd commit 809f95f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

evdev/device.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,53 @@ def fn(self):
377377
msg = 'Please use {0}.path instead of {0}.fn'.format(self.__class__.__name__)
378378
warnings.warn(msg, DeprecationWarning, stacklevel=2)
379379
return self.path
380+
381+
def get_absinfo(self, axis_num):
382+
"""
383+
Return current :class:`AbsInfo` for input device axis
384+
385+
Arguments
386+
---------
387+
axis_num : int
388+
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
389+
390+
Example
391+
-------
392+
393+
>>> device.get_absinfo(ecodes.ABS_X)
394+
AbsInfo(value=1501, min=-32768, max=32767, fuzz=0, flat=128, resolution=0)
395+
396+
"""
397+
return AbsInfo(*_input.ioctl_EVIOCGABS(self.fd, axis_num))
398+
399+
def set_absinfo(self, axis_num, value=None, min=None, max=None, fuzz=None, flat=None, resolution=None):
400+
"""
401+
Set AbsInfo values for input device.
402+
403+
Only values set will be overwritten.
404+
405+
See :class:`AbsInfo` for more info about the arguments
406+
407+
Arguments
408+
---------
409+
axis_num : int
410+
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
411+
412+
Example
413+
-------
414+
>>> device.set_absinfo(ecodes.ABS_X, min=-2000, max=2000)
415+
416+
You can also unpack AbsInfo tuple that will overwrite all values
417+
418+
>>> device.set_absinfo(ecodes.ABS_Y, *AbsInfo(0, -2000, 2000, 0, 15, 0))
419+
420+
421+
"""
422+
cur_absinfo = self.get_absinfo(axis_num)
423+
new_absinfo = AbsInfo(value if value else cur_absinfo.value,
424+
min if min else cur_absinfo.min,
425+
max if max else cur_absinfo.max,
426+
fuzz if fuzz else cur_absinfo.fuzz,
427+
flat if flat else cur_absinfo.flat,
428+
resolution if resolution else cur_absinfo.resolution)
429+
_input.ioctl_EVIOCSABS(self.fd, axis_num, new_absinfo)

evdev/input.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,63 @@ ioctl_devinfo(PyObject *self, PyObject *args)
242242
}
243243

244244

245+
static PyObject *
246+
ioctl_EVIOCGABS(PyObject *self, PyObject *args)
247+
{
248+
int fd, ev_code;
249+
struct input_absinfo absinfo;
250+
PyObject* py_absinfo = NULL;
251+
252+
int ret = PyArg_ParseTuple(args, "ii", &fd, &ev_code);
253+
if (!ret) return NULL;
254+
255+
memset(&absinfo, 0, sizeof(absinfo));
256+
ret = ioctl(fd, EVIOCGABS(ev_code), &absinfo);
257+
if (ret == -1) {
258+
PyErr_SetFromErrno(PyExc_IOError);
259+
return NULL;
260+
}
261+
262+
py_absinfo = Py_BuildValue("(iiiiii)",
263+
absinfo.value,
264+
absinfo.minimum,
265+
absinfo.maximum,
266+
absinfo.fuzz,
267+
absinfo.flat,
268+
absinfo.resolution);
269+
return py_absinfo;
270+
}
271+
272+
273+
static PyObject *
274+
ioctl_EVIOCSABS(PyObject *self, PyObject *args)
275+
{
276+
int fd, ev_code;
277+
struct input_absinfo absinfo;
278+
279+
int ret = PyArg_ParseTuple(args,
280+
"ii(iiiiii)",
281+
&fd,
282+
&ev_code,
283+
&absinfo.value,
284+
&absinfo.minimum,
285+
&absinfo.maximum,
286+
&absinfo.fuzz,
287+
&absinfo.flat,
288+
&absinfo.resolution);
289+
if (!ret) return NULL;
290+
291+
ret = ioctl(fd, EVIOCSABS(ev_code), &absinfo);
292+
if (ret == -1) {
293+
PyErr_SetFromErrno(PyExc_IOError);
294+
return NULL;
295+
}
296+
297+
Py_INCREF(Py_None);
298+
return Py_None;
299+
}
300+
301+
245302
static PyObject *
246303
ioctl_EVIOCGREP(PyObject *self, PyObject *args)
247304
{
@@ -453,6 +510,8 @@ erase_effect(PyObject *self, PyObject *args)
453510
static PyMethodDef MethodTable[] = {
454511
{ "ioctl_devinfo", ioctl_devinfo, METH_VARARGS, "fetch input device info" },
455512
{ "ioctl_capabilities", ioctl_capabilities, METH_VARARGS, "fetch input device capabilities" },
513+
{ "ioctl_EVIOCGABS", ioctl_EVIOCGABS, METH_VARARGS, "get input device absinfo"},
514+
{ "ioctl_EVIOCSABS", ioctl_EVIOCSABS, METH_VARARGS, "set input device absinfo"},
456515
{ "ioctl_EVIOCGREP", ioctl_EVIOCGREP, METH_VARARGS},
457516
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS},
458517
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS},

0 commit comments

Comments
 (0)