When the cursor is not on the input prompt, return it there when the user start typin...
[projects/chimara/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 /*#include "xepdmgrclient.h"*/
53
54
55 /* Global pointers to widgets */
56 GtkWidget *window = NULL;
57 GtkWidget *glk = NULL;
58
59 /* Display manager */
60 /* sEpd *epd = NULL;*/
61
62 static erClientChannel_t erbusyChannel;
63 static erClientChannel_t ertoolbarChannel;
64
65 static void
66 on_started(ChimaraGlk *glk)
67 {
68     g_printerr("Started!\n");
69 }
70
71 static void
72 on_stopped(ChimaraGlk *glk)
73 {
74     g_printerr("Stopped!\n");
75 }
76
77 static void
78 on_restore()
79 {
80         chimara_glk_feed_line_input( CHIMARA_GLK(glk), "restore" );
81 }
82
83 static void
84 on_save()
85 {
86         chimara_glk_feed_line_input( CHIMARA_GLK(glk), "save" );
87 }
88
89 gboolean
90 update_screen(gpointer data)
91 {
92         printf("Update screen from idle handler\n");
93         dmDisplay(dmCmdPriorNormal, dmQFull);
94
95         return FALSE;
96 }
97
98 static void
99 on_iliad_screen_update(ChimaraGlk *glk, gboolean typing)
100 {
101         printf("Update screen\n");
102         g_idle_add_full(G_PRIORITY_DEFAULT_IDLE+100, update_screen, NULL, NULL);
103 }
104
105
106 static void
107 create_window(void)
108 {
109         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
110         glk = chimara_if_new();
111         //chimara_if_set_preferred_interpreter( CHIMARA_IF(glk), CHIMARA_IF_FORMAT_Z8, CHIMARA_IF_INTERPRETER_NITFOL);
112
113         gtk_widget_set_size_request(window, 800, 800);
114         g_object_set(glk, 
115                 "border-width", 6, 
116                 "spacing", 6,
117                 "ignore-errors", TRUE,
118                 "style-sheet", "style.css",
119                 NULL);
120
121         g_signal_connect(glk, "started", G_CALLBACK(on_started), NULL);
122         g_signal_connect(glk, "stopped", G_CALLBACK(on_stopped), NULL);
123         g_signal_connect(glk, "iliad-screen-update", G_CALLBACK(on_iliad_screen_update), NULL);
124         
125         GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
126         GtkWidget *toolbar = gtk_toolbar_new();
127
128         GtkToolItem *restore_button = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
129         g_signal_connect(restore_button, "clicked", G_CALLBACK(on_restore), NULL);
130         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), restore_button, 0 );
131
132         GtkToolItem *save_button = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
133         g_signal_connect(save_button, "clicked", G_CALLBACK(on_save), NULL);
134         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), save_button, 0 );
135
136         GtkToolItem *quit_button = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
137         g_signal_connect(quit_button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
138         gtk_toolbar_insert( GTK_TOOLBAR(toolbar), quit_button, 0 );
139
140         GtkWidget *spacer = gtk_vbox_new(FALSE, 0);
141         gtk_widget_set_size_request(spacer, -1, 250);
142
143         gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
144         gtk_box_pack_start( GTK_BOX(vbox), glk, TRUE, TRUE, 0 );
145         gtk_box_pack_end( GTK_BOX(vbox), spacer, FALSE, FALSE, 0 );
146
147         gtk_container_add( GTK_CONTAINER(window), vbox );
148 }
149
150 static void
151 iliad_init_toolbar()
152 {
153         erIpcStartClient(ER_TOOLBAR_CHANNEL, &ertoolbarChannel);
154         tbSelectIconSet(ertoolbarChannel, ER_PDF_VIEWER_UA_ID);
155         tbClearIconSet(ertoolbarChannel, ER_PDF_VIEWER_UA_ID);
156
157         // Turn off trashcan
158         tbAppendPlatformIcon(  ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, -1);
159         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, iconState_grey );
160
161         // Enable then pop up keyboard
162         tbAppendPlatformIcon(  ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, -1);
163         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, iconState_selected);
164 }
165
166 static void
167 iliad_clear_toolbar()
168 {
169         // Turn on trashcan
170         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_trashcan, iconState_normal );
171
172         // Disable the keyboard
173         tbSetStatePlatformIcon(ertoolbarChannel, ER_PDF_VIEWER_UA_ID, iconID_keyboard, iconState_normal);
174 }
175
176 int
177 main(int argc, char *argv[])
178 {
179         GError *error = NULL;
180
181 #ifdef ENABLE_NLS
182         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
183         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
184         textdomain(GETTEXT_PACKAGE);
185 #endif
186
187         /* Setup connection to display manager deamon */
188         /*
189         if( (epd = EpdInit(NULL)) == NULL ) {
190                 g_critical("Could not connect to xepdmgr server\n");
191                 return 1;
192         }
193         EpdRefreshAuto(epd, 0);
194         */
195
196         if( !g_thread_supported() )
197                 g_thread_init(NULL);
198         gdk_threads_init();
199         gtk_init(&argc, &argv);
200
201         create_window();
202         gtk_widget_show_all(window);
203
204         if(argc < 2) {
205                 g_printerr("Must provide a game file\n");
206                 return 1;
207         }
208         
209         if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
210                 g_printerr("Error starting Glk library: %s\n", error->message);
211                 return 1;
212         }
213         //chimara_glk_run( CHIMARA_GLK(glk), ".libs/multiwin.so", argc, argv, NULL);
214         
215         iliad_init_toolbar();
216
217         gdk_threads_enter();
218         gtk_main();
219         gdk_threads_leave();
220
221         chimara_glk_stop(CHIMARA_GLK(glk));
222         chimara_glk_wait(CHIMARA_GLK(glk));
223
224         iliad_clear_toolbar();
225
226         return 0;
227 }