* Path fixes
[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 create_window(void)
70 {
71         GError *error = NULL;
72
73         builder = gtk_builder_new();
74         if( !gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", &error) ) {
75 #ifdef DEBUG
76                 g_error_free(error);
77                 error = NULL;
78                 if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
79 #endif /* DEBUG */
80                         error_dialog(NULL, error, "Error while building interface: ");  
81                         return;
82 #ifdef DEBUG
83                 }
84 #endif /* DEBUG */
85         }
86
87         window = GTK_WIDGET(load_object("chimara"));
88         aboutwindow = GTK_WIDGET(load_object("aboutwindow"));
89         prefswindow = GTK_WIDGET(load_object("prefswindow"));
90         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
91
92         /* Add all the actions to the action group. This for-loop is a temporary fix
93         and can be removed once Glade supports adding actions and accelerators to an
94         action group. */
95         const gchar *actions[] = { 
96                 "game", "",
97                 "open", "<ctrl>O", 
98                 "recent", "",
99                 "stop", "",
100                 "quit_chimara", NULL, /* NULL means use stock accelerator */
101                 "command", "",
102                 "undo", "<ctrl>Z",
103                 "save", NULL, 
104                 "restore", "<ctrl>R", 
105                 "restart", "",
106                 "quit", "",
107                 "edit", "",
108                 "copy", NULL,
109                 "paste", NULL,
110                 "preferences", "",
111                 "help", "",
112                 "about", "",
113                 NULL
114         };
115         const gchar **ptr;
116         for(ptr = actions; *ptr; ptr += 2)
117                 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
118         GtkRecentFilter *filter = gtk_recent_filter_new();
119         /* TODO: Use mimetypes and construct the filter dynamically depending on 
120         what plugins are installed */
121         const gchar *patterns[] = {
122                 "*.z[1-8]", "*.[zg]lb", "*.[zg]blorb", "*.ulx", "*.blb", "*.blorb", NULL
123         };
124         for(ptr = patterns; *ptr; ptr++)
125                 gtk_recent_filter_add_pattern(filter, *ptr);
126         GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object("recent"));
127         gtk_recent_chooser_add_filter(recent, filter);
128
129         uimanager = gtk_ui_manager_new();
130         if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
131 #ifdef DEBUG
132                 g_error_free(error);
133                 error = NULL;
134                 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
135 #endif /* DEBUG */
136                         error_dialog(NULL, error, "Error while building interface: ");
137                         return;
138 #ifdef DEBUG
139                 }
140 #endif /* DEBUG */
141         }
142         
143         glk = chimara_if_new();
144         g_object_set(glk, "ignore-errors", TRUE, NULL);
145         
146         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
147         if(vbox == NULL)
148         {
149                 error_dialog(NULL, NULL, "Could not find vbox");
150                 return;
151         }
152
153         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
154         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
155         GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
156
157         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
158         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
159         gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
160         
161         gtk_builder_connect_signals(builder, glk);
162 }
163
164 int
165 main(int argc, char *argv[])
166 {
167         GError *error = NULL;
168
169 #ifdef ENABLE_NLS
170         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
171         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
172         textdomain(GETTEXT_PACKAGE);
173 #endif
174
175         if( !g_thread_supported() )
176                 g_thread_init(NULL);
177         gdk_threads_init();
178         gtk_init(&argc, &argv);
179
180         create_window();
181         gtk_widget_show_all(window);
182
183         g_object_unref( G_OBJECT(builder) );
184         g_object_unref( G_OBJECT(uimanager) );
185
186         if(argc >= 2) {
187                 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
188                         error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
189                         return 1;
190                 }
191         }
192
193     gdk_threads_enter();
194         gtk_main();
195         gdk_threads_leave();
196
197         chimara_glk_stop(CHIMARA_GLK(glk));
198         chimara_glk_wait(CHIMARA_GLK(glk));
199
200         return 0;
201 }