2020-02-07 21:28:29 +01:00
|
|
|
import uuid
|
2020-02-08 13:56:52 +01:00
|
|
|
import logging
|
|
|
|
|
2020-04-04 08:03:03 +02:00
|
|
|
import core.event
|
|
|
|
|
2020-02-08 13:40:51 +01:00
|
|
|
import util.cli
|
2020-02-07 21:28:29 +01:00
|
|
|
|
|
|
|
LEFT_MOUSE = 1
|
|
|
|
MIDDLE_MOUSE = 2
|
|
|
|
RIGHT_MOUSE = 3
|
|
|
|
WHEEL_UP = 4
|
|
|
|
WHEEL_DOWN = 5
|
|
|
|
|
2020-02-08 13:56:52 +01:00
|
|
|
def button_name(button):
|
|
|
|
if button == LEFT_MOUSE: return 'left-mouse'
|
|
|
|
if button == RIGHT_MOUSE: return 'right-mouse'
|
|
|
|
if button == MIDDLE_MOUSE: return 'middle-mouse'
|
|
|
|
if button == WHEEL_UP: return 'wheel-up'
|
|
|
|
if button == WHEEL_DOWN: return 'wheel-down'
|
|
|
|
return 'n/a'
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
class Object(object):
|
|
|
|
def __init__(self):
|
2020-02-08 13:40:51 +01:00
|
|
|
super(Object, self).__init__()
|
2020-03-29 14:43:04 +02:00
|
|
|
self.id = str(uuid.uuid4())
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-04-04 13:52:10 +02:00
|
|
|
def __event_id(obj_id, button):
|
|
|
|
return '{}::{}'.format(obj_id, button_name(button))
|
|
|
|
|
|
|
|
def __execute(cmd):
|
|
|
|
try:
|
|
|
|
util.cli.execute(cmd, wait=False)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error('failed to invoke callback: {}'.format(e))
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
def register(obj, button=None, cmd=None):
|
2020-04-04 13:52:10 +02:00
|
|
|
event_id = __event_id(obj.id, button)
|
|
|
|
logging.debug('registering callback {}'.format(event_id))
|
|
|
|
if callable(cmd):
|
|
|
|
core.event.register(event_id, cmd)
|
|
|
|
else:
|
|
|
|
core.event.register(event_id, lambda _: __execute(cmd))
|
2020-02-07 21:28:29 +01:00
|
|
|
|
|
|
|
def trigger(event):
|
|
|
|
if not 'button' in event: return
|
2020-04-04 13:52:10 +02:00
|
|
|
for field in ['instance', 'name']:
|
|
|
|
if not field in event: continue
|
|
|
|
core.event.trigger(__event_id(event[field], event['button']), event)
|
2020-02-07 21:28:29 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|