Skip to content

Commit

Permalink
Merge pull request #106 from artizirk/get_set_absinfo
Browse files Browse the repository at this point in the history
Expose get/set absinfo ioctl commands
  • Loading branch information
gvalkov authored Jan 11, 2020
2 parents 0a30d1e + 809f95f commit 9450820
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
50 changes: 50 additions & 0 deletions evdev/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,53 @@ def fn(self):
msg = 'Please use {0}.path instead of {0}.fn'.format(self.__class__.__name__)
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return self.path

def get_absinfo(self, axis_num):
"""
Return current :class:`AbsInfo` for input device axis
Arguments
---------
axis_num : int
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
Example
-------
>>> device.get_absinfo(ecodes.ABS_X)
AbsInfo(value=1501, min=-32768, max=32767, fuzz=0, flat=128, resolution=0)
"""
return AbsInfo(*_input.ioctl_EVIOCGABS(self.fd, axis_num))

def set_absinfo(self, axis_num, value=None, min=None, max=None, fuzz=None, flat=None, resolution=None):
"""
Set AbsInfo values for input device.
Only values set will be overwritten.
See :class:`AbsInfo` for more info about the arguments
Arguments
---------
axis_num : int
EV_ABS keycode (example :attr:`ecodes.ABS_X`)
Example
-------
>>> device.set_absinfo(ecodes.ABS_X, min=-2000, max=2000)
You can also unpack AbsInfo tuple that will overwrite all values
>>> device.set_absinfo(ecodes.ABS_Y, *AbsInfo(0, -2000, 2000, 0, 15, 0))
"""
cur_absinfo = self.get_absinfo(axis_num)
new_absinfo = AbsInfo(value if value else cur_absinfo.value,
min if min else cur_absinfo.min,
max if max else cur_absinfo.max,
fuzz if fuzz else cur_absinfo.fuzz,
flat if flat else cur_absinfo.flat,
resolution if resolution else cur_absinfo.resolution)
_input.ioctl_EVIOCSABS(self.fd, axis_num, new_absinfo)
59 changes: 59 additions & 0 deletions evdev/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,63 @@ ioctl_devinfo(PyObject *self, PyObject *args)
}


static PyObject *
ioctl_EVIOCGABS(PyObject *self, PyObject *args)
{
int fd, ev_code;
struct input_absinfo absinfo;
PyObject* py_absinfo = NULL;

int ret = PyArg_ParseTuple(args, "ii", &fd, &ev_code);
if (!ret) return NULL;

memset(&absinfo, 0, sizeof(absinfo));
ret = ioctl(fd, EVIOCGABS(ev_code), &absinfo);
if (ret == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}

py_absinfo = Py_BuildValue("(iiiiii)",
absinfo.value,
absinfo.minimum,
absinfo.maximum,
absinfo.fuzz,
absinfo.flat,
absinfo.resolution);
return py_absinfo;
}


static PyObject *
ioctl_EVIOCSABS(PyObject *self, PyObject *args)
{
int fd, ev_code;
struct input_absinfo absinfo;

int ret = PyArg_ParseTuple(args,
"ii(iiiiii)",
&fd,
&ev_code,
&absinfo.value,
&absinfo.minimum,
&absinfo.maximum,
&absinfo.fuzz,
&absinfo.flat,
&absinfo.resolution);
if (!ret) return NULL;

ret = ioctl(fd, EVIOCSABS(ev_code), &absinfo);
if (ret == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}

Py_INCREF(Py_None);
return Py_None;
}


static PyObject *
ioctl_EVIOCGREP(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -485,6 +542,8 @@ ioctl_EVIOCGPROP(PyObject *self, PyObject *args)
static PyMethodDef MethodTable[] = {
{ "ioctl_devinfo", ioctl_devinfo, METH_VARARGS, "fetch input device info" },
{ "ioctl_capabilities", ioctl_capabilities, METH_VARARGS, "fetch input device capabilities" },
{ "ioctl_EVIOCGABS", ioctl_EVIOCGABS, METH_VARARGS, "get input device absinfo"},
{ "ioctl_EVIOCSABS", ioctl_EVIOCSABS, METH_VARARGS, "set input device absinfo"},
{ "ioctl_EVIOCGREP", ioctl_EVIOCGREP, METH_VARARGS},
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS},
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS},
Expand Down

0 comments on commit 9450820

Please sign in to comment.