File: layout.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (78 lines) | stat: -rw-r--r-- 2,280 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# pylint: disable=C0111,R0903

"""Displays and changes the current keyboard layout

Requires the following executable:
    * setxkbmap

contributed by `Pseudonick47 <https://github.com/Pseudonick47>`_ - many thanks!
"""

import core.module
import core.widget
import core.input

import util.cli


class Module(core.module.Module):
    def __init__(self, config, theme):
        super().__init__(config, theme, core.widget.Widget(self.current_layout))

        core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__next_keymap)
        core.input.register(self, button=core.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 "")

        util.cli.execute(
            "setxkbmap -layout {} -variant {}".format(
                ",".join(layout_list), ",".join(variant_list)
            ),
            ignore_errors=True,
        )

    def get_layouts(self):
        try:
            res = util.cli.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