1 /* licensing and copyright information here */
5 #include <glib/gi18n.h>
7 #include <pango/pango.h>
8 #include "chimara-glk.h"
9 #include "chimara-glk-private.h"
14 #define CHIMARA_GLK_MIN_WIDTH 0
15 #define CHIMARA_GLK_MIN_HEIGHT 0
19 * @short_description: Widget which executes a Glk program
20 * @stability: Unstable
21 * @include: chimara/chimara-glk.h
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.
27 * On Linux systems, this is a file with a name like
28 * <filename>plugin.so</filename>. For portability, you can use libtool and
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
38 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
39 * Libtool manual</ulink>).
42 typedef void (* glk_main_t) (void);
48 PROP_DEFAULT_FONT_DESCRIPTION,
49 PROP_MONOSPACE_FONT_DESCRIPTION
59 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
61 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
64 chimara_glk_init(ChimaraGlk *self)
66 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
68 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(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");
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;
91 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
93 ChimaraGlk *glk = CHIMARA_GLK(object);
97 case PROP_INTERACTIVE:
98 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
101 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
103 case PROP_DEFAULT_FONT_DESCRIPTION:
104 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
106 case PROP_MONOSPACE_FONT_DESCRIPTION:
107 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
110 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
115 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
117 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
121 case PROP_INTERACTIVE:
122 g_value_set_boolean(value, priv->interactive);
125 g_value_set_boolean(value, priv->protect);
127 case PROP_DEFAULT_FONT_DESCRIPTION:
128 g_value_set_pointer(value, priv->default_font_desc);
130 case PROP_MONOSPACE_FONT_DESCRIPTION:
131 g_value_set_pointer(value, priv->monospace_font_desc);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
139 chimara_glk_finalize(GObject *object)
141 ChimaraGlk *self = CHIMARA_GLK(object);
142 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
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);
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;
161 /* Free private data */
162 pango_font_description_free(priv->default_font_desc);
163 pango_font_description_free(priv->monospace_font_desc);
165 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
168 /* Internal function: Recursively get the Glk window tree's size request */
170 request_recurse(winid_t win, GtkRequisition *requisition)
172 if(win->type == wintype_Pair)
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);
179 /* If the split is fixed, get the size of the fixed child */
180 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
182 switch(win->split_method & winmethod_DirMask)
185 child1.width = win->constraint_size * win->key_window->unit_width;
187 case winmethod_Right:
188 child2.width = win->constraint_size * win->key_window->unit_width;
190 case winmethod_Above:
191 child1.height = win->constraint_size * win->key_window->unit_height;
193 case winmethod_Below:
194 child2.height = win->constraint_size * win->key_window->unit_height;
199 /* Add the children's requests */
200 switch(win->split_method & winmethod_DirMask)
203 case winmethod_Right:
204 requisition->width = child1.width + child2.width;
205 requisition->height = MAX(child1.height, child2.height);
207 case winmethod_Above:
208 case winmethod_Below:
209 requisition->width = MAX(child1.width, child2.width);
210 requisition->height = child1.height + child2.height;
215 /* For non-pair windows, just use the size that GTK requests */
217 gtk_widget_size_request(win->frame, requisition);
220 /* Overrides gtk_widget_size_request */
222 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
224 g_return_if_fail(widget);
225 g_return_if_fail(requisition);
226 g_return_if_fail(CHIMARA_IS_GLK(widget));
228 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
230 /* For now, just pass the size request on to the root Glk window */
231 if(priv->root_window)
232 request_recurse(priv->root_window->data, requisition);
234 requisition->width = CHIMARA_GLK_MIN_WIDTH;
235 requisition->height = CHIMARA_GLK_MIN_HEIGHT;
239 /* Recursively give the Glk windows their allocated space */
241 allocate_recurse(winid_t win, GtkAllocation *allocation)
243 if(win->type == wintype_Pair)
245 GtkAllocation child1, child2;
246 child1.x = allocation->x;
247 child1.y = allocation->y;
249 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
251 switch(win->split_method & winmethod_DirMask)
254 child1.width = win->constraint_size * win->key_window->unit_width;
255 if(child1.width > allocation->width)
256 child1.width = allocation->width;
258 case winmethod_Right:
259 child2.width = win->constraint_size * win->key_window->unit_width;
260 if(child2.width > allocation->width)
261 child2.width = allocation->width;
263 case winmethod_Above:
264 child1.height = win->constraint_size * win->key_window->unit_height;
265 if(child1.height > allocation->height)
266 child1.height = allocation->height;
268 case winmethod_Below:
269 child2.height = win->constraint_size * win->key_window->unit_height;
270 if(child2.height > allocation->height)
271 child2.height = allocation->height;
275 else /* proportional */
277 switch(win->split_method & winmethod_DirMask)
280 child1.width = (glui32)ceil((win->constraint_size / 100.0) * allocation->width);
282 case winmethod_Right:
283 child2.width = (glui32)ceil((win->constraint_size / 100.0) * allocation->width);
285 case winmethod_Above:
286 child1.height = (glui32)ceil((win->constraint_size / 100.0) * allocation->height);
288 case winmethod_Below:
289 child2.height = (glui32)ceil((win->constraint_size / 100.0) * allocation->height);
294 /* Fill in the rest of the size requisitions according to the child specified above */
295 switch(win->split_method & winmethod_DirMask)
298 child2.width = allocation->width - child1.width;
299 child2.x = child1.x + child1.width;
301 child1.height = child2.height = allocation->height;
303 case winmethod_Right:
304 child1.width = allocation->width - child2.width;
305 child2.x = child1.x + child1.width;
307 child1.height = child2.height = allocation->height;
309 case winmethod_Above:
310 child2.height = allocation->height - child1.height;
312 child2.y = child1.y + child1.height;
313 child1.width = child2.width = allocation->width;
315 case winmethod_Below:
316 child1.height = allocation->height - child2.height;
318 child2.y = child1.y + child1.height;
319 child1.width = child2.width = allocation->width;
324 allocate_recurse(win->window_node->children->data, &child1);
325 allocate_recurse(win->window_node->children->next->data, &child2);
328 /* For non-pair windows, just give them the size */
330 gtk_widget_size_allocate(win->frame, allocation);
333 /* Overrides gtk_widget_size_allocate */
335 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
337 g_return_if_fail(widget);
338 g_return_if_fail(allocation);
339 g_return_if_fail(CHIMARA_IS_GLK(widget));
341 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
343 widget->allocation = *allocation;
345 if(priv->root_window)
346 allocate_recurse(priv->root_window->data, allocation);
349 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
351 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
353 if(win->type == wintype_Pair)
355 forall_recurse(win->window_node->children->data, callback, callback_data);
356 forall_recurse(win->window_node->children->next->data, callback, callback_data);
359 (*callback)(win->frame, callback_data);
362 /* Overrides gtk_container_forall */
364 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
366 g_return_if_fail(container);
367 g_return_if_fail(CHIMARA_IS_GLK(container));
369 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
371 /* All the children are "internal" */
372 if(!include_internals)
375 if(priv->root_window)
376 forall_recurse(priv->root_window->data, callback, callback_data);
380 chimara_glk_stopped(ChimaraGlk *self)
382 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
384 /* Free the plugin */
385 if( priv->program && !g_module_close(priv->program) )
386 g_warning( "Error closing module: %s", g_module_error() );
390 chimara_glk_started(ChimaraGlk *self)
392 /* TODO: Add default signal handler implementation here */
395 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
396 #ifndef G_PARAM_STATIC_STRINGS
397 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
401 chimara_glk_class_init(ChimaraGlkClass *klass)
403 /* Override methods of parent classes */
404 GObjectClass *object_class = G_OBJECT_CLASS(klass);
405 object_class->set_property = chimara_glk_set_property;
406 object_class->get_property = chimara_glk_get_property;
407 object_class->finalize = chimara_glk_finalize;
409 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
410 widget_class->size_request = chimara_glk_size_request;
411 widget_class->size_allocate = chimara_glk_size_allocate;
413 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
414 container_class->forall = chimara_glk_forall;
417 klass->stopped = chimara_glk_stopped;
418 klass->started = chimara_glk_started;
420 * ChimaraGlk::stopped:
421 * @glk: The widget that received the signal
423 * The ::stopped signal is emitted when the a Glk program finishes
424 * executing in the widget, whether it ended normally, or was interrupted.
426 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
427 G_OBJECT_CLASS_TYPE(klass), 0,
428 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
429 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
431 * ChimaraGlk::started:
432 * @glk: The widget that received the signal
434 * The ::started signal is emitted when a Glk program starts executing in
437 chimara_glk_signals[STARTED] = g_signal_new ("started",
438 G_OBJECT_CLASS_TYPE (klass), 0,
439 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
440 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
444 pspec = g_param_spec_boolean("interactive", _("Interactive"),
445 _("Whether user input is expected in the Glk program"),
447 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
449 * ChimaraGlk:interactive:
451 * Sets whether the widget is interactive. A Glk widget is normally
452 * interactive, but in non-interactive mode, keyboard and mouse input are
453 * ignored and the Glk program is controlled by chimara_glk_feed_text().
454 * <quote>More</quote> prompts when a lot of text is printed to a text
455 * buffer are also disabled. This is typically used when you wish to control
456 * an interpreter program by feeding it a predefined list of commands.
458 g_object_class_install_property(object_class, PROP_INTERACTIVE, pspec);
459 pspec = g_param_spec_boolean("protect", _("Protected"),
460 _("Whether the Glk program is barred from doing file operations"),
462 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
464 * ChimaraGlk:protect:
466 * Sets whether the Glk program is allowed to do file operations. In protect
467 * mode, all file operations will fail.
469 g_object_class_install_property(object_class, PROP_PROTECT, pspec);
470 pspec = g_param_spec_pointer("default-font-description", _("Default Font"),
471 _("Font description of the default proportional font"),
472 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
473 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
474 initialize them with NULL */
476 * ChimaraGlk:default-font-description:
478 * Pointer to a #PangoFontDescription describing the default proportional
479 * font, to be used in text buffer windows for example.
481 * Default value: font description created from the string
482 * <quote>Sans</quote>
484 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, pspec);
485 pspec = g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
486 _("Font description of the default monospace font"),
487 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
489 * ChimaraGlk:monospace-font-description:
491 * Pointer to a #PangoFontDescription describing the default monospace font,
492 * to be used in text grid windows and #style_Preformatted, for example.
494 * Default value: font description created from the string
495 * <quote>Monospace</quote>
497 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, pspec);
499 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
502 /* PUBLIC FUNCTIONS */
507 * Creates and initializes a new #ChimaraGlk widget.
509 * Return value: a #ChimaraGlk widget, with a floating reference.
512 chimara_glk_new(void)
514 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
515 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
517 priv->event_queue = g_queue_new();
518 priv->event_lock = g_mutex_new();
519 priv->event_queue_not_empty = g_cond_new();
520 priv->event_queue_not_full = g_cond_new();
521 priv->abort_lock = g_mutex_new();
523 return GTK_WIDGET(self);
527 * chimara_glk_set_interactive:
528 * @glk: a #ChimaraGlk widget
529 * @interactive: whether the widget should expect user input
531 * Sets the #ChimaraGlk:interactive property of @glk.
534 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
536 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
538 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
539 priv->interactive = interactive;
543 * chimara_glk_get_interactive:
544 * @glk: a #ChimaraGlk widget
546 * Returns whether @glk is interactive (expecting user input). See
547 * #ChimaraGlk:interactive.
549 * Return value: %TRUE if @glk is interactive.
552 chimara_glk_get_interactive(ChimaraGlk *glk)
554 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
556 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
557 return priv->interactive;
561 * chimara_glk_set_protect:
562 * @glk: a #ChimaraGlk widget
563 * @protect: whether the widget should allow the Glk program to do file
566 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
567 * program is not allowed to do file operations.
570 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
572 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
574 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
575 priv->protect = protect;
579 * chimara_glk_get_protect:
580 * @glk: a #ChimaraGlk widget
582 * Returns whether @glk is in protect mode (banned from doing file operations).
583 * See #ChimaraGlk:protect.
585 * Return value: %TRUE if @glk is in protect mode.
588 chimara_glk_get_protect(ChimaraGlk *glk)
590 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
592 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
593 return priv->protect;
597 * chimara_glk_set_default_font_description:
598 * @glk: a #ChimaraGlk widget
599 * @font: a #PangoFontDescription
601 * Sets @glk's default proportional font. See
602 * #ChimaraGlk:default-font-description.
605 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
607 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
608 g_return_if_fail(font);
610 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
611 pango_font_description_free(priv->default_font_desc);
612 priv->default_font_desc = pango_font_description_copy(font);
614 /* TODO: Apply the font description to all the windows and recalculate the sizes */
618 * chimara_glk_set_default_font_string:
619 * @glk: a #ChimaraGlk widget
620 * @font: string representation of a font description
622 * Sets @glk's default proportional font according to the string @font, which
623 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
624 * [<replaceable>STYLE-OPTIONS</replaceable>]
625 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
626 * Italic 12</quote> or <quote>Sans</quote>. See
627 * #ChimaraGlk:default-font-description.
630 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
632 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
633 g_return_if_fail(font || *font);
635 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
636 g_return_if_fail(fontdesc);
638 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
639 pango_font_description_free(priv->default_font_desc);
640 priv->default_font_desc = fontdesc;
642 /* TODO: Apply the font description to all the windows and recalculate the sizes */
646 * chimara_glk_get_default_font_description:
648 * Returns @glk's default proportional font.
650 * Return value: a newly-allocated #PangoFontDescription which must be freed
651 * using pango_font_description_free(), or %NULL on error.
653 PangoFontDescription *
654 chimara_glk_get_default_font_description(ChimaraGlk *glk)
656 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
658 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
659 return pango_font_description_copy(priv->default_font_desc);
663 * chimara_glk_set_monospace_font_description:
664 * @glk: a #ChimaraGlk widget
665 * @font: a #PangoFontDescription
667 * Sets @glk's default monospace font. See
668 * #ChimaraGlk:monospace-font-description.
671 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
673 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
674 g_return_if_fail(font);
676 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
677 pango_font_description_free(priv->monospace_font_desc);
678 priv->monospace_font_desc = pango_font_description_copy(font);
680 /* TODO: Apply the font description to all the windows and recalculate the sizes */
684 * chimara_glk_set_monospace_font_string:
685 * @glk: a #ChimaraGlk widget
686 * @font: string representation of a font description
688 * Sets @glk's default monospace font according to the string @font, which must
689 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
690 * [<replaceable>STYLE-OPTIONS</replaceable>]
691 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
692 * Bold 12</quote> or <quote>Monospace</quote>. See
693 * #ChimaraGlk:monospace-font-description.
696 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
698 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
699 g_return_if_fail(font || *font);
701 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
702 g_return_if_fail(fontdesc);
704 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
705 pango_font_description_free(priv->monospace_font_desc);
706 priv->monospace_font_desc = fontdesc;
708 /* TODO: Apply the font description to all the windows and recalculate the sizes */
712 * chimara_glk_get_monospace_font_description:
714 * Returns @glk's default monospace font.
716 * Return value: a newly-allocated #PangoFontDescription which must be freed
717 * using pango_font_description_free(), or %NULL on error.
719 PangoFontDescription *
720 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
722 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
724 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
725 return pango_font_description_copy(priv->monospace_font_desc);
728 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
730 glk_enter(gpointer glk_main)
732 extern ChimaraGlkPrivate *glk_data;
733 g_signal_emit_by_name(glk_data->self, "started");
734 ((glk_main_t)glk_main)();
735 g_signal_emit_by_name(glk_data->self, "stopped");
741 * @glk: a #ChimaraGlk widget
742 * @plugin: path to a plugin module compiled with <filename
743 * class="header">glk.h</filename>
744 * @error: location to store a <link linkend="glib-GError">GError</link>, or
747 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
748 * a separate thread. On failure, returns %FALSE and sets @error.
750 * Return value: %TRUE if the Glk program was started successfully.
753 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
755 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
756 g_return_val_if_fail(plugin, FALSE);
758 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
760 /* Open the module to run */
762 g_assert( g_module_supported() );
763 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
767 g_warning( "Error opening module: %s", g_module_error() );
770 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
772 g_warning( "Error finding glk_main(): %s", g_module_error() );
776 extern ChimaraGlkPrivate *glk_data;
777 /* Set the thread's private data */
778 /* TODO: Do this with a GPrivate */
781 /* Run in a separate thread */
782 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
784 return !(priv->thread == NULL);
789 * @glk: a #ChimaraGlk widget
791 * Signals the Glk program running in @glk to abort. Note that if the program is
792 * caught in an infinite loop in which glk_tick() is not called, this may not
796 chimara_glk_stop(ChimaraGlk *glk)
798 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
799 /* TODO: check if glk is actually running a program */
805 * @glk: a #ChimaraGlk widget
807 * Holds up the main thread and waits for the Glk program running in @glk to
811 chimara_glk_wait(ChimaraGlk *glk)
813 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
815 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
816 g_thread_join(priv->thread);