def main(): signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGQUIT, signal_handler) signal.signal(signal.SIGIO, config_changed_handler) # sends signal if config is changed (or really anything in the config directory) fd = os.open(paths.config_dir, os.O_RDONLY) fcntl.fcntl(fd, fcntl.F_SETSIG, 0) fcntl.fcntl(fd, fcntl.F_NOTIFY, fcntl.DN_MODIFY | fcntl.DN_CREATE | fcntl.DN_MULTISHOT) # To see if config exists config_reader.read() dev, endpoint, USB_TIMEOUT, USB_IF = usb_and_keyboard_device_init.init_g910_keyboard( ) while program_running: try: usb.util.claim_interface(dev, USB_IF) # log.debug("reading control values") control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT) usb.util.release_interface(dev, USB_IF) if control: b = bytearray(control) if b in command_bytearray.commands.values(): key = list(command_bytearray.commands.keys())[list( command_bytearray.commands.values()).index(b)] log.debug(f"Pressed {key}, bytecode: {b}") emitKeys(device, key) elif b[:3] in (bytearray(b'\x11\xff\x0f'), bytearray(b'\x11\xff\x10'), bytearray(b'\x11\xff\xff')): #Suppress warnings on these values, these are return values from LEDs being set. pass else: log.warning(str(b) + ' no match') except SystemExit: sys.exit(0) except Exception as e: if e.args[0] == 110: pass elif e.args[0] == 19 or e.args[0] == 5: try: dev, endpoint, USB_TIMEOUT, USB_IF = usb_and_keyboard_device_init.init_g910_keyboard( ) except: pass else: log.error("ERROR:" + str(e)) time.sleep(0.001)
def resolve_config(key): config = config_reader.read() if key not in config.keys(): log.info(key + " pressed, unbound in config, doing nothing!") return lambda _: None key_config: dict = config[key] command = key_config.get("hotkey_type", "nothing") try: command = hotkey_type.type[command] except KeyError: raise Exception( "hotkey_type: \"" + command + "\" for key " + key + " not known! hotkey_types can only be: nothing, typeout, shortcut and run!" ) if command == 0: log.info(key + " pressed, typing out: " + repr(key_config["do"])) return lambda device: execute_writing(key_config["do"], device) if command == 1: log.info(key + " pressed, pressing: " + key_config["do"]) return lambda device: execute_hotkey(key_config["do"], device) if command == 2: log.info(key + " pressed, running: " + key_config["do"]) return lambda _: execute_command(key_config["do"]) if command == -1: log.info(key + " pressed, doing nothing!") return lambda _: None
def execute_writing(string_to_write: str, device): keyboard.writeout(string_to_write, config_reader.read()['keyboard_mapping'], device)
def execute_hotkey(string_for_hotkey: str, device): keyboard.shortcut(string_for_hotkey, config_reader.read()['keyboard_mapping'], device)