aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ioemu/gui/keymap.cc
blob: 8013693c8a685ec33491085d75b6595008f5f016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/////////////////////////////////////////////////////////////////////////
// $Id: keymap.cc,v 1.16 2003/10/11 10:43:24 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2002 MandrakeSoft S.A.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

/////////////////////////////////////////////////////////////////////////
//
// Todo
//  . Currently supported by sdl, wxGTK and x11. Check if other guis need mapping.
//  . Tables look-up should be optimised.
//

#include "bochs.h"

// Table of bochs "BX_KEY_*" symbols
// the table must be in BX_KEY_* order
char *bx_key_symbol[BX_KEY_NBKEYS] = {
  "BX_KEY_CTRL_L",         "BX_KEY_SHIFT_L",        "BX_KEY_F1",
  "BX_KEY_F2",             "BX_KEY_F3",             "BX_KEY_F4",
  "BX_KEY_F5",             "BX_KEY_F6",             "BX_KEY_F7",
  "BX_KEY_F8",             "BX_KEY_F9",             "BX_KEY_F10",
  "BX_KEY_F11",            "BX_KEY_F12",            "BX_KEY_CTRL_R",
  "BX_KEY_SHIFT_R",        "BX_KEY_CAPS_LOCK",      "BX_KEY_NUM_LOCK",
  "BX_KEY_ALT_L",          "BX_KEY_ALT_R",          "BX_KEY_A",
  "BX_KEY_B",              "BX_KEY_C",              "BX_KEY_D",
  "BX_KEY_E",              "BX_KEY_F",              "BX_KEY_G",
  "BX_KEY_H",              "BX_KEY_I",              "BX_KEY_J",
  "BX_KEY_K",              "BX_KEY_L",              "BX_KEY_M",
  "BX_KEY_N",              "BX_KEY_O",              "BX_KEY_P",
  "BX_KEY_Q",              "BX_KEY_R",              "BX_KEY_S",
  "BX_KEY_T",              "BX_KEY_U",              "BX_KEY_V",
  "BX_KEY_W",              "BX_KEY_X",              "BX_KEY_Y",
  "BX_KEY_Z",              "BX_KEY_0",              "BX_KEY_1",
  "BX_KEY_2",              "BX_KEY_3",              "BX_KEY_4",
  "BX_KEY_5",              "BX_KEY_6",              "BX_KEY_7",
  "BX_KEY_8",              "BX_KEY_9",              "BX_KEY_ESC",
  "BX_KEY_SPACE",          "BX_KEY_SINGLE_QUOTE",   "BX_KEY_COMMA",
  "BX_KEY_PERIOD",         "BX_KEY_SLASH",          "BX_KEY_SEMICOLON",
  "BX_KEY_EQUALS",         "BX_KEY_LEFT_BRACKET",   "BX_KEY_BACKSLASH",
  "BX_KEY_RIGHT_BRACKET",  "BX_KEY_MINUS",          "BX_KEY_GRAVE",
  "BX_KEY_BACKSPACE",      "BX_KEY_ENTER",          "BX_KEY_TAB",
  "BX_KEY_LEFT_BACKSLASH", "BX_KEY_PRINT",          "BX_KEY_SCRL_LOCK",
  "BX_KEY_PAUSE",          "BX_KEY_INSERT",         "BX_KEY_DELETE",
  "BX_KEY_HOME",           "BX_KEY_END",            "BX_KEY_PAGE_UP",
  "BX_KEY_PAGE_DOWN",      "BX_KEY_KP_ADD",         "BX_KEY_KP_SUBTRACT",
  "BX_KEY_KP_END",         "BX_KEY_KP_DOWN",        "BX_KEY_KP_PAGE_DOWN",
  "BX_KEY_KP_LEFT",        "BX_KEY_KP_RIGHT",       "BX_KEY_KP_HOME",
  "BX_KEY_KP_UP",          "BX_KEY_KP_PAGE_UP",     "BX_KEY_KP_INSERT",
  "BX_KEY_KP_DELETE",      "BX_KEY_KP_5",           "BX_KEY_UP",
  "BX_KEY_DOWN",           "BX_KEY_LEFT",           "BX_KEY_RIGHT",
  "BX_KEY_KP_ENTER",       "BX_KEY_KP_MULTIPLY",    "BX_KEY_KP_DIVIDE",
  "BX_KEY_WIN_L",          "BX_KEY_WIN_R",          "BX_KEY_MENU",           
  "BX_KEY_ALT_SYSREQ",     "BX_KEY_CTRL_BREAK",     "BX_KEY_INT_BACK",       
  "BX_KEY_INT_FORWARD",    "BX_KEY_INT_STOP",       "BX_KEY_INT_MAIL",       
  "BX_KEY_INT_SEARCH",     "BX_KEY_INT_FAV",        "BX_KEY_INT_HOME",       
  "BX_KEY_POWER_MYCOMP",   "BX_KEY_POWER_CALC",     "BX_KEY_POWER_SLEEP",    
  "BX_KEY_POWER_POWER",    "BX_KEY_POWER_WAKE",
  };

