Rev 4223 | Rev 4311 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4223 | Rev 4228 | ||
---|---|---|---|
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 | #define IGNORE_CODE 0x7f |
55 | #define IGNORE_CODE 0x7f |
56 | #define KEY_RELEASE 0x80 |
56 | #define KEY_RELEASE 0x80 |
57 | 57 | ||
58 | #define PRESSED_SHIFT (1 << 0) |
58 | #define PRESSED_SHIFT (1 << 0) |
59 | #define PRESSED_CAPSLOCK (1 << 1) |
59 | #define PRESSED_CAPSLOCK (1 << 1) |
60 | #define LOCKED_CAPSLOCK (1 << 0) |
60 | #define LOCKED_CAPSLOCK (1 << 0) |
61 | 61 | ||
62 | static indev_t kbrdout; |
62 | static indev_t kbrdout; |
63 | 63 | ||
64 | indev_operations_t kbrdout_ops = { |
64 | indev_operations_t kbrdout_ops = { |
65 | .poll = NULL |
65 | .poll = NULL |
66 | }; |
66 | }; |
67 | 67 | ||
68 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
68 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
69 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
69 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
70 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
70 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
71 | 71 | ||
72 | /** Process release of key. |
72 | /** Process release of key. |
73 | * |
73 | * |
74 | * @param sc Scancode of the key being released. |
74 | * @param sc Scancode of the key being released. |
75 | */ |
75 | */ |
76 | static void key_released(wchar_t sc) |
76 | static void key_released(wchar_t sc) |
77 | { |
77 | { |
78 | spinlock_lock(&keylock); |
78 | spinlock_lock(&keylock); |
79 | switch (sc) { |
79 | switch (sc) { |
80 | case SC_LSHIFT: |
80 | case SC_LSHIFT: |
81 | case SC_RSHIFT: |
81 | case SC_RSHIFT: |
82 | keyflags &= ~PRESSED_SHIFT; |
82 | keyflags &= ~PRESSED_SHIFT; |
83 | break; |
83 | break; |
84 | case SC_CAPSLOCK: |
84 | case SC_CAPSLOCK: |
85 | keyflags &= ~PRESSED_CAPSLOCK; |
85 | keyflags &= ~PRESSED_CAPSLOCK; |
86 | if (lockflags & LOCKED_CAPSLOCK) |
86 | if (lockflags & LOCKED_CAPSLOCK) |
87 | lockflags &= ~LOCKED_CAPSLOCK; |
87 | lockflags &= ~LOCKED_CAPSLOCK; |
88 | else |
88 | else |
89 | lockflags |= LOCKED_CAPSLOCK; |
89 | lockflags |= LOCKED_CAPSLOCK; |
90 | break; |
90 | break; |
91 | default: |
91 | default: |
92 | break; |
92 | break; |
93 | } |
93 | } |
94 | spinlock_unlock(&keylock); |
94 | spinlock_unlock(&keylock); |
95 | } |
95 | } |
96 | 96 | ||
97 | /** Process keypress. |
97 | /** Process keypress. |
98 | * |
98 | * |
99 | * @param sc Scancode of the key being pressed. |
99 | * @param sc Scancode of the key being pressed. |
100 | */ |
100 | */ |
101 | static void key_pressed(wchar_t sc) |
101 | static void key_pressed(wchar_t sc) |
102 | { |
102 | { |
103 | bool letter; |
103 | bool letter; |
104 | bool shift; |
104 | bool shift; |
105 | bool capslock; |
105 | bool capslock; |
106 | 106 | ||
107 | spinlock_lock(&keylock); |
107 | spinlock_lock(&keylock); |
108 | switch (sc) { |
108 | switch (sc) { |
109 | case SC_LSHIFT: |
109 | case SC_LSHIFT: |
110 | case SC_RSHIFT: |
110 | case SC_RSHIFT: |
111 | keyflags |= PRESSED_SHIFT; |
111 | keyflags |= PRESSED_SHIFT; |
112 | break; |
112 | break; |
113 | case SC_CAPSLOCK: |
113 | case SC_CAPSLOCK: |
114 | keyflags |= PRESSED_CAPSLOCK; |
114 | keyflags |= PRESSED_CAPSLOCK; |
115 | break; |
115 | break; |
116 | case SC_SCAN_ESCAPE: |
116 | case SC_SCAN_ESCAPE: |
117 | break; |
117 | break; |
118 | default: |
118 | default: |
119 | letter = islower(sc_primary_map[sc]); |
119 | letter = islower(sc_primary_map[sc]); |
120 | shift = keyflags & PRESSED_SHIFT; |
120 | shift = keyflags & PRESSED_SHIFT; |
121 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
121 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
122 | (lockflags & LOCKED_CAPSLOCK); |
122 | (lockflags & LOCKED_CAPSLOCK); |
123 | 123 | ||
124 | if ((letter) && (capslock)) |
124 | if ((letter) && (capslock)) |
125 | shift = !shift; |
125 | shift = !shift; |
126 | 126 | ||
127 | if (shift) |
127 | if (shift) |
128 | indev_push_character(stdin, sc_secondary_map[sc]); |
128 | indev_push_character(stdin, sc_secondary_map[sc]); |
129 | else |
129 | else |
130 | indev_push_character(stdin, sc_primary_map[sc]); |
130 | indev_push_character(stdin, sc_primary_map[sc]); |
131 | break; |
131 | break; |
132 | } |
132 | } |
133 | spinlock_unlock(&keylock); |
133 | spinlock_unlock(&keylock); |
134 | } |
134 | } |
135 | 135 | ||
136 | static void kkbrd(void *arg) |
136 | static void kkbrd(void *arg) |
137 | { |
137 | { |
138 | indev_t *in = (indev_t *) arg; |
138 | indev_t *in = (indev_t *) arg; |
139 | 139 | ||
140 | while (true) { |
140 | while (true) { |
141 | wchar_t sc = _getc(in); |
141 | wchar_t sc = _getc(in); |
142 | 142 | ||
143 | if ((sc == IGNORE_CODE) || (sc >= SCANCODES)) |
143 | if (sc == IGNORE_CODE) |
144 | continue; |
144 | continue; |
145 | 145 | ||
146 | if (sc & KEY_RELEASE) |
146 | if (sc & KEY_RELEASE) |
147 | key_released(sc ^ KEY_RELEASE); |
147 | key_released((sc ^ KEY_RELEASE) & 0x7f); |
148 | else |
148 | else |
149 | key_pressed(sc); |
149 | key_pressed(sc & 0x7f); |
150 | } |
150 | } |
151 | } |
151 | } |
152 | 152 | ||
153 | void kbrd_init(indev_t *devin) |
153 | void kbrd_init(indev_t *devin) |
154 | { |
154 | { |
155 | indev_initialize("kbrd", &kbrdout, &kbrdout_ops); |
155 | indev_initialize("kbrd", &kbrdout, &kbrdout_ops); |
156 | thread_t *thread |
156 | thread_t *thread |
157 | = thread_create(kkbrd, devin, TASK, 0, "kkbrd", false); |
157 | = thread_create(kkbrd, devin, TASK, 0, "kkbrd", false); |
158 | 158 | ||
159 | if (thread) { |
159 | if (thread) { |
160 | stdin = &kbrdout; |
160 | stdin = &kbrdout; |
161 | thread_ready(thread); |
161 | thread_ready(thread); |
162 | } |
162 | } |
163 | } |
163 | } |
164 | 164 | ||
165 | /** @} |
165 | /** @} |
166 | */ |
166 | */ |
167 | 167 |