comparison srogue/mdport.c @ 86:8757a0593e6e

srogue: add arrow-key support. This is a first attempt which may not be completely portable.
author John "Elwin" Edwards
date Sat, 10 Aug 2013 17:43:58 -0700
parents
children ea71ef31d9be
comparison
equal deleted inserted replaced
85:d852b8f088c5 86:8757a0593e6e
1 /*
2 mdport.c - Machine Dependent Code
3
4 Copyright (C) 2005-2008 Nicholas J. Kisseberth
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name(s) of the author(s) nor the names of other contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 SUCH DAMAGE.
30 */
31
32 /* This is a temporary stub version of rogue5's mdport.c. It is only to make
33 * md_readchar() available until srogue is ported to autoconf. Then the
34 * whole file should work.
35 */
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #if defined(_WIN32)
41 #include <Windows.h>
42 #include <Lmcons.h>
43 #include <io.h>
44 #include <conio.h>
45 #pragma warning( disable: 4201 )
46 #include <shlobj.h>
47 #pragma warning( default: 4201 )
48 #include <Shlwapi.h>
49 #undef MOUSE_MOVED
50 #endif
51
52 #include <sys/types.h>
53
54 #include <curses.h> /* AIX requires curses.h be included before term.h */
55
56 #include <ctype.h>
57 #include "rogue.h"
58
59 /*
60 Cursor/Keypad Support
61
62 Sadly Cursor/Keypad support is less straightforward than it should be.
63
64 The various terminal emulators/consoles choose to differentiate the
65 cursor and keypad keys (with modifiers) in different ways (if at all!).
66 Furthermore they use different code set sequences for each key only
67 a subset of which the various curses libraries recognize. Partly due
68 to incomplete termcap/terminfo entries and partly due to inherent
69 limitations of those terminal capability databases.
70
71 I give curses first crack at decoding the sequences. If it fails to decode
72 it we check for common ESC-prefixed sequences.
73
74 All cursor/keypad results are translated into standard rogue movement
75 commands.
76
77 Unmodified keys are translated to walk commands: hjklyubn
78 Modified (shift,control,alt) are translated to run commands: HJKLYUBN
79
80 Console and supported (differentiated) keys
81 Interix: Cursor Keys, Keypad, Ctl-Keypad
82 Cygwin: Cursor Keys, Keypad, Alt-Cursor Keys
83 MSYS: Cursor Keys, Keypad, Ctl-Cursor Keys, Ctl-Keypad
84 Win32: Cursor Keys, Keypad, Ctl/Shift/Alt-Cursor Keys, Ctl/Alt-Keypad
85 DJGPP: Cursor Keys, Keypad, Ctl/Shift/Alt-Cursor Keys, Ctl/Alt-Keypad
86
87 Interix Console (raw, ncurses)
88 ==============================
89 normal shift ctrl alt
90 ESC [D, ESC F^, ESC [D, ESC [D /# Left #/
91 ESC [C, ESC F$, ESC [C, ESC [C /# Right #/
92 ESC [A, ESC F-, local win, ESC [A /# Up #/
93 ESC [B, ESC F+, local win, ESC [B /# Down #/
94 ESC [H, ESC [H, ESC [H, ESC [H /# Home #/
95 ESC [S, local win, ESC [S, ESC [S /# Page Up #/
96 ESC [T, local win, ESC [T, ESC [T /# Page Down #/
97 ESC [U, ESC [U, ESC [U, ESC [U /# End #/
98 ESC [D, ESC F^, ESC [D, O /# Keypad Left #/
99 ESC [C, ESC F$, ESC [C, O /# Keypad Right #/
100 ESC [A, ESC [A, ESC [-1, O /# Keypad Up #/
101 ESC [B, ESC [B, ESC [-2, O /# Keypad Down #/
102 ESC [H, ESC [H, ESC [-263, O /# Keypad Home #/
103 ESC [S, ESC [S, ESC [-19, O /# Keypad PgUp #/
104 ESC [T, ESC [T, ESC [-20, O /# Keypad PgDn #/
105 ESC [U, ESC [U, ESC [-21, O /# Keypad End #/
106 nothing, nothing, nothing, O /# Kaypad 5 #/
107
108 Interix Console (term=interix, ncurses)
109 ==============================
110 KEY_LEFT, ESC F^, KEY_LEFT, KEY_LEFT /# Left #/
111 KEY_RIGHT, ESC F$, KEY_RIGHT, KEY_RIGHT /# Right #/
112 KEY_UP, 0x146, local win, KEY_UP /# Up #/
113 KEY_DOWN, 0x145, local win, KEY_DOWN /# Down #/
114 ESC [H, ESC [H, ESC [H, ESC [H /# Home #/
115 KEY_PPAGE, local win, KEY_PPAGE, KEY_PPAGE /# Page Up #/
116 KEY_NPAGE, local win, KEY_NPAGE, KEY_NPAGE /# Page Down #/
117 KEY_LL, KEY_LL, KEY_LL, KEY_LL /# End #/
118 KEY_LEFT, ESC F^, ESC [-4, O /# Keypad Left #/
119 KEY_RIGHT, ESC F$, ESC [-3, O /# Keypad Right #/
120 KEY_UP, KEY_UP, ESC [-1, O /# Keypad Up #/
121 KEY_DOWN, KEY_DOWN, ESC [-2, O /# Keypad Down #/
122 ESC [H, ESC [H, ESC [-263, O /# Keypad Home #/
123 KEY_PPAGE, KEY_PPAGE, ESC [-19, O /# Keypad PgUp #/
124 KEY_NPAGE, KEY_NPAGE, ESC [-20, O /# Keypad PgDn #/
125 KEY_LL, KEY_LL, ESC [-21, O /# Keypad End #/
126 nothing, nothing, nothing, O /# Keypad 5 #/
127
128 Cygwin Console (raw, ncurses)
129 ==============================
130 normal shift ctrl alt
131 ESC [D, ESC [D, ESC [D, ESC ESC [D /# Left #/
132 ESC [C, ESC [C, ESC [C, ESC ESC [C /# Rght #/
133 ESC [A, ESC [A, ESC [A, ESC ESC [A /# Up #/
134 ESC [B, ESC [B, ESC [B, ESC ESC [B /# Down #/
135 ESC [1~, ESC [1~, ESC [1~, ESC ESC [1~ /# Home #/
136 ESC [5~, ESC [5~, ESC [5~, ESC ESC [5~ /# Page Up #/
137 ESC [6~, ESC [6~, ESC [6~, ESC ESC [6~ /# Page Down #/
138 ESC [4~, ESC [4~, ESC [4~, ESC ESC [4~ /# End #/
139 ESC [D, ESC [D, ESC [D, ESC ESC [D,O /# Keypad Left #/
140 ESC [C, ESC [C, ESC [C, ESC ESC [C,O /# Keypad Right #/
141 ESC [A, ESC [A, ESC [A, ESC ESC [A,O /# Keypad Up #/
142 ESC [B, ESC [B, ESC [B, ESC ESC [B,O /# Keypad Down #/
143 ESC [1~, ESC [1~, ESC [1~, ESC ESC [1~,O /# Keypad Home #/
144 ESC [5~, ESC [5~, ESC [5~, ESC ESC [5~,O /# Keypad PgUp #/
145 ESC [6~, ESC [6~, ESC [6~, ESC ESC [6~,O /# Keypad PgDn #/
146 ESC [4~, ESC [4~, ESC [4~, ESC ESC [4~,O /# Keypad End #/
147 ESC [-71, nothing, nothing, O /# Keypad 5 #/
148
149 Cygwin Console (term=cygwin, ncurses)
150 ==============================
151 KEY_LEFT, KEY_LEFT, KEY_LEFT, ESC-260 /# Left #/
152 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, ESC-261 /# Rght #/
153 KEY_UP, KEY_UP, KEY_UP, ESC-259 /# Up #/
154 KEY_DOWN, KEY_DOWN, KEY_DOWN, ESC-258 /# Down #/
155 KEY_HOME, KEY_HOME, KEY_HOME, ESC-262 /# Home #/
156 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, ESC-339 /# Page Up #/
157 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, ESC-338 /# Page Down #/
158 KEY_END, KEY_END, KEY_END, ESC-360 /# End #/
159 KEY_LEFT, KEY_LEFT, KEY_LEFT, ESC-260,O /# Keypad Left #/
160 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, ESC-261,O /# Keypad Right #/
161 KEY_UP, KEY_UP, KEY_UP, ESC-259,O /# Keypad Up #/
162 KEY_DOWN, KEY_DOWN, KEY_DOWN, ESC-258,O /# Keypad Down #/
163 KEY_HOME, KEY_HOME, KEY_HOME, ESC-262,O /# Keypad Home #/
164 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, ESC-339,O /# Keypad PgUp #/
165 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, ESC-338,O /# Keypad PgDn #/
166 KEY_END, KEY_END, KEY_END, ESC-360,O /# Keypad End #/
167 ESC [G, nothing, nothing, O /# Keypad 5 #/
168
169 MSYS Console (raw, ncurses)
170 ==============================
171 normal shift ctrl alt
172 ESC OD, ESC [d, ESC Od nothing /# Left #/
173 ESC OE, ESC [e, ESC Oe, nothing /# Right #/
174 ESC OA, ESC [a, ESC Oa, nothing /# Up #/
175 ESC OB, ESC [b, ESC Ob, nothing /# Down #/
176 ESC [7~, ESC [7$, ESC [7^, nothing /# Home #/
177 ESC [5~, local window, ESC [5^, nothing /# Page Up #/
178 ESC [6~, local window, ESC [6^, nothing /# Page Down #/
179 ESC [8~, ESC [8$, ESC [8^, nothing /# End #/
180 ESC OD, ESC [d, ESC Od O /# Keypad Left #/
181 ESC OE, ESC [c, ESC Oc, O /# Keypad Right #/
182 ESC OA, ESC [a, ESC Oa, O /# Keypad Up #/
183 ESC OB, ESC [b, ESC Ob, O /# Keypad Down #/
184 ESC [7~, ESC [7$, ESC [7^, O /# Keypad Home #/
185 ESC [5~, local window, ESC [5^, O /# Keypad PgUp #/
186 ESC [6~, local window, ESC [6^, O /# Keypad PgDn #/
187 ESC [8~, ESC [8$, ESC [8^, O /# Keypad End #/
188 11, 11, 11, O /# Keypad 5 #/
189
190 MSYS Console (term=rxvt, ncurses)
191 ==============================
192 normal shift ctrl alt
193 KEY_LEFT, KEY_SLEFT, 514 nothing /# Left #/
194 KEY_RIGHT, KEY_SRIGHT, 516, nothing /# Right #/
195 KEY_UP, 518, 519, nothing /# Up #/
196 KEY_DOWN, 511, 512, nothing /# Down #/
197 KEY_HOME, KEY_SHOME, ESC [7^, nothing /# Home #/
198 KEY_PPAGE, local window, ESC [5^, nothing /# Page Up #/
199 KEY_NPAGE, local window, ESC [6^, nothing /# Page Down #/
200 KEY_END, KEY_SEND, KEY_EOL, nothing /# End #/
201 KEY_LEFT, KEY_SLEFT, 514 O /# Keypad Left #/
202 KEY_RIGHT, KEY_SRIGHT, 516, O /# Keypad Right #/
203 KEY_UP, 518, 519, O /# Keypad Up #/
204 KEY_DOWN, 511, 512, O /# Keypad Down #/
205 KEY_HOME, KEY_SHOME, ESC [7^, O /# Keypad Home #/
206 KEY_PPAGE, local window, ESC [5^, O /# Keypad PgUp #/
207 KEY_NPAGE, local window, ESC [6^, O /# Keypad PgDn #/
208 KEY_END, KEY_SEND, KEY_EOL, O /# Keypad End #/
209 11, 11, 11, O /# Keypad 5 #/
210
211 Win32 Console (raw, pdcurses)
212 DJGPP Console (raw, pdcurses)
213 ==============================
214 normal shift ctrl alt
215 260, 391, 443, 493 /# Left #/
216 261, 400, 444, 492 /# Right #/
217 259, 547, 480, 490 /# Up #/
218 258, 548, 481, 491 /# Down #/
219 262, 388, 447, 524 /# Home #/
220 339, 396, 445, 526 /# Page Up #/
221 338, 394, 446, 520 /# Page Down #/
222 358, 384, 448, 518 /# End #/
223 452, 52('4'), 511, 521 /# Keypad Left #/
224 454, 54('6'), 513, 523 /# Keypad Right #/
225 450, 56('8'), 515, 525 /# Keypad Up #/
226 456, 50('2'), 509, 519 /# Keypad Down #/
227 449, 55('7'), 514, 524 /# Keypad Home #/
228 451, 57('9'), 516, 526 /# Keypad PgUp #/
229 457, 51('3'), 510, 520 /# Keypad PgDn #/
230 455, 49('1'), 508, 518 /# Keypad End #/
231 453, 53('5'), 512, 522 /# Keypad 5 #/
232
233 Win32 Console (pdcurses, MSVC/MingW32)
234 DJGPP Console (pdcurses)
235 ==============================
236 normal shift ctrl alt
237 KEY_LEFT, KEY_SLEFT, CTL_LEFT, ALT_LEFT /# Left #/
238 KEY_RIGHT, KEY_SRIGHT, CTL_RIGHT, ALT_RIGHT /# Right #/
239 KEY_UP, KEY_SUP, CTL_UP, ALT_UP /# Up #/
240 KEY_DOWN, KEY_SDOWN, CTL_DOWN, ALT_DOWN /# Down #/
241 KEY_HOME, KEY_SHOME, CTL_HOME, ALT_HOME /# Home #/
242 KEY_PPAGE, KEY_SPREVIOUS, CTL_PGUP, ALT_PGUP /# Page Up #/
243 KEY_NPAGE, KEY_SNEXTE, CTL_PGDN, ALT_PGDN /# Page Down #/
244 KEY_END, KEY_SEND, CTL_END, ALT_END /# End #/
245 KEY_B1, 52('4'), CTL_PAD4, ALT_PAD4 /# Keypad Left #/
246 KEY_B3, 54('6'), CTL_PAD6, ALT_PAD6 /# Keypad Right #/
247 KEY_A2, 56('8'), CTL_PAD8, ALT_PAD8 /# Keypad Up #/
248 KEY_C2, 50('2'), CTL_PAD2, ALT_PAD2 /# Keypad Down #/
249 KEY_A1, 55('7'), CTL_PAD7, ALT_PAD7 /# Keypad Home #/
250 KEY_A3, 57('9'), CTL_PAD9, ALT_PAD9 /# Keypad PgUp #/
251 KEY_C3, 51('3'), CTL_PAD3, ALT_PAD3 /# Keypad PgDn #/
252 KEY_C1, 49('1'), CTL_PAD1, ALT_PAD1 /# Keypad End #/
253 KEY_B2, 53('5'), CTL_PAD5, ALT_PAD5 /# Keypad 5 #/
254
255 Windows Telnet (raw)
256 ==============================
257 normal shift ctrl alt
258 ESC [D, ESC [D, ESC [D, ESC [D /# Left #/
259 ESC [C, ESC [C, ESC [C, ESC [C /# Right #/
260 ESC [A, ESC [A, ESC [A, ESC [A /# Up #/
261 ESC [B, ESC [B, ESC [B, ESC [B /# Down #/
262 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Home #/
263 ESC [5~, ESC [5~, ESC [5~, ESC [5~ /# Page Up #/
264 ESC [6~, ESC [6~, ESC [6~, ESC [6~ /# Page Down #/
265 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# End #/
266 ESC [D, ESC [D, ESC [D, ESC [D /# Keypad Left #/
267 ESC [C, ESC [C, ESC [C, ESC [C /# Keypad Right #/
268 ESC [A, ESC [A, ESC [A, ESC [A /# Keypad Up #/
269 ESC [B, ESC [B, ESC [B, ESC [B /# Keypad Down #/
270 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Keypad Home #/
271 ESC [5~, ESC [5~, ESC [5~, ESC [5~ /# Keypad PgUp #/
272 ESC [6~, ESC [6~, ESC [6~, ESC [6~ /# Keypad PgDn #/
273 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# Keypad End #/
274 nothing, nothing, nothing, nothing /# Keypad 5 #/
275
276 Windows Telnet (term=xterm)
277 ==============================
278 normal shift ctrl alt
279 KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT /# Left #/
280 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT /# Right #/
281 KEY_UP, KEY_UP, KEY_UP, KEY_UP /# Up #/
282 KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN /# Down #/
283 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Home #/
284 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, KEY_PPAGE /# Page Up #/
285 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, KEY_NPAGE /# Page Down #/
286 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# End #/
287 KEY_LEFT, KEY_LEFT, KEY_LEFT, O /# Keypad Left #/
288 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, O /# Keypad Right #/
289 KEY_UP, KEY_UP, KEY_UP, O /# Keypad Up #/
290 KEY_DOWN, KEY_DOWN, KEY_DOWN, O /# Keypad Down #/
291 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Keypad Home #/
292 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, KEY_PPAGE /# Keypad PgUp #/
293 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, KEY_NPAGE /# Keypad PgDn #/
294 ESC [4~, ESC [4~, ESC [4~, O /# Keypad End #/
295 ESC [-71, nothing, nothing, O /# Keypad 5 #/
296
297 PuTTY
298 ==============================
299 normal shift ctrl alt
300 ESC [D, ESC [D, ESC OD, ESC [D /# Left #/
301 ESC [C, ESC [C, ESC OC, ESC [C /# Right #/
302 ESC [A, ESC [A, ESC OA, ESC [A /# Up #/
303 ESC [B, ESC [B, ESC OB, ESC [B /# Down #/
304 ESC [1~, ESC [1~, local win, ESC [1~ /# Home #/
305 ESC [5~, local win, local win, ESC [5~ /# Page Up #/
306 ESC [6~, local win, local win, ESC [6~ /# Page Down #/
307 ESC [4~, ESC [4~, local win, ESC [4~ /# End #/
308 ESC [D, ESC [D, ESC [D, O /# Keypad Left #/
309 ESC [C, ESC [C, ESC [C, O /# Keypad Right #/
310 ESC [A, ESC [A, ESC [A, O /# Keypad Up #/
311 ESC [B, ESC [B, ESC [B, O /# Keypad Down #/
312 ESC [1~, ESC [1~, ESC [1~, O /# Keypad Home #/
313 ESC [5~, ESC [5~, ESC [5~, O /# Keypad PgUp #/
314 ESC [6~, ESC [6~, ESC [6~, O /# Keypad PgDn #/
315 ESC [4~, ESC [4~, ESC [4~, O /# Keypad End #/
316 nothing, nothing, nothing, O /# Keypad 5 #/
317
318 PuTTY
319 ==============================
320 normal shift ctrl alt
321 KEY_LEFT, KEY_LEFT, ESC OD, ESC KEY_LEFT /# Left #/
322 KEY_RIGHT KEY_RIGHT, ESC OC, ESC KEY_RIGHT /# Right #/
323 KEY_UP, KEY_UP, ESC OA, ESC KEY_UP /# Up #/
324 KEY_DOWN, KEY_DOWN, ESC OB, ESC KEY_DOWN /# Down #/
325 ESC [1~, ESC [1~, local win, ESC ESC [1~ /# Home #/
326 KEY_PPAGE local win, local win, ESC KEY_PPAGE /# Page Up #/