Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """A dmenu setxkbmap wrapper."""
  3.  
  4. import os
  5. import subprocess
  6. import re
  7. import sys
  8.  
  9. dmenu_command = "dmenu"
  10. setxkbmap_command = "setxkbmap"
  11.  
  12. def current_layout():
  13. """Gets the current layout (as LAYOUT[ VARIANT])."""
  14. full_cmd = f"{setxkbmap_command} -query"
  15. code, output = subprocess.getstatusoutput(full_cmd)
  16. if code != 0:
  17. raise Exception(f"'{full_cmd}' failed")
  18.  
  19. match_layout = re.search(r"layout:\s+(\S+)", output)
  20. if match_layout:
  21. layout = match_layout.group(1)
  22. else:
  23. raise Exception(f"Cannot retrieve layout in '{full_cmd}' result")
  24.  
  25. match_variant = re.search(r"variant:\s+(\S+)", output)
  26. if match_variant:
  27. variant = match_variant.group(1)
  28. return f'{layout} {variant}'
  29.  
  30. return layout
  31.  
  32. def input_layouts():
  33. """ Retrieve script input layouts."""
  34. if "LAYOUTS" in os.environ:
  35. as_string = os.environ["LAYOUTS"]
  36. else:
  37. raise Exception("LAYOUTS must be set. Try LAYOUTS=$'fr\\nfr bepo\\nus' {script}".format(script=sys.argv[0]))
  38.  
  39. return as_string.split('\n')
  40.  
  41. def swap_first_input_if_current(curr_layout, in_layouts):
  42. """ Swap first and second layouts if first layout is the current one."""
  43. layouts = list(in_layouts)
  44. if len(layouts) > 1:
  45. if layouts[0] == curr_layout:
  46. layouts[0], layouts[1] = layouts[1], layouts[0]
  47. return layouts
  48.  
  49. def dmenu_setxkbmap(force_space_keymap=True):
  50. """Script main function.
  51.  
  52. 1. Generate a dmenu input
  53. 2. Call dmenu to ask user's choice
  54. 3. Call setxkbmap on selected choice
  55. """
  56. # Generate the desired list of X11 layouts
  57. in_layouts = swap_first_input_if_current(current_layout(), input_layouts())
  58. layouts_as_str = '\n'.join(in_layouts).encode('utf-8')
  59.  
  60. # Call dmenu on our lists of layouts
  61. proc = subprocess.run([dmenu_command] + sys.argv[1:],
  62. input=layouts_as_str,
  63. stdout=subprocess.PIPE)
  64.  
  65. if proc.returncode == 0:
  66. choice = proc.stdout.decode('utf-8').strip()
  67.  
  68. # Call setxkbmap on selected choice
  69. returncode, _ = subprocess.getstatusoutput(f'{setxkbmap_command} {choice}')
  70. success = (returncode == 0)
  71.  
  72. if success and force_space_keymap:
  73. # Force keymap of the space keycode
  74. code, _ = subprocess.getstatusoutput('xmodmap -e "keycode 65 = space space space space underscore underscore space space"')
  75. success = (code == 0)
  76. else:
  77. success = False
  78.  
  79. return success
  80.  
  81. if __name__ == "__main__":
  82. if dmenu_setxkbmap():
  83. sys.exit(0)
  84. else:
  85. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement