Subversion Repositories HelenOS

Rev

Rev 1844 | Rev 1896 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
1843 jermar 2
 * Copyright (C) 2006 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
1843 jermar 34
 * @brief   Key processing.
1702 cejka 35
 */
36
 
1843 jermar 37
#include <genarch/kbd/key.h>
1842 jermar 38
#include <genarch/kbd/scanc.h>
1844 jermar 39
#ifdef CONFIG_I8042
1842 jermar 40
#include <genarch/kbd/scanc_pc.h>
1844 jermar 41
#endif
42
#if (defined(CONFIG_Z8530) || defined(CONFIG_NS16550))
43
#include <genarch/kbd/scanc_sun.h>
44
#endif
508 jermar 45
#include <synch/spinlock.h>
1843 jermar 46
#include <console/chardev.h>
508 jermar 47
#include <typedefs.h>
517 jermar 48
#include <macros.h>
1 jermar 49
 
508 jermar 50
#define PRESSED_SHIFT       (1<<0)
51
#define PRESSED_CAPSLOCK    (1<<1)
52
#define LOCKED_CAPSLOCK     (1<<0)
53
 
894 jermar 54
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
878 vana 55
 
1780 jermar 56
static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
878 vana 57
 
623 jermar 58
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
508 jermar 59
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
509 jermar 60
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
508 jermar 61
 
62
/** Process release of key.
63
 *
64
 * @param sc Scancode of the key being released.
65
 */
1780 jermar 66
void key_released(uint8_t sc)
508 jermar 67
{
68
    spinlock_lock(&keylock);
69
    switch (sc) {
1875 jermar 70
    case SC_LSHIFT:
71
    case SC_RSHIFT:
508 jermar 72
        keyflags &= ~PRESSED_SHIFT;
73
        break;
1875 jermar 74
    case SC_CAPSLOCK:
508 jermar 75
        keyflags &= ~PRESSED_CAPSLOCK;
76
        if (lockflags & LOCKED_CAPSLOCK)
77
            lockflags &= ~LOCKED_CAPSLOCK;
78
        else
79
            lockflags |= LOCKED_CAPSLOCK;
80
        break;
1875 jermar 81
    default:
508 jermar 82
        break;
83
    }
84
    spinlock_unlock(&keylock);
85
}
86
 
87
/** Process keypress.
88
 *
89
 * @param sc Scancode of the key being pressed.
90
 */
1780 jermar 91
void key_pressed(uint8_t sc)
508 jermar 92
{
93
    char *map = sc_primary_map;
94
    char ascii = sc_primary_map[sc];
95
    bool shift, capslock;
96
    bool letter = false;
97
 
98
    spinlock_lock(&keylock);
99
    switch (sc) {
601 palkovsky 100
    case SC_LSHIFT:
101
    case SC_RSHIFT:
508 jermar 102
            keyflags |= PRESSED_SHIFT;
103
        break;
601 palkovsky 104
    case SC_CAPSLOCK:
508 jermar 105
        keyflags |= PRESSED_CAPSLOCK;
106
        break;
601 palkovsky 107
    case SC_SPEC_ESCAPE:
108
        break;
109
    case SC_LEFTARR:
110
        chardev_push_character(&kbrd, 0x1b);
111
        chardev_push_character(&kbrd, 0x5b);
112
        chardev_push_character(&kbrd, 0x44);
113
        break;
114
    case SC_RIGHTARR:
115
        chardev_push_character(&kbrd, 0x1b);
116
        chardev_push_character(&kbrd, 0x5b);
117
        chardev_push_character(&kbrd, 0x43);
118
        break;
119
    case SC_UPARR:
120
        chardev_push_character(&kbrd, 0x1b);
121
        chardev_push_character(&kbrd, 0x5b);
122
        chardev_push_character(&kbrd, 0x41);
123
        break;
124
    case SC_DOWNARR:
125
        chardev_push_character(&kbrd, 0x1b);
126
        chardev_push_character(&kbrd, 0x5b);
127
        chardev_push_character(&kbrd, 0x42);
128
        break;
606 palkovsky 129
    case SC_HOME:
130
        chardev_push_character(&kbrd, 0x1b);
131
        chardev_push_character(&kbrd, 0x4f);
132
        chardev_push_character(&kbrd, 0x48);
133
        break;
134
    case SC_END:
135
        chardev_push_character(&kbrd, 0x1b);
136
        chardev_push_character(&kbrd, 0x4f);
137
        chardev_push_character(&kbrd, 0x46);
138
        break;
139
    case SC_DELETE:
140
        chardev_push_character(&kbrd, 0x1b);
141
        chardev_push_character(&kbrd, 0x5b);
142
        chardev_push_character(&kbrd, 0x33);
143
        chardev_push_character(&kbrd, 0x7e);
144
        break;
601 palkovsky 145
    default:
517 jermar 146
            letter = is_lower(ascii);
508 jermar 147
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
148
        shift = keyflags & PRESSED_SHIFT;
149
        if (letter && capslock)
150
            shift = !shift;
151
        if (shift)
152
            map = sc_secondary_map;
511 jermar 153
        chardev_push_character(&kbrd, map[sc]);
508 jermar 154
        break;
155
    }
156
    spinlock_unlock(&keylock);
157
}
511 jermar 158
 