bx_keymap_c bx_keymap;

#define LOG_THIS bx_keymap.

bx_keymap_c::bx_keymap_c(void)
{
    put("KMAP");

    keymapCount = 0;
    keymapTable = (BXKeyEntry *)NULL;

}

bx_keymap_c::~bx_keymap_c(void)
{
    if(keymapTable != NULL) {
      free(keymapTable);
      keymapTable = (BXKeyEntry *)NULL;
      }
    keymapCount = 0;
}

    void
bx_keymap_c::loadKeymap(Bit32u stringToSymbol(const char*))
{
  if(bx_options.keyboard.OuseMapping->get()) {
    loadKeymap(stringToSymbol,bx_options.keyboard.Okeymap->getptr());
    }
}


bx_bool 
bx_keymap_c::isKeymapLoaded ()
{
  return (keymapCount > 0);
}


///////////////////
// I'll add these to the keymap object in a minute.
static unsigned char *lineptr = NULL;
static int lineCount;

static void
init_parse ()
{
  lineCount = 0;
}

static void
init_parse_line (char *line_to_parse)
{
  // chop off newline
  lineptr = (unsigned char *)line_to_parse;
  char *nl;
  if( (nl = strchr(line_to_parse,'\n')) != NULL) {
    *nl = 0;
  }
}

static Bit32s
get_next_word (char *output)
{
  char *copyp = output;
  // find first nonspace
  while (*lineptr && isspace (*lineptr))
    lineptr++;
  if (!*lineptr) 
    return -1;  // nothing but spaces until end of line
  if (*lineptr == '#')
    return -1;  // nothing but a comment
  // copy nonspaces into the output
  while (*lineptr && !isspace (*lineptr))
    *copyp++ = *lineptr++;
  *copyp=0;  // null terminate the copy
  // there must be at least one nonspace, since that's why we stopped the
  // first loop!
  BX_ASSERT (copyp != output);
  return 0;
}

static Bit32s
get_next_keymap_line (FILE *fp, char *bxsym, char *modsym, Bit32s *ascii, char *hostsym)
{
  char line[256];
  char buf[256];
  line[0] = 0;
  while (1) {
    lineCount++;
    if (!fgets(line, sizeof(line)-1, fp)) return -1;  // EOF
    init_parse_line (line);
    if (get_next_word (bxsym) >= 0) {
      modsym[0] = 0;
      char *p;
      if ((p = strchr (bxsym, '+')) != NULL) {
	*p = 0;  // truncate bxsym.
	p++;  // move one char beyond the +
	strcpy (modsym, p);  // copy the rest to modsym
      }
      if (get_next_word (buf) < 0) {
	BX_PANIC (("keymap line %d: expected 3 columns", lineCount));
	return -1;
      }
      if (buf[0] == '\'' && buf[2] == '\'' && buf[3]==0) {
	*ascii = (Bit8u) buf[1];
      } else if (!strcmp(buf, "space")) {
	*ascii = ' ';
      } else if (!strcmp(buf, "return")) {
	*ascii = '\n';
      } else if (!strcmp(buf, "tab")) {
	*ascii = '\t';
      } else if (!strcmp(buf, "backslash")) {
	*ascii = '\\';
      } else if (!strcmp(buf, "apostrophe")) {
	*ascii = '\'';
      } else if (!strcmp(buf, "none")) {
	*ascii = -1;
      } else {
	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));
      }
      if (get_next_word (hostsym) < 0) {
        BX_PANIC (("keymap line %d: expected 3 columns", lineCount));
	return -1;
      }
      return 0;
    }
    // no words on this line, keep reading.
  }
}

    void
