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: chimara/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>).
54 typedef void (* glk_main_t) (void);
55 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
75 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
77 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
80 chimara_glk_init(ChimaraGlk *self)
82 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
84 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
87 priv->interactive = TRUE;
88 priv->protect = FALSE;
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->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->resource_lock = g_mutex_new();
112 priv->resource_loaded = g_cond_new();
113 priv->resource_info_available = g_cond_new();
114 priv->image_cache = NULL;
115 priv->interrupt_handler = NULL;
116 priv->root_window = NULL;
117 priv->fileref_list = NULL;
118 priv->current_stream = NULL;
119 priv->stream_list = NULL;
121 priv->in_startup = FALSE;
122 priv->current_dir = NULL;
128 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
130 ChimaraGlk *glk = CHIMARA_GLK(object);
134 case PROP_INTERACTIVE:
135 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
138 chimara_glk_set_protect( glk, g_value_get_boolean(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);
162 g_value_set_uint(value, priv->spacing);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
170 chimara_glk_finalize(GObject *object)
172 ChimaraGlk *self = CHIMARA_GLK(object);
173 CHIMARA_GLK_USE_PRIVATE(self, priv);
175 /* Free widget properties */
176 g_free(priv->final_message);
178 g_hash_table_destroy(priv->default_styles->text_buffer);
179 g_hash_table_destroy(priv->default_styles->text_grid);
180 g_hash_table_destroy(priv->current_styles->text_buffer);
181 g_hash_table_destroy(priv->current_styles->text_grid);
182 pango_attr_list_unref(priv->pager_attr_list);
184 /* Free the event queue */
185 g_mutex_lock(priv->event_lock);
186 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
187 g_queue_free(priv->event_queue);
188 g_cond_free(priv->event_queue_not_empty);
189 g_cond_free(priv->event_queue_not_full);
190 priv->event_queue = NULL;
191 g_mutex_unlock(priv->event_lock);
192 g_mutex_free(priv->event_lock);
193 /* Free the abort signaling mechanism */
194 g_mutex_lock(priv->abort_lock);
195 /* Make sure no other thread is busy with this */
196 g_mutex_unlock(priv->abort_lock);
197 g_mutex_free(priv->abort_lock);
198 priv->abort_lock = NULL;
199 /* Free the shutdown keypress signaling mechanism */
200 g_mutex_lock(priv->shutdown_lock);
201 g_cond_free(priv->shutdown_key_pressed);
202 g_mutex_unlock(priv->shutdown_lock);
203 priv->shutdown_lock = NULL;
204 /* Free the window arrangement signaling */
205 g_mutex_lock(priv->arrange_lock);
206 g_cond_free(priv->rearranged);
207 g_mutex_unlock(priv->arrange_lock);
208 g_mutex_free(priv->arrange_lock);
209 priv->arrange_lock = NULL;
210 g_mutex_lock(priv->resource_lock);
211 g_cond_free(priv->resource_loaded);
212 g_cond_free(priv->resource_info_available);
213 g_mutex_unlock(priv->resource_lock);
214 g_mutex_free(priv->resource_lock);
215 g_slist_foreach(priv->image_cache, (GFunc)clear_image_cache, NULL);
216 g_slist_free(priv->image_cache);
217 /* Unref input queues (this should destroy them since any Glk thread has stopped by now */
218 g_async_queue_unref(priv->char_input_queue);
219 g_async_queue_unref(priv->line_input_queue);
221 /* Free other stuff */
222 g_free(priv->current_dir);
224 /* Chain up to parent */
225 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
228 /* Internal function: Recursively get the Glk window tree's size request */
230 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
232 if(win->type == wintype_Pair)
234 /* Get children's size requests */
235 GtkRequisition child1, child2;
236 request_recurse(win->window_node->children->data, &child1, spacing);
237 request_recurse(win->window_node->children->next->data, &child2, spacing);
239 glui32 division = win->split_method & winmethod_DivisionMask;
240 glui32 direction = win->split_method & winmethod_DirMask;
242 /* If the split is fixed, get the size of the fixed child */
243 if(division == winmethod_Fixed)
248 child1.width = win->key_window?
249 win->constraint_size * win->key_window->unit_width
252 case winmethod_Right:
253 child2.width = win->key_window?
254 win->constraint_size * win->key_window->unit_width
257 case winmethod_Above:
258 child1.height = win->key_window?
259 win->constraint_size * win->key_window->unit_height
262 case winmethod_Below:
263 child2.height = win->key_window?
264 win->constraint_size * win->key_window->unit_height
270 /* Add the children's requests */
274 case winmethod_Right:
275 requisition->width = child1.width + child2.width + spacing;
276 requisition->height = MAX(child1.height, child2.height);
278 case winmethod_Above:
279 case winmethod_Below:
280 requisition->width = MAX(child1.width, child2.width);
281 requisition->height = child1.height + child2.height + spacing;
286 /* For non-pair windows, just use the size that GTK requests */
288 gtk_widget_size_request(win->frame, requisition);
291 /* Overrides gtk_widget_size_request */
293 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
295 g_return_if_fail(widget);
296 g_return_if_fail(requisition);
297 g_return_if_fail(CHIMARA_IS_GLK(widget));
299 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
301 /* For now, just pass the size request on to the root Glk window */
302 if(priv->root_window)
304 request_recurse(priv->root_window->data, requisition, priv->spacing);
305 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
306 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
310 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
311 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
315 /* Recursively give the Glk windows their allocated space. Returns a window
316 containing all children of this window that must be redrawn, or NULL if there
317 are no children that require redrawing. */
319 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
321 if(win->type == wintype_Pair)
323 glui32 division = win->split_method & winmethod_DivisionMask;
324 glui32 direction = win->split_method & winmethod_DirMask;
326 /* If the space gets too small to honor the spacing property, then just
327 ignore spacing in this window and below. */
328 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
329 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
332 GtkAllocation child1, child2;
333 child1.x = allocation->x;
334 child1.y = allocation->y;
336 if(division == winmethod_Fixed)
338 /* If the key window has been closed, then default to 0; otherwise
339 use the key window to determine the size */
343 child1.width = win->key_window?
344 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
347 case winmethod_Right:
348 child2.width = win->key_window?
349 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
352 case winmethod_Above:
353 child1.height = win->key_window?
354 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
357 case winmethod_Below:
358 child2.height = win->key_window?
359 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
364 else /* proportional */
366 gdouble fraction = win->constraint_size / 100.0;
370 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
372 case winmethod_Right:
373 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
375 case winmethod_Above:
376 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
378 case winmethod_Below:
379 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
384 /* Fill in the rest of the size requisitions according to the child specified above */
388 child2.width = MAX(0, allocation->width - spacing - child1.width);
389 child2.x = child1.x + child1.width + spacing;
391 child1.height = child2.height = allocation->height;
393 case winmethod_Right:
394 child1.width = MAX(0, allocation->width - spacing - child2.width);
395 child2.x = child1.x + child1.width + spacing;
397 child1.height = child2.height = allocation->height;
399 case winmethod_Above:
400 child2.height = MAX(0, allocation->height - spacing - child1.height);
402 child2.y = child1.y + child1.height + spacing;
403 child1.width = child2.width = allocation->width;
405 case winmethod_Below:
406 child1.height = MAX(0, allocation->height - spacing - child2.height);
408 child2.y = child1.y + child1.height + spacing;
409 child1.width = child2.width = allocation->width;
414 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
415 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
423 else if(win->type == wintype_TextGrid)
425 /* Pass the size allocation on to the framing widget */
426 gtk_widget_size_allocate(win->frame, allocation);
427 /* It says in the spec that when a text grid window is resized smaller,
428 the bottom or right area is thrown away; when it is resized larger, the
429 bottom or right area is filled with blanks. */
430 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
431 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
433 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
434 GtkTextIter start, end;
436 for(line = 0; line < win->height; line++)
438 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
439 /* If this line is going to fall off the bottom, delete it */
440 if(line >= newheight)
443 gtk_text_iter_forward_to_line_end(&end);
444 gtk_text_iter_forward_char(&end);
445 gtk_text_buffer_delete(textbuffer, &start, &end);
448 /* If this line is not long enough, add spaces on the end */
449 if(newwidth > win->width)
451 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
452 gtk_text_iter_forward_to_line_end(&start);
453 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
456 /* But if it's too long, delete characters from the end */
457 else if(newwidth < win->width)
460 gtk_text_iter_forward_chars(&start, newwidth);
461 gtk_text_iter_forward_to_line_end(&end);
462 gtk_text_buffer_delete(textbuffer, &start, &end);
464 /* Note: if the widths are equal, do nothing */
466 /* Add blank lines if there aren't enough lines to fit the new size */
467 if(newheight > win->height)
469 gchar *blanks = g_strnfill(win->width, ' ');
470 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
472 for(count = 0; count < newheight - win->height; count++)
473 blanklines[count] = blanks;
474 blanklines[newheight - win->height] = NULL;
475 gchar *text = g_strjoinv("\n", blanklines);
476 g_free(blanklines); /* not g_strfreev() */
479 gtk_text_buffer_get_end_iter(textbuffer, &start);
480 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
481 gtk_text_buffer_insert(textbuffer, &start, text, -1);
485 gboolean arrange = !(win->width == newwidth && win->height == newheight);
486 win->width = newwidth;
487 win->height = newheight;
488 return arrange? win : NULL;
491 /* For non-pair, non-text-grid windows, just give them the size */
492 gtk_widget_size_allocate(win->frame, allocation);
496 /* Overrides gtk_widget_size_allocate */
498 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
500 g_return_if_fail(widget);
501 g_return_if_fail(allocation);
502 g_return_if_fail(CHIMARA_IS_GLK(widget));
504 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
506 widget->allocation = *allocation;
508 if(priv->root_window) {
510 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
511 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
512 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
513 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
514 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
516 /* arrange points to a window that contains all text grid and graphics
517 windows which have been resized */
518 g_mutex_lock(priv->arrange_lock);
519 if(!priv->ignore_next_arrange_event)
522 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
525 priv->ignore_next_arrange_event = FALSE;
526 priv->needs_rearrange = FALSE;
527 g_cond_signal(priv->rearranged);
528 g_mutex_unlock(priv->arrange_lock);
532 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
534 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
536 if(win->type == wintype_Pair)
538 forall_recurse(win->window_node->children->data, callback, callback_data);
539 forall_recurse(win->window_node->children->next->data, callback, callback_data);
542 (*callback)(win->frame, callback_data);
545 /* Overrides gtk_container_forall */
547 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
549 g_return_if_fail(container);
550 g_return_if_fail(CHIMARA_IS_GLK(container));
552 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
554 /* All the children are "internal" */
555 if(!include_internals)
558 if(priv->root_window)
559 forall_recurse(priv->root_window->data, callback, callback_data);
563 chimara_glk_stopped(ChimaraGlk *self)
565 CHIMARA_GLK_USE_PRIVATE(self, priv);
566 priv->running = FALSE;
570 chimara_glk_started(ChimaraGlk *self)
572 CHIMARA_GLK_USE_PRIVATE(self, priv);
573 priv->running = TRUE;
577 chimara_glk_waiting(ChimaraGlk *self)
579 /* Default signal handler */
583 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
585 /* Default signal handler */
589 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
591 /* Default signal handler */
595 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
597 /* Default signal handler */
600 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
601 #ifndef G_PARAM_STATIC_STRINGS
603 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
604 #if GTK_CHECK_VERSION(2,8,0)
605 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
607 #define G_PARAM_STATIC_STRINGS (0)
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
718 * chimara_glk_feed_char_input() and chimara_glk_feed_line_input().
719 * <quote>More</quote> prompts when a lot of text is printed to a text
720 * buffer are also disabled. This is typically used when you wish to control
721 * an interpreter program by feeding it a predefined list of commands.
723 g_object_class_install_property( object_class, PROP_INTERACTIVE,
724 g_param_spec_boolean("interactive", _("Interactive"),
725 _("Whether user input is expected in the Glk program"),
727 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
730 * ChimaraGlk:protect:
732 * Sets whether the Glk program is allowed to do file operations. In protect
733 * mode, all file operations will fail.
735 g_object_class_install_property(object_class, PROP_PROTECT,
736 g_param_spec_boolean("protect", _("Protected"),
737 _("Whether the Glk program is barred from doing file operations"),
739 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
742 * ChimaraGlk:spacing:
744 * The amount of space between the Glk windows.
746 g_object_class_install_property(object_class, PROP_SPACING,
747 g_param_spec_uint("spacing", _("Spacing"),
748 _("The amount of space between Glk windows"),
750 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
753 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
756 /* PUBLIC FUNCTIONS */
759 * chimara_error_quark:
761 * The error domain for errors from Chimara widgets.
763 * Returns: The string <quote>chimara-error-quark</quote> as a <link
764 * linkend="GQuark">GQuark</link>.
767 chimara_error_quark(void)
769 chimara_init(); /* This is a library entry point */
770 return g_quark_from_static_string("chimara-error-quark");
776 * Creates and initializes a new #ChimaraGlk widget.
778 * Return value: a #ChimaraGlk widget, with a floating reference.
781 chimara_glk_new(void)
783 /* This is a library entry point; initialize the library */
786 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
790 * chimara_glk_set_interactive:
791 * @glk: a #ChimaraGlk widget
792 * @interactive: whether the widget should expect user input
794 * Sets the #ChimaraGlk:interactive property of @glk.
797 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
799 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
801 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
802 priv->interactive = interactive;
803 g_object_notify(G_OBJECT(glk), "interactive");
807 * chimara_glk_get_interactive:
808 * @glk: a #ChimaraGlk widget
810 * Returns whether @glk is interactive (expecting user input). See
811 * #ChimaraGlk:interactive.
813 * Return value: %TRUE if @glk is interactive.
816 chimara_glk_get_interactive(ChimaraGlk *glk)
818 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
820 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
821 return priv->interactive;
825 * chimara_glk_set_protect:
826 * @glk: a #ChimaraGlk widget
827 * @protect: whether the widget should allow the Glk program to do file
830 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
831 * program is not allowed to do file operations.
834 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
836 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
838 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
839 priv->protect = protect;
840 g_object_notify(G_OBJECT(glk), "protect");
844 * chimara_glk_get_protect:
845 * @glk: a #ChimaraGlk widget
847 * Returns whether @glk is in protect mode (banned from doing file operations).
848 * See #ChimaraGlk:protect.
850 * Return value: %TRUE if @glk is in protect mode.
853 chimara_glk_get_protect(ChimaraGlk *glk)
855 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
857 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
858 return priv->protect;
862 * chimara_glk_set_css_to_default:
863 * @glk: a #ChimaraGlk widget
865 * Resets the styles for text buffer and text grid windows to their defaults.
867 * This function is not implemented yet.
871 chimara_glk_set_css_to_default(ChimaraGlk *glk)
873 reset_default_styles(glk);
877 * chimara_glk_set_css_from_file:
878 * @glk: a #ChimaraGlk widget
879 * @filename: path to a CSS file, or %NULL
880 * @error: location to store a <link
881 * linkend="glib-Error-Reporting">GError</link>, or %NULL
883 * Sets the styles for text buffer and text grid windows according to the CSS
884 * file @filename. Note that the styles are set cumulatively on top of whatever
885 * the styles are at the time this function is called; to reset the styles to
886 * their defaults, use chimara_glk_set_css_to_default().
888 * Returns: %TRUE on success, %FALSE if an error occurred, in which case @error
892 chimara_glk_set_css_from_file(ChimaraGlk *glk, const gchar *filename, GError **error)
894 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
895 g_return_val_if_fail(filename, FALSE);
896 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
898 int fd = open(filename, O_RDONLY);
900 *error = g_error_new(G_IO_ERROR, g_io_error_from_errno(errno),
901 _("Error opening file \"%s\": %s"), filename, g_strerror(errno));
905 GScanner *scanner = create_css_file_scanner();
906 g_scanner_input_file(scanner, fd);
907 scanner->input_name = filename;
908 scan_css_file(scanner, glk);
910 /* Set the current style to a copy of the default style */
911 /* FIXME this is not correct */
912 copy_default_styles_to_current_styles(glk);
914 if(close(fd) == -1) {
915 *error = g_error_new(G_IO_ERROR, g_io_error_from_errno(errno),
916 _("Error closing file \"%s\": %s"), filename, g_strerror(errno));
923 * chimara_glk_set_css_from_string:
924 * @glk: a #ChimaraGlk widget
925 * @css: a string containing CSS code
927 * Sets the styles for text buffer and text grid windows according to the CSS
928 * code @css. Note that the styles are set cumulatively on top of whatever the
929 * styles are at the time this function is called; to reset the styles to their
930 * defaults, use chimara_glk_set_css_to_default().
933 chimara_glk_set_css_from_string(ChimaraGlk *glk, const gchar *css)
935 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
936 g_return_if_fail(css || *css);
938 GScanner *scanner = create_css_file_scanner();
939 g_scanner_input_text(scanner, css, strlen(css));
940 scanner->input_name = "<string>";
941 scan_css_file(scanner, glk);
943 /* Set the current style to a copy of the default style */
944 /* FIXME this is not correct */
945 copy_default_styles_to_current_styles(glk);
949 * chimara_glk_set_spacing:
950 * @glk: a #ChimaraGlk widget
951 * @spacing: the number of pixels to put between Glk windows
953 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
954 * pixels between Glk windows.
957 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
959 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
961 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
962 priv->spacing = spacing;
963 g_object_notify(G_OBJECT(glk), "spacing");
967 * chimara_glk_get_spacing:
968 * @glk: a #ChimaraGlk widget
970 * Gets the value set by chimara_glk_set_spacing().
972 * Return value: pixels of spacing between Glk windows
975 chimara_glk_get_spacing(ChimaraGlk *glk)
977 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
979 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
980 return priv->spacing;
985 glkunix_startup_code_t glkunix_startup_code;
986 glkunix_startup_t args;
987 ChimaraGlkPrivate *glk_data;
990 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
992 glk_enter(struct StartupData *startup)
994 extern GPrivate *glk_data_key;
995 g_private_set(glk_data_key, startup->glk_data);
997 /* Acquire the Glk thread's references to the input queues */
998 g_async_queue_ref(startup->glk_data->char_input_queue);
999 g_async_queue_ref(startup->glk_data->line_input_queue);
1001 /* Run startup function */
1002 if(startup->glkunix_startup_code) {
1003 startup->glk_data->in_startup = TRUE;
1004 int result = startup->glkunix_startup_code(&startup->args);
1005 startup->glk_data->in_startup = FALSE;
1008 while(i < startup->args.argc)
1009 g_free(startup->args.argv[i++]);
1010 g_free(startup->args.argv);
1016 /* Run main function */
1017 glk_main_t glk_main = startup->glk_main;
1019 /* COMPAT: avoid usage of slices */
1021 g_signal_emit_by_name(startup->glk_data->self, "started");
1023 glk_exit(); /* Run shutdown code in glk_exit() even if glk_main() returns normally */
1024 g_assert_not_reached(); /* because glk_exit() calls g_thread_exit() */
1030 * @glk: a #ChimaraGlk widget
1031 * @plugin: path to a plugin module compiled with <filename
1032 * class="header">glk.h</filename>
1033 * @argc: Number of command line arguments in @argv
1034 * @argv: Array of command line arguments to pass to the plugin
1035 * @error: location to store a <link
1036 * linkend="glib-Error-Reporting">GError</link>, or %NULL
1038 * Opens a Glk program compiled as a plugin. Sorts out its command line
1039 * arguments from #glkunix_arguments, calls its startup function
1040 * glkunix_startup_code(), and then calls its main function glk_main() in
1041 * a separate thread. On failure, returns %FALSE and sets @error.
1043 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1044 * glkunix_startup_code() are optional.
1046 * Return value: %TRUE if the Glk program was started successfully.
1049 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1051 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1052 g_return_val_if_fail(plugin, FALSE);
1053 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1055 if(chimara_glk_get_running(glk)) {
1056 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_ALREADY_RUNNING, _("There was already a plugin running."));
1060 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1062 /* COMPAT: avoid usage of slices */
1063 struct StartupData *startup = g_new0(struct StartupData,1);
1065 g_assert( g_module_supported() );
1066 /* If there is already a module loaded, free it first -- you see, we want to
1067 * keep modules loaded as long as possible to avoid crashes in stack unwinding */
1068 if( priv->program && !g_module_close(priv->program) )
1069 g_warning( "Error closing module :%s", g_module_error() );
1070 /* Open the module to run */
1071 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1075 g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1078 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1080 g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1084 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1086 glkunix_argumentlist_t *glkunix_arguments;
1088 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1089 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1091 /* arguments could not be parsed, so create data ourselves */
1092 startup->args.argc = 1;
1093 startup->args.argv = g_new0(gchar *, 1);
1096 /* Set the program name */
1097 startup->args.argv[0] = g_strdup(plugin);
1099 startup->glk_data = priv;
1101 /* Run in a separate thread */
1102 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1104 return !(priv->thread == NULL);
1109 * @glk: a #ChimaraGlk widget
1111 * Signals the Glk program running in @glk to abort. Note that if the program is
1112 * caught in an infinite loop in which glk_tick() is not called, this may not
1116 chimara_glk_stop(ChimaraGlk *glk)
1118 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1119 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1121 /* Don't do anything if not running a program */
1125 if(priv->abort_lock) {
1126 g_mutex_lock(priv->abort_lock);
1127 priv->abort_signalled = TRUE;
1128 g_mutex_unlock(priv->abort_lock);
1129 /* Stop blocking on the event queue condition */
1130 event_throw(glk, evtype_Abort, NULL, 0, 0);
1131 /* Stop blocking on the shutdown key press condition */
1132 g_mutex_lock(priv->shutdown_lock);
1133 g_cond_signal(priv->shutdown_key_pressed);
1134 g_mutex_unlock(priv->shutdown_lock);
1140 * @glk: a #ChimaraGlk widget
1142 * Holds up the main thread and waits for the Glk program running in @glk to
1146 chimara_glk_wait(ChimaraGlk *glk)
1148 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1149 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1150 /* Don't do anything if not running a program */
1153 /* Unlock GDK mutex, because the Glk program might need to use it for shutdown */
1154 gdk_threads_leave();
1155 g_thread_join(priv->thread);
1156 gdk_threads_enter();
1160 * chimara_glk_get_running:
1161 * @glk: a #ChimaraGlk widget
1163 * Use this function to tell whether a program is currently running in the
1166 * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1169 chimara_glk_get_running(ChimaraGlk *glk)
1171 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1172 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1173 return priv->running;
1177 * chimara_glk_feed_char_input:
1178 * @glk: a #ChimaraGlk widget
1179 * @keyval: a key symbol as defined in <filename
1180 * class="headerfile">gdk/gdkkeysyms.h</filename>
1182 * Pretend that a key was pressed in the Glk program as a response to a
1183 * character input request. You can call this function even when no window has
1184 * requested character input, in which case the key will be saved for the
1185 * following window that requests character input. This has the disadvantage
1186 * that if more than one window has requested character input, it is arbitrary
1187 * which one gets the key press.
1190 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1192 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1193 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1194 g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1195 event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1199 * chimara_glk_feed_line_input:
1200 * @glk: a #ChimaraGlk widget
1201 * @text: text to pass to the next line input request
1203 * Pretend that @text was typed in the Glk program as a response to a line input
1204 * request. @text does not need to end with a newline. You can call this
1205 * function even when no window has requested line input, in which case the text
1206 * will be saved for the following window that requests line input. This has the
1207 * disadvantage that if more than one window has requested character input, it
1208 * is arbitrary which one gets the text.
1211 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1213 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1214 g_return_if_fail(text);
1215 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1216 g_async_queue_push(priv->line_input_queue, g_strdup(text));
1217 event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);