From 26f5fd3064c7570dc24ab9be3dbe331f17c10656 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 5 Nov 2016 15:28:33 +0100 Subject: [PATCH] [modules] Re-enable preconfigured on-click actions --- bumblebee/module.py | 4 ++-- bumblebee/modules/cpu.py | 4 +--- bumblebee/modules/disk.py | 7 ++++--- bumblebee/modules/memory.py | 4 +--- bumblebee/modules/pulseaudio.py | 17 ++++++++-------- bumblebee/output.py | 36 ++++++++++++++++++++++++--------- bumblebee/outputs/i3.py | 20 +++++++++--------- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/bumblebee/module.py b/bumblebee/module.py index f7b3ad6..efbf498 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -48,7 +48,7 @@ class Module(object): def state(self, widget): return "default" - def instance(self, widget): - return self._alias if self._alias else self.__module__.split(".")[-1] + def instance(self, widget=None): + return self.__module__.split(".")[-1] # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index 3d1acdb..46d6614 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -15,9 +15,7 @@ class Module(bumblebee.module.Module): super(Module, self).__init__(output, config, alias) self._perc = psutil.cpu_percent(percpu=False) -# TODO -# output.add_callback(module=self.__module__, button=1, -# cmd="gnome-system-monitor") + output.add_callback(module=self.instance(), button=1, cmd="gnome-system-monitor") def widgets(self): self._perc = psutil.cpu_percent(percpu=False) diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index 3618faf..da06893 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -16,9 +16,7 @@ class Module(bumblebee.module.Module): super(Module, self).__init__(output, config, alias) self._path = self._config.parameter("path", "/") -# TODO -# output.add_callback(module=self.__module__, button=1, -# cmd="nautilus {instance}") + output.add_callback(module=self.instance(), button=1, cmd="nautilus {instance}") def widgets(self): st = os.statvfs(self._path) @@ -33,6 +31,9 @@ class Module(bumblebee.module.Module): bumblebee.util.bytefmt(self._size), self._perc) ) + def instance(self, widget=None): + return self._path + def warning(self, widget): return self._perc > self._config.parameter("warning", 80) diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 0717daf..6b7070d 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -16,9 +16,7 @@ class Module(bumblebee.module.Module): super(Module, self).__init__(output, config, alias) self._mem = psutil.virtual_memory() -# TODO -# output.add_callback(module=self.__module__, button=1, -# cmd="gnome-system-monitor") + output.add_callback(module=self.instance(), button=1, cmd="gnome-system-monitor") def widgets(self): self._mem = psutil.virtual_memory() diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index 3e6c1a3..66412b8 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -36,15 +36,14 @@ class Module(bumblebee.module.Module): self._mute = False channel = "sink" if self._module == "pasink" else "source" -# TODO -# output.add_callback(module=self.__module__, button=3, -# cmd="pavucontrol") -# output.add_callback(module=self.__module__, button=1, -# cmd="pactl set-{}-mute @DEFAULT_{}@ toggle".format(channel, channel.upper())) -# output.add_callback(module=self.__module__, button=4, -# cmd="pactl set-{}-volume @DEFAULT_{}@ +2%".format(channel, channel.upper())) -# output.add_callback(module=self.__module__, button=5, -# cmd="pactl set-{}-volume @DEFAULT_{}@ -2%".format(channel, channel.upper())) + output.add_callback(module=self.instance(), button=3, + cmd="pavucontrol") + output.add_callback(module=self.instance(), button=1, + cmd="pactl set-{}-mute @DEFAULT_{}@ toggle".format(channel, channel.upper())) + output.add_callback(module=self.instance(), button=4, + cmd="pactl set-{}-volume @DEFAULT_{}@ +2%".format(channel, channel.upper())) + output.add_callback(module=self.instance(), button=5, + cmd="pactl set-{}-volume @DEFAULT_{}@ -2%".format(channel, channel.upper())) def widgets(self): res = subprocess.check_output(shlex.split("pactl info")) diff --git a/bumblebee/output.py b/bumblebee/output.py index be9bbb8..605da67 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -1,4 +1,8 @@ +import os +import shlex +import inspect import threading +import subprocess def output(args): import bumblebee.outputs.i3 @@ -28,15 +32,21 @@ class Widget(object): def module(self): return self._obj.__module__.split(".")[-1] - def name(self): - return self._obj.__module__ - def instance(self): - rv = getattr(self._obj, "instance")(self) + return getattr(self._obj, "instance")(self) def text(self): return self._text +class Command(object): + def __init__(self, command): + self._command = command + + def __call__(self, *args, **kwargs): + cmd = self._command.format(*args, **kwargs) + DEVNULL = open(os.devnull, 'wb') + subprocess.Popen(shlex.split(cmd), stdout=DEVNULL, stderr=DEVNULL) + class Output(object): def __init__(self, config): self._config = config @@ -52,22 +62,28 @@ class Output(object): def add_callback(self, cmd, button, module=None): if module: module = module.replace("bumblebee.modules.", "") + self._callbacks[( button, module, )] = cmd def callback(self, event): - cb = self._callbacks.get(( - event.get("button", -1), - event.get("name", None), - ), None) - if cb is not None: return cb cb = self._callbacks.get(( event.get("button", -1), None, ), None) - return cb + cb = self._callbacks.get(( + event.get("button", -1), + event.get("name", None), + ), cb) + cb = self._callbacks.get(( + event.get("button", -1), + event.get("instance", None), + ), cb) + if inspect.isfunction(cb) or cb is None: return cb + + return Command(cb) def wait(self): self._wait.wait(self._config.parameter("interval", 1)) diff --git a/bumblebee/outputs/i3.py b/bumblebee/outputs/i3.py index 4ed3970..0d83e7a 100644 --- a/bumblebee/outputs/i3.py +++ b/bumblebee/outputs/i3.py @@ -19,12 +19,11 @@ def read_input(output): 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) + cb( + name=event.get("name", ""), + instance=event.get("instance", ""), + button=event.get("button", -1) ) - subprocess.Popen(shlex.split(cb), stdout=DEVNULL, stderr=DEVNULL) output.redraw() class Output(bumblebee.output.Output): @@ -32,12 +31,11 @@ class Output(bumblebee.output.Output): super(Output, self).__init__(args) self._data = [] -# TODO -# self.add_callback("i3-msg workspace prev_on_output", 4) -# self.add_callback("i3-msg workspace next_on_output", 5) + self.add_callback("i3-msg workspace prev_on_output", 4) + self.add_callback("i3-msg workspace next_on_output", 5) -# self._thread = threading.Thread(target=read_input, args=(self,)) -# self._thread.start() + self._thread = threading.Thread(target=read_input, args=(self,)) + self._thread.start() def start(self): print json.dumps({ "version": 1, "click_events": True }) + "[" @@ -61,7 +59,7 @@ class Output(bumblebee.output.Output): ), "color": theme.color(widget), "background": theme.background(widget), - "name": widget.name(), + "name": widget.module(), "instance": widget.instance(), "separator": theme.default_separators(widget), "separator_block_width": theme.separator_block_width(widget),