load/save dialogs
authorJavier S. Pedro <maemo@javispedro.com>
Tue, 18 Aug 2009 00:38:28 +0000 (02:38 +0200)
committerJavier S. Pedro <maemo@javispedro.com>
Tue, 18 Aug 2009 00:38:28 +0000 (02:38 +0200)
A bit of cleanup, and load/save snapshot dialogs implemented.
They do nothing yet.

gui/Makefile
gui/plugin.c
gui/plugin.h [new file with mode: 0644]
gui/save.c [new file with mode: 0644]
gui/state.c
gui/state.h [deleted file]

index 951402c..1fdba15 100644 (file)
@@ -23,7 +23,7 @@ DATA_FILES+=drnoksnes.service drnoksnes.startup.service
 
 all: drnoksnes_plugin.so data
 
-drnoksnes_plugin.so: plugin.o state.o
+drnoksnes_plugin.so: plugin.o state.o save.o
        $(CC) $(LDFLAGS) $^ $(LDLIBS)-o $@
        
 clean: 
index a98d24e..bd12e99 100644 (file)
 #include <gconf/gconf.h>
 #include <gconf/gconf-client.h>
 #include <hildon/hildon-file-chooser-dialog.h>
+#include <hildon/hildon-banner.h>
 
 #include "../platform/hgw.h"
-#include "state.h"
+#include "plugin.h"
 
 static GtkWidget * load_plugin(void);
 static void unload_plugin(void);
@@ -53,61 +54,43 @@ static StartupPluginInfo plugin_info = {
        plugin_callback
 };
 
-static const gchar * rom_globs[] = {
-       "*.smc",
-       "*.fig",
-       "*.sfc",
-       NULL
-};
-
 STARTUP_INIT_PLUGIN(plugin_info, gs, FALSE, TRUE)
 
-// Yes, I'm using the label not only to show but also save the current value.
+char * current_rom_file = 0;
 static GtkLabel * rom_label;
 
-static GameState cur_state = GAME_STATE_STOP;
-
-static void update_game_state()
+static void set_rom(const char * rom_file)
 {
-       GameState new_state;
-       GameStateInfo info;
-       const char * rom_file = gtk_label_get_text(rom_label);
+       if (!rom_file) return;
+       if (current_rom_file) g_free(current_rom_file);
 
-       if (rom_file) {
-               game_state_fill(&info, rom_file);
-       }
+       current_rom_file = g_strdup(rom_file);
+       gtk_label_set_text(GTK_LABEL(rom_label), rom_file);
 
-       if (info.has_state_file) {
-               new_state = GAME_STATE_PAUSED; // We have a freeze file
-       } else {
-               new_state = GAME_STATE_STOP;
-       }
+       game_state_update();
+       save_clear();
+}
 
-       if (cur_state != new_state) {
-               game_state_set(new_state);
-               cur_state = new_state;
-       }
+static inline GtkWindow* get_parent_window() {
+       return GTK_WINDOW(gs.ui->hildon_appview);
 }
 
-static gchar *
-interface_file_chooser
-(GtkWindow * parent, GtkFileChooserAction action, const gchar ** extension)
+static void select_rom_callback(GtkWidget * button, gpointer data)
 {
        GtkWidget * dialog;
        GtkFileFilter * filter;
-       const gchar * current_filename;
+       const gchar * current_filename = gtk_label_get_text(rom_label);
        gchar * filename = NULL;
-       int i;
 
        filter = gtk_file_filter_new();
-       for (i = 0; extension[i]; i++) {
-               gtk_file_filter_add_pattern(filter, extension[i]);
-       }
+       gtk_file_filter_add_pattern(filter, "*.smc");
+       gtk_file_filter_add_pattern(filter, "*.sfc");
+       gtk_file_filter_add_pattern(filter, "*.fig");
 
-       dialog = hildon_file_chooser_dialog_new_with_properties(parent, 
-               "action", action, "local_only", TRUE, "filter", filter, NULL);
+       dialog = hildon_file_chooser_dialog_new_with_properties(
+               get_parent_window(),
+               "action", GTK_FILE_CHOOSER_ACTION_OPEN, "filter", filter, NULL);
 
-       current_filename = gtk_label_get_text(rom_label);
        if (current_filename && strlen(current_filename) > 1) {
                // By default open showing the last selected file
                gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), 
@@ -121,23 +104,10 @@ interface_file_chooser
 
        gtk_widget_destroy(dialog);
 
-       return filename;
-}
-
-static void select_rom_callback(GtkWidget * button, gpointer data)
-{
-       gchar * filename = interface_file_chooser(
-               GTK_WINDOW(gtk_widget_get_parent_window(button)),
-               GTK_FILE_CHOOSER_ACTION_OPEN,
-               rom_globs);
-
-       if (!filename) return;
-       
-       gtk_label_set_text(rom_label, filename);
-       
-       g_free(filename);
-
-       update_game_state();
+       if (filename) {
+               set_rom(filename);
+               g_free(filename);
+       }
 }
 
 static GtkWidget * load_plugin(void)
