source: mainline/kernel/genarch/src/kbrd/kbrd.c@ 36df4109

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36df4109 was 7ddc2c7, checked in by Martin Decky <martin@…>, 11 years ago

add support for framebuffer history paging (using Page Up and Page Down keys), inspiration taken from the code by Sebastian Köln
add support for input device out-of-band signalling

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2009 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 Keyboard processing.
35 */
36
37#include <genarch/kbrd/kbrd.h>
38#include <genarch/kbrd/scanc.h>
39
40#ifdef CONFIG_PC_KBD
41#include <genarch/kbrd/scanc_pc.h>
42#endif
43
44#ifdef CONFIG_SUN_KBD
45#include <genarch/kbrd/scanc_sun.h>
46#endif
47
48#ifdef CONFIG_MAC_KBD
49#include <genarch/kbrd/scanc_mac.h>
50#endif
51
52#include <synch/spinlock.h>
53#include <console/chardev.h>
54#include <console/console.h>
55#include <proc/thread.h>
56#include <arch.h>
57#include <macros.h>
58#include <str.h>
59
60#define IGNORE_CODE 0x7f
61#define KEY_RELEASE 0x80
62
63#define PRESSED_SHIFT (1 << 0)
64#define PRESSED_CAPSLOCK (1 << 1)
65#define LOCKED_CAPSLOCK (1 << 0)
66
67static indev_operations_t kbrd_raw_ops = {
68 .poll = NULL,
69 .signal = NULL
70};
71
72/** Process release of key.
73 *
74 * @param sc Scancode of the key being released.
75 */
76static void key_released(kbrd_instance_t *instance, wchar_t sc)
77{
78 spinlock_lock(&instance->keylock);
79
80 switch (sc) {
81 case SC_LSHIFT:
82 case SC_RSHIFT:
83 instance->keyflags &= ~PRESSED_SHIFT;
84 break;
85 case SC_CAPSLOCK:
86 instance->keyflags &= ~PRESSED_CAPSLOCK;
87 if (instance->lockflags & LOCKED_CAPSLOCK)
88 instance->lockflags &= ~LOCKED_CAPSLOCK;
89 else
90 instance->lockflags |= LOCKED_CAPSLOCK;
91 break;
92 default:
93 break;
94 }
95
96 spinlock_unlock(&instance->keylock);
97}
98
99/** Process keypress.
100 *
101 * @param sc Scancode of the key being pressed.
102 */
103static void key_pressed(kbrd_instance_t *instance, wchar_t sc)
104{
105 bool letter;
106 bool shift;
107 bool capslock;
108 wchar_t ch;
109
110 spinlock_lock(&instance->keylock);
111
112 switch (sc) {
113 case SC_LSHIFT:
114 case SC_RSHIFT:
115 instance->keyflags |= PRESSED_SHIFT;
116 break;
117 case SC_CAPSLOCK:
118 instance->keyflags |= PRESSED_CAPSLOCK;
119 break;
120 case SC_SCAN_ESCAPE:
121 break;
122 default:
123 letter = islower(sc_primary_map[sc]);
124 shift = instance->keyflags & PRESSED_SHIFT;
125 capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
126 (instance->lockflags & LOCKED_CAPSLOCK);
127
128 if ((letter) && (capslock))
129 shift = !shift;
130
131 if (shift)
132 ch = sc_secondary_map[sc];
133 else
134 ch = sc_primary_map[sc];
135
136 switch (ch) {
137 case U_PAGE_UP:
138 indev_signal(instance->sink, INDEV_SIGNAL_SCROLL_UP);
139 break;
140 case U_PAGE_DOWN:
141 indev_signal(instance->sink, INDEV_SIGNAL_SCROLL_DOWN);
142 break;
143 default:
144 indev_push_character(instance->sink, ch);
145 }
146
147 break;
148 }
149
150 spinlock_unlock(&instance->keylock);
151}
152
153static void kkbrd(void *arg)
154{
155 kbrd_instance_t *instance = (kbrd_instance_t *) arg;
156
157 while (true) {
158 wchar_t sc = indev_pop_character(&instance->raw);
159
160 if (sc == IGNORE_CODE)
161 continue;
162
163 if (sc & KEY_RELEASE)
164 key_released(instance, (sc ^ KEY_RELEASE) & 0x7f);
165 else
166 key_pressed(instance, sc & 0x7f);
167 }
168}
169
170kbrd_instance_t *kbrd_init(void)
171{
172 kbrd_instance_t *instance
173 = malloc(sizeof(kbrd_instance_t), FRAME_ATOMIC);
174 if (instance) {
175 instance->thread = thread_create(kkbrd, (void *) instance,
176 TASK, THREAD_FLAG_NONE, "kkbrd");
177
178 if (!instance->thread) {
179 free(instance);
180 return NULL;
181 }
182
183 instance->sink = NULL;
184 indev_initialize("kbrd", &instance->raw, &kbrd_raw_ops);
185
186 spinlock_initialize(&instance->keylock, "kbrd.instance.keylock");
187 instance->keyflags = 0;
188 instance->lockflags = 0;
189 }
190
191 return instance;
192}
193
194indev_t *kbrd_wire(kbrd_instance_t *instance, indev_t *sink)
195{
196 ASSERT(instance);
197 ASSERT(sink);
198
199 instance->sink = sink;
200 thread_ready(instance->thread);
201
202 return &instance->raw;
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.