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