Added stubs for all Gargoyle extensions and documented them
[rodin/chimara.git] / player / iliad.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * iliad.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * iliad.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 /* Iliad includes */
48 #include <liberdm/erdm.h>
49 #include <liberipc/eripcviewer.h>
50 #include <liberipc/eripctoolbar.h>
51 #include <liberipc/eripcbusyd.h>
52
53 /* Global pointers to widgets */
54 GtkWidget *window = NULL;
55 GtkWidget *glk = NULL;
56
57 static erClientChannel_t erbusyChannel;
58 static erClientChannel_t ertoolbarChannel;
59
60 static void
61 on_started(ChimaraGlk *glk)
62 {
63     g_printerr("Started!\n");
64 }
65
66 static void
67 on_stopped(ChimaraGlk *glk)
68 {
69     g_printerr("Stopped!\n");
70 }
71
72 static void
73 on_restore()
74 {
75         chimara_glk_feed_line_input( CHIMARA_GLK(glk), "restore" );
76 }
77
78 static void
79 on_save()
80 {
81         chimara_glk_feed_line_input( CHIMARA_GLK(glk), "save" );
82 }
83
84 static void
85 create_window(void)
86 {
87         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
88         glk = chimara_if_new();
89         //chimara_if_set_preferred_interpreter( CHIMARA_IF(glk), CHIMARA_IF_FORMAT_Z8, CHIMARA_IF_INTERPRETER_NITFOL);
90
91         gtk_widget_set_size_request(window, 800, 800);
92         g_object_set(glk, 
93                 "border-width", 6, 
94                 "spacing", 6,
95                 "ignore-errors", TRUE,
96                 NULL);
97         chimara_glk_set_default_font_string(CHIMARA_GLK(glk), "Serif 12");
98         chimara_glk_set_monospace_font_string(CHIMARA_GLK(glk), "Monospace 12");
99         g_signal_connect(glk, "started", G_CALLBACK(on_started), NULL);
100         g_signal_connect(glk, "stopped", G_CALLBACK(on_stopped), NULL);
101         
102         GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
103         GtkWidget *toolbar = gtk_toolbar_new();
104
105         GtkToolItem *restore_button = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
106         g_signal_connect(restore_button, "clicked", G_CALLBACK(on_restore), NULL);
107         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), restore_button, 0 );
108
109         GtkToolItem *save_button = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
110         g_signal_connect(save_button, "clicked", G_CALLBACK(on_save), NULL);
111         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), save_button, 0 );
112
113         GtkToolItem *quit_button = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
114         g_signal_connect(quit_button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
115         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), quit_button, 0 );
116
117         GtkWidget *spacer = gtk_vbox_new(FALSE, 0);
118         gtk_widget_set_size_request(spacer, -1, 250);
119
120         gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
121         gtk_box_pack_start( GTK_BOX(vbox), glk, TRUE, TRUE, 0 );
122         gtk_box_pack_end( GTK_BOX(vbox), spacer, FALSE, FALSE, 0 );
123
124         gtk_container_add( GTK_CONTAINER(window), vbox );
125 }
126
127 static void
128 iliad_init_toolbar()
129 {
130         erIpcStartClient(ER_TOOLBAR_CHANNEL, &ertoolbarChannel);
131         tbSelectIconSet(ertoolbarChannel, ER_PDF_VIEWER_UA_ID);
132         tbClearIconSet(ertoolbarChannel, ER_PDF_VIEWER_UA_ID);
133
134         // Turn off trashcan
135         tbAppendPlatformIcon(  ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, -1);
136         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, iconState_grey );
137
138         // Enable then pop up keyboard
139         tbAppendPlatformIcon(  ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, -1);
140         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, iconState_selected);
141 }
142
143 static void
144 iliad_clear_toolbar()
145 {
146         // Turn on trashcan
147         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, iconState_normal );
148
149         // Disable the keyboard
150         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, iconState_normal);
151 }
152
153 int
154 main(int argc, char *argv[])
155 {
156         GError *error = NULL;
157
158 #ifdef ENABLE_NLS
159         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
160         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
161         textdomain(GETTEXT_PACKAGE);
162 #endif
163
164         if( !g_thread_supported() )
165                 g_thread_init(NULL);
166         gdk_threads_init();
167         gtk_init(&argc, &argv);
168
169         create_window();
170         gtk_widget_show_all(window);
171
172         if(argc < 2) {
173                 g_printerr("Must provide a game file\n");
174                 return 1;
175         }
176         
177         if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
178                 g_printerr("Error starting Glk library: %s\n", error->message);
179                 return 1;
180         }
181         //chimara_glk_run( CHIMARA_GLK(glk), ".libs/multiwin.so", argc, argv, NULL);
182         
183         iliad_init_toolbar();
184
185         gdk_threads_enter();
186         gtk_main();
187         gdk_threads_leave();
188
189         chimara_glk_stop(CHIMARA_GLK(glk));
190         chimara_glk_wait(CHIMARA_GLK(glk));
191
192         iliad_clear_toolbar();
193
194         return 0;
195 }