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

Archive Download this file

Revision: 1931