* More cleanup, no functional change.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29671 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-03-24 09:43:39 +00:00
parent 0b5931e024
commit 193882e035
+116 -72
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2008 Haiku Inc. All rights reserved. * Copyright 2004-2009 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -8,38 +8,42 @@
*/ */
#include "Keymap.h" #include "Keymap.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ByteOrder.h> #include <ByteOrder.h>
#include <File.h> #include <File.h>
#include <input_globals.h> #include <input_globals.h>
static void static void
print_key(char *chars, int32 offset) print_key(char *chars, int32 offset)
{ {
int size = chars[offset++]; int size = chars[offset++];
switch (size) { switch (size) {
case 0: case 0:
// Not mapped // Not mapped
printf("N/A"); printf("N/A");
break; break;
case 1: case 1:
// 1-byte UTF-8/ASCII character // 1-byte UTF-8/ASCII character
printf("%c", chars[offset]); printf("%c", chars[offset]);
break; break;
default: default:
// 2-, 3-, or 4-byte UTF-8 character
{ {
// 2-, 3-, or 4-byte UTF-8 character
char *str = new char[size + 1]; char *str = new char[size + 1];
strncpy(str, &(chars[offset]), size); strncpy(str, &chars[offset], size);
str[size] = 0; str[size] = 0;
printf("%s", str); printf("%s", str);
delete [] str; delete[] str;
break;
} }
break;
} }
printf("\t"); printf("\t");
@@ -65,7 +69,6 @@ Keymap::DumpKeymap()
print_key(fChars, fKeys.control_map[i]); print_key(fChars, fKeys.control_map[i]);
printf("\n"); printf("\n");
} }
} }
@@ -118,46 +121,60 @@ Keymap::Load(entry_ref &ref)
} }
// we save a map from a file //! We save a map from a file
status_t status_t
Keymap::Save(entry_ref &ref) Keymap::Save(entry_ref& ref)
{ {
status_t err; BFile file;
status_t status = file.SetTo(&ref,
BFile file(&ref, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE ); B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if ((err = file.InitCheck()) != B_OK) { if (status != B_OK) {
printf("error %s\n", strerror(err)); printf("error %s\n", strerror(status));
return err; return status;
} }
for (uint32 i=0; i<sizeof(fKeys)/4; i++) for (uint32 i = 0; i < sizeof(fKeys) / 4; i++) {
((uint32*)&fKeys)[i] = B_HOST_TO_BENDIAN_INT32(((uint32*)&fKeys)[i]); ((uint32*)&fKeys)[i] = B_HOST_TO_BENDIAN_INT32(((uint32*)&fKeys)[i]);
if ((err = file.Write(&fKeys, sizeof(fKeys))) < (ssize_t)sizeof(fKeys)) {
return err;
} }
for (uint32 i=0; i<sizeof(fKeys)/4; i++) ssize_t bytesWritten = file.Write(&fKeys, sizeof(fKeys));
if (bytesWritten < (ssize_t)sizeof(fKeys)) {
if (bytesWritten < 0)
return bytesWritten;
return B_IO_ERROR;
}
for (uint32 i = 0; i < sizeof(fKeys) / 4; i++) {
((uint32*)&fKeys)[i] = B_BENDIAN_TO_HOST_INT32(((uint32*)&fKeys)[i]); ((uint32*)&fKeys)[i] = B_BENDIAN_TO_HOST_INT32(((uint32*)&fKeys)[i]);
}
fCharsSize = B_HOST_TO_BENDIAN_INT32(fCharsSize); fCharsSize = B_HOST_TO_BENDIAN_INT32(fCharsSize);
if ((err = file.Write(&fCharsSize, sizeof(uint32))) < (ssize_t)sizeof(uint32)) { bytesWritten = file.Write(&fCharsSize, sizeof(uint32));
return B_BAD_VALUE; if (bytesWritten < (ssize_t)sizeof(uint32)) {
if (bytesWritten < 0)
return bytesWritten;
return B_IO_ERROR;
} }
fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize); fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize);
if ((err = file.Write(fChars, fCharsSize)) < (ssize_t)fCharsSize) bytesWritten = file.Write(fChars, fCharsSize);
return err; if (bytesWritten < (ssize_t)fCharsSize) {
if (bytesWritten < 0)
return bytesWritten;
return B_IO_ERROR;
}
err = file.WriteAttr("keymap:name", B_STRING_TYPE, 0, fName, strlen(fName)); file.WriteAttr("keymap:name", B_STRING_TYPE, 0, fName, strlen(fName));
// Failing would be non-fatal
return B_OK; return B_OK;
} }
bool Keymap::Equals(const Keymap& map) const bool
Keymap::Equals(const Keymap& map) const
{ {
// not really efficient but this is the only way i found // not really efficient but this is the only way i found
// to reliably compare keymaps (used only for apply and revert) // to reliably compare keymaps (used only for apply and revert)
@@ -165,57 +182,85 @@ bool Keymap::Equals(const Keymap& map) const
} }
/* we need to know if a key is a modifier key to choose /*! We need to know if a key is a modifier key to choose
a valid key when several are pressed together a valid key when several are pressed together.
*/ */
bool bool
Keymap::IsModifierKey(uint32 keyCode) Keymap::IsModifierKey(uint32 keyCode)
{ {
if ((keyCode == fKeys.caps_key) return keyCode == fKeys.caps_key
|| (keyCode == fKeys.num_key) || keyCode == fKeys.num_key
|| (keyCode == fKeys.scroll_key) || keyCode == fKeys.scroll_key
|| (keyCode == fKeys.left_shift_key) || keyCode == fKeys.left_shift_key
|| (keyCode == fKeys.right_shift_key) || keyCode == fKeys.right_shift_key
|| (keyCode == fKeys.left_command_key) || keyCode == fKeys.left_command_key
|| (keyCode == fKeys.right_command_key) || keyCode == fKeys.right_command_key
|| (keyCode == fKeys.left_control_key) || keyCode == fKeys.left_control_key
|| (keyCode == fKeys.right_control_key) || keyCode == fKeys.right_control_key
|| (keyCode == fKeys.left_option_key) || keyCode == fKeys.left_option_key
|| (keyCode == fKeys.right_option_key) || keyCode == fKeys.right_option_key
|| (keyCode == fKeys.menu_key)) || keyCode == fKeys.menu_key;
return true;
return false;
} }
// tell if a key is a dead key, needed for draw a dead key //! Tell if a key is a dead key, needed for draw a dead key.
uint8 uint8
Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers) Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
{ {
if (fChars == NULL)
return 0;
int32 offset; int32 offset;
uint32 tableMask = 0; uint32 tableMask = 0;
switch (modifiers & 0xcf) { switch (modifiers & 0xcf) {
case B_SHIFT_KEY: offset = fKeys.shift_map[keyCode]; tableMask = B_SHIFT_TABLE; break; case B_SHIFT_KEY:
case B_CAPS_LOCK: offset = fKeys.caps_map[keyCode]; tableMask = B_CAPS_TABLE; break; offset = fKeys.shift_map[keyCode];
case B_CAPS_LOCK|B_SHIFT_KEY: offset = fKeys.caps_shift_map[keyCode]; tableMask = B_CAPS_SHIFT_TABLE; break; tableMask = B_SHIFT_TABLE;
case B_OPTION_KEY: offset = fKeys.option_map[keyCode]; tableMask = B_OPTION_TABLE; break; break;
case B_OPTION_KEY|B_SHIFT_KEY: offset = fKeys.option_shift_map[keyCode]; tableMask = B_OPTION_SHIFT_TABLE; break; case B_CAPS_LOCK:
case B_OPTION_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_map[keyCode]; tableMask = B_OPTION_CAPS_TABLE; break; offset = fKeys.caps_map[keyCode];
case B_OPTION_KEY|B_SHIFT_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_shift_map[keyCode]; tableMask = B_OPTION_CAPS_SHIFT_TABLE; break; tableMask = B_CAPS_TABLE;
case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; tableMask = B_CONTROL_TABLE; break; break;
default: offset = fKeys.normal_map[keyCode]; tableMask = B_NORMAL_TABLE; break; case B_CAPS_LOCK|B_SHIFT_KEY:
offset = fKeys.caps_shift_map[keyCode];
tableMask = B_CAPS_SHIFT_TABLE;
break;
case B_OPTION_KEY:
offset = fKeys.option_map[keyCode];
tableMask = B_OPTION_TABLE;
break;
case B_OPTION_KEY|B_SHIFT_KEY:
offset = fKeys.option_shift_map[keyCode];
tableMask = B_OPTION_SHIFT_TABLE;
break;
case B_OPTION_KEY|B_CAPS_LOCK:
offset = fKeys.option_caps_map[keyCode];
tableMask = B_OPTION_CAPS_TABLE;
break;
case B_OPTION_KEY|B_SHIFT_KEY|B_CAPS_LOCK:
offset = fKeys.option_caps_shift_map[keyCode];
tableMask = B_OPTION_CAPS_SHIFT_TABLE;
break;
case B_CONTROL_KEY:
offset = fKeys.control_map[keyCode];
tableMask = B_CONTROL_TABLE;
break;
default:
offset = fKeys.normal_map[keyCode];
tableMask = B_NORMAL_TABLE;
break;
} }
if (offset<=0) if (offset <= 0)
return 0; return 0;
uint32 numBytes = fChars[offset];
uint32 numBytes = fChars[offset];
if (!numBytes) if (!numBytes)
return 0; return 0;
char chars[4]; char chars[4];
strncpy(chars, &(fChars[offset+1]), numBytes ); strncpy(chars, &fChars[offset + 1], numBytes);
chars[numBytes] = 0; chars[numBytes] = 0;
int32 deadOffsets[] = { int32 deadOffsets[] = {
@@ -234,27 +279,25 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
fKeys.tilde_tables fKeys.tilde_tables
}; };
for (int32 i=0; i<5; i++) { for (int32 i = 0; i < 5; i++) {
if ((deadTables[i] & tableMask) == 0) if ((deadTables[i] & tableMask) == 0)
continue; continue;
if (offset == deadOffsets[i]) if (offset == deadOffsets[i])
return i+1; return i + 1;
uint32 deadNumBytes = fChars[deadOffsets[i]]; uint32 deadNumBytes = fChars[deadOffsets[i]];
if (!deadNumBytes) if (!deadNumBytes)
continue; continue;
if (strncmp(chars, &(fChars[deadOffsets[i]+1]), deadNumBytes ) == 0) { if (strncmp(chars, &fChars[deadOffsets[i] + 1], deadNumBytes) == 0)
return i+1; return i + 1;
}
} }
return 0; return 0;
} }
// tell if a key is a dead second key, needed for draw a dead second key //! Tell if a key is a dead second key, needed for draw a dead second key.
bool bool
Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey) Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
{ {
@@ -299,7 +342,8 @@ Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
if (!deadNumBytes) if (!deadNumBytes)
continue; continue;
if (strncmp(&(fChars[offset+1]), &(fChars[deadOffset[i]+1]), deadNumBytes ) == 0) if (strncmp(&fChars[offset + 1], &fChars[deadOffset[i] + 1],
deadNumBytes) == 0)
return true; return true;
i++; i++;
} }
@@ -390,7 +434,7 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
{ {
// 1-, 2-, 3-, or 4-byte UTF-8 character // 1-, 2-, 3-, or 4-byte UTF-8 character
char *str = *chars = new char[*numBytes + 1]; char *str = *chars = new char[*numBytes + 1];
strncpy(str, &fChars[deadKey[i + 1] + 1], *numBytes ); strncpy(str, &fChars[deadKey[i + 1] + 1], *numBytes);
str[*numBytes] = 0; str[*numBytes] = 0;
break; break;
} }