[411b6a6] | 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 |
|
---|
[b6f2ebc] | 29 | /** @addtogroup genarch
|
---|
[411b6a6] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[b6f2ebc] | 34 | * @brief Keyboard processing.
|
---|
[411b6a6] | 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 |
|
---|
[2a77841d] | 48 | #ifdef CONFIG_MAC_KBD
|
---|
| 49 | #include <genarch/kbrd/scanc_mac.h>
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
[411b6a6] | 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>
|
---|
[7ddc2c7] | 58 | #include <str.h>
|
---|
[411b6a6] | 59 |
|
---|
[c8bf88d] | 60 | #define IGNORE_CODE 0x7f
|
---|
[b6f2ebc] | 61 | #define KEY_RELEASE 0x80
|
---|
[411b6a6] | 62 |
|
---|
[b6f2ebc] | 63 | #define PRESSED_SHIFT (1 << 0)
|
---|
| 64 | #define PRESSED_CAPSLOCK (1 << 1)
|
---|
| 65 | #define LOCKED_CAPSLOCK (1 << 0)
|
---|
[411b6a6] | 66 |
|
---|
[c2417bc] | 67 | static indev_operations_t kbrd_raw_ops = {
|
---|
[7ddc2c7] | 68 | .poll = NULL,
|
---|
| 69 | .signal = NULL
|
---|
[411b6a6] | 70 | };
|
---|
| 71 |
|
---|
| 72 | /** Process release of key.
|
---|
| 73 | *
|
---|
| 74 | * @param sc Scancode of the key being released.
|
---|
| 75 | */
|
---|
[c2417bc] | 76 | static void key_released(kbrd_instance_t *instance, wchar_t sc)
|
---|
[411b6a6] | 77 | {
|
---|
[c2417bc] | 78 | spinlock_lock(&instance->keylock);
|
---|
| 79 |
|
---|
[411b6a6] | 80 | switch (sc) {
|
---|
| 81 | case SC_LSHIFT:
|
---|
| 82 | case SC_RSHIFT:
|
---|
[c2417bc] | 83 | instance->keyflags &= ~PRESSED_SHIFT;
|
---|
[411b6a6] | 84 | break;
|
---|
| 85 | case SC_CAPSLOCK:
|
---|
[c2417bc] | 86 | instance->keyflags &= ~PRESSED_CAPSLOCK;
|
---|
| 87 | if (instance->lockflags & LOCKED_CAPSLOCK)
|
---|
| 88 | instance->lockflags &= ~LOCKED_CAPSLOCK;
|
---|
[411b6a6] | 89 | else
|
---|
[c2417bc] | 90 | instance->lockflags |= LOCKED_CAPSLOCK;
|
---|
[411b6a6] | 91 | break;
|
---|
| 92 | default:
|
---|
| 93 | break;
|
---|
| 94 | }
|
---|
[c2417bc] | 95 |
|
---|
| 96 | spinlock_unlock(&instance->keylock);
|
---|
[411b6a6] | 97 | }
|
---|
| 98 |
|
---|
| 99 | /** Process keypress.
|
---|
| 100 | *
|
---|
| 101 | * @param sc Scancode of the key being pressed.
|
---|
| 102 | */
|
---|
[c2417bc] | 103 | static void key_pressed(kbrd_instance_t *instance, wchar_t sc)
|
---|
[411b6a6] | 104 | {
|
---|
[c8bf88d] | 105 | bool letter;
|
---|
| 106 | bool shift;
|
---|
| 107 | bool capslock;
|
---|
[7ddc2c7] | 108 | wchar_t ch;
|
---|
[b6f2ebc] | 109 |
|
---|
[c2417bc] | 110 | spinlock_lock(&instance->keylock);
|
---|
| 111 |
|
---|
[411b6a6] | 112 | switch (sc) {
|
---|
| 113 | case SC_LSHIFT:
|
---|
| 114 | case SC_RSHIFT:
|
---|
[c2417bc] | 115 | instance->keyflags |= PRESSED_SHIFT;
|
---|
[411b6a6] | 116 | break;
|
---|
| 117 | case SC_CAPSLOCK:
|
---|
[c2417bc] | 118 | instance->keyflags |= PRESSED_CAPSLOCK;
|
---|
[411b6a6] | 119 | break;
|
---|
[c8bf88d] | 120 | case SC_SCAN_ESCAPE:
|
---|
[411b6a6] | 121 | break;
|
---|
| 122 | default:
|
---|
[c8bf88d] | 123 | letter = islower(sc_primary_map[sc]);
|
---|
[c2417bc] | 124 | shift = instance->keyflags & PRESSED_SHIFT;
|
---|
| 125 | capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
|
---|
| 126 | (instance->lockflags & LOCKED_CAPSLOCK);
|
---|
[c8bf88d] | 127 |
|
---|
| 128 | if ((letter) && (capslock))
|
---|
[411b6a6] | 129 | shift = !shift;
|
---|
[c8bf88d] | 130 |
|
---|
[411b6a6] | 131 | if (shift)
|
---|
[7ddc2c7] | 132 | ch = sc_secondary_map[sc];
|
---|
[c8bf88d] | 133 | else
|
---|
[7ddc2c7] | 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 |
|
---|
[411b6a6] | 147 | break;
|
---|
| 148 | }
|
---|
[c2417bc] | 149 |
|
---|
| 150 | spinlock_unlock(&instance->keylock);
|
---|
[411b6a6] | 151 | }
|
---|
| 152 |
|
---|
[c8bf88d] | 153 | static void kkbrd(void *arg)
|
---|
| 154 | {
|
---|
[c2417bc] | 155 | kbrd_instance_t *instance = (kbrd_instance_t *) arg;
|
---|
[c8bf88d] | 156 |
|
---|
| 157 | while (true) {
|
---|
[c2417bc] | 158 | wchar_t sc = indev_pop_character(&instance->raw);
|
---|
[c8bf88d] | 159 |
|
---|
[a85aebd] | 160 | if (sc == IGNORE_CODE)
|
---|
[c8bf88d] | 161 | continue;
|
---|
| 162 |
|
---|
| 163 | if (sc & KEY_RELEASE)
|
---|
[c2417bc] | 164 | key_released(instance, (sc ^ KEY_RELEASE) & 0x7f);
|
---|
[c8bf88d] | 165 | else
|
---|
[c2417bc] | 166 | key_pressed(instance, sc & 0x7f);
|
---|
[c8bf88d] | 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[c2417bc] | 170 | kbrd_instance_t *kbrd_init(void)
|
---|
[c8bf88d] | 171 | {
|
---|
[c2417bc] | 172 | kbrd_instance_t *instance
|
---|
| 173 | = malloc(sizeof(kbrd_instance_t), FRAME_ATOMIC);
|
---|
| 174 | if (instance) {
|
---|
[6eef3c4] | 175 | instance->thread = thread_create(kkbrd, (void *) instance,
|
---|
| 176 | TASK, THREAD_FLAG_NONE, "kkbrd");
|
---|
[c2417bc] | 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 |
|
---|
[ba7371f9] | 186 | spinlock_initialize(&instance->keylock, "kbrd.instance.keylock");
|
---|
[c2417bc] | 187 | instance->keyflags = 0;
|
---|
| 188 | instance->lockflags = 0;
|
---|
[c8bf88d] | 189 | }
|
---|
[c2417bc] | 190 |
|
---|
| 191 | return instance;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | indev_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;
|
---|
[c8bf88d] | 203 | }
|
---|
| 204 |
|
---|
[411b6a6] | 205 | /** @}
|
---|
| 206 | */
|
---|