From 4fcc3337e21089b50a1034fa05c69d4bd7b84640 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 23 Mar 2021 20:19:24 +0100 Subject: Make mouse demo work --- fw/hexnoise.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'fw/hexnoise.py') diff --git a/fw/hexnoise.py b/fw/hexnoise.py index 2313f8f..549f583 100755 --- a/fw/hexnoise.py +++ b/fw/hexnoise.py @@ -6,6 +6,7 @@ import sys from contextlib import contextmanager, suppress, wraps import hashlib import secrets +import struct import serial from cobs import cobs @@ -336,14 +337,19 @@ class NoiseEngine: raise ProtocolError('Invalid report type') def uinput_passthrough(self): - with uinput.Device(KeyMapper.ALL_KEYS) as ui: + mouse_foo = [uinput.BTN_LEFT, uinput.BTN_RIGHT, uinput.BTN_MIDDLE, + uinput.REL_X, uinput.REL_Y, uinput.REL_WHEEL] + with uinput.Device(KeyMapper.ALL_KEYS + mouse_foo) as ui: old_kcs = set() for msg_type, payload in self.receive_loop(): report_len, *report = payload - if report_len != 8: - raise ValueError('Unsupported report length', report_len) + + report = bytes(report[:report_len]) if msg_type is ReportType.KEYBOARD: + if report_len != 8: + raise ValueError('Unsupported report length', report_len) + modbyte, _reserved, *keycodes = report import binascii keys = { *KeyMapper.map_modifiers(modbyte), *KeyMapper.map_regulars(keycodes) } @@ -358,8 +364,26 @@ class NoiseEngine: old_kcs = keys elif msg_type is ReportType.MOUSE: - # FIXME unhandled - pass + if report_len < 3 or report_len > 8: + raise ValueError('Unsupported report length', report_len) + import binascii + _report_type, buttons, a, b, c, w, _2 = report + x = ((b&0x0f)<<8) | a + y = ((b&0xf0) >> 4) | (c<<4) + if x >= 2048: + x -= 4096 + if y >= 2048: + y -= 4096 + if w >= 128: + w -= 256 + #print('got mouse report', binascii.hexlify(report), buttons, x, y, w) + ui.emit(uinput.BTN_LEFT, bool(buttons&1), syn=False) + ui.emit(uinput.BTN_MIDDLE, bool(buttons&4), syn=False) + ui.emit(uinput.BTN_RIGHT, bool(buttons&2), syn=False) + ui.emit(uinput.REL_X, x, syn=False) + ui.emit(uinput.REL_Y, y, syn=False) + ui.emit(uinput.REL_WHEEL, w, syn=False) + ui.syn() if __name__ == '__main__': import argparse -- cgit