2 # -*- coding: utf-8 -*-
4 # indicator-keyboard-led - simulate keyboard lock keys LED
5 # Copyright (c) 2017 Adrian I Lam <me@adrianiainlam.tk>
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
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.
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
40 APP_NAME
= 'indicator-keyboard-led'
43 ICON_LOCATION
= '/usr/share/icons/hicolor/scalable/apps/' + APP_NAME
+ '.svg'
45 t
= gettext
.translation(APP_NAME
, '/usr/share/locale')
48 class IndicatorKeyboardLED
:
49 def __init__(self
, short
=False, order
='NCS', xdotool
=None):
50 self
.validate_order(order
)
52 self
.indicator
= AppIndicator3
.Indicator
.new(
53 APP_NAME
, ICON_LOCATION
,
54 AppIndicator3
.IndicatorCategory
.APPLICATION_STATUS
)
55 self
.indicator
.set_status(AppIndicator3
.IndicatorStatus
.ACTIVE
)
58 self
.locks
= { 'N': _('N'), 'C': _('C'), 'S': _('S') }
60 self
.locks
= { 'N': _('Num'), 'C': _('Caps'), 'S': _('Scroll') }
62 keymap
= Gdk
.Keymap
.get_default()
63 keymap
.connect('state-changed', self
.update_indicator
, order
)
64 self
.update_indicator(keymap
, order
)
65 self
.create_menu(xdotool
, order
)
67 def create_menu(self
, xdotool
, order
):
69 xdotool
= xdotool
or shutil
.which('xdotool')
70 if xdotool
and os
.access(xdotool
, os
.X_OK
) and os
.path
.isfile(xdotool
):
71 def send_keypress(menuitem
, keystroke
):
72 subprocess
.call([xdotool
, 'key', keystroke
])
73 def new_menu_item(itemtype
):
75 item
= Gtk
.MenuItem
.new_with_label(_('Num Lock'))
76 item
.connect('activate', send_keypress
, 'Num_Lock')
78 item
= Gtk
.MenuItem
.new_with_label(_('Caps Lock'))
79 item
.connect('activate', send_keypress
, 'Caps_Lock')
81 item
= Gtk
.MenuItem
.new_with_label(_('Scroll Lock'))
82 item
.connect('activate', send_keypress
, 'Scroll_Lock')
84 raise ValueError('Invalid itemtype')
88 menu
.append(new_menu_item(i
))
89 menu
.append(Gtk
.SeparatorMenuItem())
91 quit_item
= Gtk
.MenuItem
.new_with_label(_('Quit'))
92 menu
.append(quit_item
)
93 quit_item
.connect('activate', Gtk
.main_quit
)
95 self
.indicator
.set_menu(menu
)
98 def update_indicator(self
, keymap
, order
):
102 state
= keymap
.get_num_lock_state()
104 state
= keymap
.get_caps_lock_state()
106 state
= keymap
.get_scroll_lock_state()
108 raise ValueError('Invalid value in ORDER')
109 labels
+= [('⚫' if state
else '⚪') + self
.locks
[i
]]
110 self
.indicator
.set_label(' '.join(labels
), '')
112 def validate_order(self
, order
):
113 order
= order
.upper()
115 if i
not in ['N', 'C', 'S']:
116 raise ValueError('Illegal character in ORDER. '
117 '(Choices: [N, C, S])')
118 if len(order
) != len(set(order
)):
119 raise ValueError('Repeated character in ORDER. '
120 'Please specify each lock at most once.')
121 if 'S' in order
and Gtk
.check_version(3, 18, 0) is not None:
122 # Silently drop Scroll lock if GTK <= 3.18
126 if __name__
== '__main__':
127 signal
.signal(signal
.SIGINT
, lambda signum
, frame
: Gtk
.main_quit())
129 parser
= argparse
.ArgumentParser(
130 description
='indicator-keyboard-led - simulate keyboard lock keys LED',
131 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
132 parser
.add_argument('-s', '--short', required
=False, action
='store_true',
133 help='use short label, i.e. ⚫N ⚫C ⚫S instead of ⚫Num ⚫Caps ⚫Scroll')
134 parser
.add_argument('-o', '--order', required
=False, default
='NCS',
135 help='specify the order of the locks displayed, e.g. CSN for '
136 '⚫Caps ⚫Scroll ⚫Num, or NC for ⚫Num ⚫Caps without Scroll lock')
137 parser
.add_argument('-x', '--xdotool', required
=False,
138 help='provide full path to xdotool executable, '
139 'e.g. "/usr/bin/xdotool"; '
140 'if not specified, search in PATH')
141 args
= parser
.parse_args()
143 IndicatorKeyboardLED(short
=args
.short
, order
=args
.order
,
144 xdotool
=args
.xdotool
)