Release new version 2.13.0
[grml-scripts.git] / usr_bin / caps-ctrl
1 #!/bin/zsh
2 # Filename:      caps-ctrl
3 # Purpose:       switch caps to control key and vice versa for linux console and X
4 # Authors:       grml-team (grml.org),  (c) Matthias Kopfermann <maddi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 if [[ -f /etc/grml/script-functions ]] ; then
10         source /etc/grml/script-functions && \
11         check4progs xmodmap loadkeys dumpkeys || exit 1
12 fi
13 if [[ -f /etc/grml/lsb-functions ]] ; then
14         source /etc/grml/lsb-functions
15 else
16         einfo() { echo  "$*" ; }
17         eerror() { echo "$*" ; }
18         eend() { echo "$*" ; }
19 fi
20
21 emulate zsh
22
23 if [[ -z $DISPLAY  ]] ; then # test if X is not running when calling us
24          if [ $(id -u) != 0 ] ; then # test if user root did invoke this command
25             eerror "As of Linux 2.6.15 you need root permissions for changing"
26             eerror "the keyboard on console using loadkeys for security reasons."
27             eerror "Run this program with root permissions. Exiting." ; eend 1
28             exit 1
29          fi
30          dumpkeys | grep -q '^keycode  58 = Caps_Lock' && \
31          ( einfo "caps-ctrl - switching caps lock and control key."
32
33            loadkeys <<- EOT
34            keycode 58 = $(repeat 15 echo -n 'Control ')
35            keycode 29 = $(repeat 7 echo -n 'Caps_Lock ')
36                 EOT
37            eend $?
38
39         ) || (
40           einfo "caps-ctrl - switching caps lock and control key."
41
42            loadkeys <<- EOT
43            keycode 58 = $(repeat 15 echo -n 'Caps_Lock ')
44            keycode 29 = $(repeat 7 echo -n 'Control ')
45                 EOT
46            eend $?
47          )
48
49 else     # running under X
50         (
51         einfo "caps-ctrl - switching caps lock and control key."
52         einfo "If you notice errors, please make sure the xmodmap you have is right"
53         einfo "or use e.g. \"setxkbmap us\" beforehand."
54         xmodmap -pke | grep 'Caps_Lock' > /dev/null || (
55         xmodmap - <<- EOT
56         keycode 66 = Caps_Lock
57                 EOT
58         )
59
60         xmodmap - <<- EOT
61         remove Lock = Caps_Lock
62         remove Control = Control_L
63         !remove Control = Control_R
64
65         keysym Control_L = Caps_Lock
66         keysym Caps_Lock = Control_L
67         !keysym Control_R = Caps_Lock
68         !keysym Caps_Lock = Control_R
69
70         add lock = Caps_Lock
71         add Control = Control_L
72         !add Control = Control_R
73                 EOT
74         eend $?
75         )
76 fi # end of test if X or console is used
77
78 ## END OF FILE #################################################################