| 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 |
| 33 | import sys |
| 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 | |
| 40 | class IndicatorKeyboardLED: |
| 41 | locks = { 'N': 'Num', 'C': 'Caps', 'S': 'Scroll' } |
| 42 | |
| 43 | def __init__(self, short=False, order='NCS'): |
| 44 | self.indicator = AppIndicator3.Indicator.new( |
| 45 | 'indicator-keyboard-led', |
| 46 | path.join(path.dirname(path.realpath(__file__)), 'icon.svg'), |
| 47 | AppIndicator3.IndicatorCategory.APPLICATION_STATUS) |
| 48 | self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) |
| 49 | |
| 50 | if short: |
| 51 | self.locks = { 'N': 'N', 'C': 'C', 'S': 'S' } |
| 52 | |
| 53 | keymap = Gdk.Keymap.get_default() |
| 54 | keymap.connect('state-changed', self.update_indicator, order) |
| 55 | self.update_indicator(keymap, order) |
| 56 | |
| 57 | menu = Gtk.Menu() |
| 58 | items = { |
| 59 | 'N': Gtk.MenuItem.new_with_label('Num Lock'), |
| 60 | 'C': Gtk.MenuItem.new_with_label('Caps Lock'), |
| 61 | 'S': Gtk.MenuItem.new_with_label('Scroll Lock') |
| 62 | } |
| 63 | items['N'].connect('activate', self.send_keypress, 'Num_Lock') |
| 64 | items['C'].connect('activate', self.send_keypress, 'Caps_Lock') |
| 65 | items['S'].connect('activate', self.send_keypress, 'Scroll_Lock') |
| 66 | |
| 67 | for i in order: |
| 68 | menu.append(items[i]) |
| 69 | |
| 70 | quit_item = Gtk.MenuItem.new_with_label('Quit') |
| 71 | menu.append(Gtk.SeparatorMenuItem()) |
| 72 | menu.append(quit_item) |
| 73 | quit_item.connect('activate', Gtk.main_quit) |
| 74 | |
| 75 | self.indicator.set_menu(menu) |
| 76 | menu.show_all() |
| 77 | |
| 78 | def update_indicator(self, keymap, order): |
| 79 | labels = [] |
| 80 | for i in order: |
| 81 | if i == 'N': |
| 82 | state = keymap.get_num_lock_state() |
| 83 | elif i == 'C': |
| 84 | state = keymap.get_caps_lock_state() |
| 85 | elif i == 'S': |
| 86 | state = keymap.get_scroll_lock_state() |
| 87 | else: |
| 88 | raise ValueError('Invalid value in ORDER') |
| 89 | labels += [('⚫' if state else '⚪') + self.locks[i]] |
| 90 | self.indicator.set_label(' '.join(labels), '') |
| 91 | |
| 92 | def send_keypress(self, menuitem, keystroke): |
| 93 | subprocess.call(['xdotool', 'key', keystroke]) |
| 94 | |
| 95 | def validate_order(args): |
| 96 | args.order = args.order.upper() |
| 97 | for i in args.order: |
| 98 | if i not in ['N', 'C', 'S']: |
| 99 | sys.exit('Illegal character in ORDER. (Choices: [N, C, S])') |
| 100 | if len(args.order) != len(set(args.order)): |
| 101 | sys.exit('Repeated character in ORDER. ' |
| 102 | 'Please specify each lock at most once.') |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | signal.signal(signal.SIGINT, lambda signum, frame: Gtk.main_quit()) |
| 106 | |
| 107 | parser = argparse.ArgumentParser( |
| 108 | description='indicator-keyboard-led - simulate keyboard lock keys LED', |
| 109 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 110 | parser.add_argument('-s', '--short', action='store_true', |
| 111 | help='use short label, i.e. ⚫N ⚫C ⚫S instead of ⚫Num ⚫Caps ⚫Scroll', |
| 112 | required=False) |
| 113 | parser.add_argument('-o', '--order', required=False, default='NCS', |
| 114 | help='specify the order of the locks displayed, e.g. CSN for ' |
| 115 | '⚫Caps ⚫Scroll ⚫Num, or NC for ⚫Num ⚫Caps without Scroll lock') |
| 116 | args = parser.parse_args() |
| 117 | validate_order(args) |
| 118 | |
| 119 | IndicatorKeyboardLED(short=args.short, order=args.order) |
| 120 | Gtk.main() |