Skip to content

Instantly share code, notes, and snippets.

@Saren-Arterius
Created March 11, 2019 11:05
Show Gist options
  • Select an option

  • Save Saren-Arterius/a596ed1b23b9a47fc816b2360fa3573c to your computer and use it in GitHub Desktop.

Select an option

Save Saren-Arterius/a596ed1b23b9a47fc816b2360fa3573c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# from PIL import Image
import subprocess
import uinput
INPUT_EVENT_ID = 13
X_MAX = 1216
Y_MAX = 680
slots = [None] * 10
# 2, 3, 1, 6, 4, 5, 0
# touchpad
clusters = {
uinput.KEY_Z: (30, 480),
uinput.KEY_X: (410, 600),
uinput.KEY_C: (700, 580),
uinput.KEY_V: (1150, 480),
uinput.KEY_S: (300, 140),
uinput.KEY_D: (630, 150),
uinput.KEY_F: (980, 150)
}
"""
# wacom
clusters = {
uinput.KEY_Z: (250, 1000),
uinput.KEY_X: (520, 1100),
uinput.KEY_C: (820, 1040),
uinput.KEY_V: (1100, 1050),
uinput.KEY_S: (400, 650),
uinput.KEY_D: (720, 700),
uinput.KEY_F: (1050, 650)
}
"""
pressing_keys = set()
def set_slot_x_y(slot, x, y):
if slot >= len(slots):
return
if x is None and y is None:
slots[slot] = None
if not slots[slot]:
slots[slot] = [x, y]
elif x is not None:
slots[slot][0] = x
elif y is not None:
slots[slot][1] = y
def processed_slots():
return map(lambda s: (s[0], s[1]), filter(lambda s: s and s[0] is not None and s[1] is not None, slots))
def find_cluster(x, y):
closest_id = None
min_error = None
for i, centroid in clusters.items():
error = ((centroid[0] - x) ** 2) + ((centroid[1] - y) ** 2)
if min_error is None or error < min_error:
min_error = error
closest_id = i
return closest_id
if __name__ == "__main__":
"""
img = Image.new('RGB', (X_MAX, Y_MAX), color = 'white')
for x in range(X_MAX):
for y in range(Y_MAX):
color, error = find_cluster(x, y)
img.putpixel((x, y), color)
if error < 10:
img.putpixel((x, y), (255, 255, 255))
img.save('1.png')
"""
# print('saved')
p = subprocess.Popen(['evtest', '--grab', f'/dev/input/event{INPUT_EVENT_ID}'], stdout=subprocess.PIPE)
current_slot = 0
with uinput.Device(clusters.keys()) as device:
for line in iter(p.stdout.readline, b''):
line = line.decode()
tmp = line.split()
if len(tmp) <= 4 or tmp[4] != '3':
continue
tmp = tmp[8:]
if tmp[0] == '(ABS_MT_SLOT),':
current_slot = int(tmp[2])
elif tmp[0] == '(ABS_MT_POSITION_X),':
set_slot_x_y(current_slot, int(tmp[2]), None)
elif tmp[0] == '(ABS_MT_POSITION_Y),':
set_slot_x_y(current_slot, None, int(tmp[2]))
elif tmp[0] == '(ABS_MT_TRACKING_ID),' and tmp[2] == '-1':
set_slot_x_y(current_slot, None, None)
"""
for s in processed_slots():
print(f'{s[0]},{s[1]}')
"""
current_pressing = set()
for s in processed_slots():
k = find_cluster(s[0], s[1])
current_pressing.add(k)
if k not in pressing_keys:
pressing_keys.add(k)
print(k, 1)
device.emit(k, 1)
for k in pressing_keys:
if k not in current_pressing:
print(k, 0)
device.emit(k, 0)
pressing_keys &= current_pressing
# print(list(map(lambda s: (s, find_cluster(s[0], s[1])), processed_slots())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment