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,
60 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
62 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
65 chimara_glk_init(ChimaraGlk *self)
67 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
69 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
72 priv->interactive = TRUE;
73 priv->protect = FALSE;
74 priv->default_font_desc = pango_font_description_from_string("Sans");
75 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
78 priv->event_queue = NULL;
79 priv->event_lock = NULL;
80 priv->event_queue_not_empty = NULL;
81 priv->event_queue_not_full = NULL;
82 priv->abort_lock = NULL;
83 priv->abort_signalled = FALSE;
84 priv->interrupt_handler = NULL;
85 priv->root_window = NULL;
86 priv->fileref_list = NULL;
87 priv->current_stream = NULL;
88 priv->stream_list = NULL;
92 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
94 ChimaraGlk *glk = CHIMARA_GLK(object);
98 case PROP_INTERACTIVE:
99 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
102 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
104 case PROP_DEFAULT_FONT_DESCRIPTION:
105 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
107 case PROP_MONOSPACE_FONT_DESCRIPTION:
108 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
111 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
114 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
119 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
121 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
125 case PROP_INTERACTIVE:
126 g_value_set_boolean(value, priv->interactive);
129 g_value_set_boolean(value, priv->protect);
131 case PROP_DEFAULT_FONT_DESCRIPTION:
132 g_value_set_pointer(value, priv->default_font_desc);
134 case PROP_MONOSPACE_FONT_DESCRIPTION:
135 g_value_set_pointer(value, priv->monospace_font_desc);
138 g_value_set_uint(value, priv->spacing);
141 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
146 chimara_glk_finalize(GObject *object)
148 ChimaraGlk *self = CHIMARA_GLK(object);
149 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
151 /* Free the event queue */
152 g_mutex_lock(priv->event_lock);
153 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
154 g_queue_free(priv->event_queue);
155 g_cond_free(priv->event_queue_not_empty);
156 g_cond_free(priv->event_queue_not_full);
157 priv->event_queue = NULL;
158 g_mutex_unlock(priv->event_lock);
159 g_mutex_free(priv->event_lock);
161 /* Free the abort signalling mechanism */
162 g_mutex_lock(priv->abort_lock);
163 /* Make sure no other thread is busy with this */
164 g_mutex_unlock(priv->abort_lock);
165 g_mutex_free(priv->abort_lock);
166 priv->abort_lock = NULL;
168 /* Free private data */
169 pango_font_description_free(priv->default_font_desc);
170 pango_font_description_free(priv->monospace_font_desc);
172 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
175 /* Internal function: Recursively get the Glk window tree's size request */
177 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
179 if(win->type == wintype_Pair)
181 /* Get children's size requests */
182 GtkRequisition child1, child2;
183 request_recurse(win->window_node->children->data, &child1, spacing);
184 request_recurse(win->window_node->children->next->data, &child2, spacing);
186 /* If the split is fixed, get the size of the fixed child */
187 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
189 switch(win->split_method & winmethod_DirMask)
192 child1.width = win->constraint_size * win->key_window->unit_width;
194 case winmethod_Right:
195 child2.width = win->constraint_size * win->key_window->unit_width;
197 case winmethod_Above:
198 child1.height = win->constraint_size * win->key_window->unit_height;
200 case winmethod_Below:
201 child2.height = win->constraint_size * win->key_window->unit_height;
206 /* Add the children's requests */
207 switch(win->split_method & winmethod_DirMask)
210 case winmethod_Right:
211 requisition->width = child1.width + child2.width + spacing;
212 requisition->height = MAX(child1.height, child2.height);
214 case winmethod_Above:
215 case winmethod_Below:
216 requisition->width = MAX(child1.width, child2.width);
217 requisition->height = child1.height + child2.height + spacing;
222 /* For non-pair windows, just use the size that GTK requests */
224 gtk_widget_size_request(win->frame, requisition);
227 /* Overrides gtk_widget_size_request */
229 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
231 g_return_if_fail(widget);
232 g_return_if_fail(requisition);
233 g_return_if_fail(CHIMARA_IS_GLK(widget));
235 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
237 /* For now, just pass the size request on to the root Glk window */
238 if(priv->root_window)
240 request_recurse(priv->root_window->data, requisition, priv->spacing);
241 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
242 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
246 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
247 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
251 /* Recursively give the Glk windows their allocated space */
253 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
255 if(win->type == wintype_Pair)
257 GtkAllocation child1, child2;
258 child1.x = allocation->x;
259 child1.y = allocation->y;
261 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
263 switch(win->split_method & winmethod_DirMask)
266 child1.width = CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing);
268 case winmethod_Right:
269 child2.width = CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing);
271 case winmethod_Above:
272 child1.height = CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing);
274 case winmethod_Below:
275 child2.height = CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing);
279 else /* proportional */
281 gdouble fraction = win->constraint_size / 100.0;
282 switch(win->split_method & winmethod_DirMask)
285 child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
287 case winmethod_Right:
288 child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
290 case winmethod_Above:
291 child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
293 case winmethod_Below:
294 child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
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 - spacing - child1.width;
304 child2.x = child1.x + child1.width + spacing;
306 child1.height = child2.height = allocation->height;
308 case winmethod_Right:
309 child1.width = allocation->width - spacing - child2.width;
310 child2.x = child1.x + child1.width + spacing;
312 child1.height = child2.height = allocation->height;
314 case winmethod_Above:
315 child2.height = allocation->height - spacing - child1.height;
317 child2.y = child1.y + child1.height + spacing;
318 child1.width = child2.width = allocation->width;
320 case winmethod_Below:
321 child1.height = allocation->height - spacing - child2.height;
323 child2.y = child1.y + child1.height + spacing;
324 child1.width = child2.width = allocation->width;
329 allocate_recurse(win->window_node->children->data, &child1, spacing);
330 allocate_recurse(win->window_node->children->next->data, &child2, spacing);
333 else if(win->type == wintype_TextGrid)
335 /* Pass the size allocation on to the framing widget */
336 gtk_widget_size_allocate(win->frame, allocation);
337 /* It says in the spec that when a text grid window is resized smaller,
338 the bottom or right area is thrown away; when it is resized larger, the
339 bottom or right area is filled with blanks. */
340 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
341 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
343 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
344 GtkTextIter start, end;
346 for(line = 0; line < win->height; line++)
348 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
352 gtk_text_iter_forward_to_line_end(&end);
353 gtk_text_iter_forward_char(&end);
354 gtk_text_buffer_delete(textbuffer, &start, &end);
357 if(newwidth > win->width)
359 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
360 gtk_text_iter_forward_to_line_end(&start);
361 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
364 else if(newwidth < win->width)
367 gtk_text_iter_forward_chars(&start, newwidth);
368 gtk_text_iter_forward_to_line_end(&end);
369 gtk_text_buffer_delete(textbuffer, &start, &end);
372 if(newheight > win->height)
374 gchar *blanks = g_strnfill(win->width, ' ');
375 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
377 for(count = 0; count < newheight - win->height; count++)
378 blanklines[count] = blanks;
379 blanklines[newheight - win->height] = NULL;
380 gchar *text = g_strjoinv("\n", blanklines);
381 g_free(blanklines); /* not g_strfreev() */
384 gtk_text_buffer_get_end_iter(textbuffer, &start);
385 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
386 gtk_text_buffer_insert(textbuffer, &start, text, -1);
390 win->width = newwidth;
391 win->height = newheight;
394 /* For non-pair, non-text-grid windows, just give them the size */
396 gtk_widget_size_allocate(win->frame, allocation);
399 /* Overrides gtk_widget_size_allocate */
401 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
403 g_return_if_fail(widget);
404 g_return_if_fail(allocation);
405 g_return_if_fail(CHIMARA_IS_GLK(widget));
407 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
409 widget->allocation = *allocation;
411 if(priv->root_window) {
413 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
414 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
415 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
416 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
417 allocate_recurse(priv->root_window->data, &child, priv->spacing);
421 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
423 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
425 if(win->type == wintype_Pair)
427 forall_recurse(win->window_node->children->data, callback, callback_data);
428 forall_recurse(win->window_node->children->next->data, callback, callback_data);
431 (*callback)(win->frame, callback_data);
434 /* Overrides gtk_container_forall */
436 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
438 g_return_if_fail(container);
439 g_return_if_fail(CHIMARA_IS_GLK(container));
441 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
443 /* All the children are "internal" */
444 if(!include_internals)
447 if(priv->root_window)
448 forall_recurse(priv->root_window->data, callback, callback_data);
452 chimara_glk_stopped(ChimaraGlk *self)
454 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
456 /* Free the plugin */
457 if( priv->program && !g_module_close(priv->program) )
458 g_warning( "Error closing module: %s", g_module_error() );
462 chimara_glk_started(ChimaraGlk *self)
464 /* TODO: Add default signal handler implementation here */
467 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
468 #ifndef G_PARAM_STATIC_STRINGS
469 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
473 chimara_glk_class_init(ChimaraGlkClass *klass)
475 /* Override methods of parent classes */
476 GObjectClass *object_class = G_OBJECT_CLASS(klass);
477 object_class->set_property = chimara_glk_set_property;
478 object_class->get_property = chimara_glk_get_property;
479 object_class->finalize = chimara_glk_finalize;
481 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
482 widget_class->size_request = chimara_glk_size_request;
483 widget_class->size_allocate = chimara_glk_size_allocate;
485 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
486 container_class->forall = chimara_glk_forall;
489 klass->stopped = chimara_glk_stopped;
490 klass->started = chimara_glk_started;
492 * ChimaraGlk::stopped:
493 * @glk: The widget that received the signal
495 * The ::stopped signal is emitted when the a Glk program finishes
496 * executing in the widget, whether it ended normally, or was interrupted.
498 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
499 G_OBJECT_CLASS_TYPE(klass), 0,
500 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
501 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
503 * ChimaraGlk::started:
504 * @glk: The widget that received the signal
506 * The ::started signal is emitted when a Glk program starts executing in
509 chimara_glk_signals[STARTED] = g_signal_new ("started",
510 G_OBJECT_CLASS_TYPE (klass), 0,
511 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
512 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
516 * ChimaraGlk:interactive:
518 * Sets whether the widget is interactive. A Glk widget is normally
519 * interactive, but in non-interactive mode, keyboard and mouse input are
520 * ignored and the Glk program is controlled by chimara_glk_feed_text().
521 * <quote>More</quote> prompts when a lot of text is printed to a text
522 * buffer are also disabled. This is typically used when you wish to control
523 * an interpreter program by feeding it a predefined list of commands.
525 g_object_class_install_property( object_class, PROP_INTERACTIVE,
526 g_param_spec_boolean("interactive", _("Interactive"),
527 _("Whether user input is expected in the Glk program"),
529 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
532 * ChimaraGlk:protect:
534 * Sets whether the Glk program is allowed to do file operations. In protect
535 * mode, all file operations will fail.
537 g_object_class_install_property(object_class, PROP_PROTECT,
538 g_param_spec_boolean("protect", _("Protected"),
539 _("Whether the Glk program is barred from doing file operations"),
541 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
543 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
544 initialize them with NULL */
546 * ChimaraGlk:default-font-description:
548 * Pointer to a #PangoFontDescription describing the default proportional
549 * font, to be used in text buffer windows for example.
551 * Default value: font description created from the string
552 * <quote>Sans</quote>
554 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
555 g_param_spec_pointer("default-font-description", _("Default Font"),
556 _("Font description of the default proportional font"),
557 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
560 * ChimaraGlk:monospace-font-description:
562 * Pointer to a #PangoFontDescription describing the default monospace font,
563 * to be used in text grid windows and #style_Preformatted, for example.
565 * Default value: font description created from the string
566 * <quote>Monospace</quote>
568 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
569 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
570 _("Font description of the default monospace font"),
571 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
574 * ChimaraGlk:spacing:
576 * The amount of space between the Glk windows.
578 g_object_class_install_property(object_class, PROP_SPACING,
579 g_param_spec_uint("spacing", _("Spacing"),
580 _("The amount of space between Glk windows"),
582 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
585 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
588 /* PUBLIC FUNCTIONS */
593 * Creates and initializes a new #ChimaraGlk widget.
595 * Return value: a #ChimaraGlk widget, with a floating reference.
598 chimara_glk_new(void)
600 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
601 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
603 priv->event_queue = g_queue_new();
604 priv->event_lock = g_mutex_new();
605 priv->event_queue_not_empty = g_cond_new();
606 priv->event_queue_not_full = g_cond_new();
607 priv->abort_lock = g_mutex_new();
609 return GTK_WIDGET(self);
613 * chimara_glk_set_interactive:
614 * @glk: a #ChimaraGlk widget
615 * @interactive: whether the widget should expect user input
617 * Sets the #ChimaraGlk:interactive property of @glk.
620 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
622 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
624 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
625 priv->interactive = interactive;
629 * chimara_glk_get_interactive:
630 * @glk: a #ChimaraGlk widget
632 * Returns whether @glk is interactive (expecting user input). See
633 * #ChimaraGlk:interactive.
635 * Return value: %TRUE if @glk is interactive.
638 chimara_glk_get_interactive(ChimaraGlk *glk)
640 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
642 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
643 return priv->interactive;
647 * chimara_glk_set_protect:
648 * @glk: a #ChimaraGlk widget
649 * @protect: whether the widget should allow the Glk program to do file
652 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
653 * program is not allowed to do file operations.
656 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
658 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
660 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
661 priv->protect = protect;
665 * chimara_glk_get_protect:
666 * @glk: a #ChimaraGlk widget
668 * Returns whether @glk is in protect mode (banned from doing file operations).
669 * See #ChimaraGlk:protect.
671 * Return value: %TRUE if @glk is in protect mode.
674 chimara_glk_get_protect(ChimaraGlk *glk)
676 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
678 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
679 return priv->protect;
683 * chimara_glk_set_default_font_description:
684 * @glk: a #ChimaraGlk widget
685 * @font: a #PangoFontDescription
687 * Sets @glk's default proportional font. See
688 * #ChimaraGlk:default-font-description.
691 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
693 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
694 g_return_if_fail(font);
696 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
697 pango_font_description_free(priv->default_font_desc);
698 priv->default_font_desc = pango_font_description_copy(font);
700 /* TODO: Apply the font description to all the windows and recalculate the sizes */
704 * chimara_glk_set_default_font_string:
705 * @glk: a #ChimaraGlk widget
706 * @font: string representation of a font description
708 * Sets @glk's default proportional font according to the string @font, which
709 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
710 * [<replaceable>STYLE-OPTIONS</replaceable>]
711 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
712 * Italic 12</quote> or <quote>Sans</quote>. See
713 * #ChimaraGlk:default-font-description.
716 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
718 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
719 g_return_if_fail(font || *font);
721 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
722 g_return_if_fail(fontdesc);
724 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
725 pango_font_description_free(priv->default_font_desc);
726 priv->default_font_desc = fontdesc;
728 /* TODO: Apply the font description to all the windows and recalculate the sizes */
732 * chimara_glk_get_default_font_description:
733 * @glk: a #ChimaraGlk widget
735 * Returns @glk's default proportional font.
737 * Return value: a newly-allocated #PangoFontDescription which must be freed
738 * using pango_font_description_free(), or %NULL on error.
740 PangoFontDescription *
741 chimara_glk_get_default_font_description(ChimaraGlk *glk)
743 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
745 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
746 return pango_font_description_copy(priv->default_font_desc);
750 * chimara_glk_set_monospace_font_description:
751 * @glk: a #ChimaraGlk widget
752 * @font: a #PangoFontDescription
754 * Sets @glk's default monospace font. See
755 * #ChimaraGlk:monospace-font-description.
758 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
760 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
761 g_return_if_fail(font);
763 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
764 pango_font_description_free(priv->monospace_font_desc);
765 priv->monospace_font_desc = pango_font_description_copy(font);
767 /* TODO: Apply the font description to all the windows and recalculate the sizes */
771 * chimara_glk_set_monospace_font_string:
772 * @glk: a #ChimaraGlk widget
773 * @font: string representation of a font description
775 * Sets @glk's default monospace font according to the string @font, which must
776 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
777 * [<replaceable>STYLE-OPTIONS</replaceable>]
778 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
779 * Bold 12</quote> or <quote>Monospace</quote>. See
780 * #ChimaraGlk:monospace-font-description.
783 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
785 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
786 g_return_if_fail(font || *font);
788 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
789 g_return_if_fail(fontdesc);
791 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
792 pango_font_description_free(priv->monospace_font_desc);
793 priv->monospace_font_desc = fontdesc;
795 /* TODO: Apply the font description to all the windows and recalculate the sizes */
799 * chimara_glk_get_monospace_font_description:
800 * @glk: a #ChimaraGlk widget
802 * Returns @glk's default monospace font.
804 * Return value: a newly-allocated #PangoFontDescription which must be freed
805 * using pango_font_description_free(), or %NULL on error.
807 PangoFontDescription *
808 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
810 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
812 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
813 return pango_font_description_copy(priv->monospace_font_desc);
817 * chimara_glk_set_spacing:
818 * @glk: a #ChimaraGlk widget
819 * @spacing: the number of pixels to put between Glk windows
821 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
822 * pixels between Glk windows.
825 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
827 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
829 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
830 priv->spacing = spacing;
834 * chimara_glk_get_spacing:
835 * @glk: a #ChimaraGlk widget
837 * Gets the value set by chimara_glk_set_spacing().
839 * Return value: pixels of spacing between Glk windows
842 chimara_glk_get_spacing(ChimaraGlk *glk)
844 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
846 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
847 return priv->spacing;
850 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
852 glk_enter(gpointer glk_main)
854 extern ChimaraGlkPrivate *glk_data;
855 g_signal_emit_by_name(glk_data->self, "started");
856 ((glk_main_t)glk_main)();
857 g_signal_emit_by_name(glk_data->self, "stopped");
863 * @glk: a #ChimaraGlk widget
864 * @plugin: path to a plugin module compiled with <filename
865 * class="header">glk.h</filename>
866 * @error: location to store a <link linkend="glib-GError">GError</link>, or
869 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
870 * a separate thread. On failure, returns %FALSE and sets @error.
872 * Return value: %TRUE if the Glk program was started successfully.
875 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
877 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
878 g_return_val_if_fail(plugin, FALSE);
880 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
882 /* Open the module to run */
884 g_assert( g_module_supported() );
885 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
889 g_warning( "Error opening module: %s", g_module_error() );
892 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
894 g_warning( "Error finding glk_main(): %s", g_module_error() );
898 extern ChimaraGlkPrivate *glk_data;
899 /* Set the thread's private data */
900 /* TODO: Do this with a GPrivate */
903 /* Run in a separate thread */
904 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
906 return !(priv->thread == NULL);
911 * @glk: a #ChimaraGlk widget
913 * Signals the Glk program running in @glk to abort. Note that if the program is
914 * caught in an infinite loop in which glk_tick() is not called, this may not
918 chimara_glk_stop(ChimaraGlk *glk)
920 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
921 /* TODO: check if glk is actually running a program */
927 * @glk: a #ChimaraGlk widget
929 * Holds up the main thread and waits for the Glk program running in @glk to
933 chimara_glk_wait(ChimaraGlk *glk)
935 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
937 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
938 g_thread_join(priv->thread);