Fix (most) UniCaseTest failures (fix #27)
[projects/chimara/chimara.git] / player / main.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * main.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * main.c is free software copyrighted by Philip en Marijn.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name ``Philip en Marijn'' nor the name of any other
17  *    contributor may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  * 
20  * main.c IS PROVIDED BY Philip en Marijn ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Philip en Marijn OR ANY OTHER CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include <glib.h>
40 #include <glib/gi18n.h>
41 #include <glib/gstdio.h>
42 #include <gtk/gtk.h>
43
44 /* Use a custom GSettings backend for our preferences file */
45 #define G_SETTINGS_ENABLE_BACKEND
46 #include <gio/gsettingsbackend.h>
47
48 #include "error.h"
49 #include <libchimara/chimara-glk.h>
50 #include <libchimara/chimara-if.h>
51
52 #include "preferences.h"
53
54 /* Static global pointers to widgets */
55 static GtkUIManager *uimanager = NULL;
56 static GtkWidget *window = NULL;
57 static GtkWidget *glk = NULL;
58
59 /* Global global pointers */
60 GtkBuilder *builder = NULL;
61 GtkWidget *aboutwindow = NULL;
62 GtkWidget *prefswindow = NULL;
63 GtkWidget *toolbar = NULL;
64 GSettings *prefs_settings = NULL;
65 GSettings *state_settings = NULL;
66
67 GObject *
68 load_object(const gchar *name)
69 {
70         GObject *retval;
71         if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
72                 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
73                 g_error("Error while getting object '%s'", name);
74         }
75         return retval;
76 }
77
78 static void
79 change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window)
80 {
81         gchar *program_name, *story_name, *title;
82         g_object_get(glk, "program-name", &program_name, "story-name", &story_name, NULL);
83         if(!program_name) {
84                 gtk_window_set_title(window, "Chimara");
85                 return;
86         }
87         else if(!story_name)
88                 title = g_strdup_printf("%s - Chimara", program_name);
89         else
90                 title = g_strdup_printf("%s - %s - Chimara", program_name, story_name);
91                 
92         g_free(program_name);
93         g_free(story_name);
94         gtk_window_set_title(window, title);
95         g_free(title);
96 }
97
98 static void
99 create_window(void)
100 {
101         GError *error = NULL;
102
103         builder = gtk_builder_new();
104         if( !gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", &error) ) {
105 #ifdef DEBUG
106                 g_error_free(error);
107                 error = NULL;
108                 if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
109 #endif /* DEBUG */
110                         error_dialog(NULL, error, "Error while building interface: ");  
111                         return;
112 #ifdef DEBUG
113                 }
114 #endif /* DEBUG */
115         }
116
117         window = GTK_WIDGET(load_object("chimara"));
118         aboutwindow = GTK_WIDGET(load_object("aboutwindow"));
119         prefswindow = GTK_WIDGET(load_object("prefswindow"));
120         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
121
122         /* Set the default value of the "View/Toolbar" menu item upon creation of a
123          new window to the "show-toolbar-default" setting, but bind the setting
124          one-way only - we don't want toolbars to disappear suddenly */
125         GtkToggleAction *toolbar_action = GTK_TOGGLE_ACTION(load_object("toolbar"));
126         gtk_toggle_action_set_active(toolbar_action, g_settings_get_boolean(state_settings, "show-toolbar-default"));
127         g_settings_bind(state_settings, "show-toolbar-default", toolbar_action, "active", G_SETTINGS_BIND_SET);
128
129         const gchar **ptr;
130         GtkRecentFilter *filter = gtk_recent_filter_new();
131         /* TODO: Use mimetypes and construct the filter dynamically depending on 
132         what plugins are installed */
133         const gchar *patterns[] = {
134                 "*.z[1-8]", "*.[zg]lb", "*.[zg]blorb", "*.ulx", "*.blb", "*.blorb", NULL
135         };
136
137         for(ptr = patterns; *ptr; ptr++)
138                 gtk_recent_filter_add_pattern(filter, *ptr);
139         GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object("recent"));
140         gtk_recent_chooser_add_filter(recent, filter);
141
142         uimanager = gtk_ui_manager_new();
143         if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
144 #ifdef DEBUG
145                 g_error_free(error);
146                 error = NULL;
147                 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
148 #endif /* DEBUG */
149                         error_dialog(NULL, error, "Error while building interface: ");
150                         return;
151 #ifdef DEBUG
152                 }
153 #endif /* DEBUG */
154         }
155
156         glk = chimara_if_new();
157         g_object_set(glk,
158             "ignore-errors", TRUE,
159             /*"interpreter-number", CHIMARA_IF_ZMACHINE_TANDY_COLOR,*/
160             NULL);
161         if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_DATA_DIR "/style.css", &error) ) {
162 #ifdef DEBUG
163                 g_error_free(error);
164                 error = NULL;
165                 if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_SRC_DIR "/style.css", &error) ) {
166 #endif /* DEBUG */
167                         error_dialog(NULL, error, "Couldn't open CSS file: ");
168                         return;
169 #ifdef DEBUG
170                 }
171 #endif /* DEBUG */
172         }
173         
174         /* DON'T UNCOMMENT THIS your eyes will burn
175          but it is a good test of programmatically altering just one style
176         chimara_glk_set_css_from_string(CHIMARA_GLK(glk),
177             "buffer.normal { font-family: 'Comic Sans MS'; }");*/
178         
179         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
180         if(vbox == NULL)
181         {
182                 error_dialog(NULL, NULL, "Could not find vbox");
183                 return;
184         }
185
186         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
187         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
188         toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
189         gtk_widget_set_no_show_all(toolbar, TRUE);
190         if(gtk_toggle_action_get_active(toolbar_action))
191                 gtk_widget_show(toolbar);
192         else
193                 gtk_widget_hide(toolbar);
194
195         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
196         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
197         gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
198         
199         gtk_builder_connect_signals(builder, glk);
200         g_signal_connect(glk, "notify::program-name", G_CALLBACK(change_window_title), window);
201         g_signal_connect(glk, "notify::story-name", G_CALLBACK(change_window_title), window);
202         
203         /* Create preferences window */
204         preferences_create(CHIMARA_GLK(glk));
205 }
206
207 int
208 main(int argc, char *argv[])
209 {
210         GError *error = NULL;
211
212 #ifdef ENABLE_NLS
213         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
214         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
215         textdomain(GETTEXT_PACKAGE);
216 #endif
217
218         if( !g_thread_supported() )
219                 g_thread_init(NULL);
220         gdk_threads_init();
221         gtk_init(&argc, &argv);
222
223         /* Create configuration dir ~/.chimara */
224         gchar *configdir = g_build_filename(g_get_home_dir(), ".chimara", NULL);
225         if(!g_file_test(configdir, G_FILE_TEST_IS_DIR)
226                 && g_mkdir(configdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
227                 g_error(_("Cannot create configuration directory ~/.chimara"));
228         g_free(configdir);
229
230         /* Initialize settings file; it can be overridden by a "chimara-config" file
231          in the current directory */
232         gchar *keyfile;
233         if(g_file_test("chimara-config", G_FILE_TEST_IS_REGULAR))
234                 keyfile = g_strdup("chimara-config");
235         else
236                 keyfile = g_build_filename(g_get_home_dir(), ".chimara", "config", NULL);
237         GSettingsBackend *backend = g_keyfile_settings_backend_new(keyfile, "/org/chimara-if/player/", NULL);
238         prefs_settings = g_settings_new_with_backend("org.chimara-if.player.preferences", backend);
239         state_settings = g_settings_new_with_backend("org.chimara-if.player.state", backend);
240         g_free(keyfile);
241
242         create_window();
243         gtk_widget_show_all(window);
244
245         g_object_unref( G_OBJECT(uimanager) );
246
247         if(argc == 3) {
248                 g_object_set(glk, "graphics-file", argv[2], NULL);
249         }
250         if(argc >= 2) {
251                 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
252                         error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
253                         return 1;
254                 }
255         }
256
257     gdk_threads_enter();
258         gtk_main();
259         gdk_threads_leave();
260
261         chimara_glk_stop(CHIMARA_GLK(glk));
262         chimara_glk_wait(CHIMARA_GLK(glk));
263
264         g_object_unref( G_OBJECT(builder) );
265
266         return 0;
267 }