def test_reading_1(self): # a single event pending_events['device 1'] = [InputEvent(EV_ABS, ABS_HAT0X, 1)] keycode_reader.start_reading('device 1') wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_HAT0X, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_reading_ignore_up(self): pending_events['device 1'] = [ InputEvent(EV_KEY, CODE_1, 0, 10), InputEvent(EV_KEY, CODE_2, 1, 11), InputEvent(EV_KEY, CODE_3, 0, 12), ] keycode_reader.start_reading('device 1') time.sleep(0.1) self.assertEqual(keycode_reader.read(), (EV_KEY, CODE_2, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_ignore_value_2(self): # this is not a combination, because (EV_KEY CODE_3, 2) is ignored pending_events['device 1'] = [ InputEvent(EV_ABS, ABS_HAT0X, 1), InputEvent(EV_KEY, CODE_3, 2) ] keycode_reader.start_reading('device 1') wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_HAT0X, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_switch_device(self): pending_events['device 2'] = [InputEvent(EV_KEY, CODE_1, 1)] pending_events['device 1'] = [InputEvent(EV_KEY, CODE_3, 1)] keycode_reader.start_reading('device 2') time.sleep(EVENT_READ_TIMEOUT * 5) keycode_reader.start_reading('device 1') time.sleep(EVENT_READ_TIMEOUT * 5) self.assertEqual(keycode_reader.read(), (EV_KEY, CODE_3, 1)) self.assertEqual(keycode_reader.read(), None)
def test_prioritizing_3_normalize(self): # take the sign of -1234, just like in test_prioritizing_2_normalize pending_events['device 1'] = [ InputEvent(EV_ABS, ABS_HAT0X, -1234, 1234.0000), InputEvent(EV_ABS, ABS_HAT0Y, 0, 1234.0030) # ignored # this time don't release anything as well, but it's not # a combination because only one event is accepted ] keycode_reader.start_reading('device 1') wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_HAT0X, -1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_reading_ignore_duplicate_down(self): pipe = multiprocessing.Pipe() pipe[1].send(InputEvent(EV_ABS, ABS_Z, 1, 10)) keycode_reader._pipe = pipe self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_Z, 1)) self.assertEqual(keycode_reader.read(), None) pipe[1].send(InputEvent(EV_ABS, ABS_Z, 1, 10)) # still none self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_ignore_btn_left(self): # click events are ignored because overwriting them would render the # mouse useless, but a mouse is needed to stop the injection # comfortably. Furthermore, reading mouse events breaks clicking # around in the table. It can still be changed in the config files. pending_events['device 1'] = [ InputEvent(EV_KEY, BTN_LEFT, 1), InputEvent(EV_KEY, CODE_2, 1), InputEvent(EV_KEY, BTN_TOOL_DOUBLETAP, 1), ] keycode_reader.start_reading('device 1') time.sleep(0.1) self.assertEqual(keycode_reader.read(), (EV_KEY, CODE_2, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def consume_newest_keycode(self): """To capture events from keyboards, mice and gamepads.""" row, focused = self.get_focused_row() # the "event" event of Gtk.Window wouldn't trigger on gamepad # events, so it became a GLib timeout to periodically check kernel # events. # letting go of one of the keys of a combination won't just make # it return the leftover key, it will continue to return None because # they have already been read. key = keycode_reader.read() keys_pressed = keycode_reader.are_keys_pressed() if isinstance(focused, Gtk.ToggleButton) and not keys_pressed: row.release() return True if key is None: return True if key.is_problematic(): self.show_status( CTX_WARNING, 'ctrl, alt and shift may not combine properly', 'Your system will probably reinterpret combinations with ' + 'those after they are injected, and by doing so break them.') # inform the currently selected row about the new keycode row, focused = self.get_focused_row() if isinstance(focused, Gtk.ToggleButton): row.set_new_key(key) return True
def test_reads_joysticks(self): # if their purpose is "buttons" custom_mapping.set('gamepad.joystick.left_purpose', BUTTONS) pending_events['gamepad'] = [InputEvent(EV_ABS, ABS_Y, MAX_ABS)] keycode_reader.start_reading('gamepad') wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_Y, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1) keycode_reader._unreleased = {} custom_mapping.set('gamepad.joystick.left_purpose', MOUSE) pending_events['gamepad'] = [InputEvent(EV_ABS, ABS_Y, MAX_ABS)] keycode_reader.start_reading('gamepad') time.sleep(0.1) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 0)
def test_reading_2(self): # a combination of events pending_events['device 1'] = [ InputEvent(EV_KEY, CODE_1, 1, 10000.1234), InputEvent(EV_KEY, CODE_3, 1, 10001.1234), InputEvent(EV_ABS, ABS_HAT0X, -1, 10002.1234) ] keycode_reader.start_reading('device 1') # sending anything arbitrary does not stop the pipe keycode_reader._pipe[0].send((EV_KEY, 1234)) wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), ((EV_KEY, CODE_1, 1), (EV_KEY, CODE_3, 1), (EV_ABS, ABS_HAT0X, -1))) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 3)
def test_wrong_device(self): pending_events['device 1'] = [ InputEvent(EV_KEY, CODE_1, 1), InputEvent(EV_KEY, CODE_2, 1), InputEvent(EV_KEY, CODE_3, 1) ] keycode_reader.start_reading('device 2') time.sleep(EVENT_READ_TIMEOUT * 5) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 0)
def test_prioritizing_1(self): # filter the ABS_MISC events of the wacom intuos 5 out that come # with every button press. Or more general, prioritize them # based on the event type pending_events['device 1'] = [ InputEvent(EV_ABS, ABS_HAT0X, 1, 1234.0000), InputEvent(EV_ABS, ABS_HAT0X, 0, 1234.0001), InputEvent(EV_ABS, ABS_HAT0X, 1, 1235.0000), # ignored InputEvent(EV_ABS, ABS_HAT0X, 0, 1235.0001), InputEvent(EV_KEY, KEY_COMMA, 1, 1235.0010), InputEvent(EV_KEY, KEY_COMMA, 0, 1235.0011), InputEvent(EV_ABS, ABS_HAT0X, 1, 1235.0020), # ignored InputEvent(EV_ABS, ABS_HAT0X, 0, 1235.0021), # ignored InputEvent(EV_ABS, ABS_HAT0X, 1, 1236.0000) ] keycode_reader.start_reading('device 1') wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), (EV_ABS, ABS_HAT0X, 1)) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 1)
def test_prioritizing_2_normalize(self): # a value of 1234 becomes 1 in the reader in order to properly map # it. Value like that are usually some sort of continuous trigger # value and normal for some ev_abs events. custom_mapping.set('gamepad.joystick.left_purpose', BUTTONS) pending_events['gamepad'] = [ InputEvent(EV_ABS, ABS_HAT0X, 1, 1234.0000), InputEvent(EV_ABS, ABS_MISC, 1, 1235.0000), # ignored InputEvent(EV_ABS, ABS_Y, MAX_ABS, 1235.0010), InputEvent(EV_ABS, ABS_MISC, 1, 1235.0020), # ignored InputEvent(EV_ABS, ABS_MISC, 1, 1235.0030) # ignored # this time, don't release anything. the combination should # ignore stuff as well. ] keycode_reader.start_reading('gamepad') time.sleep(0.5) wait(keycode_reader._pipe[0].poll, 0.5) self.assertEqual(keycode_reader.read(), ((EV_ABS, ABS_HAT0X, 1), (EV_ABS, ABS_Y, 1))) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 2)
def test_keymapper_devices(self): # Don't read from keymapper devices, their keycodes are not # representative for the original key. As long as this is not # intentionally programmed it won't even do that. But it was at some # point. pending_events['key-mapper device 2'] = [ InputEvent(EV_KEY, CODE_1, 1), InputEvent(EV_KEY, CODE_2, 1), InputEvent(EV_KEY, CODE_3, 1) ] keycode_reader.start_reading('device 2') time.sleep(EVENT_READ_TIMEOUT * 5) self.assertEqual(keycode_reader.read(), None) self.assertEqual(len(keycode_reader._unreleased), 0)
def setUp(self): # verify that tearDown properly cleared the reader self.assertEqual(keycode_reader.read(), None)