• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2011 Intel Corporation; author: H. Peter Anvin
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  * keyname.c
30  *
31  * Conversion between strings and get_key() key numbers.
32  */
33 
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/times.h>
41 #include <getkey.h>
42 #include <libutil.h>
43 
44 struct keyname {
45     const char *string;
46     int key;
47 };
48 
49 static const struct keyname key_names[] = {
50     { "Backspace", KEY_BACKSPACE },
51     { "Tab", KEY_TAB },
52     { "Enter", KEY_ENTER },
53     { "Esc", KEY_ESC },
54     { "Escape", KEY_ESC },
55     { "Space", ' ' },
56     { "^?", KEY_DEL },
57     { "F1", KEY_F1 },
58     { "F2", KEY_F2},
59     { "F3", KEY_F3 },
60     { "F4", KEY_F4 },
61     { "F5", KEY_F5 },
62     { "F6", KEY_F6 },
63     { "F7", KEY_F7 },
64     { "F8", KEY_F8 },
65     { "F9", KEY_F9 },
66     { "F10", KEY_F10 },
67     { "F11", KEY_F11 },
68     { "F12", KEY_F12 },
69     { "Up", KEY_UP },
70     { "Down", KEY_DOWN },
71     { "Left", KEY_LEFT },
72     { "Right", KEY_RIGHT },
73     { "PgUp", KEY_PGUP },
74     { "PgDn", KEY_PGDN },
75     { "Home", KEY_HOME },
76     { "End", KEY_END },
77     { "Insert", KEY_INSERT },
78     { "Delete", KEY_DELETE },
79     { NULL, KEY_NONE }
80 };
81 
key_name_to_code(const char * code)82 int key_name_to_code(const char *code)
83 {
84     const struct keyname *name;
85 
86     if (code[0] && !code[1]) {
87 	/* Single character */
88 	return (unsigned char)code[0];
89     } else if (code[0] == '^' && code[1] && !code[2]) {
90 	/* Control character */
91 	if (code[1] == '?')
92 	    return 0x7f;
93 	else
94 	    return (unsigned char)code[1] & 0x9f;
95     }
96 
97 
98     for (name = key_names; name->string; name++) {
99 	if (!strcasecmp(name->string, code))
100 	    break;
101     }
102     return name->key;	/* KEY_NONE at end of array */
103 }
104 
key_code_to_name(int key)105 const char *key_code_to_name(int key)
106 {
107     static char buf[4];
108     const struct keyname *name;
109 
110     if (key < 0)
111 	return NULL;
112 
113     if (key > ' ' && key < 0x100) {
114 	if (key & 0x60) {
115 	    buf[0] = key;
116 	    buf[1] = '\0';
117 	} else {
118 	    buf[0] = '^';
119 	    buf[1] = key | 0x40;
120 	    buf[2] = '\0';
121 	}
122 	return buf;
123     }
124 
125     for (name = key_names; name->string; name++) {
126 	if (key == name->key)
127 	    return name->string;
128     }
129 
130     if (key < ' ') {
131 	buf[0] = '^';
132 	buf[1] = key | 0x40;
133 	buf[2] = '\0';
134 	return buf;
135     }
136 
137     return NULL;
138 }
139