Skip to content

Commit 8be8e57

Browse files
docs: generate API docs (#4)
Co-authored-by: haithium <128622475+haithium@users.noreply.github.com>
1 parent 5cb4c30 commit 8be8e57

5 files changed

Lines changed: 1027 additions & 0 deletions

File tree

docs/api/device.md

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
---
2+
description: "One Linux input event returned by `Device:read()`."
3+
---
4+
5+
# `device`
6+
7+
One Linux input event returned by `Device:read()`.
8+
9+
## Functions
10+
11+
| Function | Description |
12+
| --------------------------------------------- | --------------------------------------------------------------------- |
13+
| [`close()`](#fn-close) | Close the device. |
14+
| [`events()`](#fn-events) | Return an iterator that waits for and yields input events one by one. |
15+
| [`fd()`](#fn-fd) | Return the underlying Linux file descriptor. |
16+
| [`flush()`](#fn-flush) | Drain queued events and return how many were discarded. |
17+
| [`get_repeat()`](#fn-get-repeat) | Return the current auto-repeat delay and period in milliseconds. |
18+
| [`grab()`](#fn-grab) | Take exclusive control of the input device. |
19+
| [`is_device(value)`](#fn-is-device) | Return whether a value is an `evdev.Device` instance. |
20+
| [`is_open()`](#fn-is-open) | Return whether this device handle still has an open file descriptor. |
21+
| [`open(path)`](#fn-open) | Open an input device by path. |
22+
| [`poll()`](#fn-poll) | Wait in the kernel until this device has input available. |
23+
| [`read()`](#fn-read) | Read one input event. Returns `nil` when no event is queued. |
24+
| [`set_repeat(delay, period)`](#fn-set-repeat) | Set the auto-repeat delay and period in milliseconds. |
25+
| [`ungrab()`](#fn-ungrab) | Release exclusive control of the input device. |
26+
27+
<a id="fn-close"></a>
28+
29+
### `close()`
30+
31+
Close the device.
32+
33+
**Return**:
34+
35+
- `ok` (`boolean`): `true` when the device closes successfully.
36+
- `err` (`string?`): Error message on failure.
37+
38+
**Example**:
39+
40+
```lua
41+
local Device = evdev.device.open
42+
local dev = assert(Device("/dev/input/eventX"))
43+
44+
print(dev:is_open()) --> true
45+
dev:close()
46+
print(dev:is_open()) --> false
47+
```
48+
49+
<a id="fn-events"></a>
50+
51+
### `events()`
52+
53+
Return an iterator that waits for and yields input events one by one.
54+
55+
**Return**:
56+
57+
- `evdev.event?` (`fun():`)
58+
59+
**Example**:
60+
61+
```lua
62+
local Device = evdev.device.open
63+
local dev = assert(Device("/dev/input/eventX"))
64+
65+
for e in dev:events() do
66+
if e.type == ecodes.EV_KEY then
67+
print(e.code, e.value)
68+
end
69+
end
70+
```
71+
72+
<a id="fn-fd"></a>
73+
74+
### `fd()`
75+
76+
Return the underlying Linux file descriptor.
77+
78+
**Return**:
79+
80+
- `fd` (`evdev.fd?`): Linux file descriptor.
81+
82+
**Example**:
83+
84+
```lua
85+
local Device = evdev.device.open
86+
local dev = assert(Device("/dev/input/eventX"))
87+
88+
local fd = dev:fd()
89+
print(fd)
90+
```
91+
92+
<a id="fn-flush"></a>
93+
94+
### `flush()`
95+
96+
Drain queued events and return how many were discarded.
97+
98+
This is useful after grabbing a device when you want to ignore any stale events
99+
that were already queued.
100+
101+
**Return**:
102+
103+
- `count` (`integer?`): Number of discarded events.
104+
- `err` (`string?`): Error message on failure.
105+
106+
**Example**:
107+
108+
```lua
109+
local Device = evdev.device.open
110+
local dev = assert(Device("/dev/input/eventX"))
111+
112+
assert(dev:grab())
113+
-- Move the mouse or press keys during the sleep.
114+
115+
local dropped = assert(dev:flush())
116+
print("discarded", dropped, "stale events")
117+
```
118+
119+
<a id="fn-get-repeat"></a>
120+
121+
### `get_repeat()`
122+
123+
Return the current auto-repeat delay and period in milliseconds.
124+
125+
**Return**:
126+
127+
- `delay` (`integer?`): Initial delay before repeating (milliseconds).
128+
- `period` (`integer?`): Interval between repeats (milliseconds).
129+
- `err` (`string?`): Error message on failure.
130+
131+
**Example**:
132+
133+
```lua
134+
local Device = evdev.device.open
135+
local dev = assert(Device("/dev/input/eventX"))
136+
137+
local delay, period, err = dev:get_repeat()
138+
assert(delay, err)
139+
print(delay, period)
140+
```
141+
142+
<a id="fn-grab"></a>
143+
144+
### `grab()`
145+
146+
Take exclusive control of the input device.
147+
148+
**Return**:
149+
150+
- `ok` (`true?`): `true` when the device is grabbed successfully.
151+
- `err` (`string?`): Error message on failure.
152+
153+
**Example**:
154+
155+
```lua
156+
local Device = evdev.device.open
157+
local dev = assert(Device("/dev/input/eventX"))
158+
assert(dev:grab())
159+
```
160+
161+
<a id="fn-is-device"></a>
162+
163+
### `is_device(value)`
164+
165+
Return whether a value is an `evdev.Device` instance.
166+
167+
**Parameters**:
168+
169+
- `value` (`any`)
170+
171+
**Return**:
172+
173+
- **value** (`boolean`)
174+
175+
**Example**:
176+
177+
```lua
178+
local Device = evdev.device.open
179+
local is_device = evdev.device.is_device
180+
181+
local dev = assert(Device("/dev/input/eventX"))
182+
print(is_device(dev)) --> true
183+
print(is_device({})) --> false
184+
```
185+
186+
<a id="fn-is-open"></a>
187+
188+
### `is_open()`
189+
190+
Return whether this device handle still has an open file descriptor.
191+
192+
**Return**:
193+
194+
- `isOpen` (`boolean`): `true` when the device is still open.
195+
196+
**Example**:
197+
198+
```lua
199+
local Device = evdev.device.open
200+
local dev = assert(Device("/dev/input/eventX"))
201+
202+
if dev:is_open()
203+
then dev:close()
204+
end
205+
```
206+
207+
<a id="fn-open"></a>
208+
209+
### `open(path)`
210+
211+
Open an input device by path.
212+
213+
**Parameters**:
214+
215+
- `path` (`evdev.path`)
216+
217+
**Return**:
218+
219+
- `dev` (`evdev.Device?`): Open input device.
220+
- `err` (`string?`): Error message on failure.
221+
222+
**Example**:
223+
224+
```lua
225+
local Device = evdev.device.open
226+
local dev = assert(Device("/dev/input/eventX"))
227+
```
228+
229+
<a id="fn-poll"></a>
230+
231+
### `poll()`
232+
233+
Wait in the kernel until this device has input available.
234+
235+
This does not spin the CPU. It returns when `read()` can fetch at least one
236+
queued event.
237+
238+
**Return**:
239+
240+
- `ready` (`boolean?`): `true` when input is ready to read.
241+
- `err` (`string?`): Error message on failure.
242+
243+
**Example**:
244+
245+
```lua
246+
local Device = evdev.device.open
247+
local ecodes = evdev.ecodes
248+
249+
local dev = assert(Device("/dev/input/eventX"))
250+
251+
-- This is the manual form of `dev:events()`.
252+
while true do
253+
if assert(dev:poll()) then
254+
local e = assert(dev:read())
255+
if e.type == ecodes.EV_KEY then
256+
print(e.code, e.value)
257+
end
258+
end
259+
end
260+
```
261+
262+
<a id="fn-read"></a>
263+
264+
### `read()`
265+
266+
Read one input event. Returns `nil` when no event is queued.
267+
268+
**Return**:
269+
270+
- `event` (`evdev.event?`): Next queued input event.
271+
- `err` (`string?`): Error message on failure.
272+
273+
**Example**:
274+
275+
```lua
276+
local Device = evdev.device.open
277+
local dev = assert(Device("/dev/input/eventX"))
278+
279+
-- This is the manual form of `dev:events()`.
280+
while true do
281+
if assert(dev:poll()) then
282+
local e = assert(dev:read())
283+
if e.type == ecodes.EV_KEY then
284+
print(e.code, e.value)
285+
end
286+
end
287+
end
288+
```
289+
290+
<a id="fn-set-repeat"></a>
291+
292+
### `set_repeat(delay, period)`
293+
294+
Set the auto-repeat delay and period in milliseconds.
295+
296+
**Parameters**:
297+
298+
- `delay` (`integer`): Initial delay before repeating (milliseconds).
299+
- `period` (`integer`): Interval between repeats (milliseconds).
300+
301+
**Return**:
302+
303+
- `ok` (`true?`): `true` when the repeat settings are updated successfully.
304+
- `err` (`string?`): Error message on failure.
305+
306+
**Example**:
307+
308+
```lua
309+
local Device = evdev.device.open
310+
local dev = assert(Device("/dev/input/eventX"))
311+
312+
local delay, period, err = dev:get_repeat()
313+
assert(delay, err)
314+
print(delay, period)
315+
316+
assert(dev:set_repeat(300, 40))
317+
print(dev:get_repeat())
318+
```
319+
320+
<a id="fn-ungrab"></a>
321+
322+
### `ungrab()`
323+
324+
Release exclusive control of the input device.
325+
326+
**Return**:
327+
328+
- `ok` (`true?`): `true` when the grab is released successfully.
329+
- `err` (`string?`): Error message on failure.
330+
331+
**Example**:
332+
333+
```lua
334+
local Device = evdev.device.open
335+
local dev = assert(Device("/dev/input/eventX"))
336+
assert(dev:grab())
337+
assert(dev:ungrab())
338+
```

0 commit comments

Comments
 (0)