snapshot support in gui
authorJavier S. Pedro <maemo@javispedro.com>
Mon, 17 Aug 2009 23:02:54 +0000 (01:02 +0200)
committerJavier S. Pedro <maemo@javispedro.com>
Mon, 17 Aug 2009 23:02:54 +0000 (01:02 +0200)
Detects if the selected rom has a freeze file or not, and sets up
resume/restart buttons as appropiate.

gui/Makefile
gui/plugin.c
gui/state.c [new file with mode: 0644]
gui/state.h [new file with mode: 0644]
platform/hgw.cpp

index 7a3ea3e..951402c 100644 (file)
@@ -23,11 +23,11 @@ DATA_FILES+=drnoksnes.service drnoksnes.startup.service
 
 all: drnoksnes_plugin.so data
 
-drnoksnes_plugin.so: plugin.o
+drnoksnes_plugin.so: plugin.o state.o
        $(CC) $(LDFLAGS) $^ $(LDLIBS)-o $@
        
 clean: 
-       rm -f noksnes_plugin.so *.o
+       rm -f drnoksnes_plugin.so *.o
        rm -f $(DATA_FILES)
 
 %: %.m4
index 725dda4..a98d24e 100644 (file)
@@ -31,6 +31,7 @@
 #include <hildon/hildon-file-chooser-dialog.h>
 
 #include "../platform/hgw.h"
+#include "state.h"
 
 static GtkWidget * load_plugin(void);
 static void unload_plugin(void);
@@ -64,6 +65,30 @@ STARTUP_INIT_PLUGIN(plugin_info, gs, FALSE, TRUE)
 // Yes, I'm using the label not only to show but also save the current value.
 static GtkLabel * rom_label;
 
+static GameState cur_state = GAME_STATE_STOP;
+
+static void update_game_state()
+{
+       GameState new_state;
+       GameStateInfo info;
+       const char * rom_file = gtk_label_get_text(rom_label);
+
+       if (rom_file) {
+               game_state_fill(&info, rom_file);
+       }
+
+       if (info.has_state_file) {
+               new_state = GAME_STATE_PAUSED; // We have a freeze file
+       } else {
+               new_state = GAME_STATE_STOP;
+       }
+
+       if (cur_state != new_state) {
+               game_state_set(new_state);
+               cur_state = new_state;
+       }
+}
+
 static gchar *
 interface_file_chooser
 (GtkWindow * parent, GtkFileChooserAction action, const gchar ** extension)
@@ -111,6 +136,8 @@ static void select_rom_callback(GtkWidget * button, gpointer data)
        gtk_label_set_text(rom_label, filename);
        
        g_free(filename);
+
+       update_game_state();
 }
 
 static GtkWidget * load_plugin(void)
@@ -137,6 +164,8 @@ 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();
+
        return parent;
 }
 
diff --git a/gui/state.c b/gui/state.c
new file mode 100644 (file)
index 0000000..e7057f2
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+* 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 <dbus/dbus.h>
+#include <glib.h>
+
+#include "state.h"
+
+#define FRZ_FILE_EXT ".frz.gz"
+
+void game_state_fill(GameStateInfo * info, const char * rom_file)
+{
+       char * ext = strrchr(rom_file, '.');
+       char * rom_base;
+       char * frz_file;
+
+       if (!ext) {
+               rom_base = g_strdup(rom_file);
+       } else {
+               rom_base = g_strndup(rom_file, ext - rom_file);
+       }
+
+       info->rom_exists = 
+               g_file_test(rom_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
+
+       frz_file = g_strconcat(rom_base, FRZ_FILE_EXT);
+       info->has_state_file =
+               g_file_test(frz_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
+
+       g_free(frz_file);
+       g_free(rom_base);
+}
+
+// More uglyness. If you know a better way to do this please tell.
+void game_state_set(GameState newState)
+{
+       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;
+       }
+       
+       
+       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);
+}
+
diff --git a/gui/state.h b/gui/state.h
new file mode 100644 (file)
index 0000000..3982b65
--- /dev/null
@@ -0,0 +1,21 @@
+#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
index a549780..bb400fe 100644 (file)
@@ -35,27 +35,6 @@ void HgwInit()
        }
        
        hgwLaunched = true;
-       HgwStartCommand cmd = hgw_context_get_start_command(hgw);
-
-       switch (cmd) {
-               default:
-               case HGW_COMM_NONE:     // called from libosso
-               case HGW_COMM_CONT:
-                       Config.snapshotLoad = true;
-                       Config.snapshotSave = true;
-                       break;
-               case HGW_COMM_RESTART:
-                       Config.snapshotLoad = false;
-                       Config.snapshotSave = true;
-                       break;
-               case HGW_COMM_QUIT:
-                       // hum, what?
-                       Config.snapshotLoad = false;
-                       Config.snapshotSave = false;
-                       Config.quitting = true;
-                       break;
-       }
-
        printf("Loading in HGW mode\n");
 }
 
@@ -79,8 +58,29 @@ void HgwConfig()
        if (hgw_conf_request_string(hgw, kGConfRomFile, romFile) == HGW_ERR_NONE) {
                S9xSetRomFile(romFile);
        } else {
+               hgw_context_destroy(hgw, HGW_BYE_INACTIVE);
                DIE("No Rom in Gconf!");
        }
+
+       HgwStartCommand cmd = hgw_context_get_start_command(hgw);
+       switch (cmd) {
+               default:
+               case HGW_COMM_NONE:     // called from libosso
+               case HGW_COMM_CONT:
+                       Config.snapshotLoad = true;
+                       Config.snapshotSave = true;
+                       break;
+               case HGW_COMM_RESTART:
+                       Config.snapshotLoad = false;
+                       Config.snapshotSave = true;
+                       break;
+               case HGW_COMM_QUIT:
+                       // hum, what?
+                       Config.snapshotLoad = false;
+                       Config.snapshotSave = false;
+                       Config.quitting = true;
+                       break;
+       }
 }
 
 void HgwPollEvents()