2020-04-19 09:57:33 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the current keyboard layout using libX11
|
|
|
|
|
|
|
|
Requires the following library:
|
|
|
|
* libX11.so.6
|
|
|
|
and python module:
|
|
|
|
* xkbgroup
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* layout-xkb.showname: Boolean that indicate whether the full name should be displayed. Defaults to false (only the symbol will be displayed)
|
|
|
|
* layout-xkb.show_variant: Boolean that indecates whether the variant name should be displayed. Defaults to true.
|
|
|
|
"""
|
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
from xkbgroup import *
|
2020-04-19 09:57:33 +02:00
|
|
|
|
|
|
|
import logging
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-19 09:57:33 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.current_layout))
|
2020-04-19 09:57:33 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
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)
|
|
|
|
self.__show_variant = util.format.asbool(self.parameter("show_variant", True))
|
2020-04-19 09:57:33 +02:00
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
def __next_keymap(self, event):
|
|
|
|
self.__set_keymap(1)
|
2020-04-19 09:57:33 +02:00
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
def __prev_keymap(self, event):
|
|
|
|
self.__set_keymap(-1)
|
2020-04-19 09:57:33 +02:00
|
|
|
|
2020-04-19 10:13:44 +02:00
|
|
|
def __set_keymap(self, rotation):
|
2020-04-19 09:57:33 +02:00
|
|
|
xkb = XKeyboard()
|
2020-05-03 11:15:52 +02:00
|
|
|
if xkb.groups_count < 2:
|
|
|
|
return # nothing to do
|
2020-04-19 09:57:33 +02:00
|
|
|
layouts = xkb.groups_symbols
|
|
|
|
idx = layouts.index(xkb.group_symbol)
|
|
|
|
xkb.group_symbol = str(layouts[(idx + rotation) % len(layouts)])
|
|
|
|
|
|
|
|
def current_layout(self, widget):
|
|
|
|
try:
|
|
|
|
xkb = XKeyboard()
|
2020-05-03 11:15:52 +02:00
|
|
|
log.debug("group num: {}".format(xkb.group_num))
|
|
|
|
name = (
|
|
|
|
xkb.group_name
|
|
|
|
if util.format.asbool(self.parameter("showname"), False)
|
|
|
|
else xkb.group_symbol
|
|
|
|
)
|
2020-04-19 10:13:44 +02:00
|
|
|
if self.__show_variant:
|
2020-05-03 11:15:52 +02:00
|
|
|
return (
|
|
|
|
"{} ({})".format(name, xkb.group_variant)
|
|
|
|
if xkb.group_variant
|
|
|
|
else name
|
|
|
|
)
|
2020-04-19 09:57:33 +02:00
|
|
|
return name
|
2020-04-19 10:13:44 +02:00
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
print("got exception: {}".format(e))
|
|
|
|
return "n/a"
|
|
|
|
|
2020-04-19 09:57:33 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|