Added dynamic module loading: now the Glk program (i.e., the
[rodin/chimara.git] / src / 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 "chimara-glk.h"
46
47 /* Global pointers to widgets */
48 GtkBuilder *builder = NULL;
49 GtkWidget *window = NULL;
50 GtkWidget *glk = NULL;
51
52 static void
53 create_window(void)
54 {
55         if( (window = GTK_WIDGET(gtk_builder_get_object(builder, "gargoyle-gtk"))) == NULL ) {
56                 error_dialog(NULL, NULL, "Error while getting main window object");
57                 return;
58         }
59
60         gtk_builder_connect_signals(builder, NULL);
61         
62         glk = chimara_glk_new();
63         
64         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
65         if(vbox == NULL)
66         {
67                 error_dialog(NULL, NULL, "Could not find vbox");
68                 return;
69         }
70
71         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77         GError *error = NULL;
78
79 #ifdef ENABLE_NLS
80         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
81         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
82         textdomain(GETTEXT_PACKAGE);
83 #endif
84
85         if( !g_thread_supported() )
86                 g_thread_init(NULL);
87
88         gdk_threads_init();
89     
90         gtk_set_locale();
91         gtk_init(&argc, &argv);
92
93         builder = gtk_builder_new();
94         if( !gtk_builder_add_from_file(builder, "chimara.ui", &error) ) {
95                 error_dialog(NULL, error, "Error while building interface: ");  
96                 return 1;
97         }
98
99         create_window();
100         gtk_widget_show_all(window);
101
102         g_object_unref( G_OBJECT(builder) );
103
104     if( !chimara_glk_run(CHIMARA_GLK(glk), ".libs/first.so", &error) ) {
105         error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
106         return 1;
107     }
108
109     gdk_threads_enter();
110         gtk_main();
111         gdk_threads_leave();
112
113         chimara_glk_stop(CHIMARA_GLK(glk));
114         chimara_glk_wait(CHIMARA_GLK(glk));
115
116         return 0;
117 }