Browse Source

enable event logging on pynput and evdev

master
evan 2 years ago
parent
commit
2516dc0477
  1. 2140
      remarkable_mouse/codes.py
  2. 12
      remarkable_mouse/common.py
  3. 72
      remarkable_mouse/evdev.py
  4. 22
      remarkable_mouse/generate_codes.py
  5. 36
      remarkable_mouse/pynput.py

2140
remarkable_mouse/codes.py

File diff suppressed because it is too large Load Diff

12
remarkable_mouse/common.py

@ -4,6 +4,8 @@ import logging @@ -4,6 +4,8 @@ import logging
import sys
from screeninfo import get_monitors, Monitor
from .codes import codes, types
logging.basicConfig(format='%(message)s')
log = logging.getLogger('remouse')
@ -130,3 +132,13 @@ def remap(x, y, wacom_width, wacom_height, monitor_width, @@ -130,3 +132,13 @@ def remap(x, y, wacom_width, wacom_height, monitor_width,
scaling_x * (x - (wacom_width - monitor_width / scaling_x) / 2),
scaling_y * (y - (wacom_height - monitor_height / scaling_y) / 2)
)
# log evdev event to console
def log_event(e_time, e_millis, e_type, e_code, e_value):
log.debug('{}.{:0>6} - {: <9} {: <15} {: >6}'.format(
e_time,
e_millis,
types[e_type],
codes[e_type][e_code],
e_value
))

72
remarkable_mouse/evdev.py

@ -7,8 +7,8 @@ from itertools import cycle @@ -7,8 +7,8 @@ from itertools import cycle
from socket import timeout as TimeoutError
import libevdev
from .codes import EV_SYN, EV_ABS, ABS_X, ABS_Y, SYN_REPORT
from .common import get_monitor, remap, wacom_width, wacom_height
from .codes import codes, types
from .common import get_monitor, remap, wacom_width, wacom_height, log_event
logging.basicConfig(format='%(message)s')
log = logging.getLogger('remouse')
@ -46,8 +46,8 @@ def create_local_device(): @@ -46,8 +46,8 @@ def create_local_device():
inputs = (
# touch inputs
(libevdev.EV_ABS.ABS_MT_POSITION_X, 0, 20967, 2531),
(libevdev.EV_ABS.ABS_MT_POSITION_Y, 0, 15725, 2531),
(libevdev.EV_ABS.ABS_MT_POSITION_X, 0, 767, 2531),
(libevdev.EV_ABS.ABS_MT_POSITION_Y, 0, 1023, 2531),
(libevdev.EV_ABS.ABS_MT_PRESSURE, 0, 255, None),
(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR, 0, 255, None),
(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR, 0, 255, None),
@ -57,12 +57,12 @@ def create_local_device(): @@ -57,12 +57,12 @@ def create_local_device():
(libevdev.EV_ABS.ABS_MT_TRACKING_ID, 0, 65535, None),
# pen inputs
(libevdev.EV_ABS.ABS_X, 0, 767, 2531), # cyttps5_mt driver
(libevdev.EV_ABS.ABS_Y, 0, 1023, 2531), # cyttsp5_mt
(libevdev.EV_ABS.ABS_PRESSURE, 0, 4095, None),
(libevdev.EV_ABS.ABS_DISTANCE, 0, 255, None),
(libevdev.EV_ABS.ABS_TILT_X, -9000, 9000, None),
(libevdev.EV_ABS.ABS_TILT_Y, -9000, 9000, None)
(libevdev.EV_ABS.ABS_X, 0, 20967, 2531), # cyttps5_mt driver
(libevdev.EV_ABS.ABS_Y, 0, 15725, 2531), # cyttsp5_mt
(libevdev.EV_ABS.ABS_PRESSURE, 0, 4095, None),
(libevdev.EV_ABS.ABS_DISTANCE, 0, 255, None),
(libevdev.EV_ABS.ABS_TILT_X, -9000, 9000, None),
(libevdev.EV_ABS.ABS_TILT_Y, -9000, 9000, None)
)
for code, minimum, maximum, resolution in inputs:
@ -108,46 +108,36 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode) @@ -108,46 +108,36 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
e_time, e_millis, e_type, e_code, e_value = struct.unpack('2IHHi', data)
e_bit = libevdev.evbit(e_type, e_code)
e = libevdev.InputEvent(e_bit, value=e_value)
local_device.send_events([e])
if e_type == EV_ABS:
# intercept EV_ABS events and modify coordinates
if types[e_type] == 'EV_ABS':
# handle x direction
if e_code == ABS_Y:
if codes[e_type][e_code] == 'ABS_Y':
x = e_value
# handle y direction
if e_code == ABS_X:
if codes[e_type][e_code] == 'ABS_X':
y = e_value
elif e_type == EV_SYN:
mapped_x, mapped_y = remap(
x, y,
wacom_width, wacom_height,
monitor.width, monitor.height,
mode, orientation
)
local_device.send_events([e])
print('sent')
print(e_type)
else:
local_device.send_events([e])
# While debug mode is active, we log events grouped together between
# SYN_REPORT events. Pending events for the next log are stored here
# if log.level == logging.DEBUG:
# if e_bit == SYN_REPORT:
# event_repr = ', '.join(
# '{} = {}'.format(
# e.code.name,
# e.value
# ) for event in pending_events
# )
# log.debug('{}.{:0>6} - {}'.format(e_time, e_millis, event_repr))
# pending_events = []
# else:
# pending_events.append(event)
# FIXME - something wrong with remapping
# handle x direction
# if codes[e_type][e_code] == 'ABS_Y':
# e_value = int(mapped_x)
# # handle y direction
# if codes[e_type][e_code] == 'ABS_X':
# e_value = int(mapped_y)
# pass events directly to libevdev
e_bit = libevdev.evbit(e_type, e_code)
e = libevdev.InputEvent(e_bit, value=e_value)
local_device.send_events([e])
if log.level == logging.DEBUG:
log_event(e_time, e_millis, e_type, e_code, e_value)

