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",
107filename, KEYBOARD_LAYOUTS_VERSION, version);
108goto fail;
109}
110
111if (current_layout)
112free(current_layout);
113
114current_layout = malloc(sizeof(struct keyboard_layout));
115if (!current_layout)
116goto fail;
117bzero(current_layout,sizeof(struct keyboard_layout));
118
119key_b_lseek(fd, KEYBOARD_LAYOUTS_MAP_OFFSET, 0);
120
121if (read(fd, (char*) current_layout, sizeof(struct keyboard_layout)) != sizeof(struct keyboard_layout)) {
122printf("Wrong keyboard layout file %s size\n", filename);
123goto fail;
124}
125
126close(fd);
127
128return 1;
129
130fail:
131
132if (current_layout) {
133free(current_layout);
134current_layout = NULL;
135}
136return 0;
137}
138
139uint32_t Keylayout_real_start(void)
140{
141char layoutPath[512];
142const char*val;
143intlen;
144
145if (getValueForKey("KeyLayout", &val, &len, DEFAULT_BOOT_CONFIG))
146{
147snprintf(layoutPath, sizeof(layoutPath),"/Extra/Keymaps/%s", val);
148// Add the extension if needed
149if (len <= 4 || strncmp(val+len-4,".lyt", sizeof(".lyt")) != 0)
150strlcat(layoutPath, ".lyt", sizeof(layoutPath));
151
152if (!load_keyboard_layout_file(layoutPath))
153{
154DBG("Can't load %s keyboard layout file. Keylayout will not be used !\n",
155layoutPath);
156return 0;
157}
158
159
160if (replace_system_function("_getc", &getchar_replacement) != EFI_SUCCESS )
161{
162printf("no function getc() to replace. Keylayout will not be used ! \n");
163return 0;
164}
165
166return 1;
167
168}
169return 0;
170}
171
172void Keylayout_start(void);
173void Keylayout_start(void)
174{
175Keylayout_real_start();
176}

Archive Download this file

Revision: 2182