Subversion Repositories HelenOS

Rev

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

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