1 | /*
|
---|
2 | * Copyright (c) 2006 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genarch
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Key processing.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <genarch/kbd/key.h>
|
---|
38 | #include <genarch/kbd/scanc.h>
|
---|
39 | #ifdef CONFIG_I8042
|
---|
40 | #include <genarch/kbd/scanc_pc.h>
|
---|
41 | #endif
|
---|
42 | #if (defined(CONFIG_Z8530) || defined(CONFIG_NS16550))
|
---|
43 | #include <genarch/kbd/scanc_sun.h>
|
---|
44 | #endif
|
---|
45 | #include <synch/spinlock.h>
|
---|
46 | #include <console/chardev.h>
|
---|
47 | #include <macros.h>
|
---|
48 |
|
---|
49 | #define PRESSED_SHIFT (1<<0)
|
---|
50 | #define PRESSED_CAPSLOCK (1<<1)
|
---|
51 | #define LOCKED_CAPSLOCK (1<<0)
|
---|
52 |
|
---|
53 | #define ACTIVE_READ_BUFF_SIZE 16 /* Must be power of 2 */
|
---|
54 |
|
---|
55 | chardev_t kbrd;
|
---|
56 |
|
---|
57 | static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
|
---|
58 |
|
---|
59 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */
|
---|
60 | static volatile int keyflags; /**< Tracking of multiple keypresses. */
|
---|
61 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */
|
---|
62 |
|
---|
63 | /** Process release of key.
|
---|
64 | *
|
---|
65 | * @param sc Scancode of the key being released.
|
---|
66 | */
|
---|
67 | void key_released(uint8_t sc)
|
---|
68 | {
|
---|
69 | spinlock_lock(&keylock);
|
---|
70 | switch (sc) {
|
---|
71 | case SC_LSHIFT:
|
---|
72 | case SC_RSHIFT:
|
---|
73 | keyflags &= ~PRESSED_SHIFT;
|
---|
74 | break;
|
---|
75 | case SC_CAPSLOCK:
|
---|
76 | keyflags &= ~PRESSED_CAPSLOCK;
|
---|
77 | if (lockflags & LOCKED_CAPSLOCK)
|
---|
78 | lockflags &= ~LOCKED_CAPSLOCK;
|
---|
79 | else
|
---|
80 | lockflags |= LOCKED_CAPSLOCK;
|
---|
81 | break;
|
---|
82 | default:
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | spinlock_unlock(&keylock);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Process keypress.
|
---|
89 | *
|
---|
90 | * @param sc Scancode of the key being pressed.
|
---|
91 | */
|
---|
92 | void key_pressed(uint8_t sc)
|
---|
93 | {
|
---|
94 | char *map = sc_primary_map;
|
---|
95 | char ascii = sc_primary_map[sc];
|
---|
96 | bool shift, capslock;
|
---|
97 | bool letter = false;
|
---|
98 |
|
---|
99 | spinlock_lock(&keylock);
|
---|
100 | switch (sc) {
|
---|
101 | case SC_LSHIFT:
|
---|
102 | case SC_RSHIFT:
|
---|
103 | keyflags |= PRESSED_SHIFT;
|
---|
104 | break;
|
---|
105 | case SC_CAPSLOCK:
|
---|
106 | keyflags |= PRESSED_CAPSLOCK;
|
---|
107 | break;
|
---|
108 | case SC_SPEC_ESCAPE:
|
---|
109 | break;
|
---|
110 | case SC_LEFTARR:
|
---|
111 | chardev_push_character(&kbrd, 0x1b);
|
---|
112 | chardev_push_character(&kbrd, 0x5b);
|
---|
113 | chardev_push_character(&kbrd, 0x44);
|
---|
114 | break;
|
---|
115 | case SC_RIGHTARR:
|
---|
116 | chardev_push_character(&kbrd, 0x1b);
|
---|
117 | chardev_push_character(&kbrd, 0x5b);
|
---|
118 | chardev_push_character(&kbrd, 0x43);
|
---|
119 | break;
|
---|
120 | case SC_UPARR:
|
---|
121 | chardev_push_character(&kbrd, 0x1b);
|
---|
122 | chardev_push_character(&kbrd, 0x5b);
|
---|
123 | chardev_push_character(&kbrd, 0x41);
|
---|
124 | break;
|
---|
125 | case SC_DOWNARR:
|
---|
126 | chardev_push_character(&kbrd, 0x1b);
|
---|
127 | chardev_push_character(&kbrd, 0x5b);
|
---|
128 | chardev_push_character(&kbrd, 0x42);
|
---|
129 | break;
|
---|
130 | case SC_HOME:
|
---|
131 | chardev_push_character(&kbrd, 0x1b);
|
---|
132 | chardev_push_character(&kbrd, 0x4f);
|
---|
133 | chardev_push_character(&kbrd, 0x48);
|
---|
134 | break;
|
---|
135 | case SC_END:
|
---|
136 | chardev_push_character(&kbrd, 0x1b);
|
---|
137 | chardev_push_character(&kbrd, 0x4f);
|
---|
138 | chardev_push_character(&kbrd, 0x46);
|
---|
139 | break;
|
---|
140 | case SC_DELETE:
|
---|
141 | chardev_push_character(&kbrd, 0x1b);
|
---|
142 | chardev_push_character(&kbrd, 0x5b);
|
---|
143 | chardev_push_character(&kbrd, 0x33);
|
---|
144 | chardev_push_character(&kbrd, 0x7e);
|
---|
145 | break;
|
---|
146 | default:
|
---|
147 | letter = is_lower(ascii);
|
---|
148 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
|
---|
149 | shift = keyflags & PRESSED_SHIFT;
|
---|
150 | if (letter && capslock)
|
---|
151 | shift = !shift;
|
---|
152 | if (shift)
|
---|
153 | map = sc_secondary_map;
|
---|
154 | chardev_push_character(&kbrd, map[sc]);
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | spinlock_unlock(&keylock);
|
---|
158 | }
|
---|
159 |
|
---|
160 | uint8_t active_read_buff_read(void)
|
---|
161 | {
|
---|
162 | static int i=0;
|
---|
163 | i &= (ACTIVE_READ_BUFF_SIZE-1);
|
---|
164 | if(!active_read_buff[i]) {
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 | return active_read_buff[i++];
|
---|
168 | }
|
---|
169 |
|
---|
170 | void active_read_buff_write(uint8_t ch)
|
---|
171 | {
|
---|
172 | static int i=0;
|
---|
173 | active_read_buff[i] = ch;
|
---|
174 | i++;
|
---|
175 | i &= (ACTIVE_READ_BUFF_SIZE-1);
|
---|
176 | active_read_buff[i]=0;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void active_read_key_pressed(uint8_t sc)
|
---|
181 | {
|
---|
182 | char *map = sc_primary_map;
|
---|
183 | char ascii = sc_primary_map[sc];
|
---|
184 | bool shift, capslock;
|
---|
185 | bool letter = false;
|
---|
186 |
|
---|
187 | /*spinlock_lock(&keylock);*/
|
---|
188 | switch (sc) {
|
---|
189 | case SC_LSHIFT:
|
---|
190 | case SC_RSHIFT:
|
---|
191 | keyflags |= PRESSED_SHIFT;
|
---|
192 | break;
|
---|
193 | case SC_CAPSLOCK:
|
---|
194 | keyflags |= PRESSED_CAPSLOCK;
|
---|
195 | break;
|
---|
196 | case SC_SPEC_ESCAPE:
|
---|
197 | break;
|
---|
198 | case SC_LEFTARR:
|
---|
199 | active_read_buff_write(0x1b);
|
---|
200 | active_read_buff_write(0x5b);
|
---|
201 | active_read_buff_write(0x44);
|
---|
202 | break;
|
---|
203 | case SC_RIGHTARR:
|
---|
204 | active_read_buff_write(0x1b);
|
---|
205 | active_read_buff_write(0x5b);
|
---|
206 | active_read_buff_write(0x43);
|
---|
207 | break;
|
---|
208 | case SC_UPARR:
|
---|
209 | active_read_buff_write(0x1b);
|
---|
210 | active_read_buff_write(0x5b);
|
---|
211 | active_read_buff_write(0x41);
|
---|
212 | break;
|
---|
213 | case SC_DOWNARR:
|
---|
214 | active_read_buff_write(0x1b);
|
---|
215 | active_read_buff_write(0x5b);
|
---|
216 | active_read_buff_write(0x42);
|
---|
217 | break;
|
---|
218 | case SC_HOME:
|
---|
219 | active_read_buff_write(0x1b);
|
---|
220 | active_read_buff_write(0x4f);
|
---|
221 | active_read_buff_write(0x48);
|
---|
222 | break;
|
---|
223 | case SC_END:
|
---|
224 | active_read_buff_write(0x1b);
|
---|
225 | active_read_buff_write(0x4f);
|
---|
226 | active_read_buff_write(0x46);
|
---|
227 | break;
|
---|
228 | case SC_DELETE:
|
---|
229 | active_read_buff_write(0x1b);
|
---|
230 | active_read_buff_write(0x5b);
|
---|
231 | active_read_buff_write(0x33);
|
---|
232 | active_read_buff_write(0x7e);
|
---|
233 | break;
|
---|
234 | default:
|
---|
235 | letter = is_lower(ascii);
|
---|
236 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
|
---|
237 | shift = keyflags & PRESSED_SHIFT;
|
---|
238 | if (letter && capslock)
|
---|
239 | shift = !shift;
|
---|
240 | if (shift)
|
---|
241 | map = sc_secondary_map;
|
---|
242 | active_read_buff_write(map[sc]);
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | /*spinlock_unlock(&keylock);*/
|
---|
246 |
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** @}
|
---|
250 | */
|
---|