Debian Codegraph @

src/evdev/eventio.py

raw
1 import fcntl
2 import functools
3 import os
4 import select
5 from typing import Iterator, Callable
6
7 from . import _input, _uinput, ecodes
8 from .events import InputEvent
9
10
11 # --------------------------------------------------------------------------
12 class EvdevError(Exception):
13 pass
14
15
16 class EventIO:
17 """
18 Base class for reading and writing input events.
19
20 This class is used by :class:`InputDevice` and :class:`UInput`.
21
22 - On, :class:`InputDevice` it used for reading user-generated events (e.g.
23 key presses, mouse movements) and writing feedback events (e.g. leds,
24 beeps).
25
26 - On, :class:`UInput` it used for writing user-generated events (e.g.
27 key presses, mouse movements) and reading feedback events (e.g. leds,
28 beeps).
29 """
30
31 def fileno(self) -> int:
32 """
33 Return the file descriptor to the open event device. This makes
34 it possible to pass instances directly to :func:`select.select()` and
35 :class:`asyncore.file_dispatcher`.
36 """
37 return self.fd
38
39 def read_loop(self) -> Iterator[InputEvent]:
40 """
41 Enter an endless :func:`select.select()` loop that yields input events.
42 """
43
44 while True:
45 r, w, x = select.select([self.fd], [], [])
46 for event in self.read():
47 yield event
48
49 def read_one(self) -> InputEvent | None:
50 """
51 Read and return a single input event as an instance of
52 :class:`InputEvent <evdev.events.InputEvent>`.
53
54 Return ``None`` if there are no pending input events.
55 """
56
57 # event -> (sec, usec, type, code, val)
58 event = _input.device_read(self.fd)
59
60 if event:
61 return InputEvent(*event)
62
63 def read(self) -> Iterator[InputEvent]:
64 """
65 Read multiple input events from device. Return a generator object that
66 yields :class:`InputEvent <evdev.events.InputEvent>` instances. Raises
67 `BlockingIOError` if there are no available events at the moment.
68 """
69
70 # events -> ((sec, usec, type, code, val), ...)
71 events = _input.device_read_many(self.fd)
72
73 for event in events:
74 yield InputEvent(*event)
75
76 # pylint: disable=no-self-argument
77 def need_write(func: Callable) -> Callable:
78 """
79 Decorator that raises :class:`EvdevError` if there is no write access to the
80 input device.
81 """
82
83 @functools.wraps(func)
84 def wrapper(*args):
85 fd = args[0].fd
86 if fcntl.fcntl(fd, fcntl.F_GETFL) & os.O_RDWR:
87 # pylint: disable=not-callable
88 return func(*args)
89 msg = 'no write access to device "%s"' % args[0].path
90 raise EvdevError(msg)
91
92 return wrapper
93
94 def write_event(self, event: InputEvent):
95 """
96 Inject an input event into the input subsystem. Events are
97 queued until a synchronization event is received.
98
99 Arguments
100 ---------
101 event: InputEvent
102 InputEvent instance or an object with an ``event`` attribute
103 (:class:`KeyEvent <evdev.events.KeyEvent>`, :class:`RelEvent
104 <evdev.events.RelEvent>` etc).
105
106 Example
107 -------
108 >>> ev = InputEvent(1334414993, 274296, ecodes.EV_KEY, ecodes.KEY_A, 1)
109 >>> ui.write_event(ev)
110 """
111
112 if hasattr(event, "event"):
113 event = event.event
114
115 self.write(event.type, event.code, event.value)
116
117 @need_write
118 def write(self, etype: int, code: int, value: int):
119 """
120 Inject an input event into the input subsystem. Events are
121 queued until a synchronization event is received.
122
123 Arguments
124 ---------
125 etype
126 event type (e.g. ``EV_KEY``).
127
128 code
129 event code (e.g. ``KEY_A``).
130
131 value
132 event value (e.g. 0 1 2 - depends on event type).
133
134 Example
135 ---------
136 >>> ui.write(e.EV_KEY, e.KEY_A, 1) # key A - down
137 >>> ui.write(e.EV_KEY, e.KEY_A, 0) # key A - up
138 """
139
140 _uinput.write(self.fd, etype, code, value)
141
142 def syn(self) -> None:
143 """
144 Inject a ``SYN_REPORT`` event into the input subsystem. Events
145 queued by :func:`write()` will be fired. If possible, events
146 will be merged into an 'atomic' event.
147 """
148
149 self.write(ecodes.EV_SYN, ecodes.SYN_REPORT, 0)
150
151 def close(self) -> None:
152 pass