@@ -121,21 +121,37 @@ def __init__(self,
121
121
if not events :
122
122
events = {ecodes .EV_KEY : ecodes .keys .keys ()}
123
123
124
- # The min, max, fuzz and flat values for the absolute axis for
125
- # a given code.
126
- absinfo = []
127
-
128
124
self ._verify ()
129
125
130
126
#: Write-only, non-blocking file descriptor to the uinput device node.
131
127
self .fd = _uinput .open (devnode )
132
128
129
+ # Prepare the list of events for passing to _uinput.enable and _uinput.setup.
130
+ absinfo , prepared_events = self ._prepare_events (events )
131
+
133
132
# Set phys name
134
133
_uinput .set_phys (self .fd , phys )
135
134
135
+ for etype , code in prepared_events :
136
+ _uinput .enable (self .fd , etype , code )
137
+
136
138
_uinput .setup (self .fd , name , vendor , product , version , bustype , absinfo )
137
139
138
- # Set device capabilities.
140
+ # Create the uinput device.
141
+ _uinput .create (self .fd )
142
+
143
+ self .dll = ctypes .CDLL (_uinput .__file__ )
144
+ self .dll ._uinput_begin_upload .restype = ctypes .c_int
145
+ self .dll ._uinput_end_upload .restype = ctypes .c_int
146
+
147
+ #: An :class:`InputDevice <evdev.device.InputDevice>` instance
148
+ #: for the fake input device. ``None`` if the device cannot be
149
+ #: opened for reading and writing.
150
+ self .device = self ._find_device ()
151
+
152
+ def _prepare_events (self , events ):
153
+ '''Prepare events for passing to _uinput.enable and _uinput.setup'''
154
+ absinfo , prepared_events = [], []
139
155
for etype , codes in events .items ():
140
156
for code in codes :
141
157
# Handle max, min, fuzz, flat.
@@ -148,21 +164,8 @@ def __init__(self,
148
164
f .extend ([0 ] * (6 - len (code [1 ])))
149
165
absinfo .append (f )
150
166
code = code [0 ]
151
-
152
- # TODO: remove a lot of unnecessary packing/unpacking
153
- _uinput .enable (self .fd , etype , code )
154
-
155
- # Create the uinput device.
156
- _uinput .create (self .fd )
157
-
158
- self .dll = ctypes .CDLL (_uinput .__file__ )
159
- self .dll ._uinput_begin_upload .restype = ctypes .c_int
160
- self .dll ._uinput_end_upload .restype = ctypes .c_int
161
-
162
- #: An :class:`InputDevice <evdev.device.InputDevice>` instance
163
- #: for the fake input device. ``None`` if the device cannot be
164
- #: opened for reading and writing.
165
- self .device = self ._find_device ()
167
+ prepared_events .append ((etype , code ))
168
+ return absinfo , prepared_events
166
169
167
170
def __enter__ (self ):
168
171
return self
@@ -219,29 +222,25 @@ def begin_upload(self, effect_id):
219
222
upload .effect_id = effect_id
220
223
221
224
if self .dll ._uinput_begin_upload (self .fd , ctypes .byref (upload )):
222
- raise UInputError ('Failed to begin uinput upload: ' +
223
- os .strerror ())
225
+ raise UInputError ('Failed to begin uinput upload: ' + os .strerror ())
224
226
225
227
return upload
226
228
227
229
def end_upload (self , upload ):
228
230
if self .dll ._uinput_end_upload (self .fd , ctypes .byref (upload )):
229
- raise UInputError ('Failed to end uinput upload: ' +
230
- os .strerror ())
231
+ raise UInputError ('Failed to end uinput upload: ' + os .strerror ())
231
232
232
233
def begin_erase (self , effect_id ):
233
234
erase = ff .UInputErase ()
234
235
erase .effect_id = effect_id
235
236
236
237
if self .dll ._uinput_begin_erase (self .fd , ctypes .byref (erase )):
237
- raise UInputError ('Failed to begin uinput erase: ' +
238
- os .strerror ())
238
+ raise UInputError ('Failed to begin uinput erase: ' + os .strerror ())
239
239
return erase
240
240
241
241
def end_erase (self , erase ):
242
242
if self .dll ._uinput_end_erase (self .fd , ctypes .byref (erase )):
243
- raise UInputError ('Failed to end uinput erase: ' +
244
- os .strerror ())
243
+ raise UInputError ('Failed to end uinput erase: ' + os .strerror ())
245
244
246
245
def _verify (self ):
247
246
'''
0 commit comments