Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions evdev/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def grab(self):

Warning
-------
Grabbing an already grabbed device will raise an ``IOError``.
Grabbing an already grabbed device will raise an ``OSError``.
'''

_input.ioctl_EVIOCGRAB(self.fd, 1)
Expand All @@ -321,7 +321,7 @@ def ungrab(self):
Warning
-------
Releasing an already released device will raise an
``IOError('Invalid argument')``.
``OSError('Invalid argument')``.
'''

_input.ioctl_EVIOCGRAB(self.fd, 0)
Expand Down
18 changes: 9 additions & 9 deletions evdev/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ device_read(PyObject *self, PyObject *args)
return Py_None;
}

PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ device_read_many(PyObject *self, PyObject *args)
ssize_t nread = read(fd, event, event_size*64);

if (nread < 0) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(event_list);
return NULL;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ ioctl_capabilities(PyObject *self, PyObject *args)
return capabilities;

on_err:
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -243,7 +243,7 @@ ioctl_devinfo(PyObject *self, PyObject *args)
name, phys, uniq);

on_err:
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -261,7 +261,7 @@ ioctl_EVIOCGABS(PyObject *self, PyObject *args)
memset(&absinfo, 0, sizeof(absinfo));
ret = ioctl(fd, EVIOCGABS(ev_code), &absinfo);
if (ret == -1) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -296,7 +296,7 @@ ioctl_EVIOCSABS(PyObject *self, PyObject *args)

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

Expand Down Expand Up @@ -362,7 +362,7 @@ ioctl_EVIOCGRAB(PyObject *self, PyObject *args)

ret = ioctl(fd, EVIOCGRAB, (intptr_t)flag);
if (ret != 0) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -485,7 +485,7 @@ upload_effect(PyObject *self, PyObject *args)

ret = ioctl(fd, EVIOCSFF, &effect);
if (ret != 0) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -504,7 +504,7 @@ erase_effect(PyObject *self, PyObject *args)
long ff_id = PyLong_AsLong(ff_id_obj);
ret = ioctl(fd, EVIOCRMFF, ff_id);
if (ret != 0) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down
41 changes: 31 additions & 10 deletions evdev/uinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ uinput_open(PyObject *self, PyObject *args)

int fd = open(devnode, O_RDWR | O_NONBLOCK);
if (fd < 0) {
PyErr_SetString(PyExc_IOError, "could not open uinput device in write mode");
PyErr_SetString(PyExc_OSError, "could not open uinput device in write mode");
return NULL;
}

Expand All @@ -73,7 +73,7 @@ uinput_set_phys(PyObject *self, PyObject *args)

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -93,10 +93,28 @@ uinput_set_prop(PyObject *self, PyObject *args)

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

