From 810c0b68e0197defac208a9adbb00cf66ba99602 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Fri, 24 Apr 2020 16:15:42 +0200 Subject: [PATCH] [modules] re-enable layout --- modules/contrib/layout.py | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 modules/contrib/layout.py diff --git a/modules/contrib/layout.py b/modules/contrib/layout.py new file mode 100644 index 0000000..49c59a4 --- /dev/null +++ b/modules/contrib/layout.py @@ -0,0 +1,72 @@ +# pylint: disable=C0111,R0903 + +"""Displays and changes the current keyboard layout + +Requires the following executable: + * setxkbmap +""" + +import bumblebee.util +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.current_layout) + ) + engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, + cmd=self._next_keymap) + engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, + cmd=self._prev_keymap) + + def _next_keymap(self, event): + self._set_keymap(1) + + def _prev_keymap(self, event): + self._set_keymap(-1) + + def _set_keymap(self, rotation): + layouts = self.get_layouts() + if len(layouts) == 1: return # nothing to do + layouts = layouts[rotation:] + layouts[:rotation] + + layout_list = [] + variant_list = [] + for l in layouts: + tmp = l.split(":") + layout_list.append(tmp[0]) + variant_list.append(tmp[1] if len(tmp) > 1 else "") + + try: + bumblebee.util.execute("setxkbmap -layout {} -variant {}".format(",".join(layout_list), ",".join(variant_list))) + except RuntimeError: + pass + + def get_layouts(self): + try: + res = bumblebee.util.execute("setxkbmap -query") + except RuntimeError: + return ["n/a"] + layouts = [] + variants = [] + for line in res.split("\n"): + if not line: continue + if "layout" in line: + layouts = line.split(":")[1].strip().split(",") + if "variant" in line: + variants = line.split(":")[1].strip().split(",") + + result = [] + for idx, layout in enumerate(layouts): + if len(variants) > idx and variants[idx]: + layout = "{}:{}".format(layout, variants[idx]) + result.append(layout) + return result if len(result) > 0 else ["n/a"] + + def current_layout(self, widget): + layouts = self.get_layouts() + return layouts[0] + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4