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

Last change on this file was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 19 months ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

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