static PyObject *
uinput_get_sysname(PyObject *self, PyObject *args)
{
int fd;
char sysname[64];

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

if (ioctl(fd, UI_GET_SYSNAME(sizeof(sysname)), &sysname) < 0)
goto on_err;
Comment thread
sezanzeb marked this conversation as resolved.
Resolved

return Py_BuildValue("s", &sysname);

on_err:
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

// Different kernel versions have different device setup methods. You can read
// more about it here:
Expand Down Expand Up @@ -157,7 +175,7 @@ uinput_setup(PyObject *self, PyObject *args) {

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -206,7 +224,7 @@ uinput_setup(PyObject *self, PyObject *args) {

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
#endif
Expand All @@ -227,7 +245,7 @@ uinput_create(PyObject *self, PyObject *args)

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -241,7 +259,7 @@ uinput_close(PyObject *self, PyObject *args)
if (!ret) return NULL;

if (_uinput_close(fd) < 0) {
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -269,8 +287,8 @@ uinput_write(PyObject *self, PyObject *args)

if (write(fd, &event, sizeof(event)) != sizeof(event)) {
// @todo: elaborate
// PyErr_SetString(PyExc_IOError, "error writing event to uinput device");
PyErr_SetFromErrno(PyExc_IOError);
// PyErr_SetString(PyExc_OSError, "error writing event to uinput device");
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -312,7 +330,7 @@ uinput_enable_event(PyObject *self, PyObject *args)

on_err:
_uinput_close(fd);
PyErr_SetFromErrno(PyExc_IOError);
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down Expand Up @@ -361,6 +379,9 @@ static PyMethodDef MethodTable[] = {
{ "set_phys", uinput_set_phys, METH_VARARGS,
"Set physical path"},

{ "get_sysname", uinput_get_sysname, METH_VARARGS,
"Obtain the sysname of the uinput device."},

{ "set_prop", uinput_set_prop, METH_VARARGS,
"Set device input property"},

Expand Down
74 changes: 72 additions & 2 deletions evdev/uinput.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# encoding: utf-8

import os
import platform
import re
import stat
import time
from collections import defaultdict
Expand Down Expand Up @@ -159,7 +161,7 @@ def __init__(self,
#: An :class:`InputDevice <evdev.device.InputDevice>` instance
#: for the fake input device. ``None`` if the device cannot be
#: opened for reading and writing.
self.device = self._find_device()
self.device = self._find_device(self.fd)

def _prepare_events(self, events):
'''Prepare events for passing to _uinput.enable and _uinput.setup'''
Expand Down Expand Up @@ -281,11 +283,79 @@ def _verify(self):
msg = 'uinput device name must not be longer than {} characters'
raise UInputError(msg.format(_uinput.maxnamelen))

def _find_device(self):
def _find_device(self, fd):
'''
Tries to find the device node. Will delegate this task to one of
several platform-specific functions.
'''
if platform.system() == 'Linux':
try:
sysname = _uinput.get_sysname(fd)
return self._find_device_linux(sysname)
except OSError:
# UI_GET_SYSNAME returned an error code. We're likely dealing with
# an old kernel. Guess the device based on the filesystem.
pass

# If we're not running or Linux or the above method fails for any reason,
# use the generic fallback method.
return self._find_device_fallback()

def _find_device_linux(self, sysname):
'''
Tries to find the device node when running on Linux.
'''

syspath = f'/sys/devices/virtual/input/{sysname}'

# The sysfs entry for event devices should contain exactly one folder
# whose name matches the format "event[0-9]+". It is then assumed that
# the device node in /dev/input uses the same name.
regex = re.compile('event[0-9]+')
for entry in os.listdir(syspath):
Comment thread
sezanzeb marked this conversation as resolved.
Resolved
if regex.fullmatch(entry):
device_path = f'/dev/input/{entry}'
break
else: # no break
raise FileNotFoundError()

# It is possible that there is some delay before /dev/input/event* shows
# up on old systems that do not use devtmpfs, so if the device cannot be
# found, wait for a short amount and then try again once.
try:
return device.InputDevice(device_path)
except FileNotFoundError:
time.sleep(0.1)
Comment thread
sezanzeb marked this conversation as resolved.
Resolved
return device.InputDevice(device_path)

def _find_device_fallback(self):
Comment thread
sezanzeb marked this conversation as resolved.
Resolved
'''
Tries to find the device node when UI_GET_SYSNAME is not available or
we're running on a system sufficiently exotic that we do not know how
to interpret its return value.
'''
#:bug: the device node might not be immediately available
time.sleep(0.1)

# There could also be another device with the same name already present,
# make sure to select the newest one.
# Strictly speaking, we cannot be certain that everything returned by list_devices()
# ends at event[0-9]+: it might return something like "/dev/input/events_all". Find
# the devices that have the expected structure and extract their device number.
Comment thread
KarsMulder marked this conversation as resolved.
Resolved
path_number_pairs = []
regex = re.compile('/dev/input/event([0-9]+)')
for path in util.list_devices('/dev/input/'):
regex_match = regex.fullmatch(path)
if not regex_match:
continue
device_number = int(regex_match[1])
path_number_pairs.append((path, device_number))

# The modification date of the devnode is not reliable unfortunately, so we
# are sorting by the number in the name
path_number_pairs.sort(key=lambda pair: pair[1], reverse=True)
Comment thread
KarsMulder marked this conversation as resolved.
Resolved

for (path, _) in path_number_pairs:
d = device.InputDevice(path)
if d.name == self.name:
return d