opcodes2c.pl: Add command line options
[emu8051.git] / src / gtk / main.c
index 0750559..4b360d2 100644 (file)
@@ -4,19 +4,7 @@
  * Copyright (C) 1999 Jonathan St-AndrĂ©
  * Copyright (C) 1999 Hugo Villeneuve <hugo@hugovil.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ * This file is released under the GPLv2
  */
 
 #include <stdio.h>
@@ -32,7 +20,7 @@
 #include "memory.h"
 #include "options.h"
 #include "hexfile.h"
-
+#include "timers.h"
 #include "main.h"
 #include "reset.xpm"
 #include "run.xpm"
@@ -65,9 +53,9 @@ extern struct app_config_t *cfg;
 extern struct options_t options;
 
 void
-emugtk_UpdateDisplay(void)
+emugtk_update_display(void)
 {
-       log_debug("UpdateDisplay()");
+       log_debug("update display");
        regwin_refresh();
        pgmwin_refresh();
        pswwin_refresh();
@@ -85,10 +73,10 @@ static void
 emugtk_stop_running()
 {
        if (running) {
-               log_info("StopRunning()");
+               log_info("stop running");
                g_source_remove(running_function_tag);
                running = 0;
-               emugtk_UpdateDisplay();
+               emugtk_update_display();
        }
 }
 
@@ -113,7 +101,7 @@ static void
 emugtk_start_running(void)
 {
        if (!running) {
-               log_info("StartRunning()");
+               log_info("start running");
                running_function_tag = g_idle_add(emugtk_running, 0);
                running = 1;
        }
@@ -139,15 +127,15 @@ button_add_pix(GtkWidget *box, char **xpm)
 
 /* CPU reset and Gtk UI update */
 static void
-emugtk_Reset(void)
+emugtk_reset(void)
 {
-       cpu8051_Reset();
-       emugtk_UpdateDisplay();
+       cpu8051_reset();
+       emugtk_update_display();
 }
 
 /* Signal ResetEvent (ResetButton) */
 static void
-emugtk_ResetEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+emugtk_reset_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
        /* Remove compiler warning about unused variables. */
        (void) widget;
@@ -156,20 +144,20 @@ emugtk_ResetEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
 
        log_info("ResetEvent()");
        emugtk_stop_running();
-       emugtk_Reset();
+       emugtk_reset();
 }
 
 /* CPU Step and Gtk UI update */
 static void
-emugtk_Step(void)
+emugtk_step(void)
 {
-       cpu8051_Exec();
-       emugtk_UpdateDisplay();
+       cpu8051_exec();
+       emugtk_update_display();
 }
 
 /* Signal RunEvent (RunButton) */
 static void
-emugtk_RunEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+emugtk_run_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
        /* Remove compiler warning about unused variables. */
        (void) widget;
@@ -186,7 +174,7 @@ emugtk_RunEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
 
 /* Signal StopEvent (StopButton) */
 static void
-emugtk_StopEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+emugtk_stop_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
        /* Remove compiler warning about unused variables. */
        (void) widget;
@@ -199,7 +187,7 @@ emugtk_StopEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
 
 /* Signal StepEvent (StepButton) */
 static void
-emugtk_StepEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+emugtk_step_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
        /* Remove compiler warning about unused variables. */
        (void) widget;
@@ -208,12 +196,12 @@ emugtk_StepEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
 
        log_info("StepEvent()");
        emugtk_stop_running();
-       emugtk_Step();
+       emugtk_step();
 }
 
 /* Creates the Reset, Run, Stop and Step buttons. */
 static GtkWidget *
