e73df826ca98c0a92f35970d58f6705870f76978
[projects/chimara/chimara.git] / 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 "callbacks.h"
44 #include "error.h"
45 #include <libchimara/chimara-glk.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 void
54 on_started(ChimaraGlk *glk)
55 {
56     g_printerr("Started!\n");
57 }
58
59 static void
60 on_stopped(ChimaraGlk *glk)
61 {
62     g_printerr("Stopped!\n");
63 }
64
65 static void
66 on_waiting(ChimaraGlk *glk)
67 {
68         g_printerr("Waiting!\n");
69 }
70
71 static void
72 on_char_input(ChimaraGlk *glk, guint32 window_rock, guint keysym)
73 {
74         g_printerr("Character input in window %d: key %d\n", window_rock, keysym);
75 }
76
77 static void
78 on_line_input(ChimaraGlk *glk, guint32 window_rock, gchar *text)
79 {
80         g_printerr("Line input in window %d: '%s'\n", window_rock, text);
81 }
82
83 static void
84 on_text_buffer_output(ChimaraGlk *glk, guint32 window_rock, gchar *text)
85 {
86         g_printerr("Text buffer output in window %d: '%s'\n", window_rock, text);
87 }
88
89 static GObject *
90 load_object(const gchar *name)
91 {
92         GObject *retval;
93         if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
94                 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
95                 g_error("Error while getting object '%s'", name);
96         }
97         return retval;
98 }
99
100 static void
101 create_window(void)
102 {
103         GError *error = NULL;
104
105         builder = gtk_builder_new();
106         if( !gtk_builder_add_from_file(builder, "chimara.ui", &error) ) {
107                 error_dialog(NULL, error, "Error while building interface: ");  
108                 return;
109         }
110
111         window = GTK_WIDGET(load_object("chimara"));
112         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
113
114         /* Add all the actions to the action group. This for-loop is a temporary fix
115         and can be removed once Glade supports adding actions and accelerators to an
116         action group. */
117         const gchar *actions[] = { 
118                 "game", "",
119                 "open", "<ctrl>F7", 
120                 "save", NULL, /* NULL means use stock accelerator */
121                 "quit", NULL,
122                 NULL
123         };
124         const gchar **ptr;
125         for(ptr = actions; *ptr; ptr += 2)
126                 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
127
128         uimanager = gtk_ui_manager_new();
129         if( !gtk_ui_manager_add_ui_from_file(uimanager, "chimara.menus", &error) ) {
130                 error_dialog(NULL, error, "Error while building interface: ");
131                 return;
132         }
133         
134         glk = chimara_glk_new();
135         g_object_set(glk, "border-width", 6, "spacing", 6, NULL);
136         chimara_glk_set_default_font_string(CHIMARA_GLK(glk), "Serif 12");
137         chimara_glk_set_monospace_font_string(CHIMARA_GLK(glk), "Monospace 12");
138         g_signal_connect(glk, "started", G_CALLBACK(on_started), NULL);
139         g_signal_connect(glk, "stopped", G_CALLBACK(on_stopped), NULL);
140         g_signal_connect(glk, "waiting", G_CALLBACK(on_waiting), NULL);
141         g_signal_connect(glk, "char-input", G_CALLBACK(on_char_input), NULL);
142         g_signal_connect(glk, "line-input", G_CALLBACK(on_line_input), NULL);
143         g_signal_connect(glk, "text-buffer-output", G_CALLBACK(on_text_buffer_output), NULL);
144         
145         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
146         if(vbox == NULL)
147         {
148                 error_dialog(NULL, NULL, "Could not find vbox");
149                 return;
150         }
151
152         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
153         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
154         GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
155
156         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
157         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
158         gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
159         
160         gtk_builder_connect_signals(builder, NULL);
161 }
162
163 int
164 main(int argc, char *argv[])
165 {
166         GError *error = NULL;
167
168 #ifdef ENABLE_NLS
169         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
170         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
171         textdomain(GETTEXT_PACKAGE);
172 #endif
173
174         if( !g_thread_supported() )
175                 g_thread_init(NULL);
176         gdk_threads_init();
177         gtk_init(&argc, &argv);
178
179         create_window();
180         gtk_widget_show_all(window);
181
182         g_object_unref( G_OBJECT(builder) );
183         g_object_unref( G_OBJECT(uimanager) );
184
185     if( !chimara_glk_run(CHIMARA_GLK(glk), "../interpreters/frotz/.libs/frotz.so", argc, argv, &error) ) {
186                 error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
187                 return 1;
188         }
189
190     gdk_threads_enter();
191         gtk_main();
192         gdk_threads_leave();
193
194         chimara_glk_stop(CHIMARA_GLK(glk));
195         chimara_glk_wait(CHIMARA_GLK(glk));
196
197         return 0;
198 }