504ce0b02d57493f7d17f69bae08051add5e965a
[drnoksnes] / gui / state.c
1 /*
2 * This file is part of DrNokSnes
3 *
4 * Copyright (C) 2005 INdT - Instituto Nokia de Tecnologia
5 * http://www.indt.org/maemo
6 * Copyright (C) 2009 Javier S. Pedro <maemo@javispedro.com>
7 *
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This software is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this software; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 #include <string.h>
26 #include <dbus/dbus.h>
27 #include <glib.h>
28
29 #include "plugin.h"
30
31 #define FRZ_FILE_EXT ".frz.gz"
32
33 static gchar * cur_frz_file = NULL;
34
35 static gboolean rom_get_freeze_file()
36 {
37         char * ext = strrchr(current_rom_file, '.');
38         char * rom_base;
39         char * frz_file;
40         gboolean rom_exists, frz_exists;
41
42         if (cur_frz_file) g_free(cur_frz_file);
43
44         if (!ext) {
45                 rom_base = g_strdup(current_rom_file);
46         } else {
47                 rom_base = g_strndup(current_rom_file, ext - current_rom_file);
48         }
49         rom_exists = g_file_test(current_rom_file,
50                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
51
52         frz_file = g_strconcat(rom_base, FRZ_FILE_EXT);
53         frz_exists =
54                 g_file_test(frz_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
55
56         g_free(rom_base);
57
58         if (rom_exists & frz_exists) {
59                 cur_frz_file = frz_file;
60         } else {
61                 cur_frz_file = NULL;
62                 g_free(frz_file);
63         }
64
65         return rom_exists && frz_exists;
66 }
67
68 // More uglyness. If you know a better way to do this please tell.
69 void game_state_update()
70 {
71         DBusError err;
72         DBusConnection* bus;
73         DBusMessage* m;
74         const char * m_name;
75         gboolean has_freeze = rom_get_freeze_file();
76
77         if (has_freeze) {
78                 m_name = "game_pause";
79         } else {
80                 m_name = "game_close";
81         }
82
83         dbus_error_init(&err);
84
85         bus = dbus_bus_get(DBUS_BUS_SESSION, &err);
86         if (dbus_error_is_set(&err)) {
87                 dbus_error_free(&err); 
88                 return;
89         }
90
91         m = dbus_message_new_method_call("com.javispedro.drnoksnes.startup",
92                                                                                 "/com/javispedro/drnoksnes/startup",
93                                                                                 "com.javispedro.drnoksnes.startup",
94                                                                                 m_name);
95
96         dbus_connection_send(bus, m, NULL);
97         dbus_connection_flush(bus);
98
99         dbus_message_unref(m);
100 }
101
102 void game_state_clear()
103 {
104         if (cur_frz_file) {
105                 g_free(cur_frz_file);
106                 cur_frz_file = NULL;
107         }
108 }
109
110 gboolean game_state_is_paused()
111 {
112         return cur_frz_file ? TRUE : FALSE;
113 }
114
115 const gchar * game_state_get_frz_file()
116 {
117         return cur_frz_file;
118 }
119