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"
16 #define CHIMARA_GLK_MIN_WIDTH 0
17 #define CHIMARA_GLK_MIN_HEIGHT 0
21 * @short_description: Widget which executes a Glk program
22 * @stability: Unstable
23 * @include: chimara/chimara-glk.h
25 * The ChimaraGlk widget opens and runs a Glk program. The program must be
26 * compiled as a plugin module, with a function <function>glk_main()</function>
27 * that the Glk library can hook into.
29 * On Linux systems, this is a file with a name like
30 * <filename>plugin.so</filename>. For portability, you can use libtool and
33 * pkglib_LTLIBRARIES = plugin.la
34 * plugin_la_SOURCES = plugin.c foo.c bar.c
35 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
37 * This will produce <filename>plugin.la</filename> which is a text file
38 * containing the correct plugin file to open (see the relevant section of the
40 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
41 * Libtool manual</ulink>).
44 typedef void (* glk_main_t) (void);
45 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
51 PROP_DEFAULT_FONT_DESCRIPTION,
52 PROP_MONOSPACE_FONT_DESCRIPTION,
63 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
65 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
68 chimara_glk_init(ChimaraGlk *self)
70 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
72 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
75 priv->interactive = TRUE;
76 priv->protect = FALSE;
77 priv->default_font_desc = pango_font_description_from_string("Sans");
78 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
81 priv->event_queue = NULL;
82 priv->event_lock = NULL;
83 priv->event_queue_not_empty = NULL;
84 priv->event_queue_not_full = NULL;
85 priv->abort_lock = NULL;
86 priv->abort_signalled = FALSE;
87 priv->arrange_lock = NULL;
88 priv->rearranged = NULL;
89 priv->needs_rearrange = FALSE;
90 priv->ignore_next_arrange_event = FALSE;
91 priv->interrupt_handler = NULL;
92 priv->root_window = NULL;
93 priv->fileref_list = NULL;
94 priv->current_stream = NULL;
95 priv->stream_list = NULL;
97 priv->in_startup = FALSE;
98 priv->current_dir = NULL;
102 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
104 ChimaraGlk *glk = CHIMARA_GLK(object);
108 case PROP_INTERACTIVE:
109 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
112 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
114 case PROP_DEFAULT_FONT_DESCRIPTION:
115 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
117 case PROP_MONOSPACE_FONT_DESCRIPTION:
118 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
121 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
124 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
129 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
131 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
135 case PROP_INTERACTIVE:
136 g_value_set_boolean(value, priv->interactive);
139 g_value_set_boolean(value, priv->protect);
141 case PROP_DEFAULT_FONT_DESCRIPTION:
142 g_value_set_pointer(value, priv->default_font_desc);
144 case PROP_MONOSPACE_FONT_DESCRIPTION:
145 g_value_set_pointer(value, priv->monospace_font_desc);
148 g_value_set_uint(value, priv->spacing);
151 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
156 chimara_glk_finalize(GObject *object)
158 ChimaraGlk *self = CHIMARA_GLK(object);
159 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
161 /* Free the event queue */
162 g_mutex_lock(priv->event_lock);
163 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
164 g_queue_free(priv->event_queue);
165 g_cond_free(priv->event_queue_not_empty);
166 g_cond_free(priv->event_queue_not_full);
167 priv->event_queue = NULL;
168 g_mutex_unlock(priv->event_lock);
169 g_mutex_free(priv->event_lock);
171 /* Free the abort signalling mechanism */
172 g_mutex_lock(priv->abort_lock);
173 /* Make sure no other thread is busy with this */
174 g_mutex_unlock(priv->abort_lock);
175 g_mutex_free(priv->abort_lock);
176 priv->abort_lock = NULL;
178 /* Free the window arrangement signalling */
179 g_mutex_lock(priv->arrange_lock);
180 g_cond_free(priv->rearranged);
181 g_mutex_unlock(priv->arrange_lock);
182 g_mutex_free(priv->arrange_lock);
183 priv->arrange_lock = NULL;
185 /* Free private data */
186 pango_font_description_free(priv->default_font_desc);
187 pango_font_description_free(priv->monospace_font_desc);
188 g_free(priv->current_dir);
190 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
193 /* Internal function: Recursively get the Glk window tree's size request */
195 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
197 if(win->type == wintype_Pair)
199 /* Get children's size requests */
200 GtkRequisition child1, child2;
201 request_recurse(win->window_node->children->data, &child1, spacing);
202 request_recurse(win->window_node->children->next->data, &child2, spacing);
204 glui32 division = win->split_method & winmethod_DivisionMask;
205 glui32 direction = win->split_method & winmethod_DirMask;
207 /* If the split is fixed, get the size of the fixed child */
208 if(division == winmethod_Fixed)
213 child1.width = win->key_window?
214 win->constraint_size * win->key_window->unit_width
217 case winmethod_Right:
218 child2.width = win->key_window?
219 win->constraint_size * win->key_window->unit_width
222 case winmethod_Above:
223 child1.height = win->key_window?
224 win->constraint_size * win->key_window->unit_height
227 case winmethod_Below:
228 child2.height = win->key_window?
229 win->constraint_size * win->key_window->unit_height
235 /* Add the children's requests */
239 case winmethod_Right:
240 requisition->width = child1.width + child2.width + spacing;
241 requisition->height = MAX(child1.height, child2.height);
243 case winmethod_Above:
244 case winmethod_Below:
245 requisition->width = MAX(child1.width, child2.width);
246 requisition->height = child1.height + child2.height + spacing;
251 /* For non-pair windows, just use the size that GTK requests */
253 gtk_widget_size_request(win->frame, requisition);
256 /* Overrides gtk_widget_size_request */
258 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
260 g_return_if_fail(widget);
261 g_return_if_fail(requisition);
262 g_return_if_fail(CHIMARA_IS_GLK(widget));
264 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
266 /* For now, just pass the size request on to the root Glk window */
267 if(priv->root_window)
269 request_recurse(priv->root_window->data, requisition, priv->spacing);
270 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
271 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
275 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
276 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
280 /* Recursively give the Glk windows their allocated space. Returns a window
281 containing all children of this window that must be redrawn, or NULL if there
282 are no children that require redrawing. */
284 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
286 if(win->type == wintype_Pair)
288 glui32 division = win->split_method & winmethod_DivisionMask;
289 glui32 direction = win->split_method & winmethod_DirMask;
291 /* If the space gets too small to honor the spacing property, then just
292 ignore spacing in this window and below. */
293 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
294 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
297 GtkAllocation child1, child2;
298 child1.x = allocation->x;
299 child1.y = allocation->y;
301 if(division == winmethod_Fixed)
303 /* If the key window has been closed, then default to 0; otherwise
304 use the key window to determine the size */
308 child1.width = win->key_window?
309 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
312 case winmethod_Right:
313 child2.width = win->key_window?
314 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
317 case winmethod_Above:
318 child1.height = win->key_window?
319 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
322 case winmethod_Below:
323 child2.height = win->key_window?
324 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
329 else /* proportional */
331 gdouble fraction = win->constraint_size / 100.0;
335 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
337 case winmethod_Right:
338 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
340 case winmethod_Above:
341 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
343 case winmethod_Below:
344 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
349 /* Fill in the rest of the size requisitions according to the child specified above */
353 child2.width = MAX(0, allocation->width - spacing - child1.width);
354 child2.x = child1.x + child1.width + spacing;
356 child1.height = child2.height = allocation->height;
358 case winmethod_Right:
359 child1.width = MAX(0, allocation->width - spacing - child2.width);
360 child2.x = child1.x + child1.width + spacing;
362 child1.height = child2.height = allocation->height;
364 case winmethod_Above:
365 child2.height = MAX(0, allocation->height - spacing - child1.height);
367 child2.y = child1.y + child1.height + spacing;
368 child1.width = child2.width = allocation->width;
370 case winmethod_Below:
371 child1.height = MAX(0, allocation->height - spacing - child2.height);
373 child2.y = child1.y + child1.height + spacing;
374 child1.width = child2.width = allocation->width;
379 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
380 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
388 else if(win->type == wintype_TextGrid)
390 /* Pass the size allocation on to the framing widget */
391 gtk_widget_size_allocate(win->frame, allocation);
392 /* It says in the spec that when a text grid window is resized smaller,
393 the bottom or right area is thrown away; when it is resized larger, the
394 bottom or right area is filled with blanks. */
395 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
396 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
398 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
399 GtkTextIter start, end;
401 for(line = 0; line < win->height; line++)
403 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
404 /* If this line is going to fall off the bottom, delete it */
405 if(line >= newheight)
408 gtk_text_iter_forward_to_line_end(&end);
409 gtk_text_iter_forward_char(&end);
410 gtk_text_buffer_delete(textbuffer, &start, &end);
413 /* If this line is not long enough, add spaces on the end */
414 if(newwidth > win->width)
416 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
417 gtk_text_iter_forward_to_line_end(&start);
418 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
421 /* But if it's too long, delete characters from the end */
422 else if(newwidth < win->width)
425 gtk_text_iter_forward_chars(&start, newwidth);
426 gtk_text_iter_forward_to_line_end(&end);
427 gtk_text_buffer_delete(textbuffer, &start, &end);
429 /* Note: if the widths are equal, do nothing */
431 /* Add blank lines if there aren't enough lines to fit the new size */
432 if(newheight > win->height)
434 gchar *blanks = g_strnfill(win->width, ' ');
435 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
437 for(count = 0; count < newheight - win->height; count++)
438 blanklines[count] = blanks;
439 blanklines[newheight - win->height] = NULL;
440 gchar *text = g_strjoinv("\n", blanklines);
441 g_free(blanklines); /* not g_strfreev() */
444 gtk_text_buffer_get_end_iter(textbuffer, &start);
445 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
446 gtk_text_buffer_insert(textbuffer, &start, text, -1);
450 gboolean arrange = !(win->width == newwidth && win->height == newheight);
451 win->width = newwidth;
452 win->height = newheight;
453 return arrange? win : NULL;
456 /* For non-pair, non-text-grid windows, just give them the size */
457 gtk_widget_size_allocate(win->frame, allocation);
461 /* Overrides gtk_widget_size_allocate */
463 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
465 g_return_if_fail(widget);
466 g_return_if_fail(allocation);
467 g_return_if_fail(CHIMARA_IS_GLK(widget));
469 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
471 widget->allocation = *allocation;
473 if(priv->root_window) {
475 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
476 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
477 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
478 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
479 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
481 /* arrange points to a window that contains all text grid and graphics
482 windows which have been resized */
483 g_mutex_lock(priv->arrange_lock);
484 if(!priv->ignore_next_arrange_event)
487 event_throw(evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
490 priv->ignore_next_arrange_event = FALSE;
491 priv->needs_rearrange = FALSE;
492 g_cond_signal(priv->rearranged);
493 g_mutex_unlock(priv->arrange_lock);
497 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
499 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
501 if(win->type == wintype_Pair)
503 forall_recurse(win->window_node->children->data, callback, callback_data);
504 forall_recurse(win->window_node->children->next->data, callback, callback_data);
507 (*callback)(win->frame, callback_data);
510 /* Overrides gtk_container_forall */
512 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
514 g_return_if_fail(container);
515 g_return_if_fail(CHIMARA_IS_GLK(container));
517 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
519 /* All the children are "internal" */
520 if(!include_internals)
523 if(priv->root_window)
524 forall_recurse(priv->root_window->data, callback, callback_data);
528 chimara_glk_stopped(ChimaraGlk *self)
530 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
532 /* Free the plugin */
533 if( priv->program && !g_module_close(priv->program) )
534 g_warning( "Error closing module: %s", g_module_error() );
538 chimara_glk_started(ChimaraGlk *self)
540 /* TODO: Add default signal handler implementation here */
543 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
544 #ifndef G_PARAM_STATIC_STRINGS
545 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
549 chimara_glk_class_init(ChimaraGlkClass *klass)
551 /* Override methods of parent classes */
552 GObjectClass *object_class = G_OBJECT_CLASS(klass);
553 object_class->set_property = chimara_glk_set_property;
554 object_class->get_property = chimara_glk_get_property;
555 object_class->finalize = chimara_glk_finalize;
557 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
558 widget_class->size_request = chimara_glk_size_request;
559 widget_class->size_allocate = chimara_glk_size_allocate;
561 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
562 container_class->forall = chimara_glk_forall;
565 klass->stopped = chimara_glk_stopped;
566 klass->started = chimara_glk_started;
568 * ChimaraGlk::stopped:
569 * @glk: The widget that received the signal
571 * The ::stopped signal is emitted when the a Glk program finishes
572 * executing in the widget, whether it ended normally, or was interrupted.
574 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
575 G_OBJECT_CLASS_TYPE(klass), 0,
576 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
577 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
579 * ChimaraGlk::started:
580 * @glk: The widget that received the signal
582 * The ::started signal is emitted when a Glk program starts executing in
585 chimara_glk_signals[STARTED] = g_signal_new ("started",
586 G_OBJECT_CLASS_TYPE (klass), 0,
587 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
588 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
592 * ChimaraGlk:interactive:
594 * Sets whether the widget is interactive. A Glk widget is normally
595 * interactive, but in non-interactive mode, keyboard and mouse input are
596 * ignored and the Glk program is controlled by chimara_glk_feed_text().
597 * <quote>More</quote> prompts when a lot of text is printed to a text
598 * buffer are also disabled. This is typically used when you wish to control
599 * an interpreter program by feeding it a predefined list of commands.
601 g_object_class_install_property( object_class, PROP_INTERACTIVE,
602 g_param_spec_boolean("interactive", _("Interactive"),
603 _("Whether user input is expected in the Glk program"),
605 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
608 * ChimaraGlk:protect:
610 * Sets whether the Glk program is allowed to do file operations. In protect
611 * mode, all file operations will fail.
613 g_object_class_install_property(object_class, PROP_PROTECT,
614 g_param_spec_boolean("protect", _("Protected"),
615 _("Whether the Glk program is barred from doing file operations"),
617 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
619 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
620 initialize them with NULL */
622 * ChimaraGlk:default-font-description:
624 * Pointer to a #PangoFontDescription describing the default proportional
625 * font, to be used in text buffer windows for example.
627 * Default value: font description created from the string
628 * <quote>Sans</quote>
630 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
631 g_param_spec_pointer("default-font-description", _("Default Font"),
632 _("Font description of the default proportional font"),
633 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
636 * ChimaraGlk:monospace-font-description:
638 * Pointer to a #PangoFontDescription describing the default monospace font,
639 * to be used in text grid windows and %style_Preformatted, for example.
641 * Default value: font description created from the string
642 * <quote>Monospace</quote>
644 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
645 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
646 _("Font description of the default monospace font"),
647 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
650 * ChimaraGlk:spacing:
652 * The amount of space between the Glk windows.
654 g_object_class_install_property(object_class, PROP_SPACING,
655 g_param_spec_uint("spacing", _("Spacing"),
656 _("The amount of space between Glk windows"),
658 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
661 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
664 /* PUBLIC FUNCTIONS */
669 * Creates and initializes a new #ChimaraGlk widget.
671 * Return value: a #ChimaraGlk widget, with a floating reference.
674 chimara_glk_new(void)
676 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
677 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
679 priv->event_queue = g_queue_new();
680 priv->event_lock = g_mutex_new();
681 priv->event_queue_not_empty = g_cond_new();
682 priv->event_queue_not_full = g_cond_new();
683 priv->abort_lock = g_mutex_new();
684 priv->arrange_lock = g_mutex_new();
685 priv->rearranged = g_cond_new();
687 return GTK_WIDGET(self);
691 * chimara_glk_set_interactive:
692 * @glk: a #ChimaraGlk widget
693 * @interactive: whether the widget should expect user input
695 * Sets the #ChimaraGlk:interactive property of @glk.
698 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
700 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
702 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
703 priv->interactive = interactive;
707 * chimara_glk_get_interactive:
708 * @glk: a #ChimaraGlk widget
710 * Returns whether @glk is interactive (expecting user input). See
711 * #ChimaraGlk:interactive.
713 * Return value: %TRUE if @glk is interactive.
716 chimara_glk_get_interactive(ChimaraGlk *glk)
718 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
720 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
721 return priv->interactive;
725 * chimara_glk_set_protect:
726 * @glk: a #ChimaraGlk widget
727 * @protect: whether the widget should allow the Glk program to do file
730 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
731 * program is not allowed to do file operations.
734 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
736 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
738 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
739 priv->protect = protect;
743 * chimara_glk_get_protect:
744 * @glk: a #ChimaraGlk widget
746 * Returns whether @glk is in protect mode (banned from doing file operations).
747 * See #ChimaraGlk:protect.
749 * Return value: %TRUE if @glk is in protect mode.
752 chimara_glk_get_protect(ChimaraGlk *glk)
754 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
756 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
757 return priv->protect;
761 * chimara_glk_set_default_font_description:
762 * @glk: a #ChimaraGlk widget
763 * @font: a #PangoFontDescription
765 * Sets @glk's default proportional font. See
766 * #ChimaraGlk:default-font-description.
769 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
771 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
772 g_return_if_fail(font);
774 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
775 pango_font_description_free(priv->default_font_desc);
776 priv->default_font_desc = pango_font_description_copy(font);
778 /* TODO: Apply the font description to all the windows and recalculate the sizes */
782 * chimara_glk_set_default_font_string:
783 * @glk: a #ChimaraGlk widget
784 * @font: string representation of a font description
786 * Sets @glk's default proportional font according to the string @font, which
787 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
788 * [<replaceable>STYLE-OPTIONS</replaceable>]
789 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
790 * Italic 12</quote> or <quote>Sans</quote>. See
791 * #ChimaraGlk:default-font-description.
794 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
796 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
797 g_return_if_fail(font || *font);
799 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
800 g_return_if_fail(fontdesc);
802 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
803 pango_font_description_free(priv->default_font_desc);
804 priv->default_font_desc = fontdesc;
806 /* TODO: Apply the font description to all the windows and recalculate the sizes */
810 * chimara_glk_get_default_font_description:
811 * @glk: a #ChimaraGlk widget
813 * Returns @glk's default proportional font.
815 * Return value: a newly-allocated #PangoFontDescription which must be freed
816 * using pango_font_description_free(), or %NULL on error.
818 PangoFontDescription *
819 chimara_glk_get_default_font_description(ChimaraGlk *glk)
821 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
823 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
824 return pango_font_description_copy(priv->default_font_desc);
828 * chimara_glk_set_monospace_font_description:
829 * @glk: a #ChimaraGlk widget
830 * @font: a #PangoFontDescription
832 * Sets @glk's default monospace font. See
833 * #ChimaraGlk:monospace-font-description.
836 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
838 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
839 g_return_if_fail(font);
841 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
842 pango_font_description_free(priv->monospace_font_desc);
843 priv->monospace_font_desc = pango_font_description_copy(font);
845 /* TODO: Apply the font description to all the windows and recalculate the sizes */
849 * chimara_glk_set_monospace_font_string:
850 * @glk: a #ChimaraGlk widget
851 * @font: string representation of a font description
853 * Sets @glk's default monospace font according to the string @font, which must
854 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
855 * [<replaceable>STYLE-OPTIONS</replaceable>]
856 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
857 * Bold 12</quote> or <quote>Monospace</quote>. See
858 * #ChimaraGlk:monospace-font-description.
861 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
863 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
864 g_return_if_fail(font || *font);
866 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
867 g_return_if_fail(fontdesc);
869 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
870 pango_font_description_free(priv->monospace_font_desc);
871 priv->monospace_font_desc = fontdesc;
873 /* TODO: Apply the font description to all the windows and recalculate the sizes */
877 * chimara_glk_get_monospace_font_description:
878 * @glk: a #ChimaraGlk widget
880 * Returns @glk's default monospace font.
882 * Return value: a newly-allocated #PangoFontDescription which must be freed
883 * using pango_font_description_free(), or %NULL on error.
885 PangoFontDescription *
886 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
888 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
890 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
891 return pango_font_description_copy(priv->monospace_font_desc);
895 * chimara_glk_set_spacing:
896 * @glk: a #ChimaraGlk widget
897 * @spacing: the number of pixels to put between Glk windows
899 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
900 * pixels between Glk windows.
903 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
905 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
907 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
908 priv->spacing = spacing;
912 * chimara_glk_get_spacing:
913 * @glk: a #ChimaraGlk widget
915 * Gets the value set by chimara_glk_set_spacing().
917 * Return value: pixels of spacing between Glk windows
920 chimara_glk_get_spacing(ChimaraGlk *glk)
922 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
924 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
925 return priv->spacing;
930 glkunix_startup_code_t glkunix_startup_code;
931 glkunix_startup_t args;
934 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
936 glk_enter(struct StartupData *startup)
938 extern ChimaraGlkPrivate *glk_data;
940 /* Run startup function */
941 if(startup->glkunix_startup_code) {
942 glk_data->in_startup = TRUE;
943 int result = startup->glkunix_startup_code(&startup->args);
944 glk_data->in_startup = FALSE;
947 while(i < startup->args.argc)
948 g_free(startup->args.argv[i++]);
949 g_free(startup->args.argv);
955 /* Run main function */
956 g_signal_emit_by_name(glk_data->self, "started");
957 (startup->glk_main)();
958 g_slice_free(struct StartupData, startup);
959 g_signal_emit_by_name(glk_data->self, "stopped");
965 * @glk: a #ChimaraGlk widget
966 * @plugin: path to a plugin module compiled with <filename
967 * class="header">glk.h</filename>
968 * @argc: Number of command line arguments in @argv
969 * @argv: Array of command line arguments to pass to the plugin
970 * @error: location to store a <link linkend="glib-GError">GError</link>, or
973 * Opens a Glk program compiled as a plugin. Sorts out its command line
974 * arguments from #glkunix_arguments, calls its startup function
975 * glkunix_startup_code(), and then calls its main function glk_main() in
976 * a separate thread. On failure, returns %FALSE and sets @error.
978 * The plugin must at least export a glk_main() function; #glkunix_arguments and
979 * glkunix_startup_code() are optional.
981 * Return value: %TRUE if the Glk program was started successfully.
984 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
986 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
987 g_return_val_if_fail(plugin, FALSE);
989 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
990 struct StartupData *startup = g_slice_new0(struct StartupData);
992 /* Open the module to run */
993 g_assert( g_module_supported() );
994 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
998 g_warning( "Error opening module: %s", g_module_error() );
1001 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1003 g_warning( "Error finding glk_main(): %s", g_module_error() );
1007 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1009 glkunix_argumentlist_t *glkunix_arguments;
1011 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1012 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1014 /* arguments could not be parsed, so create data ourselves */
1015 startup->args.argc = 1;
1016 startup->args.argv = g_new0(gchar *, 1);
1019 /* Set the program name */
1020 startup->args.argv[0] = g_strdup(plugin);
1023 extern ChimaraGlkPrivate *glk_data;
1024 /* Set the thread's private data */
1025 /* TODO: Do this with a GPrivate */
1028 /* Run in a separate thread */
1029 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1031 return !(priv->thread == NULL);
1036 * @glk: a #ChimaraGlk widget
1038 * Signals the Glk program running in @glk to abort. Note that if the program is
1039 * caught in an infinite loop in which glk_tick() is not called, this may not
1043 chimara_glk_stop(ChimaraGlk *glk)
1045 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1046 /* TODO: check if glk is actually running a program */
1052 * @glk: a #ChimaraGlk widget
1054 * Holds up the main thread and waits for the Glk program running in @glk to
1058 chimara_glk_wait(ChimaraGlk *glk)
1060 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1062 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1063 g_thread_join(priv->thread);