bumblebee-status/modules/contrib/layout.py

70 lines
2.1 KiB
Python
Raw Normal View History

2020-04-24 16:15:42 +02:00
# pylint: disable=C0111,R0903
"""Displays and changes the current keyboard layout
Requires the following executable:
* setxkbmap
"""
2020-04-24 16:18:19 +02:00
import core.module
import core.widget
import core.input
2020-04-24 16:15:42 +02:00
2020-04-24 16:18:19 +02:00
import util.cli
2020-04-24 16:15:42 +02:00
2020-04-24 16:18:19 +02:00
class Module(core.module.Module):
def __init__(self, config):
super().__init__(config, 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):
2020-04-24 16:15:42 +02:00
self._set_keymap(1)
2020-04-24 16:18:19 +02:00
def __prev_keymap(self, event):
2020-04-24 16:15:42 +02:00
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:
2020-04-24 16:16:14 +02:00
tmp = l.split(':')
2020-04-24 16:15:42 +02:00
layout_list.append(tmp[0])
2020-04-24 16:16:14 +02:00
variant_list.append(tmp[1] if len(tmp) > 1 else '')
2020-04-24 16:15:42 +02:00
2020-04-24 16:18:19 +02:00
util.cli.execute('setxkbmap -layout {} -variant {}'.format(','.join(layout_list), ','.join(variant_list)), ignore_errors=True)
2020-04-24 16:15:42 +02:00
def get_layouts(self):
try:
2020-04-24 16:18:19 +02:00
res = util.cli.execute('setxkbmap -query')
2020-04-24 16:15:42 +02:00
except RuntimeError:
2020-04-24 16:16:14 +02:00
return ['n/a']
2020-04-24 16:15:42 +02:00
layouts = []
variants = []
2020-04-24 16:16:14 +02:00
for line in res.split('\n'):
2020-04-24 16:15:42 +02:00
if not line: continue
2020-04-24 16:16:14 +02:00
if 'layout' in line:
layouts = line.split(':')[1].strip().split(',')
if 'variant' in line:
variants = line.split(':')[1].strip().split(',')
2020-04-24 16:15:42 +02:00
result = []
for idx, layout in enumerate(layouts):
if len(variants) > idx and variants[idx]:
2020-04-24 16:16:14 +02:00
layout = '{}:{}'.format(layout, variants[idx])
2020-04-24 16:15:42 +02:00
result.append(layout)
2020-04-24 16:16:14 +02:00
return result if len(result) > 0 else ['n/a']
2020-04-24 16:15:42 +02:00
def current_layout(self, widget):
layouts = self.get_layouts()
return layouts[0]
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4