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
|
2016-12-15 19:41:50 +01:00
|
|
|
MIDDLE_MOUSE = 2
|
2016-12-09 19:29:16 +01:00
|
|
|
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
|
|
|
|
2017-02-26 09:13:44 +01:00
|
|
|
def is_terminated():
|
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.name == "MainThread" and not thread.is_alive():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
def read_input(inp):
|
|
|
|
"""Read i3bar input and execute callbacks"""
|
2016-12-17 07:43:38 +01:00
|
|
|
epoll = select.epoll()
|
|
|
|
epoll.register(sys.stdin.fileno(), select.EPOLLIN)
|
2016-12-09 19:29:16 +01:00
|
|
|
while inp.running:
|
2017-02-26 09:13:44 +01:00
|
|
|
if is_terminated():
|
|
|
|
return
|
2016-12-10 13:45:54 +01:00
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
events = epoll.poll(1)
|
|
|
|
for fileno, event in events:
|
2016-12-17 08:04:21 +01:00
|
|
|
line = "["
|
|
|
|
while "[" in line:
|
|
|
|
line = sys.stdin.readline().strip(",").strip()
|
2016-12-17 07:43:38 +01:00
|
|
|
inp.has_event = True
|
|
|
|
try:
|
|
|
|
event = json.loads(line)
|
|
|
|
if "instance" in event:
|
|
|
|
inp.callback(event)
|
|
|
|
inp.redraw()
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
epoll.unregister(sys.stdin.fileno())
|
|
|
|
epoll.close()
|
2016-12-09 19:29:16 +01:00
|
|
|
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-11 07:38:56 +01:00
|
|
|
def _uuidstr(self, name, button):
|
|
|
|
return "{}::{}".format(name, button)
|
|
|
|
|
|
|
|
def _uid(self, obj, button):
|
2016-12-09 19:29:16 +01:00
|
|
|
uid = self.global_id
|
|
|
|
if obj:
|
|
|
|
uid = obj.id
|
2016-12-11 07:38:56 +01:00
|
|
|
return self._uuidstr(uid, button)
|
2016-12-09 19:29:16 +01:00
|
|
|
|
2016-12-10 10:26:07 +01:00
|
|
|
def deregister_callbacks(self, obj):
|
2016-12-11 07:38:56 +01:00
|
|
|
to_delete = []
|
|
|
|
uid = obj.id if obj else self.global_id
|
|
|
|
for key in self._callbacks:
|
|
|
|
if uid in key:
|
|
|
|
to_delete.append(key)
|
|
|
|
for key in to_delete:
|
|
|
|
del self._callbacks[key]
|
2016-12-10 10:26:07 +01:00
|
|
|
|
|
|
|
def register_callback(self, obj, button, cmd):
|
|
|
|
"""Register a callback function or system call"""
|
2016-12-11 07:38:56 +01:00
|
|
|
uid = self._uid(obj, button)
|
2016-12-09 19:29:16 +01:00
|
|
|
if uid not in self._callbacks:
|
|
|
|
self._callbacks[uid] = {}
|
2016-12-11 07:38:56 +01:00
|
|
|
self._callbacks[uid] = cmd
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
def callback(self, event):
|
|
|
|
"""Execute callback action for an incoming event"""
|
2016-12-11 07:38:56 +01:00
|
|
|
button = event["button"]
|
|
|
|
|
|
|
|
cmd = self._callbacks.get(self._uuidstr(self.global_id, button), None)
|
|
|
|
cmd = self._callbacks.get(self._uuidstr(event["name"], button), cmd)
|
|
|
|
cmd = self._callbacks.get(self._uuidstr(event["instance"], button), cmd)
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
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
|