Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4347 → Rev 4348

/branches/dynload/kernel/genarch/src/kbrd/kbrd.c
52,10 → 52,7
#include <arch.h>
#include <macros.h>
 
#ifdef CONFIG_SUN_KBD
#define IGNORE_CODE 0x7f
#endif
 
#define IGNORE_CODE 0x7f
#define KEY_RELEASE 0x80
 
#define PRESSED_SHIFT (1 << 0)
62,73 → 59,35
#define PRESSED_CAPSLOCK (1 << 1)
#define LOCKED_CAPSLOCK (1 << 0)
 
static indev_t kbrdout;
 
indev_operations_t kbrdout_ops = {
static indev_operations_t kbrd_raw_ops = {
.poll = NULL
};
 
SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */
static volatile int keyflags; /**< Tracking of multiple keypresses. */
static volatile int lockflags; /**< Tracking of multiple keys lockings. */
 
static void key_released(uint8_t);
static void key_pressed(uint8_t);
 
static void kkbrd(void *arg)
{
indev_t *in = (indev_t *) arg;
while (true) {
uint8_t sc = _getc(in);
#ifdef CONFIG_SUN_KBD
if (sc == IGNORE_CODE)
continue;
#endif
if (sc & KEY_RELEASE)
key_released(sc ^ KEY_RELEASE);
else
key_pressed(sc);
}
}
 
void kbrd_init(indev_t *devin)
{
indev_initialize("kbrd", &kbrdout, &kbrdout_ops);
thread_t *thread
= thread_create(kkbrd, devin, TASK, 0, "kkbrd", false);
if (thread) {
stdin = &kbrdout;
thread_ready(thread);
}
}
 
/** Process release of key.
*
* @param sc Scancode of the key being released.
*/
void key_released(uint8_t sc)
static void key_released(kbrd_instance_t *instance, wchar_t sc)
{
spinlock_lock(&keylock);
spinlock_lock(&instance->keylock);
switch (sc) {
case SC_LSHIFT:
case SC_RSHIFT:
keyflags &= ~PRESSED_SHIFT;
instance->keyflags &= ~PRESSED_SHIFT;
break;
case SC_CAPSLOCK:
keyflags &= ~PRESSED_CAPSLOCK;
if (lockflags & LOCKED_CAPSLOCK)
lockflags &= ~LOCKED_CAPSLOCK;
instance->keyflags &= ~PRESSED_CAPSLOCK;
if (instance->lockflags & LOCKED_CAPSLOCK)
instance->lockflags &= ~LOCKED_CAPSLOCK;
else
lockflags |= LOCKED_CAPSLOCK;
instance->lockflags |= LOCKED_CAPSLOCK;
break;
default:
break;
}
spinlock_unlock(&keylock);
spinlock_unlock(&instance->keylock);
}
 
/** Process keypress.
135,74 → 94,94
*
* @param sc Scancode of the key being pressed.
*/
void key_pressed(uint8_t sc)
static void key_pressed(kbrd_instance_t *instance, wchar_t sc)
{
char *map = sc_primary_map;
char ascii = sc_primary_map[sc];
bool shift, capslock;
bool letter = false;
bool letter;
bool shift;
bool capslock;
spinlock_lock(&keylock);
spinlock_lock(&instance->keylock);
switch (sc) {
case SC_LSHIFT:
case SC_RSHIFT:
keyflags |= PRESSED_SHIFT;
instance->keyflags |= PRESSED_SHIFT;
break;
case SC_CAPSLOCK:
keyflags |= PRESSED_CAPSLOCK;
instance->keyflags |= PRESSED_CAPSLOCK;
break;
case SC_SPEC_ESCAPE:
case SC_SCAN_ESCAPE:
break;
case SC_LEFTARR:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x5b);
indev_push_character(stdin, 0x44);
break;
case SC_RIGHTARR:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x5b);
indev_push_character(stdin, 0x43);
break;
case SC_UPARR:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x5b);
indev_push_character(stdin, 0x41);
break;
case SC_DOWNARR:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x5b);
indev_push_character(stdin, 0x42);
break;
case SC_HOME:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x4f);
indev_push_character(stdin, 0x48);
break;
case SC_END:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x4f);
indev_push_character(stdin, 0x46);
break;
case SC_DELETE:
indev_push_character(stdin, 0x1b);
indev_push_character(stdin, 0x5b);
indev_push_character(stdin, 0x33);
indev_push_character(stdin, 0x7e);
break;
default:
letter = islower(ascii);
capslock = (keyflags & PRESSED_CAPSLOCK) ||
(lockflags & LOCKED_CAPSLOCK);
shift = keyflags & PRESSED_SHIFT;
if (letter && capslock)
letter = islower(sc_primary_map[sc]);
shift = instance->keyflags & PRESSED_SHIFT;
capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
(instance->lockflags & LOCKED_CAPSLOCK);
if ((letter) && (capslock))
shift = !shift;
if (shift)
map = sc_secondary_map;
indev_push_character(stdin, map[sc]);
indev_push_character(instance->sink, sc_secondary_map[sc]);
else
indev_push_character(instance->sink, sc_primary_map[sc]);
break;
}
spinlock_unlock(&keylock);
spinlock_unlock(&instance->keylock);
}
 
static void kkbrd(void *arg)
{
kbrd_instance_t *instance = (kbrd_instance_t *) arg;
while (true) {
wchar_t sc = indev_pop_character(&instance->raw);
if (sc == IGNORE_CODE)
continue;
if (sc & KEY_RELEASE)
key_released(instance, (sc ^ KEY_RELEASE) & 0x7f);
else
key_pressed(instance, sc & 0x7f);
}
}
 
kbrd_instance_t *kbrd_init(void)
{
kbrd_instance_t *instance
= malloc(sizeof(kbrd_instance_t), FRAME_ATOMIC);
if (instance) {
instance->thread
= thread_create(kkbrd, (void *) instance, TASK, 0, "kkbrd", false);
if (!instance->thread) {
free(instance);
return NULL;
}
instance->sink = NULL;
indev_initialize("kbrd", &instance->raw, &kbrd_raw_ops);
spinlock_initialize(&instance->keylock, "instance_keylock");
instance->keyflags = 0;
instance->lockflags = 0;
}
return instance;
}
 
indev_t *kbrd_wire(kbrd_instance_t *instance, indev_t *sink)
{
ASSERT(instance);
ASSERT(sink);
instance->sink = sink;
thread_ready(instance->thread);
return &instance->raw;
}
 
/** @}
*/