1 | /*␊ |
2 | * Keymapper.c␊ |
3 | * Chameleon␊ |
4 | *␊ |
5 | * Created by JrCs on 28/08/11.␊ |
6 | * Copyright 2011. All rights reserved.␊ |
7 | *␊ |
8 | */␊ |
9 | ␊ |
10 | #include "libsaio.h"␊ |
11 | #include "term.h"␊ |
12 | #include "modules.h"␊ |
13 | #include "Keylayout.h"␊ |
14 | #include "bootstruct.h"␊ |
15 | ␊ |
16 | #ifndef DEBUG_KLAYOUT␊ |
17 | #define DEBUG_KLAYOUT 0␊ |
18 | #endif␊ |
19 | ␊ |
20 | #if DEBUG_KLAYOUT␊ |
21 | #define DBG(x...)␉printf(x)␊ |
22 | #else␊ |
23 | #define DBG(x...)␊ |
24 | #endif␊ |
25 | ␊ |
26 | #define kKeyboardLayout "KeyboardLayout"␊ |
27 | ␊ |
28 | struct keyboard_layout *current_layout = NULL;␊ |
29 | ␊ |
30 | int getchar_replacement() {␊ |
31 | ␉int code = bgetc();␊ |
32 | ␉int status = readKeyboardShiftFlags();␊ |
33 | ␉uint8_t scancode = code >> 8;␊ |
34 | ␊ |
35 | ␉// Special scancode sent when alt + some keys are pressed␊ |
36 | ␉if (scancode >= 0x78 && scancode <= 0x83)␊ |
37 | ␉␉scancode -= 0x76;␊ |
38 | ␊ |
39 | ␉if (scancode < KEYBOARD_MAP_SIZE && !(status & (STATUS_LCTRL| STATUS_RCTRL))) {␊ |
40 | ␉␉int key;␊ |
41 | ␉␉if ((status & (STATUS_LALT|STATUS_RALT)) &&␊ |
42 | ␉␉␉(status & (STATUS_LSHIFT|STATUS_RSHIFT|STATUS_CAPS))) {␊ |
43 | ␉␉␉key=current_layout->keyboard_map_shift_alt[scancode];␊ |
44 | ␉␉}␊ |
45 | ␉␉else if (status & (STATUS_LSHIFT|STATUS_RSHIFT|STATUS_CAPS))␊ |
46 | ␉␉␉key=current_layout->keyboard_map_shift[scancode];␊ |
47 | ␉␉else if (status & (STATUS_LALT|STATUS_RALT))␊ |
48 | ␉␉␉key=current_layout->keyboard_map_alt[scancode];␊ |
49 | ␉␉else␊ |
50 | ␉␉␉key=current_layout->keyboard_map[scancode];␊ |
51 | ␊ |
52 | ␉␉if (key != 0) // do the mapping␊ |
53 | ␉␉␉code = key;␊ |
54 | ␉}␊ |
55 | ␊ |
56 | ␉if (ASCII_KEY(code) != 0) // if ascii not null return it␊ |
57 | ␉␉code = ASCII_KEY(code);␊ |
58 | ␊ |
59 | ␉//printf("Code: %04x\n",code);␊ |
60 | ␉return (code);␊ |
61 | }␊ |
62 | ␊ |
63 | static uint32_t load_keyboard_layout_file(const char *filename) {␊ |
64 | ␉int fd;␊ |
65 | ␉char magic[KEYBOARD_LAYOUTS_MAGIC_SIZE];␊ |
66 | ␉uint32_t version;␊ |
67 | ␉␊ |
68 | #if UNUSED␊ |
69 | ␉if ((fd = open_bvdev("bt(0,0)", filename, 0)) < 0) {␊ |
70 | #else␊ |
71 | ␉if ((fd = open_bvdev("bt(0,0)", filename)) < 0) {␉␉␊ |
72 | #endif␊ |
73 | ␉␉goto fail; // fail␊ |
74 | ␉}␊ |
75 | ␉␊ |
76 | ␉if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {␊ |
77 | ␉␉printf("Can't find magic in keyboard layout file: %s\n", filename);␊ |
78 | ␉␉goto fail;␊ |
79 | ␉}␊ |
80 | ␊ |
81 | ␉if (memcmp (magic, KEYBOARD_LAYOUTS_MAGIC, KEYBOARD_LAYOUTS_MAGIC_SIZE) != 0) {␊ |
82 | ␉␉printf("Invalid magic code in keyboard layout file: %s\n", filename);␊ |
83 | ␉␉goto fail;␊ |
84 | }␊ |
85 | ␊ |
86 | ␉if (read(fd, (char*) &version, sizeof(version)) != sizeof(version)) {␊ |
87 | ␉␉printf("Can't get version of keyboard layout file: %s\n", filename);␊ |
88 | ␉␉goto fail;␊ |
89 | ␉}␊ |
90 | ␉␊ |
91 | ␉if (version != KEYBOARD_LAYOUTS_VERSION) {␊ |
92 | ␉␉verbose("Bad version for keyboard layout file %s expected v%d found v%d\n",␊ |
93 | ␉␉␉ filename, KEYBOARD_LAYOUTS_VERSION, version);␊ |
94 | ␉␉goto fail;␊ |
95 | ␉}␊ |
96 | ␉␊ |
97 | ␉if (current_layout)␊ |
98 | ␉␉free(current_layout);␊ |
99 | ␉␉␊ |
100 | ␉current_layout = malloc(sizeof(*current_layout));␊ |
101 | ␉if (!current_layout)␊ |
102 | ␉␉goto fail;␊ |
103 | ␊ |
104 | ␉b_lseek(fd, KEYBOARD_LAYOUTS_MAP_OFFSET, 0);␊ |
105 | ␉␊ |
106 | ␉if (read(fd, (char*) current_layout, sizeof(*current_layout)) != sizeof(*current_layout)) {␊ |
107 | ␉␉printf("Wrong keyboard layout file %s size\n", filename);␊ |
108 | ␉␉goto fail;␊ |
109 | ␉}␊ |
110 | ␉␊ |
111 | ␉close(fd);␊ |
112 | ␉␊ |
113 | ␉return 1;␊ |
114 | ␉␉␊ |
115 | fail:␊ |
116 | ␊ |
117 | ␉if (current_layout) {␊ |
118 | ␉␉free(current_layout);␊ |
119 | ␉␉current_layout = NULL;␊ |
120 | ␉}␊ |
121 | ␉return 0;␊ |
122 | }␊ |
123 | ␊ |
124 | uint32_t Keylayout_real_start()␊ |
125 | {␊ |
126 | ␉char layoutPath[512];␊ |
127 | ␉const char␉*val;␊ |
128 | ␉int␉␉␉len;␊ |
129 | ␉␊ |
130 | #ifdef TRUNK␊ |
131 | #define Config chameleonConfig␊ |
132 | #else␊ |
133 | #define Config bootConfig␊ |
134 | #endif␊ |
135 | ␊ |
136 | ␉if (getValueForKey("KeyLayout", &val, &len, &bootInfo->Config))␊ |
137 | ␉{␊ |
138 | ␉␉sprintf(layoutPath, "/Extra/Keymaps/%s", val);␊ |
139 | ␉␉// Add the extension if needed␊ |
140 | ␉␉if (len <= 4 || strcmp(val+len-4,".lyt") != 0)␊ |
141 | ␉␉␉strncat(layoutPath, ".lyt", sizeof(layoutPath));␊ |
142 | ␊ |
143 | ␉␉if (!load_keyboard_layout_file(layoutPath))␊ |
144 | ␉␉{␊ |
145 | ␉␉␉DBG("Can't load %s keyboard layout file. Keylayout will not be used !\n",␊ |
146 | ␉␉␉␉ layoutPath);␊ |
147 | ␉␉␉return 0;␊ |
148 | ␉␉} ␊ |
149 | ␉␉␊ |
150 | #ifdef TRUNK␉␊ |
151 | ␉␉if (!replace_function("_getchar", &getchar_replacement)) ␊ |
152 | ␉␉{␊ |
153 | ␉␉␉printf("no function getchar() to replace. Keylayout will not be used ! \n");␊ |
154 | ␉␉␉␊ |
155 | ␉␉␉return 0;␊ |
156 | ␉␉}␉␊ |
157 | ␉␉␉␊ |
158 | #else␊ |
159 | ␉␉if (replace_function("_getc", &getchar_replacement) != EFI_SUCCESS ) ␊ |
160 | ␉␉{␊ |
161 | ␉␉␉printf("no function getc() to replace. Keylayout will not be used ! \n");␊ |
162 | ␉␉␉return 0;␊ |
163 | ␉␉}␊ |
164 | #endif␉␉ ␊ |
165 | ␉␉␊ |
166 | ␉␉return 1;␊ |
167 | ␉␉␊ |
168 | ␉}␊ |
169 | ␉return 0;␊ |
170 | }␊ |
171 | ␊ |
172 | ␊ |
173 | void Keylayout_start()␊ |
174 | {␊ |
175 | ␉Keylayout_real_start();␊ |
176 | } |