summaryrefslogtreecommitdiff
path: root/tools/gen_tty_keys.sh
blob: e12db8b33cc13bf63fab8b757677de2fdd56c5f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash

if [ -z "$(type -p xdotool)" ]; then
	echo 'Install xdotool!' >&2
	exit 1
fi

if [ -z "$(type -p numlockx)" ]; then
	echo 'Install numlockx!' >&2
	exit 1
fi

if [ -t 1 ]; then
	echo 'Redirect the output to a file!' >&2
	exit 1
fi

function do_reset {
	echo -ne '\e[?1050l' >&2
	echo -ne '\e[?1051l' >&2
	echo -ne '\e[?1052l' >&2
	echo -ne '\e[?1053l' >&2
	echo -ne '\e[?1060l' >&2
	echo -ne '\e[?1061l' >&2
	echo -ne '\e[?1l' >&2  # normal cursor mode
	echo -ne '\e>' >&2     # normal keypad mode
}

do_key() {
   local key="$1"
   local shift="$2"
   local ctrl="$3"
   local alt="$4"
   local kind="$5"

   local mod=""
   local numlock=0

   if [ "$shift" = "1" ]; then
      mod="${mod}Shift+"
   fi
   if [ "$ctrl" = "1" ]; then
      mod="${mod}Ctrl+"
   fi
   if [ "$alt" = "1" ]; then
      mod="${mod}Alt+"
   fi
   if [ "$numlock" = "1" ]; then
      numlockx on
   else
      numlockx off
   fi

   skip=0
   if [[ $key =~ F && $alt = 1 ]]; then
      # Alt+Fx are usually window manager actions
      skip=1
   elif [[ $key =~ Insert && ( $shift = 1 || $ctrl = 1 ) ]]; then
      # {Shift,Ctrl}+Insert is about mouse selection
      skip=1
   elif [[ ( $key =~ Home || $key =~ End || $key =~ Page ) && $shift = 1 ]]; then
      # Shift+{PgUp,PgDn,Home,End} effect the scrollbar
      skip=1
   elif [[ ( $key =~ Up || $key =~ Down ) && $shift = 1 && $ctrl = 1 ]]; then
      # Ctrl+Shift+{Up,Dn} also effect the scrollbar
      skip=1
   elif [[ ( $key =~ Add || $key =~ Sub ) && ( $shift = 1) ]]; then
      # Shift+{+,-} changes window size in xterm
      skip=1
   fi

   echo "$kind $mod$key" >&2
   if [ $skip = 0 ]; then
      xdotool key $mod$key Return
      if [ $? -eq 0 ]; then
         IFS='' read -r esc
         esc="${esc/$'\033'/\\033}"
      else
         echo "SEGV: $mod$key" >&2
         skip=1
      fi
   else
      skip=1
   fi
   if [ $skip = 0 ]; then
      echo "    KH(\"$esc\"), // $kind $mod$key"
   else
      echo "    {NULL, 0}, // $kind $mod$key"
   fi

   sleep 0.1
   # a bit of healthy paranoia
   xdotool key at Return
   IFS='' read -r esc
   if [ "$esc" != '@' ]; then
      echo 'Sync error!' >&2
      exit 1
   fi
}

