Bochs/bochs/gui/keymap.cc
Bryce Denney cec9135e9f - Apply patch.replace-Boolean rev 1.3. Every "Boolean" is now changed to a
"bx_bool" which is always defined as Bit32u on all platforms.  In Carbon
  specific code, Boolean is still used because the Carbon header files
  define it to unsigned char.
- this fixes bug [ 623152 ] MacOSX: Triple Exception Booting win95.
  The bug was that some code in Bochs depends on Boolean to be a
  32 bit value.  (This should be fixed, but I don't know all the places
  where it needs to be fixed yet.)  Because Carbon defined Boolean as
  an unsigned char, Bochs just followed along and used the unsigned char
  definition to avoid compile problems.  This exposed the dependency
  on 32 bit Boolean on MacOS X only and led to major simulation problems,
  that could only be reproduced and debugged on that platform.
- On the mailing list we debated whether to make all Booleans into "bool" or
  our own type.  I chose bx_bool for several reasons.
  1. Unlike C++'s bool, we can guarantee that bx_bool is the same size on all
     platforms, which makes it much less likely to have more platform-specific
     simulation differences in the future.  (I spent hours on a borrowed
     MacOSX machine chasing bug 618388 before discovering that different sized
     Booleans were the problem, and I don't want to repeat that.)
  2. We still have at least one dependency on 32 bit Booleans which must be
     fixed some time, but I don't want to risk introducing new bugs into the
     simulation just before the 2.0 release.

Modified Files:
    bochs.h config.h.in gdbstub.cc logio.cc main.cc pc_system.cc
    pc_system.h plugin.cc plugin.h bios/rombios.c cpu/apic.cc
    cpu/arith16.cc cpu/arith32.cc cpu/arith64.cc cpu/arith8.cc
    cpu/cpu.cc cpu/cpu.h cpu/ctrl_xfer16.cc cpu/ctrl_xfer32.cc
    cpu/ctrl_xfer64.cc cpu/data_xfer16.cc cpu/data_xfer32.cc
    cpu/data_xfer64.cc cpu/debugstuff.cc cpu/exception.cc
    cpu/fetchdecode.cc cpu/flag_ctrl_pro.cc cpu/init.cc
    cpu/io_pro.cc cpu/lazy_flags.cc cpu/lazy_flags.h cpu/mult16.cc
    cpu/mult32.cc cpu/mult64.cc cpu/mult8.cc cpu/paging.cc
    cpu/proc_ctrl.cc cpu/segment_ctrl_pro.cc cpu/stack_pro.cc
    cpu/tasking.cc debug/dbg_main.cc debug/debug.h debug/sim2.cc
    disasm/dis_decode.cc disasm/disasm.h doc/docbook/Makefile
    docs-html/cosimulation.html fpu/wmFPUemu_glue.cc
    gui/amigaos.cc gui/beos.cc gui/carbon.cc gui/gui.cc gui/gui.h
    gui/keymap.cc gui/keymap.h gui/macintosh.cc gui/nogui.cc
    gui/rfb.cc gui/sdl.cc gui/siminterface.cc gui/siminterface.h
    gui/term.cc gui/win32.cc gui/wx.cc gui/wxmain.cc gui/wxmain.h
    gui/x.cc instrument/example0/instrument.cc
    instrument/example0/instrument.h
    instrument/example1/instrument.cc
    instrument/example1/instrument.h
    instrument/stubs/instrument.cc instrument/stubs/instrument.h
    iodev/cdrom.cc iodev/cdrom.h iodev/cdrom_osx.cc iodev/cmos.cc
    iodev/devices.cc iodev/dma.cc iodev/dma.h iodev/eth_arpback.cc
    iodev/eth_packetmaker.cc iodev/eth_packetmaker.h
    iodev/floppy.cc iodev/floppy.h iodev/guest2host.h
    iodev/harddrv.cc iodev/harddrv.h iodev/ioapic.cc
    iodev/ioapic.h iodev/iodebug.cc iodev/iodev.h
    iodev/keyboard.cc iodev/keyboard.h iodev/ne2k.h
    iodev/parallel.h iodev/pci.cc iodev/pci.h iodev/pic.h
    iodev/pit.cc iodev/pit.h iodev/pit_wrap.cc iodev/pit_wrap.h
    iodev/sb16.cc iodev/sb16.h iodev/serial.cc iodev/serial.h
    iodev/vga.cc iodev/vga.h memory/memory.h memory/misc_mem.cc
2002-10-25 11:44:41 +00:00

326 lines
10 KiB
C++

/////////////////////////////////////////////////////////////////////////
// $Id: keymap.cc,v 1.14 2002-10-25 11:44:37 bdenney 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 only by x11. Check if other guis need mapping.
// . Tables look-up should be optimised.
//
#include "bochs.h"
// Table of bocks "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 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 = 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;
}