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