source: mainline/kernel/genarch/src/kbrd/kbrd.c@ bb1d15c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bb1d15c was 11b285d, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 8 years ago

Use standard signature for malloc() in kernel.

The remaining instances of blocking allocation are replaced with
a new separate function named nfmalloc (short for non-failing malloc).

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