Added properties to the ChimaraGlk widget: "default-font-description" and
[rodin/chimara.git] / src / chimara-glk.c
1 /* licensing and copyright information here */
2
3 #include <gtk/gtk.h>
4 #include <glib/gi18n.h>
5 #include <gmodule.h>
6 #include <pango/pango.h>
7 #include "chimara-glk.h"
8 #include "chimara-glk-private.h"
9 #include "glk.h"
10 #include "abort.h"
11 #include "window.h"
12
13 #define CHIMARA_GLK_MIN_WIDTH 0
14 #define CHIMARA_GLK_MIN_HEIGHT 0
15
16 /**
17  * SECTION:chimara-glk
18  * @short_description: Widget which executes a Glk program
19  * @stability: Unstable
20  * @include: chimara/chimara-glk.h
21  * 
22  * The ChimaraGlk widget opens and runs a Glk program. The program must be
23  * compiled as a plugin module, with a function <function>glk_main()</function>
24  * that the Glk library can hook into.
25  *
26  * On Linux systems, this is a file with a name like 
27  * <filename>plugin.so</filename>. For portability, you can use libtool and 
28  * automake:
29  * <informalexample><programlisting>
30  * pkglib_LTLIBRARIES = plugin.la
31  * plugin_la_SOURCES = plugin.c foo.c bar.c
32  * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
33  * </programlisting></informalexample>
34  * This will produce <filename>plugin.la</filename> which is a text file 
35  * containing the correct plugin file to open (see the relevant section of the
36  * <ulink 
37  * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
38  * Libtool manual</ulink>).
39  */
40
41 typedef void (* glk_main_t) (void);
42
43 enum {
44     PROP_0,
45     PROP_INTERACTIVE,
46     PROP_PROTECT,
47         PROP_DEFAULT_FONT_DESCRIPTION,
48         PROP_MONOSPACE_FONT_DESCRIPTION
49 };
50
51 enum {
52         STOPPED,
53         STARTED,
54
55         LAST_SIGNAL
56 };
57
58 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
59
60 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
61
62 static void
63 chimara_glk_init(ChimaraGlk *self)
64 {
65     GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
66
67     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
68     
69     priv->self = self;
70     priv->interactive = TRUE;
71     priv->protect = FALSE;
72         priv->default_font_desc = pango_font_description_from_string("Sans");
73         priv->monospace_font_desc = pango_font_description_from_string("Monospace");
74     priv->program = NULL;
75     priv->thread = NULL;
76     priv->event_queue = NULL;
77     priv->event_lock = NULL;
78     priv->event_queue_not_empty = NULL;
79     priv->event_queue_not_full = NULL;
80     priv->abort_lock = NULL;
81     priv->abort_signalled = FALSE;
82     priv->interrupt_handler = NULL;
83     priv->root_window = NULL;
84     priv->fileref_list = NULL;
85     priv->current_stream = NULL;
86     priv->stream_list = NULL;
87 }
88
89 static void
90 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
91 {
92     ChimaraGlk *glk = CHIMARA_GLK(object);
93     
94     switch(prop_id) 
95     {
96         case PROP_INTERACTIVE:
97             chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
98             break;
99         case PROP_PROTECT:
100             chimara_glk_set_protect( glk, g_value_get_boolean(value) );
101             break;
102                 case PROP_DEFAULT_FONT_DESCRIPTION:
103                         chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
104                         break;
105                 case PROP_MONOSPACE_FONT_DESCRIPTION:
106                         chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
107                         break;
108         default:
109             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
110     }
111 }
112
113 static void
114 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
115 {
116     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
117     
118     switch(prop_id)
119     {
120         case PROP_INTERACTIVE:
121             g_value_set_boolean(value, priv->interactive);
122             break;
123         case PROP_PROTECT:
124             g_value_set_boolean(value, priv->protect);
125             break;
126                 case PROP_DEFAULT_FONT_DESCRIPTION:
127                         g_value_set_pointer(value, priv->default_font_desc);
128                         break;
129                 case PROP_MONOSPACE_FONT_DESCRIPTION:
130                         g_value_set_pointer(value, priv->monospace_font_desc);
131                         break;
132         default:
133             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
134     }
135 }
136
137 static void
138 chimara_glk_finalize(GObject *object)
139 {
140     ChimaraGlk *self = CHIMARA_GLK(object);
141     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
142     
143     /* Free the event queue */
144     g_mutex_lock(priv->event_lock);
145         g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
146         g_queue_free(priv->event_queue);
147         g_cond_free(priv->event_queue_not_empty);
148         g_cond_free(priv->event_queue_not_full);
149         priv->event_queue = NULL;
150         g_mutex_unlock(priv->event_lock);
151         g_mutex_free(priv->event_lock);
152         
153         /* Free the abort signalling mechanism */
154         g_mutex_lock(priv->abort_lock);
155         /* Make sure no other thread is busy with this */
156         g_mutex_unlock(priv->abort_lock);
157         g_mutex_free(priv->abort_lock);
158         priv->abort_lock = NULL;
159
160         /* Free private data */
161         pango_font_description_free(priv->default_font_desc);
162         pango_font_description_free(priv->monospace_font_desc);
163         
164     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
165 }
166
167 static void
168 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
169 {
170     g_return_if_fail(widget);
171     g_return_if_fail(requisition);
172     g_return_if_fail(CHIMARA_IS_GLK(widget));
173     
174     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
175     
176     /* For now, just pass the size request on to the root Glk window */
177     if(priv->root_window) { 
178         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
179        if(GTK_WIDGET_VISIBLE(child))
180             gtk_widget_size_request(child, requisition);
181     } else {
182         requisition->width = CHIMARA_GLK_MIN_WIDTH;
183         requisition->height = CHIMARA_GLK_MIN_HEIGHT;
184     }
185 }
186
187 static void
188 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
189 {
190     g_return_if_fail(widget);
191     g_return_if_fail(allocation);
192     g_return_if_fail(CHIMARA_IS_GLK(widget));
193     
194     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
195     
196     widget->allocation = *allocation;
197             
198     if(priv->root_window) {
199         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
200         if(GTK_WIDGET_VISIBLE(child))
201             gtk_widget_size_allocate(child, allocation);
202     }
203 }
204
205 static void
206 chimara_glk_forall(GtkContainer *container, gboolean include_internals,
207     GtkCallback callback, gpointer callback_data)
208 {
209     g_return_if_fail(container);
210     g_return_if_fail(CHIMARA_IS_GLK(container));
211     
212     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
213     
214     if(priv->root_window) {
215         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
216         (*callback)(child, callback_data);
217     }
218 }
219
220 static void
221 chimara_glk_stopped(ChimaraGlk *self)
222 {
223     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
224
225     /* Free the plugin */
226         if( priv->program && !g_module_close(priv->program) )
227             g_warning( "Error closing module: %s", g_module_error() );
228 }
229
230 static void
231 chimara_glk_started(ChimaraGlk *self)
232 {
233         /* TODO: Add default signal handler implementation here */
234 }
235
236 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
237 #ifndef G_PARAM_STATIC_STRINGS
238 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
239 #endif
240
241 static void
242 chimara_glk_class_init(ChimaraGlkClass *klass)
243 {
244     /* Override methods of parent classes */
245     GObjectClass *object_class = G_OBJECT_CLASS(klass);
246     object_class->set_property = chimara_glk_set_property;
247     object_class->get_property = chimara_glk_get_property;
248     object_class->finalize = chimara_glk_finalize;
249     
250     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
251     widget_class->size_request = chimara_glk_size_request;
252     widget_class->size_allocate = chimara_glk_size_allocate;
253
254     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
255     container_class->forall = chimara_glk_forall;
256
257     /* Signals */
258     klass->stopped = chimara_glk_stopped;
259     klass->started = chimara_glk_started;
260     /**
261      * ChimaraGlk::stopped:
262      * @glk: The widget that received the signal
263      *
264      * The ::stopped signal is emitted when the a Glk program finishes
265      * executing in the widget, whether it ended normally, or was interrupted.
266      */ 
267     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
268         G_OBJECT_CLASS_TYPE(klass), 0, 
269         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
270                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
271         /**
272          * ChimaraGlk::started:
273          * @glk: The widget that received the signal
274          *
275          * The ::started signal is emitted when a Glk program starts executing in
276          * the widget.
277          */
278         chimara_glk_signals[STARTED] = g_signal_new ("started",
279                 G_OBJECT_CLASS_TYPE (klass), 0,
280                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
281                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
282
283     /* Properties */
284     GParamSpec *pspec;
285     pspec = g_param_spec_boolean("interactive", _("Interactive"),
286         _("Whether user input is expected in the Glk program"),
287         TRUE,
288         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
289     /**
290      * ChimaraGlk:interactive:
291      *
292      * Sets whether the widget is interactive. A Glk widget is normally 
293      * interactive, but in non-interactive mode, keyboard and mouse input are 
294      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
295      * <quote>More</quote> prompts when a lot of text is printed to a text 
296          * buffer are also disabled. This is typically used when you wish to control
297          * an interpreter program by feeding it a predefined list of commands.
298      */
299     g_object_class_install_property(object_class, PROP_INTERACTIVE, pspec);
300     pspec = g_param_spec_boolean("protect", _("Protected"),
301         _("Whether the Glk program is barred from doing file operations"),
302         FALSE,
303         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
304     /**
305      * ChimaraGlk:protect:
306      *
307      * Sets whether the Glk program is allowed to do file operations. In protect
308      * mode, all file operations will fail.
309      */
310     g_object_class_install_property(object_class, PROP_PROTECT, pspec);
311     pspec = g_param_spec_pointer("default-font-description", _("Default Font"),
312                 _("Font description of the default proportional font"),
313                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
314         /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
315          initialize them with NULL */
316         /**
317          * ChimaraGlk:default-font-description:
318          * 
319          * Pointer to a #PangoFontDescription describing the default proportional 
320          * font, to be used in text buffer windows for example.
321          *
322          * Default value: font description created from the string 
323          * <quote>Sans</quote>
324          */
325         g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, pspec);
326         pspec = g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
327                 _("Font description of the default monospace font"),
328                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
329         /**
330          * ChimaraGlk:monospace-font-description:
331          *
332          * Pointer to a #PangoFontDescription describing the default monospace font,
333          * to be used in text grid windows and #style_Preformatted, for example.
334          *
335          * Default value: font description created from the string 
336          * <quote>Monospace</quote>
337          */
338         g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, pspec);
339     /* Private data */
340     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
341 }
342
343 /* PUBLIC FUNCTIONS */
344
345 /**
346  * chimara_glk_new:
347  *
348  * Creates and initializes a new #ChimaraGlk widget.
349  *
350  * Return value: a #ChimaraGlk widget, with a floating reference.
351  */
352 GtkWidget *
353 chimara_glk_new(void)
354 {
355     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
356     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
357     
358     priv->event_queue = g_queue_new();
359     priv->event_lock = g_mutex_new();
360     priv->event_queue_not_empty = g_cond_new();
361     priv->event_queue_not_full = g_cond_new();
362     priv->abort_lock = g_mutex_new();
363     
364     return GTK_WIDGET(self);
365 }
366
367 /**
368  * chimara_glk_set_interactive:
369  * @glk: a #ChimaraGlk widget
370  * @interactive: whether the widget should expect user input
371  *
372  * Sets the #ChimaraGlk:interactive property of @glk. 
373  */
374 void 
375 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
376 {
377     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
378     
379     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
380     priv->interactive = interactive;
381 }
382
383 /**
384  * chimara_glk_get_interactive:
385  * @glk: a #ChimaraGlk widget
386  *
387  * Returns whether @glk is interactive (expecting user input). See 
388  * #ChimaraGlk:interactive.
389  *
390  * Return value: %TRUE if @glk is interactive.
391  */
392 gboolean 
393 chimara_glk_get_interactive(ChimaraGlk *glk)
394 {
395     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
396     
397     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
398     return priv->interactive;
399 }
400
401 /**
402  * chimara_glk_set_protect:
403  * @glk: a #ChimaraGlk widget
404  * @protect: whether the widget should allow the Glk program to do file 
405  * operations
406  *
407  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
408  * program is not allowed to do file operations.
409  */
410 void 
411 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
412 {
413     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
414     
415     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
416     priv->protect = protect;
417 }
418
419 /**
420  * chimara_glk_get_protect:
421  * @glk: a #ChimaraGlk widget
422  *
423  * Returns whether @glk is in protect mode (banned from doing file operations).
424  * See #ChimaraGlk:protect.
425  *
426  * Return value: %TRUE if @glk is in protect mode.
427  */
428 gboolean 
429 chimara_glk_get_protect(ChimaraGlk *glk)
430 {
431     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
432     
433     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
434     return priv->protect;
435 }
436
437 /**
438  * chimara_glk_set_default_font_description:
439  * @glk: a #ChimaraGlk widget
440  * @font: a #PangoFontDescription
441  *
442  * Sets @glk's default proportional font. See 
443  * #ChimaraGlk:default-font-description.
444  */
445 void 
446 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
447 {
448         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
449         g_return_if_fail(font);
450         
451         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
452         pango_font_description_free(priv->default_font_desc);
453         priv->default_font_desc = pango_font_description_copy(font);
454         
455         /* TODO: Apply the font description to all the windows and recalculate the sizes */
456 }
457
458 /**
459  * chimara_glk_set_default_font_string:
460  * @glk: a #ChimaraGlk widget
461  * @font: string representation of a font description
462  *
463  * Sets @glk's default proportional font according to the string @font, which
464  * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
465  * [<replaceable>STYLE-OPTIONS</replaceable>] 
466  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia 
467  * Italic 12</quote> or <quote>Sans</quote>. See 
468  * #ChimaraGlk:default-font-description.
469  */
470 void 
471 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
472 {
473         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
474         g_return_if_fail(font || *font);
475         
476         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
477         g_return_if_fail(fontdesc);
478         
479         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
480         pango_font_description_free(priv->default_font_desc);
481         priv->default_font_desc = fontdesc;
482         
483         /* TODO: Apply the font description to all the windows and recalculate the sizes */
484 }
485         
486 /**
487  * chimara_glk_get_default_font_description:
488  * 
489  * Returns @glk's default proportional font.
490  *
491  * Return value: a newly-allocated #PangoFontDescription which must be freed
492  * using pango_font_description_free(), or %NULL on error.
493  */
494 PangoFontDescription *
495 chimara_glk_get_default_font_description(ChimaraGlk *glk)
496 {
497         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
498         
499         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
500         return pango_font_description_copy(priv->default_font_desc);
501 }
502
503 /**
504  * chimara_glk_set_monospace_font_description:
505  * @glk: a #ChimaraGlk widget
506  * @font: a #PangoFontDescription
507  *
508  * Sets @glk's default monospace font. See 
509  * #ChimaraGlk:monospace-font-description.
510  */
511 void 
512 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
513 {
514         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
515         g_return_if_fail(font);
516         
517         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
518         pango_font_description_free(priv->monospace_font_desc);
519         priv->monospace_font_desc = pango_font_description_copy(font);
520         
521         /* TODO: Apply the font description to all the windows and recalculate the sizes */
522 }
523
524 /**
525  * chimara_glk_set_monospace_font_string:
526  * @glk: a #ChimaraGlk widget
527  * @font: string representation of a font description
528  *
529  * Sets @glk's default monospace font according to the string @font, which must
530  * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
531  * [<replaceable>STYLE-OPTIONS</replaceable>] 
532  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier 
533  * Bold 12</quote> or <quote>Monospace</quote>. See 
534  * #ChimaraGlk:monospace-font-description.
535  */
536 void 
537 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
538 {
539         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
540         g_return_if_fail(font || *font);
541         
542         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
543         g_return_if_fail(fontdesc);
544         
545         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
546         pango_font_description_free(priv->monospace_font_desc);
547         priv->monospace_font_desc = fontdesc;
548         
549         /* TODO: Apply the font description to all the windows and recalculate the sizes */
550 }
551         
552 /**
553  * chimara_glk_get_monospace_font_description:
554  * 
555  * Returns @glk's default monospace font.
556  *
557  * Return value: a newly-allocated #PangoFontDescription which must be freed
558  * using pango_font_description_free(), or %NULL on error.
559  */
560 PangoFontDescription *
561 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
562 {
563         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
564         
565         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
566         return pango_font_description_copy(priv->monospace_font_desc);
567 }
568
569 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
570 static gpointer
571 glk_enter(gpointer glk_main)
572 {
573     extern ChimaraGlkPrivate *glk_data;
574     g_signal_emit_by_name(glk_data->self, "started");
575         ((glk_main_t)glk_main)();
576         g_signal_emit_by_name(glk_data->self, "stopped");
577         return NULL;
578 }
579
580 /**
581  * chimara_glk_run:
582  * @glk: a #ChimaraGlk widget
583  * @plugin: path to a plugin module compiled with <filename 
584  * class="header">glk.h</filename>
585  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
586  * %NULL
587  *
588  * Opens a Glk program compiled as a plugin and runs its glk_main() function in
589  * a separate thread. On failure, returns %FALSE and sets @error.
590  *
591  * Return value: %TRUE if the Glk program was started successfully.
592  */
593 gboolean
594 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
595 {
596     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
597     g_return_val_if_fail(plugin, FALSE);
598     
599     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
600     
601     /* Open the module to run */
602     glk_main_t glk_main;
603     g_assert( g_module_supported() );
604     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
605     
606     if(!priv->program)
607     {
608         g_warning( "Error opening module: %s", g_module_error() );
609         return FALSE;
610     }
611     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
612     {
613         g_warning( "Error finding glk_main(): %s", g_module_error() );
614         return FALSE;
615     }
616
617     extern ChimaraGlkPrivate *glk_data;
618     /* Set the thread's private data */
619     /* TODO: Do this with a GPrivate */
620     glk_data = priv;
621     
622     /* Run in a separate thread */
623         priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
624         
625         return !(priv->thread == NULL);
626 }
627
628 /**
629  * chimara_glk_stop:
630  * @glk: a #ChimaraGlk widget
631  *
632  * Signals the Glk program running in @glk to abort. Note that if the program is
633  * caught in an infinite loop in which glk_tick() is not called, this may not
634  * work.
635  */
636 void
637 chimara_glk_stop(ChimaraGlk *glk)
638 {
639     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
640     /* TODO: check if glk is actually running a program */
641     signal_abort();
642 }
643
644 /**
645  * chimara_glk_wait:
646  * @glk: a #ChimaraGlk widget
647  *
648  * Holds up the main thread and waits for the Glk program running in @glk to 
649  * finish.
650  */
651 void
652 chimara_glk_wait(ChimaraGlk *glk)
653 {
654     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
655     
656     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
657     g_thread_join(priv->thread);
658 }