Subversion Repositories HelenOS

Rev

Rev 2071 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2071 Rev 2089
1
/*
1
/*
2
 * Copyright (c) 2006 Jakub Jermar
2
 * Copyright (c) 2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup genarch
29
/** @addtogroup genarch
30
 * @{
30
 * @{
31
 */
31
 */
32
/**
32
/**
33
 * @file
33
 * @file
34
 * @brief   Key processing.
34
 * @brief   Key processing.
35
 */
35
 */
36
 
36
 
37
#include <genarch/kbd/key.h>
37
#include <genarch/kbd/key.h>
38
#include <genarch/kbd/scanc.h>
38
#include <genarch/kbd/scanc.h>
39
#ifdef CONFIG_I8042
39
#ifdef CONFIG_I8042
40
#include <genarch/kbd/scanc_pc.h>
40
#include <genarch/kbd/scanc_pc.h>
41
#endif
41
#endif
42
#if (defined(CONFIG_Z8530) || defined(CONFIG_NS16550))
42
#if (defined(CONFIG_Z8530) || defined(CONFIG_NS16550))
43
#include <genarch/kbd/scanc_sun.h>
43
#include <genarch/kbd/scanc_sun.h>
44
#endif
44
#endif
45
#include <synch/spinlock.h>
45
#include <synch/spinlock.h>
46
#include <console/chardev.h>
46
#include <console/chardev.h>
47
#include <typedefs.h>
-
 
48
#include <macros.h>
47
#include <macros.h>
49
 
48
 
50
#define PRESSED_SHIFT       (1<<0)
49
#define PRESSED_SHIFT       (1<<0)
51
#define PRESSED_CAPSLOCK    (1<<1)
50
#define PRESSED_CAPSLOCK    (1<<1)
52
#define LOCKED_CAPSLOCK     (1<<0)
51
#define LOCKED_CAPSLOCK     (1<<0)
53
 
52
 
54
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
53
#define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
55
 
54
 
56
chardev_t kbrd;
55
chardev_t kbrd;
57
 
56
 
58
static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
57
static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
59
 
58
 
60
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
59
SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
61
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
60
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
62
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
61
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
63
 
62
 
64
/** Process release of key.
63
/** Process release of key.
65
 *
64
 *
66
 * @param sc Scancode of the key being released.
65
 * @param sc Scancode of the key being released.
67
 */
66
 */
68
void key_released(uint8_t sc)
67
void key_released(uint8_t sc)
69
{
68
{
70
    spinlock_lock(&keylock);
69
    spinlock_lock(&keylock);
71
    switch (sc) {
70
    switch (sc) {
72
    case SC_LSHIFT:
71
    case SC_LSHIFT:
73
    case SC_RSHIFT:
72
    case SC_RSHIFT:
74
        keyflags &= ~PRESSED_SHIFT;
73
        keyflags &= ~PRESSED_SHIFT;
75
        break;
74
        break;
76
    case SC_CAPSLOCK:
75
    case SC_CAPSLOCK:
77
        keyflags &= ~PRESSED_CAPSLOCK;
76
        keyflags &= ~PRESSED_CAPSLOCK;
78
        if (lockflags & LOCKED_CAPSLOCK)
77
        if (lockflags & LOCKED_CAPSLOCK)
79
            lockflags &= ~LOCKED_CAPSLOCK;
78
            lockflags &= ~LOCKED_CAPSLOCK;
80
        else
79
        else
81
            lockflags |= LOCKED_CAPSLOCK;
80
            lockflags |= LOCKED_CAPSLOCK;
82
        break;
81
        break;
83
    default:
82
    default:
84
        break;
83
        break;
85
    }
84
    }
86
    spinlock_unlock(&keylock);
85
    spinlock_unlock(&keylock);
87
}
86
}
88
 
87
 
89
/** Process keypress.
88
/** Process keypress.
90
 *
89
 *
91
 * @param sc Scancode of the key being pressed.
90
 * @param sc Scancode of the key being pressed.
92
 */
91
 */