1843 jermar 159
uint8_t active_read_buff_read(void)
511 jermar 160
{
878 vana 161
    static int i=0;
894 jermar 162
    i &= (ACTIVE_READ_BUFF_SIZE-1);
163
    if(!active_read_buff[i]) {
878 vana 164
        return 0;
165
    }
166
    return active_read_buff[i++];
167
}
168
 
1843 jermar 169
void active_read_buff_write(uint8_t ch)
878 vana 170
{
171
    static int i=0;
894 jermar 172
    active_read_buff[i] = ch;
878 vana 173
    i++;
894 jermar 174
    i &= (ACTIVE_READ_BUFF_SIZE-1);
878 vana 175
    active_read_buff[i]=0;
176
}
177
 
178
 
1843 jermar 179
void active_read_key_pressed(uint8_t sc)
878 vana 180
{
181
    char *map = sc_primary_map;
182
    char ascii = sc_primary_map[sc];
183
    bool shift, capslock;
184
    bool letter = false;
185
 
186
    /*spinlock_lock(&keylock);*/
187
    switch (sc) {
188
    case SC_LSHIFT:
189
    case SC_RSHIFT:
190
            keyflags |= PRESSED_SHIFT;
191
        break;
192
    case SC_CAPSLOCK:
193
        keyflags |= PRESSED_CAPSLOCK;
194
        break;
195
    case SC_SPEC_ESCAPE:
196
        break;
197
    case SC_LEFTARR:
198
        active_read_buff_write(0x1b);
199
        active_read_buff_write(0x5b);
200
        active_read_buff_write(0x44);
201
        break;
202
    case SC_RIGHTARR:
203
        active_read_buff_write(0x1b);
204
        active_read_buff_write(0x5b);
205
        active_read_buff_write(0x43);
206
        break;
207
    case SC_UPARR:
208
        active_read_buff_write(0x1b);
209
        active_read_buff_write(0x5b);
210
        active_read_buff_write(0x41);
211
        break;
212
    case SC_DOWNARR:
213
        active_read_buff_write(0x1b);
214
        active_read_buff_write(0x5b);
215
        active_read_buff_write(0x42);
216
        break;
217
    case SC_HOME:
218
        active_read_buff_write(0x1b);
219
        active_read_buff_write(0x4f);
220
        active_read_buff_write(0x48);
221
        break;
222
    case SC_END:
223
        active_read_buff_write(0x1b);
224
        active_read_buff_write(0x4f);
225
        active_read_buff_write(0x46);
226
        break;
227
    case SC_DELETE:
228
        active_read_buff_write(0x1b);
229
        active_read_buff_write(0x5b);
230
        active_read_buff_write(0x33);
231
        active_read_buff_write(0x7e);
232
        break;
233
    default:
234
            letter = is_lower(ascii);
235
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
236
        shift = keyflags & PRESSED_SHIFT;
237
        if (letter && capslock)
238
            shift = !shift;
239
        if (shift)
240
            map = sc_secondary_map;
241
        active_read_buff_write(map[sc]);
242
        break;
243
    }
244
    /*spinlock_unlock(&keylock);*/
245
 
246
}
247
 
1754 jermar 248
/** @}
1702 cejka 249
 */