diff --git a/headers/build/private/shared/Keymap.h b/headers/build/private/shared/Keymap.h new file mode 100644 index 0000000000..464db33f09 --- /dev/null +++ b/headers/build/private/shared/Keymap.h @@ -0,0 +1,60 @@ +/* + * Copyright 2004-2010, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Jérôme Duval + * Axel Dörfler, axeld@pinc-software.de. + */ +#ifndef _KEYMAP_H +#define _KEYMAP_H + + +#include +#include + + +class BKeymap { +public: + BKeymap(); + virtual ~BKeymap(); + + status_t SetTo(const char* path); + status_t SetTo(BDataIO& stream); + status_t SetToCurrent(); + status_t SetToDefault(); + void Unset(); + + bool IsModifierKey(uint32 keyCode) const; + uint32 Modifier(uint32 keyCode) const; + uint32 KeyForModifier(uint32 modifier) const; + uint8 IsDeadKey(uint32 keyCode, + uint32 modifiers, + bool* isEnabled = NULL) const; + bool IsDeadSecondKey(uint32 keyCode, + uint32 modifiers, + uint8 activeDeadKey) const; + void GetChars(uint32 keyCode, uint32 modifiers, + uint8 activeDeadKey, char** chars, + int32* numBytes) const; + + const key_map& Map() const { return fKeys; } + + bool operator==(const BKeymap& other) const; + bool operator!=(const BKeymap& other) const; + + BKeymap& operator=(const BKeymap& other); + +protected: + int32 Offset(uint32 keyCode, uint32 modifiers, + uint32* _table = NULL) const; + uint8 DeadKeyIndex(int32 offset) const; + +protected: + char* fChars; + key_map fKeys; + uint32 fCharsSize; +}; + + +#endif // KEYMAP_H diff --git a/src/bin/keymap/Jamfile b/src/bin/keymap/Jamfile index 6802086ad9..158a0f1e0d 100644 --- a/src/bin/keymap/Jamfile +++ b/src/bin/keymap/Jamfile @@ -1,13 +1,9 @@ SubDir HAIKU_TOP src bin keymap ; SetSubDirSupportedPlatformsBeOSCompatible ; +UsePrivateHeaders shared storage ; BinCommand keymap : main.cpp Keymap.cpp - : be $(TARGET_LIBSUPC++) ; - -Package haiku-inputkit-cvs : - keymap : - boot home config bin ; - + : be libshared.a $(TARGET_LIBSUPC++) ; diff --git a/src/bin/keymap/Keymap.cpp b/src/bin/keymap/Keymap.cpp index ceb8bcb23b..3534ceef04 100644 --- a/src/bin/keymap/Keymap.cpp +++ b/src/bin/keymap/Keymap.cpp @@ -198,86 +198,12 @@ dump_keys(FILE* file, const char* name, int32* keys) Keymap::Keymap() - : - fChars(NULL), - fCharsSize(0) { - memset(&fKeys, 0, sizeof(fKeys)); } Keymap::~Keymap() { - delete[] fChars; -} - - -status_t -Keymap::LoadCurrent() -{ -#if (defined(__BEOS__) || defined(__HAIKU__)) - key_map* keys = NULL; - get_key_map(&keys, &fChars); - if (!keys) - return B_ERROR; - - memcpy(&fKeys, keys, sizeof(fKeys)); - free(keys); - return B_OK; -#else // ! __BEOS__ - fprintf(stderr, "Unsupported operation on this platform!\n"); - exit(1); -#endif // ! __BEOS__ -} - - -/*! Load a map from a file. - - file format in big endian: - struct key_map - uint32 size of following charset - charset (offsets go into this with size of character followed by - character) -*/ -status_t -Keymap::Load(const char* name) -{ - FILE* file = fopen(name, "r"); - if (file == NULL) - return errno; - - return Load(file); -} - - -status_t -Keymap::Load(FILE* file) -{ - if (fread(&fKeys, sizeof(fKeys), 1, file) < 1) - return B_BAD_VALUE; - - // convert from big-endian - for (uint32 i = 0; i < sizeof(fKeys) / 4; i++) { - ((uint32*)&fKeys)[i] = B_BENDIAN_TO_HOST_INT32(((uint32*)&fKeys)[i]); - } - - if (fKeys.version != 3) - return KEYMAP_ERROR_UNKNOWN_VERSION; - - if (fread(&fCharsSize, sizeof(uint32), 1, file) < 1) - return B_BAD_VALUE; - - fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize); - if (fCharsSize > 16 * 1024) - return B_BAD_DATA; - - delete[] fChars; - fChars = new char[fCharsSize]; - - if (fread(fChars, 1, fCharsSize, file) != fCharsSize) - return B_BAD_VALUE; - - return B_OK; } @@ -776,317 +702,6 @@ Keymap::Use() } -/*! We need to know if a key is a modifier key to choose - a valid key when several are pressed together -*/ -bool -Keymap::IsModifierKey(uint32 keyCode) -{ - return keyCode == fKeys.caps_key - || keyCode == fKeys.num_key - || keyCode == fKeys.left_shift_key - || keyCode == fKeys.right_shift_key - || keyCode == fKeys.left_command_key - || keyCode == fKeys.right_command_key - || keyCode == fKeys.left_control_key - || keyCode == fKeys.right_control_key - || keyCode == fKeys.left_option_key - || keyCode == fKeys.right_option_key - || keyCode == fKeys.menu_key; -} - - -//! Tell if a key is a dead key, needed for draw a dead key -uint8 -Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers) -{ - int32 offset; - uint32 tableMask = 0; - - switch (modifiers & 0xcf) { - case B_SHIFT_KEY: - offset = fKeys.shift_map[keyCode]; - tableMask = B_SHIFT_TABLE; - break; - case B_CAPS_LOCK: - offset = fKeys.caps_map[keyCode]; - tableMask = B_CAPS_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) - return 0; - uint32 numBytes = fChars[offset]; - - if (!numBytes) - return 0; - - char chars[4]; - strncpy(chars, &(fChars[offset+1]), numBytes); - chars[numBytes] = 0; - - int32 deadOffsets[] = { - fKeys.acute_dead_key[1], - fKeys.grave_dead_key[1], - fKeys.circumflex_dead_key[1], - fKeys.dieresis_dead_key[1], - fKeys.tilde_dead_key[1] - }; - - uint32 deadTables[] = { - fKeys.acute_tables, - fKeys.grave_tables, - fKeys.circumflex_tables, - fKeys.dieresis_tables, - fKeys.tilde_tables - }; - - for (int32 i = 0; i < 5; i++) { - if ((deadTables[i] & tableMask) == 0) - continue; - - if (offset == deadOffsets[i]) - return i+1; - - uint32 deadNumBytes = fChars[deadOffsets[i]]; - - if (!deadNumBytes) - continue; - - if (strncmp(chars, &fChars[deadOffsets[i] + 1], deadNumBytes) == 0) - return i + 1; - } - return 0; -} - - -//! Tell if a key is a dead second key, needed for draw a dead second key -bool -Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey) -{ - if (!activeDeadKey) - return false; - - int32 offset; - - switch (modifiers & 0xcf) { - case B_SHIFT_KEY: - offset = fKeys.shift_map[keyCode]; - break; - case B_CAPS_LOCK: - offset = fKeys.caps_map[keyCode]; - break; - case B_CAPS_LOCK | B_SHIFT_KEY: - offset = fKeys.caps_shift_map[keyCode]; - break; - case B_OPTION_KEY: - offset = fKeys.option_map[keyCode]; - break; - case B_OPTION_KEY | B_SHIFT_KEY: - offset = fKeys.option_shift_map[keyCode]; - break; - case B_OPTION_KEY | B_CAPS_LOCK: - offset = fKeys.option_caps_map[keyCode]; - break; - case B_OPTION_KEY | B_SHIFT_KEY|B_CAPS_LOCK: - offset = fKeys.option_caps_shift_map[keyCode]; - break; - case B_CONTROL_KEY: - offset = fKeys.control_map[keyCode]; - break; - - default: - offset = fKeys.normal_map[keyCode]; - break; - } - - uint32 numBytes = fChars[offset]; - - if (!numBytes) - return false; - - int32* deadOffsets[] = { - fKeys.acute_dead_key, - fKeys.grave_dead_key, - fKeys.circumflex_dead_key, - fKeys.dieresis_dead_key, - fKeys.tilde_dead_key - }; - - int32 *deadOffset = deadOffsets[activeDeadKey - 1]; - - for (int32 i = 0; i < 32; i++) { - if (offset == deadOffset[i]) - return true; - - uint32 deadNumBytes = fChars[deadOffset[i]]; - - if (!deadNumBytes) - continue; - - if (strncmp(&fChars[offset + 1], &fChars[deadOffset[i] + 1], - deadNumBytes) == 0) - return true; - - i++; - } - return false; -} - - -//! Get the char for a key given modifiers and active dead key -void -Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, - char** chars, int32* numBytes) -{ - int32 offset; - - *numBytes = 0; - *chars = NULL; - - // here we take NUMLOCK into account - if (modifiers & B_NUM_LOCK) { - switch (keyCode) { - case 0x37: - case 0x38: - case 0x39: - case 0x48: - case 0x49: - case 0x4a: - case 0x58: - case 0x59: - case 0x5a: - case 0x64: - case 0x65: - modifiers ^= B_SHIFT_KEY; - } - } - - // here we choose the right map given the modifiers - switch (modifiers & 0xcf) { - case B_SHIFT_KEY: - offset = fKeys.shift_map[keyCode]; - break; - case B_CAPS_LOCK: - offset = fKeys.caps_map[keyCode]; - break; - case B_CAPS_LOCK | B_SHIFT_KEY: - offset = fKeys.caps_shift_map[keyCode]; - break; - case B_OPTION_KEY: - offset = fKeys.option_map[keyCode]; - break; - case B_OPTION_KEY | B_SHIFT_KEY: - offset = fKeys.option_shift_map[keyCode]; - break; - case B_OPTION_KEY | B_CAPS_LOCK: - offset = fKeys.option_caps_map[keyCode]; - break; - case B_OPTION_KEY | B_SHIFT_KEY | B_CAPS_LOCK: - offset = fKeys.option_caps_shift_map[keyCode]; - break; - case B_CONTROL_KEY: - offset = fKeys.control_map[keyCode]; - break; - - default: - offset = fKeys.normal_map[keyCode]; - break; - } - - // here we get the char size - *numBytes = fChars[offset]; - - if (!*numBytes) - return; - - // here we take an potential active dead key - int32* deadKey; - switch (activeDeadKey) { - case 1: - deadKey = fKeys.acute_dead_key; - break; - case 2: - deadKey = fKeys.grave_dead_key; - break; - case 3: - deadKey = fKeys.circumflex_dead_key; - break; - case 4: - deadKey = fKeys.dieresis_dead_key; - break; - case 5: - deadKey = fKeys.tilde_dead_key; - break; - - default: - // if not dead, we copy and return the char - char *str = *chars = new char[*numBytes + 1]; - strncpy(str, &fChars[offset + 1], *numBytes); - str[*numBytes] = 0; - return; - } - - // If dead key, we search for our current offset char in the dead key offset - // table string comparison is needed - for (int32 i = 0; i < 32; i++) { - if (!strncmp(&fChars[offset + 1], &fChars[deadKey[i] + 1], *numBytes)) { - *numBytes = fChars[deadKey[i + 1]]; - - switch (*numBytes) { - case 0: - // Not mapped - *chars = NULL; - break; - - default: - // 1-, 2-, 3-, or 4-byte UTF-8 character - char* str = *chars = new char[*numBytes + 1]; - strncpy(str, &fChars[deadKey[i + 1] + 1], *numBytes); - str[*numBytes] = 0; - break; - } - return; - } - i++; - } - - // if not found we return the current char mapped - *chars = new char[*numBytes + 1]; - strncpy(*chars, &(fChars[offset+1]), *numBytes); - (*chars)[*numBytes] = 0; -} - - void Keymap::RestoreSystemDefault() { diff --git a/src/bin/keymap/Keymap.h b/src/bin/keymap/Keymap.h index db1b95c164..7ae2863c58 100644 --- a/src/bin/keymap/Keymap.h +++ b/src/bin/keymap/Keymap.h @@ -1,5 +1,5 @@ /* - * Copyright 2004-2009, Haiku, Inc. All Rights Reserved. + * Copyright 2004-2010, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -10,7 +10,7 @@ #define KEYMAP_H -#include +#include #if (defined(__BEOS__) || defined(__HAIKU__)) # include @@ -20,51 +20,39 @@ #include -class Keymap { +class Keymap : public BKeymap { public: - Keymap(); - ~Keymap(); + Keymap(); + ~Keymap(); - status_t LoadCurrent(); - status_t Load(const char* name); - status_t Load(FILE* file); - status_t LoadSource(const char* name); - status_t LoadSource(FILE* file); - status_t SaveAsCurrent(); - status_t Save(const char* name); - status_t SaveAsSource(const char* name); - status_t SaveAsSource(FILE* file); - status_t SaveAsCppHeader(const char* name, - const char* mapName); + status_t LoadSource(const char* name); + status_t LoadSource(FILE* file); + status_t SaveAsCurrent(); + status_t Save(const char* name); + status_t SaveAsSource(const char* name); + status_t SaveAsSource(FILE* file); + status_t SaveAsCppHeader(const char* name, + const char* mapName); - status_t Use(); + status_t Use(); - bool IsModifierKey(uint32 keyCode); - uint8 IsDeadKey(uint32 keyCode, uint32 modifiers); - bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, - uint8 activeDeadKey); - void GetChars(uint32 keyCode, uint32 modifiers, - uint8 activeDeadKey, char** chars, - int32* numBytes); - void RestoreSystemDefault(); - static bool GetKey(const char* chars, int32 offset, - char* buffer, size_t bufferSize); + void RestoreSystemDefault(); + + static bool GetKey(const char* chars, int32 offset, + char* buffer, size_t bufferSize); private: #if (defined(__BEOS__) || defined(__HAIKU__)) - void _SaveSourceText(FILE* file, - text_run_array** _textRuns = NULL); + void _SaveSourceText(FILE* file, + text_run_array** _textRuns = NULL); #else - void _SaveSourceText(FILE* file); + void _SaveSourceText(FILE* file); #endif - void _ComputeChars(const char* buffer, - struct re_registers& regs, int i, int& offset); - void _ComputeTables(const char* buffer, - struct re_registers& regs, uint32& table); - - char* fChars; - key_map fKeys; - uint32 fCharsSize; + void _ComputeChars(const char* buffer, + struct re_registers& regs, int i, + int& offset); + void _ComputeTables(const char* buffer, + struct re_registers& regs, uint32& table); }; #define KEYMAP_ERROR_UNKNOWN_VERSION (B_ERRORS_END + 1) diff --git a/src/bin/keymap/main.cpp b/src/bin/keymap/main.cpp index 02b00924a7..ba4e64a5b3 100644 --- a/src/bin/keymap/main.cpp +++ b/src/bin/keymap/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2004-2009, Haiku, Inc. All Rights Reserved. + * Copyright 2004-2010, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -14,6 +14,7 @@ #include #include +#include #include "Keymap.h" @@ -68,9 +69,11 @@ load_keymap(Keymap& keymap, const char* name, bool source) status = keymap.LoadSource(stdin); } else { if (name != NULL) - status = keymap.Load(name); - else - status = keymap.Load(stdin); + status = keymap.SetTo(name); + else { + BFileIO fileIO(stdin); + status = keymap.SetTo(fileIO); + } } if (status != B_OK) { @@ -82,7 +85,7 @@ load_keymap(Keymap& keymap, const char* name, bool source) int -main(int argc, char **argv) +main(int argc, char** argv) { const char* output = NULL; const char* input = NULL; @@ -179,7 +182,7 @@ main(int argc, char **argv) case kSaveText: { if (input == NULL) { - status_t status = keymap.LoadCurrent(); + status_t status = keymap.SetToCurrent(); if (status != B_OK) { fprintf(stderr, "%s: error while getting keymap: %s!\n", sProgramName, keymap_error(status)); diff --git a/src/build/Jamfile b/src/build/Jamfile index 33b7d5f720..ea7d40e864 100644 --- a/src/build/Jamfile +++ b/src/build/Jamfile @@ -4,4 +4,4 @@ SubInclude HAIKU_TOP src build icu ; SubInclude HAIKU_TOP src build libbe ; SubInclude HAIKU_TOP src build libhaikucompat ; SubInclude HAIKU_TOP src build libroot ; - +SubInclude HAIKU_TOP src build libshared ; diff --git a/src/build/libshared/Jamfile b/src/build/libshared/Jamfile new file mode 100644 index 0000000000..73ca5182c4 --- /dev/null +++ b/src/build/libshared/Jamfile @@ -0,0 +1,15 @@ +SubDir HAIKU_TOP src build libbe ; + +USES_BE_API on libshared_build.a = true ; + +UseHeaders [ FDirName $(HAIKU_TOP) headers build private shared ] : true ; +UseHeaders [ FDirName $(HAIKU_COMMON_DEBUG_OBJECT_DIR) servers input ] ; + +BuildPlatformStaticLibrary libshared_build.a : + Keymap.cpp +; + +Includes [ FGristFiles Keymap.cpp ] : SystemKeymap.h ; + +SEARCH on [ FGristFiles Keymap.cpp ] + = [ FDirName $(HAIKU_TOP) src kits shared ] ; diff --git a/src/kits/shared/Keymap.cpp b/src/kits/shared/Keymap.cpp index ab8af3b9b0..44ec6c4751 100644 --- a/src/kits/shared/Keymap.cpp +++ b/src/kits/shared/Keymap.cpp @@ -12,6 +12,7 @@ #include +#include #include #include diff --git a/src/tools/keymap/Jamfile b/src/tools/keymap/Jamfile index af23be6bd2..e4d1e64bad 100644 --- a/src/tools/keymap/Jamfile +++ b/src/tools/keymap/Jamfile @@ -1,5 +1,8 @@ SubDir HAIKU_TOP src tools keymap ; +UseHeaders [ FDirName $(HAIKU_TOP) headers build private shared ] : true ; +UseHeaders [ FDirName $(HAIKU_TOP) headers build private storage ] : true ; + SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src bin keymap ] ; USES_BE_API on keymap = true ; @@ -13,7 +16,7 @@ BuildPlatformMain keymap : main.cpp Keymap.cpp $(reSources) - : $(HOST_LIBSTDC++) $(HOST_LIBSUPC++) + : libshared_build.a $(HOST_LIBSTDC++) $(HOST_LIBSUPC++) ; LinkAgainst keymap : $(HOST_LIBBE) ;