#include <string.h>
#include <errno.h>
+#ifdef HAVE_LIBVNCSERVER
+#include <rfb/rfb.h>
+#include <rfb/keysym.h>
+#endif /* HAVE_LIBVNCSERVER */
+
#include "dcpu16.h"
#include "hw_keyboard.h"
#define BUF_SZ 32
-static dcpu16_hw_signal_t keyboard_reset_;
-static dcpu16_hw_signal_t keyboard_cycle_;
-static dcpu16_hw_signal_t keyboard_hwi_;
-static struct dcpu16_hw hw_ = {
- .name_ = "Generic Keyboard (compatible)",
- .id_l = 0x7406,
- .id_h = 0x30cf,
- .ver = 0x0001,
- .mfg_l = 0x0000,
- .mfg_h = 0x0000,
- .hwi = keyboard_hwi_,
- .cycle = keyboard_cycle_,
- .reset = keyboard_reset_,
- .data = (struct keyboard_ *)NULL
-};
+#ifdef WANT_VARIADIC_VOIDP_CAST
+#define VOIDP(__x__) ((void *)(__x__))
+#else
+#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;
+ size_t buf_head;
+ size_t buf_tail;
DCPU16_WORD interrupt_message;
+ unsigned char keys_pressed[256];
};
+#ifdef HAVE_LIBVNCSERVER
+
+static const struct keysym_map_ {
+ unsigned int rfb_start;
+ unsigned int rfb_end;
+ DCPU16_WORD dcpu_start;
+} keymap_[] = {
+ { XK_space, XK_asciitilde, 0x20 }, /* ASCII range */
+ { XK_Delete, XK_Delete, 0x7f }, /* ASCII del */
+ { XK_BackSpace, XK_BackSpace, 0x10 }, /* bs */
+ { XK_Return, XK_Return, 0x11 }, /* ret */
+ { XK_Insert, XK_Insert, 0x12 }, /* ins */
+ { XK_Delete, XK_Delete, 0x13 }, /* del */
+ { XK_Up, XK_Up, 0x80 }, /* arrow up */
+ { XK_Down, XK_Down, 0x81 }, /* arrow down */
+ { XK_Left, XK_Left, 0x82 }, /* arrow left */
+ { XK_Right, XK_Right, 0x83 }, /* arrow right */
+ { XK_Shift_L, XK_Shift_R, 0x90 }, /* shift range */
+ { XK_Control_L, XK_Control_R, 0x91 }, /* control range */
+ { 0, 0, 0x0 }
+};
+
+static int keysym_rfbtodcpu(unsigned int rfb, DCPU16_WORD *dcpu) {
+ const struct keysym_map_ *map;
+
+ for (map = keymap_; map->rfb_start; map++) {
+ if (rfb >= map->rfb_start
+ && rfb <= map->rfb_end) {
+ *dcpu = (map->dcpu_start + (rfb - map->rfb_start));
+ return 0;
+ }
+ }
+
+ *dcpu = 0x00;
+ return -1;
+}
+
static
-void keyboard_reset_(struct dcpu16 *vm, void *data) {
- struct keyboard_ *keyboard = (struct keyboard_ *)data;
+void keyboard_rfbevent_(rfbBool down, rfbKeySym key, rfbClientPtr cl) {
+ DCPU16_WORD dcpu_key;
+ struct dcpu16_hw *hw = (struct dcpu16_hw *)cl->screen->screenData;
+ struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
+
+ MSG_DEBUG(hw->vm, "%s>> down:%u rfb_key:0x%04x", down, key);
+
+ 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) {
+ MSG_INFO(hw->vm, "keyboard buffer overflow");
+ return;
+ }
+ keyboard->buf[keyboard->buf_tail] = dcpu_key;
+ keyboard->buf_tail += 1;
+ keyboard->buf_tail %= keyboard->buf_sz;
+ }
+ if (keyboard->interrupt_message) {
+ dcpu16_interrupt(hw->vm, keyboard->interrupt_message);
+ }
+}
+#endif /* HAVE_LIBVNCSERVER */
+
+static
+void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+ struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
(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
-void keyboard_cycle_(struct dcpu16 *vm, void *data) {
- struct keyboard_ *keyboard = (struct keyboard_ *)data;
+void keyboard_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+ struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
(void)vm, (void)keyboard;
}
static
-void keyboard_hwi_(struct dcpu16 *vm, void *data) {
- struct keyboard_ *keyboard = (struct keyboard_ *)data;
+void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+ struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
- size_t i;
switch (reg_a) {
case 0: /* clear keyboard buffer */
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 */
- vm->reg[DCPU16_REG_C] = keyboard->buf[0];
- for (i = 1; i < keyboard->buf_sz; i++) {
- keyboard->buf[i-1] = keyboard->buf[i];
- }
- keyboard->buf[i] = '\0';
+ if (keyboard->buf_head == keyboard->buf_tail)
+ break;
+ 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->warn_cb_("IMPLEMENT");
- (void)reg_b;
- vm->reg[DCPU16_REG_C] = 0;
+ 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;
}
}
-struct dcpu16_hw *keyboard_new(struct dcpu16 *vm) {
- struct dcpu16_hw *hw;
- char *b;
+static
+int keyboard_data_init_(struct dcpu16_hw *hw, void *data) {
+ size_t buf_sz = data ? *(size_t *)data : BUF_SZ;
- hw = calloc(1, sizeof *hw);
- if (hw == NULL) {
- vm->warn_cb_("%s():%s", "calloc", strerror(errno));
- return NULL;
- }
- memcpy(hw, &hw_, sizeof *hw);
- hw->data = calloc(1, sizeof hw->data);
+ hw->data = calloc(1, sizeof(struct keyboard_));
if (hw->data == NULL) {
- vm->warn_cb_("%s():%s", "calloc", strerror(errno));
- free(hw);
- return NULL;
+ MSG_ERROR(hw->vm, "%s():%s", "calloc", strerror(errno));
+ return -1;
}
- b = calloc(BUF_SZ, sizeof *b);
- if (b == NULL) {
- vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+ ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf);
+ if (((struct keyboard_ *)(hw->data))->buf == NULL) {
+ MSG_ERROR(hw->vm, "%s():%s", "malloc", strerror(errno));
free(hw->data);
- free(hw);
- return NULL;
+ hw->data = NULL;
+ return -1;
}
- ((struct keyboard_ *)(hw->data))->buf = b;
- ((struct keyboard_ *)(hw->data))->buf_sz = BUF_SZ;
- return hw;
+ ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz;
+
+ return 0;
}
-void keyboard_del(struct dcpu16_hw **hw) {
+static
+void keyboard_data_free_(struct dcpu16_hw *hw) {
if (hw) {
- free(((struct keyboard_ *)((*hw)->data))->buf);
- ((struct keyboard_ *)((*hw)->data))->buf = NULL;
+ if (hw->data) {
+ if (((struct keyboard_ *)(hw->data))->buf) {
+ free(((struct keyboard_ *)(hw->data))->buf);
+ ((struct keyboard_ *)(hw->data))->buf = NULL;
+ }
+ free(hw->data);
+ hw->data = 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;
+ }
- free((*hw)->data);
- (*hw)->data = NULL;
+ MSG_DEBUG(hw->vm, "%s>> %s now:%zu was:%zu", __func__, "buffer_size", *buf_sz_in, *buf_sz_out);
- free(*hw);
- *hw = NULL;
+ 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,
+ .mfg_l = 0x0000,
+ .mfg_h = 0x0000,
+ .hwi = keyboard_hwi_,
+ .cycle = keyboard_cycle_,
+ .reset = keyboard_reset_,
+
+ .data_init = keyboard_data_init_,
+ .data_free = keyboard_data_free_,
+ .ctl = keyboard_data_ctl_,
+ .ctl_cmd = ctl_,
+};