Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Keymapper/Keylayout.c

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

Archive Download this file

Revision: 1804