#!/usr/bin/env bash # reset-xkb --- reset xkeyboard settings to sensible default # Author: Noah Friedman # Created: 2010-02-17 # Public domain # $Id: reset-xkb,v 1.14 2015/10/05 05:33:18 friedman Exp $ # Commentary: # Sometimes the keyboard gets fubared or wasn't set right in the first # place. This also lets me make modifications I might prefer, e.g. for # meta/alt keys, in a way that's more flexible than xmodmap. # Code: verbose=t initialize() { read_dmi # Always use screen 0 because the _XKB_RULES_NAMES property may not # be defined on the other root screens. DISPLAY=${DISPLAY%.[0-9]*}.0 xkb_rules=${XKB_RULES} xkb_model=${XKB_MODEL} xkb_layout=${XKB_LAYOUT-us} xkb_geometry=${XKB_GEOMETRY} xkb_keycodes=${XKB_KEYCODES} xkb_variant=${XKB_VARIANT} xkb_symbols=${XKB_SYMBOLS} xkb_device=${XKB_DEVICE} xkb_option=${XKB_OPTION-"ctrl:nocaps compose:lwin "} # Other xkb_options: # terminate:ctrl_alt_bksp - Allow Ctrl-Alt-BS to kill X server # srvrkeys:none - Disable Ctrl-Alt-Fn vt switching # See /usr/share/X11/xkb/rules/xorg.lst for others } read_dmi() { dmi=/sys/class/dmi/id for prop in $dmi/* ; do test -L $prop && continue test -f $prop || continue test -r $prop || continue read ${prop##*/} < $prop done } customize() { case $product_version in ThinkPad* ) xkb_model=${XKB_MODEL-thinkpad} case $product_version in ThinkPad*60* ) xkb_model=${XKB_MODEL-thinkpad60} ;; ThinkPad*701* ) xkb_model=${XKB_MODEL-thinkpad60} ;; *Tablet ) xkb_model=${XKB_MODEL-thinkpad60} ;; esac ;; *"Ideapad S10"* ) : ;; esac } xmodmap_hacks() { case $bios_version:$product_version in Google_*:* ) # chromebook # Search key registers as "Multi_Key" with default Crouton # "xorg" config. docmd xmodmap -e 'keycode 133 = Control_L' docmd xmodmap -e 'add control = Control_L' ;; *:ThinkPad*Tablet ) docmd xmodmap -e 'keycode 161 = XF86RotationPB' docmd xmodmap -e 'keycode 162 = XF86RotateWindows' docmd xset -r 161 -r 162 # or use xkbset, same syntax ;; esac } run_setxkbmap() { set fnord ${SETXKBMAP-setxkbmap} -I $HOME/lib/xkb # xkb_option handled as a special case later. xkb_vars='xkb_rules xkb_model xkb_layout xkb_variant xkb_geometry xkb_keycodes xkb_symbols xkb_device ' for var in $xkb_vars ; do opt=-${var#xkb_} eval val=\"\$$var\" case $val in '' ) : ;; * ) set "$@" $opt "$val" ;; esac done set "$@" -option # null -option field cancels out existing xkb options for opt in $xkb_option; do set "$@" -option $opt done shift # fnord docmd "$@" -print } load() { run_setxkbmap | docmd xkbcomp -w "${XKB_WARNLEVEL-0}" - "$DISPLAY" xmodmap_hacks } delq() { for x in "$@"; do case $x in $1 ) continue ;; esac echo "$x" done } docmd() { case $verbose in t ) echo + "$@" 1>&2 ;; esac "$@" } option_overrides() { progname=${0##*/} getopt=' { optarg= case $1 in --*=* ) optarg=`echo "$1" | sed -e "1s/^[^=]*=//"` ; shift ;; -* ) case ${2+set} in set ) optarg=$2 ; shift ; shift ;; "" ) option=$1 case $option in --*=* ) option=${option%%=*} ;; esac exec 1>&2 echo "$progname: option $bq$option$eq requires argument." echo "$progname: use $bq--help$eq to list option syntax." exit 1 ;; esac ;; esac }' while : ; do case $# in 0) break ;; esac case $1 in -r | --rules | --r* ) eval "$getopt"; xkb_rules=$optarg ;; -m | --model | --m* ) eval "$getopt"; xkb_model=$optarg ;; -l | --layout | --l* ) eval "$getopt"; xkb_layout=$optarg ;; -v | --variant | --v* ) eval "$getopt"; xkb_variant=$optarg ;; -g | --geometry | --g* ) eval "$getopt"; xkb_geometry=$optarg ;; -k | --keycodes | --k* ) eval "$getopt"; xkb_keycodes=$optarg ;; -s | --symbols | --s* ) eval "$getopt"; xkb_symbols=$optarg ;; -d | --device | --d* ) eval "$getopt"; xkb_device=$optarg ;; -o | --option | --o* ) eval "$getopt"; xkb_option="$xkb_option $optarg" ;; -q | --quiet | --q* ) verbose=f ; shift ;; -- ) shift; break ;; # Stop option processing -? | --* ) case $1 in --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;; * ) arg=$1 ;; esac exec 1>&2 echo "$progname: unknown or ambiguous option $bq$arg$eq" echo "$progname: Use $bq--help$eq for a list of options." exit 1 ;; -??* ) # Split grouped single options into separate args and try again optarg=$1 shift set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` "$@" shift ;; * ) break ;; esac done } main() { initialize customize option_overrides "$@" load docmd xset r rate 250 30 } main "$@" # eof