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->arrange_lock = NULL;
87 priv->ignore_next_arrange_event = FALSE;
88 priv->interrupt_handler = NULL;
89 priv->root_window = NULL;
90 priv->fileref_list = NULL;
91 priv->current_stream = NULL;
92 priv->stream_list = NULL;
97 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
99 ChimaraGlk *glk = CHIMARA_GLK(object);
103 case PROP_INTERACTIVE:
104 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
107 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
109 case PROP_DEFAULT_FONT_DESCRIPTION:
110 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
112 case PROP_MONOSPACE_FONT_DESCRIPTION:
113 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
116 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
119 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
124 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
126 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
130 case PROP_INTERACTIVE:
131 g_value_set_boolean(value, priv->interactive);
134 g_value_set_boolean(value, priv->protect);
136 case PROP_DEFAULT_FONT_DESCRIPTION:
137 g_value_set_pointer(value, priv->default_font_desc);
139 case PROP_MONOSPACE_FONT_DESCRIPTION:
140 g_value_set_pointer(value, priv->monospace_font_desc);
143 g_value_set_uint(value, priv->spacing);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
151 chimara_glk_finalize(GObject *object)
153 ChimaraGlk *self = CHIMARA_GLK(object);
154 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
156 /* Free the event queue */
157 g_mutex_lock(priv->event_lock);
158 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
159 g_queue_free(priv->event_queue);
160 g_cond_free(priv->event_queue_not_empty);
161 g_cond_free(priv->event_queue_not_full);
162 priv->event_queue = NULL;
163 g_mutex_unlock(priv->event_lock);
164 g_mutex_free(priv->event_lock);
166 /* Free the abort signalling mechanism */
167 g_mutex_lock(priv->abort_lock);
168 /* Make sure no other thread is busy with this */
169 g_mutex_unlock(priv->abort_lock);
170 g_mutex_free(priv->abort_lock);
171 priv->abort_lock = NULL;
173 /* Free the window arrangement signalling */
174 g_mutex_lock(priv->arrange_lock);
175 /* Make sure no other thread is busy with this */
176 g_mutex_unlock(priv->arrange_lock);
177 g_mutex_free(priv->arrange_lock);
178 priv->arrange_lock = NULL;
180 /* Free private data */
181 pango_font_description_free(priv->default_font_desc);
182 pango_font_description_free(priv->monospace_font_desc);
184 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
187 /* Internal function: Recursively get the Glk window tree's size request */
189 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
191 if(win->type == wintype_Pair)
193 /* Get children's size requests */
194 GtkRequisition child1, child2;
195 request_recurse(win->window_node->children->data, &child1, spacing);
196 request_recurse(win->window_node->children->next->data, &child2, spacing);
198 /* If the split is fixed, get the size of the fixed child */
199 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
201 switch(win->split_method & winmethod_DirMask)
204 child1.width = win->key_window?
205 win->constraint_size * win->key_window->unit_width
208 case winmethod_Right:
209 child2.width = win->key_window?
210 win->constraint_size * win->key_window->unit_width
213 case winmethod_Above:
214 child1.height = win->key_window?
215 win->constraint_size * win->key_window->unit_height
218 case winmethod_Below:
219 child2.height = win->key_window?
220 win->constraint_size * win->key_window->unit_height
226 /* Add the children's requests */
227 switch(win->split_method & winmethod_DirMask)
230 case winmethod_Right:
231 requisition->width = child1.width + child2.width + spacing;
232 requisition->height = MAX(child1.height, child2.height);
234 case winmethod_Above:
235 case winmethod_Below:
236 requisition->width = MAX(child1.width, child2.width);
237 requisition->height = child1.height + child2.height + spacing;
242 /* For non-pair windows, just use the size that GTK requests */
244 gtk_widget_size_request(win->frame, requisition);
247 /* Overrides gtk_widget_size_request */
249 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
251 g_return_if_fail(widget);
252 g_return_if_fail(requisition);
253 g_return_if_fail(CHIMARA_IS_GLK(widget));
255 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
257 /* For now, just pass the size request on to the root Glk window */
258 if(priv->root_window)
260 request_recurse(priv->root_window->data, requisition, priv->spacing);
261 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
262 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
266 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
267 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
271 /* Recursively give the Glk windows their allocated space. Returns a window
272 containing all children of this window that must be redrawn, or NULL if there
273 are no children that require redrawing. */
275 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
277 if(win->type == wintype_Pair)
279 GtkAllocation child1, child2;
280 child1.x = allocation->x;
281 child1.y = allocation->y;
283 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
285 /* If the key window has been closed, then default to 0; otherwise
286 use the key window to determine the size */
287 switch(win->split_method & winmethod_DirMask)
290 child1.width = win->key_window?
291 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
294 case winmethod_Right:
295 child2.width = win->key_window?
296 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
299 case winmethod_Above:
300 child1.height = win->key_window?
301 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
304 case winmethod_Below:
305 child2.height = win->key_window?
306 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
311 else /* proportional */
313 gdouble fraction = win->constraint_size / 100.0;
314 switch(win->split_method & winmethod_DirMask)
317 child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
319 case winmethod_Right:
320 child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
322 case winmethod_Above:
323 child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
325 case winmethod_Below:
326 child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
331 /* Fill in the rest of the size requisitions according to the child specified above */
332 switch(win->split_method & winmethod_DirMask)
335 child2.width = allocation->width - spacing - child1.width;
336 child2.x = child1.x + child1.width + spacing;
338 child1.height = child2.height = allocation->height;
340 case winmethod_Right:
341 child1.width = allocation->width - spacing - child2.width;
342 child2.x = child1.x + child1.width + spacing;
344 child1.height = child2.height = allocation->height;
346 case winmethod_Above:
347 child2.height = allocation->height - spacing - child1.height;
349 child2.y = child1.y + child1.height + spacing;
350 child1.width = child2.width = allocation->width;
352 case winmethod_Below:
353 child1.height = allocation->height - spacing - child2.height;
355 child2.y = child1.y + child1.height + spacing;
356 child1.width = child2.width = allocation->width;
361 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
362 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
370 else if(win->type == wintype_TextGrid)
372 /* Pass the size allocation on to the framing widget */
373 gtk_widget_size_allocate(win->frame, allocation);
374 /* It says in the spec that when a text grid window is resized smaller,
375 the bottom or right area is thrown away; when it is resized larger, the
376 bottom or right area is filled with blanks. */
377 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
378 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
380 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
381 GtkTextIter start, end;
383 for(line = 0; line < win->height; line++)
385 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
386 /* If this line is going to fall off the bottom, delete it */
387 if(line >= newheight)
390 gtk_text_iter_forward_to_line_end(&end);
391 gtk_text_iter_forward_char(&end);
392 gtk_text_buffer_delete(textbuffer, &start, &end);
395 /* If this line is not long enough, add spaces on the end */
396 if(newwidth > win->width)
398 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
399 gtk_text_iter_forward_to_line_end(&start);
400 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
403 /* But if it's too long, delete characters from the end */
404 else if(newwidth < win->width)
407 gtk_text_iter_forward_chars(&start, newwidth);
408 gtk_text_iter_forward_to_line_end(&end);
409 gtk_text_buffer_delete(textbuffer, &start, &end);
411 /* Note: if the widths are equal, do nothing */
413 /* Add blank lines if there aren't enough lines to fit the new size */
414 if(newheight > win->height)
416 gchar *blanks = g_strnfill(win->width, ' ');
417 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
419 for(count = 0; count < newheight - win->height; count++)
420 blanklines[count] = blanks;
421 blanklines[newheight - win->height] = NULL;
422 gchar *text = g_strjoinv("\n", blanklines);
423 g_free(blanklines); /* not g_strfreev() */
426 gtk_text_buffer_get_end_iter(textbuffer, &start);
427 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
428 gtk_text_buffer_insert(textbuffer, &start, text, -1);
432 gboolean arrange = !(win->width == newwidth && win->height == newheight);
433 win->width = newwidth;
434 win->height = newheight;
435 return arrange? win : NULL;
438 /* For non-pair, non-text-grid windows, just give them the size */
439 gtk_widget_size_allocate(win->frame, allocation);
443 /* Overrides gtk_widget_size_allocate */
445 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
447 g_return_if_fail(widget);
448 g_return_if_fail(allocation);
449 g_return_if_fail(CHIMARA_IS_GLK(widget));
451 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
453 widget->allocation = *allocation;
455 if(priv->root_window) {
457 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
458 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
459 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
460 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
461 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
463 /* arrange points to a window that contains all text grid and graphics
464 windows which have been resized */
465 g_mutex_lock(priv->arrange_lock);
466 if(!priv->ignore_next_arrange_event)
469 event_throw(evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
472 priv->ignore_next_arrange_event = FALSE;
473 g_mutex_unlock(priv->arrange_lock);
477 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
479 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
481 if(win->type == wintype_Pair)
483 forall_recurse(win->window_node->children->data, callback, callback_data);
484 forall_recurse(win->window_node->children->next->data, callback, callback_data);
487 (*callback)(win->frame, callback_data);
490 /* Overrides gtk_container_forall */
492 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
494 g_return_if_fail(container);
495 g_return_if_fail(CHIMARA_IS_GLK(container));
497 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
499 /* All the children are "internal" */
500 if(!include_internals)
503 if(priv->root_window)
504 forall_recurse(priv->root_window->data, callback, callback_data);
508 chimara_glk_stopped(ChimaraGlk *self)
510 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
512 /* Free the plugin */
513 if( priv->program && !g_module_close(priv->program) )
514 g_warning( "Error closing module: %s", g_module_error() );
518 chimara_glk_started(ChimaraGlk *self)
520 /* TODO: Add default signal handler implementation here */
523 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
524 #ifndef G_PARAM_STATIC_STRINGS
525 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
529 chimara_glk_class_init(ChimaraGlkClass *klass)
531 /* Override methods of parent classes */
532 GObjectClass *object_class = G_OBJECT_CLASS(klass);
533 object_class->set_property = chimara_glk_set_property;
534 object_class->get_property = chimara_glk_get_property;
535 object_class->finalize = chimara_glk_finalize;
537 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
538 widget_class->size_request = chimara_glk_size_request;
539 widget_class->size_allocate = chimara_glk_size_allocate;
541 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
542 container_class->forall = chimara_glk_forall;
545 klass->stopped = chimara_glk_stopped;
546 klass->started = chimara_glk_started;
548 * ChimaraGlk::stopped:
549 * @glk: The widget that received the signal
551 * The ::stopped signal is emitted when the a Glk program finishes
552 * executing in the widget, whether it ended normally, or was interrupted.
554 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
555 G_OBJECT_CLASS_TYPE(klass), 0,
556 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
557 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
559 * ChimaraGlk::started:
560 * @glk: The widget that received the signal
562 * The ::started signal is emitted when a Glk program starts executing in
565 chimara_glk_signals[STARTED] = g_signal_new ("started",
566 G_OBJECT_CLASS_TYPE (klass), 0,
567 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
568 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
572 * ChimaraGlk:interactive:
574 * Sets whether the widget is interactive. A Glk widget is normally
575 * interactive, but in non-interactive mode, keyboard and mouse input are
576 * ignored and the Glk program is controlled by chimara_glk_feed_text().
577 * <quote>More</quote> prompts when a lot of text is printed to a text
578 * buffer are also disabled. This is typically used when you wish to control
579 * an interpreter program by feeding it a predefined list of commands.
581 g_object_class_install_property( object_class, PROP_INTERACTIVE,
582 g_param_spec_boolean("interactive", _("Interactive"),
583 _("Whether user input is expected in the Glk program"),
585 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
588 * ChimaraGlk:protect:
590 * Sets whether the Glk program is allowed to do file operations. In protect
591 * mode, all file operations will fail.
593 g_object_class_install_property(object_class, PROP_PROTECT,
594 g_param_spec_boolean("protect", _("Protected"),
595 _("Whether the Glk program is barred from doing file operations"),
597 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
599 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
600 initialize them with NULL */
602 * ChimaraGlk:default-font-description:
604 * Pointer to a #PangoFontDescription describing the default proportional
605 * font, to be used in text buffer windows for example.
607 * Default value: font description created from the string
608 * <quote>Sans</quote>
610 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
611 g_param_spec_pointer("default-font-description", _("Default Font"),
612 _("Font description of the default proportional font"),
613 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
616 * ChimaraGlk:monospace-font-description:
618 * Pointer to a #PangoFontDescription describing the default monospace font,
619 * to be used in text grid windows and #style_Preformatted, for example.
621 * Default value: font description created from the string
622 * <quote>Monospace</quote>
624 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
625 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
626 _("Font description of the default monospace font"),
627 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
630 * ChimaraGlk:spacing:
632 * The amount of space between the Glk windows.
634 g_object_class_install_property(object_class, PROP_SPACING,
635 g_param_spec_uint("spacing", _("Spacing"),
636 _("The amount of space between Glk windows"),
638 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
641 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
644 /* PUBLIC FUNCTIONS */
649 * Creates and initializes a new #ChimaraGlk widget.
651 * Return value: a #ChimaraGlk widget, with a floating reference.
654 chimara_glk_new(void)
656 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
657 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
659 priv->event_queue = g_queue_new();
660 priv->event_lock = g_mutex_new();
661 priv->event_queue_not_empty = g_cond_new();
662 priv->event_queue_not_full = g_cond_new();
663 priv->abort_lock = g_mutex_new();
664 priv->arrange_lock = g_mutex_new();
666 return GTK_WIDGET(self);
670 * chimara_glk_set_interactive:
671 * @glk: a #ChimaraGlk widget
672 * @interactive: whether the widget should expect user input
674 * Sets the #ChimaraGlk:interactive property of @glk.
677 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
679 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
681 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
682 priv->interactive = interactive;
686 * chimara_glk_get_interactive:
687 * @glk: a #ChimaraGlk widget
689 * Returns whether @glk is interactive (expecting user input). See
690 * #ChimaraGlk:interactive.
692 * Return value: %TRUE if @glk is interactive.
695 chimara_glk_get_interactive(ChimaraGlk *glk)
697 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
699 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
700 return priv->interactive;
704 * chimara_glk_set_protect:
705 * @glk: a #ChimaraGlk widget
706 * @protect: whether the widget should allow the Glk program to do file
709 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
710 * program is not allowed to do file operations.
713 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
715 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
717 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
718 priv->protect = protect;
722 * chimara_glk_get_protect:
723 * @glk: a #ChimaraGlk widget
725 * Returns whether @glk is in protect mode (banned from doing file operations).
726 * See #ChimaraGlk:protect.
728 * Return value: %TRUE if @glk is in protect mode.
731 chimara_glk_get_protect(ChimaraGlk *glk)
733 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
735 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
736 return priv->protect;
740 * chimara_glk_set_default_font_description:
741 * @glk: a #ChimaraGlk widget
742 * @font: a #PangoFontDescription
744 * Sets @glk's default proportional font. See
745 * #ChimaraGlk:default-font-description.
748 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
750 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
751 g_return_if_fail(font);
753 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
754 pango_font_description_free(priv->default_font_desc);
755 priv->default_font_desc = pango_font_description_copy(font);
757 /* TODO: Apply the font description to all the windows and recalculate the sizes */
761 * chimara_glk_set_default_font_string:
762 * @glk: a #ChimaraGlk widget
763 * @font: string representation of a font description
765 * Sets @glk's default proportional font according to the string @font, which
766 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
767 * [<replaceable>STYLE-OPTIONS</replaceable>]
768 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
769 * Italic 12</quote> or <quote>Sans</quote>. See
770 * #ChimaraGlk:default-font-description.
773 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
775 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
776 g_return_if_fail(font || *font);
778 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
779 g_return_if_fail(fontdesc);
781 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
782 pango_font_description_free(priv->default_font_desc);
783 priv->default_font_desc = fontdesc;
785 /* TODO: Apply the font description to all the windows and recalculate the sizes */
789 * chimara_glk_get_default_font_description:
790 * @glk: a #ChimaraGlk widget
792 * Returns @glk's default proportional font.
794 * Return value: a newly-allocated #PangoFontDescription which must be freed
795 * using pango_font_description_free(), or %NULL on error.
797 PangoFontDescription *
798 chimara_glk_get_default_font_description(ChimaraGlk *glk)
800 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
802 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
803 return pango_font_description_copy(priv->default_font_desc);
807 * chimara_glk_set_monospace_font_description:
808 * @glk: a #ChimaraGlk widget
809 * @font: a #PangoFontDescription
811 * Sets @glk's default monospace font. See
812 * #ChimaraGlk:monospace-font-description.
815 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
817 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
818 g_return_if_fail(font);
820 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
821 pango_font_description_free(priv->monospace_font_desc);
822 priv->monospace_font_desc = pango_font_description_copy(font);
824 /* TODO: Apply the font description to all the windows and recalculate the sizes */
828 * chimara_glk_set_monospace_font_string:
829 * @glk: a #ChimaraGlk widget
830 * @font: string representation of a font description
832 * Sets @glk's default monospace font according to the string @font, which must
833 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
834 * [<replaceable>STYLE-OPTIONS</replaceable>]
835 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
836 * Bold 12</quote> or <quote>Monospace</quote>. See
837 * #ChimaraGlk:monospace-font-description.
840 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
842 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
843 g_return_if_fail(font || *font);
845 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
846 g_return_if_fail(fontdesc);
848 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
849 pango_font_description_free(priv->monospace_font_desc);
850 priv->monospace_font_desc = fontdesc;
852 /* TODO: Apply the font description to all the windows and recalculate the sizes */
856 * chimara_glk_get_monospace_font_description:
857 * @glk: a #ChimaraGlk widget
859 * Returns @glk's default monospace font.
861 * Return value: a newly-allocated #PangoFontDescription which must be freed
862 * using pango_font_description_free(), or %NULL on error.
864 PangoFontDescription *
865 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
867 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
869 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
870 return pango_font_description_copy(priv->monospace_font_desc);
874 * chimara_glk_set_spacing:
875 * @glk: a #ChimaraGlk widget
876 * @spacing: the number of pixels to put between Glk windows
878 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
879 * pixels between Glk windows.
882 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
884 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
886 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
887 priv->spacing = spacing;
891 * chimara_glk_get_spacing:
892 * @glk: a #ChimaraGlk widget
894 * Gets the value set by chimara_glk_set_spacing().
896 * Return value: pixels of spacing between Glk windows
899 chimara_glk_get_spacing(ChimaraGlk *glk)
901 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
903 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
904 return priv->spacing;
907 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
909 glk_enter(gpointer glk_main)
911 extern ChimaraGlkPrivate *glk_data;
913 g_signal_emit_by_name(glk_data->self, "started");
914 ((glk_main_t)glk_main)();
915 g_signal_emit_by_name(glk_data->self, "stopped");
921 * @glk: a #ChimaraGlk widget
922 * @plugin: path to a plugin module compiled with <filename
923 * class="header">glk.h</filename>
924 * @error: location to store a <link linkend="glib-GError">GError</link>, or
927 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
928 * a separate thread. On failure, returns %FALSE and sets @error.
930 * Return value: %TRUE if the Glk program was started successfully.
933 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
935 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
936 g_return_val_if_fail(plugin, FALSE);
938 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
941 /* Open the module to run */
943 glkunix_startup_code_t glkunix_startup_code;
944 g_assert( g_module_supported() );
945 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
949 g_warning( "Error opening module: %s", g_module_error() );
952 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
954 g_warning( "Error finding glk_main(): %s", g_module_error() );
958 extern ChimaraGlkPrivate *glk_data;
959 /* Set the thread's private data */
960 /* TODO: Do this with a GPrivate */
963 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &glkunix_startup_code) )
965 glkunix_startup_t data;
969 glkunix_startup_code(&data);
972 /* Run in a separate thread */
973 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
975 return !(priv->thread == NULL);
980 * @glk: a #ChimaraGlk widget
982 * Signals the Glk program running in @glk to abort. Note that if the program is
983 * caught in an infinite loop in which glk_tick() is not called, this may not
987 chimara_glk_stop(ChimaraGlk *glk)
989 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
990 /* TODO: check if glk is actually running a program */
996 * @glk: a #ChimaraGlk widget
998 * Holds up the main thread and waits for the Glk program running in @glk to
1002 chimara_glk_wait(ChimaraGlk *glk)
1004 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1006 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1007 g_thread_join(priv->thread);