/* * Copyright 2004-2008 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Sandor Vroemisse * Jérôme Duval */ #include "Keymap.h" #include #include #include #include #include static void print_key(char *chars, int32 offset) { int size = chars[offset++]; switch(size) { case 0: // Not mapped printf("N/A"); break; case 1: // 1-byte UTF-8/ASCII character printf("%c", chars[offset]); break; default: // 2-, 3-, or 4-byte UTF-8 character { char *str = new char[size + 1]; strncpy(str, &(chars[offset]), size); str[size] = 0; printf("%s", str); delete [] str; } break; } printf("\t"); } void Keymap::DumpKeymap() { // Print a chart of the normal, shift, option, and option+shift // keys. printf("Key #\tNormal\tShift\tCaps\tC+S\tOption\tO+S\tO+C\tO+C+S\tControl\n"); for (int idx = 0; idx < 128; idx++) { printf(" 0x%x\t", idx ); print_key(fChars, fKeys.normal_map[idx]); print_key(fChars, fKeys.shift_map[idx]); print_key(fChars, fKeys.caps_map[idx]); print_key(fChars, fKeys.caps_shift_map[idx]); print_key(fChars, fKeys.option_map[idx]); print_key(fChars, fKeys.option_shift_map[idx]); print_key(fChars, fKeys.option_caps_map[idx]); print_key(fChars, fKeys.option_caps_shift_map[idx]); print_key(fChars, fKeys.control_map[idx]); printf("\n"); } } /* 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) { status_t err; BEntry entry(&ref, true); if ((err = entry.InitCheck()) != B_OK) { fprintf(stderr, "error loading keymap: %s\n", strerror(err)); return err; } BFile file(&entry, B_READ_ONLY); if ((err = file.InitCheck()) != B_OK) { fprintf(stderr, "error loading keymap: %s\n", strerror(err)); return err; } if ((err = file.Read(&fKeys, sizeof(fKeys))) < (ssize_t)sizeof(fKeys)) { fprintf(stderr, "error reading keymap keys: %s\n", strerror(err)); return B_BAD_VALUE; } for (uint32 i=0; i