use the -newos toolchain even if -elf is present.
[newos.git] / apps / inputd / KeyboardDevice.cpp
blobaa98b9e1bbb12e468f1d42171a2c9d13c1b75152
1 /*
2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <win/Event.h>
8 using namespace os::gui;
10 #include "InputServer.h"
11 #include "KeyboardDevice.h"
13 #include <newos/key_event.h>
15 namespace {
16 const char unshifted_keymap[] = {
17 0, 0, 0, 0, 0, 0, 0, 0,
18 0, 0, 0, 0, 0, 0, 0, 0,
19 0, 0, 0, 0, 0, 0, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0,
21 ' ', '!', '"', '#', '$', '%', '&', '\'',
22 '(', ')', '*', '+', ',', '-', '.', '/',
23 '0', '1', '2', '3', '4', '5', '6', '7',
24 '8', '9', ':', ';', '<', '=', '>', '?',
25 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
26 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
27 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
28 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
29 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
30 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
31 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
32 'x', 'y', 'z', '{', '|', '}', '~', 0
35 const char caps_keymap[] = {
36 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0,
40 ' ', '!', '"', '#', '$', '%', '&', '\'',
41 '(', ')', '*', '+', ',', '-', '.', '/',
42 '0', '1', '2', '3', '4', '5', '6', '7',
43 '8', '9', ':', ';', '<', '=', '>', '?',
44 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
45 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
46 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
47 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
48 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
49 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
50 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
51 'X', 'Y', 'Z', '{', '|', '}', '~', 0
54 const char shifted_keymap[] = {
55 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0,
59 ' ', '!', '"', '#', '$', '%', '&', '"',
60 '(', ')', '*', '+', '<', '_', '>', '?',
61 ')', '!', '@', '#', '$', '%', '^', '&',
62 '*', '(', ':', ':', '<', '+', '>', '?',
63 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
64 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
65 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
66 'X', 'Y', 'Z', '{', '|', '}', '^', '_',
67 '~', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
68 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
69 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
70 'X', 'Y', 'Z', '{', '|', '}', '~', 0
73 const char shifted_caps_keymap[] = {
74 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0,
78 ' ', '!', '"', '#', '$', '%', '&', '"',
79 '(', ')', '*', '+', '<', '_', '>', '?',
80 ')', '!', '@', '#', '$', '%', '^', '&',
81 '*', '(', ':', ':', '<', '+', '>', '?',
82 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
83 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
84 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
85 'X', 'Y', 'Z', '{', '|', '}', '^', '_',
86 '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
87 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
88 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
89 'x', 'y', 'z', '{', '|', '}', '~', 0
93 int process_key_events(_key_event &kevent, Event &event)
95 static bool shift = false;
96 static bool caps = false;
97 static bool ctrl = false;
98 static bool alt = false;
99 static bool win = false;
100 static bool menu = false;
101 char c;
103 // deal with the common case first
104 if(kevent.keycode < KEY_NONE) {
105 const char *keymap;
107 if(shift) {
108 if(caps)
109 keymap = shifted_caps_keymap;
110 else
111 keymap = shifted_keymap;
112 } else {
113 if(caps)
114 keymap = caps_keymap;
115 else
116 keymap = unshifted_keymap;
119 c = keymap[kevent.keycode];
120 if(c == 0)
121 return -1;
123 event.key = c;
124 } else {
125 /* extended keys */
126 event.key = kevent.keycode; /* let the target figure this out */
127 switch(kevent.keycode) {
128 case KEY_CAPSLOCK:
129 caps = !caps;
130 break;
131 case KEY_LSHIFT:
132 case KEY_RSHIFT:
133 if(kevent.modifiers & KEY_MODIFIER_DOWN)
134 shift = true;
135 else
136 shift = false;
137 break;
138 case KEY_LCTRL:
139 case KEY_RCTRL:
140 if(kevent.modifiers & KEY_MODIFIER_DOWN)
141 ctrl = true;
142 else
143 ctrl = false;
144 break;
145 case KEY_LALT:
146 case KEY_RALT:
147 if(kevent.modifiers & KEY_MODIFIER_DOWN)
148 alt = true;
149 else
150 alt = false;
151 break;
152 case KEY_LWIN:
153 case KEY_RWIN:
154 if(kevent.modifiers & KEY_MODIFIER_DOWN)
155 win = true;
156 else
157 win = false;
158 break;
159 case KEY_MENU:
160 if(kevent.modifiers & KEY_MODIFIER_DOWN)
161 menu = true;
162 else
163 menu = false;
164 break;
168 event.what = (kevent.modifiers & KEY_MODIFIER_DOWN) ? EVT_KEY_DOWN : EVT_KEY_UP;
169 event.modifiers = (kevent.modifiers & KEY_MODIFIER_DOWN) ? EVT_MOD_KEYDOWN : EVT_MOD_KEYUP;
170 event.modifiers |= shift ? EVT_MOD_SHIFT : 0;
171 event.modifiers |= ctrl ? EVT_MOD_CTRL : 0;
172 event.modifiers |= alt ? EVT_MOD_ALT : 0;
173 event.modifiers |= win ? EVT_MOD_WIN : 0;
174 event.modifiers |= menu ? EVT_MOD_MENU : 0;
175 return 0;
178 KeyboardDevice::KeyboardDevice(int fd, InputServer *srv)
179 : InputDevice(fd, srv),
180 mFd(fd),
181 mSrv(srv)
185 KeyboardDevice::~KeyboardDevice()
189 int KeyboardDevice::_Run()
191 for(;;) {
192 _key_event kevent;
193 ssize_t len;
194 Event event;
196 len = read(mFd, &kevent, sizeof(kevent));
197 if(len < 0)
198 break;
200 if(process_key_events(kevent, event) < 0)
201 continue;
203 mSrv->PostEvent(event);
206 return 0;