Source

small_scripts / setxkbmap_dialog.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# vi:ts=4 sw=4 et

# This program is a simple and easy-to-use interface to setxkbmap.
#
# It can set keyboard options for only one keyboard (instead of all connected
# keyboards).
#
# It has some hardcoded layouts, variants and options that are useful for me.
# If you want to use this script and you are not from Brazil, you will probably
# want to change the hardcoded menu items below.
#
# No, I don't want to "bloat" this script adding an external configuration
# file. The script itself *is* the configuration file.
#
#
# Tested with:
#   Python        2.7.1         http://www.python.org/
#   pythondialog  2.7           http://pythondialog.sourceforge.net/
#   dialog        1.1.20100428  http://invisible-island.net/dialog/dialog.html
#   xinput        1.5.3         http://xorg.freedesktop.org/
#   xorg-server   1.10.4        http://xorg.freedesktop.org/
#   xorg-x11      7.4           http://xorg.freedesktop.org/


from __future__ import print_function

import re
import subprocess
import dialog


# Sample 'xinput -list' output:
#⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
#⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
#⎜   ↳ Microsoft  Microsoft Basic Optical Mouse v2.0 	id=10	[slave  pointer  (2)]
#⎜   ↳ SynPS/2 Synaptics TouchPad              	id=13	[slave  pointer  (2)]
#⎜   ↳ Microsoft Microsoft® 2.4GHz Transceiver v8.0	id=14	[slave  pointer  (2)]
#⎜   ↳ Microsoft Microsoft® 2.4GHz Transceiver v8.0	id=15	[slave  pointer  (2)]
#⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
#    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
#    ↳ Power Button                            	id=6	[slave  keyboard (3)]
#    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
#    ↳ Sleep Button                            	id=8	[slave  keyboard (3)]
#    ↳ USB2.0 1.3M UVC WebCam                  	id=9	[slave  keyboard (3)]
#    ↳ Asus Laptop extra buttons               	id=11	[slave  keyboard (3)]
#    ↳ AT Translated Set 2 keyboard            	id=12	[slave  keyboard (3)]
#    ↳ Microsoft Microsoft® 2.4GHz Transceiver v8.0	id=16	[slave  keyboard (3)]

RE_XINPUT_LIST = re.compile(ur'''
    ^
    [\u239b-\u23bf]?  # Ignoring the leading "box" char
    \s*
    [\u2190-\u21ff]?  # Ignoring the arrow
    \s*
    (?P<name>.+?)
    \s+
    id=(?P<id>[0-9]+)
    \s+
    (?P<typestring>\[.+?\])
    \s*$
    ''',
    re.VERBOSE
)

RE_XINPUT_DEVICE_TYPE = re.compile(r'''
    ^ \s* \[? \s*
    (?P<typename>[a-zA-Z ]+?)
    \s*
    (?:
        \(
        (?P<attachment>[0-9]+)
        \)
    )?
    \s* \]? \s* $
    ''',
    re.VERBOSE
)

RE_SIMPLE_STRING = re.compile(r'^[-a-zA-Z0-9_/:.,=+]*$')


def arg_to_shell_string(arg):
    '''Receives a string and tries to quote it to be shell-safe.
    Useful when printing what command-line will be run.

    Warning: tab and newline characters are not escaped.
    '''

    if arg == '':
        # If the string is empty, return an empty string surrounded by
        # single-quotes.
        return "''"

    match = RE_SIMPLE_STRING.match(arg)
    if match:
        # If the string is simple enough, there is no need for quotes or
        # escapes.
        return arg

    # Enclose the string into single-quotes.
    # Must also escape single-quotes. Things get ugly, but should work.
    # echo 'aaa'"'"'bbb' => aaa'bbb
    return "'" + arg.replace("'", "'\"'\"'") + "'"


def args_to_cmdstring(args):
    return ' '.join(arg_to_shell_string(arg) for arg in args)


def echo_run(args):
    '''Prints the commandline and then runs it.'''
    cmdstring = args_to_cmdstring(args)
    print(cmdstring)
    subprocess.call(args)


class XInputDeviceType(object):
    def __init__(self, typestring):
        # Default values
        self.is_slave = False
        self.is_master = False
        self.is_pointer = False
        self.is_keyboard = False
        self.attachment = None

        self.typestring = typestring
        match = RE_XINPUT_DEVICE_TYPE.match(typestring)
        if match:
            self.typename = match.group('typename')
            self.attachment = match.group('attachment')

            if 'slave' in self.typename: self.is_slave = True
            if 'master' in self.typename: self.is_master = True
            if 'pointer' in self.typename: self.is_pointer = True
            if 'keyboard' in self.typename: self.is_keyboard = True
        else:
            self.typename = 'unknown'

    def __str__(self):
        return self.typestring

    def __repr__(self):
        return 'XInputDeviceType({0})'.format(
            repr(self.typestring)
        )


