Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # ----------------------------------------------------------------------
  4. # reference
  5. # ----------------------------------------------------------------------
  6.  
  7. # compose key in this setup is shift+ralt, release, then hit dead key and live key
  8. # e.g. shift+ralt, ', e = é
  9.  
  10. # both shifts simultaneously switches between first and second group (en and gr)
  11.  
  12. # /etc/udev/rules.d/00-keyboard.rules
  13. # ACTION=="add", SUBSYSTEM=="input", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/ethan/.Xauthority", RUN+="/home/ethan/bin/wm/init-keyboard"
  14.  
  15. # $HOME/.config/systemd/user/xcape.service
  16. # [Unit]
  17. # Description=Xcape Daemon
  18. # After=graphical.target
  19. #
  20. # [Service]
  21. # Type=forking
  22. # Environment=DISPLAY=:0
  23. # ExecStart=/usr/bin/xcape -e "Hyper_L=Tab;Hyper_R=backslash"
  24. # Restart=always
  25. # RestartSec=1
  26. #
  27. # [Install]
  28. # WantedBy=default.target
  29.  
  30. # ----------------------------------------------------------------------
  31. # Standard Boilerplate
  32. # ----------------------------------------------------------------------
  33. # I use this standard boilerplate in multiple scripts where I might
  34. # need to re-run as the X user. Some items are not necessary in all
  35. # scripts but I'd rather keep it consistent. In general, the *ctl
  36. # scripts (audioctl, displayctl) require this as they may be called
  37. # by udev or acpi and thus by root.
  38.  
  39. # script values and config file if we need it
  40. SCRIPTNAME=$(basename $0)
  41. SCRIPTPATH=$(readlink -f $0)
  42. SCRIPTDIR=$(dirname $SCRIPTPATH)
  43. SCRIPTOWNER=$(stat -c '%U' $SCRIPTPATH)
  44.  
  45. # rerun as X user if running as root
  46. # ------------------------------------------------------------------------
  47. # attempt to grab xuser assuming it has been run "normally" via a
  48. # display manager or startx
  49. XUSER=$(ps -C Xorg -C X -ouser=)
  50. # if we are running via xlogin@ systemd service, it will be running
  51. # as root, so attempt to match based on the script owner
  52. #[ "$XUSER" = root ] && systemctl is-active xlogin@$SCRIPTOWNER && XUSER=$SCRIPTOWNER
  53. [ "$XUSER" = root ] && XUSER=$SCRIPTOWNER
  54. # if at this point our XUSER is not root, restart as the XUSER
  55. [ "$(id -un)" = root ] && [ "$XUSER" != root ] && exec su -c "$0 $*" "$XUSER"
  56.  
  57. # grab X user id if necessary
  58. XUSERID=$(id -u $XUSER)
  59.  
  60. # key environment variables
  61. export DISPLAY=":0"
  62. export XAUTHORITY=$HOME/.Xauthority
  63. export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$XUSERID/bus"
  64. export PULSE_RUNTIME_PATH="/run/user/${XUSERID}/pulse/"
  65.  
  66. CONFIG=/home/$XUSER/.config/$SCRIPTNAME
  67.  
  68. # ----------------------------------------------------------------------
  69. # Lock
  70. # ----------------------------------------------------------------------
  71.  
  72. # locking not working properly... udev triggers this but locking is
  73. # present it fails... possibly it it running before udev has populated
  74. # all attached usb devices?
  75.  
  76. # going to try adding a sleep here to give it time to get its act together
  77.  
  78. #LOCKFILE=/tmp/${SCRIPTNAME}.lock
  79. #exec 200>$LOCKFILE
  80. #flock -n 200 || exit 0
  81. #sleep 1 # give udev time to finish
  82.  
  83. # ----------------------------------------------------------------------
  84. # dump config to file
  85. # ----------------------------------------------------------------------
  86.  
  87. # rather than dump these to a file, I could keep them in static files
  88. # in, say, .config/xkb or .xkb, but I prefer this method as it centralizes
  89. # the configuration data to just this file
  90.  
  91. XKBDIR=/tmp/xkb
  92. [ -d ${XKBDIR}/symbols ] || mkdir -p ${XKBDIR}/{keymap,symbols}
  93.  
  94. # the following is generated first from a setxkbmap command similar to:
  95. # setxkbmap -layout "us,gr(polytonic)" -option "ctrl:nocaps,grp:shifts_toggle,misc:typo,shift:break_caps,lv3:alt_switch"
  96. # and then:
  97. # setxkbmap -print
  98. # the final tweak being the addition of the "+custom(hypers)" to use my local customizations
  99.  
  100. # lv3:ralt_switch_multikey
  101. # level3(alt_switch)
  102. # level3(ralt_switch_multikey)
  103.  
  104. cat > $XKBDIR/keymap/custom.xkb << EOF
  105. xkb_keymap {
  106. xkb_keycodes { include "evdev+aliases(qwerty)" };
  107. xkb_types { include "complete" };
  108. xkb_compat { include "complete" };
  109. xkb_symbols { include "pc+us+gr(polytonic):2+inet(evdev)+group(shifts_toggle)+level3(ralt_switch_multikey)+ctrl(nocaps)+typo(base):1+typo(base):2+custom(hypers)" };
  110. xkb_geometry { include "pc(pc104)" };
  111. };
  112. EOF
  113.  
  114. cat > $XKBDIR/symbols/custom << EOF
  115. default partial
  116. xkb_symbols "hypers" {
  117. key <TAB> { [ Hyper_L, Hyper_L ] };
  118. key <BKSL> { [ Hyper_R, Hyper_R ] };
  119. key <I252> { [ Tab, ISO_Left_Tab ] };
  120. key <I253> { [ backslash, bar ] };
  121. modifier_map Mod4 { Super_L, Super_R, Hyper_L, Hyper_R };
  122. };
  123. EOF
  124.  
  125. # ----------------------------------------------------------------------
  126. # reinitialize keyboard
  127. # ----------------------------------------------------------------------
  128.  
  129. export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$XUSERID/bus"
  130. notify-send -u low "Keyboard initialized"
  131. xkbcomp -synch -w3 -I$XKBDIR $XKBDIR/keymap/custom.xkb $DISPLAY &>/dev/null
  132. #(exec killall -q xcape) & # gets restarted by the xcape systemd user service
  133. # if no service running, just start xcape here
  134. # if more than one xcape, just kill them all and let service respawn
  135. if (( $(pgrep -c xcape) > 0 )) && systemctl --user status xcape &>/dev/null
  136. then
  137. for PID in $(pgrep xcape)
  138. do
  139. kill -9 $PID
  140. done
  141. elif systemctl --user status xcape &>/dev/null
  142. then
  143. # try starting service or just run xcape if that fails
  144. systemctl --user start xcape || /usr/bin/xcape -e "Hyper_L=Tab;Hyper_R=backslash"
  145. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement