Minor cleanup in NEDsim.c, removing old TODO items, old code, etc.
[screensavers] / hacks / NEDsim / NEDsim.c
index bee5e3b..6778c2d 100644 (file)
@@ -1,24 +1,9 @@
 /* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>                  */
 /* See LICENSE.txt file for copyright and license details.                    */
 
 /* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>                  */
 /* See LICENSE.txt file for copyright and license details.                    */
 
-// TODO:
-//      - Write a brief description of the machine being simulated. Only one thread, reduced RAM, no meaningful console when running as screensaver, etc.
-
-// CLI Flags:
-//      -path-to-aout-binary
-//      -path-to-font-file
-//      -speed
-
-// Ideas for sample programs to include:
-//      - Build list of integers on the stack (DUP, IM_1, ADD).
-//      - Calculate prime numbers, placing them on the stack.
-//      - Lights sliding back and forth, like PDP-11 front panel with some OSes.
-
 #include "screenhack.h"
 #include "simulator.h"
 
 #include "screenhack.h"
 #include "simulator.h"
 
-/* Keep this source code C89 compliant per XScreensaver's instructions.       */
-
 /* -------------------------------------------------------------------------- */
 /* Data Structures                                                            */
 /* -------------------------------------------------------------------------- */
 /* -------------------------------------------------------------------------- */
 /* Data Structures                                                            */
 /* -------------------------------------------------------------------------- */
@@ -74,25 +59,6 @@ struct color_scheme {
         text;
 };
 
         text;
 };
 
-//static struct color_scheme color_list[] = {
-//    // TODO: Explain all this.
-//    // TODO: Add other color schemes, like purple PDP-11/70.
-//    // TODO: http://www.chdickman.com/pdp8/DECcolors/
-//    {
-//        {63479,63479,63479}, // 092-XXXX-123
-//        { 5140, 2056, 5654}, // 092-XXXX-152
-//        {40092,11051,15677}, // 092-XXXX-139
-//        {30326,13107,12850}, // 092-XXXX-154
-//        {65535,13107,12850}, // homemade
-//        {30326,13107,12850}, // 092-XXXX-154
-//        { 4112,12850,20046}, // 092-XXXX-145
-//        {12336,29555,37008}, // 092-XXXX-151
-//        {30326,24158, 6425}, // 092-XXXX-157
-//        {63479,63479,63479}, // 092-XXXX-123
-//        {63479,63479,63479}  // 092-XXXX-123
-//    }
-//};
-
 static struct color_scheme color_list[] = {
     // TODO: Explain all this.
     // TODO: Add other color schemes, like purple PDP-11/70.
 static struct color_scheme color_list[] = {
     // TODO: Explain all this.
     // TODO: Add other color schemes, like purple PDP-11/70.
@@ -284,6 +250,59 @@ draw_panel(struct NEDsim * nedsim)
             nedsim->cell_size * OVERALL_WIDTH_IN_CELLS,
             nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS)
     );
             nedsim->cell_size * OVERALL_WIDTH_IN_CELLS,
             nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS)
     );
+
+    // Give the panel rounded corners by first deleting the four right-angle corners...
+    set_color(nedsim, &color_list[nedsim->color_index].panel_bg);
+    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset,
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-1)),
+            nedsim->origin_y_offset,
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 1)),
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-1)),
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 1)),
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    // ...and then replacing them with filled arcs, forming rounded corners.
+    set_color(nedsim, &color_list[nedsim->color_index].panel_fg);
+    XFillArc(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset,
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            180*64, -90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
+            nedsim->origin_y_offset,
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            0, 90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 2)),
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            180*64, 90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->win, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 2)),
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            0, -90*64
+    );
 }
 
 // TODO: Explain
 }
 
 // TODO: Explain
@@ -624,8 +643,16 @@ NEDsim_event(Display * dpy, Window win, void * closure, XEvent * event)
 static void
 NEDsim_free(Display * dpy, Window win, void * closure)
 {
 static void
 NEDsim_free(Display * dpy, Window win, void * closure)
 {
-    // TODO: Replace all this with proper code to free everything.
     struct NEDsim * nedsim = closure;
     struct NEDsim * nedsim = closure;
+
+    if (nedsim->nedstate != NULL) {
+        free(nedsim->nedstate->active_thread->psw);
+        free(nedsim->nedstate->active_thread);
+        free(nedsim->nedstate->hack);
+        free(nedsim->nedstate);
+    }
+
+    // TODO: Replace all this with proper code to free everything related to the screensaver itself.
     XFreeGC(nedsim->dpy, nedsim->gc);
     free(nedsim);
 }
     XFreeGC(nedsim->dpy, nedsim->gc);
     free(nedsim);
 }
@@ -654,8 +681,15 @@ NEDsim_init(Display * dpy, Window win)
     nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
     nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
 
     nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
     nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
 
-    // TODO: Read in the a.out file. This should be done in the simulator's init function call?
-    nedsim->nedstate = init_simulator();
+    // Load the program file specified by the user or, if none is specified,
+    // randomly select one of the included programs.
+    char * input_file = get_string_resource(nedsim->dpy, "binary", "String");
+    if (input_file == NULL) {
+        // TODO: Need to include some default programs and randomly select one to load into RAM.
+        nedsim->nedstate = init_simulator("./test.out");
+    } else {
+        nedsim->nedstate = init_simulator(input_file);
+    }
 
     nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
 
 
     nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
 
@@ -728,8 +762,12 @@ NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsign
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
     /* Only restart the simulation if the window changed size.                */
     if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
     /* Only restart the simulation if the window changed size.                */
     if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
-        NEDsim_free(dpy, win, closure);
-        closure = NEDsim_init(dpy, win);
+        struct NEDstate * original_nedstate = nedsim->nedstate;
+        nedsim->nedstate = NULL;
+        NEDsim_free(dpy, win, nedsim);
+        struct NEDsim * new_nedsim = NEDsim_init(dpy, win);
+        new_nedsim->nedstate = original_nedstate;
+        closure = new_nedsim;
     }
 }
 
     }
 }
 
@@ -740,6 +778,7 @@ static const char * NEDsim_defaults[] = {
 
 static XrmOptionDescRec NEDsim_options[] = {
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
 
 static XrmOptionDescRec NEDsim_options[] = {
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
+    { "-binary",             ".binary",                 XrmoptionSepArg, 0      },
     { 0, 0, 0, 0 }
 };
 
     { 0, 0, 0, 0 }
 };