bumblebee-status/bumblebee/outputs/i3.py
Tobias Witek 579381978e [output] Add support for click-events in i3
The i3 output now has a separate thread that continuously monitors stdin
for incoming click events. The generic output class also has methods for
registering callbacks (as commands). For now, by default, scroll events
will be used to emulate the next/previous workspace.
2016-11-01 07:46:26 +01:00

84 lines
2.5 KiB
Python

from __future__ import unicode_literals
import os
import sys
import json
import shlex
import threading
import subprocess
import bumblebee.output
def read_input(output):
while True:
line = sys.stdin.readline().strip(",").strip()
if line == "[": continue
if line == "]": break
DEVNULL = open(os.devnull, 'wb')
event = json.loads(line)
cb = output.callback(event)
if cb:
cb = cb.format(
name = event.get("name", ""),
instance = event.get("instance", ""),
button = event.get("button", -1)
)
subprocess.call(shlex.split(cb), stdout=DEVNULL, stderr=DEVNULL)
class i3bar(bumblebee.output.Output):
def __init__(self, theme):
super(i3bar, self).__init__(theme)
self._data = []
self.add_callback("i3-msg workspace prev", 4)
self.add_callback("i3-msg workspace next", 5)
self._thread = threading.Thread(target=read_input, args=(self,))
self._thread.start()
def start(self):
return json.dumps({ "version": 1, "click_events": True }) + "["
def add(self, obj):
theme = self.theme()
while True:
d = obj.data()
data = {
u"full_text": "{}{}{}".format(theme.prefix(obj), d, theme.suffix(obj)),
"color": theme.color(obj),
"background": theme.background(obj),
"name": obj.__module__.replace("bumblebee.modules.",""),
"instance": obj.instance() if hasattr(obj, "instance") else None,
}
if theme.urgent(obj) and obj.critical():
data["urgent"] = True
if theme.default_separators(obj) == False:
data["separator"] = False
data["separator_block_width"] = 0
if theme.separator(obj):
self._data.append({
u"full_text": theme.separator(obj),
"color": theme.background(obj),
"background": theme.previous_background(),
"separator": False,
"separator_block_width": 0,
})
self._data.append(data)
if obj.next() == False:
break
theme.next()
def get(self):
data = json.dumps(self._data)
self._data = []
return data + ","
def stop(self):
return "]"
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4