xref: /haiku/src/add-ons/input_server/devices/keyboard/Keymap.cpp (revision 37c7d5d83a2372a6971e383411d5bacbeef0ebdc)
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 			printf("N/A");
34 			break;
35 
36 		case 1:
37 			// 1-byte UTF-8/ASCII character
38 			printf("%c", chars[offset]);
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 			printf("%s", str);
51 			delete [] str;
52 			break;
53 		}
54 	}
55 
56 	printf("\t");
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 	// Print a chart of the normal, shift, option, and option+shift
78 	// keys.
79 	printf("Key #\tNormal\tShift\tCaps\tC+S\tOption\tO+S\tO+C\tO+C+S\tControl\n");
80 
81 	for (int i = 0; i < 128; i++) {
82 		printf(" 0x%x\t", i);
83 		print_key(fChars, fKeys.normal_map[i]);
84 		print_key(fChars, fKeys.shift_map[i]);
85 		print_key(fChars, fKeys.caps_map[i]);
86 		print_key(fChars, fKeys.caps_shift_map[i]);
87 		print_key(fChars, fKeys.option_map[i]);
88 		print_key(fChars, fKeys.option_shift_map[i]);
89 		print_key(fChars, fKeys.option_caps_map[i]);
90 		print_key(fChars, fKeys.option_caps_shift_map[i]);
91 		print_key(fChars, fKeys.control_map[i]);
92 		printf("\n");
93 	}
94 }
95 
96 
97 status_t
98 Keymap::RetrieveCurrent()
99 {
100 	Unset();
101 
102 	key_map* keys;
103 	_get_key_map(&keys, &fChars, (ssize_t*)&fCharsSize);
104 	if (!keys) {
105 		fprintf(stderr, "error while getting current keymap!\n");
106 		return B_ERROR;
107 	}
108 
109 	memcpy(&fKeys, keys, sizeof(fKeys));
110 	free(keys);
111 	return B_OK;
112 }
113 
114