22
remarkable_mouse/generate_codes.py

@ -4,9 +4,21 @@ @@ -4,9 +4,21 @@
# Only runs on Linux.
import libevdev
import pprint
pp = pprint.PrettyPrinter()
with open('codes.py' , 'w') as f:
for t in libevdev.types:
f.write(f'\n\n{t.name} = {t.value}\n')
for c in t.codes:
f.write(f'{c.name} = {c.value}\n')
types = {}
codes = {}
for t in libevdev.types:
types[t.value] = t.name
codes[t.value] = {}
for c in t.codes:
codes[t.value][c.value] = c.name
with open('codes.py', 'w') as f:
f.write('# generated by generate_codes.py\n')
f.write('\ntypes= ')
f.write(pp.pformat(types))
f.write('\ncodes = ')
f.write(pp.pformat(codes))

36
remarkable_mouse/pynput.py

@ -2,8 +2,9 @@ import logging @@ -2,8 +2,9 @@ import logging
import struct
from screeninfo import get_monitors
from .codes import EV_SYN, EV_ABS, ABS_X, ABS_Y, BTN_TOUCH
from .common import get_monitor, remap, wacom_width, wacom_height
# from .codes import EV_SYN, EV_ABS, ABS_X, ABS_Y, BTN_TOUCH
from .codes import codes
from .common import get_monitor, remap, wacom_width, wacom_height, log_event
logging.basicConfig(format='%(message)s')
log = logging.getLogger('remouse')
@ -35,23 +36,27 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode) @@ -35,23 +36,27 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
x = y = 0
stream = rm_inputs['pen']
while True:
_, _, e_type, e_code, e_value = struct.unpack('2IHHi', rm_inputs['pen'].read(16))
try:
data = stream.read(16)
except TimeoutError:
continue
if e_type == EV_ABS:
e_time, e_millis, e_type, e_code, e_value = struct.unpack('2IHHi', data)
# handle x direction
if e_code == ABS_Y:
log.debug(e_value)
x = e_value
# handle x direction
if codes[e_type][e_code] == 'ABS_Y':
log.debug(e_value)
x = e_value
# handle y direction
if e_code == ABS_X:
log.debug('\t{}'.format(e_value))
y = e_value
# handle y direction
if codes[e_type][e_code] == 'ABS_X':
log.debug('\t{}'.format(e_value))
y = e_value
# handle draw
if e_code == BTN_TOUCH:
if codes[e_type][e_code] == 'BTN_TOUCH':
log.debug('\t\t{}'.format(e_value))
if e_value == 1:
log.debug('PRESS')
@ -60,7 +65,7 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode) @@ -60,7 +65,7 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
log.debug('RELEASE')
mouse.release(Button.left)
if e_type == EV_SYN:
if codes[e_type][e_code] == 'SYN_REPORT':
mapped_x, mapped_y = remap(
x, y,
wacom_width, wacom_height,
@ -71,3 +76,6 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode) @@ -71,3 +76,6 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
monitor.x + mapped_x - mouse.position[0],
monitor.y + mapped_y - mouse.position[1]
)
if log.level == logging.DEBUG:
log_event(e_time, e_millis, e_type, e_code, e_value)

Loading…
Cancel
Save