1 /* licensing and copyright information here */
11 #include <glib/gi18n-lib.h>
13 #include <pango/pango.h>
14 #include "chimara-glk.h"
15 #include "chimara-glk-private.h"
16 #include "chimara-marshallers.h"
26 #define CHIMARA_GLK_MIN_WIDTH 0
27 #define CHIMARA_GLK_MIN_HEIGHT 0
31 * @short_description: Widget which executes a Glk program
32 * @stability: Unstable
33 * @include: libchimara/chimara-glk.h
35 * The #ChimaraGlk widget opens and runs a Glk program. The program must be
36 * compiled as a plugin module, with a function <function>glk_main()</function>
37 * that the Glk library can hook into.
39 * On Linux systems, this is a file with a name like
40 * <filename>plugin.so</filename>. For portability, you can use libtool and
43 * pkglib_LTLIBRARIES = plugin.la
44 * plugin_la_SOURCES = plugin.c foo.c bar.c
45 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
47 * This will produce <filename>plugin.la</filename> which is a text file
48 * containing the correct plugin file to open (see the relevant section of the
50 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
51 * Libtool manual</ulink>).
53 * You need to initialize multithreading in any program you use a #ChimaraGlk
54 * widget in. This means including the following incantation at the beginning
57 * if(!g_thread_supported())
58 * g_thread_init(NULL);
61 * This initialization must take place <emphasis>before</emphasis> the call to
62 * gtk_init(). In addition to this, you must also protect your call to
63 * gtk_main() by calling gdk_threads_enter() right before it, and
64 * gdk_threads_leave() right after it.
66 * The following sample program shows how to initialize and construct a simple
67 * GTK window that runs a Glk program:
70 * #include <gtk/gtk.h>
71 * #include <libchimara/chimara-glk.h>
81 * main(int argc, char *argv[])
83 * GtkWidget *window, *glk;
84 * GError *error = NULL;
85 * gchar *plugin_argv[] = { "plugin.so", "-option" };
87 * /<!---->* Initialize threads and GTK *<!---->/
88 * if(!g_thread_supported())
89 * g_thread_init(NULL);
91 * gtk_init(&argc, &argv);
93 * /<!---->* Construct the window and its contents. We quit the GTK main loop
94 * * when the window's close button is clicked. *<!---->/
95 * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
96 * g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL);
97 * glk = chimara_glk_new();
98 * gtk_container_add(GTK_CONTAINER(window), glk);
99 * gtk_widget_show_all(window);
101 * /<!---->* Start the Glk program in a separate thread *<!---->/
102 * if(!chimara_glk_run(CHIMARA_GLK(glk), "./plugin.so", 2, plugin_argv, &error))
103 * g_error("Error starting Glk library: %s\n", error->message);
105 * /<!---->* Start the GTK main loop *<!---->/
106 * gdk_threads_enter();
108 * gdk_threads_leave();
110 * /<!---->* After the GTK main loop exits, signal the Glk program to shut down if
111 * * it is still running, and wait for it to exit. *<!---->/
112 * chimara_glk_stop(CHIMARA_GLK(glk));
113 * chimara_glk_wait(CHIMARA_GLK(glk));
120 typedef void (* glk_main_t) (void);
121 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
144 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
146 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
149 chimara_glk_init(ChimaraGlk *self)
151 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
153 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
156 priv->interactive = TRUE;
157 priv->protect = FALSE;
158 priv->default_styles = g_new0(StyleSet,1);
159 priv->current_styles = g_new0(StyleSet,1);
160 priv->pager_attr_list = pango_attr_list_new();
161 priv->final_message = g_strdup("[ The game has finished ]");
162 priv->running = FALSE;
163 priv->program = NULL;
165 priv->event_queue = g_queue_new();
166 priv->event_lock = g_mutex_new();
167 priv->event_queue_not_empty = g_cond_new();
168 priv->event_queue_not_full = g_cond_new();
169 priv->abort_lock = g_mutex_new();
170 priv->abort_signalled = FALSE;
171 priv->shutdown_lock = g_mutex_new();
172 priv->shutdown_key_pressed = g_cond_new();
173 priv->arrange_lock = g_mutex_new();
174 priv->rearranged = g_cond_new();
175 priv->needs_rearrange = FALSE;
176 priv->ignore_next_arrange_event = FALSE;
177 priv->char_input_queue = g_async_queue_new();
178 priv->line_input_queue = g_async_queue_new();
179 /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
180 priv->resource_lock = g_mutex_new();
181 priv->resource_loaded = g_cond_new();
182 priv->resource_info_available = g_cond_new();
183 priv->image_cache = NULL;
184 priv->program_name = NULL;
185 priv->program_info = NULL;
186 priv->story_name = NULL;
187 priv->interrupt_handler = NULL;
188 priv->root_window = NULL;
189 priv->fileref_list = NULL;
190 priv->current_stream = NULL;
191 priv->stream_list = NULL;
193 priv->in_startup = FALSE;
194 priv->current_dir = NULL;
200 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
202 ChimaraGlk *glk = CHIMARA_GLK(object);
206 case PROP_INTERACTIVE:
207 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
210 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
213 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
216 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
221 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
223 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
227 case PROP_INTERACTIVE:
228 g_value_set_boolean(value, priv->interactive);
231 g_value_set_boolean(value, priv->protect);
234 g_value_set_uint(value, priv->spacing);
236 case PROP_PROGRAM_NAME:
237 g_value_set_string(value, priv->program_name);
239 case PROP_PROGRAM_INFO:
240 g_value_set_string(value, priv->program_info);
242 case PROP_STORY_NAME:
243 g_value_set_string(value, priv->story_name);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
251 chimara_glk_finalize(GObject *object)
253 ChimaraGlk *self = CHIMARA_GLK(object);
254 CHIMARA_GLK_USE_PRIVATE(self, priv);
256 /* Free widget properties */
257 g_free(priv->final_message);
259 g_hash_table_destroy(priv->default_styles->text_buffer);
260 g_hash_table_destroy(priv->default_styles->text_grid);
261 g_hash_table_destroy(priv->current_styles->text_buffer);
262 g_hash_table_destroy(priv->current_styles->text_grid);
263 pango_attr_list_unref(priv->pager_attr_list);
265 /* Free the event queue */
266 g_mutex_lock(priv->event_lock);
267 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
268 g_queue_free(priv->event_queue);
269 g_cond_free(priv->event_queue_not_empty);
270 g_cond_free(priv->event_queue_not_full);
271 priv->event_queue = NULL;
272 g_mutex_unlock(priv->event_lock);
273 g_mutex_free(priv->event_lock);
274 /* Free the abort signaling mechanism */
275 g_mutex_lock(priv->abort_lock);
276 /* Make sure no other thread is busy with this */
277 g_mutex_unlock(priv->abort_lock);
278 g_mutex_free(priv->abort_lock);
279 priv->abort_lock = NULL;
280 /* Free the shutdown keypress signaling mechanism */
281 g_mutex_lock(priv->shutdown_lock);
282 g_cond_free(priv->shutdown_key_pressed);
283 g_mutex_unlock(priv->shutdown_lock);
284 priv->shutdown_lock = NULL;
285 /* Free the window arrangement signaling */
286 g_mutex_lock(priv->arrange_lock);
287 g_cond_free(priv->rearranged);
288 g_mutex_unlock(priv->arrange_lock);
289 g_mutex_free(priv->arrange_lock);
290 priv->arrange_lock = NULL;
291 g_mutex_lock(priv->resource_lock);
292 g_cond_free(priv->resource_loaded);
293 g_cond_free(priv->resource_info_available);
294 g_mutex_unlock(priv->resource_lock);
295 g_mutex_free(priv->resource_lock);
296 g_slist_foreach(priv->image_cache, (GFunc)clear_image_cache, NULL);
297 g_slist_free(priv->image_cache);
298 /* Unref input queues (this should destroy them since any Glk thread has stopped by now */
299 g_async_queue_unref(priv->char_input_queue);
300 g_async_queue_unref(priv->line_input_queue);
302 /* Free other stuff */
303 g_free(priv->current_dir);
304 g_free(priv->program_name);
305 g_free(priv->program_info);
306 g_free(priv->story_name);
308 /* Chain up to parent */
309 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
312 /* Internal function: Recursively get the Glk window tree's size request */
314 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
316 if(win->type == wintype_Pair)
318 /* Get children's size requests */
319 GtkRequisition child1, child2;
320 request_recurse(win->window_node->children->data, &child1, spacing);
321 request_recurse(win->window_node->children->next->data, &child2, spacing);
323 glui32 division = win->split_method & winmethod_DivisionMask;
324 glui32 direction = win->split_method & winmethod_DirMask;
326 /* If the split is fixed, get the size of the fixed child */
327 if(division == winmethod_Fixed)
332 child1.width = win->key_window?
333 win->constraint_size * win->key_window->unit_width
336 case winmethod_Right:
337 child2.width = win->key_window?
338 win->constraint_size * win->key_window->unit_width
341 case winmethod_Above:
342 child1.height = win->key_window?
343 win->constraint_size * win->key_window->unit_height
346 case winmethod_Below:
347 child2.height = win->key_window?
348 win->constraint_size * win->key_window->unit_height
354 /* Add the children's requests */
358 case winmethod_Right:
359 requisition->width = child1.width + child2.width + spacing;
360 requisition->height = MAX(child1.height, child2.height);
362 case winmethod_Above:
363 case winmethod_Below:
364 requisition->width = MAX(child1.width, child2.width);
365 requisition->height = child1.height + child2.height + spacing;
370 /* For non-pair windows, just use the size that GTK requests */
372 gtk_widget_size_request(win->frame, requisition);
375 /* Overrides gtk_widget_size_request */
377 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
379 g_return_if_fail(widget);
380 g_return_if_fail(requisition);
381 g_return_if_fail(CHIMARA_IS_GLK(widget));
383 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
385 /* For now, just pass the size request on to the root Glk window */
386 if(priv->root_window)
388 request_recurse(priv->root_window->data, requisition, priv->spacing);
389 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
390 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
394 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
395 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
399 /* Recursively give the Glk windows their allocated space. Returns a window
400 containing all children of this window that must be redrawn, or NULL if there
401 are no children that require redrawing. */
403 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
405 if(win->type == wintype_Pair)
407 glui32 division = win->split_method & winmethod_DivisionMask;
408 glui32 direction = win->split_method & winmethod_DirMask;
410 /* If the space gets too small to honor the spacing property, then just
411 ignore spacing in this window and below. */
412 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
413 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
416 GtkAllocation child1, child2;
417 child1.x = allocation->x;
418 child1.y = allocation->y;
420 if(division == winmethod_Fixed)
422 /* If the key window has been closed, then default to 0; otherwise
423 use the key window to determine the size */
427 child1.width = win->key_window?
428 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
431 case winmethod_Right:
432 child2.width = win->key_window?
433 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
436 case winmethod_Above:
437 child1.height = win->key_window?
438 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
441 case winmethod_Below:
442 child2.height = win->key_window?
443 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
448 else /* proportional */
450 gdouble fraction = win->constraint_size / 100.0;
454 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
456 case winmethod_Right:
457 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
459 case winmethod_Above:
460 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
462 case winmethod_Below:
463 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
468 /* Fill in the rest of the size requisitions according to the child specified above */
472 child2.width = MAX(0, allocation->width - spacing - child1.width);
473 child2.x = child1.x + child1.width + spacing;
475 child1.height = child2.height = allocation->height;
477 case winmethod_Right:
478 child1.width = MAX(0, allocation->width - spacing - child2.width);
479 child2.x = child1.x + child1.width + spacing;
481 child1.height = child2.height = allocation->height;
483 case winmethod_Above:
484 child2.height = MAX(0, allocation->height - spacing - child1.height);
486 child2.y = child1.y + child1.height + spacing;
487 child1.width = child2.width = allocation->width;
489 case winmethod_Below:
490 child1.height = MAX(0, allocation->height - spacing - child2.height);
492 child2.y = child1.y + child1.height + spacing;
493 child1.width = child2.width = allocation->width;
498 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
499 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
507 else if(win->type == wintype_TextGrid)
509 /* Pass the size allocation on to the framing widget */
510 gtk_widget_size_allocate(win->frame, allocation);
511 /* It says in the spec that when a text grid window is resized smaller,
512 the bottom or right area is thrown away; when it is resized larger, the
513 bottom or right area is filled with blanks. */
514 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
515 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
517 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
518 GtkTextIter start, end;
520 for(line = 0; line < win->height; line++)
522 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
523 /* If this line is going to fall off the bottom, delete it */
524 if(line >= newheight)
527 gtk_text_iter_forward_to_line_end(&end);
528 gtk_text_iter_forward_char(&end);
529 gtk_text_buffer_delete(textbuffer, &start, &end);
532 /* If this line is not long enough, add spaces on the end */
533 if(newwidth > win->width)
535 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
536 gtk_text_iter_forward_to_line_end(&start);
537 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
540 /* But if it's too long, delete characters from the end */
541 else if(newwidth < win->width)
544 gtk_text_iter_forward_chars(&start, newwidth);
545 gtk_text_iter_forward_to_line_end(&end);
546 gtk_text_buffer_delete(textbuffer, &start, &end);
548 /* Note: if the widths are equal, do nothing */
550 /* Add blank lines if there aren't enough lines to fit the new size */
551 if(newheight > win->height)
553 gchar *blanks = g_strnfill(win->width, ' ');
554 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
556 for(count = 0; count < newheight - win->height; count++)
557 blanklines[count] = blanks;
558 blanklines[newheight - win->height] = NULL;
559 gchar *text = g_strjoinv("\n", blanklines);
560 g_free(blanklines); /* not g_strfreev() */
563 gtk_text_buffer_get_end_iter(textbuffer, &start);
564 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
565 gtk_text_buffer_insert(textbuffer, &start, text, -1);
569 gboolean arrange = !(win->width == newwidth && win->height == newheight);
570 win->width = newwidth;
571 win->height = newheight;
572 return arrange? win : NULL;
575 /* For non-pair, non-text-grid windows, just give them the size */
576 gtk_widget_size_allocate(win->frame, allocation);
580 /* Overrides gtk_widget_size_allocate */
582 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
584 g_return_if_fail(widget);
585 g_return_if_fail(allocation);
586 g_return_if_fail(CHIMARA_IS_GLK(widget));
588 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
590 widget->allocation = *allocation;
592 if(priv->root_window) {
594 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
595 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
596 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
597 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
598 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
600 /* arrange points to a window that contains all text grid and graphics
601 windows which have been resized */
602 g_mutex_lock(priv->arrange_lock);
603 if(!priv->ignore_next_arrange_event)
606 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
609 priv->ignore_next_arrange_event = FALSE;
610 priv->needs_rearrange = FALSE;
611 g_cond_signal(priv->rearranged);
612 g_mutex_unlock(priv->arrange_lock);
616 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
618 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
620 if(win->type == wintype_Pair)
622 forall_recurse(win->window_node->children->data, callback, callback_data);
623 forall_recurse(win->window_node->children->next->data, callback, callback_data);
626 (*callback)(win->frame, callback_data);
629 /* Overrides gtk_container_forall */
631 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
633 g_return_if_fail(container);
634 g_return_if_fail(CHIMARA_IS_GLK(container));
636 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
638 /* All the children are "internal" */
639 if(!include_internals)
642 if(priv->root_window)
643 forall_recurse(priv->root_window->data, callback, callback_data);
647 chimara_glk_stopped(ChimaraGlk *self)
649 CHIMARA_GLK_USE_PRIVATE(self, priv);
650 priv->running = FALSE;
651 priv->program_name = NULL;
652 g_object_notify(G_OBJECT(self), "program-name");
653 priv->program_info = NULL;
654 g_object_notify(G_OBJECT(self), "program-info");
655 priv->story_name = NULL;
656 g_object_notify(G_OBJECT(self), "story-name");
660 chimara_glk_started(ChimaraGlk *self)
662 CHIMARA_GLK_USE_PRIVATE(self, priv);
663 priv->running = TRUE;
667 chimara_glk_waiting(ChimaraGlk *self)
669 /* Default signal handler */
673 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
675 /* Default signal handler */
679 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
681 /* Default signal handler */
685 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
687 /* Default signal handler */
690 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
691 #ifndef G_PARAM_STATIC_STRINGS
693 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
694 #if GTK_CHECK_VERSION(2,8,0)
695 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
697 #define G_PARAM_STATIC_STRINGS (0)
703 chimara_glk_class_init(ChimaraGlkClass *klass)
705 /* Override methods of parent classes */
706 GObjectClass *object_class = G_OBJECT_CLASS(klass);
707 object_class->set_property = chimara_glk_set_property;
708 object_class->get_property = chimara_glk_get_property;
709 object_class->finalize = chimara_glk_finalize;
711 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
712 widget_class->size_request = chimara_glk_size_request;
713 widget_class->size_allocate = chimara_glk_size_allocate;
715 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
716 container_class->forall = chimara_glk_forall;
719 klass->stopped = chimara_glk_stopped;
720 klass->started = chimara_glk_started;
721 klass->waiting = chimara_glk_waiting;
722 klass->char_input = chimara_glk_char_input;
723 klass->line_input = chimara_glk_line_input;
724 klass->text_buffer_output = chimara_glk_text_buffer_output;
726 * ChimaraGlk::stopped:
727 * @glk: The widget that received the signal
729 * Emitted when the a Glk program finishes executing in the widget, whether
730 * it ended normally, or was interrupted.
732 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
733 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
734 /* FIXME: Should be G_SIGNAL_RUN_CLEANUP but that segfaults??! */
735 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
736 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
738 * ChimaraGlk::started:
739 * @glk: The widget that received the signal
741 * Emitted when a Glk program starts executing in the widget.
743 chimara_glk_signals[STARTED] = g_signal_new ("started",
744 G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
745 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
746 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
748 * ChimaraGlk::waiting:
749 * @glk: The widget that received the signal
751 * Emitted when glk_select() is called by the Glk program and the event
752 * queue is empty, which means that the widget is waiting for input.
754 chimara_glk_signals[WAITING] = g_signal_new("waiting",
755 G_OBJECT_CLASS_TYPE(klass), 0,
756 G_STRUCT_OFFSET(ChimaraGlkClass, waiting), NULL, NULL,
757 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
759 * ChimaraGlk::char-input:
760 * @glk: The widget that received the signal
761 * @window_rock: The rock value of the window that received character input
762 * (see <link linkend="chimara-Rocks">Rocks</link>)
763 * @keysym: The key that was typed, in the form of a key symbol from
764 * <filename class="headerfile">gdk/gdkkeysyms.h</filename>
766 * Emitted when a Glk window receives character input.
768 chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
769 G_OBJECT_CLASS_TYPE(klass), 0,
770 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
771 _chimara_marshal_VOID__UINT_UINT,
772 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
774 * ChimaraGlk::line-input:
775 * @glk: The widget that received the signal
776 * @window_rock: The rock value of the window that received line input (see
777 * <link linkend="chimara-Rocks">Rocks</link>)
778 * @text: The text that was typed
780 * Emitted when a Glk window receives line input.
782 chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
783 G_OBJECT_CLASS_TYPE(klass), 0,
784 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
785 _chimara_marshal_VOID__UINT_STRING,
786 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
788 * ChimaraGlk::text-buffer-output:
789 * @glk: The widget that received the signal
790 * @window_rock: The rock value of the window that was printed to (see <link
791 * linkend="chimara-Rocks">Rocks</link>)
793 * Emitted when text is printed to a text buffer window.
795 chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
796 G_OBJECT_CLASS_TYPE(klass), 0,
797 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
798 _chimara_marshal_VOID__UINT_STRING,
799 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
803 * ChimaraGlk:interactive:
805 * Sets whether the widget is interactive. A Glk widget is normally
806 * interactive, but in non-interactive mode, keyboard and mouse input are
807 * ignored and the Glk program is controlled by
808 * chimara_glk_feed_char_input() and chimara_glk_feed_line_input().
809 * <quote>More</quote> prompts when a lot of text is printed to a text
810 * buffer are also disabled. This is typically used when you wish to control
811 * an interpreter program by feeding it a predefined list of commands.
813 g_object_class_install_property( object_class, PROP_INTERACTIVE,
814 g_param_spec_boolean("interactive", _("Interactive"),
815 _("Whether user input is expected in the Glk program"),
817 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
820 * ChimaraGlk:protect:
822 * Sets whether the Glk program is allowed to do file operations. In protect
823 * mode, all file operations will fail.
825 g_object_class_install_property(object_class, PROP_PROTECT,
826 g_param_spec_boolean("protect", _("Protected"),
827 _("Whether the Glk program is barred from doing file operations"),
829 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
832 * ChimaraGlk:spacing:
834 * The amount of space between the Glk windows.
836 g_object_class_install_property(object_class, PROP_SPACING,
837 g_param_spec_uint("spacing", _("Spacing"),
838 _("The amount of space between Glk windows"),
840 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
843 * ChimaraGlk:program-name:
845 * The name of the currently running Glk program. You cannot set this
846 * property yourself. It is set to the filename of the plugin when you call
847 * chimara_glk_run(), but the plugin can change it by calling
848 * garglk_set_program_name(). To find out when this information changes,
849 * for example to put the program name in the title bar of a window, connect
850 * to the <code>::notify::program-name</code> signal.
852 g_object_class_install_property(object_class, PROP_PROGRAM_NAME,
853 g_param_spec_string("program-name", _("Program name"),
854 _("Name of the currently running program"),
856 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS) );
859 * ChimaraGlk:program-info:
861 * Information about the currently running Glk program. You cannot set this
862 * property yourself. The plugin can change it by calling
863 * garglk_set_program_info(). See also #ChimaraGlk:program-name.
865 g_object_class_install_property(object_class, PROP_PROGRAM_INFO,
866 g_param_spec_string("program-info", _("Program info"),
867 _("Information about the currently running program"),
869 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS) );
872 * ChimaraGlk:story-name:
874 * The name of the story currently running in the Glk interpreter. You
875 * cannot set this property yourself. It is set to the story filename when
876 * you call chimara_if_run_game(), but the plugin can change it by calling
877 * garglk_set_story_name().
879 * Strictly speaking, this should be a property of #ChimaraIF, but it is
880 * legal for any Glk program to call garglk_set_story_name(), even if it is
881 * not an interpreter and does not load story files.
883 g_object_class_install_property(object_class, PROP_STORY_NAME,
884 g_param_spec_string("story-name", _("Story name"),
885 _("Name of the story currently loaded in the interpreter"),
887 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS) );
890 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
893 /* PUBLIC FUNCTIONS */
896 * chimara_error_quark:
898 * The error domain for errors from Chimara widgets.
900 * Returns: The string <quote>chimara-error-quark</quote> as a <link
901 * linkend="GQuark">GQuark</link>.
904 chimara_error_quark(void)
906 chimara_init(); /* This is a library entry point */
907 return g_quark_from_static_string("chimara-error-quark");
913 * Creates and initializes a new #ChimaraGlk widget.
915 * Return value: a #ChimaraGlk widget, with a floating reference.
918 chimara_glk_new(void)
920 /* This is a library entry point; initialize the library */
923 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
927 * chimara_glk_set_interactive:
928 * @glk: a #ChimaraGlk widget
929 * @interactive: whether the widget should expect user input
931 * Sets the #ChimaraGlk:interactive property of @glk.
934 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
936 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
938 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
939 priv->interactive = interactive;
940 g_object_notify(G_OBJECT(glk), "interactive");
944 * chimara_glk_get_interactive:
945 * @glk: a #ChimaraGlk widget
947 * Returns whether @glk is interactive (expecting user input). See
948 * #ChimaraGlk:interactive.
950 * Return value: %TRUE if @glk is interactive.
953 chimara_glk_get_interactive(ChimaraGlk *glk)
955 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
957 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
958 return priv->interactive;
962 * chimara_glk_set_protect:
963 * @glk: a #ChimaraGlk widget
964 * @protect: whether the widget should allow the Glk program to do file
967 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
968 * program is not allowed to do file operations.
971 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
973 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
975 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
976 priv->protect = protect;
977 g_object_notify(G_OBJECT(glk), "protect");
981 * chimara_glk_get_protect:
982 * @glk: a #ChimaraGlk widget
984 * Returns whether @glk is in protect mode (banned from doing file operations).
985 * See #ChimaraGlk:protect.
987 * Return value: %TRUE if @glk is in protect mode.
990 chimara_glk_get_protect(ChimaraGlk *glk)
992 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
994 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
995 return priv->protect;
999 * chimara_glk_set_css_to_default:
1000 * @glk: a #ChimaraGlk widget
1002 * Resets the styles for text buffer and text grid windows to their defaults.
1004 * This function is not implemented yet.
1008 chimara_glk_set_css_to_default(ChimaraGlk *glk)
1010 reset_default_styles(glk);
1014 * chimara_glk_set_css_from_file:
1015 * @glk: a #ChimaraGlk widget
1016 * @filename: path to a CSS file, or %NULL
1017 * @error: location to store a <link
1018 * linkend="glib-Error-Reporting">GError</link>, or %NULL
1020 * Sets the styles for text buffer and text grid windows according to the CSS
1021 * file @filename. Note that the styles are set cumulatively on top of whatever
1022 * the styles are at the time this function is called; to reset the styles to
1023 * their defaults, use chimara_glk_set_css_to_default().
1025 * Returns: %TRUE on success, %FALSE if an error occurred, in which case @error
1029 chimara_glk_set_css_from_file(ChimaraGlk *glk, const gchar *filename, GError **error)
1031 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1032 g_return_val_if_fail(filename, FALSE);
1033 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1035 int fd = open(filename, O_RDONLY);
1037 *error = g_error_new(G_IO_ERROR, g_io_error_from_errno(errno),
1038 _("Error opening file \"%s\": %s"), filename, g_strerror(errno));
1042 GScanner *scanner = create_css_file_scanner();
1043 g_scanner_input_file(scanner, fd);
1044 scanner->input_name = filename;
1045 scan_css_file(scanner, glk);
1047 /* Set the current style to a copy of the default style */
1048 /* FIXME this is not correct */
1049 copy_default_styles_to_current_styles(glk);
1051 if(close(fd) == -1) {
1052 *error = g_error_new(G_IO_ERROR, g_io_error_from_errno(errno),
1053 _("Error closing file \"%s\": %s"), filename, g_strerror(errno));
1060 * chimara_glk_set_css_from_string:
1061 * @glk: a #ChimaraGlk widget
1062 * @css: a string containing CSS code
1064 * Sets the styles for text buffer and text grid windows according to the CSS
1065 * code @css. Note that the styles are set cumulatively on top of whatever the
1066 * styles are at the time this function is called; to reset the styles to their
1067 * defaults, use chimara_glk_set_css_to_default().
1070 chimara_glk_set_css_from_string(ChimaraGlk *glk, const gchar *css)
1072 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1073 g_return_if_fail(css || *css);
1075 GScanner *scanner = create_css_file_scanner();
1076 g_scanner_input_text(scanner, css, strlen(css));
1077 scanner->input_name = "<string>";
1078 scan_css_file(scanner, glk);
1080 /* Set the current style to a copy of the default style */
1081 /* FIXME this is not correct */
1082 copy_default_styles_to_current_styles(glk);
1086 * chimara_glk_set_spacing:
1087 * @glk: a #ChimaraGlk widget
1088 * @spacing: the number of pixels to put between Glk windows
1090 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
1091 * pixels between Glk windows.
1094 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
1096 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
1098 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1099 priv->spacing = spacing;
1100 g_object_notify(G_OBJECT(glk), "spacing");
1104 * chimara_glk_get_spacing:
1105 * @glk: a #ChimaraGlk widget
1107 * Gets the value set by chimara_glk_set_spacing().
1109 * Return value: pixels of spacing between Glk windows
1112 chimara_glk_get_spacing(ChimaraGlk *glk)
1114 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
1116 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1117 return priv->spacing;
1120 struct StartupData {
1121 glk_main_t glk_main;
1122 glkunix_startup_code_t glkunix_startup_code;
1123 glkunix_startup_t args;
1124 ChimaraGlkPrivate *glk_data;
1127 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
1129 glk_enter(struct StartupData *startup)
1131 extern GPrivate *glk_data_key;
1132 g_private_set(glk_data_key, startup->glk_data);
1134 /* Acquire the Glk thread's references to the input queues */
1135 g_async_queue_ref(startup->glk_data->char_input_queue);
1136 g_async_queue_ref(startup->glk_data->line_input_queue);
1138 /* Run startup function */
1139 if(startup->glkunix_startup_code) {
1140 startup->glk_data->in_startup = TRUE;
1141 int result = startup->glkunix_startup_code(&startup->args);
1142 startup->glk_data->in_startup = FALSE;
1145 while(i < startup->args.argc)
1146 g_free(startup->args.argv[i++]);
1147 g_free(startup->args.argv);
1153 /* Run main function */
1154 glk_main_t glk_main = startup->glk_main;
1156 /* COMPAT: avoid usage of slices */
1158 g_signal_emit_by_name(startup->glk_data->self, "started");
1160 glk_exit(); /* Run shutdown code in glk_exit() even if glk_main() returns normally */
1161 g_assert_not_reached(); /* because glk_exit() calls g_thread_exit() */
1167 * @glk: a #ChimaraGlk widget
1168 * @plugin: path to a plugin module compiled with <filename
1169 * class="header">glk.h</filename>
1170 * @argc: Number of command line arguments in @argv
1171 * @argv: Array of command line arguments to pass to the plugin
1172 * @error: location to store a <link
1173 * linkend="glib-Error-Reporting">GError</link>, or %NULL
1175 * Opens a Glk program compiled as a plugin. Sorts out its command line
1176 * arguments from #glkunix_arguments, calls its startup function
1177 * glkunix_startup_code(), and then calls its main function glk_main() in
1178 * a separate thread. On failure, returns %FALSE and sets @error.
1180 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1181 * glkunix_startup_code() are optional.
1183 * Return value: %TRUE if the Glk program was started successfully.
1186 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1188 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1189 g_return_val_if_fail(plugin, FALSE);
1190 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1192 if(chimara_glk_get_running(glk)) {
1193 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_ALREADY_RUNNING, _("There was already a plugin running."));
1197 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1199 /* COMPAT: avoid usage of slices */
1200 struct StartupData *startup = g_new0(struct StartupData,1);
1202 g_assert( g_module_supported() );
1203 /* If there is already a module loaded, free it first -- you see, we want to
1204 * keep modules loaded as long as possible to avoid crashes in stack unwinding */
1205 if( priv->program && !g_module_close(priv->program) )
1206 g_warning( "Error closing module :%s", g_module_error() );
1207 /* Open the module to run */
1208 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1212 g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1215 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1217 g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1221 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1223 glkunix_argumentlist_t *glkunix_arguments;
1225 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1226 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1228 /* arguments could not be parsed, so create data ourselves */
1229 startup->args.argc = 1;
1230 startup->args.argv = g_new0(gchar *, 1);
1233 /* Set the program invocation name */
1234 startup->args.argv[0] = g_strdup(plugin);
1236 startup->glk_data = priv;
1238 /* Set the program name */
1239 priv->program_name = g_path_get_basename(plugin);
1240 g_object_notify(G_OBJECT(glk), "program-name");
1242 /* Run in a separate thread */
1243 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1245 return !(priv->thread == NULL);
1250 * @glk: a #ChimaraGlk widget
1252 * Signals the Glk program running in @glk to abort. Note that if the program is
1253 * caught in an infinite loop in which glk_tick() is not called, this may not
1257 chimara_glk_stop(ChimaraGlk *glk)
1259 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1260 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1262 /* Don't do anything if not running a program */
1266 if(priv->abort_lock) {
1267 g_mutex_lock(priv->abort_lock);
1268 priv->abort_signalled = TRUE;
1269 g_mutex_unlock(priv->abort_lock);
1270 /* Stop blocking on the event queue condition */
1271 event_throw(glk, evtype_Abort, NULL, 0, 0);
1272 /* Stop blocking on the shutdown key press condition */
1273 g_mutex_lock(priv->shutdown_lock);
1274 g_cond_signal(priv->shutdown_key_pressed);
1275 g_mutex_unlock(priv->shutdown_lock);
1281 * @glk: a #ChimaraGlk widget
1283 * Holds up the main thread and waits for the Glk program running in @glk to
1287 chimara_glk_wait(ChimaraGlk *glk)
1289 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1290 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1291 /* Don't do anything if not running a program */
1294 /* Unlock GDK mutex, because the Glk program might need to use it for shutdown */
1295 gdk_threads_leave();
1296 g_thread_join(priv->thread);
1297 gdk_threads_enter();
1301 * chimara_glk_get_running:
1302 * @glk: a #ChimaraGlk widget
1304 * Use this function to tell whether a program is currently running in the
1307 * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1310 chimara_glk_get_running(ChimaraGlk *glk)
1312 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1313 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1314 return priv->running;
1318 * chimara_glk_feed_char_input:
1319 * @glk: a #ChimaraGlk widget
1320 * @keyval: a key symbol as defined in <filename
1321 * class="headerfile">gdk/gdkkeysyms.h</filename>
1323 * Pretend that a key was pressed in the Glk program as a response to a
1324 * character input request. You can call this function even when no window has
1325 * requested character input, in which case the key will be saved for the
1326 * following window that requests character input. This has the disadvantage
1327 * that if more than one window has requested character input, it is arbitrary
1328 * which one gets the key press.
1331 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1333 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1334 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1335 g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1336 event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1340 * chimara_glk_feed_line_input:
1341 * @glk: a #ChimaraGlk widget
1342 * @text: text to pass to the next line input request
1344 * Pretend that @text was typed in the Glk program as a response to a line input
1345 * request. @text does not need to end with a newline. You can call this
1346 * function even when no window has requested line input, in which case the text
1347 * will be saved for the following window that requests line input. This has the
1348 * disadvantage that if more than one window has requested character input, it
1349 * is arbitrary which one gets the text.
1352 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1354 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1355 g_return_if_fail(text);
1356 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1357 g_async_queue_push(priv->line_input_queue, g_strdup(text));
1358 event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);