Implement config changes for mode switching and key holding

This commit is contained in:
NeoTheFox 2021-02-09 19:57:55 +03:00
parent b8c16ed06d
commit ae291f6406

View File

@ -2,10 +2,12 @@
import os import os
import time import time
import signal import signal
import configparser
from _xdo_cffi import ffi, lib from _xdo_cffi import ffi, lib
CONFIG_FILE_PATH = os.path.expanduser('~/.config/huion_keys.conf') CONFIG_FILE_PATH = os.path.expanduser('~/.config/huion_keys.conf')
CONFIG = configparser.ConfigParser()
TABLET_MODELS = { TABLET_MODELS = {
"Kamvas Pro (2019)": "256c:006e", "Kamvas Pro (2019)": "256c:006e",
@ -13,8 +15,14 @@ TABLET_MODELS = {
} }
BUTTON_BINDINGS = {} BUTTON_BINDINGS = {}
BUTTON_BINDINGS_HOLD = {}
CYCLE_BUTTON = None
CYCLE_MODE = 1
CYCLE_MODES = 1
DIAL_MODES = {}
def main(): def main():
global CYCLE_MODES, CYCLE_MODE, CYCLE_BUTTON
xdo = lib.xdo_new(ffi.NULL) xdo = lib.xdo_new(ffi.NULL)
if os.path.isfile(CONFIG_FILE_PATH): if os.path.isfile(CONFIG_FILE_PATH):
read_config(CONFIG_FILE_PATH) read_config(CONFIG_FILE_PATH)
@ -52,12 +60,21 @@ def main():
time.sleep(3) time.sleep(3)
break break
print("Got button %s" % (btn,)) print("Got button %s" % (btn,))
if btn == 3: if btn == CYCLE_BUTTON:
print("Pressing ctrl") CYCLE_MODE = CYCLE_MODE + 1
lib.xdo_send_keysequence_window_down(xdo, lib.CURRENTWINDOW, b'ctrl', 12000) if CYCLE_MODE > CYCLE_MODES:
CYCLE_MODE = 1
print("Cycling to mode %s" % (CYCLE_MODE,))
elif btn in DIAL_MODES[CYCLE_MODE]:
print("Sinding %s from Mode %d" % (DIAL_MODES[CYCLE_MODE][btn], CYCLE_MODE),)
lib.xdo_send_keysequence_window(
xdo, lib.CURRENTWINDOW, DIAL_MODES[CYCLE_MODE][btn], 1000)
elif btn in BUTTON_BINDINGS_HOLD:
print("Pressing %s" % (BUTTON_BINDINGS_HOLD[btn],))
lib.xdo_send_keysequence_window_down(xdo, lib.CURRENTWINDOW, BUTTON_BINDINGS_HOLD[btn], 12000)
get_button_release(hidraw) get_button_release(hidraw)
print("Releasing ctrl") print("Releasing %s" % (BUTTON_BINDINGS_HOLD[btn],))
lib.xdo_send_keysequence_window_up(xdo, lib.CURRENTWINDOW, b'ctrl', 12000) lib.xdo_send_keysequence_window_up(xdo, lib.CURRENTWINDOW, BUTTON_BINDINGS_HOLD[btn], 12000)
elif btn in BUTTON_BINDINGS: elif btn in BUTTON_BINDINGS:
print("Sending %s" % (BUTTON_BINDINGS[btn],)) print("Sending %s" % (BUTTON_BINDINGS[btn],))
lib.xdo_send_keysequence_window( lib.xdo_send_keysequence_window(
@ -80,29 +97,46 @@ def get_tablet_hidraw(device_id):
def read_config(config_file): def read_config(config_file):
with open(config_file, 'r') as config: global CYCLE_MODES, CYCLE_MODE, CYCLE_BUTTON
for line in config.readlines(): CONFIG.read(config_file)
# strip out any comment # It is still better for performance to pre-encode these values
if line.find('#') > -1: for binding in CONFIG['Bindings']:
line = line[:line.find('#')] if binding.isdigit():
if line.find('='): # store button configs with their 1-indexed ID
setting = line[:line.find('=')].strip() BUTTON_BINDINGS[int(binding)] = CONFIG['Bindings'][binding].encode('utf-8')
value = line[line.find('=')+1:].strip() elif binding == 'scroll_up':
if setting.isdigit(): BUTTON_BINDINGS['scroll_up'] = CONFIG['Bindings'][binding].encode('utf-8')
# store button configs with their 1-indexed ID elif binding == 'scroll_down':
BUTTON_BINDINGS[int(setting)] = value.encode('utf-8') BUTTON_BINDINGS['scroll_down'] = CONFIG['Bindings'][binding].encode('utf-8')
elif setting == 'scroll_up': elif binding == 'dial_cw':
BUTTON_BINDINGS['scroll_up'] = value.encode('utf-8') BUTTON_BINDINGS['dial_cw'] = CONFIG['Bindings'][binding].encode('utf-8')
elif setting == 'scroll_down': elif binding == 'dial_ccw':
BUTTON_BINDINGS['scroll_down'] = value.encode('utf-8') BUTTON_BINDINGS['dial_ccw'] = CONFIG['Bindings'][binding].encode('utf-8')
elif setting == 'dial_cw': elif binding == '':
BUTTON_BINDINGS['dial_cw'] = value.encode('utf-8') continue # ignore empty line
elif setting == 'dial_ccw': else:
BUTTON_BINDINGS['dial_ccw'] = value.encode('utf-8') print("[WARN] unrecognized regular binding '%s'" % (binding,))
elif setting == '': #Same, but for buttons that should be held down
continue # ignore empty line for binding in CONFIG['Hold']:
else: if binding.isdigit():
print("[WARN] unrecognized setting '%s'" % (setting,)) BUTTON_BINDINGS_HOLD[int(binding)] = CONFIG['Hold'][binding].encode('utf-8')
elif binding == '':
continue
else:
print ("[WARN] unrecognized hold binding '%s'" % (binding,))
# Assume that if cycle is assigned we have modes for now
if 'Dial' in CONFIG:
CYCLE_BUTTON = int(CONFIG['Dial']['cycle'])
for key in CONFIG:
if key.startswith("Mode"):
# Count the modes
mode = int(key.split(' ')[1])
if mode > CYCLE_MODES:
CYCLE_MODES = mode
DIAL_MODES[mode] = {}
for binding in CONFIG[key]:
DIAL_MODES[mode][binding] = CONFIG[key][binding].encode('utf-8')
def handle_reload_signal(signum, frame): def handle_reload_signal(signum, frame):
print("SIGUSR1 recieved - reloading config..") print("SIGUSR1 recieved - reloading config..")
@ -114,6 +148,7 @@ def create_default_config(config_file):
# use one line for each button you want to configure # use one line for each button you want to configure
# buttons that aren't in this file will be ignored by this program # buttons that aren't in this file will be ignored by this program
# (but may be handled by another driver) # (but may be handled by another driver)
[Bindings]
4=ctrl+s 4=ctrl+s
5=ctrl+z 5=ctrl+z
6=ctrl+shift+equal 6=ctrl+shift+equal
@ -122,9 +157,18 @@ def create_default_config(config_file):
16=Tab 16=Tab
scroll_up=bracketright scroll_up=bracketright
scroll_down=bracketleft scroll_down=bracketleft
#Buttons that should be held instead of instantly firing
[Hold]
3=ctrl
#Q620M Dial #Q620M Dial
[Dial]
cycle = 9
[Mode 1]
dial_cw=6 dial_cw=6
dial_ccw=4 dial_ccw=4
[Mode 2]
dial_cw=bracketright
dial_ccw=bracketleft
""") """)