93
void key_pressed(uint8_t sc)
92
void key_pressed(uint8_t sc)
94
{
93
{
95
    char *map = sc_primary_map;
94
    char *map = sc_primary_map;
96
    char ascii = sc_primary_map[sc];
95
    char ascii = sc_primary_map[sc];
97
    bool shift, capslock;
96
    bool shift, capslock;
98
    bool letter = false;
97
    bool letter = false;
99
 
98
 
100
    spinlock_lock(&keylock);
99
    spinlock_lock(&keylock);
101
    switch (sc) {
100
    switch (sc) {
102
    case SC_LSHIFT:
101
    case SC_LSHIFT:
103
    case SC_RSHIFT:
102
    case SC_RSHIFT:
104
            keyflags |= PRESSED_SHIFT;
103
            keyflags |= PRESSED_SHIFT;
105
        break;
104
        break;
106
    case SC_CAPSLOCK:
105
    case SC_CAPSLOCK:
107
        keyflags |= PRESSED_CAPSLOCK;
106
        keyflags |= PRESSED_CAPSLOCK;
108
        break;
107
        break;
109
    case SC_SPEC_ESCAPE:
108
    case SC_SPEC_ESCAPE:
110
        break;
109
        break;
111
    case SC_LEFTARR:
110
    case SC_LEFTARR:
112
        chardev_push_character(&kbrd, 0x1b);
111
        chardev_push_character(&kbrd, 0x1b);
113
        chardev_push_character(&kbrd, 0x5b);
112
        chardev_push_character(&kbrd, 0x5b);
114
        chardev_push_character(&kbrd, 0x44);
113
        chardev_push_character(&kbrd, 0x44);
115
        break;
114
        break;
116
    case SC_RIGHTARR:
115
    case SC_RIGHTARR:
117
        chardev_push_character(&kbrd, 0x1b);
116
        chardev_push_character(&kbrd, 0x1b);
118
        chardev_push_character(&kbrd, 0x5b);
117
        chardev_push_character(&kbrd, 0x5b);
119
        chardev_push_character(&kbrd, 0x43);
118
        chardev_push_character(&kbrd, 0x43);
120
        break;
119
        break;
121
    case SC_UPARR:
120
    case SC_UPARR:
122
        chardev_push_character(&kbrd, 0x1b);
121
        chardev_push_character(&kbrd, 0x1b);
123
        chardev_push_character(&kbrd, 0x5b);
122
        chardev_push_character(&kbrd, 0x5b);
124
        chardev_push_character(&kbrd, 0x41);
123
        chardev_push_character(&kbrd, 0x41);
125
        break;
124
        break;
126
    case SC_DOWNARR:
125
    case SC_DOWNARR:
127
        chardev_push_character(&kbrd, 0x1b);
126
        chardev_push_character(&kbrd, 0x1b);
128
        chardev_push_character(&kbrd, 0x5b);
127
        chardev_push_character(&kbrd, 0x5b);
129
        chardev_push_character(&kbrd, 0x42);
128
        chardev_push_character(&kbrd, 0x42);
130
        break;
129
        break;
131
    case SC_HOME:
130
    case SC_HOME:
132
        chardev_push_character(&kbrd, 0x1b);
131
        chardev_push_character(&kbrd, 0x1b);
133
        chardev_push_character(&kbrd, 0x4f);
132
        chardev_push_character(&kbrd, 0x4f);
134
        chardev_push_character(&kbrd, 0x48);
133
        chardev_push_character(&kbrd, 0x48);
135
        break;
134
        break;
136
    case SC_END:
135
    case SC_END:
137
        chardev_push_character(&kbrd, 0x1b);
136
        chardev_push_character(&kbrd, 0x1b);
138
        chardev_push_character(&kbrd, 0x4f);
137
        chardev_push_character(&kbrd, 0x4f);
139
        chardev_push_character(&kbrd, 0x46);
138
        chardev_push_character(&kbrd, 0x46);
140
        break;
139
        break;
141
    case SC_DELETE:
140
    case SC_DELETE:
142
        chardev_push_character(&kbrd, 0x1b);
141
        chardev_push_character(&kbrd, 0x1b);
143
        chardev_push_character(&kbrd, 0x5b);
142
        chardev_push_character(&kbrd, 0x5b);
144
        chardev_push_character(&kbrd, 0x33);
143
        chardev_push_character(&kbrd, 0x33);
145
        chardev_push_character(&kbrd, 0x7e);
144
        chardev_push_character(&kbrd, 0x7e);
146
        break;
145
        break;
147
    default:
146
    default:
148
            letter = is_lower(ascii);
147
            letter = is_lower(ascii);
149
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
148
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
150
        shift = keyflags & PRESSED_SHIFT;
149
        shift = keyflags & PRESSED_SHIFT;
151
        if (letter && capslock)
150
        if (letter && capslock)
152
            shift = !shift;
151
            shift = !shift;
153
        if (shift)
152
        if (shift)
154
            map = sc_secondary_map;
153
            map = sc_secondary_map;
155
        chardev_push_character(&kbrd, map[sc]);
154
        chardev_push_character(&kbrd, map[sc]);
156
        break;
155
        break;
157
    }
156
    }
158
    spinlock_unlock(&keylock);
157
    spinlock_unlock(&keylock);
159
}
158
}
160
 
159
 
161
uint8_t active_read_buff_read(void)
160
uint8_t active_read_buff_read(void)
162
{
161
{
163
    static int i=0;
162
    static int i=0;
164
    i &= (ACTIVE_READ_BUFF_SIZE-1);
163
    i &= (ACTIVE_READ_BUFF_SIZE-1);
165
    if(!active_read_buff[i]) {
164
    if(!active_read_buff[i]) {
166
        return 0;
165
        return 0;
167
    }
166
    }
168
    return active_read_buff[i++];
167
    return active_read_buff[i++];
169
}
168
}
170
 
169
 
171
void active_read_buff_write(uint8_t ch)
170
void active_read_buff_write(uint8_t ch)
172
{
171
{
173
    static int i=0;
172
    static int i=0;
174
    active_read_buff[i] = ch;
173
    active_read_buff[i] = ch;
175
    i++;
174
    i++;
176
    i &= (ACTIVE_READ_BUFF_SIZE-1);
175
    i &= (ACTIVE_READ_BUFF_SIZE-1);
177
    active_read_buff[i]=0;
176
    active_read_buff[i]=0;
178
}
177
}
179
 
