Added a "spacing" property which sets the border width between Glk windows.
[projects/chimara/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 = win->constraint_size * win->key_window->unit_width;
267                                         if(child1.width > allocation->width - spacing)
268                                                 child1.width = allocation->width - spacing;
269                                         break;
270                                 case winmethod_Right:
271                                         child2.width = win->constraint_size * win->key_window->unit_width;
272                                         if(child2.width > allocation->width - spacing)
273                                                 child2.width = allocation->width - spacing;
274                                         break;
275                                 case winmethod_Above:
276                                         child1.height = win->constraint_size * win->key_window->unit_height;
277                                         if(child1.height > allocation->height - spacing)
278                                                 child1.height = allocation->height - spacing;
279                                         break;
280                                 case winmethod_Below:
281                                         child2.height = win->constraint_size * win->key_window->unit_height;
282                                         if(child2.height > allocation->height - spacing)
283                                                 child2.height = allocation->height - spacing;
284                                         break;
285                         }
286                 }
287                 else /* proportional */
288                 {
289                         gdouble fraction = win->constraint_size / 100.0;
290                         switch(win->split_method & winmethod_DirMask)
291                         {
292                                 case winmethod_Left:
293                                         child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
294                                         break;
295                                 case winmethod_Right:
296                                         child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
297                                         break;
298                                 case winmethod_Above:
299                                         child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
300                                         break;
301                                 case winmethod_Below:
302                                         child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
303                                         break;
304                         }
305                 }
306                 
307                 /* Fill in the rest of the size requisitions according to the child specified above */
308                 switch(win->split_method & winmethod_DirMask)
309                 {
310                         case winmethod_Left:
311                                 child2.width = allocation->width - spacing - child1.width;
312                                 child2.x = child1.x + child1.width + spacing;
313                                 child2.y = child1.y;
314                                 child1.height = child2.height = allocation->height;
315                                 break;
316                         case winmethod_Right:
317                                 child1.width = allocation->width - spacing - child2.width;
318                                 child2.x = child1.x + child1.width + spacing;
319                                 child2.y = child1.y;
320                                 child1.height = child2.height = allocation->height;
321                                 break;
322                         case winmethod_Above:
323                                 child2.height = allocation->height - spacing - child1.height;
324                                 child2.x = child1.x;
325                                 child2.y = child1.y + child1.height + spacing;
326                                 child1.width = child2.width = allocation->width;
327                                 break;
328                         case winmethod_Below:
329                                 child1.height = allocation->height - spacing - child2.height;
330                                 child2.x = child1.x;
331                                 child2.y = child1.y + child1.height + spacing;
332                                 child1.width = child2.width = allocation->width;
333                                 break;
334                 }
335                 
336                 /* Recurse */
337                 allocate_recurse(win->window_node->children->data, &child1, spacing);
338                 allocate_recurse(win->window_node->children->next->data, &child2, spacing);
339         }
340         
341         /* For non-pair windows, just give them the size */
342         else
343                 gtk_widget_size_allocate(win->frame, allocation);
344 }
345
346 /* Overrides gtk_widget_size_allocate */
347 static void
348 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
349 {
350     g_return_if_fail(widget);
351     g_return_if_fail(allocation);
352     g_return_if_fail(CHIMARA_IS_GLK(widget));
353     
354     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
355     
356     widget->allocation = *allocation;
357             
358     if(priv->root_window) {
359                 GtkAllocation child;
360                 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
361                 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
362                 child.width = allocation->width - 2 * GTK_CONTAINER(widget)->border_width;
363                 child.height = allocation->height - 2 * GTK_CONTAINER(widget)->border_width;
364                 if(child.width < 0)
365                         child.width = 0;
366                 if(child.height < 0)
367                         child.height = 0;
368                 allocate_recurse(priv->root_window->data, &child, priv->spacing);
369         }
370 }
371
372 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
373 static void
374 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
375 {
376         if(win->type == wintype_Pair)
377         {
378                 forall_recurse(win->window_node->children->data, callback, callback_data);
379                 forall_recurse(win->window_node->children->next->data, callback, callback_data);
380         }
381         else
382                 (*callback)(win->frame, callback_data);
383 }
384
385 /* Overrides gtk_container_forall */
386 static void
387 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
388 {
389     g_return_if_fail(container);
390     g_return_if_fail(CHIMARA_IS_GLK(container));
391     
392     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
393     
394         /* All the children are "internal" */
395         if(!include_internals)
396                 return;
397         
398     if(priv->root_window)
399                 forall_recurse(priv->root_window->data, callback, callback_data);
400 }
401
402 static void
403 chimara_glk_stopped(ChimaraGlk *self)
404 {
405     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
406
407     /* Free the plugin */
408         if( priv->program && !g_module_close(priv->program) )
409             g_warning( "Error closing module: %s", g_module_error() );
410 }
411
412 static void
413 chimara_glk_started(ChimaraGlk *self)
414 {
415         /* TODO: Add default signal handler implementation here */
416 }
417
418 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
419 #ifndef G_PARAM_STATIC_STRINGS
420 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
421 #endif
422
423 static void
424 chimara_glk_class_init(ChimaraGlkClass *klass)
425 {
426     /* Override methods of parent classes */
427     GObjectClass *object_class = G_OBJECT_CLASS(klass);
428     object_class->set_property = chimara_glk_set_property;
429     object_class->get_property = chimara_glk_get_property;
430     object_class->finalize = chimara_glk_finalize;
431     
432     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
433     widget_class->size_request = chimara_glk_size_request;
434     widget_class->size_allocate = chimara_glk_size_allocate;
435
436     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
437     container_class->forall = chimara_glk_forall;
438
439     /* Signals */
440     klass->stopped = chimara_glk_stopped;
441     klass->started = chimara_glk_started;
442     /**
443      * ChimaraGlk::stopped:
444      * @glk: The widget that received the signal
445      *
446      * The ::stopped signal is emitted when the a Glk program finishes
447      * executing in the widget, whether it ended normally, or was interrupted.
448      */ 
449     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
450         G_OBJECT_CLASS_TYPE(klass), 0, 
451         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
452                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
453         /**
454          * ChimaraGlk::started:
455          * @glk: The widget that received the signal
456          *
457          * The ::started signal is emitted when a Glk program starts executing in
458          * the widget.
459          */
460         chimara_glk_signals[STARTED] = g_signal_new ("started",
461                 G_OBJECT_CLASS_TYPE (klass), 0,
462                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
463                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
464
465     /* Properties */
466     /**
467      * ChimaraGlk:interactive:
468      *
469      * Sets whether the widget is interactive. A Glk widget is normally 
470      * interactive, but in non-interactive mode, keyboard and mouse input are 
471      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
472      * <quote>More</quote> prompts when a lot of text is printed to a text 
473          * buffer are also disabled. This is typically used when you wish to control
474          * an interpreter program by feeding it a predefined list of commands.
475      */
476     g_object_class_install_property( object_class, PROP_INTERACTIVE, 
477                 g_param_spec_boolean("interactive", _("Interactive"),
478         _("Whether user input is expected in the Glk program"),
479         TRUE,
480         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
481
482         /**
483      * ChimaraGlk:protect:
484      *
485      * Sets whether the Glk program is allowed to do file operations. In protect
486      * mode, all file operations will fail.
487      */
488     g_object_class_install_property(object_class, PROP_PROTECT, 
489                 g_param_spec_boolean("protect", _("Protected"),
490         _("Whether the Glk program is barred from doing file operations"),
491         FALSE,
492         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
493
494         /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
495          initialize them with NULL */
496         /**
497          * ChimaraGlk:default-font-description:
498          * 
499          * Pointer to a #PangoFontDescription describing the default proportional 
500          * font, to be used in text buffer windows for example.
501          *
502          * Default value: font description created from the string 
503          * <quote>Sans</quote>
504          */
505         g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, 
506                 g_param_spec_pointer("default-font-description", _("Default Font"),
507                 _("Font description of the default proportional font"),
508                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
509
510         /**
511          * ChimaraGlk:monospace-font-description:
512          *
513          * Pointer to a #PangoFontDescription describing the default monospace font,
514          * to be used in text grid windows and #style_Preformatted, for example.
515          *
516          * Default value: font description created from the string 
517          * <quote>Monospace</quote>
518          */
519         g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, 
520                 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
521                 _("Font description of the default monospace font"),
522                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
523
524         /**
525          * ChimaraGlk:spacing:
526          *
527          * The amount of space between the Glk windows.
528          */
529         g_object_class_install_property(object_class, PROP_SPACING,
530                 g_param_spec_uint("spacing", _("Spacing"),
531                 _("The amount of space between Glk windows"),
532                 0, G_MAXUINT, 0,
533                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
534         
535     /* Private data */
536     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
537 }
538
539 /* PUBLIC FUNCTIONS */
540
541 /**
542  * chimara_glk_new:
543  *
544  * Creates and initializes a new #ChimaraGlk widget.
545  *
546  * Return value: a #ChimaraGlk widget, with a floating reference.
547  */
548 GtkWidget *
549 chimara_glk_new(void)
550 {
551     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
552     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
553     
554     priv->event_queue = g_queue_new();
555     priv->event_lock = g_mutex_new();
556     priv->event_queue_not_empty = g_cond_new();
557     priv->event_queue_not_full = g_cond_new();
558     priv->abort_lock = g_mutex_new();
559     
560     return GTK_WIDGET(self);
561 }
562
563 /**
564  * chimara_glk_set_interactive:
565  * @glk: a #ChimaraGlk widget
566  * @interactive: whether the widget should expect user input
567  *
568  * Sets the #ChimaraGlk:interactive property of @glk. 
569  */
570 void 
571 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
572 {
573     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
574     
575     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
576     priv->interactive = interactive;
577 }
578
579 /**
580  * chimara_glk_get_interactive:
581  * @glk: a #ChimaraGlk widget
582  *
583  * Returns whether @glk is interactive (expecting user input). See 
584  * #ChimaraGlk:interactive.
585  *
586  * Return value: %TRUE if @glk is interactive.
587  */
588 gboolean 
589 chimara_glk_get_interactive(ChimaraGlk *glk)
590 {
591     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
592     
593     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
594     return priv->interactive;
595 }
596
597 /**
598  * chimara_glk_set_protect:
599  * @glk: a #ChimaraGlk widget
600  * @protect: whether the widget should allow the Glk program to do file 
601  * operations
602  *
603  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
604  * program is not allowed to do file operations.
605  */
606 void 
607 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
608 {
609     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
610     
611     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
612     priv->protect = protect;
613 }
614
615 /**
616  * chimara_glk_get_protect:
617  * @glk: a #ChimaraGlk widget
618  *
619  * Returns whether @glk is in protect mode (banned from doing file operations).
620  * See #ChimaraGlk:protect.
621  *
622  * Return value: %TRUE if @glk is in protect mode.
623  */
624 gboolean 
625 chimara_glk_get_protect(ChimaraGlk *glk)
626 {
627     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
628     
629     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
630     return priv->protect;
631 }
632
633 /**
634  * chimara_glk_set_default_font_description:
635  * @glk: a #ChimaraGlk widget
636  * @font: a #PangoFontDescription
637  *
638  * Sets @glk's default proportional font. See 
639  * #ChimaraGlk:default-font-description.
640  */
641 void 
642 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
643 {
644         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
645         g_return_if_fail(font);
646         
647         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
648         pango_font_description_free(priv->default_font_desc);
649         priv->default_font_desc = pango_font_description_copy(font);
650         
651         /* TODO: Apply the font description to all the windows and recalculate the sizes */
652 }
653
654 /**
655  * chimara_glk_set_default_font_string:
656  * @glk: a #ChimaraGlk widget
657  * @font: string representation of a font description
658  *
659  * Sets @glk's default proportional font according to the string @font, which
660  * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
661  * [<replaceable>STYLE-OPTIONS</replaceable>] 
662  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia 
663  * Italic 12</quote> or <quote>Sans</quote>. See 
664  * #ChimaraGlk:default-font-description.
665  */
666 void 
667 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
668 {
669         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
670         g_return_if_fail(font || *font);
671         
672         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
673         g_return_if_fail(fontdesc);
674         
675         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
676         pango_font_description_free(priv->default_font_desc);
677         priv->default_font_desc = fontdesc;
678         
679         /* TODO: Apply the font description to all the windows and recalculate the sizes */
680 }
681         
682 /**
683  * chimara_glk_get_default_font_description:
684  * @glk: a #ChimaraGlk widget
685  * 
686  * Returns @glk's default proportional font.
687  *
688  * Return value: a newly-allocated #PangoFontDescription which must be freed
689  * using pango_font_description_free(), or %NULL on error.
690  */
691 PangoFontDescription *
692 chimara_glk_get_default_font_description(ChimaraGlk *glk)
693 {
694         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
695         
696         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
697         return pango_font_description_copy(priv->default_font_desc);
698 }
699
700 /**
701  * chimara_glk_set_monospace_font_description:
702  * @glk: a #ChimaraGlk widget
703  * @font: a #PangoFontDescription
704  *
705  * Sets @glk's default monospace font. See 
706  * #ChimaraGlk:monospace-font-description.
707  */
708 void 
709 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
710 {
711         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
712         g_return_if_fail(font);
713         
714         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
715         pango_font_description_free(priv->monospace_font_desc);
716         priv->monospace_font_desc = pango_font_description_copy(font);
717         
718         /* TODO: Apply the font description to all the windows and recalculate the sizes */
719 }
720
721 /**
722  * chimara_glk_set_monospace_font_string:
723  * @glk: a #ChimaraGlk widget
724  * @font: string representation of a font description
725  *
726  * Sets @glk's default monospace font according to the string @font, which must
727  * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
728  * [<replaceable>STYLE-OPTIONS</replaceable>] 
729  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier 
730  * Bold 12</quote> or <quote>Monospace</quote>. See 
731  * #ChimaraGlk:monospace-font-description.
732  */
733 void 
734 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
735 {
736         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
737         g_return_if_fail(font || *font);
738         
739         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
740         g_return_if_fail(fontdesc);
741         
742         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
743         pango_font_description_free(priv->monospace_font_desc);
744         priv->monospace_font_desc = fontdesc;
745         
746         /* TODO: Apply the font description to all the windows and recalculate the sizes */
747 }
748         
749 /**
750  * chimara_glk_get_monospace_font_description:
751  * @glk: a #ChimaraGlk widget
752  * 
753  * Returns @glk's default monospace font.
754  *
755  * Return value: a newly-allocated #PangoFontDescription which must be freed
756  * using pango_font_description_free(), or %NULL on error.
757  */
758 PangoFontDescription *
759 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
760 {
761         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
762         
763         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
764         return pango_font_description_copy(priv->monospace_font_desc);
765 }
766
767 /**
768  * chimara_glk_set_spacing:
769  * @glk: a #ChimaraGlk widget
770  * @spacing: the number of pixels to put between Glk windows
771  *
772  * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
773  * pixels between Glk windows.
774  */
775 void 
776 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
777 {
778         g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
779         
780         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
781         priv->spacing = spacing;
782 }
783
784 /**
785  * chimara_glk_get_spacing:
786  * @glk: a #ChimaraGlk widget
787  *
788  * Gets the value set by chimara_glk_set_spacing().
789  *
790  * Return value: pixels of spacing between Glk windows
791  */
792 guint 
793 chimara_glk_get_spacing(ChimaraGlk *glk)
794 {
795         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
796         
797         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
798         return priv->spacing;
799 }
800
801 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
802 static gpointer
803 glk_enter(gpointer glk_main)
804 {
805     extern ChimaraGlkPrivate *glk_data;
806     g_signal_emit_by_name(glk_data->self, "started");
807         ((glk_main_t)glk_main)();
808         g_signal_emit_by_name(glk_data->self, "stopped");
809         return NULL;
810 }
811
812 /**
813  * chimara_glk_run:
814  * @glk: a #ChimaraGlk widget
815  * @plugin: path to a plugin module compiled with <filename 
816  * class="header">glk.h</filename>
817  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
818  * %NULL
819  *
820  * Opens a Glk program compiled as a plugin and runs its glk_main() function in
821  * a separate thread. On failure, returns %FALSE and sets @error.
822  *
823  * Return value: %TRUE if the Glk program was started successfully.
824  */
825 gboolean
826 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
827 {
828     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
829     g_return_val_if_fail(plugin, FALSE);
830     
831     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
832     
833     /* Open the module to run */
834     glk_main_t glk_main;
835     g_assert( g_module_supported() );
836     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
837     
838     if(!priv->program)
839     {
840         g_warning( "Error opening module: %s", g_module_error() );
841         return FALSE;
842     }
843     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
844     {
845         g_warning( "Error finding glk_main(): %s", g_module_error() );
846         return FALSE;
847     }
848
849     extern ChimaraGlkPrivate *glk_data;
850     /* Set the thread's private data */
851     /* TODO: Do this with a GPrivate */
852     glk_data = priv;
853     
854     /* Run in a separate thread */
855         priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
856         
857         return !(priv->thread == NULL);
858 }
859
860 /**
861  * chimara_glk_stop:
862  * @glk: a #ChimaraGlk widget
863  *
864  * Signals the Glk program running in @glk to abort. Note that if the program is
865  * caught in an infinite loop in which glk_tick() is not called, this may not
866  * work.
867  */
868 void
869 chimara_glk_stop(ChimaraGlk *glk)
870 {
871     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
872     /* TODO: check if glk is actually running a program */
873     signal_abort();
874 }
875
876 /**
877  * chimara_glk_wait:
878  * @glk: a #ChimaraGlk widget
879  *
880  * Holds up the main thread and waits for the Glk program running in @glk to 
881  * finish.
882  */
883 void
884 chimara_glk_wait(ChimaraGlk *glk)
885 {
886     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
887     
888     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
889     g_thread_join(priv->thread);
890 }