-AddButtons(void)
+add_buttons(void)
 {
        GtkWidget *button_hbox;
        GtkWidget *button;
@@ -224,32 +212,32 @@ AddButtons(void)
        /* Creating the RESET button. */
        button = button_add_pix(button_hbox, reset_xpm);
        g_signal_connect(button, "clicked",
-                        G_CALLBACK(emugtk_ResetEvent),
+                        G_CALLBACK(emugtk_reset_event),
                         NULL);
 
        /* Creating the RUN button. */
        button = button_add_pix(button_hbox, run_xpm);
        g_signal_connect(button, "clicked",
-                        G_CALLBACK(emugtk_RunEvent),
+                        G_CALLBACK(emugtk_run_event),
                         NULL);
 
        /* Creating STOP button. */
        button = button_add_pix(button_hbox, stop_xpm);
        g_signal_connect(GTK_OBJECT(button), "clicked",
-                        G_CALLBACK(emugtk_StopEvent),
+                        G_CALLBACK(emugtk_stop_event),
                         NULL);
 
        /* Creating STEP button. */
        button = button_add_pix(button_hbox, step_xpm);
        g_signal_connect(GTK_OBJECT(button), "clicked",
-                        G_CALLBACK(emugtk_StepEvent),
+                        G_CALLBACK(emugtk_step_event),
                         NULL);
 
        return button_hbox;
 }
 
 static GtkWidget *
-AddMenu(void)
+add_menu(void)
 {
        GtkWidget *menu_bar;
 
@@ -257,13 +245,13 @@ AddMenu(void)
        menu_bar = gtk_menu_bar_new();
 
        /* Adding the 'File' submenu */
-       FileAddMenu(menu_bar);
+       file_add_menu(menu_bar);
 
        /* Adding the 'View' submenu */
-       ViewAddMenu(menu_bar);
+       view_add_menu(menu_bar);
 
        /* Adding the 'Help' submenu */
-       HelpAddMenu(menu_bar);
+       help_add_menu(menu_bar);
 
        return menu_bar;
 }
@@ -277,6 +265,8 @@ mainwin_configure_event(GtkWindow *window, GdkEvent *event, gpointer data)
 
        cfg->win_width = event->configure.width;
        cfg->win_height = event->configure.height;
+       cfg->win_pos_x = event->configure.x;
+       cfg->win_pos_y = event->configure.y;
 
        /*
         * Important:
@@ -332,7 +322,7 @@ static void
 emugtk_show_memory_paned(void)
 {
        gtk_widget_show_all(mainwin);
-       emugtk_UpdateDisplay();
+       emugtk_update_display();
 }
 
 void
@@ -342,7 +332,7 @@ emugtk_create_int_memory_paned(void)
                                    INT_MEM_ID);
        gtk_paned_pack1(GTK_PANED(vpaned1), scrollwin_int,
                        FALSE, FALSE);
-       if (emugtk_window_init_complete == true)
+       if (emugtk_window_init_complete)
                emugtk_show_memory_paned();
 }
 
@@ -365,7 +355,7 @@ emugtk_create_ext_memory_paned(void)
        gtk_paned_pack2(GTK_PANED(vpaned1), scrollwin_ext,
                        TRUE, FALSE);
 
-       if (emugtk_window_init_complete == true)
+       if (emugtk_window_init_complete)
                emugtk_show_memory_paned();
 }
 
@@ -392,7 +382,7 @@ emugtk_recreate_memory_paned(void)
                emugtk_create_ext_memory_paned();
        }
 
-       if (emugtk_window_init_complete == true)
+       if (emugtk_window_init_complete)
                emugtk_show_memory_paned();
 }
 
@@ -410,8 +400,24 @@ emugtk_create_memory_paned(void)
                emugtk_recreate_memory_paned();
 
                return vpaned1;
-       } else
+       } else {
                return NULL;
+       }
+}
+
+static void
+emugtk_set_geometry_hints(GtkWidget *window)
+{
+       GdkGeometry hints;
+
+        hints.min_width = 100;
+        hints.min_height = 100;
+
+       /* Set reference point to top left corner */
+       hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
+
+       gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &hints,
+                                     GDK_HINT_MIN_SIZE);
 }
 
 /*
@@ -478,6 +484,8 @@ emugtk_create_memory_paned(void)
 static void
 emugtk_window_init(void)
 {
+       int geometry_ok = false;
+       int id;
        GtkWidget *vbox;
        GtkWidget *menu_bar;
        GtkWidget *buttons_bar;
@@ -489,9 +497,7 @@ emugtk_window_init(void)
        emugtk_window_init_complete = false;
 
        mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_title(GTK_WINDOW(mainwin), PACKAGE);
-       gtk_window_set_default_size(GTK_WINDOW(mainwin),
-                                   cfg->win_width, cfg->win_height);
+       gtk_window_set_title(GTK_WINDOW(mainwin), PACKAGE_NAME);
        gtk_container_set_border_width(GTK_CONTAINER(mainwin), 0);
 
        /* Window DESTROY event. */
