bumblebee-status/modules/contrib/layout.py

73 lines
2.2 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
"""
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:
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
try:
2020-04-24 16:16:14 +02:00
bumblebee.util.execute('setxkbmap -layout {} -variant {}'.format(','.join(layout_list), ','.join(variant_list)))
2020-04-24 16:15:42 +02:00
except RuntimeError:
pass
def get_layouts(self):
try:
2020-04-24 16:16:14 +02:00
res = bumblebee.util.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