Rev 4092 | Rev 4263 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4092 | Rev 4153 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Jakub Jermar |
2 | * Copyright (c) 2009 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genarch |
29 | /** @addtogroup genarch |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief Keyboard processing. |
34 | * @brief Keyboard processing. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <genarch/kbrd/kbrd.h> |
37 | #include <genarch/kbrd/kbrd.h> |
38 | #include <genarch/kbrd/scanc.h> |
38 | #include <genarch/kbrd/scanc.h> |
39 | 39 | ||
40 | #ifdef CONFIG_PC_KBD |
40 | #ifdef CONFIG_PC_KBD |
41 | #include <genarch/kbrd/scanc_pc.h> |
41 | #include <genarch/kbrd/scanc_pc.h> |
42 | #endif |
42 | #endif |
43 | 43 | ||
44 | #ifdef CONFIG_SUN_KBD |
44 | #ifdef CONFIG_SUN_KBD |
45 | #include <genarch/kbrd/scanc_sun.h> |
45 | #include <genarch/kbrd/scanc_sun.h> |
46 | #endif |
46 | #endif |
47 | 47 | ||
48 | #include <synch/spinlock.h> |
48 | #include <synch/spinlock.h> |
49 | #include <console/chardev.h> |
49 | #include <console/chardev.h> |
50 | #include <console/console.h> |
50 | #include <console/console.h> |
51 | #include <proc/thread.h> |
51 | #include <proc/thread.h> |
52 | #include <arch.h> |
52 | #include <arch.h> |
53 | #include <macros.h> |
53 | #include <macros.h> |
54 | 54 | ||
55 | #ifdef CONFIG_SUN_KBD |
55 | #ifdef CONFIG_SUN_KBD |
56 | #define IGNORE_CODE 0x7f |
56 | #define IGNORE_CODE 0x7f |
57 | #endif |
57 | #endif |
58 | 58 | ||
59 | #define KEY_RELEASE 0x80 |
59 | #define KEY_RELEASE 0x80 |
60 | 60 | ||
61 | #define PRESSED_SHIFT (1 << 0) |
61 | #define PRESSED_SHIFT (1 << 0) |
62 | #define PRESSED_CAPSLOCK (1 << 1) |
62 | #define PRESSED_CAPSLOCK (1 << 1) |
63 | #define LOCKED_CAPSLOCK (1 << 0) |
63 | #define LOCKED_CAPSLOCK (1 << 0) |
64 | 64 | ||
65 | static indev_t kbrdout; |
65 | static indev_t kbrdout; |
66 | 66 | ||
67 | indev_operations_t kbrdout_ops = { |
67 | indev_operations_t kbrdout_ops = { |
68 | .poll = NULL |
68 | .poll = NULL |
69 | }; |
69 | }; |
70 | 70 | ||
71 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
71 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
72 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
72 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
73 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
73 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
74 | 74 | ||
75 | static void key_released(uint8_t); |
75 | static void key_released(uint8_t); |
76 | static void key_pressed(uint8_t); |
76 | static void key_pressed(uint8_t); |
77 | 77 | ||
78 | static void kkbrd(void *arg) |
78 | static void kkbrd(void *arg) |
79 | { |
79 | { |
80 | indev_t *in = (indev_t *) arg; |
80 | indev_t *in = (indev_t *) arg; |
81 | 81 | ||
82 | while (true) { |
82 | while (true) { |
83 | uint8_t sc = _getc(in); |
83 | uint8_t sc = _getc(in); |
84 | 84 | ||
85 | #ifdef CONFIG_SUN_KBD |
85 | #ifdef CONFIG_SUN_KBD |
86 | if (sc == IGNORE_CODE) |
86 | if (sc == IGNORE_CODE) |
87 | continue; |
87 | continue; |
88 | #endif |
88 | #endif |
89 | 89 | ||
90 | if (sc & KEY_RELEASE) |
90 | if (sc & KEY_RELEASE) |
91 | key_released(sc ^ KEY_RELEASE); |
91 | key_released(sc ^ KEY_RELEASE); |
92 | else |
92 | else |
93 | key_pressed(sc); |
93 | key_pressed(sc); |
94 | } |
94 | } |
95 | } |
95 | } |
96 | 96 | ||
97 | void kbrd_init(indev_t *devin) |
97 | void kbrd_init(indev_t *devin) |
98 | { |
98 | { |
99 | indev_initialize("kbrd", &kbrdout, &kbrdout_ops); |
99 | indev_initialize("kbrd", &kbrdout, &kbrdout_ops); |
100 | thread_t *thread |
100 | thread_t *thread |
101 | = thread_create(kkbrd, devin, TASK, 0, "kkbrd", false); |
101 | = thread_create(kkbrd, devin, TASK, 0, "kkbrd", false); |
102 | 102 | ||
103 | if (thread) { |
103 | if (thread) { |
104 | stdin = &kbrdout; |
104 | stdin = &kbrdout; |
105 | thread_ready(thread); |
105 | thread_ready(thread); |
106 | } |
106 | } |
107 | } |
107 | } |
108 | 108 | ||
109 | /** Process release of key. |
109 | /** Process release of key. |
110 | * |
110 | * |
111 | * @param sc Scancode of the key being released. |
111 | * @param sc Scancode of the key being released. |
112 | */ |
112 | */ |
113 | void key_released(uint8_t sc) |
113 | void key_released(uint8_t sc) |
114 | { |
114 | { |
115 | spinlock_lock(&keylock); |
115 | spinlock_lock(&keylock); |
116 | switch (sc) { |
116 | switch (sc) { |
117 | case SC_LSHIFT: |
117 | case SC_LSHIFT: |
118 | case SC_RSHIFT: |
118 | case SC_RSHIFT: |
119 | keyflags &= ~PRESSED_SHIFT; |
119 | keyflags &= ~PRESSED_SHIFT; |
120 | break; |
120 | break; |
121 | case SC_CAPSLOCK: |
121 | case SC_CAPSLOCK: |
122 | keyflags &= ~PRESSED_CAPSLOCK; |
122 | keyflags &= ~PRESSED_CAPSLOCK; |
123 | if (lockflags & LOCKED_CAPSLOCK) |
123 | if (lockflags & LOCKED_CAPSLOCK) |
124 | lockflags &= ~LOCKED_CAPSLOCK; |
124 | lockflags &= ~LOCKED_CAPSLOCK; |
125 | else |
125 | else |
126 | lockflags |= LOCKED_CAPSLOCK; |
126 | lockflags |= LOCKED_CAPSLOCK; |
127 | break; |
127 | break; |
128 | default: |
128 | default: |
129 | break; |
129 | break; |
130 | } |
130 | } |
131 | spinlock_unlock(&keylock); |
131 | spinlock_unlock(&keylock); |
132 | } |
132 | } |
133 | 133 | ||
134 | /** Process keypress. |
134 | /** Process keypress. |
135 | * |
135 | * |
136 | * @param sc Scancode of the key being pressed. |
136 | * @param sc Scancode of the key being pressed. |
137 | */ |
137 | */ |
138 | void key_pressed(uint8_t sc) |
138 | void key_pressed(uint8_t sc) |
139 | { |
139 | { |
140 | char *map = sc_primary_map; |
140 | char *map = sc_primary_map; |
141 | char ascii = sc_primary_map[sc]; |
141 | char ascii = sc_primary_map[sc]; |
142 | bool shift, capslock; |
142 | bool shift, capslock; |
143 | bool letter = false; |
143 | bool letter = false; |
144 | 144 | ||
145 | spinlock_lock(&keylock); |
145 | spinlock_lock(&keylock); |
146 | switch (sc) { |
146 | switch (sc) { |
147 | case SC_LSHIFT: |
147 | case SC_LSHIFT: |
148 | case SC_RSHIFT: |
148 | case SC_RSHIFT: |
149 | keyflags |= PRESSED_SHIFT; |
149 | keyflags |= PRESSED_SHIFT; |
150 | break; |
150 | break; |
151 | case SC_CAPSLOCK: |
151 | case SC_CAPSLOCK: |
152 | keyflags |= PRESSED_CAPSLOCK; |
152 | keyflags |= PRESSED_CAPSLOCK; |
153 | break; |
153 | break; |
154 | case SC_SPEC_ESCAPE: |
154 | case SC_SPEC_ESCAPE: |
155 | break; |
155 | break; |
156 | case SC_LEFTARR: |
156 | case SC_LEFTARR: |
157 | indev_push_character(stdin, 0x1b); |
157 | indev_push_character(stdin, 0x1b); |
158 | indev_push_character(stdin, 0x5b); |
158 | indev_push_character(stdin, 0x5b); |
159 | indev_push_character(stdin, 0x44); |
159 | indev_push_character(stdin, 0x44); |
160 | break; |
160 | break; |
161 | case SC_RIGHTARR: |
161 | case SC_RIGHTARR: |
162 | indev_push_character(stdin, 0x1b); |
162 | indev_push_character(stdin, 0x1b); |
163 | indev_push_character(stdin, 0x5b); |
163 | indev_push_character(stdin, 0x5b); |
164 | indev_push_character(stdin, 0x43); |
164 | indev_push_character(stdin, 0x43); |
165 | break; |
165 | break; |
166 | case SC_UPARR: |
166 | case SC_UPARR: |
167 | indev_push_character(stdin, 0x1b); |
167 | indev_push_character(stdin, 0x1b); |
168 | indev_push_character(stdin, 0x5b); |
168 | indev_push_character(stdin, 0x5b); |
169 | indev_push_character(stdin, 0x41); |
169 | indev_push_character(stdin, 0x41); |
170 | break; |
170 | break; |
171 | case SC_DOWNARR: |
171 | case SC_DOWNARR: |
172 | indev_push_character(stdin, 0x1b); |
172 | indev_push_character(stdin, 0x1b); |
173 | indev_push_character(stdin, 0x5b); |
173 | indev_push_character(stdin, 0x5b); |
174 | indev_push_character(stdin, 0x42); |
174 | indev_push_character(stdin, 0x42); |
175 | break; |
175 | break; |
176 | case SC_HOME: |
176 | case SC_HOME: |
177 | indev_push_character(stdin, 0x1b); |
177 | indev_push_character(stdin, 0x1b); |
178 | indev_push_character(stdin, 0x4f); |
178 | indev_push_character(stdin, 0x4f); |
179 | indev_push_character(stdin, 0x48); |
179 | indev_push_character(stdin, 0x48); |
180 | break; |
180 | break; |
181 | case SC_END: |
181 | case SC_END: |
182 | indev_push_character(stdin, 0x1b); |
182 | indev_push_character(stdin, 0x1b); |
183 | indev_push_character(stdin, 0x4f); |
183 | indev_push_character(stdin, 0x4f); |
184 | indev_push_character(stdin, 0x46); |
184 | indev_push_character(stdin, 0x46); |
185 | break; |
185 | break; |
186 | case SC_DELETE: |
186 | case SC_DELETE: |
187 | indev_push_character(stdin, 0x1b); |
187 | indev_push_character(stdin, 0x1b); |
188 | indev_push_character(stdin, 0x5b); |
188 | indev_push_character(stdin, 0x5b); |
189 | indev_push_character(stdin, 0x33); |
189 | indev_push_character(stdin, 0x33); |
190 | indev_push_character(stdin, 0x7e); |
190 | indev_push_character(stdin, 0x7e); |
191 | break; |
191 | break; |
192 | default: |
192 | default: |
193 | letter = islower(ascii); |
193 | letter = islower(ascii); |
194 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
194 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
195 | (lockflags & LOCKED_CAPSLOCK); |
195 | (lockflags & LOCKED_CAPSLOCK); |
196 | shift = keyflags & PRESSED_SHIFT; |
196 | shift = keyflags & PRESSED_SHIFT; |
197 | if (letter && capslock) |
197 | if (letter && capslock) |
198 | shift = !shift; |
198 | shift = !shift; |
199 | if (shift) |
199 | if (shift) |
200 | map = sc_secondary_map; |
200 | map = sc_secondary_map; |
201 | indev_push_character(stdin, map[sc]); |
201 | indev_push_character(stdin, map[sc]); |
202 | break; |
202 | break; |
203 | } |
203 | } |
204 | spinlock_unlock(&keylock); |
204 | spinlock_unlock(&keylock); |
205 | } |
205 | } |
206 | 206 | ||
207 | /** @} |
207 | /** @} |
208 | */ |
208 | */ |
209 | 209 |