* Added libshared_build.a, currently only contains Keymap.cpp.

* keymap and <build>keymap are now using the BKeymap class as a base as well.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36368 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2010-04-20 08:17:07 +00:00
parent 624693f0c8
commit 910f10f2fc
9 changed files with 118 additions and 437 deletions

View File

@ -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 <DataIO.h>
#include <InterfaceDefs.h>
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

View File

@ -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++) ;

View File

@ -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()
{

View File

@ -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 <InterfaceDefs.h>
#include <Keymap.h>
#if (defined(__BEOS__) || defined(__HAIKU__))
# include <TextView.h>
@ -20,51 +20,39 @@
#include <regex.h>
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)

View File

@ -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 <string.h>
#include <Application.h>
#include <FileIO.h>
#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));

View File

@ -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 ;

View File

@ -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 ] : <src!servers!input>SystemKeymap.h ;
SEARCH on [ FGristFiles Keymap.cpp ]
= [ FDirName $(HAIKU_TOP) src kits shared ] ;

View File

@ -12,6 +12,7 @@
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -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 <build>keymap = true ;
@ -13,7 +16,7 @@ BuildPlatformMain <build>keymap :
main.cpp
Keymap.cpp
$(reSources)
: $(HOST_LIBSTDC++) $(HOST_LIBSUPC++)
: libshared_build.a $(HOST_LIBSTDC++) $(HOST_LIBSUPC++)
;
LinkAgainst <build>keymap : $(HOST_LIBBE) ;