Adding debian version 4.03+dfsg-7.
[syslinux-debian/hramrach.git] / com32 / libutil / get_key.c
blobf277b43b3dc413e4241f87d78cf9dcf8d9858cd6
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * get_key.c
31 * Get a single key, and try to pick apart function key codes.
32 * This doesn't decode anywhere close to all possiblities, but
33 * hopefully is enough to be useful.
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <time.h>
41 #include <sys/times.h>
42 #include <getkey.h>
43 #include <libutil.h>
45 struct keycode {
46 int code;
47 int seqlen;
48 const unsigned char *seq;
51 #define MAXLEN 8
52 #define CODE(x,y) { x, (sizeof y)-1, (const unsigned char *)(y) }
54 static const struct keycode keycodes[] = {
55 /* First, the BIOS combined codes */
56 CODE(KEY_F1, "\0\x3B"),
57 CODE(KEY_F2, "\0\x3C"),
58 CODE(KEY_F3, "\0\x3D"),
59 CODE(KEY_F4, "\0\x3E"),
60 CODE(KEY_F5, "\0\x3F"),
61 CODE(KEY_F6, "\0\x40"),
62 CODE(KEY_F7, "\0\x41"),
63 CODE(KEY_F8, "\0\x42"),
64 CODE(KEY_F9, "\0\x43"),
65 CODE(KEY_F10, "\0\x44"),
66 CODE(KEY_F11, "\0\x85"),
67 CODE(KEY_F12, "\0\x86"),
69 CODE(KEY_UP, "\0\x48"),
70 CODE(KEY_DOWN, "\0\x50"),
71 CODE(KEY_LEFT, "\0\x4B"),
72 CODE(KEY_RIGHT, "\0\x4D"),
73 CODE(KEY_PGUP, "\0\x49"),
74 CODE(KEY_PGDN, "\0\x51"),
75 CODE(KEY_HOME, "\0\x47"),
76 CODE(KEY_END, "\0\x4F"),
77 CODE(KEY_INSERT, "\0\x52"),
78 CODE(KEY_DELETE, "\0\x53"),
80 /* Now, VT/xterm/Linux codes */
81 CODE(KEY_F1, "\033[[A"),
82 CODE(KEY_F1, "\033OP"),
83 CODE(KEY_F2, "\033[[B"),
84 CODE(KEY_F2, "\033OQ"),
85 CODE(KEY_F3, "\033[[C"),
86 CODE(KEY_F3, "\033OR"),
87 CODE(KEY_F4, "\033[[D"),
88 CODE(KEY_F4, "\033OS"),
89 CODE(KEY_F5, "\033[[E"),
90 CODE(KEY_F5, "\033[15~"),
91 CODE(KEY_F6, "\033[17~"),
92 CODE(KEY_F7, "\033[18~"),
93 CODE(KEY_F8, "\033[19~"),
94 CODE(KEY_F9, "\033[20~"),
95 CODE(KEY_F10, "\033[21~"),
96 CODE(KEY_F11, "\033[23~"),
97 CODE(KEY_F12, "\033[24~"),
99 CODE(KEY_UP, "\033[A"),
100 CODE(KEY_DOWN, "\033[B"),
101 CODE(KEY_LEFT, "\033[D"),
102 CODE(KEY_RIGHT, "\033[C"),
103 CODE(KEY_PGUP, "\033[5~"),
104 CODE(KEY_PGUP, "\033[V"),
105 CODE(KEY_PGDN, "\033[6~"),
106 CODE(KEY_PGDN, "\033[U"),
107 CODE(KEY_HOME, "\033[1~"),
108 CODE(KEY_HOME, "\033[H"),
109 CODE(KEY_END, "\033[4~"),
110 CODE(KEY_END, "\033[F"),
111 CODE(KEY_END, "\033OF"),
112 CODE(KEY_INSERT, "\033[2~"),
113 CODE(KEY_INSERT, "\033[@"),
114 CODE(KEY_DELETE, "\033[3~"),
117 #define NCODES ((int)(sizeof keycodes/sizeof(struct keycode)))
119 #define KEY_TIMEOUT ((CLK_TCK+9)/10)
121 int get_key(FILE * f, clock_t timeout)
123 unsigned char buffer[MAXLEN];
124 int nc, i, rv;
125 const struct keycode *kc;
126 int another;
127 unsigned char ch;
128 clock_t start;
130 /* We typically start in the middle of a clock tick */
131 if (timeout)
132 timeout++;
134 nc = 0;
135 start = times(NULL);
136 do {
137 rv = read(fileno(f), &ch, 1);
138 if (rv == 0 || (rv == -1 && errno == EAGAIN)) {
139 clock_t lateness = times(NULL) - start;
140 if (nc && lateness > 1 + KEY_TIMEOUT) {
141 if (nc == 1)
142 return buffer[0]; /* timeout in sequence */
143 else if (timeout && lateness > timeout)
144 return KEY_NONE;
145 } else if (!nc && timeout && lateness > timeout)
146 return KEY_NONE; /* timeout before sequence */
148 do_idle();
150 another = 1;
151 continue;
154 start = times(NULL);
156 buffer[nc++] = ch;
158 another = 0;
159 for (i = 0, kc = keycodes; i < NCODES; i++, kc++) {
160 if (nc == kc->seqlen && !memcmp(buffer, kc->seq, nc))
161 return kc->code;
162 else if (nc < kc->seqlen && !memcmp(buffer, kc->seq, nc)) {
163 another = 1;
164 break;
167 } while (another);
169 /* We got an unrecognized sequence; return the first character */
170 /* We really should remember this and return subsequent characters later */
171 return buffer[0];