* Added the game anchorhead to test
[projects/chimara/chimara.git] / libchimara / chimara-glk.c
1 /* licensing and copyright information here */
2
3 #include <math.h>
4 #include <gtk/gtk.h>
5 #include <glib/gi18n.h>
6 #include <gmodule.h>
7 #include <pango/pango.h>
8 #include "chimara-glk.h"
9 #include "chimara-glk-private.h"
10 #include "glk.h"
11 #include "abort.h"
12 #include "window.h"
13 #include "glkstart.h"
14
15 #define CHIMARA_GLK_MIN_WIDTH 0
16 #define CHIMARA_GLK_MIN_HEIGHT 0
17
18 /**
19  * SECTION:chimara-glk
20  * @short_description: Widget which executes a Glk program
21  * @stability: Unstable
22  * @include: chimara/chimara-glk.h
23  * 
24  * The ChimaraGlk widget opens and runs a Glk program. The program must be
25  * compiled as a plugin module, with a function <function>glk_main()</function>
26  * that the Glk library can hook into.
27  *
28  * On Linux systems, this is a file with a name like 
29  * <filename>plugin.so</filename>. For portability, you can use libtool and 
30  * automake:
31  * |[
32  * pkglib_LTLIBRARIES = plugin.la
33  * plugin_la_SOURCES = plugin.c foo.c bar.c
34  * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
35  * ]|
36  * This will produce <filename>plugin.la</filename> which is a text file 
37  * containing the correct plugin file to open (see the relevant section of the
38  * <ulink 
39  * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
40  * Libtool manual</ulink>).
41  */
42
43 typedef void (* glk_main_t) (void);
44 typedef void (* glkunix_startup_code_t) (glkunix_startup_t*);
45
46 enum {
47     PROP_0,
48     PROP_INTERACTIVE,
49     PROP_PROTECT,
50         PROP_DEFAULT_FONT_DESCRIPTION,
51         PROP_MONOSPACE_FONT_DESCRIPTION,
52         PROP_SPACING
53 };
54
55 enum {
56         STOPPED,
57         STARTED,
58
59         LAST_SIGNAL
60 };
61
62 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
63
64 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
65
66 static void
67 chimara_glk_init(ChimaraGlk *self)
68 {
69     GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
70
71     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
72     
73     priv->self = self;
74     priv->interactive = TRUE;
75     priv->protect = FALSE;
76         priv->default_font_desc = pango_font_description_from_string("Sans");
77         priv->monospace_font_desc = pango_font_description_from_string("Monospace");
78     priv->program = NULL;
79     priv->thread = NULL;
80     priv->event_queue = NULL;
81     priv->event_lock = NULL;
82     priv->event_queue_not_empty = NULL;
83     priv->event_queue_not_full = NULL;
84     priv->abort_lock = NULL;
85     priv->abort_signalled = FALSE;
86     priv->interrupt_handler = NULL;
87     priv->root_window = NULL;
88     priv->fileref_list = NULL;
89     priv->current_stream = NULL;
90     priv->stream_list = NULL;
91         priv->timer_id = 0;
92 }
93
94 static void
95 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
96 {
97     ChimaraGlk *glk = CHIMARA_GLK(object);
98     
99     switch(prop_id) 
100     {
101         case PROP_INTERACTIVE:
102             chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
103             break;
104         case PROP_PROTECT:
105             chimara_glk_set_protect( glk, g_value_get_boolean(value) );
106             break;
107                 case PROP_DEFAULT_FONT_DESCRIPTION:
108                         chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
109                         break;
110                 case PROP_MONOSPACE_FONT_DESCRIPTION:
111                         chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
112                         break;
113                 case PROP_SPACING:
114                         chimara_glk_set_spacing( glk, g_value_get_uint(value) );
115                         break;
116         default:
117             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
118     }
119 }
120
121 static void
122 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
123 {
124     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
125     
126     switch(prop_id)
127     {
128         case PROP_INTERACTIVE:
129             g_value_set_boolean(value, priv->interactive);
130             break;
131         case PROP_PROTECT:
132             g_value_set_boolean(value, priv->protect);
133             break;
134                 case PROP_DEFAULT_FONT_DESCRIPTION:
135                         g_value_set_pointer(value, priv->default_font_desc);
136                         break;
137                 case PROP_MONOSPACE_FONT_DESCRIPTION:
138                         g_value_set_pointer(value, priv->monospace_font_desc);
139                         break;
140                 case PROP_SPACING:
141                         g_value_set_uint(value, priv->spacing);
142                         break;
143         default:
144             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
145     }
146 }
147
148 static void
149 chimara_glk_finalize(GObject *object)
150 {
151     ChimaraGlk *self = CHIMARA_GLK(object);
152     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
153     
154     /* Free the event queue */
155     g_mutex_lock(priv->event_lock);
156         g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
157         g_queue_free(priv->event_queue);
158         g_cond_free(priv->event_queue_not_empty);
159         g_cond_free(priv->event_queue_not_full);
160         priv->event_queue = NULL;
161         g_mutex_unlock(priv->event_lock);
162         g_mutex_free(priv->event_lock);
163         
164         /* Free the abort signalling mechanism */
165         g_mutex_lock(priv->abort_lock);
166         /* Make sure no other thread is busy with this */
167         g_mutex_unlock(priv->abort_lock);
168         g_mutex_free(priv->abort_lock);
169         priv->abort_lock = NULL;
170
171         /* Free private data */
172         pango_font_description_free(priv->default_font_desc);
173         pango_font_description_free(priv->monospace_font_desc);
174         
175     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
176 }
177
178 /* Internal function: Recursively get the Glk window tree's size request */
179 static void
180 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
181 {
182         if(win->type == wintype_Pair)
183         {
184                 /* Get children's size requests */
185                 GtkRequisition child1, child2;
186                 request_recurse(win->window_node->children->data, &child1, spacing);
187                 request_recurse(win->window_node->children->next->data, &child2, spacing);
188                 
189                 /* If the split is fixed, get the size of the fixed child */
190                 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
191                 {
192                         switch(win->split_method & winmethod_DirMask)
193                         {
194                                 case winmethod_Left:
195                                         child1.width = win->key_window?
196                                                 win->constraint_size * win->key_window->unit_width
197                                                 : 0;
198                                         break;
199                                 case winmethod_Right:
200                                         child2.width = win->key_window?
201                                                 win->constraint_size * win->key_window->unit_width
202                                                 : 0;
203                                         break;
204                                 case winmethod_Above:
205                                         child1.height = win->key_window?
206                                                 win->constraint_size * win->key_window->unit_height
207                                                 : 0;
208                                         break;
209                                 case winmethod_Below:
210                                         child2.height = win->key_window?
211                                                 win->constraint_size * win->key_window->unit_height
212                                                 : 0;
213                                         break;
214                         }
215                 }
216                 
217                 /* Add the children's requests */
218                 switch(win->split_method & winmethod_DirMask)
219                 {
220                         case winmethod_Left:
221                         case winmethod_Right:
222                                 requisition->width = child1.width + child2.width + spacing;
223                                 requisition->height = MAX(child1.height, child2.height);
224                                 break;
225                         case winmethod_Above:
226                         case winmethod_Below:
227                                 requisition->width = MAX(child1.width, child2.width);
228                                 requisition->height = child1.height + child2.height + spacing;
229                                 break;
230                 }
231         }
232         
233         /* For non-pair windows, just use the size that GTK requests */
234         else
235                 gtk_widget_size_request(win->frame, requisition);
236 }
237
238 /* Overrides gtk_widget_size_request */
239 static void
240 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
241 {
242     g_return_if_fail(widget);
243     g_return_if_fail(requisition);
244     g_return_if_fail(CHIMARA_IS_GLK(widget));
245     
246     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
247     
248     /* For now, just pass the size request on to the root Glk window */
249     if(priv->root_window) 
250         {
251                 request_recurse(priv->root_window->data, requisition, priv->spacing);
252                 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
253                 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
254         } 
255         else 
256         {
257         requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
258         requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
259     }
260 }
261
262 /* Recursively give the Glk windows their allocated space */
263 static void
264 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
265 {
266         if(win->type == wintype_Pair)
267         {
268                 GtkAllocation child1, child2;
269                 child1.x = allocation->x;
270                 child1.y = allocation->y;
271                 
272                 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
273                 {
274                         /* If the key window has been closed, then default to 0; otherwise
275                          use the key window to determine the size */
276                         switch(win->split_method & winmethod_DirMask)
277                         {
278                                 case winmethod_Left:
279                                         child1.width = win->key_window? 
280                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing) 
281                                                 : 0;
282                                         break;
283                                 case winmethod_Right:
284                                         child2.width = win->key_window? 
285                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
286                                                 : 0;
287                                         break;
288                                 case winmethod_Above:
289                                         child1.height = win->key_window? 
290                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
291                                                 : 0;
292                                         break;
293                                 case winmethod_Below:
294                                         child2.height = win->key_window?
295                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
296                                                 : 0;
297                                         break;
298                         }
299                 }
300                 else /* proportional */
301                 {
302                         gdouble fraction = win->constraint_size / 100.0;
303                         switch(win->split_method & winmethod_DirMask)
304                         {
305                                 case winmethod_Left:
306                                         child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
307                                         break;
308                                 case winmethod_Right:
309                                         child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
310                                         break;
311                                 case winmethod_Above:
312                                         child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
313                                         break;
314                                 case winmethod_Below:
315                                         child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
316                                         break;
317                         }
318                 }
319                 
320                 /* Fill in the rest of the size requisitions according to the child specified above */
321                 switch(win->split_method & winmethod_DirMask)
322                 {
323                         case winmethod_Left:
324                                 child2.width = allocation->width - spacing - child1.width;
325                                 child2.x = child1.x + child1.width + spacing;
326                                 child2.y = child1.y;
327                                 child1.height = child2.height = allocation->height;
328                                 break;
329                         case winmethod_Right:
330                                 child1.width = allocation->width - spacing - child2.width;
331                                 child2.x = child1.x + child1.width + spacing;
332                                 child2.y = child1.y;
333                                 child1.height = child2.height = allocation->height;
334                                 break;
335                         case winmethod_Above:
336                                 child2.height = allocation->height - spacing - child1.height;
337                                 child2.x = child1.x;
338                                 child2.y = child1.y + child1.height + spacing;
339                                 child1.width = child2.width = allocation->width;
340                                 break;
341                         case winmethod_Below:
342                                 child1.height = allocation->height - spacing - child2.height;
343                                 child2.x = child1.x;
344                                 child2.y = child1.y + child1.height + spacing;
345                                 child1.width = child2.width = allocation->width;
346                                 break;
347                 }
348                 
349                 /* Recurse */
350                 allocate_recurse(win->window_node->children->data, &child1, spacing);
351                 allocate_recurse(win->window_node->children->next->data, &child2, spacing);
352         }
353         
354         else if(win->type == wintype_TextGrid)
355         {
356                 /* Pass the size allocation on to the framing widget */
357                 gtk_widget_size_allocate(win->frame, allocation);
358                 /* It says in the spec that when a text grid window is resized smaller,
359                  the bottom or right area is thrown away; when it is resized larger, the
360                  bottom or right area is filled with blanks. */
361                 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
362                 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
363                 gint line;
364                 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
365                 GtkTextIter start, end;
366         
367                 for(line = 0; line < win->height; line++)
368                 {
369                         gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
370                         /* If this line is going to fall off the bottom, delete it */
371                         if(line >= newheight)
372                         {
373                                 end = start;
374                                 gtk_text_iter_forward_to_line_end(&end);
375                                 gtk_text_iter_forward_char(&end);
376                                 gtk_text_buffer_delete(textbuffer, &start, &end);
377                                 break;
378                         }
379                         /* If this line is not long enough, add spaces on the end */
380                         if(newwidth > win->width)
381                         {
382                                 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
383                                 gtk_text_iter_forward_to_line_end(&start);
384                                 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
385                                 g_free(spaces);
386                         }
387                         /* But if it's too long, delete characters from the end */
388                         else if(newwidth < win->width)
389                         {
390                                 end = start;
391                                 gtk_text_iter_forward_chars(&start, newwidth);
392                                 gtk_text_iter_forward_to_line_end(&end);
393                                 gtk_text_buffer_delete(textbuffer, &start, &end);
394                         }
395                         /* Note: if the widths are equal, do nothing */
396                 }
397                 /* Add blank lines if there aren't enough lines to fit the new size */
398                 if(newheight > win->height)
399                 {
400                         gchar *blanks = g_strnfill(win->width, ' ');
401                     gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
402                     int count;
403                     for(count = 0; count < newheight - win->height; count++)
404                         blanklines[count] = blanks;
405                     blanklines[newheight - win->height] = NULL;
406                     gchar *text = g_strjoinv("\n", blanklines);
407                     g_free(blanklines); /* not g_strfreev() */
408                     g_free(blanks);
409                     
410                         gtk_text_buffer_get_end_iter(textbuffer, &start);
411                         gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
412                     gtk_text_buffer_insert(textbuffer, &start, text, -1);
413                     g_free(text);
414                 }
415         
416                 win->width = newwidth;
417                 win->height = newheight;
418         }
419         
420         /* For non-pair, non-text-grid windows, just give them the size */
421         else
422                 gtk_widget_size_allocate(win->frame, allocation);
423 }
424
425 /* Overrides gtk_widget_size_allocate */
426 static void
427 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
428 {
429     g_return_if_fail(widget);
430     g_return_if_fail(allocation);
431     g_return_if_fail(CHIMARA_IS_GLK(widget));
432     
433     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
434     
435     widget->allocation = *allocation;
436             
437     if(priv->root_window) {
438                 GtkAllocation child;
439                 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
440                 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
441                 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
442                 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
443                 allocate_recurse(priv->root_window->data, &child, priv->spacing);
444         }
445 }
446
447 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
448 static void
449 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
450 {
451         if(win->type == wintype_Pair)
452         {
453                 forall_recurse(win->window_node->children->data, callback, callback_data);
454                 forall_recurse(win->window_node->children->next->data, callback, callback_data);
455         }
456         else
457                 (*callback)(win->frame, callback_data);
458 }
459
460 /* Overrides gtk_container_forall */
461 static void
462 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
463 {
464     g_return_if_fail(container);
465     g_return_if_fail(CHIMARA_IS_GLK(container));
466     
467     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
468     
469         /* All the children are "internal" */
470         if(!include_internals)
471                 return;
472         
473     if(priv->root_window)
474                 forall_recurse(priv->root_window->data, callback, callback_data);
475 }
476
477 static void
478 chimara_glk_stopped(ChimaraGlk *self)
479 {
480     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
481
482     /* Free the plugin */
483         if( priv->program && !g_module_close(priv->program) )
484             g_warning( "Error closing module: %s", g_module_error() );
485 }
486
487 static void
488 chimara_glk_started(ChimaraGlk *self)
489 {
490         /* TODO: Add default signal handler implementation here */
491 }
492
493 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
494 #ifndef G_PARAM_STATIC_STRINGS
495 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
496 #endif
497
498 static void
499 chimara_glk_class_init(ChimaraGlkClass *klass)
500 {
501     /* Override methods of parent classes */
502     GObjectClass *object_class = G_OBJECT_CLASS(klass);
503     object_class->set_property = chimara_glk_set_property;
504     object_class->get_property = chimara_glk_get_property;
505     object_class->finalize = chimara_glk_finalize;
506     
507     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
508     widget_class->size_request = chimara_glk_size_request;
509     widget_class->size_allocate = chimara_glk_size_allocate;
510
511     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
512     container_class->forall = chimara_glk_forall;
513
514     /* Signals */
515     klass->stopped = chimara_glk_stopped;
516     klass->started = chimara_glk_started;
517     /**
518      * ChimaraGlk::stopped:
519      * @glk: The widget that received the signal
520      *
521      * The ::stopped signal is emitted when the a Glk program finishes
522      * executing in the widget, whether it ended normally, or was interrupted.
523      */ 
524     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
525         G_OBJECT_CLASS_TYPE(klass), 0, 
526         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
527                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
528         /**
529          * ChimaraGlk::started:
530          * @glk: The widget that received the signal
531          *
532          * The ::started signal is emitted when a Glk program starts executing in
533          * the widget.
534          */
535         chimara_glk_signals[STARTED] = g_signal_new ("started",
536                 G_OBJECT_CLASS_TYPE (klass), 0,
537                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
538                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
539
540     /* Properties */
541     /**
542      * ChimaraGlk:interactive:
543      *
544      * Sets whether the widget is interactive. A Glk widget is normally 
545      * interactive, but in non-interactive mode, keyboard and mouse input are 
546      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
547      * <quote>More</quote> prompts when a lot of text is printed to a text 
548          * buffer are also disabled. This is typically used when you wish to control
549          * an interpreter program by feeding it a predefined list of commands.
550      */
551     g_object_class_install_property( object_class, PROP_INTERACTIVE, 
552                 g_param_spec_boolean("interactive", _("Interactive"),
553         _("Whether user input is expected in the Glk program"),
554         TRUE,
555         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
556
557         /**
558      * ChimaraGlk:protect:
559      *
560      * Sets whether the Glk program is allowed to do file operations. In protect
561      * mode, all file operations will fail.
562      */
563     g_object_class_install_property(object_class, PROP_PROTECT, 
564                 g_param_spec_boolean("protect", _("Protected"),
565         _("Whether the Glk program is barred from doing file operations"),
566         FALSE,
567         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
568
569         /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
570          initialize them with NULL */
571         /**
572          * ChimaraGlk:default-font-description:
573          * 
574          * Pointer to a #PangoFontDescription describing the default proportional 
575          * font, to be used in text buffer windows for example.
576          *
577          * Default value: font description created from the string 
578          * <quote>Sans</quote>
579          */
580         g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, 
581                 g_param_spec_pointer("default-font-description", _("Default Font"),
582                 _("Font description of the default proportional font"),
583                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
584
585         /**
586          * ChimaraGlk:monospace-font-description:
587          *
588          * Pointer to a #PangoFontDescription describing the default monospace font,
589          * to be used in text grid windows and #style_Preformatted, for example.
590          *
591          * Default value: font description created from the string 
592          * <quote>Monospace</quote>
593          */
594         g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, 
595                 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
596                 _("Font description of the default monospace font"),
597                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
598
599         /**
600          * ChimaraGlk:spacing:
601          *
602          * The amount of space between the Glk windows.
603          */
604         g_object_class_install_property(object_class, PROP_SPACING,
605                 g_param_spec_uint("spacing", _("Spacing"),
606                 _("The amount of space between Glk windows"),
607                 0, G_MAXUINT, 0,
608                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
609         
610     /* Private data */
611     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
612 }
613
614 /* PUBLIC FUNCTIONS */
615
616 /**
617  * chimara_glk_new:
618  *
619  * Creates and initializes a new #ChimaraGlk widget.
620  *
621  * Return value: a #ChimaraGlk widget, with a floating reference.
622  */
623 GtkWidget *
624 chimara_glk_new(void)
625 {
626     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
627     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
628     
629     priv->event_queue = g_queue_new();
630     priv->event_lock = g_mutex_new();
631     priv->event_queue_not_empty = g_cond_new();
632     priv->event_queue_not_full = g_cond_new();
633     priv->abort_lock = g_mutex_new();
634     
635     return GTK_WIDGET(self);
636 }
637
638 /**
639  * chimara_glk_set_interactive:
640  * @glk: a #ChimaraGlk widget
641  * @interactive: whether the widget should expect user input
642  *
643  * Sets the #ChimaraGlk:interactive property of @glk. 
644  */
645 void 
646 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
647 {
648     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
649     
650     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
651     priv->interactive = interactive;
652 }
653
654 /**
655  * chimara_glk_get_interactive:
656  * @glk: a #ChimaraGlk widget
657  *
658  * Returns whether @glk is interactive (expecting user input). See 
659  * #ChimaraGlk:interactive.
660  *
661  * Return value: %TRUE if @glk is interactive.
662  */
663 gboolean 
664 chimara_glk_get_interactive(ChimaraGlk *glk)
665 {
666     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
667     
668     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
669     return priv->interactive;
670 }
671
672 /**
673  * chimara_glk_set_protect:
674  * @glk: a #ChimaraGlk widget
675  * @protect: whether the widget should allow the Glk program to do file 
676  * operations
677  *
678  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
679  * program is not allowed to do file operations.
680  */
681 void 
682 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
683 {
684     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
685     
686     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
687     priv->protect = protect;
688 }
689
690 /**
691  * chimara_glk_get_protect:
692  * @glk: a #ChimaraGlk widget
693  *
694  * Returns whether @glk is in protect mode (banned from doing file operations).
695  * See #ChimaraGlk:protect.
696  *
697  * Return value: %TRUE if @glk is in protect mode.
698  */
699 gboolean 
700 chimara_glk_get_protect(ChimaraGlk *glk)
701 {
702     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
703     
704     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
705     return priv->protect;
706 }
707
708 /**
709  * chimara_glk_set_default_font_description:
710  * @glk: a #ChimaraGlk widget
711  * @font: a #PangoFontDescription
712  *
713  * Sets @glk's default proportional font. See 
714  * #ChimaraGlk:default-font-description.
715  */
716 void 
717 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
718 {
719         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
720         g_return_if_fail(font);
721         
722         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
723         pango_font_description_free(priv->default_font_desc);
724         priv->default_font_desc = pango_font_description_copy(font);
725         
726         /* TODO: Apply the font description to all the windows and recalculate the sizes */
727 }
728
729 /**
730  * chimara_glk_set_default_font_string:
731  * @glk: a #ChimaraGlk widget
732  * @font: string representation of a font description
733  *
734  * Sets @glk's default proportional font according to the string @font, which
735  * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
736  * [<replaceable>STYLE-OPTIONS</replaceable>] 
737  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia 
738  * Italic 12</quote> or <quote>Sans</quote>. See 
739  * #ChimaraGlk:default-font-description.
740  */
741 void 
742 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
743 {
744         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
745         g_return_if_fail(font || *font);
746         
747         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
748         g_return_if_fail(fontdesc);
749         
750         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
751         pango_font_description_free(priv->default_font_desc);
752         priv->default_font_desc = fontdesc;
753         
754         /* TODO: Apply the font description to all the windows and recalculate the sizes */
755 }
756         
757 /**
758  * chimara_glk_get_default_font_description:
759  * @glk: a #ChimaraGlk widget
760  * 
761  * Returns @glk's default proportional font.
762  *
763  * Return value: a newly-allocated #PangoFontDescription which must be freed
764  * using pango_font_description_free(), or %NULL on error.
765  */
766 PangoFontDescription *
767 chimara_glk_get_default_font_description(ChimaraGlk *glk)
768 {
769         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
770         
771         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
772         return pango_font_description_copy(priv->default_font_desc);
773 }
774
775 /**
776  * chimara_glk_set_monospace_font_description:
777  * @glk: a #ChimaraGlk widget
778  * @font: a #PangoFontDescription
779  *
780  * Sets @glk's default monospace font. See 
781  * #ChimaraGlk:monospace-font-description.
782  */
783 void 
784 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
785 {
786         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
787         g_return_if_fail(font);
788         
789         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
790         pango_font_description_free(priv->monospace_font_desc);
791         priv->monospace_font_desc = pango_font_description_copy(font);
792         
793         /* TODO: Apply the font description to all the windows and recalculate the sizes */
794 }
795
796 /**
797  * chimara_glk_set_monospace_font_string:
798  * @glk: a #ChimaraGlk widget
799  * @font: string representation of a font description
800  *
801  * Sets @glk's default monospace font according to the string @font, which must
802  * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
803  * [<replaceable>STYLE-OPTIONS</replaceable>] 
804  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier 
805  * Bold 12</quote> or <quote>Monospace</quote>. See 
806  * #ChimaraGlk:monospace-font-description.
807  */
808 void 
809 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
810 {
811         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
812         g_return_if_fail(font || *font);
813         
814         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
815         g_return_if_fail(fontdesc);
816         
817         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
818         pango_font_description_free(priv->monospace_font_desc);
819         priv->monospace_font_desc = fontdesc;
820         
821         /* TODO: Apply the font description to all the windows and recalculate the sizes */
822 }
823         
824 /**
825  * chimara_glk_get_monospace_font_description:
826  * @glk: a #ChimaraGlk widget
827  * 
828  * Returns @glk's default monospace font.
829  *
830  * Return value: a newly-allocated #PangoFontDescription which must be freed
831  * using pango_font_description_free(), or %NULL on error.
832  */
833 PangoFontDescription *
834 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
835 {
836         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
837         
838         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
839         return pango_font_description_copy(priv->monospace_font_desc);
840 }
841
842 /**
843  * chimara_glk_set_spacing:
844  * @glk: a #ChimaraGlk widget
845  * @spacing: the number of pixels to put between Glk windows
846  *
847  * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
848  * pixels between Glk windows.
849  */
850 void 
851 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
852 {
853         g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
854         
855         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
856         priv->spacing = spacing;
857 }
858
859 /**
860  * chimara_glk_get_spacing:
861  * @glk: a #ChimaraGlk widget
862  *
863  * Gets the value set by chimara_glk_set_spacing().
864  *
865  * Return value: pixels of spacing between Glk windows
866  */
867 guint 
868 chimara_glk_get_spacing(ChimaraGlk *glk)
869 {
870         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
871         
872         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
873         return priv->spacing;
874 }
875
876 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
877 static gpointer
878 glk_enter(gpointer glk_main)
879 {
880     extern ChimaraGlkPrivate *glk_data;
881
882     g_signal_emit_by_name(glk_data->self, "started");
883         ((glk_main_t)glk_main)();
884         g_signal_emit_by_name(glk_data->self, "stopped");
885         return NULL;
886 }
887
888 /**
889  * chimara_glk_run:
890  * @glk: a #ChimaraGlk widget
891  * @plugin: path to a plugin module compiled with <filename 
892  * class="header">glk.h</filename>
893  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
894  * %NULL
895  *
896  * Opens a Glk program compiled as a plugin and runs its glk_main() function in
897  * a separate thread. On failure, returns %FALSE and sets @error.
898  *
899  * Return value: %TRUE if the Glk program was started successfully.
900  */
901 gboolean
902 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
903 {
904     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
905     g_return_val_if_fail(plugin, FALSE);
906     
907     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
908     
909
910     /* Open the module to run */
911     glk_main_t glk_main;
912         glkunix_startup_code_t glkunix_startup_code;
913     g_assert( g_module_supported() );
914     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
915     
916     if(!priv->program)
917     {
918         g_warning( "Error opening module: %s", g_module_error() );
919         return FALSE;
920     }
921     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
922     {
923         g_warning( "Error finding glk_main(): %s", g_module_error() );
924         return FALSE;
925     }
926
927     extern ChimaraGlkPrivate *glk_data;
928     /* Set the thread's private data */
929     /* TODO: Do this with a GPrivate */
930     glk_data = priv;
931
932     if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &glkunix_startup_code) )
933     {
934                 glkunix_startup_t data;
935                 data.argc = argc;
936                 data.argv = argv;
937
938                 glkunix_startup_code(&data);
939     }
940
941     /* Run in a separate thread */
942         priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
943         
944         return !(priv->thread == NULL);
945 }
946
947 /**
948  * chimara_glk_stop:
949  * @glk: a #ChimaraGlk widget
950  *
951  * Signals the Glk program running in @glk to abort. Note that if the program is
952  * caught in an infinite loop in which glk_tick() is not called, this may not
953  * work.
954  */
955 void
956 chimara_glk_stop(ChimaraGlk *glk)
957 {
958     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
959     /* TODO: check if glk is actually running a program */
960     signal_abort();
961 }
962
963 /**
964  * chimara_glk_wait:
965  * @glk: a #ChimaraGlk widget
966  *
967  * Holds up the main thread and waits for the Glk program running in @glk to 
968  * finish.
969  */
970 void
971 chimara_glk_wait(ChimaraGlk *glk)
972 {
973     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
974     
975     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
976     g_thread_join(priv->thread);
977 }