2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
10 #include <newos/tty_priv.h>
11 #include <newos/key_event.h>
15 const char unshifted_keymap
[] = {
16 0, 0, 0, 0, 0, 0, 0, 0,
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 ' ', '!', '"', '#', '$', '%', '&', '\'',
21 '(', ')', '*', '+', ',', '-', '.', '/',
22 '0', '1', '2', '3', '4', '5', '6', '7',
23 '8', '9', ':', ';', '<', '=', '>', '?',
24 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
25 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
26 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
27 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
28 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
29 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
30 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
31 'x', 'y', 'z', '{', '|', '}', '~', 0
34 const char caps_keymap
[] = {
35 0, 0, 0, 0, 0, 0, 0, 0,
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 ' ', '!', '"', '#', '$', '%', '&', '\'',
40 '(', ')', '*', '+', ',', '-', '.', '/',
41 '0', '1', '2', '3', '4', '5', '6', '7',
42 '8', '9', ':', ';', '<', '=', '>', '?',
43 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
44 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
45 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
46 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
47 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
48 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
49 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
50 'X', 'Y', 'Z', '{', '|', '}', '~', 0
53 const char shifted_keymap
[] = {
54 0, 0, 0, 0, 0, 0, 0, 0,
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 ' ', '!', '"', '#', '$', '%', '&', '"',
59 '(', ')', '*', '+', '<', '_', '>', '?',
60 ')', '!', '@', '#', '$', '%', '^', '&',
61 '*', '(', ':', ':', '<', '+', '>', '?',
62 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
63 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
64 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
65 'X', 'Y', 'Z', '{', '|', '}', '^', '_',
66 '~', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
67 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
68 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
69 'X', 'Y', 'Z', '{', '|', '}', '~', 0
72 const char shifted_caps_keymap
[] = {
73 0, 0, 0, 0, 0, 0, 0, 0,
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 ' ', '!', '"', '#', '$', '%', '&', '"',
78 '(', ')', '*', '+', '<', '_', '>', '?',
79 ')', '!', '@', '#', '$', '%', '^', '&',
80 '*', '(', ':', ':', '<', '+', '>', '?',
81 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
82 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
83 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
84 'X', 'Y', 'Z', '{', '|', '}', '^', '_',
85 '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
86 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
87 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
88 'x', 'y', 'z', '{', '|', '}', '~', 0
91 int process_key_events(_key_event
*kevents
, int num_events
, char *out_buf
, int buf_len
, int keyboard_fd
)
94 static bool numlock
= false;
95 static bool shift
= false;
96 static bool caps
= false;
97 static bool ctrl
= false;
98 // dont really need to track these
99 static bool alt
= false;
100 static bool win
= false;
101 static bool menu
= false;
102 static const char *keymap
= unshifted_keymap
; // track the status of shift + caps
107 for(i
=0; i
<num_events
; i
++) {
108 bool reset_keymap
= false;
109 // printf("process_key_events: event %d: keycode 0x%x modifiers 0x%x\n", i, kevents[i].keycode, kevents[i].modifiers);
111 if(kevents
[i
].modifiers
& KEY_MODIFIER_DOWN
) {
112 // deal with the common case first
113 if(kevents
[i
].keycode
< KEY_NONE
) {
114 c
= keymap
[kevents
[i
].keycode
];
122 char temp
= toupper(c
);
123 if(temp
>= 'A' && temp
<= ']')
128 out_buf
[buf_pos
++] = c
;
135 switch(kevents
[i
].keycode
) {
159 leds
= caps
? (leds
| KEY_LED_CAPS
) : (leds
& ~KEY_LED_CAPS
);
160 _kern_ioctl(keyboard_fd
, _KEYBOARD_IOCTL_SET_LEDS
, &leds
, sizeof(leds
));
162 case KEY_PAD_NUMLOCK
:
164 leds
= numlock
? (leds
| KEY_LED_NUM
) : (leds
& ~KEY_LED_NUM
);
165 _kern_ioctl(keyboard_fd
, _KEYBOARD_IOCTL_SET_LEDS
, &leds
, sizeof(leds
));
180 case KEY_PAD_MULTIPLY
:
193 if(numlock
) buf
[count
++] = '0';
196 if(numlock
) buf
[count
++] = '1';
199 if(numlock
) buf
[count
++] = '2';
202 if(numlock
) buf
[count
++] = '3';
205 if(numlock
) buf
[count
++] = '4';
208 if(numlock
) buf
[count
++] = '5';
211 if(numlock
) buf
[count
++] = '6';
214 if(numlock
) buf
[count
++] = '7';
217 if(numlock
) buf
[count
++] = '8';
220 if(numlock
) buf
[count
++] = '9';
223 for(j
= 0; j
< count
; j
++) {
224 out_buf
[buf_pos
++] = buf
[j
];
227 } else if(kevents
[i
].modifiers
& KEY_MODIFIER_UP
) {
228 switch(kevents
[i
].keycode
) {
252 // the shift or caps state changed, reset the keymap
255 if(caps
) keymap
= shifted_caps_keymap
;
256 else keymap
= shifted_keymap
;
258 if(caps
) keymap
= caps_keymap
;
259 else keymap
= unshifted_keymap
;