2016-12-09 19:29:16 +01:00
|
|
|
"""Input classes"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
import time
|
2016-12-10 13:45:54 +01:00
|
|
|
import select
|
2016-12-09 19:29:16 +01:00
|
|
|
import threading
|
|
|
|
import bumblebee.util
|
|
|
|
|
|
|
|
LEFT_MOUSE = 1
|
|
|
|
RIGHT_MOUSE = 3
|
2016-12-10 12:14:12 +01:00
|
|
|
WHEEL_UP = 4
|
|
|
|
WHEEL_DOWN = 5
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
def read_input(inp):
|
|
|
|
"""Read i3bar input and execute callbacks"""
|
|
|
|
while inp.running:
|
2016-12-10 13:45:54 +01:00
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.name == "MainThread" and not thread.is_alive():
|
|
|
|
return
|
|
|
|
|
|
|
|
rlist, _, _ = select.select([sys.stdin], [], [], 1)
|
|
|
|
if not rlist:
|
|
|
|
continue
|
2016-12-09 19:29:16 +01:00
|
|
|
line = sys.stdin.readline().strip(",").strip()
|
|
|
|
inp.has_event = True
|
|
|
|
try:
|
|
|
|
event = json.loads(line)
|
|
|
|
inp.callback(event)
|
2016-12-10 10:26:07 +01:00
|
|
|
inp.redraw()
|
2016-12-09 19:29:16 +01:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
inp.has_event = True
|
|
|
|
inp.clean_exit = True
|
|
|
|
|
|
|
|
class I3BarInput(object):
|
|
|
|
"""Process incoming events from the i3bar"""
|
|
|
|
def __init__(self):
|
|
|
|
self.running = True
|
|
|
|
self._callbacks = {}
|
|
|
|
self.clean_exit = False
|
|
|
|
self.global_id = str(uuid.uuid4())
|
|
|
|
self.need_event = False
|
|
|
|
self.has_event = False
|
2016-12-10 10:26:07 +01:00
|
|
|
self._condition = threading.Condition()
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Start asynchronous input processing"""
|
2016-12-10 08:37:04 +01:00
|
|
|
self.has_event = False
|
|
|
|
self.running = True
|
2016-12-10 10:26:07 +01:00
|
|
|
self._condition.acquire()
|
2016-12-10 08:37:04 +01:00
|
|
|
self._thread = threading.Thread(target=read_input, args=(self,))
|
2016-12-09 19:29:16 +01:00
|
|
|
self._thread.start()
|
|
|
|
|
2016-12-10 10:26:07 +01:00
|
|
|
def redraw(self):
|
|
|
|
self._condition.acquire()
|
|
|
|
self._condition.notify()
|
|
|
|
self._condition.release()
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
def alive(self):
|
|
|
|
"""Check whether the input processing is still active"""
|
|
|
|
return self._thread.is_alive()
|
|
|
|
|
2016-12-10 10:26:07 +01:00
|
|
|
def wait(self, timeout):
|
|
|
|
self._condition.wait(timeout)
|
|
|
|
|
2016-12-10 18:22:05 +01:00
|
|
|
def _wait(self):
|
2016-12-10 08:37:04 +01:00
|
|
|
while not self.has_event:
|
|
|
|
time.sleep(0.1)
|
|
|
|
self.has_event = False
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
def stop(self):
|
|
|
|
"""Stop asynchronous input processing"""
|
2016-12-10 10:26:07 +01:00
|
|
|
self._condition.release()
|
2016-12-09 19:29:16 +01:00
|
|
|
if self.need_event:
|
2016-12-10 18:22:05 +01:00
|
|
|
self._wait()
|
2016-12-09 19:29:16 +01:00
|
|
|
self.running = False
|
|
|
|
self._thread.join()
|
|
|
|
return self.clean_exit
|
|
|
|
|
2016-12-10 10:26:07 +01:00
|
|
|
def _uid(self, obj):
|
2016-12-09 19:29:16 +01:00
|
|
|
uid = self.global_id
|
|
|
|
if obj:
|
|
|
|
uid = obj.id
|
2016-12-10 10:26:07 +01:00
|
|
|
return uid
|
2016-12-09 19:29:16 +01:00
|
|
|
|
2016-12-10 10:26:07 +01:00
|
|
|
def deregister_callbacks(self, obj):
|
|
|
|
uid = self._uid(obj)
|
|
|
|
if uid in self._callbacks:
|
|
|
|
del self._callbacks[uid]
|
|
|
|
|
|
|
|
def register_callback(self, obj, button, cmd):
|
|
|
|
"""Register a callback function or system call"""
|
|
|
|
uid = self._uid(obj)
|
2016-12-09 19:29:16 +01:00
|
|
|
if uid not in self._callbacks:
|
|
|
|
self._callbacks[uid] = {}
|
|
|
|
self._callbacks[uid][button] = cmd
|
|
|
|
|
|
|
|
def callback(self, event):
|
|
|
|
"""Execute callback action for an incoming event"""
|
|
|
|
cmd = self._callbacks.get(self.global_id, {})
|
|
|
|
cmd = self._callbacks.get(event["name"], cmd)
|
|
|
|
cmd = self._callbacks.get(event["instance"], cmd)
|
|
|
|
cmd = cmd.get(event["button"], None)
|
|
|
|
if cmd is None:
|
|
|
|
return
|
|
|
|
if callable(cmd):
|
|
|
|
cmd(event)
|
|
|
|
else:
|
2016-12-10 10:47:23 +01:00
|
|
|
bumblebee.util.execute(cmd, False)
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|