further reorg of module abstraction and control interface
[dcpu16] / hw_keyboard.c
index d5c90b3faf4b2af6f5a1519894c02107d8b7ea8d..592b6735736a23135220f5ceb4952e3fb39b8fed 100644 (file)
 
 #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_ = {
-    .vm     = NULL,
-    .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
 
 struct keyboard_ {
     char *buf;
@@ -81,12 +70,12 @@ 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);
+    hw->vm->trace_cb_("%s>> down:%u rfb_key:0x%04x", down, key);
 
-    fprintf(stderr, "%s: hw:%p name:%s vm:%p\n", __func__, hw, hw->name_, hw->vm);
+    if (keysym_rfbtodcpu(key, &dcpu_key)) {
+        /* unhandled key event */
+        return;
+    }
 
     keyboard->keys_pressed[dcpu_key] = (down ? 1 : 0);
     if (down) {
@@ -110,25 +99,27 @@ void keyboard_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr rfbScreen) {
 #endif /* HAVE_LIBVNCSERVER */
 
 static
-void keyboard_reset_(struct dcpu16 *vm, void *data) {
-    struct keyboard_ *keyboard = (struct keyboard_ *)data;
+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);
 }
 
 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];
 
@@ -158,47 +149,112 @@ void keyboard_hwi_(struct dcpu16 *vm, void *data) {
     }
 }
 
-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;
+        hw->vm->warn_cb_("%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) {
+        hw->vm->warn_cb_("%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;
 
-    hw->vm = vm;
+    ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz;
 
-    return hw;
+    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;
+        }
+    }
+}
 
-        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;
+        }
 
-        free(*hw);
-        *hw = NULL;
+        if (buf_sz_in) {
+            hw->vm->trace_cb_("%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) {
+                hw->vm->warn_cb_("%s():%s", "realloc", strerror(errno));
+                return -1;
+            }
+            keyboard->buf = tmp_ptr;
+            keyboard->buf_sz = *buf_sz_in;
+            keyboard->buf_head = keyboard->buf_tail = 0;
+        }
+
+        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_;
+
+        return 0;
+    }
+#endif /* HAVE_LIBVNCSERVER */
+
+    return -EINVAL;
 }
+
+static struct dcpu16_hw hw_ = {
+    .vm     = NULL,
+    .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
+};
+
+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_,
+};