xref: /haiku/src/add-ons/input_server/devices/keyboard/Keymap.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2004-2012, 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, bool last = false)
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 	if (!last)
57 		fputs("\t", stdout);
58 }
59 
60 
61 //	#pragma mark -
62 
63 
64 Keymap::Keymap()
65 {
66 	RetrieveCurrent();
67 }
68 
69 
70 Keymap::~Keymap()
71 {
72 }
73 
74 
75 void
76 Keymap::DumpKeymap()
77 {
78 	if (fKeys.version != 3)
79 		return;
80 
81 	// Print a chart of the normal, shift, control, option, option+shift,
82 	// Caps, Caps+shift, Caps+option, and Caps+option+shift keys.
83 	puts("Key #\tn\ts\tc\to\tos\tC\tCs\tCo\tCos\n");
84 
85 	for (uint8 i = 0; i < 128; i++) {
86 		printf(" 0x%02x\t", i);
87 		print_key(fChars, fKeys.normal_map[i]);
88 		print_key(fChars, fKeys.shift_map[i]);
89 		print_key(fChars, fKeys.control_map[i]);
90 		print_key(fChars, fKeys.option_map[i]);
91 		print_key(fChars, fKeys.option_shift_map[i]);
92 		print_key(fChars, fKeys.caps_map[i]);
93 		print_key(fChars, fKeys.caps_shift_map[i]);
94 		print_key(fChars, fKeys.option_caps_map[i]);
95 		print_key(fChars, fKeys.option_caps_shift_map[i], true);
96 		fputs("\n", stdout);
97 	}
98 }
99 
100 
101 status_t
102 Keymap::RetrieveCurrent()
103 {
104 	Unset();
105 
106 	key_map* keys;
107 	_get_key_map(&keys, &fChars, (ssize_t*)&fCharsSize);
108 	if (!keys) {
109 		fprintf(stderr, "error while getting current keymap!\n");
110 		return B_ERROR;
111 	}
112 
113 	memcpy(&fKeys, keys, sizeof(fKeys));
114 	free(keys);
115 	return B_OK;
116 }
117 
118