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->rearranged = NULL;
88 priv->needs_rearrange = FALSE;
89 priv->ignore_next_arrange_event = FALSE;
90 priv->interrupt_handler = NULL;
91 priv->root_window = NULL;
92 priv->fileref_list = NULL;
93 priv->current_stream = NULL;
94 priv->stream_list = NULL;
99 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
101 ChimaraGlk *glk = CHIMARA_GLK(object);
105 case PROP_INTERACTIVE:
106 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
109 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
111 case PROP_DEFAULT_FONT_DESCRIPTION:
112 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
114 case PROP_MONOSPACE_FONT_DESCRIPTION:
115 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
118 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
121 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
126 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
128 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
132 case PROP_INTERACTIVE:
133 g_value_set_boolean(value, priv->interactive);
136 g_value_set_boolean(value, priv->protect);
138 case PROP_DEFAULT_FONT_DESCRIPTION:
139 g_value_set_pointer(value, priv->default_font_desc);
141 case PROP_MONOSPACE_FONT_DESCRIPTION:
142 g_value_set_pointer(value, priv->monospace_font_desc);
145 g_value_set_uint(value, priv->spacing);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
153 chimara_glk_finalize(GObject *object)
155 ChimaraGlk *self = CHIMARA_GLK(object);
156 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
158 /* Free the event queue */
159 g_mutex_lock(priv->event_lock);
160 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
161 g_queue_free(priv->event_queue);
162 g_cond_free(priv->event_queue_not_empty);
163 g_cond_free(priv->event_queue_not_full);
164 priv->event_queue = NULL;
165 g_mutex_unlock(priv->event_lock);
166 g_mutex_free(priv->event_lock);
168 /* Free the abort signalling mechanism */
169 g_mutex_lock(priv->abort_lock);
170 /* Make sure no other thread is busy with this */
171 g_mutex_unlock(priv->abort_lock);
172 g_mutex_free(priv->abort_lock);
173 priv->abort_lock = NULL;
175 /* Free the window arrangement signalling */
176 g_mutex_lock(priv->arrange_lock);
177 g_cond_free(priv->rearranged);
178 g_mutex_unlock(priv->arrange_lock);
179 g_mutex_free(priv->arrange_lock);
180 priv->arrange_lock = NULL;
182 /* Free private data */
183 pango_font_description_free(priv->default_font_desc);
184 pango_font_description_free(priv->monospace_font_desc);
186 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
189 /* Internal function: Recursively get the Glk window tree's size request */
191 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
193 if(win->type == wintype_Pair)
195 /* Get children's size requests */
196 GtkRequisition child1, child2;
197 request_recurse(win->window_node->children->data, &child1, spacing);
198 request_recurse(win->window_node->children->next->data, &child2, spacing);
200 /* If the split is fixed, get the size of the fixed child */
201 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
203 switch(win->split_method & winmethod_DirMask)
206 child1.width = win->key_window?
207 win->constraint_size * win->key_window->unit_width
210 case winmethod_Right:
211 child2.width = win->key_window?
212 win->constraint_size * win->key_window->unit_width
215 case winmethod_Above:
216 child1.height = win->key_window?
217 win->constraint_size * win->key_window->unit_height
220 case winmethod_Below:
221 child2.height = win->key_window?
222 win->constraint_size * win->key_window->unit_height
228 /* Add the children's requests */
229 switch(win->split_method & winmethod_DirMask)
232 case winmethod_Right:
233 requisition->width = child1.width + child2.width + spacing;
234 requisition->height = MAX(child1.height, child2.height);
236 case winmethod_Above:
237 case winmethod_Below:
238 requisition->width = MAX(child1.width, child2.width);
239 requisition->height = child1.height + child2.height + spacing;
244 /* For non-pair windows, just use the size that GTK requests */
246 gtk_widget_size_request(win->frame, requisition);
249 /* Overrides gtk_widget_size_request */
251 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
253 g_return_if_fail(widget);
254 g_return_if_fail(requisition);
255 g_return_if_fail(CHIMARA_IS_GLK(widget));
257 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
259 /* For now, just pass the size request on to the root Glk window */
260 if(priv->root_window)
262 request_recurse(priv->root_window->data, requisition, priv->spacing);
263 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
264 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
268 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
269 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
273 /* Recursively give the Glk windows their allocated space. Returns a window
274 containing all children of this window that must be redrawn, or NULL if there
275 are no children that require redrawing. */
277 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
279 if(win->type == wintype_Pair)
281 GtkAllocation child1, child2;
282 child1.x = allocation->x;
283 child1.y = allocation->y;
285 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
287 /* If the key window has been closed, then default to 0; otherwise
288 use the key window to determine the size */
289 switch(win->split_method & winmethod_DirMask)
292 child1.width = win->key_window?
293 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
296 case winmethod_Right:
297 child2.width = win->key_window?
298 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
301 case winmethod_Above:
302 child1.height = win->key_window?
303 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
306 case winmethod_Below:
307 child2.height = win->key_window?
308 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
313 else /* proportional */
315 gdouble fraction = win->constraint_size / 100.0;
316 switch(win->split_method & winmethod_DirMask)
319 child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
321 case winmethod_Right:
322 child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
324 case winmethod_Above:
325 child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
327 case winmethod_Below:
328 child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
333 /* Fill in the rest of the size requisitions according to the child specified above */
334 switch(win->split_method & winmethod_DirMask)
337 child2.width = allocation->width - spacing - child1.width;
338 child2.x = child1.x + child1.width + spacing;
340 child1.height = child2.height = allocation->height;
342 case winmethod_Right:
343 child1.width = allocation->width - spacing - child2.width;
344 child2.x = child1.x + child1.width + spacing;
346 child1.height = child2.height = allocation->height;
348 case winmethod_Above:
349 child2.height = allocation->height - spacing - child1.height;
351 child2.y = child1.y + child1.height + spacing;
352 child1.width = child2.width = allocation->width;
354 case winmethod_Below:
355 child1.height = allocation->height - spacing - child2.height;
357 child2.y = child1.y + child1.height + spacing;
358 child1.width = child2.width = allocation->width;
363 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
364 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
372 else if(win->type == wintype_TextGrid)
374 /* Pass the size allocation on to the framing widget */
375 gtk_widget_size_allocate(win->frame, allocation);
376 /* It says in the spec that when a text grid window is resized smaller,
377 the bottom or right area is thrown away; when it is resized larger, the
378 bottom or right area is filled with blanks. */
379 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
380 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
382 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
383 GtkTextIter start, end;
385 for(line = 0; line < win->height; line++)
387 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
388 /* If this line is going to fall off the bottom, delete it */
389 if(line >= newheight)
392 gtk_text_iter_forward_to_line_end(&end);
393 gtk_text_iter_forward_char(&end);
394 gtk_text_buffer_delete(textbuffer, &start, &end);
397 /* If this line is not long enough, add spaces on the end */
398 if(newwidth > win->width)
400 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
401 gtk_text_iter_forward_to_line_end(&start);
402 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
405 /* But if it's too long, delete characters from the end */
406 else if(newwidth < win->width)
409 gtk_text_iter_forward_chars(&start, newwidth);
410 gtk_text_iter_forward_to_line_end(&end);
411 gtk_text_buffer_delete(textbuffer, &start, &end);
413 /* Note: if the widths are equal, do nothing */
415 /* Add blank lines if there aren't enough lines to fit the new size */
416 if(newheight > win->height)
418 gchar *blanks = g_strnfill(win->width, ' ');
419 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
421 for(count = 0; count < newheight - win->height; count++)
422 blanklines[count] = blanks;
423 blanklines[newheight - win->height] = NULL;
424 gchar *text = g_strjoinv("\n", blanklines);
425 g_free(blanklines); /* not g_strfreev() */
428 gtk_text_buffer_get_end_iter(textbuffer, &start);
429 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
430 gtk_text_buffer_insert(textbuffer, &start, text, -1);
434 gboolean arrange = !(win->width == newwidth && win->height == newheight);
435 win->width = newwidth;
436 win->height = newheight;
437 return arrange? win : NULL;
440 /* For non-pair, non-text-grid windows, just give them the size */
441 gtk_widget_size_allocate(win->frame, allocation);
445 /* Overrides gtk_widget_size_allocate */
447 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
449 g_return_if_fail(widget);
450 g_return_if_fail(allocation);
451 g_return_if_fail(CHIMARA_IS_GLK(widget));
453 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
455 widget->allocation = *allocation;
457 if(priv->root_window) {
459 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
460 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
461 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
462 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
463 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
465 /* arrange points to a window that contains all text grid and graphics
466 windows which have been resized */
467 g_mutex_lock(priv->arrange_lock);
468 if(!priv->ignore_next_arrange_event)
471 event_throw(evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
474 priv->ignore_next_arrange_event = FALSE;
475 priv->needs_rearrange = FALSE;
476 g_cond_signal(priv->rearranged);
477 g_mutex_unlock(priv->arrange_lock);
481 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
483 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
485 if(win->type == wintype_Pair)
487 forall_recurse(win->window_node->children->data, callback, callback_data);
488 forall_recurse(win->window_node->children->next->data, callback, callback_data);
491 (*callback)(win->frame, callback_data);
494 /* Overrides gtk_container_forall */
496 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
498 g_return_if_fail(container);
499 g_return_if_fail(CHIMARA_IS_GLK(container));
501 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
503 /* All the children are "internal" */
504 if(!include_internals)
507 if(priv->root_window)
508 forall_recurse(priv->root_window->data, callback, callback_data);
512 chimara_glk_stopped(ChimaraGlk *self)
514 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
516 /* Free the plugin */
517 if( priv->program && !g_module_close(priv->program) )
518 g_warning( "Error closing module: %s", g_module_error() );
522 chimara_glk_started(ChimaraGlk *self)
524 /* TODO: Add default signal handler implementation here */
527 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
528 #ifndef G_PARAM_STATIC_STRINGS
529 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
533 chimara_glk_class_init(ChimaraGlkClass *klass)
535 /* Override methods of parent classes */
536 GObjectClass *object_class = G_OBJECT_CLASS(klass);
537 object_class->set_property = chimara_glk_set_property;
538 object_class->get_property = chimara_glk_get_property;
539 object_class->finalize = chimara_glk_finalize;
541 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
542 widget_class->size_request = chimara_glk_size_request;
543 widget_class->size_allocate = chimara_glk_size_allocate;
545 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
546 container_class->forall = chimara_glk_forall;
549 klass->stopped = chimara_glk_stopped;
550 klass->started = chimara_glk_started;
552 * ChimaraGlk::stopped:
553 * @glk: The widget that received the signal
555 * The ::stopped signal is emitted when the a Glk program finishes
556 * executing in the widget, whether it ended normally, or was interrupted.
558 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
559 G_OBJECT_CLASS_TYPE(klass), 0,
560 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
561 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
563 * ChimaraGlk::started:
564 * @glk: The widget that received the signal
566 * The ::started signal is emitted when a Glk program starts executing in
569 chimara_glk_signals[STARTED] = g_signal_new ("started",
570 G_OBJECT_CLASS_TYPE (klass), 0,
571 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
572 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
576 * ChimaraGlk:interactive:
578 * Sets whether the widget is interactive. A Glk widget is normally
579 * interactive, but in non-interactive mode, keyboard and mouse input are
580 * ignored and the Glk program is controlled by chimara_glk_feed_text().
581 * <quote>More</quote> prompts when a lot of text is printed to a text
582 * buffer are also disabled. This is typically used when you wish to control
583 * an interpreter program by feeding it a predefined list of commands.
585 g_object_class_install_property( object_class, PROP_INTERACTIVE,
586 g_param_spec_boolean("interactive", _("Interactive"),
587 _("Whether user input is expected in the Glk program"),
589 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
592 * ChimaraGlk:protect:
594 * Sets whether the Glk program is allowed to do file operations. In protect
595 * mode, all file operations will fail.
597 g_object_class_install_property(object_class, PROP_PROTECT,
598 g_param_spec_boolean("protect", _("Protected"),
599 _("Whether the Glk program is barred from doing file operations"),
601 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
603 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
604 initialize them with NULL */
606 * ChimaraGlk:default-font-description:
608 * Pointer to a #PangoFontDescription describing the default proportional
609 * font, to be used in text buffer windows for example.
611 * Default value: font description created from the string
612 * <quote>Sans</quote>
614 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
615 g_param_spec_pointer("default-font-description", _("Default Font"),
616 _("Font description of the default proportional font"),
617 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
620 * ChimaraGlk:monospace-font-description:
622 * Pointer to a #PangoFontDescription describing the default monospace font,
623 * to be used in text grid windows and #style_Preformatted, for example.
625 * Default value: font description created from the string
626 * <quote>Monospace</quote>
628 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
629 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
630 _("Font description of the default monospace font"),
631 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
634 * ChimaraGlk:spacing:
636 * The amount of space between the Glk windows.
638 g_object_class_install_property(object_class, PROP_SPACING,
639 g_param_spec_uint("spacing", _("Spacing"),
640 _("The amount of space between Glk windows"),
642 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
645 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
648 /* PUBLIC FUNCTIONS */
653 * Creates and initializes a new #ChimaraGlk widget.
655 * Return value: a #ChimaraGlk widget, with a floating reference.
658 chimara_glk_new(void)
660 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
661 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
663 priv->event_queue = g_queue_new();
664 priv->event_lock = g_mutex_new();
665 priv->event_queue_not_empty = g_cond_new();
666 priv->event_queue_not_full = g_cond_new();
667 priv->abort_lock = g_mutex_new();
668 priv->arrange_lock = g_mutex_new();
669 priv->rearranged = g_cond_new();
671 return GTK_WIDGET(self);
675 * chimara_glk_set_interactive:
676 * @glk: a #ChimaraGlk widget
677 * @interactive: whether the widget should expect user input
679 * Sets the #ChimaraGlk:interactive property of @glk.
682 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
684 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
686 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
687 priv->interactive = interactive;
691 * chimara_glk_get_interactive:
692 * @glk: a #ChimaraGlk widget
694 * Returns whether @glk is interactive (expecting user input). See
695 * #ChimaraGlk:interactive.
697 * Return value: %TRUE if @glk is interactive.
700 chimara_glk_get_interactive(ChimaraGlk *glk)
702 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
704 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
705 return priv->interactive;
709 * chimara_glk_set_protect:
710 * @glk: a #ChimaraGlk widget
711 * @protect: whether the widget should allow the Glk program to do file
714 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
715 * program is not allowed to do file operations.
718 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
720 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
722 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
723 priv->protect = protect;
727 * chimara_glk_get_protect:
728 * @glk: a #ChimaraGlk widget
730 * Returns whether @glk is in protect mode (banned from doing file operations).
731 * See #ChimaraGlk:protect.
733 * Return value: %TRUE if @glk is in protect mode.
736 chimara_glk_get_protect(ChimaraGlk *glk)
738 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
740 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
741 return priv->protect;
745 * chimara_glk_set_default_font_description:
746 * @glk: a #ChimaraGlk widget
747 * @font: a #PangoFontDescription
749 * Sets @glk's default proportional font. See
750 * #ChimaraGlk:default-font-description.
753 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
755 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
756 g_return_if_fail(font);
758 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
759 pango_font_description_free(priv->default_font_desc);
760 priv->default_font_desc = pango_font_description_copy(font);
762 /* TODO: Apply the font description to all the windows and recalculate the sizes */
766 * chimara_glk_set_default_font_string:
767 * @glk: a #ChimaraGlk widget
768 * @font: string representation of a font description
770 * Sets @glk's default proportional font according to the string @font, which
771 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
772 * [<replaceable>STYLE-OPTIONS</replaceable>]
773 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
774 * Italic 12</quote> or <quote>Sans</quote>. See
775 * #ChimaraGlk:default-font-description.
778 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
780 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
781 g_return_if_fail(font || *font);
783 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
784 g_return_if_fail(fontdesc);
786 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
787 pango_font_description_free(priv->default_font_desc);
788 priv->default_font_desc = fontdesc;
790 /* TODO: Apply the font description to all the windows and recalculate the sizes */
794 * chimara_glk_get_default_font_description:
795 * @glk: a #ChimaraGlk widget
797 * Returns @glk's default proportional font.
799 * Return value: a newly-allocated #PangoFontDescription which must be freed
800 * using pango_font_description_free(), or %NULL on error.
802 PangoFontDescription *
803 chimara_glk_get_default_font_description(ChimaraGlk *glk)
805 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
807 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
808 return pango_font_description_copy(priv->default_font_desc);
812 * chimara_glk_set_monospace_font_description:
813 * @glk: a #ChimaraGlk widget
814 * @font: a #PangoFontDescription
816 * Sets @glk's default monospace font. See
817 * #ChimaraGlk:monospace-font-description.
820 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
822 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
823 g_return_if_fail(font);
825 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
826 pango_font_description_free(priv->monospace_font_desc);
827 priv->monospace_font_desc = pango_font_description_copy(font);
829 /* TODO: Apply the font description to all the windows and recalculate the sizes */
833 * chimara_glk_set_monospace_font_string:
834 * @glk: a #ChimaraGlk widget
835 * @font: string representation of a font description
837 * Sets @glk's default monospace font according to the string @font, which must
838 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
839 * [<replaceable>STYLE-OPTIONS</replaceable>]
840 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
841 * Bold 12</quote> or <quote>Monospace</quote>. See
842 * #ChimaraGlk:monospace-font-description.
845 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
847 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
848 g_return_if_fail(font || *font);
850 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
851 g_return_if_fail(fontdesc);
853 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
854 pango_font_description_free(priv->monospace_font_desc);
855 priv->monospace_font_desc = fontdesc;
857 /* TODO: Apply the font description to all the windows and recalculate the sizes */
861 * chimara_glk_get_monospace_font_description:
862 * @glk: a #ChimaraGlk widget
864 * Returns @glk's default monospace font.
866 * Return value: a newly-allocated #PangoFontDescription which must be freed
867 * using pango_font_description_free(), or %NULL on error.
869 PangoFontDescription *
870 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
872 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
874 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
875 return pango_font_description_copy(priv->monospace_font_desc);
879 * chimara_glk_set_spacing:
880 * @glk: a #ChimaraGlk widget
881 * @spacing: the number of pixels to put between Glk windows
883 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
884 * pixels between Glk windows.
887 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
889 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
891 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
892 priv->spacing = spacing;
896 * chimara_glk_get_spacing:
897 * @glk: a #ChimaraGlk widget
899 * Gets the value set by chimara_glk_set_spacing().
901 * Return value: pixels of spacing between Glk windows
904 chimara_glk_get_spacing(ChimaraGlk *glk)
906 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
908 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
909 return priv->spacing;
912 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
914 glk_enter(gpointer glk_main)
916 extern ChimaraGlkPrivate *glk_data;
918 g_signal_emit_by_name(glk_data->self, "started");
919 ((glk_main_t)glk_main)();
920 g_signal_emit_by_name(glk_data->self, "stopped");
926 * @glk: a #ChimaraGlk widget
927 * @plugin: path to a plugin module compiled with <filename
928 * class="header">glk.h</filename>
929 * @error: location to store a <link linkend="glib-GError">GError</link>, or
932 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
933 * a separate thread. On failure, returns %FALSE and sets @error.
935 * Return value: %TRUE if the Glk program was started successfully.
938 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
940 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
941 g_return_val_if_fail(plugin, FALSE);
943 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
946 /* Open the module to run */
948 glkunix_startup_code_t glkunix_startup_code;
949 g_assert( g_module_supported() );
950 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
954 g_warning( "Error opening module: %s", g_module_error() );
957 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
959 g_warning( "Error finding glk_main(): %s", g_module_error() );
963 extern ChimaraGlkPrivate *glk_data;
964 /* Set the thread's private data */
965 /* TODO: Do this with a GPrivate */
968 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &glkunix_startup_code) )
970 glkunix_startup_t data;
974 glkunix_startup_code(&data);
977 /* Run in a separate thread */
978 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
980 return !(priv->thread == NULL);
985 * @glk: a #ChimaraGlk widget
987 * Signals the Glk program running in @glk to abort. Note that if the program is
988 * caught in an infinite loop in which glk_tick() is not called, this may not
992 chimara_glk_stop(ChimaraGlk *glk)
994 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
995 /* TODO: check if glk is actually running a program */
1001 * @glk: a #ChimaraGlk widget
1003 * Holds up the main thread and waits for the Glk program running in @glk to
1007 chimara_glk_wait(ChimaraGlk *glk)
1009 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1011 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1012 g_thread_join(priv->thread);