1 |
2 |
alfik |
/////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// $Id: keymap.cc 11058 2012-02-23 17:16:35Z vruppert $
|
3 |
|
|
/////////////////////////////////////////////////////////////////////////
|
4 |
|
|
//
|
5 |
|
|
// Copyright (C) 2002-2009 The Bochs Project
|
6 |
|
|
//
|
7 |
|
|
// This library is free software; you can redistribute it and/or
|
8 |
|
|
// modify it under the terms of the GNU Lesser General Public
|
9 |
|
|
// License as published by the Free Software Foundation; either
|
10 |
|
|
// version 2 of the License, or (at your option) any later version.
|
11 |
|
|
//
|
12 |
|
|
// This library is distributed in the hope that it will be useful,
|
13 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
|
|
// Lesser General Public License for more details.
|
16 |
|
|
//
|
17 |
|
|
// You should have received a copy of the GNU Lesser General Public
|
18 |
|
|
// License along with this library; if not, write to the Free Software
|
19 |
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
20 |
|
|
|
21 |
|
|
/////////////////////////////////////////////////////////////////////////
|
22 |
|
|
//
|
23 |
|
|
// Todo
|
24 |
|
|
// . Currently supported by sdl, wxGTK and x11. Check if other guis need mapping.
|
25 |
|
|
// . Tables look-up should be optimised.
|
26 |
|
|
//
|
27 |
|
|
|
28 |
|
|
#include "param_names.h"
|
29 |
|
|
#include "bochs.h"
|
30 |
|
|
#include "keymap.h"
|
31 |
|
|
|
32 |
|
|
// Table of bochs "BX_KEY_*" symbols
|
33 |
|
|
// the table must be in BX_KEY_* order
|
34 |
|
|
const char *bx_key_symbol[BX_KEY_NBKEYS] = {
|
35 |
|
|
"BX_KEY_CTRL_L", "BX_KEY_SHIFT_L", "BX_KEY_F1",
|
36 |
|
|
"BX_KEY_F2", "BX_KEY_F3", "BX_KEY_F4",
|
37 |
|
|
"BX_KEY_F5", "BX_KEY_F6", "BX_KEY_F7",
|
38 |
|
|
"BX_KEY_F8", "BX_KEY_F9", "BX_KEY_F10",
|
39 |
|
|
"BX_KEY_F11", "BX_KEY_F12", "BX_KEY_CTRL_R",
|
40 |
|
|
"BX_KEY_SHIFT_R", "BX_KEY_CAPS_LOCK", "BX_KEY_NUM_LOCK",
|
41 |
|
|
"BX_KEY_ALT_L", "BX_KEY_ALT_R", "BX_KEY_A",
|
42 |
|
|
"BX_KEY_B", "BX_KEY_C", "BX_KEY_D",
|
43 |
|
|
"BX_KEY_E", "BX_KEY_F", "BX_KEY_G",
|
44 |
|
|
"BX_KEY_H", "BX_KEY_I", "BX_KEY_J",
|
45 |
|
|
"BX_KEY_K", "BX_KEY_L", "BX_KEY_M",
|
46 |
|
|
"BX_KEY_N", "BX_KEY_O", "BX_KEY_P",
|
47 |
|
|
"BX_KEY_Q", "BX_KEY_R", "BX_KEY_S",
|
48 |
|
|
"BX_KEY_T", "BX_KEY_U", "BX_KEY_V",
|
49 |
|
|
"BX_KEY_W", "BX_KEY_X", "BX_KEY_Y",
|
50 |
|
|
"BX_KEY_Z", "BX_KEY_0", "BX_KEY_1",
|
51 |
|
|
"BX_KEY_2", "BX_KEY_3", "BX_KEY_4",
|
52 |
|
|
"BX_KEY_5", "BX_KEY_6", "BX_KEY_7",
|
53 |
|
|
"BX_KEY_8", "BX_KEY_9", "BX_KEY_ESC",
|
54 |
|
|
"BX_KEY_SPACE", "BX_KEY_SINGLE_QUOTE", "BX_KEY_COMMA",
|
55 |
|
|
"BX_KEY_PERIOD", "BX_KEY_SLASH", "BX_KEY_SEMICOLON",
|
56 |
|
|
"BX_KEY_EQUALS", "BX_KEY_LEFT_BRACKET", "BX_KEY_BACKSLASH",
|
57 |
|
|
"BX_KEY_RIGHT_BRACKET", "BX_KEY_MINUS", "BX_KEY_GRAVE",
|
58 |
|
|
"BX_KEY_BACKSPACE", "BX_KEY_ENTER", "BX_KEY_TAB",
|
59 |
|
|
"BX_KEY_LEFT_BACKSLASH", "BX_KEY_PRINT", "BX_KEY_SCRL_LOCK",
|
60 |
|
|
"BX_KEY_PAUSE", "BX_KEY_INSERT", "BX_KEY_DELETE",
|
61 |
|
|
"BX_KEY_HOME", "BX_KEY_END", "BX_KEY_PAGE_UP",
|
62 |
|
|
"BX_KEY_PAGE_DOWN", "BX_KEY_KP_ADD", "BX_KEY_KP_SUBTRACT",
|
63 |
|
|
"BX_KEY_KP_END", "BX_KEY_KP_DOWN", "BX_KEY_KP_PAGE_DOWN",
|
64 |
|
|
"BX_KEY_KP_LEFT", "BX_KEY_KP_RIGHT", "BX_KEY_KP_HOME",
|
65 |
|
|
"BX_KEY_KP_UP", "BX_KEY_KP_PAGE_UP", "BX_KEY_KP_INSERT",
|
66 |
|
|
"BX_KEY_KP_DELETE", "BX_KEY_KP_5", "BX_KEY_UP",
|
67 |
|
|
"BX_KEY_DOWN", "BX_KEY_LEFT", "BX_KEY_RIGHT",
|
68 |
|
|
"BX_KEY_KP_ENTER", "BX_KEY_KP_MULTIPLY", "BX_KEY_KP_DIVIDE",
|
69 |
|
|
"BX_KEY_WIN_L", "BX_KEY_WIN_R", "BX_KEY_MENU",
|
70 |
|
|
"BX_KEY_ALT_SYSREQ", "BX_KEY_CTRL_BREAK", "BX_KEY_INT_BACK",
|
71 |
|
|
"BX_KEY_INT_FORWARD", "BX_KEY_INT_STOP", "BX_KEY_INT_MAIL",
|
72 |
|
|
"BX_KEY_INT_SEARCH", "BX_KEY_INT_FAV", "BX_KEY_INT_HOME",
|
73 |
|
|
"BX_KEY_POWER_MYCOMP", "BX_KEY_POWER_CALC", "BX_KEY_POWER_SLEEP",
|
74 |
|
|
"BX_KEY_POWER_POWER", "BX_KEY_POWER_WAKE",
|
75 |
|
|
};
|
76 |
|
|
|
77 |
|
|
bx_keymap_c bx_keymap;
|
78 |
|
|
|
79 |
|
|
#define LOG_THIS bx_keymap.
|
80 |
|
|
|
81 |
|
|
bx_keymap_c::bx_keymap_c(void)
|
82 |
|
|
{
|
83 |
|
|
put("keymap", "KMAP");
|
84 |
|
|
|
85 |
|
|
keymapCount = 0;
|
86 |
|
|
keymapTable = (BXKeyEntry *)NULL;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
bx_keymap_c::~bx_keymap_c(void)
|
90 |
|
|
{
|
91 |
|
|
if(keymapTable != NULL) {
|
92 |
|
|
free(keymapTable);
|
93 |
|
|
keymapTable = (BXKeyEntry *)NULL;
|
94 |
|
|
}
|
95 |
|
|
keymapCount = 0;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
void bx_keymap_c::loadKeymap(Bit32u stringToSymbol(const char*))
|
99 |
|
|
{
|
100 |
|
|
if (SIM->get_param_bool(BXPN_KBD_USEMAPPING)->get()) {
|
101 |
|
|
loadKeymap(stringToSymbol, SIM->get_param_string(BXPN_KBD_KEYMAP)->getptr());
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
bx_bool bx_keymap_c::isKeymapLoaded ()
|
106 |
|
|
{
|
107 |
|
|
return (keymapCount > 0);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
///////////////////
|
112 |
|
|
// I'll add these to the keymap object in a minute.
|
113 |
|
|
static unsigned char *lineptr = NULL;
|
114 |
|
|
static int lineCount;
|
115 |
|
|
|
116 |
|
|
static void init_parse()
|
117 |
|
|
{
|
118 |
|
|
lineCount = 0;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
static void init_parse_line(char *line_to_parse)
|
122 |
|
|
{
|
123 |
|
|
// chop off newline
|
124 |
|
|
lineptr = (unsigned char *)line_to_parse;
|
125 |
|
|
char *nl;
|
126 |
|
|
if ((nl = strchr(line_to_parse,'\n')) != NULL) {
|
127 |
|
|
*nl = 0;
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
static Bit32s get_next_word(char *output)
|
132 |
|
|
{
|
133 |
|
|
char *copyp = output;
|
134 |
|
|
// find first nonspace
|
135 |
|
|
while (*lineptr && isspace(*lineptr))
|
136 |
|
|
lineptr++;
|
137 |
|
|
if (!*lineptr)
|
138 |
|
|
return -1; // nothing but spaces until end of line
|
139 |
|
|
if (*lineptr == '#')
|
140 |
|
|
return -1; // nothing but a comment
|
141 |
|
|
// copy nonspaces into the output
|
142 |
|
|
while (*lineptr && !isspace(*lineptr))
|
143 |
|
|
*copyp++ = *lineptr++;
|
144 |
|
|
*copyp=0; // null terminate the copy
|
145 |
|
|
// there must be at least one nonspace, since that's why we stopped the
|
146 |
|
|
// first loop!
|
147 |
|
|
BX_ASSERT (copyp != output);
|
148 |
|
|
return 0;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
static Bit32s get_next_keymap_line (FILE *fp, char *bxsym, char *modsym, Bit32s *ascii, char *hostsym)
|
152 |
|
|
{
|
153 |
|
|
char line[256];
|
154 |
|
|
char buf[256];
|
155 |
|
|
line[0] = 0;
|
156 |
|
|
while (1) {
|
157 |
|
|
lineCount++;
|
158 |
|
|
if (!fgets(line, sizeof(line)-1, fp)) return -1; // EOF
|
159 |
|
|
init_parse_line (line);
|
160 |
|
|
if (get_next_word (bxsym) >= 0) {
|
161 |
|
|
modsym[0] = 0;
|
162 |
|
|
char *p;
|
163 |
|
|
if ((p = strchr (bxsym, '+')) != NULL) {
|
164 |
|
|
*p = 0; // truncate bxsym.
|
165 |
|
|
p++; // move one char beyond the +
|
166 |
|
|
strcpy (modsym, p); // copy the rest to modsym
|
167 |
|
|
}
|
168 |
|
|
if (get_next_word (buf) < 0) {
|
169 |
|
|
BX_PANIC (("keymap line %d: expected 3 columns", lineCount));
|
170 |
|
|
return -1;
|
171 |
|
|
}
|
172 |
|
|
if (buf[0] == '\'' && buf[2] == '\'' && buf[3]==0) {
|
173 |
|
|
*ascii = (Bit8u) buf[1];
|
174 |
|
|
} else if (!strcmp(buf, "space")) {
|
175 |
|
|
*ascii = ' ';
|
176 |
|
|
} else if (!strcmp(buf, "return")) {
|
177 |
|
|
*ascii = '\n';
|
178 |
|
|
} else if (!strcmp(buf, "tab")) {
|
179 |
|
|
*ascii = '\t';
|
180 |
|
|
} else if (!strcmp(buf, "backslash")) {
|
181 |
|
|
*ascii = '\\';
|
182 |
|
|
} else if (!strcmp(buf, "apostrophe")) {
|
183 |
|
|
*ascii = '\'';
|
184 |
|
|
} else if (!strcmp(buf, "none")) {
|
185 |
|
|
*ascii = -1;
|
186 |
|
|
} else {
|
187 |
|
|
BX_PANIC (("keymap line %d: ascii equivalent is \"%s\" but it must be char constant like 'x', or one of space,tab,return,none", lineCount, buf));
|
188 |
|
|
}
|
189 |
|
|
if (get_next_word (hostsym) < 0) {
|
190 |
|
|
BX_PANIC (("keymap line %d: expected 3 columns", lineCount));
|
191 |
|
|
return -1;
|
192 |
|
|
}
|
193 |
|
|
return 0;
|
194 |
|
|
}
|
195 |
|
|
// no words on this line, keep reading.
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
void bx_keymap_c::loadKeymap(Bit32u stringToSymbol(const char*), const char* filename)
|
200 |
|
|
{
|
201 |
|
|
FILE *keymapFile;
|
202 |
|
|
char baseSym[256], modSym[256], hostSym[256];
|
203 |
|
|
Bit32s ascii = 0;
|
204 |
|
|
Bit32u baseKey, modKey, hostKey;
|
205 |
|
|
struct stat status;
|
206 |
|
|
|
207 |
|
|
if (stat(filename, &status)) {
|
208 |
|
|
BX_PANIC(("Can not stat keymap file '%s'.",filename));
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
if (!(S_ISREG(status.st_mode))) {
|
212 |
|
|
BX_PANIC(("Keymap file '%s' is not a file",filename));
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
if((keymapFile = fopen(filename,"r"))==NULL) {
|
216 |
|
|
BX_PANIC(("Can not open keymap file '%s'.",filename));
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
BX_INFO(("Loading keymap from '%s'",filename));
|
220 |
|
|
init_parse();
|
221 |
|
|
|
222 |
|
|
// Read keymap file one line at a time
|
223 |
|
|
while(1) {
|
224 |
|
|
if (get_next_keymap_line (keymapFile,
|
225 |
|
|
baseSym, modSym, &ascii, hostSym) < 0) { break; }
|
226 |
|
|
|
227 |
|
|
// convert X_KEY_* symbols to values
|
228 |
|
|
baseKey = convertStringToBXKey(baseSym);
|
229 |
|
|
modKey = convertStringToBXKey(modSym);
|
230 |
|
|
hostKey = 0;
|
231 |
|
|
if (stringToSymbol != NULL)
|
232 |
|
|
hostKey = stringToSymbol(hostSym);
|
233 |
|
|
|
234 |
|
|
BX_DEBUG(("baseKey='%s' (%d), modSym='%s' (%d), ascii=%d, guisym='%s' (%d)", baseSym, baseKey, modSym, modKey, ascii, hostSym, hostKey));
|
235 |
|
|
|
236 |
|
|
// Check if data is valid
|
237 |
|
|
if (baseKey==BX_KEYMAP_UNKNOWN) {
|
238 |
|
|
BX_PANIC (("line %d: unknown BX_KEY constant '%s'",lineCount,baseSym));
|
239 |
|
|
continue;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
if (hostKey==BX_KEYMAP_UNKNOWN) {
|
243 |
|
|
BX_PANIC (("line %d: unknown host key name '%s' (wrong keymap ?)",lineCount,hostSym));
|
244 |
|
|
continue;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
keymapTable=(BXKeyEntry*)realloc(keymapTable,(keymapCount+1) * sizeof(BXKeyEntry));
|
248 |
|
|
|
249 |
|
|
if (keymapTable==NULL)
|
250 |
|
|
BX_PANIC(("Can not allocate memory for keymap table."));
|
251 |
|
|
|
252 |
|
|
keymapTable[keymapCount].baseKey=baseKey;
|
253 |
|
|
keymapTable[keymapCount].modKey=modKey;
|
254 |
|
|
keymapTable[keymapCount].ascii=ascii;
|
255 |
|
|
keymapTable[keymapCount].hostKey=hostKey;
|
256 |
|
|
|
257 |
|
|
keymapCount++;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
BX_INFO(("Loaded %d symbols",keymapCount));
|
261 |
|
|
|
262 |
|
|
fclose(keymapFile);
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
Bit32u bx_keymap_c::convertStringToBXKey(const char* string)
|
266 |
|
|
{
|
267 |
|
|
// We look through the bx_key_symbol table to find the searched string
|
268 |
|
|
for (Bit16u i=0; i<BX_KEY_NBKEYS; i++) {
|
269 |
|
|
if (strcmp(string,bx_key_symbol[i])==0) {
|
270 |
|
|
return i;
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
// Key is not known
|
275 |
|
|
return BX_KEYMAP_UNKNOWN;
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
BXKeyEntry *bx_keymap_c::findHostKey(Bit32u key)
|
279 |
|
|
{
|
280 |
|
|
// We look through the keymap table to find the searched key
|
281 |
|
|
for (Bit16u i=0; i<keymapCount; i++) {
|
282 |
|
|
if (keymapTable[i].hostKey == key) {
|
283 |
|
|
BX_DEBUG (("key 0x%02x matches hostKey for entry #%d", key, i));
|
284 |
|
|
return &keymapTable[i];
|
285 |
|
|
}
|
286 |
|
|
}
|
287 |
|
|
BX_DEBUG(("key %02x matches no entries", key));
|
288 |
|
|
|
289 |
|
|
// Return default
|
290 |
|
|
return NULL;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
BXKeyEntry *bx_keymap_c::findAsciiChar(Bit8u ch)
|
294 |
|
|
{
|
295 |
|
|
BX_DEBUG (("findAsciiChar (0x%02x)", ch));
|
296 |
|
|
|
297 |
|
|
// We look through the keymap table to find the searched key
|
298 |
|
|
for (Bit16u i=0; i<keymapCount; i++) {
|
299 |
|
|
if (keymapTable[i].ascii == ch) {
|
300 |
|
|
BX_DEBUG (("key %02x matches ascii for entry #%d", ch, i));
|
301 |
|
|
return &keymapTable[i];
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
BX_DEBUG (("key 0x%02x matches no entries", ch));
|
305 |
|
|
|
306 |
|
|
// Return default
|
307 |
|
|
return NULL;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
const char *bx_keymap_c::getBXKeyName(Bit32u key)
|
311 |
|
|
{
|
312 |
|
|
return bx_key_symbol[key & 0x7fffffff];
|
313 |
|
|
}
|