@@ -152,10 +122,6 @@ static GtkWidget * load_plugin(void)
        
        gtk_widget_set_size_request(GTK_WIDGET(selectRomBtn),
                                                                180, 50);
-                                                               
-       gtk_label_set_text(rom_label,
-                                               gconf_client_get_string(gcc, kGConfRomFile, NULL));
-                                                               
 
        g_signal_connect(G_OBJECT(selectRomBtn), "clicked",
                                        G_CALLBACK (select_rom_callback), NULL);
@@ -164,20 +130,31 @@ static GtkWidget * load_plugin(void)
        gtk_box_pack_end(GTK_BOX(parent_hbox), GTK_WIDGET(rom_label), TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(parent), parent_hbox, FALSE, FALSE, 0);
 
-       update_game_state();
+       // Load current configuration from gconf
+       set_rom(gconf_client_get_string(gcc, kGConfRomFile, NULL));
 
        return parent;
 }
 
 static void unload_plugin(void)
 {
+       if (current_rom_file) {
+               g_free(current_rom_file);
+               current_rom_file = 0;
+       }
+       game_state_clear();
+       save_clear();
        g_object_unref(gcc);
 }
 
 static void write_config(void)
 {
-       gconf_client_set_string(gcc, kGConfRomFile,
-        gtk_label_get_text(GTK_LABEL(rom_label)), NULL);
+       if (current_rom_file) {
+               gconf_client_set_string(gcc, kGConfRomFile, current_rom_file, NULL);
+       } else {
+               hildon_banner_show_information(GTK_WIDGET(get_parent_window()), NULL,
+                       "No ROM selected");
+       }
 }
 
 static GtkWidget **load_menu(guint *nitems)
@@ -194,6 +171,16 @@ static void update_menu(void)
 
 static void plugin_callback(GtkWidget * menu_item, gpointer data)
 {
-
+       switch ((gint) data) {
+               case 20:        // ME_GAME_OPEN
+                       save_load(get_parent_window());
+                       break;
+               case 21:        // ME_GAME_SAVE
+                       save_save(get_parent_window());
+                       break;
+               case 22:        // ME_GAME_SAVE_AS
+                       save_save_as(get_parent_window());
+                       break;
+       }
 }
 
diff --git a/gui/plugin.h b/gui/plugin.h
new file mode 100644 (file)
index 0000000..5ca61f3
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef _PLUGIN_H_
+#define _PLUGIN_H_
+
+#include <glib.h>
+#include <gtk/gtk.h>
+
+/* plugin.c */
+extern char * current_rom_file;
+
+/* state.c */
+gchar * game_state_get_frz_file();
+void game_state_update();
+void game_state_clear();
+gboolean game_state_is_paused();
+
+/* save.c */
+void save_clear();
+void save_load(GtkWindow* parent);
+void save_save(GtkWindow* parent);
+void save_save_as(GtkWindow* parent);
+
+#endif
diff --git a/gui/save.c b/gui/save.c
new file mode 100644 (file)
index 0000000..b026d95
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+* This file is part of DrNokSnes
+*
+* Copyright (C) 2005 INdT - Instituto Nokia de Tecnologia
+* http://www.indt.org/maemo
+* Copyright (C) 2009 Javier S. Pedro <maemo@javispedro.com>
+*
+* This software is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public License
+* as published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA
+*
+*/
+
+#include <string.h>
+#include <glib.h>
+#include <hildon/hildon-file-chooser-dialog.h>
+
+#include "plugin.h"
+
+static gchar * cur_save_filename = NULL;
+
+void save_clear()
+{
+       if (cur_save_filename) {
+               g_free(cur_save_filename);
+               cur_save_filename = NULL;
+       }
+}
+
+static gchar * show_dialog(GtkWindow* parent, GtkFileChooserAction action)
+{
+       GtkWidget * dialog;
+       GtkFileFilter * filter;
+       gchar * filename = NULL;
+
+       filter = gtk_file_filter_new();
+       gtk_file_filter_add_pattern(filter, "*.snsg");
+
+       dialog = hildon_file_chooser_dialog_new_with_properties(GTK_WINDOW(parent), 
+               "action", action, "filter", filter, NULL);
+
+       // TODO Default path
+
+       gtk_widget_show_all(GTK_WIDGET(dialog));
+       if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
+               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+       }
+
+       gtk_widget_destroy(dialog);
+
+       return filename;
+}
+
+void save_load(GtkWindow* parent)
+{
+       gchar * filename = show_dialog(parent, GTK_FILE_CHOOSER_ACTION_OPEN);
+       
+       // TODO: Something
+}
+
+void save_save(GtkWindow* parent)
+{
+       if (!cur_save_filename) {
+               save_save_as(parent);
+       }
+       
+       // TODO: Something again
+}
+
+void save_save_as(GtkWindow* parent)
+{
+       gchar * filename = show_dialog(parent, GTK_FILE_CHOOSER_ACTION_SAVE);
+       
+       // TODO: Something again
+}
+
index e7057f2..2d72f9b 100644 (file)
 #include <dbus/dbus.h>
 #include <glib.h>
 
