99from .events import InputEvent , event_factory , KeyEvent , RelEvent , AbsEvent , SynEvent
1010
1111
12- def list_devices (input_device_dir : Union [str , bytes , os .PathLike ] = "/dev/input" ) -> List [str ]:
13- """List readable character devices in ``input_device_dir``."""
12+ def list_devices (input_device_dir : Union [str , bytes , os .PathLike ] = "/dev/input" , writable : bool = True ) -> List [str ]:
13+ """List readable (and optionally writable) character devices in ``input_device_dir``.
14+
15+ Arguments
16+ ---------
17+ input_device_dir : str|bytes|PathLike
18+ Path to the input device directory. Default is ``/dev/input``.
19+ writable : bool
20+ If True (default), only list devices that are both readable and
21+ writable. If False, list all readable devices.
22+ """
1423
1524 fns = glob .glob ("{}/event*" .format (input_device_dir ))
16- return list (filter (is_device , fns ))
25+ return list (filter (lambda fn : is_device (fn , writable = writable ), fns ))
26+
1727
28+ def is_device (fn : Union [str , bytes , os .PathLike ], writable : bool = True ) -> bool :
29+ """Check if ``fn`` is a readable (and optionally writable) character device.
1830
19- def is_device (fn : Union [str , bytes , os .PathLike ]) -> bool :
20- """Check if ``fn`` is a readable and writable character device."""
31+ Arguments
32+ ---------
33+ fn : str|bytes|PathLike
34+ Path to the device file.
35+ writable : bool
36+ If True (default), also check for write access. If False, only
37+ check for read access.
38+ """
2139
2240 if not os .path .exists (fn ):
2341 return False
@@ -26,7 +44,8 @@ def is_device(fn: Union[str, bytes, os.PathLike]) -> bool:
2644 if not stat .S_ISCHR (m ):
2745 return False
2846
29- if not os .access (fn , os .R_OK | os .W_OK ):
47+ access = os .R_OK | os .W_OK if writable else os .R_OK
48+ if not os .access (fn , access ):
3049 return False
3150
3251 return True
0 commit comments