Commit | Line | Data |
---|---|---|
fc1c0791 AIL |
1 | #!/usr/bin/env python3 |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # indicator-keyboard-led - simulate keyboard lock keys LED | |
5 | # Copyright (c) 2016 Adrian I Lam <adrianiainlam@gmail.com> | |
6 | # | |
7 | # Permission is hereby granted, free of charge, to any person obtaining a | |
8 | # copy of this software and associated documentation files (the "Software"), | |
9 | # to deal in the Software without restriction, including without limitation | |
10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | # and/or sell copies of the Software, and to permit persons to whom the | |
12 | # Software is furnished to do so, subject to the following conditions: | |
13 | # | |
14 | # The above copyright notice and this permission notice shall be included in | |
15 | # all copies or substantial portions of the Software. | |
16 | # | |
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | # THE AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | # IN THE SOFTWARE. | |
24 | # | |
25 | # I would like to thank Tobias Schlitt <toby@php.net>, who wrote | |
26 | # indicator-chars <https://github.com/tobyS/indicator-chars> which I used | |
27 | # as a reference when writing this software. | |
28 | ||
29 | import signal | |
30 | import subprocess | |
31 | from os import path | |
32 | import argparse | |
170ff396 | 33 | import sys |
fc1c0791 AIL |
34 | import gi |
35 | gi.require_version('Gdk', '3.0') | |
36 | gi.require_version('Gtk', '3.0') | |
37 | gi.require_version('AppIndicator3', '0.1') | |
38 | from gi.repository import Gdk, Gtk, AppIndicator3 | |
39 | ||
f462ecb9 AIL |
40 | SCRIPT_DIR = path.dirname(path.realpath(__file__)) |
41 | import gettext | |
42 | t = gettext.translation('default', path.join(SCRIPT_DIR, 'locale')) | |
43 | _ = t.gettext | |
44 | ||
fc1c0791 | 45 | class IndicatorKeyboardLED: |
f462ecb9 | 46 | locks = { 'N': _('Num'), 'C': _('Caps'), 'S': _('Scroll') } |
fc1c0791 | 47 | |
170ff396 | 48 | def __init__(self, short=False, order='NCS'): |
fc1c0791 AIL |
49 | self.indicator = AppIndicator3.Indicator.new( |
50 | 'indicator-keyboard-led', | |
f462ecb9 | 51 | path.join(SCRIPT_DIR, 'icon.svg'), |
fc1c0791 AIL |
52 | AppIndicator3.IndicatorCategory.APPLICATION_STATUS) |
53 | self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) | |
170ff396 | 54 | |
fc1c0791 | 55 | if short: |
f462ecb9 | 56 | self.locks = { 'N': _('N'), 'C': _('C'), 'S': _('S') } |
fc1c0791 AIL |
57 | |
58 | keymap = Gdk.Keymap.get_default() | |
170ff396 AIL |
59 | keymap.connect('state-changed', self.update_indicator, order) |
60 | self.update_indicator(keymap, order) | |
fc1c0791 AIL |
61 | |
62 | menu = Gtk.Menu() | |
63 | items = { | |
f462ecb9 AIL |
64 | 'N': Gtk.MenuItem.new_with_label(_('Num Lock')), |
65 | 'C': Gtk.MenuItem.new_with_label(_('Caps Lock')), | |
66 | 'S': Gtk.MenuItem.new_with_label(_('Scroll Lock')) | |
fc1c0791 | 67 | } |
170ff396 AIL |
68 | items['N'].connect('activate', self.send_keypress, 'Num_Lock') |
69 | items['C'].connect('activate', self.send_keypress, 'Caps_Lock') | |
70 | items['S'].connect('activate', self.send_keypress, 'Scroll_Lock') | |
71 | ||
72 | for i in order: | |
73 | menu.append(items[i]) | |
fc1c0791 | 74 | |
f462ecb9 | 75 | quit_item = Gtk.MenuItem.new_with_label(_('Quit')) |
170ff396 AIL |
76 | menu.append(Gtk.SeparatorMenuItem()) |
77 | menu.append(quit_item) | |
78 | quit_item.connect('activate', Gtk.main_quit) | |
fc1c0791 AIL |
79 | |
80 | self.indicator.set_menu(menu) | |
81 | menu.show_all() | |
82 | ||
170ff396 AIL |
83 | def update_indicator(self, keymap, order): |
84 | labels = [] | |
85 | for i in order: | |
86 | if i == 'N': | |
87 | state = keymap.get_num_lock_state() | |
88 | elif i == 'C': | |
89 | state = keymap.get_caps_lock_state() | |
90 | elif i == 'S': | |
91 | state = keymap.get_scroll_lock_state() | |
92 | else: | |
93 | raise ValueError('Invalid value in ORDER') | |
94 | labels += [('⚫' if state else '⚪') + self.locks[i]] | |
95 | self.indicator.set_label(' '.join(labels), '') | |
fc1c0791 AIL |
96 | |
97 | def send_keypress(self, menuitem, keystroke): | |
98 | subprocess.call(['xdotool', 'key', keystroke]) | |
99 | ||
170ff396 AIL |
100 | def validate_order(args): |
101 | args.order = args.order.upper() | |
102 | for i in args.order: | |
103 | if i not in ['N', 'C', 'S']: | |
104 | sys.exit('Illegal character in ORDER. (Choices: [N, C, S])') | |
105 | if len(args.order) != len(set(args.order)): | |
106 | sys.exit('Repeated character in ORDER. ' | |
107 | 'Please specify each lock at most once.') | |
108 | ||
fc1c0791 AIL |
109 | if __name__ == '__main__': |
110 | signal.signal(signal.SIGINT, lambda signum, frame: Gtk.main_quit()) | |
170ff396 | 111 | |
fc1c0791 | 112 | parser = argparse.ArgumentParser( |
170ff396 AIL |
113 | description='indicator-keyboard-led - simulate keyboard lock keys LED', |
114 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
f462ecb9 AIL |
115 | parser.add_argument('-s', '--short', required=False, action='store_true', |
116 | help='use short label, i.e. ⚫N ⚫C ⚫S instead of ⚫Num ⚫Caps ⚫Scroll') | |
170ff396 AIL |
117 | parser.add_argument('-o', '--order', required=False, default='NCS', |
118 | help='specify the order of the locks displayed, e.g. CSN for ' | |
119 | '⚫Caps ⚫Scroll ⚫Num, or NC for ⚫Num ⚫Caps without Scroll lock') | |
fc1c0791 | 120 | args = parser.parse_args() |
170ff396 AIL |
121 | validate_order(args) |
122 | ||
123 | IndicatorKeyboardLED(short=args.short, order=args.order) | |
fc1c0791 | 124 | Gtk.main() |