* More or less rewrote main.cpp to use getopt_long() instead of the self-made
argument evaluation. Also separated the evaluation from performing the options, solving a TODO, and added long versions for the options. * Changed Keymap methods to work on a path instead of entry_refs - this simplifies the code a bit over the place. * Added new option to load a source keymap directly (-s, --load-source). * Renamed some methods to make clearer what they do. * Added a way to directly write a source keymap to a file (by specifying the -o option together with -d). * Coding style cleanup, ordered Keymap methods in the order they are declared. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31195 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+731
-562
File diff suppressed because it is too large
Load Diff
+40
-30
@@ -1,10 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2004-2006, Haiku, Inc.
|
* Copyright 2004-2009, Haiku, Inc. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* This software is part of the Haiku distribution and is covered
|
* Authors:
|
||||||
* by the Haiku license.
|
* Jérôme Duval
|
||||||
*
|
* Axel Dörfler, [email protected].
|
||||||
* Author: Jérôme Duval
|
|
||||||
*/
|
*/
|
||||||
#ifndef KEYMAP_H
|
#ifndef KEYMAP_H
|
||||||
#define KEYMAP_H
|
#define KEYMAP_H
|
||||||
@@ -18,34 +18,44 @@
|
|||||||
|
|
||||||
|
|
||||||
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(const char* name);
|
||||||
status_t Save(entry_ref &ref);
|
status_t Load(FILE* file);
|
||||||
status_t LoadSource(FILE *f);
|
status_t LoadSource(const char* name);
|
||||||
status_t LoadSourceFromRef(entry_ref &ref);
|
status_t LoadSource(FILE* file);
|
||||||
void SaveAsCurrent();
|
status_t SaveAsCurrent();
|
||||||
void SaveAsHeader(entry_ref &ref, const char *mapname);
|
status_t Save(const char* name);
|
||||||
status_t Use();
|
status_t SaveAsSource(const char* name);
|
||||||
void Dump();
|
status_t SaveAsSource(FILE* file);
|
||||||
bool IsModifierKey(uint32 keyCode);
|
status_t SaveAsCppHeader(const char* name,
|
||||||
uint8 IsDeadKey(uint32 keyCode, uint32 modifiers);
|
const char* mapName);
|
||||||
bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey);
|
|
||||||
void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
|
|
||||||
char** chars, int32* numBytes);
|
|
||||||
void RestoreSystemDefault();
|
|
||||||
static void GetKey(char *chars, int32 offset, char* string);
|
|
||||||
|
|
||||||
private:
|
status_t Use();
|
||||||
void ComputeChars(const char *buffer, struct re_registers ®s, int i, int &offset);
|
|
||||||
void ComputeTables(const char *buffer, struct re_registers ®s, uint32 &table);
|
|
||||||
|
|
||||||
char *fChars;
|
bool IsModifierKey(uint32 keyCode);
|
||||||
key_map fKeys;
|
uint8 IsDeadKey(uint32 keyCode, uint32 modifiers);
|
||||||
uint32 fCharsSize;
|
bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers,
|
||||||
|
uint8 activeDeadKey);
|
||||||
|
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 _SaveSourceText(FILE* file);
|
||||||
|
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)
|
#define KEYMAP_ERROR_UNKNOWN_VERSION (B_ERRORS_END + 1)
|
||||||
|
|||||||
+178
-91
@@ -1,13 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2004-2006, Haiku, Inc.
|
* Copyright 2004-2009, Haiku, Inc. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* This software is part of the Haiku distribution and is covered
|
* Authors:
|
||||||
* by the MIT license.
|
* Jérôme Duval
|
||||||
*
|
* Axel Dörfler, [email protected].
|
||||||
* Author: Jérôme Duval
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <getopt.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -24,21 +25,27 @@ static const char *sProgramName = __progname;
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage()
|
||||||
{
|
{
|
||||||
printf("usage: %s {-o output_file} -[d|l|r|c|b|h input_file]\n"
|
printf("usage: %s {-o <output-file>} [-[l|r] | -[b|h|c|d <input-file>]]\n"
|
||||||
" -o change output file to output_file (default:keymap.out)\n"
|
" -o, --output Change output file to output-file (default: "
|
||||||
" -d dump key map to standard output\n"
|
"keymap.out|h).\n"
|
||||||
" -l load key map from standard input\n"
|
" -d, --dump Decompile key map to standard output (can be "
|
||||||
" -b load binary key map from file\n"
|
"redirected\n"
|
||||||
" -r restore system default key map\n"
|
" via -o).\n"
|
||||||
" -c compile source keymap to binary\n"
|
" -l, --load Load key map. If no input-file is specified, it "
|
||||||
" -h compile source keymap to header\n",
|
"will be\n"
|
||||||
|
" read from standard input.\n"
|
||||||
|
" -s, --load-source Load source key map from standard input when no\n"
|
||||||
|
" input-file is specified.\n"
|
||||||
|
" -r, --restore Restore system default key map.\n"
|
||||||
|
" -c, --compile Compile source keymap to binary.\n"
|
||||||
|
" -h, --header Translate source keymap to C++ header.\n",
|
||||||
sProgramName);
|
sProgramName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *
|
static const char*
|
||||||
keymap_error(status_t status)
|
keymap_error(status_t status)
|
||||||
{
|
{
|
||||||
if (status == KEYMAP_ERROR_UNKNOWN_VERSION)
|
if (status == KEYMAP_ERROR_UNKNOWN_VERSION)
|
||||||
@@ -49,15 +56,24 @@ keymap_error(status_t status)
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
load_keymap_source(Keymap& keymap, const char* name)
|
load_keymap(Keymap& keymap, const char* name, bool source)
|
||||||
{
|
{
|
||||||
entry_ref ref;
|
status_t status;
|
||||||
get_ref_for_path(name, &ref);
|
if (source) {
|
||||||
|
if (name != NULL)
|
||||||
|
status = keymap.LoadSource(name);
|
||||||
|
else
|
||||||
|
status = keymap.LoadSource(stdin);
|
||||||
|
} else {
|
||||||
|
if (name != NULL)
|
||||||
|
status = keymap.Load(name);
|
||||||
|
else
|
||||||
|
status = keymap.Load(stdin);
|
||||||
|
}
|
||||||
|
|
||||||
status_t status = keymap.LoadSourceFromRef(ref);
|
|
||||||
if (status != B_OK) {
|
if (status != B_OK) {
|
||||||
fprintf(stderr, "%s: error when loading the keymap: %s\n",
|
fprintf(stderr, "%s: error when loading the keymap: %s\n", sProgramName,
|
||||||
sProgramName, keymap_error(status));
|
keymap_error(status));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,81 +82,152 @@ load_keymap_source(Keymap& keymap, const char* name)
|
|||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char operation = ' ';
|
const char* output = NULL;
|
||||||
entry_ref outputRef;
|
const char* input = NULL;
|
||||||
get_ref_for_path("keymap.out", &outputRef);
|
enum {
|
||||||
|
kUnspecified,
|
||||||
|
kLoadBinary,
|
||||||
|
kLoadText,
|
||||||
|
kSaveText,
|
||||||
|
kRestore,
|
||||||
|
kCompile,
|
||||||
|
kSaveHeader,
|
||||||
|
} mode = kUnspecified;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
static struct option const kLongOptions[] = {
|
||||||
if (strncmp(argv[i], "-", 1) == 0) {
|
{"output", required_argument, 0, 'o'},
|
||||||
if (strlen(argv[i]) > 1)
|
{"dump", optional_argument, 0, 'd'},
|
||||||
operation = argv[i][1];
|
{"load", optional_argument, 0, 'l'},
|
||||||
else
|
{"load-source", optional_argument, 0, 's'},
|
||||||
|
{"restore", no_argument, 0, 'r'},
|
||||||
|
{"compile", optional_argument, 0, 'c'},
|
||||||
|
{"header", optional_argument, 0, 'h'},
|
||||||
|
{"help", no_argument, 0, 'H'},
|
||||||
|
{NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int c;
|
||||||
|
while ((c = getopt_long(argc, argv, "o:dblsrchH", kLongOptions,
|
||||||
|
NULL)) != -1) {
|
||||||
|
switch (c) {
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
case 'o':
|
||||||
|
output = optarg;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
mode = kSaveText;
|
||||||
|
input = optarg;
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
case 'b':
|
||||||
|
mode = kLoadBinary;
|
||||||
|
input = optarg;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
mode = kLoadText;
|
||||||
|
input = optarg;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
mode = kRestore;
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
mode = kCompile;
|
||||||
|
input = optarg;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
mode = kSaveHeader;
|
||||||
|
input = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (operation == 'd') {
|
case 'H':
|
||||||
Keymap keymap;
|
default:
|
||||||
if (keymap.LoadCurrent() != B_OK) {
|
usage();
|
||||||
fprintf(stderr, "%s: error while getting current keymap!\n", sProgramName);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
keymap.Dump();
|
|
||||||
return 0;
|
|
||||||
} else if (operation == 'r') {
|
|
||||||
Keymap keymap;
|
|
||||||
keymap.RestoreSystemDefault();
|
|
||||||
printf("System default key map restored.\n");
|
|
||||||
return 0;
|
|
||||||
} else if (operation == 'l') {
|
|
||||||
Keymap keymap;
|
|
||||||
status_t status = keymap.LoadSource(stdin);
|
|
||||||
if (status != B_OK) {
|
|
||||||
fprintf(stderr, "%s: error when loading the keymap: %s\n",
|
|
||||||
sProgramName, keymap_error(status));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
keymap.SaveAsCurrent();
|
|
||||||
printf("Key map loaded.\n");
|
|
||||||
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') {
|
|
||||||
Keymap keymap;
|
|
||||||
load_keymap_source(keymap, argv[i]);
|
|
||||||
|
|
||||||
status_t status = keymap.Save(outputRef);
|
|
||||||
if (status < B_OK) {
|
|
||||||
fprintf(stderr, "%s: error saving \"%s\": %s\n", sProgramName,
|
|
||||||
argv[i], strerror(status));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
} else if (operation == 'h') {
|
|
||||||
Keymap keymap;
|
|
||||||
load_keymap_source(keymap, argv[i]);
|
|
||||||
keymap.SaveAsHeader(outputRef, argv[i]);
|
|
||||||
return 0;
|
|
||||||
} else if (operation == 'b') {
|
|
||||||
entry_ref ref;
|
|
||||||
get_ref_for_path(argv[i], &ref);
|
|
||||||
Keymap keymap;
|
|
||||||
status_t status = keymap.Load(ref);
|
|
||||||
if (status != B_OK) {
|
|
||||||
fprintf(stderr, "%s: error when loading the keymap: %s\n",
|
|
||||||
sProgramName, keymap_error(status));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
keymap.SaveAsCurrent();
|
|
||||||
printf("Key map loaded.\n");
|
|
||||||
return 0;
|
|
||||||
} else
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
usage();
|
if (argc > optind && input == NULL)
|
||||||
return 1;
|
input = argv[optind];
|
||||||
|
|
||||||
|
Keymap keymap;
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case kUnspecified:
|
||||||
|
usage();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case kLoadBinary:
|
||||||
|
case kLoadText:
|
||||||
|
{
|
||||||
|
load_keymap(keymap, input, mode == kLoadText);
|
||||||
|
|
||||||
|
status_t status = keymap.SaveAsCurrent();
|
||||||
|
if (status != B_OK) {
|
||||||
|
fprintf(stderr, "%s: error when saving as current: %s",
|
||||||
|
sProgramName, strerror(status));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Key map loaded.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case kSaveText:
|
||||||
|
{
|
||||||
|
if (input == NULL) {
|
||||||
|
status_t status = keymap.LoadCurrent();
|
||||||
|
if (status != B_OK) {
|
||||||
|
fprintf(stderr, "%s: error while getting keymap: %s!\n",
|
||||||
|
sProgramName, keymap_error(status));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
load_keymap(keymap, input, false);
|
||||||
|
|
||||||
|
if (output != NULL)
|
||||||
|
keymap.SaveAsSource(output);
|
||||||
|
else
|
||||||
|
keymap.SaveAsSource(stdout);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case kRestore:
|
||||||
|
keymap.RestoreSystemDefault();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case kCompile:
|
||||||
|
{
|
||||||
|
load_keymap(keymap, input, true);
|
||||||
|
|
||||||
|
if (output == NULL)
|
||||||
|
output = "keymap.out";
|
||||||
|
|
||||||
|
status_t status = keymap.Save(output);
|
||||||
|
if (status != B_OK) {
|
||||||
|
fprintf(stderr, "%s: error saving \"%s\": %s\n", sProgramName,
|
||||||
|
output, strerror(status));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case kSaveHeader:
|
||||||
|
{
|
||||||
|
load_keymap(keymap, input, true);
|
||||||
|
|
||||||
|
if (output == NULL)
|
||||||
|
output = "keymap.h";
|
||||||
|
|
||||||
|
status_t status = keymap.SaveAsCppHeader(output, input);
|
||||||
|
if (status != B_OK) {
|
||||||
|
fprintf(stderr, "%s: error saving \"%s\": %s\n", sProgramName,
|
||||||
|
output, strerror(status));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user