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"
15 #define CHIMARA_GLK_MIN_WIDTH 0
16 #define CHIMARA_GLK_MIN_HEIGHT 0
20 * @short_description: Widget which executes a Glk program
21 * @stability: Unstable
22 * @include: chimara/chimara-glk.h
24 * The ChimaraGlk widget opens and runs a Glk program. The program must be
25 * compiled as a plugin module, with a function <function>glk_main()</function>
26 * that the Glk library can hook into.
28 * On Linux systems, this is a file with a name like
29 * <filename>plugin.so</filename>. For portability, you can use libtool and
32 * pkglib_LTLIBRARIES = plugin.la
33 * plugin_la_SOURCES = plugin.c foo.c bar.c
34 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
36 * This will produce <filename>plugin.la</filename> which is a text file
37 * containing the correct plugin file to open (see the relevant section of the
39 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
40 * Libtool manual</ulink>).
43 typedef void (* glk_main_t) (void);
44 typedef void (* glkunix_startup_code_t) (glkunix_startup_t*);
50 PROP_DEFAULT_FONT_DESCRIPTION,
51 PROP_MONOSPACE_FONT_DESCRIPTION,
62 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
64 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
67 chimara_glk_init(ChimaraGlk *self)
69 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
71 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
74 priv->interactive = TRUE;
75 priv->protect = FALSE;
76 priv->default_font_desc = pango_font_description_from_string("Sans");
77 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
80 priv->event_queue = NULL;
81 priv->event_lock = NULL;
82 priv->event_queue_not_empty = NULL;
83 priv->event_queue_not_full = NULL;
84 priv->abort_lock = NULL;
85 priv->abort_signalled = FALSE;
86 priv->interrupt_handler = NULL;
87 priv->root_window = NULL;
88 priv->fileref_list = NULL;
89 priv->current_stream = NULL;
90 priv->stream_list = NULL;
95 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
97 ChimaraGlk *glk = CHIMARA_GLK(object);
101 case PROP_INTERACTIVE:
102 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
105 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
107 case PROP_DEFAULT_FONT_DESCRIPTION:
108 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
110 case PROP_MONOSPACE_FONT_DESCRIPTION:
111 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
114 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
117 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
122 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
124 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
128 case PROP_INTERACTIVE:
129 g_value_set_boolean(value, priv->interactive);
132 g_value_set_boolean(value, priv->protect);
134 case PROP_DEFAULT_FONT_DESCRIPTION:
135 g_value_set_pointer(value, priv->default_font_desc);
137 case PROP_MONOSPACE_FONT_DESCRIPTION:
138 g_value_set_pointer(value, priv->monospace_font_desc);
141 g_value_set_uint(value, priv->spacing);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
149 chimara_glk_finalize(GObject *object)
151 ChimaraGlk *self = CHIMARA_GLK(object);
152 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
154 /* Free the event queue */
155 g_mutex_lock(priv->event_lock);
156 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
157 g_queue_free(priv->event_queue);
158 g_cond_free(priv->event_queue_not_empty);
159 g_cond_free(priv->event_queue_not_full);
160 priv->event_queue = NULL;
161 g_mutex_unlock(priv->event_lock);
162 g_mutex_free(priv->event_lock);
164 /* Free the abort signalling mechanism */
165 g_mutex_lock(priv->abort_lock);
166 /* Make sure no other thread is busy with this */
167 g_mutex_unlock(priv->abort_lock);
168 g_mutex_free(priv->abort_lock);
169 priv->abort_lock = NULL;
171 /* Free private data */
172 pango_font_description_free(priv->default_font_desc);
173 pango_font_description_free(priv->monospace_font_desc);
175 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
178 /* Internal function: Recursively get the Glk window tree's size request */
180 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
182 if(win->type == wintype_Pair)
184 /* Get children's size requests */
185 GtkRequisition child1, child2;
186 request_recurse(win->window_node->children->data, &child1, spacing);
187 request_recurse(win->window_node->children->next->data, &child2, spacing);
189 /* If the split is fixed, get the size of the fixed child */
190 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
192 switch(win->split_method & winmethod_DirMask)
195 child1.width = win->key_window?
196 win->constraint_size * win->key_window->unit_width
199 case winmethod_Right:
200 child2.width = win->key_window?
201 win->constraint_size * win->key_window->unit_width
204 case winmethod_Above:
205 child1.height = win->key_window?
206 win->constraint_size * win->key_window->unit_height
209 case winmethod_Below:
210 child2.height = win->key_window?
211 win->constraint_size * win->key_window->unit_height
217 /* Add the children's requests */
218 switch(win->split_method & winmethod_DirMask)
221 case winmethod_Right:
222 requisition->width = child1.width + child2.width + spacing;
223 requisition->height = MAX(child1.height, child2.height);
225 case winmethod_Above:
226 case winmethod_Below:
227 requisition->width = MAX(child1.width, child2.width);
228 requisition->height = child1.height + child2.height + spacing;
233 /* For non-pair windows, just use the size that GTK requests */
235 gtk_widget_size_request(win->frame, requisition);
238 /* Overrides gtk_widget_size_request */
240 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
242 g_return_if_fail(widget);
243 g_return_if_fail(requisition);
244 g_return_if_fail(CHIMARA_IS_GLK(widget));
246 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
248 /* For now, just pass the size request on to the root Glk window */
249 if(priv->root_window)
251 request_recurse(priv->root_window->data, requisition, priv->spacing);
252 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
253 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
257 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
258 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
262 /* Recursively give the Glk windows their allocated space */
264 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
266 if(win->type == wintype_Pair)
268 GtkAllocation child1, child2;
269 child1.x = allocation->x;
270 child1.y = allocation->y;
272 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
274 /* If the key window has been closed, then default to 0; otherwise
275 use the key window to determine the size */
276 switch(win->split_method & winmethod_DirMask)
279 child1.width = win->key_window?
280 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
283 case winmethod_Right:
284 child2.width = win->key_window?
285 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
288 case winmethod_Above:
289 child1.height = win->key_window?
290 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
293 case winmethod_Below:
294 child2.height = win->key_window?
295 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
300 else /* proportional */
302 gdouble fraction = win->constraint_size / 100.0;
303 switch(win->split_method & winmethod_DirMask)
306 child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
308 case winmethod_Right:
309 child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
311 case winmethod_Above:
312 child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
314 case winmethod_Below:
315 child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
320 /* Fill in the rest of the size requisitions according to the child specified above */
321 switch(win->split_method & winmethod_DirMask)
324 child2.width = allocation->width - spacing - child1.width;
325 child2.x = child1.x + child1.width + spacing;
327 child1.height = child2.height = allocation->height;
329 case winmethod_Right:
330 child1.width = allocation->width - spacing - child2.width;
331 child2.x = child1.x + child1.width + spacing;
333 child1.height = child2.height = allocation->height;
335 case winmethod_Above:
336 child2.height = allocation->height - spacing - child1.height;
338 child2.y = child1.y + child1.height + spacing;
339 child1.width = child2.width = allocation->width;
341 case winmethod_Below:
342 child1.height = allocation->height - spacing - child2.height;
344 child2.y = child1.y + child1.height + spacing;
345 child1.width = child2.width = allocation->width;
350 allocate_recurse(win->window_node->children->data, &child1, spacing);
351 allocate_recurse(win->window_node->children->next->data, &child2, spacing);
354 else if(win->type == wintype_TextGrid)
356 /* Pass the size allocation on to the framing widget */
357 gtk_widget_size_allocate(win->frame, allocation);
358 /* It says in the spec that when a text grid window is resized smaller,
359 the bottom or right area is thrown away; when it is resized larger, the
360 bottom or right area is filled with blanks. */
361 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
362 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
364 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
365 GtkTextIter start, end;
367 for(line = 0; line < win->height; line++)
369 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
370 /* If this line is going to fall off the bottom, delete it */
371 if(line >= newheight)
374 gtk_text_iter_forward_to_line_end(&end);
375 gtk_text_iter_forward_char(&end);
376 gtk_text_buffer_delete(textbuffer, &start, &end);
379 /* If this line is not long enough, add spaces on the end */
380 if(newwidth > win->width)
382 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
383 gtk_text_iter_forward_to_line_end(&start);
384 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
387 /* But if it's too long, delete characters from the end */
388 else if(newwidth < win->width)
391 gtk_text_iter_forward_chars(&start, newwidth);
392 gtk_text_iter_forward_to_line_end(&end);
393 gtk_text_buffer_delete(textbuffer, &start, &end);
395 /* Note: if the widths are equal, do nothing */
397 /* Add blank lines if there aren't enough lines to fit the new size */
398 if(newheight > win->height)
400 gchar *blanks = g_strnfill(win->width, ' ');
401 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
403 for(count = 0; count < newheight - win->height; count++)
404 blanklines[count] = blanks;
405 blanklines[newheight - win->height] = NULL;
406 gchar *text = g_strjoinv("\n", blanklines);
407 g_free(blanklines); /* not g_strfreev() */
410 gtk_text_buffer_get_end_iter(textbuffer, &start);
411 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
412 gtk_text_buffer_insert(textbuffer, &start, text, -1);
416 win->width = newwidth;
417 win->height = newheight;
420 /* For non-pair, non-text-grid windows, just give them the size */
422 gtk_widget_size_allocate(win->frame, allocation);
425 /* Overrides gtk_widget_size_allocate */
427 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
429 g_return_if_fail(widget);
430 g_return_if_fail(allocation);
431 g_return_if_fail(CHIMARA_IS_GLK(widget));
433 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
435 widget->allocation = *allocation;
437 if(priv->root_window) {
439 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
440 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
441 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
442 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
443 allocate_recurse(priv->root_window->data, &child, priv->spacing);
447 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
449 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
451 if(win->type == wintype_Pair)
453 forall_recurse(win->window_node->children->data, callback, callback_data);
454 forall_recurse(win->window_node->children->next->data, callback, callback_data);
457 (*callback)(win->frame, callback_data);
460 /* Overrides gtk_container_forall */
462 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
464 g_return_if_fail(container);
465 g_return_if_fail(CHIMARA_IS_GLK(container));
467 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
469 /* All the children are "internal" */
470 if(!include_internals)
473 if(priv->root_window)
474 forall_recurse(priv->root_window->data, callback, callback_data);
478 chimara_glk_stopped(ChimaraGlk *self)
480 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
482 /* Free the plugin */
483 if( priv->program && !g_module_close(priv->program) )
484 g_warning( "Error closing module: %s", g_module_error() );
488 chimara_glk_started(ChimaraGlk *self)
490 /* TODO: Add default signal handler implementation here */
493 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
494 #ifndef G_PARAM_STATIC_STRINGS
495 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
499 chimara_glk_class_init(ChimaraGlkClass *klass)
501 /* Override methods of parent classes */
502 GObjectClass *object_class = G_OBJECT_CLASS(klass);
503 object_class->set_property = chimara_glk_set_property;
504 object_class->get_property = chimara_glk_get_property;
505 object_class->finalize = chimara_glk_finalize;
507 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
508 widget_class->size_request = chimara_glk_size_request;
509 widget_class->size_allocate = chimara_glk_size_allocate;
511 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
512 container_class->forall = chimara_glk_forall;
515 klass->stopped = chimara_glk_stopped;
516 klass->started = chimara_glk_started;
518 * ChimaraGlk::stopped:
519 * @glk: The widget that received the signal
521 * The ::stopped signal is emitted when the a Glk program finishes
522 * executing in the widget, whether it ended normally, or was interrupted.
524 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
525 G_OBJECT_CLASS_TYPE(klass), 0,
526 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
527 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
529 * ChimaraGlk::started:
530 * @glk: The widget that received the signal
532 * The ::started signal is emitted when a Glk program starts executing in
535 chimara_glk_signals[STARTED] = g_signal_new ("started",
536 G_OBJECT_CLASS_TYPE (klass), 0,
537 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
538 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
542 * ChimaraGlk:interactive:
544 * Sets whether the widget is interactive. A Glk widget is normally
545 * interactive, but in non-interactive mode, keyboard and mouse input are
546 * ignored and the Glk program is controlled by chimara_glk_feed_text().
547 * <quote>More</quote> prompts when a lot of text is printed to a text
548 * buffer are also disabled. This is typically used when you wish to control
549 * an interpreter program by feeding it a predefined list of commands.
551 g_object_class_install_property( object_class, PROP_INTERACTIVE,
552 g_param_spec_boolean("interactive", _("Interactive"),
553 _("Whether user input is expected in the Glk program"),
555 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
558 * ChimaraGlk:protect:
560 * Sets whether the Glk program is allowed to do file operations. In protect
561 * mode, all file operations will fail.
563 g_object_class_install_property(object_class, PROP_PROTECT,
564 g_param_spec_boolean("protect", _("Protected"),
565 _("Whether the Glk program is barred from doing file operations"),
567 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
569 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
570 initialize them with NULL */
572 * ChimaraGlk:default-font-description:
574 * Pointer to a #PangoFontDescription describing the default proportional
575 * font, to be used in text buffer windows for example.
577 * Default value: font description created from the string
578 * <quote>Sans</quote>
580 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
581 g_param_spec_pointer("default-font-description", _("Default Font"),
582 _("Font description of the default proportional font"),
583 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
586 * ChimaraGlk:monospace-font-description:
588 * Pointer to a #PangoFontDescription describing the default monospace font,
589 * to be used in text grid windows and #style_Preformatted, for example.
591 * Default value: font description created from the string
592 * <quote>Monospace</quote>
594 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
595 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
596 _("Font description of the default monospace font"),
597 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
600 * ChimaraGlk:spacing:
602 * The amount of space between the Glk windows.
604 g_object_class_install_property(object_class, PROP_SPACING,
605 g_param_spec_uint("spacing", _("Spacing"),
606 _("The amount of space between Glk windows"),
608 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
611 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
614 /* PUBLIC FUNCTIONS */
619 * Creates and initializes a new #ChimaraGlk widget.
621 * Return value: a #ChimaraGlk widget, with a floating reference.
624 chimara_glk_new(void)
626 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
627 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
629 priv->event_queue = g_queue_new();
630 priv->event_lock = g_mutex_new();
631 priv->event_queue_not_empty = g_cond_new();
632 priv->event_queue_not_full = g_cond_new();
633 priv->abort_lock = g_mutex_new();
635 return GTK_WIDGET(self);
639 * chimara_glk_set_interactive:
640 * @glk: a #ChimaraGlk widget
641 * @interactive: whether the widget should expect user input
643 * Sets the #ChimaraGlk:interactive property of @glk.
646 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
648 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
650 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
651 priv->interactive = interactive;
655 * chimara_glk_get_interactive:
656 * @glk: a #ChimaraGlk widget
658 * Returns whether @glk is interactive (expecting user input). See
659 * #ChimaraGlk:interactive.
661 * Return value: %TRUE if @glk is interactive.
664 chimara_glk_get_interactive(ChimaraGlk *glk)
666 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
668 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
669 return priv->interactive;
673 * chimara_glk_set_protect:
674 * @glk: a #ChimaraGlk widget
675 * @protect: whether the widget should allow the Glk program to do file
678 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
679 * program is not allowed to do file operations.
682 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
684 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
686 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
687 priv->protect = protect;
691 * chimara_glk_get_protect:
692 * @glk: a #ChimaraGlk widget
694 * Returns whether @glk is in protect mode (banned from doing file operations).
695 * See #ChimaraGlk:protect.
697 * Return value: %TRUE if @glk is in protect mode.
700 chimara_glk_get_protect(ChimaraGlk *glk)
702 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
704 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
705 return priv->protect;
709 * chimara_glk_set_default_font_description:
710 * @glk: a #ChimaraGlk widget
711 * @font: a #PangoFontDescription
713 * Sets @glk's default proportional font. See
714 * #ChimaraGlk:default-font-description.
717 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
719 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
720 g_return_if_fail(font);
722 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
723 pango_font_description_free(priv->default_font_desc);
724 priv->default_font_desc = pango_font_description_copy(font);
726 /* TODO: Apply the font description to all the windows and recalculate the sizes */
730 * chimara_glk_set_default_font_string:
731 * @glk: a #ChimaraGlk widget
732 * @font: string representation of a font description
734 * Sets @glk's default proportional font according to the string @font, which
735 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
736 * [<replaceable>STYLE-OPTIONS</replaceable>]
737 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
738 * Italic 12</quote> or <quote>Sans</quote>. See
739 * #ChimaraGlk:default-font-description.
742 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
744 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
745 g_return_if_fail(font || *font);
747 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
748 g_return_if_fail(fontdesc);
750 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
751 pango_font_description_free(priv->default_font_desc);
752 priv->default_font_desc = fontdesc;
754 /* TODO: Apply the font description to all the windows and recalculate the sizes */
758 * chimara_glk_get_default_font_description:
759 * @glk: a #ChimaraGlk widget
761 * Returns @glk's default proportional font.
763 * Return value: a newly-allocated #PangoFontDescription which must be freed
764 * using pango_font_description_free(), or %NULL on error.
766 PangoFontDescription *
767 chimara_glk_get_default_font_description(ChimaraGlk *glk)
769 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
771 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
772 return pango_font_description_copy(priv->default_font_desc);
776 * chimara_glk_set_monospace_font_description:
777 * @glk: a #ChimaraGlk widget
778 * @font: a #PangoFontDescription
780 * Sets @glk's default monospace font. See
781 * #ChimaraGlk:monospace-font-description.
784 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
786 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
787 g_return_if_fail(font);
789 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
790 pango_font_description_free(priv->monospace_font_desc);
791 priv->monospace_font_desc = pango_font_description_copy(font);
793 /* TODO: Apply the font description to all the windows and recalculate the sizes */
797 * chimara_glk_set_monospace_font_string:
798 * @glk: a #ChimaraGlk widget
799 * @font: string representation of a font description
801 * Sets @glk's default monospace font according to the string @font, which must
802 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
803 * [<replaceable>STYLE-OPTIONS</replaceable>]
804 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
805 * Bold 12</quote> or <quote>Monospace</quote>. See
806 * #ChimaraGlk:monospace-font-description.
809 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
811 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
812 g_return_if_fail(font || *font);
814 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
815 g_return_if_fail(fontdesc);
817 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
818 pango_font_description_free(priv->monospace_font_desc);
819 priv->monospace_font_desc = fontdesc;
821 /* TODO: Apply the font description to all the windows and recalculate the sizes */
825 * chimara_glk_get_monospace_font_description:
826 * @glk: a #ChimaraGlk widget
828 * Returns @glk's default monospace font.
830 * Return value: a newly-allocated #PangoFontDescription which must be freed
831 * using pango_font_description_free(), or %NULL on error.
833 PangoFontDescription *
834 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
836 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
838 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
839 return pango_font_description_copy(priv->monospace_font_desc);
843 * chimara_glk_set_spacing:
844 * @glk: a #ChimaraGlk widget
845 * @spacing: the number of pixels to put between Glk windows
847 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
848 * pixels between Glk windows.
851 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
853 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
855 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
856 priv->spacing = spacing;
860 * chimara_glk_get_spacing:
861 * @glk: a #ChimaraGlk widget
863 * Gets the value set by chimara_glk_set_spacing().
865 * Return value: pixels of spacing between Glk windows
868 chimara_glk_get_spacing(ChimaraGlk *glk)
870 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
872 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
873 return priv->spacing;
876 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
878 glk_enter(gpointer glk_main)
880 extern ChimaraGlkPrivate *glk_data;
882 g_signal_emit_by_name(glk_data->self, "started");
883 ((glk_main_t)glk_main)();
884 g_signal_emit_by_name(glk_data->self, "stopped");
890 * @glk: a #ChimaraGlk widget
891 * @plugin: path to a plugin module compiled with <filename
892 * class="header">glk.h</filename>
893 * @error: location to store a <link linkend="glib-GError">GError</link>, or
896 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
897 * a separate thread. On failure, returns %FALSE and sets @error.
899 * Return value: %TRUE if the Glk program was started successfully.
902 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
904 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
905 g_return_val_if_fail(plugin, FALSE);
907 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
910 /* Open the module to run */
912 glkunix_startup_code_t glkunix_startup_code;
913 g_assert( g_module_supported() );
914 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
918 g_warning( "Error opening module: %s", g_module_error() );
921 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
923 g_warning( "Error finding glk_main(): %s", g_module_error() );
927 extern ChimaraGlkPrivate *glk_data;
928 /* Set the thread's private data */
929 /* TODO: Do this with a GPrivate */
932 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &glkunix_startup_code) )
934 glkunix_startup_t data;
938 glkunix_startup_code(&data);
941 /* Run in a separate thread */
942 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
944 return !(priv->thread == NULL);
949 * @glk: a #ChimaraGlk widget
951 * Signals the Glk program running in @glk to abort. Note that if the program is
952 * caught in an infinite loop in which glk_tick() is not called, this may not
956 chimara_glk_stop(ChimaraGlk *glk)
958 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
959 /* TODO: check if glk is actually running a program */
965 * @glk: a #ChimaraGlk widget
967 * Holds up the main thread and waits for the Glk program running in @glk to
971 chimara_glk_wait(ChimaraGlk *glk)
973 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
975 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
976 g_thread_join(priv->thread);