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