Skip to content

Commit 96ffac8

Browse files
committed
Merge pull request gvalkov#46 from paulo-raca/uinput-read
Support for read() operation on UInput
2 parents 765b79c + d599ca9 commit 96ffac8

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎evdev/uinput.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ uinput_open(PyObject *self, PyObject *args)
3737
int ret = PyArg_ParseTuple(args, "s", &devnode);
3838
if (!ret) return NULL;
3939

40-
int fd = open(devnode, O_WRONLY | O_NONBLOCK);
40+
int fd = open(devnode, O_RDWR | O_NONBLOCK);
4141
if (fd < 0) {
4242
PyErr_SetString(PyExc_IOError, "could not open uinput device in write mode");
4343
return NULL;

‎evdev/uinput.py‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import stat
55
import time
66

7-
from evdev import _uinput
7+
from evdev import _input, _uinput
88
from evdev import ecodes, util, device
9-
9+
from evdev.events import InputEvent
1010

1111
class UInputError(Exception):
1212
pass
@@ -121,6 +121,50 @@ def close(self):
121121
_uinput.close(self.fd)
122122
self.fd = -1
123123

124+
def fileno(self):
125+
'''
126+
Return the file descriptor to the open event device. This
127+
makes it possible to pass pass ``InputDevice`` instances
128+
directly to :func:`select.select()` and
129+
:class:`asyncore.file_dispatcher`.'''
130+
131+
return self.fd
132+
133+
def read_one(self):
134+
'''
135+
Read and return a single input event as an instance of
136+
:class:`InputEvent <evdev.events.InputEvent>`.
137+
138+
Return ``None`` if there are no pending input events.
139+
'''
140+
141+
# event -> (sec, usec, type, code, val)
142+
event = _input.device_read(self.fd)
143+
144+
if event:
145+
return InputEvent(*event)
146+
147+
def read_loop(self):
148+
'''Enter an endless ``select()`` loop that yields input events.'''
149+
150+
while True:
151+
r, w, x = select([self.fd], [], [])
152+
for event in self.read():
153+
yield event
154+
155+
def read(self):
156+
'''
157+
Read multiple input events from device. Return a generator object that
158+
yields :class:`InputEvent <evdev.events.InputEvent>` instances. Raises
159+
`BlockingIOError` if there are no available events at the moment.
160+
'''
161+
162+
# events -> [(sec, usec, type, code, val), ...]
163+
events = _input.device_read_many(self.fd)
164+
165+
for i in events:
166+
yield InputEvent(*i)
167+
124168
def write_event(self, event):
125169
'''
126170
Inject an input event into the input subsystem. Events are

0 commit comments

Comments
 (0)