2016-11-05 14:28:33 +00:00
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
import inspect
|
2016-11-04 20:10:21 +00:00
|
|
|
import threading
|
2016-11-05 14:28:33 +00:00
|
|
|
import subprocess
|
2016-10-30 16:56:04 +00:00
|
|
|
|
2016-11-04 20:41:22 +00:00
|
|
|
def output(args):
|
|
|
|
import bumblebee.outputs.i3
|
|
|
|
return bumblebee.outputs.i3.Output(args)
|
|
|
|
|
2016-11-05 07:59:43 +00:00
|
|
|
class Widget(object):
|
2016-11-12 11:11:42 +00:00
|
|
|
def __init__(self, obj, text, instance=None):
|
2016-11-05 11:14:33 +00:00
|
|
|
self._obj = obj
|
2016-11-05 07:59:43 +00:00
|
|
|
self._text = text
|
2016-11-05 12:42:26 +00:00
|
|
|
self._store = {}
|
2016-11-12 11:11:42 +00:00
|
|
|
self._instance = instance
|
2016-11-05 12:42:26 +00:00
|
|
|
|
2016-11-26 12:57:33 +00:00
|
|
|
obj._output.register_widget(self.instance(), self)
|
|
|
|
|
2016-11-05 12:42:26 +00:00
|
|
|
def set(self, key, value):
|
|
|
|
self._store[key] = value
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
return self._store.get(key, default)
|
2016-11-05 07:59:43 +00:00
|
|
|
|
|
|
|
def state(self):
|
2016-11-05 12:42:26 +00:00
|
|
|
return self._obj.state(self)
|
2016-11-05 07:59:43 +00:00
|
|
|
|
|
|
|
def warning(self):
|
2016-11-05 12:42:26 +00:00
|
|
|
return self._obj.warning(self)
|
2016-11-05 07:59:43 +00:00
|
|
|
|
|
|
|
def critical(self):
|
2016-11-05 12:42:26 +00:00
|
|
|
return self._obj.critical(self)
|
2016-11-05 07:59:43 +00:00
|
|
|
|
|
|
|
def module(self):
|
2016-11-05 11:14:33 +00:00
|
|
|
return self._obj.__module__.split(".")[-1]
|
2016-11-05 07:59:43 +00:00
|
|
|
|
2016-11-05 12:23:46 +00:00
|
|
|
def instance(self):
|
2016-11-12 11:11:42 +00:00
|
|
|
return self._instance if self._instance else getattr(self._obj, "instance")(self)
|
2016-11-05 12:23:46 +00:00
|
|
|
|
2016-11-05 07:59:43 +00:00
|
|
|
def text(self):
|
|
|
|
return self._text
|
|
|
|
|
2016-11-05 14:28:33 +00:00
|
|
|
class Command(object):
|
2016-11-26 12:57:33 +00:00
|
|
|
def __init__(self, command, event, widget):
|
2016-11-05 14:28:33 +00:00
|
|
|
self._command = command
|
2016-11-26 12:57:33 +00:00
|
|
|
self._event = event
|
|
|
|
self._widget = widget
|
2016-11-05 14:28:33 +00:00
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
2016-11-23 17:24:02 +00:00
|
|
|
if not isinstance(self._command, list):
|
|
|
|
self._command = [ self._command ]
|
|
|
|
|
|
|
|
for cmd in self._command:
|
2016-11-29 18:35:54 +00:00
|
|
|
if not cmd: continue
|
2016-11-26 12:57:33 +00:00
|
|
|
if inspect.ismethod(cmd):
|
|
|
|
cmd(self._event, self._widget)
|
|
|
|
else:
|
|
|
|
c = cmd.format(*args, **kwargs)
|
|
|
|
DEVNULL = open(os.devnull, 'wb')
|
2016-11-29 18:35:54 +00:00
|
|
|
subprocess.Popen(shlex.split(c), stdout=DEVNULL, stderr=DEVNULL)
|
2016-11-05 14:28:33 +00:00
|
|
|
|
2016-10-30 16:56:04 +00:00
|
|
|
class Output(object):
|
2016-11-05 07:59:43 +00:00
|
|
|
def __init__(self, config):
|
|
|
|
self._config = config
|
2016-11-01 06:46:26 +00:00
|
|
|
self._callbacks = {}
|
2016-11-04 20:10:21 +00:00
|
|
|
self._wait = threading.Condition()
|
|
|
|
self._wait.acquire()
|
2016-11-26 12:57:33 +00:00
|
|
|
self._widgets = {}
|
|
|
|
|
|
|
|
def register_widget(self, identity, widget):
|
|
|
|
self._widgets[identity] = widget
|
2016-11-01 06:46:26 +00:00
|
|
|
|
2016-11-04 18:11:10 +00:00
|
|
|
def redraw(self):
|
2016-11-04 20:10:21 +00:00
|
|
|
self._wait.acquire()
|
|
|
|
self._wait.notify()
|
|
|
|
self._wait.release()
|
2016-11-04 18:11:10 +00:00
|
|
|
|
2016-11-01 06:58:50 +00:00
|
|
|
def add_callback(self, cmd, button, module=None):
|
2016-11-01 07:09:10 +00:00
|
|
|
if module:
|
|
|
|
module = module.replace("bumblebee.modules.", "")
|
2016-11-05 14:28:33 +00:00
|
|
|
|
2016-11-05 14:39:21 +00:00
|
|
|
if self._callbacks.get((button, module)): return
|
|
|
|
|
2016-11-01 06:46:26 +00:00
|
|
|
self._callbacks[(
|
|
|
|
button,
|
|
|
|
module,
|
|
|
|
)] = cmd
|
|
|
|
|
|
|
|
def callback(self, event):
|
|
|
|
cb = self._callbacks.get((
|
|
|
|
event.get("button", -1),
|
2016-11-05 15:39:46 +00:00
|
|
|
None,
|
2016-11-01 06:46:26 +00:00
|
|
|
), None)
|
2016-11-05 15:39:46 +00:00
|
|
|
cb = self._callbacks.get((
|
|
|
|
event.get("button", -1),
|
2016-11-27 17:08:22 +00:00
|
|
|
event.get("instance", event.get("module", None)),
|
2016-11-05 15:39:46 +00:00
|
|
|
), cb)
|
2016-11-05 14:28:33 +00:00
|
|
|
|
2016-11-27 17:08:22 +00:00
|
|
|
identity = event.get("instance", event.get("module", None))
|
2016-11-26 12:57:33 +00:00
|
|
|
return Command(cb, event, self._widgets.get(identity, None))
|
2016-10-31 09:45:15 +00:00
|
|
|
|
2016-11-05 07:59:43 +00:00
|
|
|
def wait(self):
|
|
|
|
self._wait.wait(self._config.parameter("interval", 1))
|
2016-11-04 20:10:21 +00:00
|
|
|
|
2016-10-30 16:56:04 +00:00
|
|
|
def start(self):
|
|
|
|
pass
|
|
|
|
|
2016-11-05 07:59:43 +00:00
|
|
|
def draw(self, widgets, theme):
|
2016-11-05 12:12:30 +00:00
|
|
|
if not type(widgets) is list:
|
|
|
|
widgets = [ widgets ]
|
|
|
|
self._draw(widgets, theme)
|
|
|
|
|
|
|
|
def _draw(self, widgets, theme):
|
2016-10-30 16:56:04 +00:00
|
|
|
pass
|
|
|
|
|
2016-11-05 07:59:43 +00:00
|
|
|
def flush(self):
|
2016-10-30 16:56:04 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|