Skip to content

Commit 9450820

Browse files
authored
Merge pull request #106 from artizirk/get_set_absinfo
Expose get/set absinfo ioctl commands
2 parents 0a30d1e + 809f95f commit 9450820

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

evdev/device.py

+50
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,53 @@ def fn(self):
398398
msg = 'Please use {0}.path instead of {0}.fn'.format(self.__class__.__name__)
399399
warnings.warn(msg, DeprecationWarning, stacklevel=2)
400400
return self.path
401+
402+
def get_absinfo(self, axis_num):
403+
"""
404+
Return current :class:`AbsInfo` for input device axis
405+
406+
Arguments
407+
---------
408+
axis_num : int
409+
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
410+
411+
Example
412+
-------
413+
414+
>>> device.get_absinfo(ecodes.ABS_X)
415+
AbsInfo(value=1501, min=-32768, max=32767, fuzz=0, flat=128, resolution=0)
416+
417+
"""
418+
return AbsInfo(*_input.ioctl_EVIOCGABS(self.fd, axis_num))
419+
420+
def set_absinfo(self, axis_num, value=None, min=None, max=None, fuzz=None, flat=None, resolution=None):
421+
"""
422+
Set AbsInfo values for input device.
423+
424+
Only values set will be overwritten.
425+
426+
See :class:`AbsInfo` for more info about the arguments
427+
428+
Arguments
429+
---------
430+
axis_num : int
431+
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
432+
433+
Example
434+
-------
435+
>>> device.set_absinfo(ecodes.ABS_X, min=-2000, max=2000)
436+
437+
You can also unpack AbsInfo tuple that will overwrite all values
438+
439+
>>> device.set_absinfo(ecodes.ABS_Y, *AbsInfo(0, -2000, 2000, 0, 15, 0))
440+
441+
442+
"""
443+
cur_absinfo = self.get_absinfo(axis_num)
444+
new_absinfo = AbsInfo(value if value else cur_absinfo.value,
445+
min if min else cur_absinfo.min,
446+
max if max else cur_absinfo.max,
447+
fuzz if fuzz else cur_absinfo.fuzz,
448+
flat if flat else cur_absinfo.flat,
449+
resolution if resolution else cur_absinfo.resolution)
450+
_input.ioctl_EVIOCSABS(self.fd, axis_num, new_absinfo)

evdev/input.c

+59
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,63 @@ ioctl_devinfo(PyObject *self, PyObject *args)
247247
}
248248

249249

250+
static PyObject *
251+
ioctl_EVIOCGABS(PyObject *self, PyObject *args)
252+
{
253+
int fd, ev_code;
254+
struct input_absinfo absinfo;
255+
PyObject* py_absinfo = NULL;
256+
257+
int ret = PyArg_ParseTuple(args, "ii", &fd, &ev_code);
258+
if (!ret) return NULL;
259+
260+
memset(&absinfo, 0, sizeof(absinfo));
261+
ret = ioctl(fd, EVIOCGABS(ev_code), &absinfo);
262+
if (ret == -1) {
263+
PyErr_SetFromErrno(PyExc_IOError);
264+
return NULL;
265+
}
266+
267+
py_absinfo = Py_BuildValue("(iiiiii)",
268+
absinfo.value,
269+
absinfo.minimum,
270+
absinfo.maximum,
271+
absinfo.fuzz,
272+
absinfo.flat,
273+
absinfo.resolution);
274+
return py_absinfo;
275+
}
276+
277+
278+
static PyObject *
279+
ioctl_EVIOCSABS(PyObject *self, PyObject *args)
280+
{
281+
int fd, ev_code;
282+
struct input_absinfo absinfo;
283+
284+
int ret = PyArg_ParseTuple(args,
285+
"ii(iiiiii)",
286+
&fd,
287+
&ev_code,
288+
&absinfo.value,
289+
&absinfo.minimum,
290+
&absinfo.maximum,
291+
&absinfo.fuzz,
292+
&absinfo.flat,
293+
&absinfo.resolution);
294+
if (!ret) return NULL;
295+
296+
ret = ioctl(fd, EVIOCSABS(ev_code), &absinfo);
297+
if (ret == -1) {
298+
PyErr_SetFromErrno(PyExc_IOError);
299+
return NULL;
300+
}
301+
302+
Py_INCREF(Py_None);
303+
return Py_None;
304+
}
305+
306+
250307
static PyObject *
251308
ioctl_EVIOCGREP(PyObject *self, PyObject *args)
252309
{
@@ -485,6 +542,8 @@ ioctl_EVIOCGPROP(PyObject *self, PyObject *args)
485542
static PyMethodDef MethodTable[] = {
486543
{ "ioctl_devinfo", ioctl_devinfo, METH_VARARGS, "fetch input device info" },
487544
{ "ioctl_capabilities", ioctl_capabilities, METH_VARARGS, "fetch input device capabilities" },
545+
{ "ioctl_EVIOCGABS", ioctl_EVIOCGABS, METH_VARARGS, "get input device absinfo"},
546+
{ "ioctl_EVIOCSABS", ioctl_EVIOCSABS, METH_VARARGS, "set input device absinfo"},
488547
{ "ioctl_EVIOCGREP", ioctl_EVIOCGREP, METH_VARARGS},
489548
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS},
490549
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS},

0 commit comments

Comments
 (0)