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

Archive Download this file

Revision: 2112