summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2014-09-21 18:27:23 +0200
committerBoris Faure <billiob@gmail.com>2014-09-21 23:46:49 +0200
commit671fe7967b613c51b9de670a5955dbc9fe3f3f6f (patch)
tree4fcd5f6abc3d10c8fe1aa2fdbff262bfd74de073 /tools
parenta61c34999597113929bd477e891b0042ed89d130 (diff)
generate tty_keys.h from xterm itself
Script gen_tty_keys.sh is heavily based on the script escapes.sh by Egmont Koblinger. Closes T1564
Diffstat (limited to 'tools')
-rwxr-xr-xtools/gen_tty_keys.sh277
1 files changed, 277 insertions, 0 deletions
diff --git a/tools/gen_tty_keys.sh b/tools/gen_tty_keys.sh
new file mode 100755
index 0000000..e12db8b
--- /dev/null
+++ b/tools/gen_tty_keys.sh
@@ -0,0 +1,277 @@
1#!/bin/bash
2
3if [ -z "$(type -p xdotool)" ]; then
4 echo 'Install xdotool!' >&2
5 exit 1
6fi
7
8if [ -z "$(type -p numlockx)" ]; then
9 echo 'Install numlockx!' >&2
10 exit 1
11fi
12
13if [ -t 1 ]; then
14 echo 'Redirect the output to a file!' >&2
15 exit 1
16fi
17
18function do_reset {
19 echo -ne '\e[?1050l' >&2
20 echo -ne '\e[?1051l' >&2
21 echo -ne '\e[?1052l' >&2
22 echo -ne '\e[?1053l' >&2
23 echo -ne '\e[?1060l' >&2
24 echo -ne '\e[?1061l' >&2
25 echo -ne '\e[?1l' >&2 # normal cursor mode
26 echo -ne '\e>' >&2 # normal keypad mode
27}
28
29do_key() {
30 local key="$1"
31 local shift="$2"
32 local ctrl="$3"
33 local alt="$4"
34 local kind="$5"
35
36 local mod=""
37 local numlock=0
38
39 if [ "$shift" = "1" ]; then
40 mod="${mod}Shift+"
41 fi
42 if [ "$ctrl" = "1" ]; then
43 mod="${mod}Ctrl+"
44 fi
45 if [ "$alt" = "1" ]; then
46 mod="${mod}Alt+"
47 fi
48 if [ "$numlock" = "1" ]; then
49 numlockx on
50 else
51 numlockx off
52 fi
53
54 skip=0
55 if [[ $key =~ F && $alt = 1 ]]; then
56 # Alt+Fx are usually window manager actions
57 skip=1
58 elif [[ $key =~ Insert && ( $shift = 1 || $ctrl = 1 ) ]]; then
59 # {Shift,Ctrl}+Insert is about mouse selection
60 skip=1
61 elif [[ ( $key =~ Home || $key =~ End || $key =~ Page ) && $shift = 1 ]]; then
62 # Shift+{PgUp,PgDn,Home,End} effect the scrollbar
63 skip=1
64 elif [[ ( $key =~ Up || $key =~ Down ) && $shift = 1 && $ctrl = 1 ]]; then
65 # Ctrl+Shift+{Up,Dn} also effect the scrollbar
66 skip=1
67 elif [[ ( $key =~ Add || $key =~ Sub ) && ( $shift = 1) ]]; then
68 # Shift+{+,-} changes window size in xterm
69 skip=1
70 fi
71
72 echo "$kind $mod$key" >&2
73 if [ $skip = 0 ]; then
74 xdotool key $mod$key Return
75 if [ $? -eq 0 ]; then
76 IFS='' read -r esc
77 esc="${esc/$'\033'/\\033}"
78 else
79 echo "SEGV: $mod$key" >&2
80 skip=1
81 fi
82 else
83 skip=1
84 fi
85 if [ $skip = 0 ]; then
86 echo " KH(\"$esc\"), // $kind $mod$key"
87 else
88 echo " {NULL, 0}, // $kind $mod$key"
89 fi
90
91 sleep 0.1
92 # a bit of healthy paranoia
93 xdotool key at Return
94 IFS='' read -r esc
95 if [ "$esc" != '@' ]; then
96 echo 'Sync error!' >&2
97 exit 1
98 fi
99}
100
101do_keys_mode() {
102
103
104 local keys='F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Left Right Up Down Home End Insert Delete Prior Next Tab minus space Menu Find Help Select '
105
106 echo "#define KH(in) { in, sizeof(in) - 1 }"
107
108 echo "static const Tty_Key tty_keys[] = {"
109 for k in $keys; do
110 echo "{"
111 echo " \"$k\","
112 echo " sizeof(\"$k\") - 1,"
113 #default
114 echo " {"
115 do_reset
116 do_key $k 0 0 0 "default"
117 do_key $k 0 0 1 "default"
118 do_key $k 0 1 0 "default"
119 do_key $k 0 1 1 "default"
120 do_key $k 1 0 0 "default"
121 do_key $k 1 0 1 "default"
122 do_key $k 1 1 0 "default"
123 do_key $k 1 1 1 "default"
124 echo " },"
125
126 #cursor
127 echo " {"
128 do_reset
129 echo -ne '\e[?1h' >&2 # application cursor mode
130 do_key $k 0 0 0 "cursor"
131 do_key $k 0 0 1 "cursor"
132 do_key $k 0 1 0 "cursor"
133 do_key $k 0 1 1 "cursor"
134 do_key $k 1 0 0 "cursor"
135 do_key $k 1 0 1 "cursor"
136 do_key $k 1 1 0 "cursor"
137 do_key $k 1 1 1 "cursor"
138 echo " },"
139 echo "},"
140 done
141 echo "};"
142
143
144 # Kp_*
145
146 keys='KP_Up KP_Down KP_Right KP_Left KP_Insert KP_Delete KP_Home KP_Prior KP_Next KP_Begin KP_End'
147
148 echo "static const Tty_Key tty_keys_kp_plain[] = {"
149 for k in $keys; do
150 echo "{"
151 echo " \"$k\","
152 echo " sizeof(\"$k\") - 1,"
153 #default
154 echo " {"
155 do_reset
156 do_key $k 0 0 0 "default"
157 do_key $k 0 0 1 "default"
158 do_key $k 0 1 0 "default"
159 do_key $k 0 1 1 "default"
160 do_key $k 1 0 0 "default"
161 do_key $k 1 0 1 "default"
162 do_key $k 1 1 0 "default"
163 do_key $k 1 1 1 "default"
164 echo " },"
165
166 #cursor
167 echo " {"
168 do_reset
169 echo -ne '\e[?1h' >&2 # application cursor mode
170 do_key $k 0 0 0 "cursor"
171 do_key $k 0 0 1 "cursor"
172 do_key $k 0 1 0 "cursor"
173 do_key $k 0 1 1 "cursor"
174 do_key $k 1 0 0 "cursor"
175 do_key $k 1 0 1 "cursor"
176 do_key $k 1 1 0 "cursor"
177 do_key $k 1 1 1 "cursor"
178 echo " },"
179 echo "},"
180 done
181 echo "};"
182 echo "static const Tty_Key tty_keys_kp_app[] = {"
183 for k in $keys; do
184 echo "{"
185 echo " \"$k\","
186 echo " sizeof(\"$k\") - 1,"
187 #default
188 echo " {"
189 do_reset
190 echo -ne '\e[?1l' >&2 # normal cursor mode
191 echo -ne '\e=' >&2 # application keypad
192 do_key $k 0 0 0 "default"
193 do_key $k 0 0 1 "default"
194 do_key $k 0 1 0 "default"
195 do_key $k 0 1 1 "default"
196 do_key $k 1 0 0 "default"
197 do_key $k 1 0 1 "default"
198 do_key $k 1 1 0 "default"
199 do_key $k 1 1 1 "default"
200 echo " },"
201
202 #cursor
203 echo " {"
204 do_reset
205 echo -ne '\e[?1h' >&2 # application cursor mode
206 echo -ne '\e=' >&2 # application keypad
207 do_key $k 0 0 0 "cursor"
208 do_key $k 0 0 1 "cursor"
209 do_key $k 0 1 0 "cursor"
210 do_key $k 0 1 1 "cursor"
211 do_key $k 1 0 0 "cursor"
212 do_key $k 1 0 1 "cursor"
213 do_key $k 1 1 0 "cursor"
214 do_key $k 1 1 1 "cursor"
215 echo " },"
216 echo "},"
217 done
218 echo "};"
219 #
220 #echo ' normal'
221 #echo '### normal' >&2
222 #do_one_cursor_and_keypad_mode
223
224 #echo -ne '\e[?1h' >&2 # application cursor mode
225 #echo ' cursor'
226 #echo '### cursor' >&2
227 #do_one_cursor_and_keypad_mode
228
229 #echo -ne '\e[?1l' >&2 # normal cursor mode
230 #echo -ne '\e=' >&2 # application keypad
231 #echo ' keypad'
232 #echo '### keypad' >&2
233 #do_one_cursor_and_keypad_mode
234
235 #echo -ne '\e[?1h' >&2 # application cursor mode
236 #echo ' cursor+keypad'
237 #echo '### cursor+keypad' >&2
238 #do_one_cursor_and_keypad_mode
239
240 echo "#undef KH"
241}
242
243cat <<END >&2
244
245Don't do anything!
246Don't touch your keyboard! Not even modifiers or numlock!
247Don't change window focus!
248Just leave it running!
249
250Make sure to have English keyboard layout.
251
252Press Ctrl+C if you really need to abort.
253
254END
255
256# Sometimes the first run of xdotool produces some garbage, no clue why.
257xdotool key T e s t space 1 Return
258read x
259xdotool key T e s t space 2 Return
260read x
261if [ "$x" != 'Test 2' ]; then
262 echo "Oops, xdotool doesn't seem to work!" >&2
263 exit 1
264fi
265
266# Let's go!
267
268trap 'do_reset; echo >&2; echo >&2; echo "If your keyboard is stuck, press and release the modifiers one by one" >&2' EXIT
269
270do_keys_mode DEFAULT 0
271
272do_reset
273
274echo >&2
275echo "Ready." >&2
276
277trap EXIT