Created
January 17, 2020 08:53
-
-
Save rjeli/34b606fb0d302bded2f8f2da2f52ec54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
import evdev | |
from evdev import ecodes | |
SCROLL_FRAC = 0.1 | |
if __name__ == '__main__': | |
devs = [evdev.InputDevice(path) for path in evdev.list_devices()] | |
dev = None | |
for d in devs: | |
if 'eGalaxTouch' in d.name: | |
dev = d | |
assert dev is not None | |
print('grabbing device', dev.name) | |
dev.grab() | |
abs_x_info = dev.absinfo(ecodes.ABS_X) | |
abs_y_info = dev.absinfo(ecodes.ABS_Y) | |
cap = { | |
ecodes.EV_KEY: [ecodes.BTN_TOUCH, ecodes.BTN_LEFT], | |
ecodes.EV_REL: [ecodes.REL_WHEEL], | |
ecodes.EV_ABS: [ | |
(ecodes.ABS_X, abs_x_info), | |
(ecodes.ABS_Y, abs_y_info), | |
], | |
} | |
print('creating ui') | |
ui = evdev.UInput(cap, name='touchscroll', version=0x3) | |
time.sleep(1) | |
print(ui) | |
last_pos = (None, None) | |
quant_err = 0. | |
for evt in dev.read_loop(): | |
evt = evdev.util.categorize(evt) | |
if isinstance(evt, evdev.KeyEvent): | |
if evt.keystate == evt.key_up: | |
last_pos = (None, None) | |
acc = 0. | |
elif isinstance(evt, evdev.AbsEvent): | |
if evt.event.code == ecodes.ABS_X: | |
pos = (evt.event.value, last_pos[1]) | |
elif evt.event.code == ecodes.ABS_Y: | |
pos = (last_pos[0], evt.event.value) | |
else: | |
continue | |
if None in last_pos and None not in pos: | |
print('move to', pos) | |
ui.write(ecodes.EV_KEY, ecodes.BTN_LEFT, 1) | |
ui.write(ecodes.EV_ABS, ecodes.ABS_X, pos[0]) | |
ui.write(ecodes.EV_ABS, ecodes.ABS_Y, pos[1]) | |
ui.syn() | |
ui.write(ecodes.EV_KEY, ecodes.BTN_LEFT, 0) | |
ui.syn() | |
if last_pos[1] is not None: | |
y_scroll = pos[1] - last_pos[1] | |
print('scroll', y_scroll) | |
adj = y_scroll * SCROLL_FRAC + quant_err | |
adj_floor = int(adj) | |
quant_err = adj - adj_floor | |
ui.write(ecodes.EV_REL, ecodes.REL_WHEEL, adj_floor) | |
ui.syn() | |
last_pos = pos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment