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 | int getchar_replacement(void);␊ |
30 | ␊ |
31 | int getchar_replacement(void) {␊ |
32 | ␉int code = bgetc();␊ |
33 | ␉int status = readKeyboardShiftFlags();␊ |
34 | ␉uint8_t scancode = code >> 8;␊ |
35 | ␊ |
36 | ␉// Special scancode sent when alt + some keys are pressed␊ |
37 | ␉if (scancode >= 0x78 && scancode <= 0x83)␊ |
38 | ␉␉scancode -= 0x76;␊ |
39 | ␊ |
40 | ␉if (scancode < KEYBOARD_MAP_SIZE && !(status & (STATUS_LCTRL| STATUS_RCTRL))) {␊ |
41 | ␉␉int key;␊ |
42 | ␉␉if ((status & (STATUS_LALT|STATUS_RALT)) &&␊ |
43 | ␉␉␉(status & (STATUS_LSHIFT|STATUS_RSHIFT|STATUS_CAPS))) {␊ |
44 | ␉␉␉key=current_layout->keyboard_map_shift_alt[scancode];␊ |
45 | ␉␉}␊ |
46 | ␉␉else if (status & (STATUS_LSHIFT|STATUS_RSHIFT|STATUS_CAPS))␊ |
47 | ␉␉␉key=current_layout->keyboard_map_shift[scancode];␊ |
48 | ␉␉else if (status & (STATUS_LALT|STATUS_RALT))␊ |
49 | ␉␉␉key=current_layout->keyboard_map_alt[scancode];␊ |
50 | ␉␉else␊ |
51 | ␉␉␉key=current_layout->keyboard_map[scancode];␊ |
52 | ␊ |
53 | ␉␉if (key != 0) // do the mapping␊ |
54 | ␉␉␉code = key;␊ |
55 | ␉}␊ |
56 | ␊ |
57 | ␉if (ASCII_KEY(code) != 0) // if ascii not null return it␊ |
58 | ␉␉code = ASCII_KEY(code);␊ |
59 | ␊ |
60 | ␉//printf("Code: %04x\n",code);␊ |
61 | ␉return (code);␊ |
62 | }␊ |
63 | ␊ |
64 | static uint32_t load_keyboard_layout_file(const char *filename) {␊ |
65 | ␉int fd;␊ |
66 | ␉char magic[KEYBOARD_LAYOUTS_MAGIC_SIZE];␊ |
67 | ␉uint32_t version;␊ |
68 | ␉␊ |
69 | #if UNUSED␊ |
70 | ␉if ((fd = open_bvdev("bt(0,0)", filename, 0)) < 0) {␊ |
71 | #else␊ |
72 | ␉if ((fd = open_bvdev("bt(0,0)", filename)) < 0) {␉␉␊ |
73 | #endif␊ |
74 | ␉␉goto fail; // fail␊ |
75 | ␉}␊ |
76 | ␉␊ |
77 | ␉if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {␊ |
78 | ␉␉printf("Can't find magic in keyboard layout file: %s\n", filename);␊ |
79 | ␉␉goto fail;␊ |
80 | ␉}␊ |
81 | ␊ |
82 | ␉if (memcmp (magic, KEYBOARD_LAYOUTS_MAGIC, KEYBOARD_LAYOUTS_MAGIC_SIZE) != 0) {␊ |
83 | ␉␉printf("Invalid magic code in keyboard layout file: %s\n", filename);␊ |
84 | ␉␉goto fail;␊ |
85 | }␊ |
86 | ␊ |
87 | ␉if (read(fd, (char*) &version, sizeof(version)) != sizeof(version)) {␊ |
88 | ␉␉printf("Can't get version of keyboard layout file: %s\n", filename);␊ |
89 | ␉␉goto fail;␊ |
90 | ␉}␊ |
91 | ␉␊ |
92 | ␉if (version != KEYBOARD_LAYOUTS_VERSION) {␊ |
93 | ␉␉verbose("Bad version for keyboard layout file %s expected v%d found v%d\n",␊ |
94 | ␉␉␉ filename, KEYBOARD_LAYOUTS_VERSION, version);␊ |
95 | ␉␉goto fail;␊ |
96 | ␉}␊ |
97 | ␉␊ |
98 | ␉if (current_layout)␊ |
99 | ␉␉free(current_layout);␊ |
100 | ␉␉␊ |
101 | ␉current_layout = malloc(sizeof(*current_layout));␊ |
102 | ␉if (!current_layout)␊ |
103 | ␉␉goto fail;␊ |
104 | ␊ |
105 | ␉b_lseek(fd, KEYBOARD_LAYOUTS_MAP_OFFSET, 0);␊ |
106 | ␉␊ |
107 | ␉if (read(fd, (char*) current_layout, sizeof(*current_layout)) != sizeof(*current_layout)) {␊ |
108 | ␉␉printf("Wrong keyboard layout file %s size\n", filename);␊ |
109 | ␉␉goto fail;␊ |
110 | ␉}␊ |
111 | ␉␊ |
112 | ␉close(fd);␊ |
113 | ␉␊ |
114 | ␉return 1;␊ |
115 | ␉␉␊ |
116 | fail:␊ |
117 | ␊ |
118 | ␉if (current_layout) {␊ |
119 | ␉␉free(current_layout);␊ |
120 | ␉␉current_layout = NULL;␊ |
121 | ␉}␊ |
122 | ␉return 0;␊ |
123 | }␊ |
124 | ␊ |
125 | uint32_t Keylayout_real_start(void)␊ |
126 | {␊ |
127 | ␉char layoutPath[512];␊ |
128 | ␉const char␉*val;␊ |
129 | ␉int␉␉␉len;␊ |
130 | ␉␊ |
131 | ␉if (getValueForKey("KeyLayout", &val, &len, DEFAULT_BOOT_CONFIG))␊ |
132 | ␉{␊ |
133 | ␉␉sprintf(layoutPath, "/Extra/Keymaps/%s", val);␊ |
134 | ␉␉// Add the extension if needed␊ |
135 | ␉␉if (len <= 4 || strcmp(val+len-4,".lyt") != 0)␊ |
136 | ␉␉␉strncat(layoutPath, ".lyt", sizeof(layoutPath));␊ |
137 | ␊ |
138 | ␉␉if (!load_keyboard_layout_file(layoutPath))␊ |
139 | ␉␉{␊ |
140 | ␉␉␉DBG("Can't load %s keyboard layout file. Keylayout will not be used !\n",␊ |
141 | ␉␉␉␉ layoutPath);␊ |
142 | ␉␉␉return 0;␊ |
143 | ␉␉} ␊ |
144 | ␉␉␊ |
145 | ␊ |
146 | ␉␉if (replace_system_function("_getc", &getchar_replacement) != EFI_SUCCESS ) ␊ |
147 | ␉␉{␊ |
148 | ␉␉␉printf("no function getc() to replace. Keylayout will not be used ! \n");␊ |
149 | ␉␉␉return 0;␊ |
150 | ␉␉}␊ |
151 | ␉␉␊ |
152 | ␉␉return 1;␊ |
153 | ␉␉␊ |
154 | ␉}␊ |
155 | ␉return 0;␊ |
156 | }␊ |
157 | ␊ |
158 | void Keylayout_start(void);␊ |
159 | void Keylayout_start(void)␊ |
160 | {␊ |
161 | ␉Keylayout_real_start();␊ |
162 | } |