X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=hw_keyboard.c;h=89f91ec00557c8c90abca944126e4d05a3205225;hb=a4194ad128ad2b396f3b483da1b48a7a9dedda2f;hp=428971d5a6a6ecfcb51c0e19e88d3ed024c23ab6;hpb=8b552fe61db48874f043bccfb589e5444509444c;p=dcpu16 diff --git a/hw_keyboard.c b/hw_keyboard.c index 428971d..89f91ec 100644 --- a/hw_keyboard.c +++ b/hw_keyboard.c @@ -18,6 +18,15 @@ #define VOIDP(__x__) (__x__) #endif +#define MSG_(__level__, __vm__, ...) do { ((__vm__) ? ((struct dcpu16 *)(__vm__))->msg_cb_ : dcpu16_msg_)(__level__, __VA_ARGS__); } while (0) +#define MSG_INFO(__vm__, ...) MSG_(DCPU16_MSG_INFO, __vm__, __VA_ARGS__) +#define MSG_ERROR(__vm__, ...) MSG_(DCPU16_MSG_ERROR, __vm__, __VA_ARGS__) +#ifdef DEBUG +#define MSG_DEBUG(__vm__, ...) MSG_(DCPU16_MSG_DEBUG, __vm__, __VA_ARGS__) +#else /* DEBUG */ +#define MSG_DEBUG(__vm__, ...) do { } while (0) +#endif /* DEBUG */ + struct keyboard_ { char *buf; size_t buf_sz; @@ -70,24 +79,17 @@ void keyboard_rfbevent_(rfbBool down, rfbKeySym key, rfbClientPtr cl) { struct dcpu16_hw *hw = (struct dcpu16_hw *)cl->screen->screenData; struct keyboard_ *keyboard = (struct keyboard_ *)hw->data; - keysym_rfbtodcpu(key, &dcpu_key); - - fprintf(stderr, "%s: down:%u key:0x%04x dcpu_key:0x%04x\n", - __func__, - down, - key, - dcpu_key); + MSG_DEBUG(hw->vm, "%s>> down:%u rfb_key:0x%04x", down, key); - fprintf(stderr, "%s: hw:%p name:%s vm:%p\n", - __func__, - VOIDP(hw), - hw->name_, - VOIDP(hw->vm)); + if (keysym_rfbtodcpu(key, &dcpu_key)) { + /* unhandled key event */ + return; + } keyboard->keys_pressed[dcpu_key] = (down ? 1 : 0); if (down) { if ((keyboard->buf_tail + 1) % keyboard->buf_sz == keyboard->buf_head) { - hw->vm->warn_cb_("keyboard buffer overflow"); + MSG_INFO(hw->vm, "keyboard buffer overflow"); return; } keyboard->buf[keyboard->buf_tail] = dcpu_key; @@ -98,11 +100,6 @@ void keyboard_rfbevent_(rfbBool down, rfbKeySym key, rfbClientPtr cl) { dcpu16_interrupt(hw->vm, keyboard->interrupt_message); } } - -void keyboard_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr rfbScreen) { - rfbScreen->screenData = hw; - rfbScreen->kbdAddEvent = keyboard_rfbevent_; -} #endif /* HAVE_LIBVNCSERVER */ static @@ -112,7 +109,11 @@ void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) { (void)vm; keyboard->interrupt_message = 0; - memset(keyboard->buf, 0, keyboard->buf_sz); + keyboard->buf_head = 0; + keyboard->buf_tail = 0; + memset(keyboard->keys_pressed, 0, sizeof keyboard->keys_pressed); + + MSG_DEBUG(vm, "%s>>", __func__); } static @@ -133,6 +134,8 @@ void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { memset(keyboard->buf, 0, keyboard->buf_sz); keyboard->buf_head = 0; keyboard->buf_tail = 0; + + MSG_DEBUG(vm, "%s>> buffer cleared", __func__); break; case 1: /* get next key from buffer as C */ @@ -141,14 +144,20 @@ void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) { vm->reg[DCPU16_REG_C] = keyboard->buf[keyboard->buf_head]; keyboard->buf_head += 1; keyboard->buf_head %= keyboard->buf_sz; + + MSG_DEBUG(vm, "%s>> next key: %u", __func__, vm->reg[DCPU16_REG_C]); break; case 2: /* get currently-pressed-state of key in B as C */ vm->reg[DCPU16_REG_C] = keyboard->keys_pressed[reg_b & 0x00ff]; + + MSG_DEBUG(vm, "%s>> state of key 0x%02x: %spressed", __func__, reg_b & 0x00ff, keyboard->keys_pressed[reg_b & 0x00ff] ? "" : "not "); break; case 3: /* set interrupt state */ keyboard->interrupt_message = reg_b; + + MSG_DEBUG(vm, "%s>> interrupt_message:0x%04x", __func__, reg_b); break; } @@ -160,13 +169,13 @@ int keyboard_data_init_(struct dcpu16_hw *hw, void *data) { hw->data = calloc(1, sizeof(struct keyboard_)); if (hw->data == NULL) { - hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno)); + MSG_ERROR(hw->vm, "%s():%s", "calloc", strerror(errno)); return -1; } ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf); if (((struct keyboard_ *)(hw->data))->buf == NULL) { - hw->vm->warn_cb_("%s():%s", "malloc", strerror(errno)); + MSG_ERROR(hw->vm, "%s():%s", "malloc", strerror(errno)); free(hw->data); hw->data = NULL; return -1; @@ -191,9 +200,65 @@ void keyboard_data_free_(struct dcpu16_hw *hw) { } } -static struct dcpu16_hw hw_ = { - .vm = NULL, +static struct dcpu16_hw_ctl_cmd ctl_[] = { + { "buffer_size", "const size_t *", "size_t *", "get or set current buffer size" }, + { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this keyboard instance with an rfb display" }, + { NULL, NULL, NULL, NULL } +}; +static +int keyboard_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) { + if (strcmp(cmd, "buffer_size") == 0) { + struct keyboard_ *keyboard = (struct keyboard_ *)hw->data; + void *tmp_ptr; + const size_t *buf_sz_in = (const size_t *)data_in; + size_t *buf_sz_out = (size_t *)data_out; + + if (buf_sz_out) { + *buf_sz_out = keyboard->buf_sz; + } + + if (buf_sz_in) { + MSG_DEBUG(hw->vm, "%s>> resizing buffer from %zu to %zu", __func__, keyboard->buf_sz, *buf_sz_in); + + tmp_ptr = realloc(keyboard->buf, *buf_sz_in); + if (tmp_ptr == NULL) { + MSG_ERROR(hw->vm, "%s():%s", "realloc", strerror(errno)); + return -1; + } + keyboard->buf = tmp_ptr; + keyboard->buf_sz = *buf_sz_in; + keyboard->buf_head = keyboard->buf_tail = 0; + } + + MSG_DEBUG(hw->vm, "%s>> %s now:%zu was:%zu", __func__, "buffer_size", *buf_sz_in, *buf_sz_out); + + return 0; + } + +#ifdef HAVE_LIBVNCSERVER + if (strcmp(cmd, "associate_rfbScreen") == 0) { + rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_in; + (void)data_out; + + if (rfbScreen == NULL) + return -EFAULT; + + rfbScreen->screenData = hw; + rfbScreen->kbdAddEvent = keyboard_rfbevent_; + + MSG_DEBUG(hw->vm, "%s>> %s rfbScreen:%p", __func__, "associate_rfbScreen", rfbScreen); + + return 0; + } +#endif /* HAVE_LIBVNCSERVER */ + + return -EINVAL; +} + + +struct dcpu16_hw_module dcpu16_hw_module_keyboard = { .name_ = "Generic Keyboard (compatible)", + .id_l = 0x7406, .id_h = 0x30cf, .ver = 0x0001, @@ -202,11 +267,9 @@ static struct dcpu16_hw hw_ = { .hwi = keyboard_hwi_, .cycle = keyboard_cycle_, .reset = keyboard_reset_, - .data = (struct keyboard_ *)NULL -}; -struct dcpu16_hw_module dcpu16_hw_module_keyboard = { - .template = &hw_, .data_init = keyboard_data_init_, .data_free = keyboard_data_free_, + .ctl = keyboard_data_ctl_, + .ctl_cmd = ctl_, };