-#include "state.h"
+#include "plugin.h"
 
 #define FRZ_FILE_EXT ".frz.gz"
 
-void game_state_fill(GameStateInfo * info, const char * rom_file)
+static gchar * cur_frz_file = NULL;
+
+static gboolean rom_get_freeze_file()
 {
-       char * ext = strrchr(rom_file, '.');
+       char * ext = strrchr(current_rom_file, '.');
        char * rom_base;
        char * frz_file;
+       gboolean rom_exists, frz_exists;
+
+       if (cur_frz_file) g_free(cur_frz_file);
 
        if (!ext) {
-               rom_base = g_strdup(rom_file);
+               rom_base = g_strdup(current_rom_file);
        } else {
-               rom_base = g_strndup(rom_file, ext - rom_file);
+               rom_base = g_strndup(current_rom_file, ext - current_rom_file);
        }
-
-       info->rom_exists = 
-               g_file_test(rom_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
+       rom_exists = g_file_test(current_rom_file,
+                       G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
 
        frz_file = g_strconcat(rom_base, FRZ_FILE_EXT);
-       info->has_state_file =
+       frz_exists =
                g_file_test(frz_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
 
-       g_free(frz_file);
        g_free(rom_base);
+
+       if (rom_exists & frz_exists) {
+               cur_frz_file = frz_file;
+       } else {
+               cur_frz_file = NULL;
+               g_free(frz_file);
+       }
+
+       return rom_exists && frz_exists;
 }
 
 // More uglyness. If you know a better way to do this please tell.
-void game_state_set(GameState newState)
+void game_state_update()
 {
        DBusError err;
        DBusConnection* bus;
        DBusMessage* m;
        const char * m_name;
-       
-       switch (newState) {
-               case GAME_STATE_PAUSED:
-                       m_name = "game_pause";
-                       break;
-               case GAME_STATE_STOP:
-                       m_name = "game_close";
-                       break;
-               default:
-                       return;
+       gboolean has_freeze = rom_get_freeze_file();
+
+       if (has_freeze) {
+               m_name = "game_pause";
+       } else {
+               m_name = "game_close";
        }
-       
-       
+
        dbus_error_init(&err);
-       
+
        bus = dbus_bus_get(DBUS_BUS_SESSION, &err);
        if (dbus_error_is_set(&err)) {
                dbus_error_free(&err); 
                return;
        }
-       
+
        m = dbus_message_new_method_call("com.javispedro.drnoksnes.startup",
                                                                                "/com/javispedro/drnoksnes/startup",
                                                                                "com.javispedro.drnoksnes.startup",
                                                                                m_name);
-                                                                               
+
        dbus_connection_send(bus, m, NULL);
        dbus_connection_flush(bus);
 
        dbus_message_unref(m);
 }
 
+void game_state_clear()
+{
+       if (cur_frz_file) {
+               g_free(cur_frz_file);
+               cur_frz_file = NULL;
+       }
+}
+
+gboolean game_state_is_paused()
+{
+       return cur_frz_file ? TRUE : FALSE;
+}
+
diff --git a/gui/state.h b/gui/state.h
deleted file mode 100644 (file)
index 3982b65..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef _STATE_H_
-#define _STATE_H_
-
-#include <glib.h>
-
-typedef struct GameStateInfo
-{
-       gboolean rom_exists;
-       gboolean has_state_file;
-} GameStateInfo;
-typedef enum GameState
-{
-       GAME_STATE_NONE = 0,
-       GAME_STATE_PAUSED,
-       GAME_STATE_STOP
-} GameState;
-
-void game_state_fill(GameStateInfo * info, const char * rom_file);
-void game_state_set(GameState newState);
-
-#endif