do_keys_mode() {


   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 '

   echo "#define KH(in) { in, sizeof(in) - 1 }"

   echo "static const Tty_Key tty_keys[] = {"
   for k in $keys; do
      echo "{"
      echo "  \"$k\","
      echo "  sizeof(\"$k\") - 1,"
      #default
      echo "  {"
      do_reset
      do_key $k 0 0 0 "default"
      do_key $k 0 0 1 "default"
      do_key $k 0 1 0 "default"
      do_key $k 0 1 1 "default"
      do_key $k 1 0 0 "default"
      do_key $k 1 0 1 "default"
      do_key $k 1 1 0 "default"
      do_key $k 1 1 1 "default"
      echo "  },"

      #cursor
      echo "  {"
      do_reset
      echo -ne '\e[?1h' >&2  # application cursor mode
      do_key $k 0 0 0 "cursor"
      do_key $k 0 0 1 "cursor"
      do_key $k 0 1 0 "cursor"
      do_key $k 0 1 1 "cursor"
      do_key $k 1 0 0 "cursor"
      do_key $k 1 0 1 "cursor"
      do_key $k 1 1 0 "cursor"
      do_key $k 1 1 1 "cursor"
      echo "  },"
      echo "},"
   done
   echo "};"


   # Kp_*

   keys='KP_Up KP_Down KP_Right KP_Left KP_Insert KP_Delete KP_Home KP_Prior KP_Next KP_Begin KP_End'

   echo "static const Tty_Key tty_keys_kp_plain[] = {"
   for k in $keys; do
      echo "{"
      echo "  \"$k\","
      echo "  sizeof(\"$k\") - 1,"
      #default
      echo "  {"
      do_reset
      do_key $k 0 0 0 "default"
      do_key $k 0 0 1 "default"
      do_key $k 0 1 0 "default"
      do_key $k 0 1 1 "default"
      do_key $k 1 0 0 "default"
      do_key $k 1 0 1 "default"
      do_key $k 1 1 0 "default"
      do_key $k 1 1 1 "default"
      echo "  },"

      #cursor
      echo "  {"
      do_reset
      echo -ne '\e[?1h' >&2  # application cursor mode
      do_key $k 0 0 0 "cursor"
      do_key $k 0 0 1 "cursor"
      do_key $k 0 1 0 "cursor"
      do_key $k 0 1 1 "cursor"
      do_key $k 1 0 0 "cursor"
      do_key $k 1 0 1 "cursor"
      do_key $k 1 1 0 "cursor"
      do_key $k 1 1 1 "cursor"
      echo "  },"
      echo "},"
   done
   echo "};"
   echo "static const Tty_Key tty_keys_kp_app[] = {"
   for k in $keys; do
      echo "{"
      echo "  \"$k\","
      echo "  sizeof(\"$k\") - 1,"
      #default
      echo "  {"
      do_reset
      echo -ne '\e[?1l' >&2  # normal cursor mode
      echo -ne '\e=' >&2     # application keypad
      do_key $k 0 0 0 "default"
      do_key $k 0 0 1 "default"
      do_key $k 0 1 0 "default"
      do_key $k 0 1 1 "default"
      do_key $k 1 0 0 "default"
      do_key $k 1 0 1 "default"
      do_key $k 1 1 0 "default"
      do_key $k 1 1 1 "default"
      echo "  },"

      #cursor
      echo "  {"
      do_reset
      echo -ne '\e[?1h' >&2  # application cursor mode
      echo -ne '\e=' >&2     # application keypad
      do_key $k 0 0 0 "cursor"
      do_key $k 0 0 1 "cursor"
      do_key $k 0 1 0 "cursor"
      do_key $k 0 1 1 "cursor"
      do_key $k 1 0 0 "cursor"
      do_key $k 1 0 1 "cursor"
      do_key $k 1 1 0 "cursor"
      do_key $k 1 1 1 "cursor"
      echo "  },"
      echo "},"
   done
   echo "};"
   #
   #echo '  normal'
   #echo '###   normal' >&2
   #do_one_cursor_and_keypad_mode

   #echo -ne '\e[?1h' >&2  # application cursor mode
   #echo '  cursor'
   #echo '###   cursor' >&2
   #do_one_cursor_and_keypad_mode

   #echo -ne '\e[?1l' >&2  # normal cursor mode
   #echo -ne '\e=' >&2     # application keypad
   #echo '  keypad'
   #echo '###   keypad' >&2
   #do_one_cursor_and_keypad_mode

   #echo -ne '\e[?1h' >&2  # application cursor mode
   #echo '  cursor+keypad'
   #echo '###   cursor+keypad' >&2
   #do_one_cursor_and_keypad_mode

   echo "#undef KH"
}

cat <<END >&2

Don't do anything!
Don't touch your keyboard!  Not even modifiers or numlock!
Don't change window focus!
Just leave it running!

Make sure to have English keyboard layout.

Press Ctrl+C if you really need to abort.

END

# Sometimes the first run of xdotool produces some garbage, no clue why.
xdotool key T e s t space 1 Return
read x
xdotool key T e s t space 2 Return
read x
if [ "$x" != 'Test 2' ]; then
	echo "Oops, xdotool doesn't seem to work!" >&2
	exit 1
fi

# Let's go!

trap 'do_reset; echo >&2; echo >&2; echo "If your keyboard is stuck, press and release the modifiers one by one" >&2' EXIT

do_keys_mode DEFAULT 0

do_reset

echo >&2
echo "Ready." >&2

trap EXIT