* 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, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: Keymap.cpp
// Author: Jérôme Duval
// Description: keymap bin
// Created : July 29, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
/*
* Copyright (c) 2004-2006, Haiku, Inc.
*
* This software is part of the Haiku distribution and is covered
* by the Haiku license.
*
* Author: Jérôme Duval
*/
#include "Keymap.h"
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <ByteOrder.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#define CHARS_TABLE_MAXSIZE 10000
Keymap::Keymap()
: fChars(NULL),
:
fChars(NULL),
fCharsSize(0)
{
}
@@ -34,8 +34,7 @@ Keymap::Keymap()
Keymap::~Keymap()
{
if (fChars)
free(fChars);
delete[] fChars;
}
@@ -77,8 +76,7 @@ Keymap::GetKey( char *chars, int32 offset, char* string)
void
Keymap::Dump()
{
printf(
"#!/bin/keymap -l\n"
printf("#!/bin/keymap -l\n"
"#\n"
"#\tRaw key numbering for 101 keyboard...\n"
"# [sys] [brk]\n"
@@ -124,8 +122,7 @@ Keymap::Dump()
printf("LOption = 0x%02lx\n", fKeys.left_option_key);
printf("ROption = 0x%02lx\n", fKeys.right_option_key);
printf("Menu = 0x%02lx\n", fKeys.menu_key);
printf(
"#\n"
printf("#\n"
"# Lock settings\n"
"# To set NumLock, do the following:\n"
"# LockSettings = NumLock\n"
@@ -141,8 +138,7 @@ Keymap::Dump()
if (fKeys.lock_settings & B_SCROLL_LOCK)
printf("ScrollLock ");
printf("\n");
printf(
"# Legend:\n"
printf("# Legend:\n"
"# n = Normal\n"
"# s = Shift\n"
"# c = Control\n"
@@ -230,7 +226,6 @@ Keymap::Dump()
printf("CapsLock-Option-Shift ");
printf("\n");
}
}
@@ -238,7 +233,6 @@ status_t
Keymap::LoadCurrent()
{
#ifdef __BEOS__
key_map *keys = NULL;
get_key_map(&keys, &fChars);
if (!keys) {
@@ -255,13 +249,15 @@ Keymap::LoadCurrent()
#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)
*/
// we load a map from a file
status_t
Keymap::Load(entry_ref &ref)
{
@@ -273,19 +269,18 @@ Keymap::Load(entry_ref &ref)
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;
}
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]);
}
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;
}
fCharsSize = B_BENDIAN_TO_HOST_INT32(fCharsSize);
if (!fChars)
@@ -566,7 +561,6 @@ Keymap::LoadSource(FILE * f)
if (error)
fprintf(stderr, error);
char buffer[1024];
delete[] fChars;
@@ -594,6 +588,7 @@ Keymap::LoadSource(FILE * f)
while (fgets(buffer, 1024-1, f) != NULL) {
if (buffer[0] == '#' || buffer[0] == '\n')
continue;
struct re_registers regs;
if (re_search(&versionBuf, buffer, strlen(buffer), 0, strlen(buffer), &regs) >= 0) {
sscanf(buffer + regs.start[1], "%ld", &fKeys.version);
@@ -626,6 +621,7 @@ Keymap::LoadSource(FILE * f)
for (int32 i = 1; i <= 3; i++) {
if (regs.end[i] - regs.start[i] <= 0)
break;
if (strncmp(buffer + regs.start[i], "CapsLock", regs.end[i] - regs.start[i]) == 0)
fKeys.lock_settings |= B_CAPS_LOCK;
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) {
uint32 keyCode;
if (sscanf(buffer + regs.start[1], "0x%lx", &keyCode) > 0) {
for (int i = 2; i <= 10; i++) {
maps[i - 2][keyCode] = offset;
ComputeChars(buffer, regs, i, offset);
@@ -683,7 +678,7 @@ Keymap::LoadSource(FILE * f)
fCharsSize = offset;
if (fKeys.version != 3)
return B_ERROR;
return KEYMAP_ERROR_UNKNOWN_VERSION;
return B_OK;
}
@@ -765,23 +760,27 @@ Keymap::SaveAsHeader(entry_ref &ref)
fprintf(f, "\tlock_settings:0x%lx,\n", fKeys.lock_settings);
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},\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},\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},\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},\n");
fprintf(f, "\toption_map:{\n");
@@ -815,23 +814,27 @@ Keymap::SaveAsHeader(entry_ref &ref)
fprintf(f, "\t},\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},\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},\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},\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},\n");
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, "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, "};\n\n");
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
*/
bool
Keymap::IsModifierKey(uint32 keyCode)
{
if ((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))
return true;
return false;
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
//! Tell if a key is a dead key, needed for draw a dead key
uint8
Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
{
@@ -931,15 +934,14 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
if (!deadNumBytes)
continue;
if (strncmp(chars, &(fChars[deadOffsets[i]+1]), deadNumBytes ) == 0) {
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
//! 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)
{
@@ -986,15 +988,17 @@ Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
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
//! 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)
Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes)
{
int32 offset;
@@ -1002,7 +1006,7 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
*chars = NULL;
// here we take NUMLOCK into account
if (modifiers & B_NUM_LOCK)
if (modifiers & B_NUM_LOCK) {
switch (keyCode) {
case 0x37:
case 0x38:
@@ -1017,6 +1021,7 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
case 0x65:
modifiers ^= B_SHIFT_KEY;
}
}
// here we choose the right map given the modifiers
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 4: dead_key = fKeys.dieresis_dead_key; break;
case 5: dead_key = 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
@@ -1066,13 +1070,12 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
// 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[dead_key[i+1]+1], *numBytes );
str[*numBytes] = 0;
}
break;
}
return;
@@ -1084,9 +1087,9 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
*chars = new char[*numBytes + 1];
strncpy(*chars, &(fChars[offset+1]), *numBytes );
(*chars)[*numBytes] = 0;
}
status_t _restore_key_map_();
@@ -1119,7 +1122,6 @@ void
Keymap::SaveAsCurrent()
{
#ifdef __BEOS__
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
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
Keymap::Use()
{
#ifdef __BEOS__
return _restore_key_map_();
#else // ! __BEOS__
+18 -18
View File
@@ -1,30 +1,26 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: Keymap.h
// Author: Jérôme Duval
// Description: keymap bin
// Created : July 30, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
/*
* Copyright (c) 2004-2006, Haiku, Inc.
*
* This software is part of the Haiku distribution and is covered
* by the Haiku license.
*
* Author: Jérôme Duval
*/
#ifndef KEYMAP_H
#define KEYMAP_H
#include <InterfaceDefs.h>
#include <Entry.h>
#include <stdio.h>
class Keymap
{
class Keymap {
public:
Keymap();
~Keymap();
status_t LoadCurrent();
status_t Load(entry_ref &ref);
status_t Save(entry_ref &ref);
@@ -37,16 +33,20 @@ public:
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 GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes);
void RestoreSystemDefault();
static void GetKey(char *chars, int32 offset, char* string);
private:
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;
};
#define KEYMAP_ERROR_UNKNOWN_VERSION (B_ERRORS_END + 1)
#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
* 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
main(int argc, char **argv)
{
@@ -60,8 +85,10 @@ main(int argc, char **argv)
return 0;
} else if (operation == 'l') {
Keymap keymap;
if (keymap.LoadSource(stdin)!=B_OK) {
printf("error when loading the keymap\n");
status_t status = keymap.LoadSource(stdin);
if (status != B_OK) {
fprintf(stderr, "error when loading the keymap: %s\n",
keymap_error(status));
return 1;
}
keymap.SaveAsCurrent();
@@ -69,34 +96,28 @@ main(int argc, char **argv)
return 0;
}
} else {
// TODO: the actual action should be issued *AFTER* the command line
// has been parsed, not mixed in.
if (operation == 'o') {
get_ref_for_path(argv[i], &outputRef);
} else if (operation == 'c') {
entry_ref ref;
get_ref_for_path(argv[i], &ref);
Keymap keymap;
if (keymap.LoadSourceFromRef(ref)!=B_OK) {
printf("error when loading the keymap\n");
return 1;
}
load_keymap_source(keymap, argv[i]);
keymap.Save(outputRef);
return 0;
} else if (operation == 'h') {
entry_ref ref;
get_ref_for_path(argv[i], &ref);
Keymap keymap;
if (keymap.LoadSourceFromRef(ref)!=B_OK) {
printf("error when loading the keymap\n");
return 1;
}
load_keymap_source(keymap, argv[i]);
keymap.SaveAsHeader(outputRef);
return 0;
} else if (operation == 'b') {
entry_ref ref;
get_ref_for_path(argv[i], &ref);
Keymap keymap;
if (keymap.Load(ref)!=B_OK) {
printf("error when loading the keymap\n");
status_t status = keymap.Load(ref);
if (status != B_OK) {
fprintf(stderr, "error when loading the keymap: %s\n",
keymap_error(status));
return 1;
}
keymap.SaveAsCurrent();