Subversion Repositories HelenOS

Rev

Rev 4348 | 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
 
4691 svoboda 48
#ifdef CONFIG_MAC_KBD
49
#include <genarch/kbrd/scanc_mac.h>
50
#endif
51
 
508 jermar 52
#include <synch/spinlock.h>
1843 jermar 53
#include <console/chardev.h>
4042 jermar 54
#include <console/console.h>
55
#include <proc/thread.h>
56
#include <arch.h>
517 jermar 57
#include <macros.h>
1 jermar 58
 
4348 svoboda 59
#define IGNORE_CODE  0x7f
4092 decky 60
#define KEY_RELEASE  0x80
878 vana 61
 
4092 decky 62
#define PRESSED_SHIFT     (1 << 0)
63
#define PRESSED_CAPSLOCK  (1 << 1)
64
#define LOCKED_CAPSLOCK   (1 << 0)
1896 jermar 65
 
4348 svoboda 66
static indev_operations_t kbrd_raw_ops = {
4092 decky 67
    .poll = NULL
4042 jermar 68
};
69
 
508 jermar 70
/** Process release of key.
71
 *
72
 * @param sc Scancode of the key being released.
73
 */
4348 svoboda 74
static void key_released(kbrd_instance_t *instance, wchar_t sc)
508 jermar 75
{
4348 svoboda 76
    spinlock_lock(&instance->keylock);
77
 
508 jermar 78
    switch (sc) {
1875 jermar 79
    case SC_LSHIFT:
80
    case SC_RSHIFT:
4348 svoboda 81
        instance->keyflags &= ~PRESSED_SHIFT;
508 jermar 82
        break;
1875 jermar 83
    case SC_CAPSLOCK:
4348 svoboda 84
        instance->keyflags &= ~PRESSED_CAPSLOCK;
85
        if (instance->lockflags & LOCKED_CAPSLOCK)
86
            instance->lockflags &= ~LOCKED_CAPSLOCK;
508 jermar 87
        else
4348 svoboda 88
            instance->lockflags |= LOCKED_CAPSLOCK;
508 jermar 89
        break;
1875 jermar 90
    default:
508 jermar 91
        break;
92
    }
4348 svoboda 93
 
94
    spinlock_unlock(&instance->keylock);
508 jermar 95
}
96
 
97
/** Process keypress.
98
 *
99
 * @param sc Scancode of the key being pressed.
100
 */
4348 svoboda 101
static void key_pressed(kbrd_instance_t *instance, wchar_t sc)
508 jermar 102
{
4348 svoboda 103
    bool letter;
104
    bool shift;
105
    bool capslock;
4092 decky 106
 
4348 svoboda 107
    spinlock_lock(&instance->keylock);
108
 
508 jermar 109
    switch (sc) {
601 palkovsky 110
    case SC_LSHIFT:
111
    case SC_RSHIFT:
4348 svoboda 112
        instance->keyflags |= PRESSED_SHIFT;
508 jermar 113
        break;
601 palkovsky 114
    case SC_CAPSLOCK:
4348 svoboda 115
        instance->keyflags |= PRESSED_CAPSLOCK;
508 jermar 116
        break;
4348 svoboda 117
    case SC_SCAN_ESCAPE:
601 palkovsky 118
        break;
119
    default:
4348 svoboda 120
        letter = islower(sc_primary_map[sc]);
121
        shift = instance->keyflags & PRESSED_SHIFT;
122
        capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
123
            (instance->lockflags & LOCKED_CAPSLOCK);
124
 
125
        if ((letter) && (capslock))
508 jermar 126
            shift = !shift;
4348 svoboda 127
 
508 jermar 128
        if (shift)
4348 svoboda 129
            indev_push_character(instance->sink, sc_secondary_map[sc]);
130
        else
131
            indev_push_character(instance->sink, sc_primary_map[sc]);
508 jermar 132
        break;
133
    }
4348 svoboda 134
 
135
    spinlock_unlock(&instance->keylock);
508 jermar 136
}
511 jermar 137
 
4348 svoboda 138
static void kkbrd(void *arg)
139
{
140
    kbrd_instance_t *instance = (kbrd_instance_t *) arg;
141
 
142
    while (true) {
143
        wchar_t sc = indev_pop_character(&instance->raw);
144
 
145
        if (sc == IGNORE_CODE)
146
            continue;
147
 
148
        if (sc & KEY_RELEASE)
149
            key_released(instance, (sc ^ KEY_RELEASE) & 0x7f);
150
        else
151
            key_pressed(instance, sc & 0x7f);
152
    }
153
}
154
 
155
kbrd_instance_t *kbrd_init(void)
156
{
157
    kbrd_instance_t *instance
158
        = malloc(sizeof(kbrd_instance_t), FRAME_ATOMIC);
159
    if (instance) {
160
        instance->thread
161
            = thread_create(kkbrd, (void *) instance, TASK, 0, "kkbrd", false);
162
 
163
        if (!instance->thread) {
164
            free(instance);
165
            return NULL;
166
        }
167
 
168
        instance->sink = NULL;
169
        indev_initialize("kbrd", &instance->raw, &kbrd_raw_ops);
170
 
171
        spinlock_initialize(&instance->keylock, "instance_keylock");
172
        instance->keyflags = 0;
173
        instance->lockflags = 0;
174
    }
175
 
176
    return instance;
177
}
178
 
179
indev_t *kbrd_wire(kbrd_instance_t *instance, indev_t *sink)
180
{
181
    ASSERT(instance);
182
    ASSERT(sink);
183
 
184
    instance->sink = sink;
185
    thread_ready(instance->thread);
186
 
187
    return &instance->raw;
188
}
189
 
1754 jermar 190
/** @}
1702 cejka 191
 */