|
4 | 4 | import stat |
5 | 5 | import time |
6 | 6 |
|
7 | | -from evdev import _uinput |
| 7 | +from evdev import _input, _uinput |
8 | 8 | from evdev import ecodes, util, device |
9 | | - |
| 9 | +from evdev.events import InputEvent |
10 | 10 |
|
11 | 11 | class UInputError(Exception): |
12 | 12 | pass |
@@ -121,6 +121,50 @@ def close(self): |
121 | 121 | _uinput.close(self.fd) |
122 | 122 | self.fd = -1 |
123 | 123 |
|
| 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 | + |
124 | 168 | def write_event(self, event): |
125 | 169 | ''' |
126 | 170 | Inject an input event into the input subsystem. Events are |
|
0 commit comments