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"
21 #define CHIMARA_GLK_MIN_WIDTH 0
22 #define CHIMARA_GLK_MIN_HEIGHT 0
26 * @short_description: Widget which executes a Glk program
27 * @stability: Unstable
28 * @include: chimara/chimara-glk.h
30 * The #ChimaraGlk widget opens and runs a Glk program. The program must be
31 * compiled as a plugin module, with a function <function>glk_main()</function>
32 * that the Glk library can hook into.
34 * On Linux systems, this is a file with a name like
35 * <filename>plugin.so</filename>. For portability, you can use libtool and
38 * pkglib_LTLIBRARIES = plugin.la
39 * plugin_la_SOURCES = plugin.c foo.c bar.c
40 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
42 * This will produce <filename>plugin.la</filename> which is a text file
43 * containing the correct plugin file to open (see the relevant section of the
45 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
46 * Libtool manual</ulink>).
49 typedef void (* glk_main_t) (void);
50 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
56 PROP_DEFAULT_FONT_DESCRIPTION,
57 PROP_MONOSPACE_FONT_DESCRIPTION,
72 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
74 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
77 chimara_glk_init(ChimaraGlk *self)
79 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
81 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
84 priv->interactive = TRUE;
85 priv->protect = FALSE;
86 priv->default_font_desc = pango_font_description_from_string("Serif");
87 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
88 priv->css_file = "style.css";
89 priv->default_styles = g_new0(StyleSet,1);
90 priv->current_styles = g_new0(StyleSet,1);
91 priv->pager_attr_list = pango_attr_list_new();
92 priv->style_initialized = FALSE;
93 priv->final_message = g_strdup("[ The game has finished ]");
94 priv->running = FALSE;
97 priv->event_queue = g_queue_new();
98 priv->event_lock = g_mutex_new();
99 priv->event_queue_not_empty = g_cond_new();
100 priv->event_queue_not_full = g_cond_new();
101 priv->abort_lock = g_mutex_new();
102 priv->abort_signalled = FALSE;
103 priv->shutdown_lock = g_mutex_new();
104 priv->shutdown_key_pressed = g_cond_new();
105 priv->arrange_lock = g_mutex_new();
106 priv->rearranged = g_cond_new();
107 priv->needs_rearrange = FALSE;
108 priv->ignore_next_arrange_event = FALSE;
109 priv->char_input_queue = g_async_queue_new();
110 priv->line_input_queue = g_async_queue_new();
111 /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
112 priv->resource_lock = g_mutex_new();
113 priv->resource_loaded = g_cond_new();
114 priv->resource_info_available = g_cond_new();
115 priv->image_cache = NULL;
116 priv->interrupt_handler = NULL;
117 priv->root_window = NULL;
118 priv->fileref_list = NULL;
119 priv->current_stream = NULL;
120 priv->stream_list = NULL;
122 priv->in_startup = FALSE;
123 priv->current_dir = NULL;
127 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
129 ChimaraGlk *glk = CHIMARA_GLK(object);
133 case PROP_INTERACTIVE:
134 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
137 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
139 case PROP_DEFAULT_FONT_DESCRIPTION:
140 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
142 case PROP_MONOSPACE_FONT_DESCRIPTION:
143 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
146 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
154 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
156 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
160 case PROP_INTERACTIVE:
161 g_value_set_boolean(value, priv->interactive);
164 g_value_set_boolean(value, priv->protect);
166 case PROP_DEFAULT_FONT_DESCRIPTION:
167 g_value_set_pointer(value, priv->default_font_desc);
169 case PROP_MONOSPACE_FONT_DESCRIPTION:
170 g_value_set_pointer(value, priv->monospace_font_desc);
173 g_value_set_uint(value, priv->spacing);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
181 chimara_glk_finalize(GObject *object)
183 ChimaraGlk *self = CHIMARA_GLK(object);
184 CHIMARA_GLK_USE_PRIVATE(self, priv);
186 /* Free widget properties */
187 pango_font_description_free(priv->default_font_desc);
188 pango_font_description_free(priv->monospace_font_desc);
189 g_free(priv->final_message);
191 g_hash_table_destroy(priv->default_styles->text_buffer);
192 g_hash_table_destroy(priv->default_styles->text_grid);
193 g_hash_table_destroy(priv->current_styles->text_buffer);
194 g_hash_table_destroy(priv->current_styles->text_grid);
195 pango_attr_list_unref(priv->pager_attr_list);
196 priv->style_initialized = FALSE;
198 /* Free the event queue */
199 g_mutex_lock(priv->event_lock);
200 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
201 g_queue_free(priv->event_queue);
202 g_cond_free(priv->event_queue_not_empty);
203 g_cond_free(priv->event_queue_not_full);
204 priv->event_queue = NULL;
205 g_mutex_unlock(priv->event_lock);
206 g_mutex_free(priv->event_lock);
207 /* Free the abort signaling mechanism */
208 g_mutex_lock(priv->abort_lock);
209 /* Make sure no other thread is busy with this */
210 g_mutex_unlock(priv->abort_lock);
211 g_mutex_free(priv->abort_lock);
212 priv->abort_lock = NULL;
213 /* Free the shutdown keypress signaling mechanism */
214 g_mutex_lock(priv->shutdown_lock);
215 g_cond_free(priv->shutdown_key_pressed);
216 g_mutex_unlock(priv->shutdown_lock);
217 priv->shutdown_lock = NULL;
218 /* Free the window arrangement signaling */
219 g_mutex_lock(priv->arrange_lock);
220 g_cond_free(priv->rearranged);
221 g_mutex_unlock(priv->arrange_lock);
222 g_mutex_free(priv->arrange_lock);
223 priv->arrange_lock = NULL;
224 g_mutex_lock(priv->resource_lock);
225 g_cond_free(priv->resource_loaded);
226 g_cond_free(priv->resource_info_available);
227 g_mutex_unlock(priv->resource_lock);
228 g_mutex_free(priv->resource_lock);
229 g_slist_foreach(priv->image_cache, (GFunc)clear_image_cache, NULL);
230 g_slist_free(priv->image_cache);
231 /* Unref input queues (this should destroy them since any Glk thread has stopped by now */
232 g_async_queue_unref(priv->char_input_queue);
233 g_async_queue_unref(priv->line_input_queue);
235 /* Free other stuff */
236 if(priv->current_dir)
237 g_free(priv->current_dir);
239 /* Chain up to parent */
240 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
243 /* Internal function: Recursively get the Glk window tree's size request */
245 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
247 if(win->type == wintype_Pair)
249 /* Get children's size requests */
250 GtkRequisition child1, child2;
251 request_recurse(win->window_node->children->data, &child1, spacing);
252 request_recurse(win->window_node->children->next->data, &child2, spacing);
254 glui32 division = win->split_method & winmethod_DivisionMask;
255 glui32 direction = win->split_method & winmethod_DirMask;
257 /* If the split is fixed, get the size of the fixed child */
258 if(division == winmethod_Fixed)
263 child1.width = win->key_window?
264 win->constraint_size * win->key_window->unit_width
267 case winmethod_Right:
268 child2.width = win->key_window?
269 win->constraint_size * win->key_window->unit_width
272 case winmethod_Above:
273 child1.height = win->key_window?
274 win->constraint_size * win->key_window->unit_height
277 case winmethod_Below:
278 child2.height = win->key_window?
279 win->constraint_size * win->key_window->unit_height
285 /* Add the children's requests */
289 case winmethod_Right:
290 requisition->width = child1.width + child2.width + spacing;
291 requisition->height = MAX(child1.height, child2.height);
293 case winmethod_Above:
294 case winmethod_Below:
295 requisition->width = MAX(child1.width, child2.width);
296 requisition->height = child1.height + child2.height + spacing;
301 /* For non-pair windows, just use the size that GTK requests */
303 gtk_widget_size_request(win->frame, requisition);
306 /* Overrides gtk_widget_size_request */
308 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
310 g_return_if_fail(widget);
311 g_return_if_fail(requisition);
312 g_return_if_fail(CHIMARA_IS_GLK(widget));
314 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
316 /* For now, just pass the size request on to the root Glk window */
317 if(priv->root_window)
319 request_recurse(priv->root_window->data, requisition, priv->spacing);
320 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
321 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
325 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
326 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
330 /* Recursively give the Glk windows their allocated space. Returns a window
331 containing all children of this window that must be redrawn, or NULL if there
332 are no children that require redrawing. */
334 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
336 if(win->type == wintype_Pair)
338 glui32 division = win->split_method & winmethod_DivisionMask;
339 glui32 direction = win->split_method & winmethod_DirMask;
341 /* If the space gets too small to honor the spacing property, then just
342 ignore spacing in this window and below. */
343 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
344 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
347 GtkAllocation child1, child2;
348 child1.x = allocation->x;
349 child1.y = allocation->y;
351 if(division == winmethod_Fixed)
353 /* If the key window has been closed, then default to 0; otherwise
354 use the key window to determine the size */
358 child1.width = win->key_window?
359 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
362 case winmethod_Right:
363 child2.width = win->key_window?
364 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
367 case winmethod_Above:
368 child1.height = win->key_window?
369 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
372 case winmethod_Below:
373 child2.height = win->key_window?
374 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
379 else /* proportional */
381 gdouble fraction = win->constraint_size / 100.0;
385 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
387 case winmethod_Right:
388 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
390 case winmethod_Above:
391 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
393 case winmethod_Below:
394 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
399 /* Fill in the rest of the size requisitions according to the child specified above */
403 child2.width = MAX(0, allocation->width - spacing - child1.width);
404 child2.x = child1.x + child1.width + spacing;
406 child1.height = child2.height = allocation->height;
408 case winmethod_Right:
409 child1.width = MAX(0, allocation->width - spacing - child2.width);
410 child2.x = child1.x + child1.width + spacing;
412 child1.height = child2.height = allocation->height;
414 case winmethod_Above:
415 child2.height = MAX(0, allocation->height - spacing - child1.height);
417 child2.y = child1.y + child1.height + spacing;
418 child1.width = child2.width = allocation->width;
420 case winmethod_Below:
421 child1.height = MAX(0, allocation->height - spacing - child2.height);
423 child2.y = child1.y + child1.height + spacing;
424 child1.width = child2.width = allocation->width;
429 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
430 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
438 else if(win->type == wintype_TextGrid)
440 /* Pass the size allocation on to the framing widget */
441 gtk_widget_size_allocate(win->frame, allocation);
442 /* It says in the spec that when a text grid window is resized smaller,
443 the bottom or right area is thrown away; when it is resized larger, the
444 bottom or right area is filled with blanks. */
445 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
446 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
448 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
449 GtkTextIter start, end;
451 for(line = 0; line < win->height; line++)
453 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
454 /* If this line is going to fall off the bottom, delete it */
455 if(line >= newheight)
458 gtk_text_iter_forward_to_line_end(&end);
459 gtk_text_iter_forward_char(&end);
460 gtk_text_buffer_delete(textbuffer, &start, &end);
463 /* If this line is not long enough, add spaces on the end */
464 if(newwidth > win->width)
466 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
467 gtk_text_iter_forward_to_line_end(&start);
468 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
471 /* But if it's too long, delete characters from the end */
472 else if(newwidth < win->width)
475 gtk_text_iter_forward_chars(&start, newwidth);
476 gtk_text_iter_forward_to_line_end(&end);
477 gtk_text_buffer_delete(textbuffer, &start, &end);
479 /* Note: if the widths are equal, do nothing */
481 /* Add blank lines if there aren't enough lines to fit the new size */
482 if(newheight > win->height)
484 gchar *blanks = g_strnfill(win->width, ' ');
485 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
487 for(count = 0; count < newheight - win->height; count++)
488 blanklines[count] = blanks;
489 blanklines[newheight - win->height] = NULL;
490 gchar *text = g_strjoinv("\n", blanklines);
491 g_free(blanklines); /* not g_strfreev() */
494 gtk_text_buffer_get_end_iter(textbuffer, &start);
495 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
496 gtk_text_buffer_insert(textbuffer, &start, text, -1);
500 gboolean arrange = !(win->width == newwidth && win->height == newheight);
501 win->width = newwidth;
502 win->height = newheight;
503 return arrange? win : NULL;
506 /* For non-pair, non-text-grid windows, just give them the size */
507 gtk_widget_size_allocate(win->frame, allocation);
511 /* Overrides gtk_widget_size_allocate */
513 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
515 g_return_if_fail(widget);
516 g_return_if_fail(allocation);
517 g_return_if_fail(CHIMARA_IS_GLK(widget));
519 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
521 widget->allocation = *allocation;
523 if(priv->root_window) {
525 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
526 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
527 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
528 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
529 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
531 /* arrange points to a window that contains all text grid and graphics
532 windows which have been resized */
533 g_mutex_lock(priv->arrange_lock);
534 if(!priv->ignore_next_arrange_event)
537 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
540 priv->ignore_next_arrange_event = FALSE;
541 priv->needs_rearrange = FALSE;
542 g_cond_signal(priv->rearranged);
543 g_mutex_unlock(priv->arrange_lock);
547 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
549 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
551 if(win->type == wintype_Pair)
553 forall_recurse(win->window_node->children->data, callback, callback_data);
554 forall_recurse(win->window_node->children->next->data, callback, callback_data);
557 (*callback)(win->frame, callback_data);
560 /* Overrides gtk_container_forall */
562 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
564 g_return_if_fail(container);
565 g_return_if_fail(CHIMARA_IS_GLK(container));
567 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
569 /* All the children are "internal" */
570 if(!include_internals)
573 if(priv->root_window)
574 forall_recurse(priv->root_window->data, callback, callback_data);
578 chimara_glk_stopped(ChimaraGlk *self)
580 CHIMARA_GLK_USE_PRIVATE(self, priv);
581 priv->running = FALSE;
583 /* Free the plugin */
584 if( priv->program && !g_module_close(priv->program) )
585 g_warning( "Error closing module: %s", g_module_error() );
586 priv->program = NULL;
590 chimara_glk_started(ChimaraGlk *self)
592 CHIMARA_GLK_USE_PRIVATE(self, priv);
593 priv->running = TRUE;
597 chimara_glk_waiting(ChimaraGlk *self)
599 /* Default signal handler */
603 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
605 /* Default signal handler */
609 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
611 /* Default signal handler */
615 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
617 /* Default signal handler */
620 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
621 #ifndef G_PARAM_STATIC_STRINGS
623 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
624 #if GTK_CHECK_VERSION(2,8,0)
625 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
627 #define G_PARAM_STATIC_STRINGS (0)
633 chimara_glk_class_init(ChimaraGlkClass *klass)
635 /* Override methods of parent classes */
636 GObjectClass *object_class = G_OBJECT_CLASS(klass);
637 object_class->set_property = chimara_glk_set_property;
638 object_class->get_property = chimara_glk_get_property;
639 object_class->finalize = chimara_glk_finalize;
641 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
642 widget_class->size_request = chimara_glk_size_request;
643 widget_class->size_allocate = chimara_glk_size_allocate;
645 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
646 container_class->forall = chimara_glk_forall;
649 klass->stopped = chimara_glk_stopped;
650 klass->started = chimara_glk_started;
651 klass->waiting = chimara_glk_waiting;
652 klass->char_input = chimara_glk_char_input;
653 klass->line_input = chimara_glk_line_input;
654 klass->text_buffer_output = chimara_glk_text_buffer_output;
656 * ChimaraGlk::stopped:
657 * @glk: The widget that received the signal
659 * Emitted when the a Glk program finishes executing in the widget, whether
660 * it ended normally, or was interrupted.
662 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
663 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
664 /* FIXME: Should be G_SIGNAL_RUN_CLEANUP but that segfaults??! */
665 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
666 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
668 * ChimaraGlk::started:
669 * @glk: The widget that received the signal
671 * Emitted when a Glk program starts executing in the widget.
673 chimara_glk_signals[STARTED] = g_signal_new ("started",
674 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
675 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
676 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
678 * ChimaraGlk::waiting:
679 * @glk: The widget that received the signal
681 * Emitted when glk_select() is called by the Glk program and the event
682 * queue is empty, which means that the widget is waiting for input.
684 chimara_glk_signals[WAITING] = g_signal_new("waiting",
685 G_OBJECT_CLASS_TYPE(klass), 0,
686 G_STRUCT_OFFSET(ChimaraGlkClass, waiting), NULL, NULL,
687 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
689 * ChimaraGlk::char-input:
690 * @glk: The widget that received the signal
691 * @window_rock: The rock value of the window that received character input
692 * (see <link linkend="chimara-Rocks">Rocks</link>)
693 * @keysym: The key that was typed, in the form of a key symbol from
694 * <filename class="headerfile">gdk/gdkkeysyms.h</filename>
696 * Emitted when a Glk window receives character input.
698 chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
699 G_OBJECT_CLASS_TYPE(klass), 0,
700 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
701 chimara_marshal_VOID__UINT_UINT,
702 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
704 * ChimaraGlk::line-input:
705 * @glk: The widget that received the signal
706 * @window_rock: The rock value of the window that received line input (see
707 * <link linkend="chimara-Rocks">Rocks</link>)
708 * @text: The text that was typed
710 * Emitted when a Glk window receives line input.
712 chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
713 G_OBJECT_CLASS_TYPE(klass), 0,
714 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
715 chimara_marshal_VOID__UINT_STRING,
716 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
718 * ChimaraGlk::text-buffer-output:
719 * @glk: The widget that received the signal
720 * @window_rock: The rock value of the window that was printed to (see <link
721 * linkend="chimara-Rocks">Rocks</link>)
723 * Emitted when text is printed to a text buffer window.
725 chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
726 G_OBJECT_CLASS_TYPE(klass), 0,
727 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
728 chimara_marshal_VOID__UINT_STRING,
729 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
733 * ChimaraGlk:interactive:
735 * Sets whether the widget is interactive. A Glk widget is normally
736 * interactive, but in non-interactive mode, keyboard and mouse input are
737 * ignored and the Glk program is controlled by chimara_glk_feed_text().
738 * <quote>More</quote> prompts when a lot of text is printed to a text
739 * buffer are also disabled. This is typically used when you wish to control
740 * an interpreter program by feeding it a predefined list of commands.
742 g_object_class_install_property( object_class, PROP_INTERACTIVE,
743 g_param_spec_boolean("interactive", _("Interactive"),
744 _("Whether user input is expected in the Glk program"),
746 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
749 * ChimaraGlk:protect:
751 * Sets whether the Glk program is allowed to do file operations. In protect
752 * mode, all file operations will fail.
754 g_object_class_install_property(object_class, PROP_PROTECT,
755 g_param_spec_boolean("protect", _("Protected"),
756 _("Whether the Glk program is barred from doing file operations"),
758 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
760 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
761 initialize them with NULL */
763 * ChimaraGlk:default-font-description:
765 * Pointer to a #PangoFontDescription describing the default proportional
766 * font, to be used in text buffer windows for example.
768 * Default value: font description created from the string
769 * <quote>Sans</quote>
771 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
772 g_param_spec_pointer("default-font-description", _("Default Font"),
773 _("Font description of the default proportional font"),
774 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
777 * ChimaraGlk:monospace-font-description:
779 * Pointer to a #PangoFontDescription describing the default monospace font,
780 * to be used in text grid windows and %style_Preformatted, for example.
782 * Default value: font description created from the string
783 * <quote>Monospace</quote>
785 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
786 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
787 _("Font description of the default monospace font"),
788 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
791 * ChimaraGlk:spacing:
793 * The amount of space between the Glk windows.
795 g_object_class_install_property(object_class, PROP_SPACING,
796 g_param_spec_uint("spacing", _("Spacing"),
797 _("The amount of space between Glk windows"),
799 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
802 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
805 /* PUBLIC FUNCTIONS */
808 * chimara_error_quark:
810 * The error domain for errors from Chimara widgets.
812 * Returns: The string <quote>chimara-error-quark</quote> as a <link
813 * linkend="GQuark">GQuark</link>.
816 chimara_error_quark(void)
818 chimara_init(); /* This is a library entry point */
819 return g_quark_from_static_string("chimara-error-quark");
825 * Creates and initializes a new #ChimaraGlk widget.
827 * Return value: a #ChimaraGlk widget, with a floating reference.
830 chimara_glk_new(void)
832 /* This is a library entry point; initialize the library */
835 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
839 * chimara_glk_set_interactive:
840 * @glk: a #ChimaraGlk widget
841 * @interactive: whether the widget should expect user input
843 * Sets the #ChimaraGlk:interactive property of @glk.
846 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
848 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
850 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
851 priv->interactive = interactive;
852 g_object_notify(G_OBJECT(glk), "interactive");
856 * chimara_glk_get_interactive:
857 * @glk: a #ChimaraGlk widget
859 * Returns whether @glk is interactive (expecting user input). See
860 * #ChimaraGlk:interactive.
862 * Return value: %TRUE if @glk is interactive.
865 chimara_glk_get_interactive(ChimaraGlk *glk)
867 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
869 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
870 return priv->interactive;
874 * chimara_glk_set_protect:
875 * @glk: a #ChimaraGlk widget
876 * @protect: whether the widget should allow the Glk program to do file
879 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
880 * program is not allowed to do file operations.
883 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
885 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
887 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
888 priv->protect = protect;
889 g_object_notify(G_OBJECT(glk), "protect");
893 * chimara_glk_get_protect:
894 * @glk: a #ChimaraGlk widget
896 * Returns whether @glk is in protect mode (banned from doing file operations).
897 * See #ChimaraGlk:protect.
899 * Return value: %TRUE if @glk is in protect mode.
902 chimara_glk_get_protect(ChimaraGlk *glk)
904 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
906 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
907 return priv->protect;
911 * chimara_glk_set_default_font_description:
912 * @glk: a #ChimaraGlk widget
913 * @font: a #PangoFontDescription
915 * Sets @glk's default proportional font. See
916 * #ChimaraGlk:default-font-description.
919 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
921 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
922 g_return_if_fail(font);
924 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
925 pango_font_description_free(priv->default_font_desc);
926 priv->default_font_desc = pango_font_description_copy(font);
927 g_object_notify(G_OBJECT(glk), "default-font-description");
928 /* TODO: Apply the font description to all the windows and recalculate the sizes */
932 * chimara_glk_set_default_font_string:
933 * @glk: a #ChimaraGlk widget
934 * @font: string representation of a font description
936 * Sets @glk's default proportional font according to the string @font, which
937 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
938 * [<replaceable>STYLE-OPTIONS</replaceable>]
939 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
940 * Italic 12</quote> or <quote>Sans</quote>. See
941 * #ChimaraGlk:default-font-description.
944 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
946 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
947 g_return_if_fail(font || *font);
949 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
950 g_return_if_fail(fontdesc);
952 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
953 pango_font_description_free(priv->default_font_desc);
954 priv->default_font_desc = fontdesc;
955 g_object_notify(G_OBJECT(glk), "default-font-description");
957 /* TODO: Apply the font description to all the windows and recalculate the sizes */
961 * chimara_glk_get_default_font_description:
962 * @glk: a #ChimaraGlk widget
964 * Returns @glk's default proportional font.
966 * Return value: a newly-allocated #PangoFontDescription which must be freed
967 * using pango_font_description_free(), or %NULL on error.
969 PangoFontDescription *
970 chimara_glk_get_default_font_description(ChimaraGlk *glk)
972 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
974 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
975 return pango_font_description_copy(priv->default_font_desc);
979 * chimara_glk_set_monospace_font_description:
980 * @glk: a #ChimaraGlk widget
981 * @font: a #PangoFontDescription
983 * Sets @glk's default monospace font. See
984 * #ChimaraGlk:monospace-font-description.
987 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
989 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
990 g_return_if_fail(font);
992 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
993 pango_font_description_free(priv->monospace_font_desc);
994 priv->monospace_font_desc = pango_font_description_copy(font);
995 g_object_notify(G_OBJECT(glk), "monospace-font-description");
997 /* TODO: Apply the font description to all the windows and recalculate the sizes */
1001 * chimara_glk_set_monospace_font_string:
1002 * @glk: a #ChimaraGlk widget
1003 * @font: string representation of a font description
1005 * Sets @glk's default monospace font according to the string @font, which must
1006 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
1007 * [<replaceable>STYLE-OPTIONS</replaceable>]
1008 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
1009 * Bold 12</quote> or <quote>Monospace</quote>. See
1010 * #ChimaraGlk:monospace-font-description.
1013 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
1015 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1016 g_return_if_fail(font || *font);
1018 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
1019 g_return_if_fail(fontdesc);
1021 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1022 pango_font_description_free(priv->monospace_font_desc);
1023 priv->monospace_font_desc = fontdesc;
1024 g_object_notify(G_OBJECT(glk), "monospace-font-description");
1026 /* TODO: Apply the font description to all the windows and recalculate the sizes */
1030 * chimara_glk_get_monospace_font_description:
1031 * @glk: a #ChimaraGlk widget
1033 * Returns @glk's default monospace font.
1035 * Return value: a newly-allocated #PangoFontDescription which must be freed
1036 * using pango_font_description_free(), or %NULL on error.
1038 PangoFontDescription *
1039 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
1041 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
1043 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1044 return pango_font_description_copy(priv->monospace_font_desc);
1048 * chimara_glk_set_spacing:
1049 * @glk: a #ChimaraGlk widget
1050 * @spacing: the number of pixels to put between Glk windows
1052 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
1053 * pixels between Glk windows.
1056 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
1058 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
1060 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1061 priv->spacing = spacing;
1062 g_object_notify(G_OBJECT(glk), "spacing");
1066 * chimara_glk_get_spacing:
1067 * @glk: a #ChimaraGlk widget
1069 * Gets the value set by chimara_glk_set_spacing().
1071 * Return value: pixels of spacing between Glk windows
1074 chimara_glk_get_spacing(ChimaraGlk *glk)
1076 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
1078 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1079 return priv->spacing;
1082 struct StartupData {
1083 glk_main_t glk_main;
1084 glkunix_startup_code_t glkunix_startup_code;
1085 glkunix_startup_t args;
1086 ChimaraGlkPrivate *glk_data;
1089 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
1091 glk_enter(struct StartupData *startup)
1093 extern GPrivate *glk_data_key;
1094 g_private_set(glk_data_key, startup->glk_data);
1096 /* Acquire the Glk thread's references to the input queues */
1097 g_async_queue_ref(startup->glk_data->char_input_queue);
1098 g_async_queue_ref(startup->glk_data->line_input_queue);
1100 /* Run startup function */
1101 if(startup->glkunix_startup_code) {
1102 startup->glk_data->in_startup = TRUE;
1103 int result = startup->glkunix_startup_code(&startup->args);
1104 startup->glk_data->in_startup = FALSE;
1107 while(i < startup->args.argc)
1108 g_free(startup->args.argv[i++]);
1109 g_free(startup->args.argv);
1115 /* Run main function */
1116 glk_main_t glk_main = startup->glk_main;
1118 /* COMPAT: avoid usage of slices */
1120 g_signal_emit_by_name(startup->glk_data->self, "started");
1122 glk_exit(); /* Run shutdown code in glk_exit() even if glk_main() returns normally */
1123 g_assert_not_reached(); /* because glk_exit() calls g_thread_exit() */
1129 * @glk: a #ChimaraGlk widget
1130 * @plugin: path to a plugin module compiled with <filename
1131 * class="header">glk.h</filename>
1132 * @argc: Number of command line arguments in @argv
1133 * @argv: Array of command line arguments to pass to the plugin
1134 * @error: location to store a <link linkend="glib-GError">GError</link>, or
1137 * Opens a Glk program compiled as a plugin. Sorts out its command line
1138 * arguments from #glkunix_arguments, calls its startup function
1139 * glkunix_startup_code(), and then calls its main function glk_main() in
1140 * a separate thread. On failure, returns %FALSE and sets @error.
1142 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1143 * glkunix_startup_code() are optional.
1145 * Return value: %TRUE if the Glk program was started successfully.
1148 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1150 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1151 g_return_val_if_fail(plugin, FALSE);
1152 if(chimara_glk_get_running(glk)) {
1153 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_ALREADY_RUNNING, _("There was already a plugin running."));
1157 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1159 /* COMPAT: avoid usage of slices */
1160 struct StartupData *startup = g_new0(struct StartupData,1);
1162 /* Open the module to run */
1163 g_assert( g_module_supported() );
1164 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1168 g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1171 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1173 g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1177 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1179 glkunix_argumentlist_t *glkunix_arguments;
1181 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1182 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1184 /* arguments could not be parsed, so create data ourselves */
1185 startup->args.argc = 1;
1186 startup->args.argv = g_new0(gchar *, 1);
1189 /* Set the program name */
1190 startup->args.argv[0] = g_strdup(plugin);
1192 startup->glk_data = priv;
1194 /* Run in a separate thread */
1195 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1197 return !(priv->thread == NULL);
1202 * @glk: a #ChimaraGlk widget
1204 * Signals the Glk program running in @glk to abort. Note that if the program is
1205 * caught in an infinite loop in which glk_tick() is not called, this may not
1209 chimara_glk_stop(ChimaraGlk *glk)
1211 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1212 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1214 /* Don't do anything if not running a program */
1218 if(priv->abort_lock) {
1219 g_mutex_lock(priv->abort_lock);
1220 priv->abort_signalled = TRUE;
1221 g_mutex_unlock(priv->abort_lock);
1222 /* Stop blocking on the event queue condition */
1223 event_throw(glk, evtype_Abort, NULL, 0, 0);
1224 /* Stop blocking on the shutdown key press condition */
1225 g_mutex_lock(priv->shutdown_lock);
1226 g_cond_signal(priv->shutdown_key_pressed);
1227 g_mutex_unlock(priv->shutdown_lock);
1233 * @glk: a #ChimaraGlk widget
1235 * Holds up the main thread and waits for the Glk program running in @glk to
1239 chimara_glk_wait(ChimaraGlk *glk)
1241 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1242 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1243 /* Don't do anything if not running a program */
1246 /* Unlock GDK mutex, because the Glk program might need to use it for shutdown */
1247 gdk_threads_leave();
1248 g_thread_join(priv->thread);
1249 gdk_threads_enter();
1253 * chimara_glk_get_running:
1254 * @glk: a #ChimaraGlk widget
1256 * Use this function to tell whether a program is currently running in the
1259 * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1262 chimara_glk_get_running(ChimaraGlk *glk)
1264 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1265 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1266 return priv->running;
1270 * chimara_glk_feed_char_input:
1271 * @glk: a #ChimaraGlk widget
1272 * @keyval: a key symbol as defined in <filename
1273 * class="headerfile">gdk/gdkkeysyms.h</filename>
1275 * Pretend that a key was pressed in the Glk program as a response to a
1276 * character input request. You can call this function even when no window has
1277 * requested character input, in which case the key will be saved for the
1278 * following window that requests character input. This has the disadvantage
1279 * that if more than one window has requested character input, it is arbitrary
1280 * which one gets the key press.
1283 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1285 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1286 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1287 g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1288 event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1292 * chimara_glk_feed_line_input:
1293 * @glk: a #ChimaraGlk widget
1294 * @text: text to pass to the next line input request
1296 * Pretend that @text was typed in the Glk program as a response to a line input
1297 * request. @text does not need to end with a newline. You can call this
1298 * function even when no window has requested line input, in which case the text
1299 * will be saved for the following window that requests line input. This has the
1300 * disadvantage that if more than one window has requested character input, it
1301 * is arbitrary which one gets the text.
1304 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1306 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1307 g_return_if_fail(text);
1308 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1309 g_async_queue_push(priv->line_input_queue, g_strdup(text));
1310 event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);