source: bootcd/isolinux/syslinux-6.03/com32/elflink/ldlinux/get_key.c

Last change on this file was e16e8f2, checked in by Edwin Eefting <edwin@datux.nl>, 3 years ago

bootstuff

  • Property mode set to 100644
File size: 6.6 KB
RevLine 
[e16e8f2]1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
4 *
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:
13 *
14 *   The above copyright notice and this permission notice shall
15 *   be included in all copies or substantial portions of the Software.
16 *
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.
25 *
26 * ----------------------------------------------------------------------- */
27
28/*
29 * get_key.c
30 *
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.
34 */
35
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>
44#include <sys/file.h>
45
46struct keycode {
47    int code;
48    int seqlen;
49    const unsigned char *seq;
50};
51
52#define CODE(x,y) { x, (sizeof y)-1, (const unsigned char *)(y) }
53
54static 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"),
68
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"),
79
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~"),
98
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~"),
115
116    /* EFI scan codes */
117    CODE(KEY_UP, "\0\x01"),
118    CODE(KEY_DOWN, "\0\x02"),
119    CODE(KEY_RIGHT, "\0\x03"),
120    CODE(KEY_LEFT, "\0\x04"),
121    CODE(KEY_HOME, "\0\x05"),
122    CODE(KEY_END, "\0\x06"),
123    CODE(KEY_INSERT, "\0\x07"),
124    CODE(KEY_DELETE, "\0\x08"),
125    CODE(KEY_PGUP, "\0\x09"),
126    CODE(KEY_PGDN, "\0\x0a"),
127    CODE(KEY_F1, "\0\x0b"),
128    CODE(KEY_F2, "\0\x0c"),
129    CODE(KEY_F3, "\0\x0d"),
130    CODE(KEY_F4, "\0\x0e"),
131    CODE(KEY_F5, "\0\x0f"),
132    CODE(KEY_F6, "\0\x10"),
133    CODE(KEY_F7, "\0\x11"),
134    CODE(KEY_F8, "\0\x12"),
135    CODE(KEY_F9, "\0\x13"),
136    CODE(KEY_F10, "\0\x14"),
137    CODE(KEY_F11, "\0\x15"),
138    CODE(KEY_F12, "\0\x16"),
139    CODE(KEY_ESC, "\0\x17"),
140};
141
142#define NCODES ((int)(sizeof keycodes/sizeof(struct keycode)))
143
144#define KEY_TIMEOUT ((CLK_TCK+9)/10)
145
146/*
147 * Attempt to decode the key sequence in 'buffer'.
148 *
149 * On success (the data in 'buffer' matches a key code) put the
150 * corresponding key code in 'code' and return 0. Return 1 if 'buffer'
151 * partially matches a key code, i.e. we need more data before we can
152 * make an unambiguous match. Return -1 if the buffer does not contain
153 * a key code.
154 */
155int get_key_decode(char *buffer, int nc, int *code)
156{
157    const struct keycode *kc;
158    int i, rv;
159
160    rv = -1;
161    for (i = 0, kc = keycodes; i < NCODES; i++, kc++) {
162        if (nc == kc->seqlen && !memcmp(buffer, kc->seq, nc)) {
163            *code = kc->code;
164            rv = 0;
165            break;
166        } else if (nc < kc->seqlen && !memcmp(buffer, kc->seq, nc)) {
167            rv = 1;
168            break;
169        }
170    }
171
172    return rv;
173}
174
175#ifdef __COM32__
176extern ssize_t __rawcon_read(struct file_info *fp, void *buf, size_t count);
177
178int raw_read(int fd, void *buf, size_t count)
179{
180        (void)fd;
181
182        /*
183         * Instead of using the read(2) stdlib function use
184         * __rawcon_read() directly since we want a single key and
185         * don't want any processing/batching of the user input to
186         * occur - we want the raw data.
187         */
188        return __rawcon_read(NULL, buf, count);
189}
190#else
191extern int raw_read(int fd, void *buf, size_t count);
192#endif
193
194__export int get_key(FILE * f, clock_t timeout)
195{
196    char buffer[KEY_MAXLEN];
197    int nc, rv;
198    int another;
199    char ch;
200    clock_t start;
201    int code;
202
203    /* We typically start in the middle of a clock tick */
204    if (timeout)
205        timeout++;
206
207    nc = 0;
208    start = times(NULL);
209    do {
210        rv = raw_read(fileno(f), &ch, 1);
211        if (rv == 0 || (rv == -1 && errno == EAGAIN)) {
212            clock_t lateness = times(NULL) - start;
213            if (nc && lateness > 1 + KEY_TIMEOUT) {
214                if (nc == 1)
215                    return (unsigned char)buffer[0];    /* timeout */
216                else if (timeout && lateness > timeout)
217                    return KEY_NONE;
218            } else if (!nc && timeout && lateness > timeout)
219                return KEY_NONE;        /* timeout before sequence */
220
221            do_idle();
222
223            another = 1;
224            continue;
225        }
226
227        start = times(NULL);
228
229        buffer[nc++] = ch;
230
231        another = 0;
232        rv = get_key_decode(buffer, nc, &code);
233        if (!rv)
234                return code;
235        else if (rv == 1)
236                another = 1;
237
238    } while (another);
239
240    /* We got an unrecognized sequence; return the first character */
241    /* We really should remember this and return subsequent characters later */
242    return (unsigned char)buffer[0];
243}
Note: See TracBrowser for help on using the repository browser.