Rev 3877 | Rev 4092 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 4042 | jermar | 2 | * Copyright (c) 2009 Jakub Jermar |
| 1 | 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 | |||
| 1754 | jermar | 29 | /** @addtogroup genarch |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 1754 | jermar | 32 | /** |
| 1757 | jermar | 33 | * @file |
| 4042 | jermar | 34 | * @brief Keyboard processing. |
| 1702 | cejka | 35 | */ |
| 36 | |||
| 4042 | jermar | 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> |
||
| 1844 | jermar | 42 | #endif |
| 3577 | vana | 43 | |
| 4042 | jermar | 44 | #ifdef CONFIG_SUN_KBD |
| 45 | #include <genarch/kbrd/scanc_sun.h> |
||
| 1844 | jermar | 46 | #endif |
| 3577 | vana | 47 | |
| 508 | jermar | 48 | #include <synch/spinlock.h> |
| 1843 | jermar | 49 | #include <console/chardev.h> |
| 4042 | jermar | 50 | #include <console/console.h> |
| 51 | #include <proc/thread.h> |
||
| 52 | #include <arch.h> |
||
| 517 | jermar | 53 | #include <macros.h> |
| 1 | jermar | 54 | |
| 4042 | jermar | 55 | #ifdef CONFIG_SUN_KBD |
| 56 | # define IGNORE_CODE 0x7f |
||
| 57 | #endif |
||
| 508 | jermar | 58 | |
| 4042 | jermar | 59 | #define KEY_RELEASE 0x80 |
| 878 | vana | 60 | |
| 4042 | jermar | 61 | #define PRESSED_SHIFT (1 << 0) |
| 62 | #define PRESSED_CAPSLOCK (1 << 1) |
||
| 63 | #define LOCKED_CAPSLOCK (1 << 0) |
||
| 1896 | jermar | 64 | |
| 4042 | jermar | 65 | chardev_t kbrdin; |
| 66 | static chardev_t *kbdout; |
||
| 878 | vana | 67 | |
| 4042 | jermar | 68 | static void kbrdin_suspend(chardev_t *d) |
| 69 | { |
||
| 70 | } |
||
| 71 | |||
| 72 | static void kbrdin_resume(chardev_t *d) |
||
| 73 | { |
||
| 74 | } |
||
| 75 | |||
| 76 | chardev_operations_t kbrdin_ops = { |
||
| 77 | .suspend = kbrdin_suspend, |
||
| 78 | .resume = kbrdin_resume, |
||
| 79 | }; |
||
| 80 | |||
| 623 | jermar | 81 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
| 508 | jermar | 82 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
| 509 | jermar | 83 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
| 508 | jermar | 84 | |
| 4042 | jermar | 85 | static void key_released(uint8_t); |
| 86 | static void key_pressed(uint8_t); |
||
| 87 | |||
| 88 | static void kkbrd(void *arg) |
||
| 89 | { |
||
| 90 | chardev_t *in = (chardev_t *) arg; |
||
| 91 | uint8_t sc; |
||
| 92 | |||
| 93 | while (1) { |
||
| 94 | sc = _getc(in); |
||
| 95 | |||
| 96 | #ifdef CONFIG_SUN_KBD |
||
| 97 | if (sc == IGNORE_CODE) |
||
| 98 | continue; |
||
| 99 | #endif |
||
| 100 | |||
| 101 | if (sc & KEY_RELEASE) |
||
| 102 | key_released(sc ^ KEY_RELEASE); |
||
| 103 | else |
||
| 104 | key_pressed(sc); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | void kbrd_init(chardev_t *devout) |
||
| 110 | { |
||
| 111 | thread_t *t; |
||
| 112 | |||
| 113 | chardev_initialize("kbrd", &kbrdin, &kbrdin_ops); |
||
| 114 | kbdout = devout; |
||
| 115 | |||
| 116 | t = thread_create(kkbrd, &kbrdin, TASK, 0, "kkbrd", false); |
||
| 117 | ASSERT(t); |
||
| 118 | thread_ready(t); |
||
| 119 | } |
||
| 120 | |||
| 508 | jermar | 121 | /** Process release of key. |
| 122 | * |
||
| 123 | * @param sc Scancode of the key being released. |
||
| 124 | */ |
||
| 1780 | jermar | 125 | void key_released(uint8_t sc) |
| 508 | jermar | 126 | { |
| 127 | spinlock_lock(&keylock); |
||
| 128 | switch (sc) { |
||
| 1875 | jermar | 129 | case SC_LSHIFT: |
| 130 | case SC_RSHIFT: |
||
| 508 | jermar | 131 | keyflags &= ~PRESSED_SHIFT; |
| 132 | break; |
||
| 1875 | jermar | 133 | case SC_CAPSLOCK: |
| 508 | jermar | 134 | keyflags &= ~PRESSED_CAPSLOCK; |
| 135 | if (lockflags & LOCKED_CAPSLOCK) |
||
| 136 | lockflags &= ~LOCKED_CAPSLOCK; |
||
| 137 | else |
||
| 138 | lockflags |= LOCKED_CAPSLOCK; |
||
| 139 | break; |
||
| 1875 | jermar | 140 | default: |
| 508 | jermar | 141 | break; |
| 142 | } |
||
| 143 | spinlock_unlock(&keylock); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** Process keypress. |
||
| 147 | * |
||
| 148 | * @param sc Scancode of the key being pressed. |
||
| 149 | */ |
||
| 1780 | jermar | 150 | void key_pressed(uint8_t sc) |
| 508 | jermar | 151 | { |
| 152 | char *map = sc_primary_map; |
||
| 153 | char ascii = sc_primary_map[sc]; |
||
| 154 | bool shift, capslock; |
||
| 155 | bool letter = false; |
||
| 156 | |||
| 157 | spinlock_lock(&keylock); |
||
| 158 | switch (sc) { |
||
| 601 | palkovsky | 159 | case SC_LSHIFT: |
| 160 | case SC_RSHIFT: |
||
| 508 | jermar | 161 | keyflags |= PRESSED_SHIFT; |
| 162 | break; |
||
| 601 | palkovsky | 163 | case SC_CAPSLOCK: |
| 508 | jermar | 164 | keyflags |= PRESSED_CAPSLOCK; |
| 165 | break; |
||
| 601 | palkovsky | 166 | case SC_SPEC_ESCAPE: |
| 167 | break; |
||
| 168 | case SC_LEFTARR: |
||
| 4042 | jermar | 169 | chardev_push_character(kbdout, 0x1b); |
| 170 | chardev_push_character(kbdout, 0x5b); |
||
| 171 | chardev_push_character(kbdout, 0x44); |
||
| 601 | palkovsky | 172 | break; |
| 173 | case SC_RIGHTARR: |
||
| 4042 | jermar | 174 | chardev_push_character(kbdout, 0x1b); |
| 175 | chardev_push_character(kbdout, 0x5b); |
||
| 176 | chardev_push_character(kbdout, 0x43); |
||
| 601 | palkovsky | 177 | break; |
| 178 | case SC_UPARR: |
||
| 4042 | jermar | 179 | chardev_push_character(kbdout, 0x1b); |
| 180 | chardev_push_character(kbdout, 0x5b); |
||
| 181 | chardev_push_character(kbdout, 0x41); |
||
| 601 | palkovsky | 182 | break; |
| 183 | case SC_DOWNARR: |
||
| 4042 | jermar | 184 | chardev_push_character(kbdout, 0x1b); |
| 185 | chardev_push_character(kbdout, 0x5b); |
||
| 186 | chardev_push_character(kbdout, 0x42); |
||
| 601 | palkovsky | 187 | break; |
| 606 | palkovsky | 188 | case SC_HOME: |
| 4042 | jermar | 189 | chardev_push_character(kbdout, 0x1b); |
| 190 | chardev_push_character(kbdout, 0x4f); |
||
| 191 | chardev_push_character(kbdout, 0x48); |
||
| 606 | palkovsky | 192 | break; |
| 193 | case SC_END: |
||
| 4042 | jermar | 194 | chardev_push_character(kbdout, 0x1b); |
| 195 | chardev_push_character(kbdout, 0x4f); |
||
| 196 | chardev_push_character(kbdout, 0x46); |
||
| 606 | palkovsky | 197 | break; |
| 198 | case SC_DELETE: |
||
| 4042 | jermar | 199 | chardev_push_character(kbdout, 0x1b); |
| 200 | chardev_push_character(kbdout, 0x5b); |
||
| 201 | chardev_push_character(kbdout, 0x33); |
||
| 202 | chardev_push_character(kbdout, 0x7e); |
||
| 606 | palkovsky | 203 | break; |
| 601 | palkovsky | 204 | default: |
| 2572 | jermar | 205 | letter = islower(ascii); |
| 206 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
||
| 207 | (lockflags & LOCKED_CAPSLOCK); |
||
| 508 | jermar | 208 | shift = keyflags & PRESSED_SHIFT; |
| 209 | if (letter && capslock) |
||
| 210 | shift = !shift; |
||
| 211 | if (shift) |
||
| 212 | map = sc_secondary_map; |
||
| 4042 | jermar | 213 | chardev_push_character(kbdout, map[sc]); |
| 508 | jermar | 214 | break; |
| 215 | } |
||
| 216 | spinlock_unlock(&keylock); |
||
| 217 | } |
||
| 511 | jermar | 218 | |
| 1754 | jermar | 219 | /** @} |
| 1702 | cejka | 220 | */ |