further reorg of module abstraction and control interface
[dcpu16] / hw_lem1802.c
index f17ebc73287a2eff7eaa083f37fb400e6bf3548c..06b0d5ac2c5d9392fb58d0b76c19995fb38d67f8 100644 (file)
 #include "chargen-4x8.h"
 #include "hw_lem1802.h"
 
+/* lem1802
+ *
+ * TODO:
+ * multiple vnc displays
+ */
+
 #ifdef DEBUG
 #define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
 #else /* DEBUG  */
@@ -337,15 +343,6 @@ rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) {
     return s;
 }
 
-/* set up a new screen to see our pixels */
-void lem1802_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr s) {
-    struct lem1802_ *display = (struct lem1802_ *)hw->data;
-
-    s->desktopName = "NYA ELEKTRISKA LEM1802";
-    s->frameBuffer = (char *)display->pixbuf;
-
-    TRACE("%s>> s:%p\n", __func__, VOIDP(s));
-}
 
 /* notify rfb server that pixels may have changed */
 static
@@ -512,20 +509,6 @@ static struct renderer_ {
     { NULL, NULL, NULL }
 };
 
-int lem1802_renderer_set(struct dcpu16_hw *hw, const char *renderer, void *data) {
-    struct renderer_ *r;
-
-    for (r = lem1802_renderers_; r->renderer; r++) {
-        if (strcmp(renderer, r->name) == 0) {
-            ((struct lem1802_ *)(hw->data))->render = r->renderer;
-            ((struct lem1802_ *)(hw->data))->renderer_data = data;
-            TRACE("%s>> renderer set to %s", __func__, renderer);
-            return 0;
-        }
-    }
-
-    return -1;
-}
 
 char *lem1802_renderers_iter(void **iterp, char **name, char **args) {
     struct renderer_ **r = (struct renderer_ **)iterp;
@@ -572,20 +555,106 @@ int lem1802_data_init_(struct dcpu16_hw *hw, void *data) {
 void lem1802_data_free_(struct dcpu16_hw *hw) {
     if (hw) {
         if (hw->data) {
-            /* FIXME: free renderer data */
-            hw->vm->warn_cb_("FIXME");
-
             if (((struct lem1802_ *)(hw->data))->pixbuf) {
                 free(((struct lem1802_ *)(hw->data))->pixbuf);
                 ((struct lem1802_ *)(hw->data))->pixbuf = NULL;
             }
-
             free(hw->data);
             hw->data = NULL;
         }
     }
 }
 
+static struct dcpu16_hw_ctl_cmd ctl_[] = {
+    { "blink_rate", "const unsigned int *rate", "unsigned int *rate", "sets or gets cycles per blink toggle" },
+    { "refresh_rate", "const unsigned int *rate", "unsigned int *rate", "sets or gets cycles per screen refresh" },
+#ifdef HAVE_LIBVNCSERVER
+    { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this lem1802 instance with an rfb display" },
+#endif /* HAVE_LIBVNCSERVER */
+    { "renderer", "const char *", "NULL", "sets this lem1802 instance to use renderer" },
+    { "renderer_data", "void *", "NULL", "sets renderer-specific data" },
+    { NULL, NULL, NULL, NULL }
+};
+int lem1802_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) {
+    if (strcmp(cmd, "blink_rate") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        const unsigned int *rate_in = (const unsigned int *)data_in;
+        unsigned int *rate_out = (unsigned int *)data_out;
+
+        if (rate_out) {
+            *rate_out = display->blink_rate;
+        }
+
+        if (rate_in) {
+            display->blink_rate = *rate_in;
+        }
+
+        return 0;
+    }
+
+    if (strcmp(cmd, "refresh_rate") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        const unsigned int *rate_in = (const unsigned int *)data_in;
+        unsigned int *rate_out = (unsigned int *)data_out;
+
+        if (rate_out) {
+            *rate_out = display->refresh_rate;
+        }
+
+        if (rate_in) {
+            display->refresh_rate = *rate_in;
+        }
+
+        return 0;
+    }
+
+#ifdef HAVE_LIBVNCSERVER
+    if (strcmp(cmd, "associate_rfbScreen") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_out;
+        (void)data_in;
+
+        if (rfbScreen == NULL)
+            return -EFAULT;
+
+        rfbScreen->desktopName = "NYA ELEKTRISKA LEM1802";
+        rfbScreen->frameBuffer = (char *)display->pixbuf;
+
+        return 0;
+    }
+#endif /* HAVE_LIBVNCSERVER */
+
+    if (strcmp(cmd, "renderer") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        char *renderer = (char *)data_in;
+        (void)data_out;
+        struct renderer_ *r;
+
+        for (r = lem1802_renderers_; r->renderer; r++) {
+            if (strcmp(renderer, r->name) == 0) {
+                display->render = r->renderer;
+                TRACE("%s>> renderer set to %s", __func__, renderer);
+                return 0;
+            }
+        }
+
+        hw->vm->warn_cb_("unknown renderer '%s'", renderer);
+
+        return -ENOENT;
+    }
+
+    if (strcmp(cmd, "renderer_data") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        (void)data_out;
+
+        display->renderer_data = data_in;
+
+        return 0;
+    }
+
+    return -EINVAL;
+}
+
 
 static struct dcpu16_hw hw_ = {
     .name_  = "LEM1802 - Low Energy Monitor",
@@ -604,5 +673,7 @@ struct dcpu16_hw_module dcpu16_hw_module_lem1802 = {
     .template = &hw_,
     .data_init = lem1802_data_init_,
     .data_free = lem1802_data_free_,
+    .ctl = lem1802_data_ctl_,
+    .ctl_cmd = ctl_,
 };