workaround a problem with the harmattan gcc
[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 /** Caches the freeze file of the current loaded rom, or NULL if it does not
34  *  have any.
35  *  It is updated from the game_state_update() and game_state_clear() functions.
36  */
37 static gchar * cur_frz_file = NULL;
38
39 /** Updates cur_frz_file.
40  *  @return TRUE if cur_frz_file is now not NULL (so a freeze file is present).
41  */
42 static gboolean rom_get_freeze_file()
43 {
44         char * ext;
45         char * rom_base;
46         char * frz_file;
47         gboolean frz_exists;
48
49         if (cur_frz_file) g_free(cur_frz_file);
50
51         if (!current_rom_file_exists) {
52                 cur_frz_file = NULL;
53                 return FALSE;
54         }
55
56         ext = strrchr(current_rom_file, '.');
57         if (ext && g_ascii_strcasecmp(ext, ".gz") == 0) {
58                 // Ignore the .gz part in rom filename.
59                 ext = g_strrstr_len(current_rom_file, ext - current_rom_file, ".");
60         }
61         if (!ext) {
62                 rom_base = g_strdup(current_rom_file);
63         } else {
64                 rom_base = g_strndup(current_rom_file, ext - current_rom_file);
65         }
66
67         if (current_rom_file_exists) {
68                 frz_file = g_strconcat(rom_base, FRZ_FILE_EXT, NULL);
69                 frz_exists =
70                         g_file_test(frz_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
71
72                 if (frz_exists) {
73                         cur_frz_file = frz_file;
74                 } else {
75                         cur_frz_file = NULL;
76                         g_free(frz_file);
77                 }
78         } else {
79                 cur_frz_file = NULL;
80         }
81
82         g_free(rom_base);
83
84         return cur_frz_file ? TRUE : FALSE;
85 }
86
87 // More uglyness. If you know a better way to do this please tell.
88 void game_state_update()
89 {
90         DBusError err;
91         DBusConnection* bus;
92         DBusMessage* m;
93         const char * m_name;
94         gboolean has_freeze = rom_get_freeze_file();
95
96         if (has_freeze) {
97                 m_name = "game_pause";
98         } else {
99                 m_name = "game_close";
100         }
101
102         dbus_error_init(&err);
103
104         bus = dbus_bus_get(DBUS_BUS_SESSION, &err);
105         if (dbus_error_is_set(&err)) {
106                 dbus_error_free(&err); 
107                 return;
108         }
109
110         m = dbus_message_new_method_call("com.javispedro.drnoksnes.startup",
111                                                                                 "/com/javispedro/drnoksnes/startup",
112                                                                                 "com.javispedro.drnoksnes.startup",
113                                                                                 m_name);
114
115         dbus_connection_send(bus, m, NULL);
116         dbus_connection_flush(bus);
117
118         dbus_message_unref(m);
119 }
120
121 void game_state_clear()
122 {
123         if (cur_frz_file) {
124                 g_free(cur_frz_file);
125                 cur_frz_file = NULL;
126         }
127 }
128
129 gboolean game_state_is_paused()
130 {
131         return cur_frz_file ? TRUE : FALSE;
132 }
133
134 const gchar * game_state_get_frz_file()
135 {
136         return cur_frz_file;
137 }
138