Subversion Repositories HelenOS

Rev

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