178
 
180
 
179
 
181
void active_read_key_pressed(uint8_t sc)
180
void active_read_key_pressed(uint8_t sc)
182
{
181
{
183
    char *map = sc_primary_map;
182
    char *map = sc_primary_map;
184
    char ascii = sc_primary_map[sc];
183
    char ascii = sc_primary_map[sc];
185
    bool shift, capslock;
184
    bool shift, capslock;
186
    bool letter = false;
185
    bool letter = false;
187
 
186
 
188
    /*spinlock_lock(&keylock);*/
187
    /*spinlock_lock(&keylock);*/
189
    switch (sc) {
188
    switch (sc) {
190
    case SC_LSHIFT:
189
    case SC_LSHIFT:
191
    case SC_RSHIFT:
190
    case SC_RSHIFT:
192
            keyflags |= PRESSED_SHIFT;
191
            keyflags |= PRESSED_SHIFT;
193
        break;
192
        break;
194
    case SC_CAPSLOCK:
193
    case SC_CAPSLOCK:
195
        keyflags |= PRESSED_CAPSLOCK;
194
        keyflags |= PRESSED_CAPSLOCK;
196
        break;
195
        break;
197
    case SC_SPEC_ESCAPE:
196
    case SC_SPEC_ESCAPE:
198
        break;
197
        break;
199
    case SC_LEFTARR:
198
    case SC_LEFTARR:
200
        active_read_buff_write(0x1b);
199
        active_read_buff_write(0x1b);
201
        active_read_buff_write(0x5b);
200
        active_read_buff_write(0x5b);
202
        active_read_buff_write(0x44);
201
        active_read_buff_write(0x44);
203
        break;
202
        break;
204
    case SC_RIGHTARR:
203
    case SC_RIGHTARR:
205
        active_read_buff_write(0x1b);
204
        active_read_buff_write(0x1b);
206
        active_read_buff_write(0x5b);
205
        active_read_buff_write(0x5b);
207
        active_read_buff_write(0x43);
206
        active_read_buff_write(0x43);
208
        break;
207
        break;
209
    case SC_UPARR:
208
    case SC_UPARR:
210
        active_read_buff_write(0x1b);
209
        active_read_buff_write(0x1b);
211
        active_read_buff_write(0x5b);
210
        active_read_buff_write(0x5b);
212
        active_read_buff_write(0x41);
211
        active_read_buff_write(0x41);
213
        break;
212
        break;
214
    case SC_DOWNARR:
213
    case SC_DOWNARR:
215
        active_read_buff_write(0x1b);
214
        active_read_buff_write(0x1b);
216
        active_read_buff_write(0x5b);
215
        active_read_buff_write(0x5b);
217
        active_read_buff_write(0x42);
216
        active_read_buff_write(0x42);
218
        break;
217
        break;
219
    case SC_HOME:
218
    case SC_HOME:
220
        active_read_buff_write(0x1b);
219
        active_read_buff_write(0x1b);
221
        active_read_buff_write(0x4f);
220
        active_read_buff_write(0x4f);
222
        active_read_buff_write(0x48);
221
        active_read_buff_write(0x48);
223
        break;
222
        break;
224
    case SC_END:
223
    case SC_END:
225
        active_read_buff_write(0x1b);
224
        active_read_buff_write(0x1b);
226
        active_read_buff_write(0x4f);
225
        active_read_buff_write(0x4f);
227
        active_read_buff_write(0x46);
226
        active_read_buff_write(0x46);
228
        break;
227
        break;
229
    case SC_DELETE:
228
    case SC_DELETE:
230
        active_read_buff_write(0x1b);
229
        active_read_buff_write(0x1b);
231
        active_read_buff_write(0x5b);
230
        active_read_buff_write(0x5b);
232
        active_read_buff_write(0x33);
231
        active_read_buff_write(0x33);
233
        active_read_buff_write(0x7e);
232
        active_read_buff_write(0x7e);
234
        break;
233
        break;
235
    default:
234
    default:
236
            letter = is_lower(ascii);
235
            letter = is_lower(ascii);
237
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
236
        capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
238
        shift = keyflags & PRESSED_SHIFT;
237
        shift = keyflags & PRESSED_SHIFT;
239
        if (letter && capslock)
238
        if (letter && capslock)
240
            shift = !shift;
239
            shift = !shift;
241
        if (shift)
240
        if (shift)
242
            map = sc_secondary_map;
241
            map = sc_secondary_map;
243
        active_read_buff_write(map[sc]);
242
        active_read_buff_write(map[sc]);
244
        break;
243
        break;
245
    }
244
    }
246
    /*spinlock_unlock(&keylock);*/
245
    /*spinlock_unlock(&keylock);*/
247
 
246
 
248
}
247
}
249
 
248
 
250
/** @}
249
/** @}
251
 */
250
 */
252
 
251