Subversion Repositories HelenOS

Rev

Rev 4296 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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