1 /* licensing and copyright information here */
6 #include <glib/gi18n-lib.h>
8 #include <pango/pango.h>
9 #include "chimara-glk.h"
10 #include "chimara-glk-private.h"
18 #define CHIMARA_GLK_MIN_WIDTH 0
19 #define CHIMARA_GLK_MIN_HEIGHT 0
23 * @short_description: Widget which executes a Glk program
24 * @stability: Unstable
25 * @include: chimara/chimara-glk.h
27 * The ChimaraGlk widget opens and runs a Glk program. The program must be
28 * compiled as a plugin module, with a function <function>glk_main()</function>
29 * that the Glk library can hook into.
31 * On Linux systems, this is a file with a name like
32 * <filename>plugin.so</filename>. For portability, you can use libtool and
35 * pkglib_LTLIBRARIES = plugin.la
36 * plugin_la_SOURCES = plugin.c foo.c bar.c
37 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
39 * This will produce <filename>plugin.la</filename> which is a text file
40 * containing the correct plugin file to open (see the relevant section of the
42 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
43 * Libtool manual</ulink>).
46 typedef void (* glk_main_t) (void);
47 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
53 PROP_DEFAULT_FONT_DESCRIPTION,
54 PROP_MONOSPACE_FONT_DESCRIPTION,
65 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
67 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
70 chimara_glk_init(ChimaraGlk *self)
72 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
74 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
77 priv->interactive = TRUE;
78 priv->protect = FALSE;
79 priv->default_font_desc = pango_font_description_from_string("Sans");
80 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
83 priv->event_queue = NULL;
84 priv->event_lock = NULL;
85 priv->event_queue_not_empty = NULL;
86 priv->event_queue_not_full = NULL;
87 priv->abort_lock = NULL;
88 priv->abort_signalled = FALSE;
89 priv->arrange_lock = NULL;
90 priv->rearranged = NULL;
91 priv->needs_rearrange = FALSE;
92 priv->ignore_next_arrange_event = FALSE;
93 priv->interrupt_handler = NULL;
94 priv->root_window = NULL;
95 priv->fileref_list = NULL;
96 priv->current_stream = NULL;
97 priv->stream_list = NULL;
99 priv->in_startup = FALSE;
100 priv->current_dir = NULL;
104 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
106 ChimaraGlk *glk = CHIMARA_GLK(object);
110 case PROP_INTERACTIVE:
111 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
114 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
116 case PROP_DEFAULT_FONT_DESCRIPTION:
117 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
119 case PROP_MONOSPACE_FONT_DESCRIPTION:
120 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
123 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
126 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
131 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
133 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
137 case PROP_INTERACTIVE:
138 g_value_set_boolean(value, priv->interactive);
141 g_value_set_boolean(value, priv->protect);
143 case PROP_DEFAULT_FONT_DESCRIPTION:
144 g_value_set_pointer(value, priv->default_font_desc);
146 case PROP_MONOSPACE_FONT_DESCRIPTION:
147 g_value_set_pointer(value, priv->monospace_font_desc);
150 g_value_set_uint(value, priv->spacing);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
158 chimara_glk_finalize(GObject *object)
160 ChimaraGlk *self = CHIMARA_GLK(object);
161 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
163 /* Free the event queue */
164 g_mutex_lock(priv->event_lock);
165 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
166 g_queue_free(priv->event_queue);
167 g_cond_free(priv->event_queue_not_empty);
168 g_cond_free(priv->event_queue_not_full);
169 priv->event_queue = NULL;
170 g_mutex_unlock(priv->event_lock);
171 g_mutex_free(priv->event_lock);
173 /* Free the abort signalling mechanism */
174 g_mutex_lock(priv->abort_lock);
175 /* Make sure no other thread is busy with this */
176 g_mutex_unlock(priv->abort_lock);
177 g_mutex_free(priv->abort_lock);
178 priv->abort_lock = NULL;
180 /* Free the window arrangement signalling */
181 g_mutex_lock(priv->arrange_lock);
182 g_cond_free(priv->rearranged);
183 g_mutex_unlock(priv->arrange_lock);
184 g_mutex_free(priv->arrange_lock);
185 priv->arrange_lock = NULL;
187 /* Free private data */
188 pango_font_description_free(priv->default_font_desc);
189 pango_font_description_free(priv->monospace_font_desc);
190 g_free(priv->current_dir);
192 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
195 /* Internal function: Recursively get the Glk window tree's size request */
197 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
199 if(win->type == wintype_Pair)
201 /* Get children's size requests */
202 GtkRequisition child1, child2;
203 request_recurse(win->window_node->children->data, &child1, spacing);
204 request_recurse(win->window_node->children->next->data, &child2, spacing);
206 glui32 division = win->split_method & winmethod_DivisionMask;
207 glui32 direction = win->split_method & winmethod_DirMask;
209 /* If the split is fixed, get the size of the fixed child */
210 if(division == winmethod_Fixed)
215 child1.width = win->key_window?
216 win->constraint_size * win->key_window->unit_width
219 case winmethod_Right:
220 child2.width = win->key_window?
221 win->constraint_size * win->key_window->unit_width
224 case winmethod_Above:
225 child1.height = win->key_window?
226 win->constraint_size * win->key_window->unit_height
229 case winmethod_Below:
230 child2.height = win->key_window?
231 win->constraint_size * win->key_window->unit_height
237 /* Add the children's requests */
241 case winmethod_Right:
242 requisition->width = child1.width + child2.width + spacing;
243 requisition->height = MAX(child1.height, child2.height);
245 case winmethod_Above:
246 case winmethod_Below:
247 requisition->width = MAX(child1.width, child2.width);
248 requisition->height = child1.height + child2.height + spacing;
253 /* For non-pair windows, just use the size that GTK requests */
255 gtk_widget_size_request(win->frame, requisition);
258 /* Overrides gtk_widget_size_request */
260 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
262 g_return_if_fail(widget);
263 g_return_if_fail(requisition);
264 g_return_if_fail(CHIMARA_IS_GLK(widget));
266 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
268 /* For now, just pass the size request on to the root Glk window */
269 if(priv->root_window)
271 request_recurse(priv->root_window->data, requisition, priv->spacing);
272 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
273 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
277 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
278 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
282 /* Recursively give the Glk windows their allocated space. Returns a window
283 containing all children of this window that must be redrawn, or NULL if there
284 are no children that require redrawing. */
286 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
288 if(win->type == wintype_Pair)
290 glui32 division = win->split_method & winmethod_DivisionMask;
291 glui32 direction = win->split_method & winmethod_DirMask;
293 /* If the space gets too small to honor the spacing property, then just
294 ignore spacing in this window and below. */
295 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
296 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
299 GtkAllocation child1, child2;
300 child1.x = allocation->x;
301 child1.y = allocation->y;
303 if(division == winmethod_Fixed)
305 /* If the key window has been closed, then default to 0; otherwise
306 use the key window to determine the size */
310 child1.width = win->key_window?
311 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
314 case winmethod_Right:
315 child2.width = win->key_window?
316 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
319 case winmethod_Above:
320 child1.height = win->key_window?
321 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
324 case winmethod_Below:
325 child2.height = win->key_window?
326 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
331 else /* proportional */
333 gdouble fraction = win->constraint_size / 100.0;
337 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
339 case winmethod_Right:
340 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
342 case winmethod_Above:
343 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
345 case winmethod_Below:
346 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
351 /* Fill in the rest of the size requisitions according to the child specified above */
355 child2.width = MAX(0, allocation->width - spacing - child1.width);
356 child2.x = child1.x + child1.width + spacing;
358 child1.height = child2.height = allocation->height;
360 case winmethod_Right:
361 child1.width = MAX(0, allocation->width - spacing - child2.width);
362 child2.x = child1.x + child1.width + spacing;
364 child1.height = child2.height = allocation->height;
366 case winmethod_Above:
367 child2.height = MAX(0, allocation->height - spacing - child1.height);
369 child2.y = child1.y + child1.height + spacing;
370 child1.width = child2.width = allocation->width;
372 case winmethod_Below:
373 child1.height = MAX(0, allocation->height - spacing - child2.height);
375 child2.y = child1.y + child1.height + spacing;
376 child1.width = child2.width = allocation->width;
381 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
382 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
390 else if(win->type == wintype_TextGrid)
392 /* Pass the size allocation on to the framing widget */
393 gtk_widget_size_allocate(win->frame, allocation);
394 /* It says in the spec that when a text grid window is resized smaller,
395 the bottom or right area is thrown away; when it is resized larger, the
396 bottom or right area is filled with blanks. */
397 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
398 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
400 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
401 GtkTextIter start, end;
403 for(line = 0; line < win->height; line++)
405 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
406 /* If this line is going to fall off the bottom, delete it */
407 if(line >= newheight)
410 gtk_text_iter_forward_to_line_end(&end);
411 gtk_text_iter_forward_char(&end);
412 gtk_text_buffer_delete(textbuffer, &start, &end);
415 /* If this line is not long enough, add spaces on the end */
416 if(newwidth > win->width)
418 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
419 gtk_text_iter_forward_to_line_end(&start);
420 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
423 /* But if it's too long, delete characters from the end */
424 else if(newwidth < win->width)
427 gtk_text_iter_forward_chars(&start, newwidth);
428 gtk_text_iter_forward_to_line_end(&end);
429 gtk_text_buffer_delete(textbuffer, &start, &end);
431 /* Note: if the widths are equal, do nothing */
433 /* Add blank lines if there aren't enough lines to fit the new size */
434 if(newheight > win->height)
436 gchar *blanks = g_strnfill(win->width, ' ');
437 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
439 for(count = 0; count < newheight - win->height; count++)
440 blanklines[count] = blanks;
441 blanklines[newheight - win->height] = NULL;
442 gchar *text = g_strjoinv("\n", blanklines);
443 g_free(blanklines); /* not g_strfreev() */
446 gtk_text_buffer_get_end_iter(textbuffer, &start);
447 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
448 gtk_text_buffer_insert(textbuffer, &start, text, -1);
452 gboolean arrange = !(win->width == newwidth && win->height == newheight);
453 win->width = newwidth;
454 win->height = newheight;
455 return arrange? win : NULL;
458 /* For non-pair, non-text-grid windows, just give them the size */
459 gtk_widget_size_allocate(win->frame, allocation);
463 /* Overrides gtk_widget_size_allocate */
465 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
467 g_return_if_fail(widget);
468 g_return_if_fail(allocation);
469 g_return_if_fail(CHIMARA_IS_GLK(widget));
471 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
473 widget->allocation = *allocation;
475 if(priv->root_window) {
477 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
478 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
479 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
480 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
481 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
483 /* arrange points to a window that contains all text grid and graphics
484 windows which have been resized */
485 g_mutex_lock(priv->arrange_lock);
486 if(!priv->ignore_next_arrange_event)
489 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
492 priv->ignore_next_arrange_event = FALSE;
493 priv->needs_rearrange = FALSE;
494 g_cond_signal(priv->rearranged);
495 g_mutex_unlock(priv->arrange_lock);
499 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
501 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
503 if(win->type == wintype_Pair)
505 forall_recurse(win->window_node->children->data, callback, callback_data);
506 forall_recurse(win->window_node->children->next->data, callback, callback_data);
509 (*callback)(win->frame, callback_data);
512 /* Overrides gtk_container_forall */
514 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
516 g_return_if_fail(container);
517 g_return_if_fail(CHIMARA_IS_GLK(container));
519 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
521 /* All the children are "internal" */
522 if(!include_internals)
525 if(priv->root_window)
526 forall_recurse(priv->root_window->data, callback, callback_data);
530 chimara_glk_stopped(ChimaraGlk *self)
532 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
534 /* Free the plugin */
535 if( priv->program && !g_module_close(priv->program) )
536 g_warning( "Error closing module: %s", g_module_error() );
540 chimara_glk_started(ChimaraGlk *self)
542 /* TODO: Add default signal handler implementation here */
545 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
546 #ifndef G_PARAM_STATIC_STRINGS
547 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
551 chimara_glk_class_init(ChimaraGlkClass *klass)
553 /* Override methods of parent classes */
554 GObjectClass *object_class = G_OBJECT_CLASS(klass);
555 object_class->set_property = chimara_glk_set_property;
556 object_class->get_property = chimara_glk_get_property;
557 object_class->finalize = chimara_glk_finalize;
559 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
560 widget_class->size_request = chimara_glk_size_request;
561 widget_class->size_allocate = chimara_glk_size_allocate;
563 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
564 container_class->forall = chimara_glk_forall;
567 klass->stopped = chimara_glk_stopped;
568 klass->started = chimara_glk_started;
570 * ChimaraGlk::stopped:
571 * @glk: The widget that received the signal
573 * The ::stopped signal is emitted when the a Glk program finishes
574 * executing in the widget, whether it ended normally, or was interrupted.
576 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
577 G_OBJECT_CLASS_TYPE(klass), 0,
578 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
579 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
581 * ChimaraGlk::started:
582 * @glk: The widget that received the signal
584 * The ::started signal is emitted when a Glk program starts executing in
587 chimara_glk_signals[STARTED] = g_signal_new ("started",
588 G_OBJECT_CLASS_TYPE (klass), 0,
589 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
590 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
594 * ChimaraGlk:interactive:
596 * Sets whether the widget is interactive. A Glk widget is normally
597 * interactive, but in non-interactive mode, keyboard and mouse input are
598 * ignored and the Glk program is controlled by chimara_glk_feed_text().
599 * <quote>More</quote> prompts when a lot of text is printed to a text
600 * buffer are also disabled. This is typically used when you wish to control
601 * an interpreter program by feeding it a predefined list of commands.
603 g_object_class_install_property( object_class, PROP_INTERACTIVE,
604 g_param_spec_boolean("interactive", _("Interactive"),
605 _("Whether user input is expected in the Glk program"),
607 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
610 * ChimaraGlk:protect:
612 * Sets whether the Glk program is allowed to do file operations. In protect
613 * mode, all file operations will fail.
615 g_object_class_install_property(object_class, PROP_PROTECT,
616 g_param_spec_boolean("protect", _("Protected"),
617 _("Whether the Glk program is barred from doing file operations"),
619 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
621 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
622 initialize them with NULL */
624 * ChimaraGlk:default-font-description:
626 * Pointer to a #PangoFontDescription describing the default proportional
627 * font, to be used in text buffer windows for example.
629 * Default value: font description created from the string
630 * <quote>Sans</quote>
632 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
633 g_param_spec_pointer("default-font-description", _("Default Font"),
634 _("Font description of the default proportional font"),
635 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
638 * ChimaraGlk:monospace-font-description:
640 * Pointer to a #PangoFontDescription describing the default monospace font,
641 * to be used in text grid windows and %style_Preformatted, for example.
643 * Default value: font description created from the string
644 * <quote>Monospace</quote>
646 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
647 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
648 _("Font description of the default monospace font"),
649 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
652 * ChimaraGlk:spacing:
654 * The amount of space between the Glk windows.
656 g_object_class_install_property(object_class, PROP_SPACING,
657 g_param_spec_uint("spacing", _("Spacing"),
658 _("The amount of space between Glk windows"),
660 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
663 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
666 /* PUBLIC FUNCTIONS */
671 * Creates and initializes a new #ChimaraGlk widget.
673 * Return value: a #ChimaraGlk widget, with a floating reference.
676 chimara_glk_new(void)
678 /* This is a library entry point; initialize the library */
681 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
682 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
684 priv->event_queue = g_queue_new();
685 priv->event_lock = g_mutex_new();
686 priv->event_queue_not_empty = g_cond_new();
687 priv->event_queue_not_full = g_cond_new();
688 priv->abort_lock = g_mutex_new();
689 priv->arrange_lock = g_mutex_new();
690 priv->rearranged = g_cond_new();
692 return GTK_WIDGET(self);
696 * chimara_glk_set_interactive:
697 * @glk: a #ChimaraGlk widget
698 * @interactive: whether the widget should expect user input
700 * Sets the #ChimaraGlk:interactive property of @glk.
703 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
705 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
707 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
708 priv->interactive = interactive;
712 * chimara_glk_get_interactive:
713 * @glk: a #ChimaraGlk widget
715 * Returns whether @glk is interactive (expecting user input). See
716 * #ChimaraGlk:interactive.
718 * Return value: %TRUE if @glk is interactive.
721 chimara_glk_get_interactive(ChimaraGlk *glk)
723 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
725 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
726 return priv->interactive;
730 * chimara_glk_set_protect:
731 * @glk: a #ChimaraGlk widget
732 * @protect: whether the widget should allow the Glk program to do file
735 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
736 * program is not allowed to do file operations.
739 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
741 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
743 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
744 priv->protect = protect;
748 * chimara_glk_get_protect:
749 * @glk: a #ChimaraGlk widget
751 * Returns whether @glk is in protect mode (banned from doing file operations).
752 * See #ChimaraGlk:protect.
754 * Return value: %TRUE if @glk is in protect mode.
757 chimara_glk_get_protect(ChimaraGlk *glk)
759 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
761 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
762 return priv->protect;
766 * chimara_glk_set_default_font_description:
767 * @glk: a #ChimaraGlk widget
768 * @font: a #PangoFontDescription
770 * Sets @glk's default proportional font. See
771 * #ChimaraGlk:default-font-description.
774 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
776 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
777 g_return_if_fail(font);
779 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
780 pango_font_description_free(priv->default_font_desc);
781 priv->default_font_desc = pango_font_description_copy(font);
783 /* TODO: Apply the font description to all the windows and recalculate the sizes */
787 * chimara_glk_set_default_font_string:
788 * @glk: a #ChimaraGlk widget
789 * @font: string representation of a font description
791 * Sets @glk's default proportional font according to the string @font, which
792 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
793 * [<replaceable>STYLE-OPTIONS</replaceable>]
794 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
795 * Italic 12</quote> or <quote>Sans</quote>. See
796 * #ChimaraGlk:default-font-description.
799 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
801 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
802 g_return_if_fail(font || *font);
804 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
805 g_return_if_fail(fontdesc);
807 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
808 pango_font_description_free(priv->default_font_desc);
809 priv->default_font_desc = fontdesc;
811 /* TODO: Apply the font description to all the windows and recalculate the sizes */
815 * chimara_glk_get_default_font_description:
816 * @glk: a #ChimaraGlk widget
818 * Returns @glk's default proportional font.
820 * Return value: a newly-allocated #PangoFontDescription which must be freed
821 * using pango_font_description_free(), or %NULL on error.
823 PangoFontDescription *
824 chimara_glk_get_default_font_description(ChimaraGlk *glk)
826 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
828 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
829 return pango_font_description_copy(priv->default_font_desc);
833 * chimara_glk_set_monospace_font_description:
834 * @glk: a #ChimaraGlk widget
835 * @font: a #PangoFontDescription
837 * Sets @glk's default monospace font. See
838 * #ChimaraGlk:monospace-font-description.
841 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
843 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
844 g_return_if_fail(font);
846 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
847 pango_font_description_free(priv->monospace_font_desc);
848 priv->monospace_font_desc = pango_font_description_copy(font);
850 /* TODO: Apply the font description to all the windows and recalculate the sizes */
854 * chimara_glk_set_monospace_font_string:
855 * @glk: a #ChimaraGlk widget
856 * @font: string representation of a font description
858 * Sets @glk's default monospace font according to the string @font, which must
859 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
860 * [<replaceable>STYLE-OPTIONS</replaceable>]
861 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
862 * Bold 12</quote> or <quote>Monospace</quote>. See
863 * #ChimaraGlk:monospace-font-description.
866 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
868 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
869 g_return_if_fail(font || *font);
871 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
872 g_return_if_fail(fontdesc);
874 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
875 pango_font_description_free(priv->monospace_font_desc);
876 priv->monospace_font_desc = fontdesc;
878 /* TODO: Apply the font description to all the windows and recalculate the sizes */
882 * chimara_glk_get_monospace_font_description:
883 * @glk: a #ChimaraGlk widget
885 * Returns @glk's default monospace font.
887 * Return value: a newly-allocated #PangoFontDescription which must be freed
888 * using pango_font_description_free(), or %NULL on error.
890 PangoFontDescription *
891 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
893 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
895 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
896 return pango_font_description_copy(priv->monospace_font_desc);
900 * chimara_glk_set_spacing:
901 * @glk: a #ChimaraGlk widget
902 * @spacing: the number of pixels to put between Glk windows
904 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
905 * pixels between Glk windows.
908 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
910 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
912 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
913 priv->spacing = spacing;
917 * chimara_glk_get_spacing:
918 * @glk: a #ChimaraGlk widget
920 * Gets the value set by chimara_glk_set_spacing().
922 * Return value: pixels of spacing between Glk windows
925 chimara_glk_get_spacing(ChimaraGlk *glk)
927 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
929 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
930 return priv->spacing;
935 glkunix_startup_code_t glkunix_startup_code;
936 glkunix_startup_t args;
937 ChimaraGlkPrivate *glk_data;
940 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
942 glk_enter(struct StartupData *startup)
944 extern GPrivate *glk_data_key;
945 g_private_set(glk_data_key, startup->glk_data);
947 /* Run startup function */
948 if(startup->glkunix_startup_code) {
949 startup->glk_data->in_startup = TRUE;
950 int result = startup->glkunix_startup_code(&startup->args);
951 startup->glk_data->in_startup = FALSE;
954 while(i < startup->args.argc)
955 g_free(startup->args.argv[i++]);
956 g_free(startup->args.argv);
962 /* Run main function */
963 g_signal_emit_by_name(startup->glk_data->self, "started");
964 (startup->glk_main)();
965 g_signal_emit_by_name(startup->glk_data->self, "stopped");
966 g_slice_free(struct StartupData, startup);
972 * @glk: a #ChimaraGlk widget
973 * @plugin: path to a plugin module compiled with <filename
974 * class="header">glk.h</filename>
975 * @argc: Number of command line arguments in @argv
976 * @argv: Array of command line arguments to pass to the plugin
977 * @error: location to store a <link linkend="glib-GError">GError</link>, or
980 * Opens a Glk program compiled as a plugin. Sorts out its command line
981 * arguments from #glkunix_arguments, calls its startup function
982 * glkunix_startup_code(), and then calls its main function glk_main() in
983 * a separate thread. On failure, returns %FALSE and sets @error.
985 * The plugin must at least export a glk_main() function; #glkunix_arguments and
986 * glkunix_startup_code() are optional.
988 * Return value: %TRUE if the Glk program was started successfully.
991 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
993 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
994 g_return_val_if_fail(plugin, FALSE);
996 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
997 struct StartupData *startup = g_slice_new0(struct StartupData);
999 /* Open the module to run */
1000 g_assert( g_module_supported() );
1001 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1005 g_warning( "Error opening module: %s", g_module_error() );
1008 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1010 g_warning( "Error finding glk_main(): %s", g_module_error() );
1014 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1016 glkunix_argumentlist_t *glkunix_arguments;
1018 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1019 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1021 /* arguments could not be parsed, so create data ourselves */
1022 startup->args.argc = 1;
1023 startup->args.argv = g_new0(gchar *, 1);
1026 /* Set the program name */
1027 startup->args.argv[0] = g_strdup(plugin);
1029 startup->glk_data = priv;
1031 /* Run in a separate thread */
1032 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1034 return !(priv->thread == NULL);
1039 * @glk: a #ChimaraGlk widget
1041 * Signals the Glk program running in @glk to abort. Note that if the program is
1042 * caught in an infinite loop in which glk_tick() is not called, this may not
1046 chimara_glk_stop(ChimaraGlk *glk)
1048 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1049 /* TODO: check if glk is actually running a program */
1050 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1051 if(priv->abort_lock) {
1052 g_mutex_lock(priv->abort_lock);
1053 priv->abort_signalled = TRUE;
1054 g_mutex_unlock(priv->abort_lock);
1055 /* Stop blocking on the event queue condition */
1056 event_throw(glk, evtype_Abort, NULL, 0, 0);
1062 * @glk: a #ChimaraGlk widget
1064 * Holds up the main thread and waits for the Glk program running in @glk to
1068 chimara_glk_wait(ChimaraGlk *glk)
1070 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1072 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1073 g_thread_join(priv->thread);