bx_keymap_c::loadKeymap(Bit32u stringToSymbol(const char*), const char* filename)
{
    FILE   *keymapFile;
    char baseSym[256], modSym[256], hostSym[256]; 
    Bit32s ascii;
    Bit32u baseKey, modKey, hostKey;
    struct stat status;

    if (stat(filename, &status)) {
      BX_PANIC(("Can not stat keymap file '%s'.",filename));
      }

    if (!(S_ISREG(status.st_mode))) {
      BX_PANIC(("Keymap file '%s' is not a file",filename));
      }

    if((keymapFile = fopen(filename,"r"))==NULL) {
      BX_PANIC(("Can not open keymap file '%s'.",filename));
      }
    
    BX_INFO(("Loading keymap from '%s'",filename));
    init_parse ();

    // Read keymap file one line at a time
    while(1) {
      if (get_next_keymap_line (keymapFile, 
            baseSym, modSym, &ascii, hostSym) < 0) { break; }


      // convert X_KEY_* symbols to values
      baseKey = convertStringToBXKey(baseSym);
      modKey = convertStringToBXKey(modSym);
      hostKey = 0;
      if (stringToSymbol != NULL)
        hostKey = stringToSymbol(hostSym);

      BX_DEBUG (("baseKey='%s' (%d), modSym='%s' (%d), ascii=%d, guisym='%s' (%d)", baseSym, baseKey, modSym, modKey, ascii, hostSym, hostKey));
       
      // Check if data is valid
      if( baseKey==BX_KEYMAP_UNKNOWN ) {
        BX_PANIC (("line %d: unknown BX_KEY constant '%s'",lineCount,baseSym));
        continue;
        }

      if( hostKey==BX_KEYMAP_UNKNOWN ) {
        BX_PANIC (("line %d: unknown host key name '%s'",lineCount,hostSym));
        continue;
        }

      keymapTable=(BXKeyEntry*)realloc(keymapTable,(keymapCount+1) * sizeof(BXKeyEntry));
      
      if(keymapTable==NULL) 
        BX_PANIC(("Can not allocate memory for keymap table."));

      keymapTable[keymapCount].baseKey=baseKey;
      keymapTable[keymapCount].modKey=modKey;
      keymapTable[keymapCount].ascii=ascii;
      keymapTable[keymapCount].hostKey=hostKey;
      
      keymapCount++;
      }

    BX_INFO(("Loaded %d symbols",keymapCount));

    fclose(keymapFile);
}

    Bit32u
bx_keymap_c::convertStringToBXKey(const char* string)
{
    Bit16u i;

    // We look through the bx_key_symbol table to find the searched string
    for (i=0; i<BX_KEY_NBKEYS; i++) {
      if (strcmp(string,bx_key_symbol[i])==0) {
        return i;
        }
      }
  
    // Key is not known
    return BX_KEYMAP_UNKNOWN;
}

    BXKeyEntry *
bx_keymap_c::findHostKey(Bit32u key)
{
    Bit16u i;

    // We look through the keymap table to find the searched key
    for (i=0; i<keymapCount; i++) {
      if (keymapTable[i].hostKey == key) {
	BX_DEBUG (("key 0x%02x matches hostKey for entry #%d", key, i));
        return &keymapTable[i];
        }
      }
    BX_DEBUG (("key %02x matches no entries", key));

    // Return default
    return NULL;
}

    BXKeyEntry *
bx_keymap_c::findAsciiChar(Bit8u ch)
{
    Bit16u i;
    BX_DEBUG (("findAsciiChar (0x%02x)", ch));

    // We look through the keymap table to find the searched key
    for (i=0; i<keymapCount; i++) {
      if (keymapTable[i].ascii == ch) {
	BX_DEBUG (("key %02x matches ascii for entry #%d", ch, i));
        return &keymapTable[i];
        }
      }
    BX_DEBUG (("key 0x%02x matches no entries", ch));

    // Return default
    return NULL;
}

    char *
bx_keymap_c::getBXKeyName(Bit32u key)
{
    return bx_key_symbol[key & 0x7fffffff];
}