[modules/layout] Add rotation logic and language parameter

User can now specify a list of languages as parameter (pipe-separated
list). Variants can also be specified by separating language and variant
with a :

For instance: -p layout.lang="us|rs:latin"

Left click moves on to the next language, right click to the previous.

Right now, there are the following caveats:
* The first entry in the list must be the language used when the bar
  starts
* kxbd changes outside the bar are not picked up automatically
This commit is contained in:
Tobi-wan Kenobi 2016-11-27 18:08:22 +01:00
parent 1f1e7748a3
commit 60d96506e8
2 changed files with 25 additions and 21 deletions

View file

@ -6,36 +6,40 @@ import bumblebee.util
def description(): def description():
return "Showws current keyboard layout and change it on click." return "Showws current keyboard layout and change it on click."
#def parameters(): def parameters():
# module = __name__.split(".")[-1] return [
# return "layout.lang: pipe-separated list of languages to cycle through (e.g. us|rs|de). Default: en"
]
class Module(bumblebee.module.Module): class Module(bumblebee.module.Module):
def __init__(self, output, config, alias): def __init__(self, output, config, alias):
super(Module, self).__init__(output, config, alias) super(Module, self).__init__(output, config, alias)
self._languages = self._config.parameter("lang", "en").split("|")
self._idx = 0
# click output.add_callback(module=self.instance(), button=1, cmd=self.next_keymap)
output.add_callback(module="layout.cir", button=1, cmd="setxkbmap us") output.add_callback(module=self.instance(), button=3, cmd=self.prev_keymap)
output.add_callback(module="layout.us", button=1, cmd="setxkbmap rs")
output.add_callback(module="layout.rs", button=1, cmd="setxkbmap -layout rs -variant latin")
def next_keymap(self, event, widget):
self._idx = self._idx + 1 if self._idx < len(self._languages) - 1 else 0
self.set_keymap()
def prev_keymap(self, event, widget):
self._idx = self._idx - 1 if self._idx > 0 else len(self._languages) - 1
self.set_keymap()
def set_keymap(self):
tmp = self._languages[self._idx].split(":")
layout = tmp[0]
variant = ""
if len(tmp) > 1:
variant = "-variant {}".format(tmp[1])
bumblebee.util.execute("setxkbmap -layout {} {}".format(layout, variant))
def widgets(self): def widgets(self):
output = subprocess.check_output(["setxkbmap", "-print"], stderr=subprocess.STDOUT) lang = self._languages[self._idx]
for line in str(output).split("\\n"): return bumblebee.output.Widget(self, lang)
if "xkb_symbols" in line:
res = line.split("\"")[1].split("+")[1]
if res == "rs":
return bumblebee.output.Widget(self, res, instance="layout.rs")
elif res == "us":
return bumblebee.output.Widget(self, res, instance="layout.us")
else:
return bumblebee.output.Widget(self, res, instance="layout.cir")
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -93,10 +93,10 @@ class Output(object):
), None) ), None)
cb = self._callbacks.get(( cb = self._callbacks.get((
event.get("button", -1), event.get("button", -1),
event.get("instance", event.get("name", None)), event.get("instance", event.get("module", None)),
), cb) ), cb)
identity = event.get("instance", event.get("name", None)) identity = event.get("instance", event.get("module", None))
return Command(cb, event, self._widgets.get(identity, None)) return Command(cb, event, self._widgets.get(identity, None))
def wait(self): def wait(self):