[core/input] Move event handling to core.event
Until now, bumblebee-status did event handling in two places with almost identical code: in core.event (makes sense) and core.input (still makes sense, but a bit more dubious). Changed core.input to use core.event
This commit is contained in:
parent
a43917594e
commit
04fc9867ac
1 changed files with 18 additions and 21 deletions
|
@ -19,35 +19,32 @@ def button_name(button):
|
||||||
if button == WHEEL_DOWN: return 'wheel-down'
|
if button == WHEEL_DOWN: return 'wheel-down'
|
||||||
return 'n/a'
|
return 'n/a'
|
||||||
|
|
||||||
callbacks = {}
|
|
||||||
|
|
||||||
class Object(object):
|
class Object(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Object, self).__init__()
|
super(Object, self).__init__()
|
||||||
self.id = str(uuid.uuid4())
|
self.id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
def register(obj, button=None, cmd=None):
|
def register(obj, button=None, cmd=None):
|
||||||
logging.debug('registering callback {} {}'.format(obj.id, button))
|
event_id = __event_id(obj.id, button)
|
||||||
callbacks.setdefault(obj.id, {}).setdefault(button, []).append(cmd)
|
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))
|
||||||
|
|
||||||
def trigger(event):
|
def trigger(event):
|
||||||
for field in ['instance', 'name']:
|
|
||||||
if field in event:
|
|
||||||
cb = callbacks.get(event[field])
|
|
||||||
__invoke(event, cb)
|
|
||||||
|
|
||||||
def __invoke(event, callback):
|
|
||||||
if not callback: return
|
|
||||||
if not 'button' in event: return
|
if not 'button' in event: return
|
||||||
|
for field in ['instance', 'name']:
|
||||||
for cb in callback.get(event['button'], []):
|
if not field in event: continue
|
||||||
if callable(cb):
|
core.event.trigger(__event_id(event[field], event['button']), event)
|
||||||
cb(event)
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
util.cli.execute(cb, wait=False)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error('failed to invoke callback: {}'.format(e))
|
|
||||||
return
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue