first.c compilet en draait!
[projects/chimara/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 <gtk/gtk.h>
41
42 #include "callbacks.h"
43 #include "error.h"
44 #include "event.h"
45 #include "glk.h"
46
47 /*
48  * Standard gettext macros.
49  */
50 #ifdef ENABLE_NLS
51 #  include <libintl.h>
52 #  undef _
53 #  define _(String) dgettext (PACKAGE, String)
54 #  ifdef gettext_noop
55 #    define N_(String) gettext_noop (String)
56 #  else
57 #    define N_(String) (String)
58 #  endif
59 #else
60 #  define textdomain(String) (String)
61 #  define gettext(String) (String)
62 #  define dgettext(Domain,Message) (Message)
63 #  define dcgettext(Domain,Message,Type) (Message)
64 #  define bindtextdomain(Domain,Directory) (Domain)
65 #  define _(String) (String)
66 #  define N_(String) (String)
67 #endif
68
69 #include "callbacks.h"
70
71 /* The global builder object to be used to request handles to widgets */
72 GtkBuilder *builder = NULL;
73         
74 static GtkWidget*
75 create_window(void)
76 {
77         GtkWidget *window;
78
79         if( (window = GTK_WIDGET(gtk_builder_get_object(builder, "gargoyle-gtk"))) == NULL ) {
80                 error_dialog(NULL, NULL, "Error while getting main window object");
81                 return NULL;
82         }
83
84         gtk_builder_connect_signals(builder, NULL);
85         
86         return window;
87 }
88
89 /**
90  * glk_enter:
91  *
92  * Is called to create a new thread in which glk_main() runs.
93  */
94 static gpointer
95 glk_enter(gpointer data)
96 {
97         glk_main();
98         return NULL;
99 }
100
101
102 int
103 main(int argc, char *argv[])
104 {
105         GError *error = NULL;
106         GtkWidget *window;
107         GThread *glk_thread;
108
109 #ifdef ENABLE_NLS
110         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
111         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
112         textdomain(GETTEXT_PACKAGE);
113 #endif
114
115         if( !g_thread_supported() )
116                 g_thread_init(NULL);
117
118         gdk_threads_init();
119
120         gtk_set_locale();
121         gtk_init(&argc, &argv);
122
123         builder = gtk_builder_new();
124         if( !gtk_builder_add_from_file(builder, "gargoyle-gtk.ui", &error) ) {
125                 error_dialog(NULL, error, "Error while building interface: ");  
126                 return 1;
127         }
128
129         window = create_window();
130         gtk_widget_show(window);
131
132         events_init();
133
134         /* In een aparte thread of proces */
135         if( (glk_thread = g_thread_create(glk_enter, NULL, TRUE, &error)) == NULL ) {
136                 error_dialog(NULL, error, "Error while creating glk thread: "); 
137                 g_object_unref( G_OBJECT(builder) );
138                 return 1;
139         }
140
141         gdk_threads_enter();
142         gtk_main();
143         gdk_threads_leave();
144
145         event_throw(EVENT_TYPE_QUIT, NULL, 0, 0);       
146         g_thread_join(glk_thread);
147
148         g_object_unref( G_OBJECT(builder) );
149         events_free();
150
151         return 0;
152 }