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