@@ -121,21 +121,37 @@ def __init__(self,
121121 if not events :
122122 events = {ecodes .EV_KEY : ecodes .keys .keys ()}
123123
124- # The min, max, fuzz and flat values for the absolute axis for
125- # a given code.
126- absinfo = []
127-
128124 self ._verify ()
129125
130126 #: Write-only, non-blocking file descriptor to the uinput device node.
131127 self .fd = _uinput .open (devnode )
132128
129+ # Prepare the list of events for passing to _uinput.enable and _uinput.setup.
130+ absinfo , prepared_events = self ._prepare_events (events )
131+
133132 # Set phys name
134133 _uinput .set_phys (self .fd , phys )
135134
135+ for etype , code in prepared_events :
136+ _uinput .enable (self .fd , etype , code )
137+
136138 _uinput .setup (self .fd , name , vendor , product , version , bustype , absinfo )
137139
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 = [], []
139155 for etype , codes in events .items ():
140156 for code in codes :
141157 # Handle max, min, fuzz, flat.
@@ -148,21 +164,8 @@ def __init__(self,
148164 f .extend ([0 ] * (6 - len (code [1 ])))
149165 absinfo .append (f )
150166 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
166169
167170 def __enter__ (self ):
168171 return self
@@ -219,29 +222,25 @@ def begin_upload(self, effect_id):
219222 upload .effect_id = effect_id
220223
221224 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 ())
224226
225227 return upload
226228
227229 def end_upload (self , upload ):
228230 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 ())
231232
232233 def begin_erase (self , effect_id ):
233234 erase = ff .UInputErase ()
234235 erase .effect_id = effect_id
235236
236237 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 ())
239239 return erase
240240
241241 def end_erase (self , erase ):
242242 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 ())
245244
246245 def _verify (self ):
247246 '''
0 commit comments