1
- import collections
2
1
import contextlib
3
2
import os
3
+ from typing import NamedTuple , Tuple , Union
4
4
5
5
from . import _input , ecodes , util
6
6
10
10
from .eventio import EvdevError , EventIO
11
11
12
12
13
- # --------------------------------------------------------------------------
14
- _AbsInfo = collections .namedtuple ("AbsInfo" , ["value" , "min" , "max" , "fuzz" , "flat" , "resolution" ])
15
-
16
- _KbdInfo = collections .namedtuple ("KbdInfo" , ["delay" , "repeat" ])
17
-
18
- _DeviceInfo = collections .namedtuple ("DeviceInfo" , ["bustype" , "vendor" , "product" , "version" ])
19
-
20
-
21
- class AbsInfo (_AbsInfo ):
13
+ class AbsInfo (NamedTuple ):
22
14
"""Absolute axis information.
23
15
24
- A ``namedtuple`` used for storing absolute axis information -
16
+ A ``namedtuple`` with absolute axis information -
25
17
corresponds to the ``input_absinfo`` struct:
26
18
27
19
Attributes
@@ -57,11 +49,18 @@ class AbsInfo(_AbsInfo):
57
49
58
50
"""
59
51
52
+ value : int
53
+ min : int
54
+ max : int
55
+ fuzz : int
56
+ flat : int
57
+ resolution : int
58
+
60
59
def __str__ (self ):
61
- return "val {}, min {}, max {}, fuzz {}, flat {}, res {}" .format (* self )
60
+ return "value {}, min {}, max {}, fuzz {}, flat {}, res {}" .format (* self ) # pylint: disable=not-an-iterable
62
61
63
62
64
- class KbdInfo (_KbdInfo ):
63
+ class KbdInfo (NamedTuple ):
65
64
"""Keyboard repeat rate.
66
65
67
66
Attributes
@@ -74,11 +73,14 @@ class KbdInfo(_KbdInfo):
74
73
Keyboard repeat rate in characters per second.
75
74
"""
76
75
76
+ delay : int
77
+ repeat : int
78
+
77
79
def __str__ (self ):
78
- return "delay {}, repeat {}" .format (* self )
80
+ return "delay {}, repeat {}" .format (self . delay , self . repeat )
79
81
80
82
81
- class DeviceInfo (_DeviceInfo ):
83
+ class DeviceInfo (NamedTuple ):
82
84
"""
83
85
Attributes
84
86
----------
@@ -88,9 +90,14 @@ class DeviceInfo(_DeviceInfo):
88
90
version
89
91
"""
90
92
93
+ bustype : int
94
+ vendor : int
95
+ product : int
96
+ version : int
97
+
91
98
def __str__ (self ):
92
99
msg = "bus: {:04x}, vendor {:04x}, product {:04x}, version {:04x}"
93
- return msg .format (* self )
100
+ return msg .format (* self ) # pylint: disable=not-an-iterable
94
101
95
102
96
103
class InputDevice (EventIO ):
@@ -100,7 +107,7 @@ class InputDevice(EventIO):
100
107
101
108
__slots__ = ("path" , "fd" , "info" , "name" , "phys" , "uniq" , "_rawcapabilities" , "version" , "ff_effects_count" )
102
109
103
- def __init__ (self , dev ):
110
+ def __init__ (self , dev : Union [ str , bytes , os . PathLike ] ):
104
111
"""
105
112
Arguments
106
113
---------
@@ -111,15 +118,14 @@ def __init__(self, dev):
111
118
#: Path to input device.
112
119
self .path = dev if not hasattr (dev , "__fspath__" ) else dev .__fspath__ ()
113
120
114
- # Certain operations are possible only when the device is opened in
115
- # read-write mode.
121
+ # Certain operations are possible only when the device is opened in read-write mode.
116
122
try :
117
123
fd = os .open (dev , os .O_RDWR | os .O_NONBLOCK )
118
124
except OSError :
119
125
fd = os .open (dev , os .O_RDONLY | os .O_NONBLOCK )
120
126
121
127
#: A non-blocking file descriptor to the device file.
122
- self .fd = fd
128
+ self .fd : int = fd
123
129
124
130
# Returns (bustype, vendor, product, version, name, phys, capabilities).
125
131
info_res = _input .ioctl_devinfo (self .fd )
@@ -128,16 +134,16 @@ def __init__(self, dev):
128
134
self .info = DeviceInfo (* info_res [:4 ])
129
135
130
136
#: The name of the event device.
131
- self .name = info_res [4 ]
137
+ self .name : str = info_res [4 ]
132
138
133
139
#: The physical topology of the device.
134
- self .phys = info_res [5 ]
140
+ self .phys : str = info_res [5 ]
135
141
136
142
#: The unique identifier of the device.
137
- self .uniq = info_res [6 ]
143
+ self .uniq : str = info_res [6 ]
138
144
139
145
#: The evdev protocol version.
140
- self .version = _input .ioctl_EVIOCGVERSION (self .fd )
146
+ self .version : int = _input .ioctl_EVIOCGVERSION (self .fd )
141
147
142
148
#: The raw dictionary of device capabilities - see `:func:capabilities()`.
143
149
self ._rawcapabilities = _input .ioctl_capabilities (self .fd )
@@ -152,7 +158,7 @@ def __del__(self):
152
158
except (OSError , ImportError , AttributeError ):
153
159
pass
154
160
155
- def _capabilities (self , absinfo = True ):
161
+ def _capabilities (self , absinfo : bool = True ):
156
162
res = {}
157
163
158
164
for etype , _ecodes in self ._rawcapabilities .items ():
@@ -170,7 +176,7 @@ def _capabilities(self, absinfo=True):
170
176
171
177
return res
172
178
173
- def capabilities (self , verbose = False , absinfo = True ):
179
+ def capabilities (self , verbose : bool = False , absinfo : bool = True ):
174
180
"""
175
181
Return the event types that this device supports as a mapping of
176
182
supported event types to lists of handled event codes.
@@ -215,7 +221,7 @@ def capabilities(self, verbose=False, absinfo=True):
215
221
else :
216
222
return self ._capabilities (absinfo )
217
223
218
- def input_props (self , verbose = False ):
224
+ def input_props (self , verbose : bool = False ):
219
225
"""
220
226
Get device properties and quirks.
221
227
@@ -236,7 +242,7 @@ def input_props(self, verbose=False):
236
242
237
243
return props
238
244
239
- def leds (self , verbose = False ):
245
+ def leds (self , verbose : bool = False ):
240
246
"""
241
247
Return currently set LED keys.
242
248
@@ -257,7 +263,7 @@ def leds(self, verbose=False):
257
263
258
264
return leds
259
265
260
- def set_led (self , led_num , value ):
266
+ def set_led (self , led_num : int , value : int ):
261
267
"""
262
268
Set the state of the selected LED.
263
269
@@ -327,7 +333,7 @@ def grab_context(self):
327
333
yield
328
334
self .ungrab ()
329
335
330
- def upload_effect (self , effect ):
336
+ def upload_effect (self , effect : "ff.Effect" ):
331
337
"""
332
338
Upload a force feedback effect to a force feedback device.
333
339
"""
@@ -354,10 +360,10 @@ def repeat(self):
354
360
return KbdInfo (* _input .ioctl_EVIOCGREP (self .fd ))
355
361
356
362
@repeat .setter
357
- def repeat (self , value ):
363
+ def repeat (self , value : Tuple [ int , int ] ):
358
364
return _input .ioctl_EVIOCSREP (self .fd , * value )
359
365
360
- def active_keys (self , verbose = False ):
366
+ def active_keys (self , verbose : bool = False ):
361
367
"""
362
368
Return currently active keys.
363
369
@@ -380,7 +386,7 @@ def active_keys(self, verbose=False):
380
386
381
387
return active_keys
382
388
383
- def absinfo (self , axis_num ):
389
+ def absinfo (self , axis_num : int ):
384
390
"""
385
391
Return current :class:`AbsInfo` for input device axis
386
392
@@ -396,7 +402,7 @@ def absinfo(self, axis_num):
396
402
"""
397
403
return AbsInfo (* _input .ioctl_EVIOCGABS (self .fd , axis_num ))
398
404
399
- def set_absinfo (self , axis_num , value = None , min = None , max = None , fuzz = None , flat = None , resolution = None ):
405
+ def set_absinfo (self , axis_num : int , value = None , min = None , max = None , fuzz = None , flat = None , resolution = None ):
400
406
"""
401
407
Update :class:`AbsInfo` values. Only specified values will be overwritten.
402
408
0 commit comments