* Improved error output.

* Fixed some minor style issues.
* fChars was allocated with new[] but deleted with free() in the Keymap destructor.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17686 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-06-01 10:05:56 +00:00
parent 9427e13a87
commit 37e0ed4ace
3 changed files with 349 additions and 327 deletions
+78 -77
View File
@@ -1,32 +1,32 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /*
// * Copyright (c) 2004-2006, Haiku, Inc.
// Copyright (c) 2004, Haiku *
// * This software is part of the Haiku distribution and is covered
// This software is part of the Haiku distribution and is covered * by the Haiku license.
// by the Haiku license. *
// * Author: Jérôme Duval
// */
// File: Keymap.cpp
// Author: Jérôme Duval
// Description: keymap bin
// Created : July 29, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include "Keymap.h" #include "Keymap.h"
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <ByteOrder.h> #include <ByteOrder.h>
#include <File.h> #include <File.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Path.h> #include <Path.h>
#include <String.h> #include <String.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#define CHARS_TABLE_MAXSIZE 10000 #define CHARS_TABLE_MAXSIZE 10000
Keymap::Keymap() Keymap::Keymap()
: fChars(NULL), :
fChars(NULL),
fCharsSize(0) fCharsSize(0)
{ {
} }
@@ -34,8 +34,7 @@ Keymap::Keymap()
Keymap::~Keymap() Keymap::~Keymap()
{ {
if (fChars) delete[] fChars;
free(fChars);
} }
@@ -77,8 +76,7 @@ Keymap::GetKey( char *chars, int32 offset, char* string)
void void
Keymap::Dump() Keymap::Dump()
{ {
printf( printf("#!/bin/keymap -l\n"
"#!/bin/keymap -l\n"
"#\n" "#\n"
"#\tRaw key numbering for 101 keyboard...\n" "#\tRaw key numbering for 101 keyboard...\n"
"# [sys] [brk]\n" "# [sys] [brk]\n"
@@ -124,8 +122,7 @@ Keymap::Dump()
printf("LOption = 0x%02lx\n", fKeys.left_option_key); printf("LOption = 0x%02lx\n", fKeys.left_option_key);
printf("ROption = 0x%02lx\n", fKeys.right_option_key); printf("ROption = 0x%02lx\n", fKeys.right_option_key);
printf("Menu = 0x%02lx\n", fKeys.menu_key); printf("Menu = 0x%02lx\n", fKeys.menu_key);
printf( printf("#\n"
"#\n"
"# Lock settings\n" "# Lock settings\n"
"# To set NumLock, do the following:\n" "# To set NumLock, do the following:\n"
"# LockSettings = NumLock\n" "# LockSettings = NumLock\n"
@@ -141,8 +138,7 @@ Keymap::Dump()
if (fKeys.lock_settings & B_SCROLL_LOCK) if (fKeys.lock_settings & B_SCROLL_LOCK)
printf("ScrollLock "); printf("ScrollLock ");
printf("\n"); printf("\n");
printf( printf("# Legend:\n"
"# Legend:\n"
"# n = Normal\n" "# n = Normal\n"
"# s = Shift\n" "# s = Shift\n"
"# c = Control\n" "# c = Control\n"
@@ -230,7 +226,6 @@ Keymap::Dump()
printf("CapsLock-Option-Shift "); printf("CapsLock-Option-Shift ");
printf("\n"); printf("\n");
} }
} }
@@ -238,7 +233,6 @@ status_t
Keymap::LoadCurrent() Keymap::LoadCurrent()
{ {
#ifdef __BEOS__ #ifdef __BEOS__
key_map *keys = NULL; key_map *keys = NULL;
get_key_map(&keys, &fChars); get_key_map(&keys, &fChars);
if (!keys) { if (!keys) {
@@ -255,13 +249,15 @@ Keymap::LoadCurrent()
#endif // ! __BEOS__ #endif // ! __BEOS__
} }
/*
/*!
Load a map from a file.
file format in big endian: file format in big endian:
struct key_map struct key_map
uint32 size of following charset uint32 size of following charset
charset (offsets go into this with size of character followed by character) charset (offsets go into this with size of character followed by character)
*/ */
// we load a map from a file
status_t status_t
Keymap::Load(entry_ref &ref) Keymap::Load(entry_ref &ref)
{ {
@@ -273,19 +269,18 @@ Keymap::Load(entry_ref &ref)
return err; return err;
} }
if (file.Read(&fKeys, sizeof(fKeys)) < (ssize_t)sizeof(fKeys)) { if (file.Read(&fKeys, sizeof(fKeys)) < (ssize_t)sizeof(fKeys))
return B_BAD_VALUE; return B_BAD_VALUE;
}
for (uint32 i=0; i<sizeof(fKeys)/4; i++) 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]);
}
if (fKeys.version != 3) if (fKeys.version != 3)
return B_ERROR; return KEYMAP_ERROR_UNKNOWN_VERSION;
if (file.Read(&fCharsSize, sizeof(uint32)) < (ssize_t)sizeof(uint32)) { if (file.Read(&fCharsSize, sizeof(uint32)) < (ssize_t)sizeof(uint32))
return B_BAD_VALUE; return B_BAD_VALUE;
}
fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize); fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize);
if (!fChars) if (!fChars)
@@ -566,7 +561,6 @@ Keymap::LoadSource(FILE * f)
if (error) if (error)
fprintf(stderr, error); fprintf(stderr, error);
char buffer[1024]; char buffer[1024];
delete[] fChars; delete[] fChars;
@@ -594,6 +588,7 @@ Keymap::LoadSource(FILE * f)
while (fgets(buffer, 1024-1, f) != NULL) { while (fgets(buffer, 1024-1, f) != NULL) {
if (buffer[0] == '#' || buffer[0] == '\n') if (buffer[0] == '#' || buffer[0] == '\n')
continue; continue;
struct re_registers regs; struct re_registers regs;
if (re_search(&versionBuf, buffer, strlen(buffer), 0, strlen(buffer), &regs) >= 0) { if (re_search(&versionBuf, buffer, strlen(buffer), 0, strlen(buffer), &regs) >= 0) {
sscanf(buffer + regs.start[1], "%ld", &fKeys.version); sscanf(buffer + regs.start[1], "%ld", &fKeys.version);
@@ -626,6 +621,7 @@ Keymap::LoadSource(FILE * f)
for (int32 i = 1; i <= 3; i++) { for (int32 i = 1; i <= 3; i++) {
if (regs.end[i] - regs.start[i] <= 0) if (regs.end[i] - regs.start[i] <= 0)
break; break;
if (strncmp(buffer + regs.start[i], "CapsLock", regs.end[i] - regs.start[i]) == 0) if (strncmp(buffer + regs.start[i], "CapsLock", regs.end[i] - regs.start[i]) == 0)
fKeys.lock_settings |= B_CAPS_LOCK; fKeys.lock_settings |= B_CAPS_LOCK;
else if (strncmp(buffer + regs.start[i], "NumLock", regs.end[i] - regs.start[i]) == 0) else if (strncmp(buffer + regs.start[i], "NumLock", regs.end[i] - regs.start[i]) == 0)
@@ -636,7 +632,6 @@ Keymap::LoadSource(FILE * f)
} else if (re_search(&keyBuf, buffer, strlen(buffer), 0, strlen(buffer), &regs) >= 0) { } else if (re_search(&keyBuf, buffer, strlen(buffer), 0, strlen(buffer), &regs) >= 0) {
uint32 keyCode; uint32 keyCode;
if (sscanf(buffer + regs.start[1], "0x%lx", &keyCode) > 0) { if (sscanf(buffer + regs.start[1], "0x%lx", &keyCode) > 0) {
for (int i = 2; i <= 10; i++) { for (int i = 2; i <= 10; i++) {
maps[i - 2][keyCode] = offset; maps[i - 2][keyCode] = offset;
ComputeChars(buffer, regs, i, offset); ComputeChars(buffer, regs, i, offset);
@@ -683,7 +678,7 @@ Keymap::LoadSource(FILE * f)
fCharsSize = offset; fCharsSize = offset;
if (fKeys.version != 3) if (fKeys.version != 3)
return B_ERROR; return KEYMAP_ERROR_UNKNOWN_VERSION;
return B_OK; return B_OK;
} }
@@ -765,23 +760,27 @@ Keymap::SaveAsHeader(entry_ref &ref)
fprintf(f, "\tlock_settings:0x%lx,\n", fKeys.lock_settings); fprintf(f, "\tlock_settings:0x%lx,\n", fKeys.lock_settings);
fprintf(f, "\tcontrol_map:{\n"); fprintf(f, "\tcontrol_map:{\n");
for (uint32 i=0; i<128; i++) for (uint32 i = 0; i < 128; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.control_map[i]); fprintf(f, "\t\t%ld,\n", fKeys.control_map[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\toption_caps_shift_map:{\n"); fprintf(f, "\toption_caps_shift_map:{\n");
for (uint32 i=0; i<128; i++) for (uint32 i = 0; i < 128; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.option_caps_shift_map[i]); fprintf(f, "\t\t%ld,\n", fKeys.option_caps_shift_map[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\toption_caps_map:{\n"); fprintf(f, "\toption_caps_map:{\n");
for (uint32 i=0; i<128; i++) for (uint32 i = 0; i < 128; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.option_caps_map[i]); fprintf(f, "\t\t%ld,\n", fKeys.option_caps_map[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\toption_shift_map:{\n"); fprintf(f, "\toption_shift_map:{\n");
for (uint32 i=0; i<128; i++) for (uint32 i = 0; i < 128; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.option_shift_map[i]); fprintf(f, "\t\t%ld,\n", fKeys.option_shift_map[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\toption_map:{\n"); fprintf(f, "\toption_map:{\n");
@@ -815,23 +814,27 @@ Keymap::SaveAsHeader(entry_ref &ref)
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\tgrave_dead_key:{\n"); fprintf(f, "\tgrave_dead_key:{\n");
for (uint32 i=0; i<32; i++) for (uint32 i = 0; i < 32; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.grave_dead_key[i]); fprintf(f, "\t\t%ld,\n", fKeys.grave_dead_key[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\tcircumflex_dead_key:{\n"); fprintf(f, "\tcircumflex_dead_key:{\n");
for (uint32 i=0; i<32; i++) for (uint32 i = 0; i < 32; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.circumflex_dead_key[i]); fprintf(f, "\t\t%ld,\n", fKeys.circumflex_dead_key[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\tdieresis_dead_key:{\n"); fprintf(f, "\tdieresis_dead_key:{\n");
for (uint32 i=0; i<32; i++) for (uint32 i = 0; i < 32; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.dieresis_dead_key[i]); fprintf(f, "\t\t%ld,\n", fKeys.dieresis_dead_key[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\ttilde_dead_key:{\n"); fprintf(f, "\ttilde_dead_key:{\n");
for (uint32 i=0; i<32; i++) for (uint32 i = 0; i < 32; i++) {
fprintf(f, "\t\t%ld,\n", fKeys.tilde_dead_key[i]); fprintf(f, "\t\t%ld,\n", fKeys.tilde_dead_key[i]);
}
fprintf(f, "\t},\n"); fprintf(f, "\t},\n");
fprintf(f, "\tacute_tables:0x%lx,\n", fKeys.acute_tables); fprintf(f, "\tacute_tables:0x%lx,\n", fKeys.acute_tables);
@@ -843,37 +846,37 @@ Keymap::SaveAsHeader(entry_ref &ref)
fprintf(f, "};\n\n"); fprintf(f, "};\n\n");
fprintf(f, "const char sSystemKeyChars[] = {\n"); fprintf(f, "const char sSystemKeyChars[] = {\n");
for (uint32 i=0; i<fCharsSize; i++) for (uint32 i = 0; i<fCharsSize; i++) {
fprintf(f, "\t%hhd,\n", fChars[i]); fprintf(f, "\t%hhd,\n", fChars[i]);
}
fprintf(f, "};\n\n"); fprintf(f, "};\n\n");
fprintf(f, "const uint32 sSystemKeyCharsSize = %ld;\n", fCharsSize); fprintf(f, "const uint32 sSystemKeyCharsSize = %ld;\n", fCharsSize);
} }
/* 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.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)
{ {
@@ -931,15 +934,14 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
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)
{ {
@@ -986,15 +988,17 @@ Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
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++;
} }
return false; return false;
} }
// get the char for a key given modifiers and active dead key //! Get the char for a key given modifiers and active dead key
void void
Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** chars, int32* numBytes) Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes)
{ {
int32 offset; int32 offset;
@@ -1002,7 +1006,7 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
*chars = NULL; *chars = NULL;
// here we take NUMLOCK into account // here we take NUMLOCK into account
if (modifiers & B_NUM_LOCK) if (modifiers & B_NUM_LOCK) {
switch (keyCode) { switch (keyCode) {
case 0x37: case 0x37:
case 0x38: case 0x38:
@@ -1017,6 +1021,7 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
case 0x65: case 0x65:
modifiers ^= B_SHIFT_KEY; modifiers ^= B_SHIFT_KEY;
} }
}
// here we choose the right map given the modifiers // here we choose the right map given the modifiers
switch (modifiers & 0xcf) { switch (modifiers & 0xcf) {
@@ -1045,15 +1050,14 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
case 3: dead_key = fKeys.circumflex_dead_key; break; case 3: dead_key = fKeys.circumflex_dead_key; break;
case 4: dead_key = fKeys.dieresis_dead_key; break; case 4: dead_key = fKeys.dieresis_dead_key; break;
case 5: dead_key = fKeys.tilde_dead_key; break; case 5: dead_key = fKeys.tilde_dead_key; break;
default: default:
{
// if not dead, we copy and return the char // if not dead, we copy and return the char
char *str = *chars = new char[*numBytes + 1]; char *str = *chars = new char[*numBytes + 1];
strncpy(str, &(fChars[offset+1]), *numBytes ); strncpy(str, &(fChars[offset+1]), *numBytes );
str[*numBytes] = 0; str[*numBytes] = 0;
return; return;
} }
}
// if dead key, we search for our current offset char in the dead key offset table // if dead key, we search for our current offset char in the dead key offset table
// string comparison is needed // string comparison is needed
@@ -1066,13 +1070,12 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
// Not mapped // Not mapped
*chars = NULL; *chars = NULL;
break; break;
default: default:
// 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[dead_key[i+1]+1], *numBytes ); strncpy(str, &fChars[dead_key[i+1]+1], *numBytes );
str[*numBytes] = 0; str[*numBytes] = 0;
}
break; break;
} }
return; return;
@@ -1084,9 +1087,9 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
*chars = new char[*numBytes + 1]; *chars = new char[*numBytes + 1];
strncpy(*chars, &(fChars[offset+1]), *numBytes ); strncpy(*chars, &(fChars[offset+1]), *numBytes );
(*chars)[*numBytes] = 0; (*chars)[*numBytes] = 0;
} }
status_t _restore_key_map_(); status_t _restore_key_map_();
@@ -1119,7 +1122,6 @@ void
Keymap::SaveAsCurrent() Keymap::SaveAsCurrent()
{ {
#ifdef __BEOS__ #ifdef __BEOS__
BPath path; BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return; return;
@@ -1143,12 +1145,11 @@ Keymap::SaveAsCurrent()
} }
// we make our input server use the map in /boot/home/config/settings/Keymap //! We make our input server use the map in /boot/home/config/settings/Keymap
status_t status_t
Keymap::Use() Keymap::Use()
{ {
#ifdef __BEOS__ #ifdef __BEOS__
return _restore_key_map_(); return _restore_key_map_();
#else // ! __BEOS__ #else // ! __BEOS__
+18 -18
View File
@@ -1,30 +1,26 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /*
// * Copyright (c) 2004-2006, Haiku, Inc.
// Copyright (c) 2004, Haiku *
// * This software is part of the Haiku distribution and is covered
// This software is part of the Haiku distribution and is covered * by the Haiku license.
// by the Haiku license. *
// * Author: Jérôme Duval
// */
// File: Keymap.h
// Author: Jérôme Duval
// Description: keymap bin
// Created : July 30, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#ifndef KEYMAP_H #ifndef KEYMAP_H
#define KEYMAP_H #define KEYMAP_H
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <Entry.h> #include <Entry.h>
#include <stdio.h> #include <stdio.h>
class Keymap
{ class Keymap {
public: public:
Keymap(); Keymap();
~Keymap(); ~Keymap();
status_t LoadCurrent(); status_t LoadCurrent();
status_t Load(entry_ref &ref); status_t Load(entry_ref &ref);
status_t Save(entry_ref &ref); status_t Save(entry_ref &ref);
@@ -37,16 +33,20 @@ public:
bool IsModifierKey(uint32 keyCode); bool IsModifierKey(uint32 keyCode);
uint8 IsDeadKey(uint32 keyCode, uint32 modifiers); uint8 IsDeadKey(uint32 keyCode, uint32 modifiers);
bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey); bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey);
void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** chars, int32* numBytes); void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes);
void RestoreSystemDefault(); void RestoreSystemDefault();
static void GetKey(char *chars, int32 offset, char* string); static void GetKey(char *chars, int32 offset, char* string);
private: private:
void ComputeChars(const char *buffer, struct re_registers &regs, int i, int &offset); void ComputeChars(const char *buffer, struct re_registers &regs, int i, int &offset);
void ComputeTables(const char *buffer, struct re_registers &regs, uint32 &table); void ComputeTables(const char *buffer, struct re_registers &regs, uint32 &table);
char *fChars; char *fChars;
key_map fKeys; key_map fKeys;
uint32 fCharsSize; uint32 fCharsSize;
}; };
#define KEYMAP_ERROR_UNKNOWN_VERSION (B_ERRORS_END + 1)
#endif // KEYMAP_H #endif // KEYMAP_H
+38 -17
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004-2005, Haiku * Copyright (c) 2004-2006, Haiku, Inc.
* *
* This software is part of the Haiku distribution and is covered * This software is part of the Haiku distribution and is covered
* by the MIT license. * by the MIT license.
@@ -33,6 +33,31 @@ usage(void)
} }
static const char *
keymap_error(status_t status)
{
if (status == KEYMAP_ERROR_UNKNOWN_VERSION)
return "Unknown keymap version";
return strerror(status);
}
static void
load_keymap_source(Keymap& keymap, const char* name)
{
entry_ref ref;
get_ref_for_path(name, &ref);
status_t status = keymap.LoadSourceFromRef(ref);
if (status != B_OK) {
fprintf(stderr, "error when loading the keymap: %s\n",
keymap_error(status));
exit(1);
}
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@@ -60,8 +85,10 @@ main(int argc, char **argv)
return 0; return 0;
} else if (operation == 'l') { } else if (operation == 'l') {
Keymap keymap; Keymap keymap;
if (keymap.LoadSource(stdin)!=B_OK) { status_t status = keymap.LoadSource(stdin);
printf("error when loading the keymap\n"); if (status != B_OK) {
fprintf(stderr, "error when loading the keymap: %s\n",
keymap_error(status));
return 1; return 1;
} }
keymap.SaveAsCurrent(); keymap.SaveAsCurrent();
@@ -69,34 +96,28 @@ main(int argc, char **argv)
return 0; return 0;
} }
} else { } else {
// TODO: the actual action should be issued *AFTER* the command line
// has been parsed, not mixed in.
if (operation == 'o') { if (operation == 'o') {
get_ref_for_path(argv[i], &outputRef); get_ref_for_path(argv[i], &outputRef);
} else if (operation == 'c') { } else if (operation == 'c') {
entry_ref ref;
get_ref_for_path(argv[i], &ref);
Keymap keymap; Keymap keymap;
if (keymap.LoadSourceFromRef(ref)!=B_OK) { load_keymap_source(keymap, argv[i]);
printf("error when loading the keymap\n");
return 1;
}
keymap.Save(outputRef); keymap.Save(outputRef);
return 0; return 0;
} else if (operation == 'h') { } else if (operation == 'h') {
entry_ref ref;
get_ref_for_path(argv[i], &ref);
Keymap keymap; Keymap keymap;
if (keymap.LoadSourceFromRef(ref)!=B_OK) { load_keymap_source(keymap, argv[i]);
printf("error when loading the keymap\n");
return 1;
}
keymap.SaveAsHeader(outputRef); keymap.SaveAsHeader(outputRef);
return 0; return 0;
} else if (operation == 'b') { } else if (operation == 'b') {
entry_ref ref; entry_ref ref;
get_ref_for_path(argv[i], &ref); get_ref_for_path(argv[i], &ref);
Keymap keymap; Keymap keymap;
if (keymap.Load(ref)!=B_OK) { status_t status = keymap.Load(ref);
printf("error when loading the keymap\n"); if (status != B_OK) {
fprintf(stderr, "error when loading the keymap: %s\n",
keymap_error(status));
return 1; return 1;
} }
keymap.SaveAsCurrent(); keymap.SaveAsCurrent();