Seperation of client code from the tests
[rodin/chimara.git] / client / 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 /* Global pointers to widgets */
48 GtkBuilder *builder = NULL;
49 GtkUIManager *uimanager = NULL;
50 GtkWidget *window = NULL;
51 GtkWidget *glk = NULL;
52
53 static GObject *
54 load_object(const gchar *name)
55 {
56         GObject *retval;
57         if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
58                 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
59                 g_error("Error while getting object '%s'", name);
60         }
61         return retval;
62 }
63
64 static void
65 create_window(void)
66 {
67         GError *error = NULL;
68
69         builder = gtk_builder_new();
70         if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
71                 error_dialog(NULL, error, "Error while building interface: ");  
72                 return;
73         }
74
75         window = GTK_WIDGET(load_object("chimara"));
76         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
77
78         /* Add all the actions to the action group. This for-loop is a temporary fix
79         and can be removed once Glade supports adding actions and accelerators to an
80         action group. */
81         const gchar *actions[] = { 
82                 "game", "",
83                 "open", "<ctrl>F7", 
84                 "save", NULL, /* NULL means use stock accelerator */
85                 "quit", NULL,
86                 "hint", "",
87                 "char_input", "",
88                 "char_input2", "",
89                 NULL
90         };
91         const gchar **ptr;
92         for(ptr = actions; *ptr; ptr += 2)
93                 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
94
95         uimanager = gtk_ui_manager_new();
96         if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
97                 error_dialog(NULL, error, "Error while building interface: ");
98                 return;
99         }
100         
101         glk = chimara_if_new();
102         //chimara_if_set_preferred_interpreter( CHIMARA_IF(glk), CHIMARA_IF_FORMAT_Z8, CHIMARA_IF_INTERPRETER_NITFOL);
103
104         g_object_set(glk, 
105                 "border-width", 6, 
106                 "spacing", 6,
107                 "ignore-errors", TRUE,
108                 NULL);
109         chimara_glk_set_default_font_string(CHIMARA_GLK(glk), "Serif 12");
110         chimara_glk_set_monospace_font_string(CHIMARA_GLK(glk), "Monospace 12");
111         
112         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
113         if(vbox == NULL)
114         {
115                 error_dialog(NULL, NULL, "Could not find vbox");
116                 return;
117         }
118
119         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
120         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
121         GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
122
123         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
124         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
125         gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
126         
127         gtk_builder_connect_signals(builder, glk);
128 }
129
130 int
131 main(int argc, char *argv[])
132 {
133         GError *error = NULL;
134
135 #ifdef ENABLE_NLS
136         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
137         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
138         textdomain(GETTEXT_PACKAGE);
139 #endif
140
141         if( !g_thread_supported() )
142                 g_thread_init(NULL);
143         gdk_threads_init();
144         gtk_init(&argc, &argv);
145
146         create_window();
147         gtk_widget_show_all(window);
148
149         g_object_unref( G_OBJECT(builder) );
150         g_object_unref( G_OBJECT(uimanager) );
151
152         if(argc < 2) {
153                 error_dialog(GTK_WINDOW(window), NULL, "Must provide a game file");
154                 return 1;
155         }
156         
157     if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
158                 error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
159                 return 1;
160         }
161
162     gdk_threads_enter();
163         gtk_main();
164         gdk_threads_leave();
165
166         chimara_glk_stop(CHIMARA_GLK(glk));
167         chimara_glk_wait(CHIMARA_GLK(glk));
168
169         return 0;
170 }