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)
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;
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;
244 /* Recursively give the Glk windows their allocated space */
246 allocate_recurse(winid_t win, GtkAllocation *allocation)
248 if(win->type == wintype_Pair)
250 GtkAllocation child1, child2;
251 child1.x = allocation->x;
252 child1.y = allocation->y;
254 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
256 switch(win->split_method & winmethod_DirMask)
259 child1.width = win->constraint_size * win->key_window->unit_width;
260 if(child1.width > allocation->width)
261 child1.width = allocation->width;
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;
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;
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;
280 else /* proportional */
282 switch(win->split_method & winmethod_DirMask)
285 child1.width = (glui32)ceil((win->constraint_size / 100.0) * allocation->width);
287 case winmethod_Right:
288 child2.width = (glui32)ceil((win->constraint_size / 100.0) * allocation->width);
290 case winmethod_Above:
291 child1.height = (glui32)ceil((win->constraint_size / 100.0) * allocation->height);
293 case winmethod_Below:
294 child2.height = (glui32)ceil((win->constraint_size / 100.0) * allocation->height);
299 /* Fill in the rest of the size requisitions according to the child specified above */
300 switch(win->split_method & winmethod_DirMask)
303 child2.width = allocation->width - child1.width;
304 child2.x = child1.x + child1.width;
306 child1.height = child2.height = allocation->height;
308 case winmethod_Right:
309 child1.width = allocation->width - child2.width;
310 child2.x = child1.x + child1.width;
312 child1.height = child2.height = allocation->height;
314 case winmethod_Above:
315 child2.height = allocation->height - child1.height;
317 child2.y = child1.y + child1.height;
318 child1.width = child2.width = allocation->width;
320 case winmethod_Below:
321 child1.height = allocation->height - child2.height;
323 child2.y = child1.y + child1.height;
324 child1.width = child2.width = allocation->width;
329 allocate_recurse(win->window_node->children->data, &child1);
330 allocate_recurse(win->window_node->children->next->data, &child2);
333 /* For non-pair windows, just give them the size */
335 gtk_widget_size_allocate(win->frame, allocation);
338 /* Overrides gtk_widget_size_allocate */
340 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
342 g_return_if_fail(widget);
343 g_return_if_fail(allocation);
344 g_return_if_fail(CHIMARA_IS_GLK(widget));
346 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
348 widget->allocation = *allocation;
350 if(priv->root_window) {
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);
360 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
362 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
364 if(win->type == wintype_Pair)
366 forall_recurse(win->window_node->children->data, callback, callback_data);
367 forall_recurse(win->window_node->children->next->data, callback, callback_data);
370 (*callback)(win->frame, callback_data);
373 /* Overrides gtk_container_forall */
375 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
377 g_return_if_fail(container);
378 g_return_if_fail(CHIMARA_IS_GLK(container));
380 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
382 /* All the children are "internal" */
383 if(!include_internals)
386 if(priv->root_window)
387 forall_recurse(priv->root_window->data, callback, callback_data);
391 chimara_glk_stopped(ChimaraGlk *self)
393 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
395 /* Free the plugin */
396 if( priv->program && !g_module_close(priv->program) )
397 g_warning( "Error closing module: %s", g_module_error() );
401 chimara_glk_started(ChimaraGlk *self)
403 /* TODO: Add default signal handler implementation here */
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)
412 chimara_glk_class_init(ChimaraGlkClass *klass)
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;
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;
424 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
425 container_class->forall = chimara_glk_forall;
428 klass->stopped = chimara_glk_stopped;
429 klass->started = chimara_glk_started;
431 * ChimaraGlk::stopped:
432 * @glk: The widget that received the signal
434 * The ::stopped signal is emitted when the a Glk program finishes
435 * executing in the widget, whether it ended normally, or was interrupted.
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);
442 * ChimaraGlk::started:
443 * @glk: The widget that received the signal
445 * The ::started signal is emitted when a Glk program starts executing in
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);
455 pspec = g_param_spec_boolean("interactive", _("Interactive"),
456 _("Whether user input is expected in the Glk program"),
458 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
460 * ChimaraGlk:interactive:
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.
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"),
473 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
475 * ChimaraGlk:protect:
477 * Sets whether the Glk program is allowed to do file operations. In protect
478 * mode, all file operations will fail.
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 */
487 * ChimaraGlk:default-font-description:
489 * Pointer to a #PangoFontDescription describing the default proportional
490 * font, to be used in text buffer windows for example.
492 * Default value: font description created from the string
493 * <quote>Sans</quote>
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);
500 * ChimaraGlk:monospace-font-description:
502 * Pointer to a #PangoFontDescription describing the default monospace font,
503 * to be used in text grid windows and #style_Preformatted, for example.
505 * Default value: font description created from the string
506 * <quote>Monospace</quote>
508 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, pspec);
510 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
513 /* PUBLIC FUNCTIONS */
518 * Creates and initializes a new #ChimaraGlk widget.
520 * Return value: a #ChimaraGlk widget, with a floating reference.
523 chimara_glk_new(void)
525 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
526 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
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();
534 return GTK_WIDGET(self);
538 * chimara_glk_set_interactive:
539 * @glk: a #ChimaraGlk widget
540 * @interactive: whether the widget should expect user input
542 * Sets the #ChimaraGlk:interactive property of @glk.
545 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
547 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
549 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
550 priv->interactive = interactive;
554 * chimara_glk_get_interactive:
555 * @glk: a #ChimaraGlk widget
557 * Returns whether @glk is interactive (expecting user input). See
558 * #ChimaraGlk:interactive.
560 * Return value: %TRUE if @glk is interactive.
563 chimara_glk_get_interactive(ChimaraGlk *glk)
565 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
567 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
568 return priv->interactive;
572 * chimara_glk_set_protect:
573 * @glk: a #ChimaraGlk widget
574 * @protect: whether the widget should allow the Glk program to do file
577 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
578 * program is not allowed to do file operations.
581 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
583 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
585 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
586 priv->protect = protect;
590 * chimara_glk_get_protect:
591 * @glk: a #ChimaraGlk widget
593 * Returns whether @glk is in protect mode (banned from doing file operations).
594 * See #ChimaraGlk:protect.
596 * Return value: %TRUE if @glk is in protect mode.
599 chimara_glk_get_protect(ChimaraGlk *glk)
601 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
603 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
604 return priv->protect;
608 * chimara_glk_set_default_font_description:
609 * @glk: a #ChimaraGlk widget
610 * @font: a #PangoFontDescription
612 * Sets @glk's default proportional font. See
613 * #ChimaraGlk:default-font-description.
616 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
618 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
619 g_return_if_fail(font);
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);
625 /* TODO: Apply the font description to all the windows and recalculate the sizes */
629 * chimara_glk_set_default_font_string:
630 * @glk: a #ChimaraGlk widget
631 * @font: string representation of a font description
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.
641 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
643 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
644 g_return_if_fail(font || *font);
646 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
647 g_return_if_fail(fontdesc);
649 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
650 pango_font_description_free(priv->default_font_desc);
651 priv->default_font_desc = fontdesc;
653 /* TODO: Apply the font description to all the windows and recalculate the sizes */
657 * chimara_glk_get_default_font_description:
659 * Returns @glk's default proportional font.
661 * Return value: a newly-allocated #PangoFontDescription which must be freed
662 * using pango_font_description_free(), or %NULL on error.
664 PangoFontDescription *
665 chimara_glk_get_default_font_description(ChimaraGlk *glk)
667 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
669 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
670 return pango_font_description_copy(priv->default_font_desc);
674 * chimara_glk_set_monospace_font_description:
675 * @glk: a #ChimaraGlk widget
676 * @font: a #PangoFontDescription
678 * Sets @glk's default monospace font. See
679 * #ChimaraGlk:monospace-font-description.
682 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
684 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
685 g_return_if_fail(font);
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);
691 /* TODO: Apply the font description to all the windows and recalculate the sizes */
695 * chimara_glk_set_monospace_font_string:
696 * @glk: a #ChimaraGlk widget
697 * @font: string representation of a font description
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.
707 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
709 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
710 g_return_if_fail(font || *font);
712 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
713 g_return_if_fail(fontdesc);
715 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
716 pango_font_description_free(priv->monospace_font_desc);
717 priv->monospace_font_desc = fontdesc;
719 /* TODO: Apply the font description to all the windows and recalculate the sizes */
723 * chimara_glk_get_monospace_font_description:
725 * Returns @glk's default monospace font.
727 * Return value: a newly-allocated #PangoFontDescription which must be freed
728 * using pango_font_description_free(), or %NULL on error.
730 PangoFontDescription *
731 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
733 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
735 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
736 return pango_font_description_copy(priv->monospace_font_desc);
739 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
741 glk_enter(gpointer glk_main)
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");
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
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.
761 * Return value: %TRUE if the Glk program was started successfully.
764 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
766 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
767 g_return_val_if_fail(plugin, FALSE);
769 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
771 /* Open the module to run */
773 g_assert( g_module_supported() );
774 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
778 g_warning( "Error opening module: %s", g_module_error() );
781 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
783 g_warning( "Error finding glk_main(): %s", g_module_error() );
787 extern ChimaraGlkPrivate *glk_data;
788 /* Set the thread's private data */
789 /* TODO: Do this with a GPrivate */
792 /* Run in a separate thread */
793 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
795 return !(priv->thread == NULL);
800 * @glk: a #ChimaraGlk widget
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
807 chimara_glk_stop(ChimaraGlk *glk)
809 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
810 /* TODO: check if glk is actually running a program */
816 * @glk: a #ChimaraGlk widget
818 * Holds up the main thread and waits for the Glk program running in @glk to
822 chimara_glk_wait(ChimaraGlk *glk)
824 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
826 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
827 g_thread_join(priv->thread);