Skip to content

Commit af11cb2

Browse files
committed
Merge pull request gvalkov#52 from paulo-raca/uinput_phys
Support setting physical path ("phys") on UInput
2 parents ea2da71 + a246054 commit af11cb2

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

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
@@ -14,6 +14,7 @@
1414
from evdev.eventio import EventIO
1515

1616

17+
1718
class UInputError(Exception):
1819
pass
1920

@@ -33,7 +34,7 @@ def __init__(self,
3334
events=None,
3435
name='py-evdev-uinput',
3536
vendor=0x1, product=0x1, version=0x1, bustype=0x3,
36-
devnode='/dev/uinput'):
37+
devnode='/dev/uinput', phys='py-evdev-uinput'):
3738
'''
3839
Arguments
3940
---------
@@ -57,6 +58,9 @@ def __init__(self,
5758
bustype
5859
bustype identifier.
5960
61+
phys
62+
physical path.
63+
6064
Note
6165
----
6266
If you do not specify any events, the uinput device will be able
@@ -68,6 +72,7 @@ def __init__(self,
6872
self.product = product #: Device product identifier.
6973
self.version = version #: Device version identifier.
7074
self.bustype = bustype #: Device bustype - e.g. ``BUS_USB``.
75+
self.phys = phys #: Uinput device physical path.
7176
self.devnode = devnode #: Uinput device node - e.g. ``/dev/uinput/``.
7277

7378
if not events:
@@ -81,6 +86,9 @@ def __init__(self,
8186

8287
#: Write-only, non-blocking file descriptor to the uinput device node.
8388
self.fd = _uinput.open(devnode)
89+
90+
# Set phys name
91+
_uinput.set_phys(self.fd, phys)
8492

8593
# Set device capabilities.
8694
for etype, codes in events.items():
@@ -113,17 +121,17 @@ def __exit__(self, type, value, tb):
113121
def __repr__(self):
114122
# TODO:
115123
v = (repr(getattr(self, i)) for i in
116-
('name', 'bustype', 'vendor', 'product', 'version'))
124+
('name', 'bustype', 'vendor', 'product', 'version', 'phys'))
117125
return '{}({})'.format(self.__class__.__name__, ', '.join(v))
118126

119127
def __str__(self):
120-
msg = ('name "{}", bus "{}", vendor "{:04x}", product "{:04x}", version "{:04x}"\n'
128+
msg = ('name "{}", bus "{}", vendor "{:04x}", product "{:04x}", version "{:04x}", phys "{}"\n'
121129
'event types: {}')
122130

123131
evtypes = [i[0] for i in self.capabilities(True).keys()]
124132
msg = msg.format(self.name, ecodes.BUS[self.bustype],
125133
self.vendor, self.product,
126-
self.version, ' '.join(evtypes))
134+
self.version, self.phys, ' '.join(evtypes))
127135

128136
return msg
129137

0 commit comments

Comments
 (0)