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

22
remarkable_mouse/generate_codes.py

@ -4,9 +4,21 @@
# Only runs on Linux. # Only runs on Linux.
import libevdev import libevdev
import pprint
pp = pprint.PrettyPrinter()
with open('codes.py' , 'w') as f: types = {}
for t in libevdev.types: codes = {}
f.write(f'\n\n{t.name} = {t.value}\n')
for c in t.codes: for t in libevdev.types:
f.write(f'{c.name} = {c.value}\n') 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
import struct import struct
from screeninfo import get_monitors from screeninfo import get_monitors
from .codes import EV_SYN, EV_ABS, ABS_X, ABS_Y, BTN_TOUCH # 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 codes
from .common import get_monitor, remap, wacom_width, wacom_height, log_event
logging.basicConfig(format='%(message)s') logging.basicConfig(format='%(message)s')
log = logging.getLogger('remouse') log = logging.getLogger('remouse')
@ -35,23 +36,27 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
x = y = 0 x = y = 0
stream = rm_inputs['pen']
while True: 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 # handle x direction
if e_code == ABS_Y: if codes[e_type][e_code] == 'ABS_Y':
log.debug(e_value) log.debug(e_value)
x = e_value x = e_value
# handle y direction # handle y direction
if e_code == ABS_X: if codes[e_type][e_code] == 'ABS_X':
log.debug('\t{}'.format(e_value)) log.debug('\t{}'.format(e_value))
y = e_value y = e_value
# handle draw # handle draw
if e_code == BTN_TOUCH: if codes[e_type][e_code] == 'BTN_TOUCH':
log.debug('\t\t{}'.format(e_value)) log.debug('\t\t{}'.format(e_value))
if e_value == 1: if e_value == 1:
log.debug('PRESS') log.debug('PRESS')
@ -60,7 +65,7 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
log.debug('RELEASE') log.debug('RELEASE')
mouse.release(Button.left) mouse.release(Button.left)
if e_type == EV_SYN: if codes[e_type][e_code] == 'SYN_REPORT':
mapped_x, mapped_y = remap( mapped_x, mapped_y = remap(
x, y, x, y,
wacom_width, wacom_height, wacom_width, wacom_height,
@ -71,3 +76,6 @@ def read_tablet(rm_inputs, *, orientation, monitor_num, region, threshold, mode)
monitor.x + mapped_x - mouse.position[0], monitor.x + mapped_x - mouse.position[0],
monitor.y + mapped_y - mouse.position[1] 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