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-05-03 11:15:52 +02:00
|
|
|
|
2020-02-08 13:56:52 +01:00
|
|
|
def button_name(button):
|
2020-05-03 11:15:52 +02:00
|
|
|
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-08 13:56:52 +01:00
|
|
|
|
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-05-03 11:15:52 +02:00
|
|
|
|
2020-04-04 13:52:10 +02:00
|
|
|
def __event_id(obj_id, button):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}::{}".format(obj_id, button_name(button))
|
|
|
|
|
2020-04-04 13:52:10 +02:00
|
|
|
|
2020-06-09 20:29:51 +02:00
|
|
|
def __execute(event, cmd, wait=False):
|
2020-04-04 13:52:10 +02:00
|
|
|
try:
|
2020-06-09 20:29:51 +02:00
|
|
|
util.cli.execute(
|
|
|
|
cmd.format(instance=event.get("instance", ""), name=event.get("name", ""),),
|
|
|
|
wait=wait,
|
|
|
|
shell=True,
|
|
|
|
)
|
2020-04-04 13:52:10 +02:00
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.error("failed to invoke callback: {}".format(e))
|
|
|
|
|
2020-04-04 13:52:10 +02:00
|
|
|
|
2020-05-16 15:16:23 +02:00
|
|
|
def register(obj, button=None, cmd=None, wait=False):
|
2020-05-03 11:15:52 +02:00
|
|
|
event_id = __event_id(obj.id if obj is not None else "", button)
|
|
|
|
logging.debug("registering callback {}".format(event_id))
|
2020-04-04 13:52:10 +02:00
|
|
|
if callable(cmd):
|
|
|
|
core.event.register(event_id, cmd)
|
|
|
|
else:
|
2020-06-09 20:29:51 +02:00
|
|
|
core.event.register(event_id, lambda event: __execute(event, cmd, wait))
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
def trigger(event):
|
2020-05-03 11:15:52 +02:00
|
|
|
if not "button" in event:
|
|
|
|
return
|
2020-05-02 13:54:45 +02:00
|
|
|
|
|
|
|
triggered = False
|
2020-05-03 11:15:52 +02:00
|
|
|
for field in ["instance", "name"]:
|
|
|
|
if not field in event:
|
|
|
|
continue
|
|
|
|
if core.event.trigger(__event_id(event[field], event["button"]), event):
|
2020-05-02 13:54:45 +02:00
|
|
|
triggered = True
|
|
|
|
if not triggered:
|
2020-05-03 11:15:52 +02:00
|
|
|
core.event.trigger(__event_id("", event["button"]), event)
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|