@@ -502,16 +508,19 @@ emugtk_window_init(void)
                         G_CALLBACK(mainwin_configure_event), NULL);
 
        /* Creating the menu bar. */
-       menu_bar = AddMenu();
+       menu_bar = add_menu();
 
        /* Creating the buttons bar. */
-       buttons_bar = AddButtons();
+       buttons_bar = add_buttons();
 
        scrollwin = pswwin_init();
-       gtk_box_pack_start(GTK_BOX(buttons_bar), scrollwin, FALSE, FALSE, 100);
+       gtk_box_pack_start(GTK_BOX(buttons_bar), scrollwin, FALSE, FALSE, 50);
 
-       scrollwin = timerwin_init();
-       gtk_box_pack_start(GTK_BOX(buttons_bar), scrollwin, FALSE, FALSE, 100);
+       for (id = 0; id < GP_TIMERS_COUNT; id++) {
+               scrollwin = timerwin_init(id);
+               gtk_box_pack_start(GTK_BOX(buttons_bar), scrollwin,
+                                  FALSE, FALSE, 15);
+       }
 
        /* hpaned will contain registers and disassembly windows. */
        hpaned = gtk_hpaned_new();
@@ -561,13 +570,41 @@ emugtk_window_init(void)
 
        g_signal_connect(mainwin, "destroy", G_CALLBACK(emugtk_quit_gui), NULL);
 
+       emugtk_set_geometry_hints(mainwin);
+
+       /*
+        * If either a size or a position can be extracted from the geometry
+        * string, gtk_window_parse_geometry() returns TRUE and calls
+        * gtk_window_set_default_size() and/or gtk_window_move() to resize/move
+        * the window.
+        */
+       if (options.g != NULL) {
+               geometry_ok = gtk_window_parse_geometry(GTK_WINDOW(mainwin),
+                                                       options.g);
+               if (!geometry_ok)
+                       log_err("Failed to parse geometry argument: %s",
+                               options.g);
+       }
+
+       /*
+        * If geometry was not specified, or was improperly parsed, use
+        * saved window size.
+        */
+       if (geometry_ok == false) {
+               log_err("Use saved window size");
+               gtk_window_set_default_size(GTK_WINDOW(mainwin),
+                                           cfg->win_width, cfg->win_height);
+               gtk_window_move(GTK_WINDOW(mainwin),
+                               cfg->win_pos_x, cfg->win_pos_y);
+       }
+
        gtk_widget_show_all(mainwin);
 
        emugtk_window_init_complete = true;
 }
 
 void
-AddMenuSeparator(GtkWidget *menu)
+add_menu_separator(GtkWidget *menu)
 {
        GtkWidget *item;
 
@@ -582,14 +619,14 @@ emugtk_new_file(char *file)
 
        emugtk_stop_running();
 
-       rc = LoadHexFile(file);
-       if (rc == false) {
+       rc = hexfile_load(file);
+       if (!rc) {
                message_show_error("Error parsing hex file");
        } else {
                if (cfg->clear_ram_on_file_load)
-                       emugtk_Reset();
+                       emugtk_reset();
 
-               emugtk_UpdateDisplay();
+               emugtk_update_display();
        }
 }
 
@@ -608,15 +645,15 @@ main(int argc, char **argv)
        gtk_init(&argc, &argv);
 
        if (options.filename != NULL)
-               rc_load_hexfile = LoadHexFile(options.filename);
+               rc_load_hexfile = hexfile_load(options.filename);
 
-       cpu8051_Reset();
+       cpu8051_reset();
 
        log_info("Init GUI");
        emugtk_window_init();
-       emugtk_UpdateDisplay();
+       emugtk_update_display();
 
-       if (rc_load_hexfile == false)
+       if (!rc_load_hexfile)
                message_show_error("Error parsing hex file");
 
        gtk_main();