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->style_initialized = FALSE;
92 priv->final_message = g_strdup("[ The game has finished ]");
93 priv->running = FALSE;
96 priv->event_queue = g_queue_new();
97 priv->event_lock = g_mutex_new();
98 priv->event_queue_not_empty = g_cond_new();
99 priv->event_queue_not_full = g_cond_new();
100 priv->abort_lock = g_mutex_new();
101 priv->abort_signalled = FALSE;
102 priv->shutdown_lock = g_mutex_new();
103 priv->shutdown_key_pressed = g_cond_new();
104 priv->arrange_lock = g_mutex_new();
105 priv->rearranged = g_cond_new();
106 priv->needs_rearrange = FALSE;
107 priv->ignore_next_arrange_event = FALSE;
108 priv->char_input_queue = g_async_queue_new();
109 priv->line_input_queue = g_async_queue_new();
110 /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
111 priv->interrupt_handler = NULL;
112 priv->root_window = NULL;
113 priv->fileref_list = NULL;
114 priv->current_stream = NULL;
115 priv->stream_list = NULL;
117 priv->in_startup = FALSE;
118 priv->current_dir = NULL;
122 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
124 ChimaraGlk *glk = CHIMARA_GLK(object);
128 case PROP_INTERACTIVE:
129 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
132 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
134 case PROP_DEFAULT_FONT_DESCRIPTION:
135 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
137 case PROP_MONOSPACE_FONT_DESCRIPTION:
138 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
141 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
144 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
149 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
151 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
155 case PROP_INTERACTIVE:
156 g_value_set_boolean(value, priv->interactive);
159 g_value_set_boolean(value, priv->protect);
161 case PROP_DEFAULT_FONT_DESCRIPTION:
162 g_value_set_pointer(value, priv->default_font_desc);
164 case PROP_MONOSPACE_FONT_DESCRIPTION:
165 g_value_set_pointer(value, priv->monospace_font_desc);
168 g_value_set_uint(value, priv->spacing);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
176 chimara_glk_finalize(GObject *object)
178 ChimaraGlk *self = CHIMARA_GLK(object);
179 CHIMARA_GLK_USE_PRIVATE(self, priv);
181 /* Free widget properties */
182 pango_font_description_free(priv->default_font_desc);
183 pango_font_description_free(priv->monospace_font_desc);
184 g_free(priv->final_message);
186 g_hash_table_destroy(priv->default_styles->text_buffer);
187 g_hash_table_destroy(priv->default_styles->text_grid);
188 g_hash_table_destroy(priv->current_styles->text_buffer);
189 g_hash_table_destroy(priv->current_styles->text_grid);
190 priv->style_initialized = FALSE;
192 /* Free the event queue */
193 g_mutex_lock(priv->event_lock);
194 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
195 g_queue_free(priv->event_queue);
196 g_cond_free(priv->event_queue_not_empty);
197 g_cond_free(priv->event_queue_not_full);
198 priv->event_queue = NULL;
199 g_mutex_unlock(priv->event_lock);
200 g_mutex_free(priv->event_lock);
201 /* Free the abort signaling mechanism */
202 g_mutex_lock(priv->abort_lock);
203 /* Make sure no other thread is busy with this */
204 g_mutex_unlock(priv->abort_lock);
205 g_mutex_free(priv->abort_lock);
206 priv->abort_lock = NULL;
207 /* Free the shutdown keypress signaling mechanism */
208 g_mutex_lock(priv->shutdown_lock);
209 g_cond_free(priv->shutdown_key_pressed);
210 g_mutex_unlock(priv->shutdown_lock);
211 priv->shutdown_lock = NULL;
212 /* Free the window arrangement signaling */
213 g_mutex_lock(priv->arrange_lock);
214 g_cond_free(priv->rearranged);
215 g_mutex_unlock(priv->arrange_lock);
216 g_mutex_free(priv->arrange_lock);
217 priv->arrange_lock = NULL;
218 /* Unref input queues (this should destroy them since any Glk thread has stopped by now */
219 g_async_queue_unref(priv->char_input_queue);
220 g_async_queue_unref(priv->line_input_queue);
222 /* Free other stuff */
223 if(priv->current_dir)
224 g_free(priv->current_dir);
226 /* Chain up to parent */
227 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
230 /* Internal function: Recursively get the Glk window tree's size request */
232 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
234 if(win->type == wintype_Pair)
236 /* Get children's size requests */
237 GtkRequisition child1, child2;
238 request_recurse(win->window_node->children->data, &child1, spacing);
239 request_recurse(win->window_node->children->next->data, &child2, spacing);
241 glui32 division = win->split_method & winmethod_DivisionMask;
242 glui32 direction = win->split_method & winmethod_DirMask;
244 /* If the split is fixed, get the size of the fixed child */
245 if(division == winmethod_Fixed)
250 child1.width = win->key_window?
251 win->constraint_size * win->key_window->unit_width
254 case winmethod_Right:
255 child2.width = win->key_window?
256 win->constraint_size * win->key_window->unit_width
259 case winmethod_Above:
260 child1.height = win->key_window?
261 win->constraint_size * win->key_window->unit_height
264 case winmethod_Below:
265 child2.height = win->key_window?
266 win->constraint_size * win->key_window->unit_height
272 /* Add the children's requests */
276 case winmethod_Right:
277 requisition->width = child1.width + child2.width + spacing;
278 requisition->height = MAX(child1.height, child2.height);
280 case winmethod_Above:
281 case winmethod_Below:
282 requisition->width = MAX(child1.width, child2.width);
283 requisition->height = child1.height + child2.height + spacing;
288 /* For non-pair windows, just use the size that GTK requests */
290 gtk_widget_size_request(win->frame, requisition);
293 /* Overrides gtk_widget_size_request */
295 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
297 g_return_if_fail(widget);
298 g_return_if_fail(requisition);
299 g_return_if_fail(CHIMARA_IS_GLK(widget));
301 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
303 /* For now, just pass the size request on to the root Glk window */
304 if(priv->root_window)
306 request_recurse(priv->root_window->data, requisition, priv->spacing);
307 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
308 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
312 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
313 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
317 /* Recursively give the Glk windows their allocated space. Returns a window
318 containing all children of this window that must be redrawn, or NULL if there
319 are no children that require redrawing. */
321 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
323 if(win->type == wintype_Pair)
325 glui32 division = win->split_method & winmethod_DivisionMask;
326 glui32 direction = win->split_method & winmethod_DirMask;
328 /* If the space gets too small to honor the spacing property, then just
329 ignore spacing in this window and below. */
330 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
331 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
334 GtkAllocation child1, child2;
335 child1.x = allocation->x;
336 child1.y = allocation->y;
338 if(division == winmethod_Fixed)
340 /* If the key window has been closed, then default to 0; otherwise
341 use the key window to determine the size */
345 child1.width = win->key_window?
346 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
349 case winmethod_Right:
350 child2.width = win->key_window?
351 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
354 case winmethod_Above:
355 child1.height = win->key_window?
356 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
359 case winmethod_Below:
360 child2.height = win->key_window?
361 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
366 else /* proportional */
368 gdouble fraction = win->constraint_size / 100.0;
372 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
374 case winmethod_Right:
375 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
377 case winmethod_Above:
378 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
380 case winmethod_Below:
381 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
386 /* Fill in the rest of the size requisitions according to the child specified above */
390 child2.width = MAX(0, allocation->width - spacing - child1.width);
391 child2.x = child1.x + child1.width + spacing;
393 child1.height = child2.height = allocation->height;
395 case winmethod_Right:
396 child1.width = MAX(0, allocation->width - spacing - child2.width);
397 child2.x = child1.x + child1.width + spacing;
399 child1.height = child2.height = allocation->height;
401 case winmethod_Above:
402 child2.height = MAX(0, allocation->height - spacing - child1.height);
404 child2.y = child1.y + child1.height + spacing;
405 child1.width = child2.width = allocation->width;
407 case winmethod_Below:
408 child1.height = MAX(0, allocation->height - spacing - child2.height);
410 child2.y = child1.y + child1.height + spacing;
411 child1.width = child2.width = allocation->width;
416 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
417 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
425 else if(win->type == wintype_TextGrid)
427 /* Pass the size allocation on to the framing widget */
428 gtk_widget_size_allocate(win->frame, allocation);
429 /* It says in the spec that when a text grid window is resized smaller,
430 the bottom or right area is thrown away; when it is resized larger, the
431 bottom or right area is filled with blanks. */
432 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
433 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
435 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
436 GtkTextIter start, end;
438 for(line = 0; line < win->height; line++)
440 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
441 /* If this line is going to fall off the bottom, delete it */
442 if(line >= newheight)
445 gtk_text_iter_forward_to_line_end(&end);
446 gtk_text_iter_forward_char(&end);
447 gtk_text_buffer_delete(textbuffer, &start, &end);
450 /* If this line is not long enough, add spaces on the end */
451 if(newwidth > win->width)
453 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
454 gtk_text_iter_forward_to_line_end(&start);
455 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
458 /* But if it's too long, delete characters from the end */
459 else if(newwidth < win->width)
462 gtk_text_iter_forward_chars(&start, newwidth);
463 gtk_text_iter_forward_to_line_end(&end);
464 gtk_text_buffer_delete(textbuffer, &start, &end);
466 /* Note: if the widths are equal, do nothing */
468 /* Add blank lines if there aren't enough lines to fit the new size */
469 if(newheight > win->height)
471 gchar *blanks = g_strnfill(win->width, ' ');
472 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
474 for(count = 0; count < newheight - win->height; count++)
475 blanklines[count] = blanks;
476 blanklines[newheight - win->height] = NULL;
477 gchar *text = g_strjoinv("\n", blanklines);
478 g_free(blanklines); /* not g_strfreev() */
481 gtk_text_buffer_get_end_iter(textbuffer, &start);
482 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
483 gtk_text_buffer_insert(textbuffer, &start, text, -1);
487 gboolean arrange = !(win->width == newwidth && win->height == newheight);
488 win->width = newwidth;
489 win->height = newheight;
490 return arrange? win : NULL;
493 /* For non-pair, non-text-grid windows, just give them the size */
494 gtk_widget_size_allocate(win->frame, allocation);
498 /* Overrides gtk_widget_size_allocate */
500 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
502 g_return_if_fail(widget);
503 g_return_if_fail(allocation);
504 g_return_if_fail(CHIMARA_IS_GLK(widget));
506 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
508 widget->allocation = *allocation;
510 if(priv->root_window) {
512 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
513 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
514 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
515 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
516 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
518 /* arrange points to a window that contains all text grid and graphics
519 windows which have been resized */
520 g_mutex_lock(priv->arrange_lock);
521 if(!priv->ignore_next_arrange_event)
524 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
527 priv->ignore_next_arrange_event = FALSE;
528 priv->needs_rearrange = FALSE;
529 g_cond_signal(priv->rearranged);
530 g_mutex_unlock(priv->arrange_lock);
534 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
536 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
538 if(win->type == wintype_Pair)
540 forall_recurse(win->window_node->children->data, callback, callback_data);
541 forall_recurse(win->window_node->children->next->data, callback, callback_data);
544 (*callback)(win->frame, callback_data);
547 /* Overrides gtk_container_forall */
549 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
551 g_return_if_fail(container);
552 g_return_if_fail(CHIMARA_IS_GLK(container));
554 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
556 /* All the children are "internal" */
557 if(!include_internals)
560 if(priv->root_window)
561 forall_recurse(priv->root_window->data, callback, callback_data);
565 chimara_glk_stopped(ChimaraGlk *self)
567 CHIMARA_GLK_USE_PRIVATE(self, priv);
568 priv->running = FALSE;
570 /* Free the plugin */
571 if( priv->program && !g_module_close(priv->program) )
572 g_warning( "Error closing module: %s", g_module_error() );
573 priv->program = NULL;
577 chimara_glk_started(ChimaraGlk *self)
579 CHIMARA_GLK_USE_PRIVATE(self, priv);
580 priv->running = TRUE;
584 chimara_glk_waiting(ChimaraGlk *self)
586 /* Default signal handler */
590 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
592 /* Default signal handler */
596 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
598 /* Default signal handler */
602 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
604 /* Default signal handler */
607 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
608 #ifndef G_PARAM_STATIC_STRINGS
609 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
613 chimara_glk_class_init(ChimaraGlkClass *klass)
615 /* Override methods of parent classes */
616 GObjectClass *object_class = G_OBJECT_CLASS(klass);
617 object_class->set_property = chimara_glk_set_property;
618 object_class->get_property = chimara_glk_get_property;
619 object_class->finalize = chimara_glk_finalize;
621 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
622 widget_class->size_request = chimara_glk_size_request;
623 widget_class->size_allocate = chimara_glk_size_allocate;
625 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
626 container_class->forall = chimara_glk_forall;
629 klass->stopped = chimara_glk_stopped;
630 klass->started = chimara_glk_started;
631 klass->waiting = chimara_glk_waiting;
632 klass->char_input = chimara_glk_char_input;
633 klass->line_input = chimara_glk_line_input;
634 klass->text_buffer_output = chimara_glk_text_buffer_output;
636 * ChimaraGlk::stopped:
637 * @glk: The widget that received the signal
639 * Emitted when the a Glk program finishes executing in the widget, whether
640 * it ended normally, or was interrupted.
642 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
643 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
644 /* FIXME: Should be G_SIGNAL_RUN_CLEANUP but that segfaults??! */
645 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
646 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
648 * ChimaraGlk::started:
649 * @glk: The widget that received the signal
651 * Emitted when a Glk program starts executing in the widget.
653 chimara_glk_signals[STARTED] = g_signal_new ("started",
654 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
655 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
656 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
658 * ChimaraGlk::waiting:
659 * @glk: The widget that received the signal
661 * Emitted when glk_select() is called by the Glk program and the event
662 * queue is empty, which means that the widget is waiting for input.
664 chimara_glk_signals[WAITING] = g_signal_new("waiting",
665 G_OBJECT_CLASS_TYPE(klass), 0,
666 G_STRUCT_OFFSET(ChimaraGlkClass, waiting), NULL, NULL,
667 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
669 * ChimaraGlk::char-input:
670 * @glk: The widget that received the signal
671 * @window_rock: The rock value of the window that received character input
672 * (see <link linkend="chimara-Rocks">Rocks</link>)
673 * @keysym: The key that was typed, in the form of a key symbol from
674 * <filename class="headerfile">gdk/gdkkeysyms.h</filename>
676 * Emitted when a Glk window receives character input.
678 chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
679 G_OBJECT_CLASS_TYPE(klass), 0,
680 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
681 chimara_marshal_VOID__UINT_UINT,
682 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
684 * ChimaraGlk::line-input:
685 * @glk: The widget that received the signal
686 * @window_rock: The rock value of the window that received line input (see
687 * <link linkend="chimara-Rocks">Rocks</link>)
688 * @text: The text that was typed
690 * Emitted when a Glk window receives line input.
692 chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
693 G_OBJECT_CLASS_TYPE(klass), 0,
694 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
695 chimara_marshal_VOID__UINT_STRING,
696 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
698 * ChimaraGlk::text-buffer-output:
699 * @glk: The widget that received the signal
700 * @window_rock: The rock value of the window that was printed to (see <link
701 * linkend="chimara-Rocks">Rocks</link>)
703 * Emitted when text is printed to a text buffer window.
705 chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
706 G_OBJECT_CLASS_TYPE(klass), 0,
707 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
708 chimara_marshal_VOID__UINT_STRING,
709 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
713 * ChimaraGlk:interactive:
715 * Sets whether the widget is interactive. A Glk widget is normally
716 * interactive, but in non-interactive mode, keyboard and mouse input are
717 * ignored and the Glk program is controlled by chimara_glk_feed_text().
718 * <quote>More</quote> prompts when a lot of text is printed to a text
719 * buffer are also disabled. This is typically used when you wish to control
720 * an interpreter program by feeding it a predefined list of commands.
722 g_object_class_install_property( object_class, PROP_INTERACTIVE,
723 g_param_spec_boolean("interactive", _("Interactive"),
724 _("Whether user input is expected in the Glk program"),
726 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
729 * ChimaraGlk:protect:
731 * Sets whether the Glk program is allowed to do file operations. In protect
732 * mode, all file operations will fail.
734 g_object_class_install_property(object_class, PROP_PROTECT,
735 g_param_spec_boolean("protect", _("Protected"),
736 _("Whether the Glk program is barred from doing file operations"),
738 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
740 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
741 initialize them with NULL */
743 * ChimaraGlk:default-font-description:
745 * Pointer to a #PangoFontDescription describing the default proportional
746 * font, to be used in text buffer windows for example.
748 * Default value: font description created from the string
749 * <quote>Sans</quote>
751 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
752 g_param_spec_pointer("default-font-description", _("Default Font"),
753 _("Font description of the default proportional font"),
754 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
757 * ChimaraGlk:monospace-font-description:
759 * Pointer to a #PangoFontDescription describing the default monospace font,
760 * to be used in text grid windows and %style_Preformatted, for example.
762 * Default value: font description created from the string
763 * <quote>Monospace</quote>
765 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
766 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
767 _("Font description of the default monospace font"),
768 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
771 * ChimaraGlk:spacing:
773 * The amount of space between the Glk windows.
775 g_object_class_install_property(object_class, PROP_SPACING,
776 g_param_spec_uint("spacing", _("Spacing"),
777 _("The amount of space between Glk windows"),
779 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
782 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
785 /* PUBLIC FUNCTIONS */
788 * chimara_error_quark:
790 * The error domain for errors from Chimara widgets.
792 * Returns: The string <quote>chimara-error-quark</quote> as a <link
793 * linkend="GQuark">GQuark</link>.
796 chimara_error_quark(void)
798 chimara_init(); /* This is a library entry point */
799 return g_quark_from_static_string("chimara-error-quark");
805 * Creates and initializes a new #ChimaraGlk widget.
807 * Return value: a #ChimaraGlk widget, with a floating reference.
810 chimara_glk_new(void)
812 /* This is a library entry point; initialize the library */
815 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
819 * chimara_glk_set_interactive:
820 * @glk: a #ChimaraGlk widget
821 * @interactive: whether the widget should expect user input
823 * Sets the #ChimaraGlk:interactive property of @glk.
826 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
828 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
830 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
831 priv->interactive = interactive;
835 * chimara_glk_get_interactive:
836 * @glk: a #ChimaraGlk widget
838 * Returns whether @glk is interactive (expecting user input). See
839 * #ChimaraGlk:interactive.
841 * Return value: %TRUE if @glk is interactive.
844 chimara_glk_get_interactive(ChimaraGlk *glk)
846 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
848 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
849 return priv->interactive;
853 * chimara_glk_set_protect:
854 * @glk: a #ChimaraGlk widget
855 * @protect: whether the widget should allow the Glk program to do file
858 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
859 * program is not allowed to do file operations.
862 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
864 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
866 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
867 priv->protect = protect;
871 * chimara_glk_get_protect:
872 * @glk: a #ChimaraGlk widget
874 * Returns whether @glk is in protect mode (banned from doing file operations).
875 * See #ChimaraGlk:protect.
877 * Return value: %TRUE if @glk is in protect mode.
880 chimara_glk_get_protect(ChimaraGlk *glk)
882 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
884 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
885 return priv->protect;
889 * chimara_glk_set_default_font_description:
890 * @glk: a #ChimaraGlk widget
891 * @font: a #PangoFontDescription
893 * Sets @glk's default proportional font. See
894 * #ChimaraGlk:default-font-description.
897 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
899 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
900 g_return_if_fail(font);
902 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
903 pango_font_description_free(priv->default_font_desc);
904 priv->default_font_desc = pango_font_description_copy(font);
906 /* TODO: Apply the font description to all the windows and recalculate the sizes */
910 * chimara_glk_set_default_font_string:
911 * @glk: a #ChimaraGlk widget
912 * @font: string representation of a font description
914 * Sets @glk's default proportional font according to the string @font, which
915 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
916 * [<replaceable>STYLE-OPTIONS</replaceable>]
917 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
918 * Italic 12</quote> or <quote>Sans</quote>. See
919 * #ChimaraGlk:default-font-description.
922 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
924 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
925 g_return_if_fail(font || *font);
927 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
928 g_return_if_fail(fontdesc);
930 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
931 pango_font_description_free(priv->default_font_desc);
932 priv->default_font_desc = fontdesc;
934 /* TODO: Apply the font description to all the windows and recalculate the sizes */
938 * chimara_glk_get_default_font_description:
939 * @glk: a #ChimaraGlk widget
941 * Returns @glk's default proportional font.
943 * Return value: a newly-allocated #PangoFontDescription which must be freed
944 * using pango_font_description_free(), or %NULL on error.
946 PangoFontDescription *
947 chimara_glk_get_default_font_description(ChimaraGlk *glk)
949 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
951 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
952 return pango_font_description_copy(priv->default_font_desc);
956 * chimara_glk_set_monospace_font_description:
957 * @glk: a #ChimaraGlk widget
958 * @font: a #PangoFontDescription
960 * Sets @glk's default monospace font. See
961 * #ChimaraGlk:monospace-font-description.
964 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
966 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
967 g_return_if_fail(font);
969 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
970 pango_font_description_free(priv->monospace_font_desc);
971 priv->monospace_font_desc = pango_font_description_copy(font);
973 /* TODO: Apply the font description to all the windows and recalculate the sizes */
977 * chimara_glk_set_monospace_font_string:
978 * @glk: a #ChimaraGlk widget
979 * @font: string representation of a font description
981 * Sets @glk's default monospace font according to the string @font, which must
982 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
983 * [<replaceable>STYLE-OPTIONS</replaceable>]
984 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
985 * Bold 12</quote> or <quote>Monospace</quote>. See
986 * #ChimaraGlk:monospace-font-description.
989 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
991 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
992 g_return_if_fail(font || *font);
994 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
995 g_return_if_fail(fontdesc);
997 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
998 pango_font_description_free(priv->monospace_font_desc);
999 priv->monospace_font_desc = fontdesc;
1001 /* TODO: Apply the font description to all the windows and recalculate the sizes */
1005 * chimara_glk_get_monospace_font_description:
1006 * @glk: a #ChimaraGlk widget
1008 * Returns @glk's default monospace font.
1010 * Return value: a newly-allocated #PangoFontDescription which must be freed
1011 * using pango_font_description_free(), or %NULL on error.
1013 PangoFontDescription *
1014 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
1016 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
1018 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1019 return pango_font_description_copy(priv->monospace_font_desc);
1023 * chimara_glk_set_spacing:
1024 * @glk: a #ChimaraGlk widget
1025 * @spacing: the number of pixels to put between Glk windows
1027 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
1028 * pixels between Glk windows.
1031 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
1033 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
1035 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1036 priv->spacing = spacing;
1040 * chimara_glk_get_spacing:
1041 * @glk: a #ChimaraGlk widget
1043 * Gets the value set by chimara_glk_set_spacing().
1045 * Return value: pixels of spacing between Glk windows
1048 chimara_glk_get_spacing(ChimaraGlk *glk)
1050 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
1052 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1053 return priv->spacing;
1056 struct StartupData {
1057 glk_main_t glk_main;
1058 glkunix_startup_code_t glkunix_startup_code;
1059 glkunix_startup_t args;
1060 ChimaraGlkPrivate *glk_data;
1063 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
1065 glk_enter(struct StartupData *startup)
1067 extern GPrivate *glk_data_key;
1068 g_private_set(glk_data_key, startup->glk_data);
1070 /* Acquire the Glk thread's references to the input queues */
1071 g_async_queue_ref(startup->glk_data->char_input_queue);
1072 g_async_queue_ref(startup->glk_data->line_input_queue);
1074 /* Run startup function */
1075 if(startup->glkunix_startup_code) {
1076 startup->glk_data->in_startup = TRUE;
1077 int result = startup->glkunix_startup_code(&startup->args);
1078 startup->glk_data->in_startup = FALSE;
1081 while(i < startup->args.argc)
1082 g_free(startup->args.argv[i++]);
1083 g_free(startup->args.argv);
1089 /* Run main function */
1090 glk_main_t glk_main = startup->glk_main;
1091 g_slice_free(struct StartupData, startup);
1092 g_signal_emit_by_name(startup->glk_data->self, "started");
1094 glk_exit(); /* Run shutdown code in glk_exit() even if glk_main() returns normally */
1095 g_assert_not_reached(); /* because glk_exit() calls g_thread_exit() */
1101 * @glk: a #ChimaraGlk widget
1102 * @plugin: path to a plugin module compiled with <filename
1103 * class="header">glk.h</filename>
1104 * @argc: Number of command line arguments in @argv
1105 * @argv: Array of command line arguments to pass to the plugin
1106 * @error: location to store a <link linkend="glib-GError">GError</link>, or
1109 * Opens a Glk program compiled as a plugin. Sorts out its command line
1110 * arguments from #glkunix_arguments, calls its startup function
1111 * glkunix_startup_code(), and then calls its main function glk_main() in
1112 * a separate thread. On failure, returns %FALSE and sets @error.
1114 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1115 * glkunix_startup_code() are optional.
1117 * Return value: %TRUE if the Glk program was started successfully.
1120 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1122 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1123 g_return_val_if_fail(plugin, FALSE);
1124 if(chimara_glk_get_running(glk)) {
1125 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_ALREADY_RUNNING, _("There was already a plugin running."));
1129 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1130 struct StartupData *startup = g_slice_new0(struct StartupData);
1132 /* Open the module to run */
1133 g_assert( g_module_supported() );
1134 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1138 g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1141 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1143 g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1147 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1149 glkunix_argumentlist_t *glkunix_arguments;
1151 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1152 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1154 /* arguments could not be parsed, so create data ourselves */
1155 startup->args.argc = 1;
1156 startup->args.argv = g_new0(gchar *, 1);
1159 /* Set the program name */
1160 startup->args.argv[0] = g_strdup(plugin);
1162 startup->glk_data = priv;
1164 /* Run in a separate thread */
1165 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1167 return !(priv->thread == NULL);
1172 * @glk: a #ChimaraGlk widget
1174 * Signals the Glk program running in @glk to abort. Note that if the program is
1175 * caught in an infinite loop in which glk_tick() is not called, this may not
1179 chimara_glk_stop(ChimaraGlk *glk)
1181 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1182 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1184 /* Don't do anything if not running a program */
1188 if(priv->abort_lock) {
1189 g_mutex_lock(priv->abort_lock);
1190 priv->abort_signalled = TRUE;
1191 g_mutex_unlock(priv->abort_lock);
1192 /* Stop blocking on the event queue condition */
1193 event_throw(glk, evtype_Abort, NULL, 0, 0);
1194 /* Stop blocking on the shutdown key press condition */
1195 g_mutex_lock(priv->shutdown_lock);
1196 g_cond_signal(priv->shutdown_key_pressed);
1197 g_mutex_unlock(priv->shutdown_lock);
1203 * @glk: a #ChimaraGlk widget
1205 * Holds up the main thread and waits for the Glk program running in @glk to
1209 chimara_glk_wait(ChimaraGlk *glk)
1211 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1212 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1213 /* Don't do anything if not running a program */
1216 /* Unlock GDK mutex, because the Glk program might need to use it for shutdown */
1217 gdk_threads_leave();
1218 g_thread_join(priv->thread);
1219 gdk_threads_enter();
1223 * chimara_glk_get_running:
1224 * @glk: a #ChimaraGlk widget
1226 * Use this function to tell whether a program is currently running in the
1229 * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1232 chimara_glk_get_running(ChimaraGlk *glk)
1234 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1235 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1236 return priv->running;
1240 * chimara_glk_feed_char_input:
1241 * @glk: a #ChimaraGlk widget
1242 * @keyval: a key symbol as defined in <filename
1243 * class="headerfile">gdk/gdkkeysyms.h</filename>
1245 * Pretend that a key was pressed in the Glk program as a response to a
1246 * character input request. You can call this function even when no window has
1247 * requested character input, in which case the key will be saved for the
1248 * following window that requests character input. This has the disadvantage
1249 * that if more than one window has requested character input, it is arbitrary
1250 * which one gets the key press.
1253 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1255 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1256 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1257 g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1258 event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1262 * chimara_glk_feed_line_input:
1263 * @glk: a #ChimaraGlk widget
1264 * @text: text to pass to the next line input request
1266 * Pretend that @text was typed in the Glk program as a response to a line input
1267 * request. @text does not need to end with a newline. You can call this
1268 * function even when no window has requested line input, in which case the text
1269 * will be saved for the following window that requests line input. This has the
1270 * disadvantage that if more than one window has requested character input, it
1271 * is arbitrary which one gets the text.
1274 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1276 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1277 g_return_if_fail(text);
1278 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1279 g_async_queue_push(priv->line_input_queue, g_strdup(text));
1280 event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);