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"
11 #include "chimara-marshallers.h"
19 #define CHIMARA_GLK_MIN_WIDTH 0
20 #define CHIMARA_GLK_MIN_HEIGHT 0
24 * @short_description: Widget which executes a Glk program
25 * @stability: Unstable
26 * @include: chimara/chimara-glk.h
28 * The ChimaraGlk widget opens and runs a Glk program. The program must be
29 * compiled as a plugin module, with a function <function>glk_main()</function>
30 * that the Glk library can hook into.
32 * On Linux systems, this is a file with a name like
33 * <filename>plugin.so</filename>. For portability, you can use libtool and
36 * pkglib_LTLIBRARIES = plugin.la
37 * plugin_la_SOURCES = plugin.c foo.c bar.c
38 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
40 * This will produce <filename>plugin.la</filename> which is a text file
41 * containing the correct plugin file to open (see the relevant section of the
43 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
44 * Libtool manual</ulink>).
47 typedef void (* glk_main_t) (void);
48 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
54 PROP_DEFAULT_FONT_DESCRIPTION,
55 PROP_MONOSPACE_FONT_DESCRIPTION,
69 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
71 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
74 chimara_glk_init(ChimaraGlk *self)
76 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
78 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
81 priv->interactive = TRUE;
82 priv->protect = FALSE;
83 priv->default_font_desc = pango_font_description_from_string("Sans");
84 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
85 priv->css_file = "style.css";
86 priv->default_styles = g_hash_table_new(g_str_hash, g_str_equal);
89 priv->event_queue = NULL;
90 priv->event_lock = NULL;
91 priv->event_queue_not_empty = NULL;
92 priv->event_queue_not_full = NULL;
93 priv->abort_lock = NULL;
94 priv->abort_signalled = FALSE;
95 priv->arrange_lock = NULL;
96 priv->rearranged = NULL;
97 priv->needs_rearrange = FALSE;
98 priv->ignore_next_arrange_event = FALSE;
99 priv->interrupt_handler = NULL;
100 priv->root_window = NULL;
101 priv->fileref_list = NULL;
102 priv->current_stream = NULL;
103 priv->stream_list = NULL;
105 priv->in_startup = FALSE;
106 priv->current_dir = NULL;
110 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
112 ChimaraGlk *glk = CHIMARA_GLK(object);
116 case PROP_INTERACTIVE:
117 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
120 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
122 case PROP_DEFAULT_FONT_DESCRIPTION:
123 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
125 case PROP_MONOSPACE_FONT_DESCRIPTION:
126 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
129 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
132 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
137 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
139 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
143 case PROP_INTERACTIVE:
144 g_value_set_boolean(value, priv->interactive);
147 g_value_set_boolean(value, priv->protect);
149 case PROP_DEFAULT_FONT_DESCRIPTION:
150 g_value_set_pointer(value, priv->default_font_desc);
152 case PROP_MONOSPACE_FONT_DESCRIPTION:
153 g_value_set_pointer(value, priv->monospace_font_desc);
156 g_value_set_uint(value, priv->spacing);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
164 chimara_glk_finalize(GObject *object)
166 ChimaraGlk *self = CHIMARA_GLK(object);
167 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
169 /* Free the event queue */
170 g_mutex_lock(priv->event_lock);
171 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
172 g_queue_free(priv->event_queue);
173 g_cond_free(priv->event_queue_not_empty);
174 g_cond_free(priv->event_queue_not_full);
175 priv->event_queue = NULL;
176 g_mutex_unlock(priv->event_lock);
177 g_mutex_free(priv->event_lock);
179 /* Free the abort signalling mechanism */
180 g_mutex_lock(priv->abort_lock);
181 /* Make sure no other thread is busy with this */
182 g_mutex_unlock(priv->abort_lock);
183 g_mutex_free(priv->abort_lock);
184 priv->abort_lock = NULL;
186 /* Free the window arrangement signalling */
187 g_mutex_lock(priv->arrange_lock);
188 g_cond_free(priv->rearranged);
189 g_mutex_unlock(priv->arrange_lock);
190 g_mutex_free(priv->arrange_lock);
191 priv->arrange_lock = NULL;
193 /* Free private data */
194 pango_font_description_free(priv->default_font_desc);
195 pango_font_description_free(priv->monospace_font_desc);
196 g_free(priv->current_dir);
197 g_hash_table_destroy(priv->default_styles);
199 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
202 /* Internal function: Recursively get the Glk window tree's size request */
204 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
206 if(win->type == wintype_Pair)
208 /* Get children's size requests */
209 GtkRequisition child1, child2;
210 request_recurse(win->window_node->children->data, &child1, spacing);
211 request_recurse(win->window_node->children->next->data, &child2, spacing);
213 glui32 division = win->split_method & winmethod_DivisionMask;
214 glui32 direction = win->split_method & winmethod_DirMask;
216 /* If the split is fixed, get the size of the fixed child */
217 if(division == winmethod_Fixed)
222 child1.width = win->key_window?
223 win->constraint_size * win->key_window->unit_width
226 case winmethod_Right:
227 child2.width = win->key_window?
228 win->constraint_size * win->key_window->unit_width
231 case winmethod_Above:
232 child1.height = win->key_window?
233 win->constraint_size * win->key_window->unit_height
236 case winmethod_Below:
237 child2.height = win->key_window?
238 win->constraint_size * win->key_window->unit_height
244 /* Add the children's requests */
248 case winmethod_Right:
249 requisition->width = child1.width + child2.width + spacing;
250 requisition->height = MAX(child1.height, child2.height);
252 case winmethod_Above:
253 case winmethod_Below:
254 requisition->width = MAX(child1.width, child2.width);
255 requisition->height = child1.height + child2.height + spacing;
260 /* For non-pair windows, just use the size that GTK requests */
262 gtk_widget_size_request(win->frame, requisition);
265 /* Overrides gtk_widget_size_request */
267 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
269 g_return_if_fail(widget);
270 g_return_if_fail(requisition);
271 g_return_if_fail(CHIMARA_IS_GLK(widget));
273 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
275 /* For now, just pass the size request on to the root Glk window */
276 if(priv->root_window)
278 request_recurse(priv->root_window->data, requisition, priv->spacing);
279 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
280 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
284 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
285 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
289 /* Recursively give the Glk windows their allocated space. Returns a window
290 containing all children of this window that must be redrawn, or NULL if there
291 are no children that require redrawing. */
293 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
295 if(win->type == wintype_Pair)
297 glui32 division = win->split_method & winmethod_DivisionMask;
298 glui32 direction = win->split_method & winmethod_DirMask;
300 /* If the space gets too small to honor the spacing property, then just
301 ignore spacing in this window and below. */
302 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
303 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
306 GtkAllocation child1, child2;
307 child1.x = allocation->x;
308 child1.y = allocation->y;
310 if(division == winmethod_Fixed)
312 /* If the key window has been closed, then default to 0; otherwise
313 use the key window to determine the size */
317 child1.width = win->key_window?
318 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
321 case winmethod_Right:
322 child2.width = win->key_window?
323 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
326 case winmethod_Above:
327 child1.height = win->key_window?
328 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
331 case winmethod_Below:
332 child2.height = win->key_window?
333 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
338 else /* proportional */
340 gdouble fraction = win->constraint_size / 100.0;
344 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
346 case winmethod_Right:
347 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
349 case winmethod_Above:
350 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
352 case winmethod_Below:
353 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
358 /* Fill in the rest of the size requisitions according to the child specified above */
362 child2.width = MAX(0, allocation->width - spacing - child1.width);
363 child2.x = child1.x + child1.width + spacing;
365 child1.height = child2.height = allocation->height;
367 case winmethod_Right:
368 child1.width = MAX(0, allocation->width - spacing - child2.width);
369 child2.x = child1.x + child1.width + spacing;
371 child1.height = child2.height = allocation->height;
373 case winmethod_Above:
374 child2.height = MAX(0, allocation->height - spacing - child1.height);
376 child2.y = child1.y + child1.height + spacing;
377 child1.width = child2.width = allocation->width;
379 case winmethod_Below:
380 child1.height = MAX(0, allocation->height - spacing - child2.height);
382 child2.y = child1.y + child1.height + spacing;
383 child1.width = child2.width = allocation->width;
388 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
389 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
397 else if(win->type == wintype_TextGrid)
399 /* Pass the size allocation on to the framing widget */
400 gtk_widget_size_allocate(win->frame, allocation);
401 /* It says in the spec that when a text grid window is resized smaller,
402 the bottom or right area is thrown away; when it is resized larger, the
403 bottom or right area is filled with blanks. */
404 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
405 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
407 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
408 GtkTextIter start, end;
410 for(line = 0; line < win->height; line++)
412 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
413 /* If this line is going to fall off the bottom, delete it */
414 if(line >= newheight)
417 gtk_text_iter_forward_to_line_end(&end);
418 gtk_text_iter_forward_char(&end);
419 gtk_text_buffer_delete(textbuffer, &start, &end);
422 /* If this line is not long enough, add spaces on the end */
423 if(newwidth > win->width)
425 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
426 gtk_text_iter_forward_to_line_end(&start);
427 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
430 /* But if it's too long, delete characters from the end */
431 else if(newwidth < win->width)
434 gtk_text_iter_forward_chars(&start, newwidth);
435 gtk_text_iter_forward_to_line_end(&end);
436 gtk_text_buffer_delete(textbuffer, &start, &end);
438 /* Note: if the widths are equal, do nothing */
440 /* Add blank lines if there aren't enough lines to fit the new size */
441 if(newheight > win->height)
443 gchar *blanks = g_strnfill(win->width, ' ');
444 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
446 for(count = 0; count < newheight - win->height; count++)
447 blanklines[count] = blanks;
448 blanklines[newheight - win->height] = NULL;
449 gchar *text = g_strjoinv("\n", blanklines);
450 g_free(blanklines); /* not g_strfreev() */
453 gtk_text_buffer_get_end_iter(textbuffer, &start);
454 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
455 gtk_text_buffer_insert(textbuffer, &start, text, -1);
459 gboolean arrange = !(win->width == newwidth && win->height == newheight);
460 win->width = newwidth;
461 win->height = newheight;
462 return arrange? win : NULL;
465 /* For non-pair, non-text-grid windows, just give them the size */
466 gtk_widget_size_allocate(win->frame, allocation);
470 /* Overrides gtk_widget_size_allocate */
472 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
474 g_return_if_fail(widget);
475 g_return_if_fail(allocation);
476 g_return_if_fail(CHIMARA_IS_GLK(widget));
478 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
480 widget->allocation = *allocation;
482 if(priv->root_window) {
484 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
485 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
486 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
487 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
488 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
490 /* arrange points to a window that contains all text grid and graphics
491 windows which have been resized */
492 g_mutex_lock(priv->arrange_lock);
493 if(!priv->ignore_next_arrange_event)
496 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
499 priv->ignore_next_arrange_event = FALSE;
500 priv->needs_rearrange = FALSE;
501 g_cond_signal(priv->rearranged);
502 g_mutex_unlock(priv->arrange_lock);
506 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
508 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
510 if(win->type == wintype_Pair)
512 forall_recurse(win->window_node->children->data, callback, callback_data);
513 forall_recurse(win->window_node->children->next->data, callback, callback_data);
516 (*callback)(win->frame, callback_data);
519 /* Overrides gtk_container_forall */
521 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
523 g_return_if_fail(container);
524 g_return_if_fail(CHIMARA_IS_GLK(container));
526 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
528 /* All the children are "internal" */
529 if(!include_internals)
532 if(priv->root_window)
533 forall_recurse(priv->root_window->data, callback, callback_data);
537 chimara_glk_stopped(ChimaraGlk *self)
539 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
541 /* Free the plugin */
542 if( priv->program && !g_module_close(priv->program) )
543 g_warning( "Error closing module: %s", g_module_error() );
547 chimara_glk_started(ChimaraGlk *self)
549 /* TODO: Add default signal handler implementation here */
553 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
555 /* TODO: Add default signal handler */
559 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
561 /* TODO: Add default signal handler */
565 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
567 /* TODO: Add default signal handler */
570 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
571 #ifndef G_PARAM_STATIC_STRINGS
572 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
576 chimara_glk_class_init(ChimaraGlkClass *klass)
578 /* Override methods of parent classes */
579 GObjectClass *object_class = G_OBJECT_CLASS(klass);
580 object_class->set_property = chimara_glk_set_property;
581 object_class->get_property = chimara_glk_get_property;
582 object_class->finalize = chimara_glk_finalize;
584 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
585 widget_class->size_request = chimara_glk_size_request;
586 widget_class->size_allocate = chimara_glk_size_allocate;
588 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
589 container_class->forall = chimara_glk_forall;
592 klass->stopped = chimara_glk_stopped;
593 klass->started = chimara_glk_started;
594 klass->char_input = chimara_glk_char_input;
595 klass->line_input = chimara_glk_line_input;
596 klass->text_buffer_output = chimara_glk_text_buffer_output;
598 * ChimaraGlk::stopped:
599 * @glk: The widget that received the signal
601 * The ::stopped signal is emitted when the a Glk program finishes
602 * executing in the widget, whether it ended normally, or was interrupted.
604 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
605 G_OBJECT_CLASS_TYPE(klass), 0,
606 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
607 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
609 * ChimaraGlk::started:
610 * @glk: The widget that received the signal
612 * The ::started signal is emitted when a Glk program starts executing in
615 chimara_glk_signals[STARTED] = g_signal_new ("started",
616 G_OBJECT_CLASS_TYPE (klass), 0,
617 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
618 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
620 chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
621 G_OBJECT_CLASS_TYPE(klass), 0,
622 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
623 chimara_marshal_VOID__UINT_UINT,
624 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
626 chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
627 G_OBJECT_CLASS_TYPE(klass), 0,
628 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
629 chimara_marshal_VOID__UINT_STRING,
630 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
632 chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
633 G_OBJECT_CLASS_TYPE(klass), 0,
634 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
635 chimara_marshal_VOID__UINT_STRING,
636 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
640 * ChimaraGlk:interactive:
642 * Sets whether the widget is interactive. A Glk widget is normally
643 * interactive, but in non-interactive mode, keyboard and mouse input are
644 * ignored and the Glk program is controlled by chimara_glk_feed_text().
645 * <quote>More</quote> prompts when a lot of text is printed to a text
646 * buffer are also disabled. This is typically used when you wish to control
647 * an interpreter program by feeding it a predefined list of commands.
649 g_object_class_install_property( object_class, PROP_INTERACTIVE,
650 g_param_spec_boolean("interactive", _("Interactive"),
651 _("Whether user input is expected in the Glk program"),
653 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
656 * ChimaraGlk:protect:
658 * Sets whether the Glk program is allowed to do file operations. In protect
659 * mode, all file operations will fail.
661 g_object_class_install_property(object_class, PROP_PROTECT,
662 g_param_spec_boolean("protect", _("Protected"),
663 _("Whether the Glk program is barred from doing file operations"),
665 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
667 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
668 initialize them with NULL */
670 * ChimaraGlk:default-font-description:
672 * Pointer to a #PangoFontDescription describing the default proportional
673 * font, to be used in text buffer windows for example.
675 * Default value: font description created from the string
676 * <quote>Sans</quote>
678 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
679 g_param_spec_pointer("default-font-description", _("Default Font"),
680 _("Font description of the default proportional font"),
681 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
684 * ChimaraGlk:monospace-font-description:
686 * Pointer to a #PangoFontDescription describing the default monospace font,
687 * to be used in text grid windows and %style_Preformatted, for example.
689 * Default value: font description created from the string
690 * <quote>Monospace</quote>
692 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
693 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
694 _("Font description of the default monospace font"),
695 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
698 * ChimaraGlk:spacing:
700 * The amount of space between the Glk windows.
702 g_object_class_install_property(object_class, PROP_SPACING,
703 g_param_spec_uint("spacing", _("Spacing"),
704 _("The amount of space between Glk windows"),
706 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
709 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
712 /* PUBLIC FUNCTIONS */
717 * Creates and initializes a new #ChimaraGlk widget.
719 * Return value: a #ChimaraGlk widget, with a floating reference.
722 chimara_glk_new(void)
724 /* This is a library entry point; initialize the library */
727 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
728 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
730 priv->event_queue = g_queue_new();
731 priv->event_lock = g_mutex_new();
732 priv->event_queue_not_empty = g_cond_new();
733 priv->event_queue_not_full = g_cond_new();
734 priv->abort_lock = g_mutex_new();
735 priv->arrange_lock = g_mutex_new();
736 priv->rearranged = g_cond_new();
738 return GTK_WIDGET(self);
742 * chimara_glk_set_interactive:
743 * @glk: a #ChimaraGlk widget
744 * @interactive: whether the widget should expect user input
746 * Sets the #ChimaraGlk:interactive property of @glk.
749 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
751 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
753 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
754 priv->interactive = interactive;
758 * chimara_glk_get_interactive:
759 * @glk: a #ChimaraGlk widget
761 * Returns whether @glk is interactive (expecting user input). See
762 * #ChimaraGlk:interactive.
764 * Return value: %TRUE if @glk is interactive.
767 chimara_glk_get_interactive(ChimaraGlk *glk)
769 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
771 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
772 return priv->interactive;
776 * chimara_glk_set_protect:
777 * @glk: a #ChimaraGlk widget
778 * @protect: whether the widget should allow the Glk program to do file
781 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
782 * program is not allowed to do file operations.
785 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
787 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
789 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
790 priv->protect = protect;
794 * chimara_glk_get_protect:
795 * @glk: a #ChimaraGlk widget
797 * Returns whether @glk is in protect mode (banned from doing file operations).
798 * See #ChimaraGlk:protect.
800 * Return value: %TRUE if @glk is in protect mode.
803 chimara_glk_get_protect(ChimaraGlk *glk)
805 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
807 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
808 return priv->protect;
812 * chimara_glk_set_default_font_description:
813 * @glk: a #ChimaraGlk widget
814 * @font: a #PangoFontDescription
816 * Sets @glk's default proportional font. See
817 * #ChimaraGlk:default-font-description.
820 chimara_glk_set_default_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->default_font_desc);
827 priv->default_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_default_font_string:
834 * @glk: a #ChimaraGlk widget
835 * @font: string representation of a font description
837 * Sets @glk's default proportional font according to the string @font, which
838 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
839 * [<replaceable>STYLE-OPTIONS</replaceable>]
840 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
841 * Italic 12</quote> or <quote>Sans</quote>. See
842 * #ChimaraGlk:default-font-description.
845 chimara_glk_set_default_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->default_font_desc);
855 priv->default_font_desc = fontdesc;
857 /* TODO: Apply the font description to all the windows and recalculate the sizes */
861 * chimara_glk_get_default_font_description:
862 * @glk: a #ChimaraGlk widget
864 * Returns @glk's default proportional 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_default_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->default_font_desc);
879 * chimara_glk_set_monospace_font_description:
880 * @glk: a #ChimaraGlk widget
881 * @font: a #PangoFontDescription
883 * Sets @glk's default monospace font. See
884 * #ChimaraGlk:monospace-font-description.
887 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
889 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
890 g_return_if_fail(font);
892 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
893 pango_font_description_free(priv->monospace_font_desc);
894 priv->monospace_font_desc = pango_font_description_copy(font);
896 /* TODO: Apply the font description to all the windows and recalculate the sizes */
900 * chimara_glk_set_monospace_font_string:
901 * @glk: a #ChimaraGlk widget
902 * @font: string representation of a font description
904 * Sets @glk's default monospace font according to the string @font, which must
905 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
906 * [<replaceable>STYLE-OPTIONS</replaceable>]
907 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
908 * Bold 12</quote> or <quote>Monospace</quote>. See
909 * #ChimaraGlk:monospace-font-description.
912 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
914 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
915 g_return_if_fail(font || *font);
917 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
918 g_return_if_fail(fontdesc);
920 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
921 pango_font_description_free(priv->monospace_font_desc);
922 priv->monospace_font_desc = fontdesc;
924 /* TODO: Apply the font description to all the windows and recalculate the sizes */
928 * chimara_glk_get_monospace_font_description:
929 * @glk: a #ChimaraGlk widget
931 * Returns @glk's default monospace font.
933 * Return value: a newly-allocated #PangoFontDescription which must be freed
934 * using pango_font_description_free(), or %NULL on error.
936 PangoFontDescription *
937 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
939 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
941 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
942 return pango_font_description_copy(priv->monospace_font_desc);
946 * chimara_glk_set_spacing:
947 * @glk: a #ChimaraGlk widget
948 * @spacing: the number of pixels to put between Glk windows
950 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
951 * pixels between Glk windows.
954 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
956 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
958 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
959 priv->spacing = spacing;
963 * chimara_glk_get_spacing:
964 * @glk: a #ChimaraGlk widget
966 * Gets the value set by chimara_glk_set_spacing().
968 * Return value: pixels of spacing between Glk windows
971 chimara_glk_get_spacing(ChimaraGlk *glk)
973 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
975 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
976 return priv->spacing;
981 glkunix_startup_code_t glkunix_startup_code;
982 glkunix_startup_t args;
983 ChimaraGlkPrivate *glk_data;
986 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
988 glk_enter(struct StartupData *startup)
990 extern GPrivate *glk_data_key;
991 g_private_set(glk_data_key, startup->glk_data);
993 /* Run startup function */
994 if(startup->glkunix_startup_code) {
995 startup->glk_data->in_startup = TRUE;
996 int result = startup->glkunix_startup_code(&startup->args);
997 startup->glk_data->in_startup = FALSE;
1000 while(i < startup->args.argc)
1001 g_free(startup->args.argv[i++]);
1002 g_free(startup->args.argv);
1008 /* Run main function */
1009 g_signal_emit_by_name(startup->glk_data->self, "started");
1010 (startup->glk_main)();
1011 g_signal_emit_by_name(startup->glk_data->self, "stopped");
1012 g_slice_free(struct StartupData, startup);
1018 * @glk: a #ChimaraGlk widget
1019 * @plugin: path to a plugin module compiled with <filename
1020 * class="header">glk.h</filename>
1021 * @argc: Number of command line arguments in @argv
1022 * @argv: Array of command line arguments to pass to the plugin
1023 * @error: location to store a <link linkend="glib-GError">GError</link>, or
1026 * Opens a Glk program compiled as a plugin. Sorts out its command line
1027 * arguments from #glkunix_arguments, calls its startup function
1028 * glkunix_startup_code(), and then calls its main function glk_main() in
1029 * a separate thread. On failure, returns %FALSE and sets @error.
1031 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1032 * glkunix_startup_code() are optional.
1034 * Return value: %TRUE if the Glk program was started successfully.
1037 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
1039 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1040 g_return_val_if_fail(plugin, FALSE);
1042 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1043 struct StartupData *startup = g_slice_new0(struct StartupData);
1045 /* Open the module to run */
1046 g_assert( g_module_supported() );
1047 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1051 g_warning( "Error opening module: %s", g_module_error() );
1054 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1056 g_warning( "Error finding glk_main(): %s", g_module_error() );
1060 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1062 glkunix_argumentlist_t *glkunix_arguments;
1064 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1065 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1067 /* arguments could not be parsed, so create data ourselves */
1068 startup->args.argc = 1;
1069 startup->args.argv = g_new0(gchar *, 1);
1072 /* Set the program name */
1073 startup->args.argv[0] = g_strdup(plugin);
1075 startup->glk_data = priv;
1077 /* Run in a separate thread */
1078 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1080 return !(priv->thread == NULL);
1085 * @glk: a #ChimaraGlk widget
1087 * Signals the Glk program running in @glk to abort. Note that if the program is
1088 * caught in an infinite loop in which glk_tick() is not called, this may not
1092 chimara_glk_stop(ChimaraGlk *glk)
1094 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1095 /* TODO: check if glk is actually running a program */
1096 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1097 if(priv->abort_lock) {
1098 g_mutex_lock(priv->abort_lock);
1099 priv->abort_signalled = TRUE;
1100 g_mutex_unlock(priv->abort_lock);
1101 /* Stop blocking on the event queue condition */
1102 event_throw(glk, evtype_Abort, NULL, 0, 0);
1108 * @glk: a #ChimaraGlk widget
1110 * Holds up the main thread and waits for the Glk program running in @glk to
1114 chimara_glk_wait(ChimaraGlk *glk)
1116 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1118 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1119 g_thread_join(priv->thread);