962ca30e0ba3f05198a9f55d163f56a2e8646769
[rodin/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 <gtk/gtk.h>
42
43 #include "error.h"
44 #include <libchimara/chimara-glk.h>
45 #include <libchimara/chimara-if.h>
46
47 /* Static global pointers to widgets */
48 static GtkBuilder *builder = NULL;
49 static GtkUIManager *uimanager = NULL;
50 static GtkWidget *window = NULL;
51 static GtkWidget *glk = NULL;
52
53 /* Global global pointers */
54 GtkWidget *aboutwindow = NULL;
55 GtkWidget *prefswindow = NULL;
56
57 static GObject *
58 load_object(const gchar *name)
59 {
60         GObject *retval;
61         if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
62                 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
63                 g_error("Error while getting object '%s'", name);
64         }
65         return retval;
66 }
67
68 static void
69 change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window)
70 {
71         gchar *program_name, *story_name, *title;
72         g_object_get(glk, "program-name", &program_name, "story-name", &story_name, NULL);
73         if(!program_name) {
74                 gtk_window_set_title(window, "Chimara");
75                 return;
76         }
77         else if(!story_name)
78                 title = g_strdup_printf("%s - Chimara", program_name);
79         else
80                 title = g_strdup_printf("%s - %s - Chimara", program_name, story_name);
81                 
82         g_free(program_name);
83         g_free(story_name);
84         gtk_window_set_title(window, title);
85         g_free(title);
86 }
87
88 static void
89 create_window(void)
90 {
91         GError *error = NULL;
92
93         builder = gtk_builder_new();
94         if( !gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", &error) ) {
95 #ifdef DEBUG
96                 g_error_free(error);
97                 error = NULL;
98                 if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
99 #endif /* DEBUG */
100                         error_dialog(NULL, error, "Error while building interface: ");  
101                         return;
102 #ifdef DEBUG
103                 }
104 #endif /* DEBUG */
105         }
106
107         window = GTK_WIDGET(load_object("chimara"));
108         aboutwindow = GTK_WIDGET(load_object("aboutwindow"));
109         prefswindow = GTK_WIDGET(load_object("prefswindow"));
110         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
111
112         /* Add all the actions to the action group. This for-loop is a temporary fix
113         and can be removed once Glade supports adding actions and accelerators to an
114         action group. */
115         const gchar *actions[] = { 
116                 "game", "",
117                 "open", "<ctrl>O", 
118                 "recent", "",
119                 "stop", "",
120                 "quit_chimara", NULL, /* NULL means use stock accelerator */
121                 "command", "",
122                 "undo", "<ctrl>Z",
123                 "save", NULL, 
124                 "restore", "<ctrl>R", 
125                 "restart", "",
126                 "quit", "",
127                 "edit", "",
128                 "copy", NULL,
129                 "paste", NULL,
130                 "preferences", "",
131                 "help", "",
132                 "about", "",
133                 NULL
134         };
135         const gchar **ptr;
136         for(ptr = actions; *ptr; ptr += 2)
137                 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
138         GtkRecentFilter *filter = gtk_recent_filter_new();
139         /* TODO: Use mimetypes and construct the filter dynamically depending on 
140         what plugins are installed */
141         const gchar *patterns[] = {
142                 "*.z[1-8]", "*.[zg]lb", "*.[zg]blorb", "*.ulx", "*.blb", "*.blorb", NULL
143         };
144         for(ptr = patterns; *ptr; ptr++)
145                 gtk_recent_filter_add_pattern(filter, *ptr);
146         GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object("recent"));
147         gtk_recent_chooser_add_filter(recent, filter);
148
149         uimanager = gtk_ui_manager_new();
150         if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
151 #ifdef DEBUG
152                 g_error_free(error);
153                 error = NULL;
154                 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
155 #endif /* DEBUG */
156                         error_dialog(NULL, error, "Error while building interface: ");
157                         return;
158 #ifdef DEBUG
159                 }
160 #endif /* DEBUG */
161         }
162         
163         glk = chimara_if_new();
164         g_object_set(glk, "ignore-errors", TRUE, NULL);
165         if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_DATA_DIR "/style.css", &error) ) {
166 #ifdef DEBUG
167                 g_error_free(error);
168                 error = NULL;
169                 if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_SRC_DIR "/macstyle.css", &error) ) {
170 #endif /* DEBUG */
171                         error_dialog(NULL, error, "Couldn't open CSS file: ");
172                         return;
173 #ifdef DEBUG
174                 }
175 #endif /* DEBUG */
176         }
177         
178         /* DON'T UNCOMMENT THIS your eyes will burn
179          but it is a good test of programmatically altering just one style
180         chimara_glk_set_css_from_string(CHIMARA_GLK(glk),
181             "buffer.normal { font-family: 'Comic Sans MS'; }");*/
182         
183         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
184         if(vbox == NULL)
185         {
186                 error_dialog(NULL, NULL, "Could not find vbox");
187                 return;
188         }
189
190         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
191         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
192         GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
193
194         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
195         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
196         gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
197         
198         gtk_builder_connect_signals(builder, glk);
199         g_signal_connect(glk, "notify::program-name", G_CALLBACK(change_window_title), window);
200         g_signal_connect(glk, "notify::story-name", G_CALLBACK(change_window_title), window);
201 }
202
203 int
204 main(int argc, char *argv[])
205 {
206         GError *error = NULL;
207
208 #ifdef ENABLE_NLS
209         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
210         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
211         textdomain(GETTEXT_PACKAGE);
212 #endif
213
214         if( !g_thread_supported() )
215                 g_thread_init(NULL);
216         gdk_threads_init();
217         gtk_init(&argc, &argv);
218
219         create_window();
220         gtk_widget_show_all(window);
221
222         g_object_unref( G_OBJECT(builder) );
223         g_object_unref( G_OBJECT(uimanager) );
224
225         if(argc >= 2) {
226                 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
227                         error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
228                         return 1;
229                 }
230         }
231
232     gdk_threads_enter();
233         gtk_main();
234         gdk_threads_leave();
235
236         chimara_glk_stop(CHIMARA_GLK(glk));
237         chimara_glk_wait(CHIMARA_GLK(glk));
238
239         return 0;
240 }