Skip to content

Commit a246054

Browse files
committed
Support setting physical path ("phys") on UInput
1 parent 5475ec8 commit a246054

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

evdev/uinput.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ uinput_open(PyObject *self, PyObject *args)
4747
}
4848

4949

50+
static PyObject *
51+
uinput_set_phys(PyObject *self, PyObject *args)
52+
{
53+
int fd;
54+
const char* phys;
55+
56+
int ret = PyArg_ParseTuple(args, "is", &fd, &phys);
57+
if (!ret) return NULL;
58+
59+
if (ioctl(fd, UI_SET_PHYS, phys) < 0)
60+
goto on_err;
61+
62+
Py_RETURN_NONE;
63+
64+
on_err:
65+
_uinput_close(fd);
66+
PyErr_SetFromErrno(PyExc_IOError);
67+
return NULL;
68+
}
69+
70+
5071
static PyObject *
5172
uinput_create(PyObject *self, PyObject *args) {
5273
int fd, len, i, abscode;
@@ -205,6 +226,9 @@ static PyMethodDef MethodTable[] = {
205226
{ "enable", uinput_enable_event, METH_VARARGS,
206227
"Enable a type of event."},
207228

229+
{ "set_phys", uinput_set_phys, METH_VARARGS,
230+
"Set physical path"},
231+
208232
{ NULL, NULL, 0, NULL}
209233
};
210234

evdev/uinput.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from evdev.events import InputEvent
1010
from evdev.eventio import EventIO
1111

12+
1213
class UInputError(Exception):
1314
pass
1415

@@ -28,7 +29,7 @@ def __init__(self,
2829
events=None,
2930
name='py-evdev-uinput',
3031
vendor=0x1, product=0x1, version=0x1, bustype=0x3,
31-
devnode='/dev/uinput'):
32+
devnode='/dev/uinput', phys='py-evdev-uinput'):
3233
'''
3334
Arguments
3435
---------
@@ -52,6 +53,9 @@ def __init__(self,
5253
bustype
5354
bustype identifier.
5455
56+
phys
57+
physical path.
58+
5559
Note
5660
----
5761
If you do not specify any events, the uinput device will be able
@@ -63,6 +67,7 @@ def __init__(self,
6367
self.product = product #: Device product identifier.
6468
self.version = version #: Device version identifier.
6569
self.bustype = bustype #: Device bustype - e.g. ``BUS_USB``.
70+
self.phys = phys #: Uinput device physical path.
6671
self.devnode = devnode #: Uinput device node - e.g. ``/dev/uinput/``.
6772

6873
if not events:
@@ -76,6 +81,9 @@ def __init__(self,
7681

7782
#: Write-only, non-blocking file descriptor to the uinput device node.
7883
self.fd = _uinput.open(devnode)
84+
85+
# Set phys name
86+
_uinput.set_phys(self.fd, phys)
7987

8088
# Set device capabilities.
8189
for etype, codes in events.items():
@@ -108,17 +116,17 @@ def __exit__(self, type, value, tb):
108116
def __repr__(self):
109117
# TODO:
110118
v = (repr(getattr(self, i)) for i in
111-
('name', 'bustype', 'vendor', 'product', 'version'))
119+
('name', 'bustype', 'vendor', 'product', 'version', 'phys'))
112120
return '{}({})'.format(self.__class__.__name__, ', '.join(v))
113121

114122
def __str__(self):
115-
msg = ('name "{}", bus "{}", vendor "{:04x}", product "{:04x}", version "{:04x}"\n'
123+
msg = ('name "{}", bus "{}", vendor "{:04x}", product "{:04x}", version "{:04x}", phys "{}"\n'
116124
'event types: {}')
117125

118126
evtypes = [i[0] for i in self.capabilities(True).keys()]
119127
msg = msg.format(self.name, ecodes.BUS[self.bustype],
120128
self.vendor, self.product,
121-
self.version, ' '.join(evtypes))
129+
self.version, self.phys, ' '.join(evtypes))
122130

123131
return msg
124132

0 commit comments

Comments
 (0)