class XInputDevice(object):
    def __init__(self, name, id, typestring):
        self.name = name   # XIDeviceInfo->name
        self.id = int(id)  # XIDeviceInfo->deviceid
        self.type = XInputDeviceType(typestring)

    def __str__(self):
        return '{0:2} {1} {2}'.format(
            self.id,
            self.name,
            self.type
        )

    def __repr__(self):
        return 'XInputDevice({0}, {1}, {2})'.format(
            repr(self.name),
            repr(self.id),
            repr(self.type),
        )


def list_devices_from_xinput():
    '''Returns a list of devices.'''

    devices = []
    output = subprocess.check_output(['xinput', '-list'])
    output = output.decode('utf8')
    for line in output.splitlines():
        match = RE_XINPUT_LIST.match(line)
        if match:
            dev = XInputDevice(**match.groupdict())
            devices.append(dev)
        else:
            # else, ignoring unknown lines
            pass
    return devices


class Program(object):
    def __init__(self):
        self.d = dialog.Dialog(dialog='dialog')
        self.d.add_persistent_args([
            '--tab-correct',
            '--backtitle', 'setxkbmap interactive dialog',
        ])

    def setxkbmap_main_menu(self):
        choices = [
            ('-query', ''),
            ('-print', ''),
            ('Change settings', ''),
        ]

        (returncode, tag) = self.d.menu(
            'Select an action',
            title='setxkbmap',
            choices=choices,
            height=0, width=0, menu_height=0
        )
        if returncode == 0:
            # -verbose 10
            if tag == '-query':
                echo_run(['setxkbmap', '-query'])
            elif tag == '-print':
                echo_run(['setxkbmap', '-print'])
            elif tag == 'Change settings':
                self.setxkbmap_change_settings()
        else:
            return None

    def select_keyboard_dialog(self):
        '''Opens a dialog and returns the keyboard id selected by the user.
        Returns None if the user cancels.
        '''

        choices = [
            (
                str(dev.id),
                u'{0} {1}'.format(dev.name, dev.type.typestring)
            )
            for dev in list_devices_from_xinput()
            if dev.type.is_keyboard
        ]
        choices.insert(0, ('all', '-all-'))

        (returncode, tag) = self.d.menu(
            'Select a keyboard:',
            title='X Input keyboards',
            choices=choices,
            height=0, width=0, menu_height=0
        )
        if returncode == 0:
            if tag == 'all':
                return tag
            else:
                return int(tag)
        else:
            return None

    def select_layout_variant_dialog(self):
        '''Opens a dialog and returns a tuple (layout, variant).
        Returns None if the user cancels.
        '''

        choices = [
            ('_',                  'don\'t change'),
            ('br_abnt2',           'Portuguese (Brazil)'),
            ('br_dvorak',          'Portuguese (Brazil, Dvorak)'),
            ('us_basic',           'English (US)'),
            ('us_intl',            'English (US, international with dead keys)'),
            ('us_alt-intl',        'English (US, alternative international)'),
            ('us_dvorak',          'English (Dvorak)'),
            ('us_dvorak-intl',     'English (Dvorak international with dead keys)'),
            ('us_dvorak-alt-intl', 'English (Dvorak alternative international no dead keys)'),
            ('us_dvorak-l',        'English (left handed Dvorak)'),
            ('us_dvorak-r',        'English (right handed Dvorak)'),
            ('us_dvorak-classic',  'English (classic Dvorak)'),
            ('us_dvp',             'English (programmer Dvorak)'),
            ('us_mac',             'English (Macintosh)'),
            ('us_altgr-intl',      'English (international AltGr dead keys)'),
            ('us_unicode',         'English (US, international AltGr Unicode combining)'),
        ]

        (returncode, tag) = self.d.menu(
            'Select a keyboard layout+variant:',
            title='Keyboard layouts and variants',
            choices=choices,
            height=0, width=0, menu_height=0
        )
        if returncode == 0:
            tmp = tag.partition('_')
            return (tmp[0], tmp[2])
        else:
            return None

    def select_options_dialog(self):
        '''Opens a dialog and returns a list of selected options.
        Returns None if the user cancels.
        '''

        choices = [
            ('CLEAR',                   'Clear all previously set options', 1),

            ('>> keypad',               'Numeric keypad layout selection', 0),
            ('keypad:legacy',           'Legacy', 0),
            ('keypad:oss',              'Unicode additions (arrows and math operators)', 0),
            ('keypad:hex',              'Hexadecimal', 0),
            ('keypad:atm',              'ATM/phone-style', 0),

            ('>> caps',                 'Caps Lock key behavior', 0),
            ('caps:escape',             'Make Caps Lock an additional ESC', 1),
            ('caps:backspace',          'Make Caps Lock an additional Backspace', 0),
            ('caps:super',              'Make Caps Lock an additional Super', 0),
            ('caps:hyper',              'Make Caps Lock an additional Hyper', 0),
            ('caps:shiftlock',          'Caps Lock toggles Shift so all keys are affected', 0),
            ('caps:none',               'Caps Lock is disabled', 0),

            ('>> compat',               'Miscellaneous compatibility options', 0),
            ('numpad:pc',               'Default numeric keypad keys', 0),
            ('numpad:mac',              'Numeric keypad keys work as with Macintosh', 0),
            ('numpad:microsoft',        'Shift with numeric keypad keys works as in MS Windows', 1),
            ('numpad:shift3',           'Shift does not cancel Num Lock, chooses 3rd level instead', 0),
            ('apple:alupckeys',         'Apple Aluminium Keyboard: emulate PC keys (Print, Scroll Lock, Pause, Num Lock)', 0),

            ('>> terminate',            'Key sequence to kill the X server', 0),
            ('terminate:ctrl_alt_bksp', 'Control + Alt + Backspace', 0),

            ('>> altwin',               'Alt/Win key behavior', 0),
            ('altwin:menu',             'Add the standard behavior to Menu key', 0),
            ('altwin:meta_alt',         'Alt and Meta are on Alt keys', 0),
            ('altwin:ctrl_win',         'Control is mapped to Win keys (and the usual Ctrl keys)', 0),
            ('altwin:ctrl_alt_win',     'Control is mapped to Alt keys, Alt is mapped to Win keys', 0),
            ('altwin:meta_win',         'Meta is mapped to Win keys', 0),
            ('altwin:left_meta_win',    'Meta is mapped to Left Win', 0),
            ('altwin:hyper_win',        'Hyper is mapped to Win-keys', 0),
            ('altwin:alt_super_win',    'Alt is mapped to Right Win, Super to Menu', 0),
            ('altwin:swap_lalt_lwin',   'Left Alt is swapped with Left Win', 0),
            # This one is now default, no need to explicitly set it.
            # https://bugs.freedesktop.org/show_bug.cgi?id=19500
            # http://cgit.freedesktop.org/xkeyboard-config/commit/symbols/altwin?id=5de02aa07a8d4bbe1957af3a38212c3507f2436f
            #('altwin:super_win',        '', 1),

            ('>> Compose key',          'Compose key position', 0),
            ('compose:ralt',            'Right Alt', 0),
            ('compose:lwin',            'Left Win', 0),
            ('compose:rwin',            'Right Win', 0),
            ('compose:menu',            'Menu', 1),
            ('compose:lctrl',           'Left Ctrl', 0),
            ('compose:rctrl',           'Right Ctrl', 0),
            ('compose:caps',            'Caps Lock', 0),
            ('compose:102',             '<Less/Greater>', 0),
            ('compose:paus',            'Pause', 0),
            ('compose:prsc',            'PrtSc', 0),
            ('compose:sclk',            'Scroll Lock', 0),
        ]

        (returncode, tags) = self.d.checklist(
            'Select the desired options:',
            title='Keyboard options',
            choices=choices,
            height=0, width=0, list_height=0
        )
        if returncode == 0:
            tags = [tag for tag in tags if not tag.startswith('>>')]
            if tags[0] == 'CLEAR':
                tags[0] = ''
            return tags
        else:
            return None

    def confirm_run_command(self, command):
        returncode = self.d.yesno(
            'About to run the following command:\n'
            '\n'
            '{0}'
            '\n'
            '\n'
            'Confirm?'.format(command),
            title='Confirm command',
            height=0, width=0
        )
        if returncode == 0:
            return True
        elif returncode == 1:
            return False
        else:
            return None

    def setxkbmap_change_settings(self):
        setxkbmap_cmdline = ['setxkbmap']

        # Selecting a keyboard
        kbd = self.select_keyboard_dialog()
        if kbd is None:
            return
        else:
            if kbd == 'all':
                pass
            else:
                setxkbmap_cmdline.extend(['-device', str(kbd)])

        # Choosing a layout
        lv = self.select_layout_variant_dialog()
        if lv is None:
            return
        else:
            layout, variant = lv
            if layout:
                setxkbmap_cmdline.extend(['-layout', layout])
            if variant:
                setxkbmap_cmdline.extend(['-variant', variant])

        # Choosing options
        opts = self.select_options_dialog()
        if opts is None:
            return
        else:
            for opt in opts:
                setxkbmap_cmdline.extend(['-option', opt])

        # Final confirmation
        cmdstring = args_to_cmdstring(setxkbmap_cmdline)
        confirm = self.confirm_run_command(cmdstring)
        #if confirm is None:
        #    return
        if confirm:
            echo_run(setxkbmap_cmdline)

    def run(self):
        self.setxkbmap_main_menu()


if __name__ == '__main__':
    p = Program()
    p.run()