Subversion Repositories HelenOS

Rev

Rev 4042 | Rev 4228 | 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
 
4092 decky 29
/** @addtogroup genarch
1702 cejka 30
 * @{
31
 */
1754 jermar 32
/**
1757 jermar 33
 * @file
4092 decky 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
4092 decky 56
    #define IGNORE_CODE  0x7f
4042 jermar 57
#endif
508 jermar 58
 
4092 decky 59
#define KEY_RELEASE  0x80
878 vana 60
 
4092 decky 61
#define PRESSED_SHIFT     (1 << 0)
62
#define PRESSED_CAPSLOCK  (1 << 1)
63
#define LOCKED_CAPSLOCK   (1 << 0)
1896 jermar 64
 
4092 decky 65
static indev_t kbrdout;
878 vana 66
 
4092 decky 67
indev_operations_t kbrdout_ops = {
68
    .poll = NULL
4042 jermar 69
};
70
 
4092 decky 71
SPINLOCK_INITIALIZE(keylock);   /**< keylock protects keyflags and lockflags. */
72
static volatile int keyflags;   /**< Tracking of multiple keypresses. */
73
static volatile int lockflags;  /**< Tracking of multiple keys lockings. */
508 jermar 74
 
4042 jermar 75
static void key_released(uint8_t);
76
static void key_pressed(uint8_t);
77
 
78
static void kkbrd(void *arg)
79
{
4092 decky 80
    indev_t *in = (indev_t *) arg;
81
 
82
    while (true) {
83
        uint8_t sc = _getc(in);
84
 
4042 jermar 85
#ifdef CONFIG_SUN_KBD
86
        if (sc == IGNORE_CODE)
87
            continue;
88
#endif
89
 
90
        if (sc & KEY_RELEASE)
91
            key_released(sc ^ KEY_RELEASE);
92
        else
93
            key_pressed(sc);
94
    }
95
}
96
 
4092 decky 97
void kbrd_init(indev_t *devin)
4042 jermar 98
{
4092 decky 99
    indev_initialize("kbrd", &kbrdout, &kbrdout_ops);
100
    thread_t *thread
101
        = thread_create(kkbrd, devin, TASK, 0, "kkbrd", false);
4042 jermar 102
 
4092 decky 103
    if (thread) {
104
        stdin = &kbrdout;
105
        thread_ready(thread);
106
    }
4042 jermar 107
}
108
 
508 jermar 109
/** Process release of key.
110
 *
111
 * @param sc Scancode of the key being released.
112
 */
1780 jermar 113
void key_released(uint8_t sc)
508 jermar 114
{
115
    spinlock_lock(&keylock);
116
    switch (sc) {
1875 jermar 117
    case SC_LSHIFT:
118
    case SC_RSHIFT:
508 jermar 119
        keyflags &= ~PRESSED_SHIFT;
120
        break;
1875 jermar 121
    case SC_CAPSLOCK:
508 jermar 122
        keyflags &= ~PRESSED_CAPSLOCK;
123
        if (lockflags & LOCKED_CAPSLOCK)
124
            lockflags &= ~LOCKED_CAPSLOCK;
125
        else
126
            lockflags |= LOCKED_CAPSLOCK;
127
        break;
1875 jermar 128
    default:
508 jermar 129
        break;
130
    }
131
    spinlock_unlock(&keylock);
132
}
133
 
134
/** Process keypress.
135
 *
136
 * @param sc Scancode of the key being pressed.
137
 */
1780 jermar 138
void key_pressed(uint8_t sc)
508 jermar 139
{
140
    char *map = sc_primary_map;
141
    char ascii = sc_primary_map[sc];
142
    bool shift, capslock;
143
    bool letter = false;
4092 decky 144
 
508 jermar 145
    spinlock_lock(&keylock);
146
    switch (sc) {
601 palkovsky 147
    case SC_LSHIFT:
148
    case SC_RSHIFT:
4092 decky 149
        keyflags |= PRESSED_SHIFT;
508 jermar 150
        break;
601 palkovsky 151
    case SC_CAPSLOCK:
508 jermar 152
        keyflags |= PRESSED_CAPSLOCK;
153
        break;
601 palkovsky 154
    case SC_SPEC_ESCAPE:
155
        break;
156
    case SC_LEFTARR:
4092 decky 157
        indev_push_character(stdin, 0x1b);
158
        indev_push_character(stdin, 0x5b);
159
        indev_push_character(stdin, 0x44);
601 palkovsky 160
        break;
161
    case SC_RIGHTARR:
4092 decky 162
        indev_push_character(stdin, 0x1b);
163
        indev_push_character(stdin, 0x5b);
164
        indev_push_character(stdin, 0x43);
601 palkovsky 165
        break;
166
    case SC_UPARR:
4092 decky 167
        indev_push_character(stdin, 0x1b);
168
        indev_push_character(stdin, 0x5b);
169
        indev_push_character(stdin, 0x41);
601 palkovsky 170
        break;
171
    case SC_DOWNARR:
4092 decky 172
        indev_push_character(stdin, 0x1b);
173
        indev_push_character(stdin, 0x5b);
174
        indev_push_character(stdin, 0x42);
601 palkovsky 175
        break;
606 palkovsky 176
    case SC_HOME:
4092 decky 177
        indev_push_character(stdin, 0x1b);
178
        indev_push_character(stdin, 0x4f);
179
        indev_push_character(stdin, 0x48);
606 palkovsky 180
        break;
181
    case SC_END:
4092 decky 182
        indev_push_character(stdin, 0x1b);
183
        indev_push_character(stdin, 0x4f);
184
        indev_push_character(stdin, 0x46);
606 palkovsky 185
        break;
186
    case SC_DELETE:
4092 decky 187
        indev_push_character(stdin, 0x1b);
188
        indev_push_character(stdin, 0x5b);
189
        indev_push_character(stdin, 0x33);
190
        indev_push_character(stdin, 0x7e);
606 palkovsky 191
        break;
601 palkovsky 192
    default:
4092 decky 193
        letter = islower(ascii);
2572 jermar 194
        capslock = (keyflags & PRESSED_CAPSLOCK) ||
195
            (lockflags & LOCKED_CAPSLOCK);
508 jermar 196
        shift = keyflags & PRESSED_SHIFT;
197
        if (letter && capslock)
198
            shift = !shift;
199
        if (shift)
200
            map = sc_secondary_map;
4092 decky 201
        indev_push_character(stdin, map[sc]);
508 jermar 202
        break;
203
    }
204
    spinlock_unlock(&keylock);
205
}
511 jermar 206
 
1754 jermar 207
/** @}
1702 cejka 208
 */