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