1 /* 2 * Copyright 2004-2010, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Jérôme Duval 7 */ 8 9 10 #include "Keymap.h" 11 12 #include <ByteOrder.h> 13 #include <File.h> 14 #include <InputServerTypes.h> 15 #include <Message.h> 16 #include <input_globals.h> 17 18 #include <errno.h> 19 #include <new> 20 #include <stdlib.h> 21 #include <stdio.h> 22 #include <string.h> 23 24 25 static void 26 print_key(char* chars, int32 offset) 27 { 28 int size = chars[offset++]; 29 30 switch (size) { 31 case 0: 32 // Not mapped 33 fputs("N/A", stdout); 34 break; 35 36 case 1: 37 // single-byte UTF-8/ASCII character 38 fputc(chars[offset], stdout); 39 break; 40 41 default: 42 { 43 // 2-, 3-, or 4-byte UTF-8 character 44 char* str = new (std::nothrow) char[size + 1]; 45 if (str == NULL) 46 break; 47 48 strncpy(str, &(chars[offset]), size); 49 str[size] = 0; 50 fputs(str, stdout); 51 delete [] str; 52 break; 53 } 54 } 55 56 fputs("\t", stdout); 57 } 58 59 60 // #pragma mark - 61 62 63 Keymap::Keymap() 64 { 65 RetrieveCurrent(); 66 } 67 68 69 Keymap::~Keymap() 70 { 71 } 72 73 74 void 75 Keymap::DumpKeymap() 76 { 77 if (fKeys.version != 3) 78 return; 79 80 // Print a chart of the normal, shift, control, option, option+shift, 81 // Caps, Caps+shift, Caps+option, and Caps+option+shift keys. 82 puts("Key #\tn\ts\tc\to\tos\tC\tCs\tCo\tCos\n"); 83 84 for (uint8 i = 0; i < 128; i++) { 85 printf(" 0x%02x\t", i); 86 print_key(fChars, fKeys.normal_map[i]); 87 print_key(fChars, fKeys.shift_map[i]); 88 print_key(fChars, fKeys.control_map[i]); 89 print_key(fChars, fKeys.option_map[i]); 90 print_key(fChars, fKeys.option_shift_map[i]); 91 print_key(fChars, fKeys.caps_map[i]); 92 print_key(fChars, fKeys.caps_shift_map[i]); 93 print_key(fChars, fKeys.option_caps_map[i]); 94 print_key(fChars, fKeys.option_caps_shift_map[i]); 95 fputs("\n", stdout); 96 } 97 } 98 99 100 status_t 101 Keymap::RetrieveCurrent() 102 { 103 Unset(); 104 105 key_map* keys; 106 _get_key_map(&keys, &fChars, (ssize_t*)&fCharsSize); 107 if (!keys) { 108 fprintf(stderr, "error while getting current keymap!\n"); 109 return B_ERROR; 110 } 111 112 memcpy(&fKeys, keys, sizeof(fKeys)); 113 free(keys); 114 return B_OK; 115 } 116 117