def test_controls(): j = Joystick(None, backend=StubJoystick) e1 = event_tuple(123, EventTypes.push) e2 = event_tuple(123, EventTypes.release) e3 = event_tuple(100, EventTypes.push) state = frozenset([123]), frozenset(), frozenset([100]) assert j.postprocess([e1, e2, e2, e3]) == states_tuple(*state)
def test_controls(): evt_queue = Queue() j = Joystick(evt_queue, backend='queue') evt_queue.put(event_tuple(123, EventTypes.push)) evt_queue.put(event_tuple(123, EventTypes.release)) evt_queue.put(event_tuple(100, EventTypes.push)) ch = ControlHandler() ch.register_controler(j) states_tuple = ch.poll_buffered(0) assert states_tuple.pushed == frozenset([123]) assert states_tuple.released == frozenset([]) assert states_tuple.held == frozenset([100])
def update(self): pushed = set() while not self.queue.empty(): button_id = self.queue.get_nowait() if button_id in pushed: continue pushed.add(button_id) self.events.append(event_tuple(button_id, EventTypes.push)) released_buttons = self.pushed - pushed for button_id in released_buttons: self.events.append(event_tuple(button_id, EventTypes.release)) self.pushed = (self.pushed | pushed) - released_buttons
def translate(self, event): """:type event: event_tuple""" def shift(button): new_button = button - self.scancode_offset logger.debug("shift %d -> %d", button, new_button) return new_button button, type = event new_button = shift(button) return event_tuple(new_button, type)
def reset_stale(self): if not self.last_key_at: return if time.time() - self.last_key_at < 30: return for btn_id, timestamp in self.keys_held.items(): if not timestamp: continue event = event_tuple(btn_id, EventTypes.release) self.events.append(event) self.keys_held[btn_id] = False
def update(self): while not self.queue.empty(): button_id, event_id = self.queue.get_nowait() event = EventTypes(event_id) logging.info("%s %s", button_id, event) self.events.append(event_tuple(button_id, event)) if event == EventTypes.push: timestamp = time.time() self.keys_held[button_id] = timestamp self.last_key_at = timestamp self.reset_stale()
def translate(self, event): """:type event: event_tuple""" def shift(button): new_button = button - self.scancode_offset logger.debug("shift %d -> %d", button, new_button) return new_button def remap(button): new_button = self.mapping.get(button, button) logger.debug("remap %d -> %d", button, new_button) return new_button button, type = event new_button = remap(shift(button)) return event_tuple(new_button, type)
def to_tuple(event): button = event.button.jbutton type = EventTypes.from_sdl(event.type) return event_tuple(button, type)
def to_tuple(event): type = EventTypes(event.value) button = event.code return event_tuple(button, type)
def get_events(self): e1 = event_tuple(4, EventTypes.push) e2 = event_tuple(4, EventTypes.release) return [e1, e2]