1 /* licensing and copyright information here */
6 #include <glib/gi18n-lib.h>
8 #include <pango/pango.h>
9 #include "chimara-glk.h"
10 #include "chimara-glk-private.h"
11 #include "chimara-marshallers.h"
19 #define CHIMARA_GLK_MIN_WIDTH 0
20 #define CHIMARA_GLK_MIN_HEIGHT 0
24 * @short_description: Widget which executes a Glk program
25 * @stability: Unstable
26 * @include: chimara/chimara-glk.h
28 * The #ChimaraGlk widget opens and runs a Glk program. The program must be
29 * compiled as a plugin module, with a function <function>glk_main()</function>
30 * that the Glk library can hook into.
32 * On Linux systems, this is a file with a name like
33 * <filename>plugin.so</filename>. For portability, you can use libtool and
36 * pkglib_LTLIBRARIES = plugin.la
37 * plugin_la_SOURCES = plugin.c foo.c bar.c
38 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
40 * This will produce <filename>plugin.la</filename> which is a text file
41 * containing the correct plugin file to open (see the relevant section of the
43 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
44 * Libtool manual</ulink>).
47 typedef void (* glk_main_t) (void);
48 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
54 PROP_DEFAULT_FONT_DESCRIPTION,
55 PROP_MONOSPACE_FONT_DESCRIPTION,
70 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
72 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
75 chimara_glk_init(ChimaraGlk *self)
77 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
79 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
82 priv->interactive = TRUE;
83 priv->protect = FALSE;
84 priv->default_font_desc = pango_font_description_from_string("Serif");
85 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
86 priv->css_file = "style.css";
87 priv->default_styles = g_new0(StyleSet,1);
88 priv->current_styles = g_new0(StyleSet,1);
89 priv->running = FALSE;
92 priv->event_queue = g_queue_new();
93 priv->event_lock = g_mutex_new();
94 priv->event_queue_not_empty = g_cond_new();
95 priv->event_queue_not_full = g_cond_new();
96 priv->abort_lock = g_mutex_new();
97 priv->abort_signalled = FALSE;
98 priv->arrange_lock = g_mutex_new();
99 priv->rearranged = g_cond_new();
100 priv->needs_rearrange = FALSE;
101 priv->ignore_next_arrange_event = FALSE;
102 priv->char_input_queue = g_async_queue_new();
103 priv->line_input_queue = g_async_queue_new();
104 /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */
105 priv->interrupt_handler = NULL;
106 priv->root_window = NULL;
107 priv->fileref_list = NULL;
108 priv->current_stream = NULL;
109 priv->stream_list = NULL;
111 priv->style_initialized = FALSE;
112 priv->in_startup = FALSE;
113 priv->current_dir = NULL;
117 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
119 ChimaraGlk *glk = CHIMARA_GLK(object);
123 case PROP_INTERACTIVE:
124 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
127 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
129 case PROP_DEFAULT_FONT_DESCRIPTION:
130 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
132 case PROP_MONOSPACE_FONT_DESCRIPTION:
133 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
136 chimara_glk_set_spacing( glk, g_value_get_uint(value) );
139 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
144 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
146 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
150 case PROP_INTERACTIVE:
151 g_value_set_boolean(value, priv->interactive);
154 g_value_set_boolean(value, priv->protect);
156 case PROP_DEFAULT_FONT_DESCRIPTION:
157 g_value_set_pointer(value, priv->default_font_desc);
159 case PROP_MONOSPACE_FONT_DESCRIPTION:
160 g_value_set_pointer(value, priv->monospace_font_desc);
163 g_value_set_uint(value, priv->spacing);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
171 chimara_glk_free_private_data(ChimaraGlk *self)
173 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
175 /* Free the event queue */
176 g_mutex_lock(priv->event_lock);
177 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
178 g_queue_free(priv->event_queue);
179 g_cond_free(priv->event_queue_not_empty);
180 g_cond_free(priv->event_queue_not_full);
181 priv->event_queue = NULL;
182 g_mutex_unlock(priv->event_lock);
183 g_mutex_free(priv->event_lock);
185 /* Free the abort signaling mechanism */
186 g_mutex_lock(priv->abort_lock);
187 /* Make sure no other thread is busy with this */
188 g_mutex_unlock(priv->abort_lock);
189 g_mutex_free(priv->abort_lock);
190 priv->abort_lock = NULL;
192 /* Free the window arrangement signaling */
193 g_mutex_lock(priv->arrange_lock);
194 g_cond_free(priv->rearranged);
195 g_mutex_unlock(priv->arrange_lock);
196 g_mutex_free(priv->arrange_lock);
197 priv->arrange_lock = NULL;
199 /* Unref input queues */
200 g_async_queue_unref(priv->char_input_queue);
201 g_async_queue_unref(priv->line_input_queue);
204 pango_font_description_free(priv->default_font_desc);
205 pango_font_description_free(priv->monospace_font_desc);
207 g_free(priv->current_dir);
208 g_hash_table_destroy(priv->default_styles->text_buffer);
209 g_hash_table_destroy(priv->default_styles->text_grid);
210 g_hash_table_destroy(priv->current_styles->text_buffer);
211 g_hash_table_destroy(priv->current_styles->text_grid);
215 chimara_glk_finalize(GObject *object)
217 ChimaraGlk *self = CHIMARA_GLK(object);
218 chimara_glk_free_private_data(self);
220 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
225 * @self: The ChimaraGLK widget to reset
227 * Resets the widget back to it's origional state. IE: it resets all the private data.
230 chimara_glk_reset(ChimaraGlk *self)
232 chimara_glk_free_private_data(self);
233 chimara_glk_init(self);
237 /* Internal function: Recursively get the Glk window tree's size request */
239 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
241 if(win->type == wintype_Pair)
243 /* Get children's size requests */
244 GtkRequisition child1, child2;
245 request_recurse(win->window_node->children->data, &child1, spacing);
246 request_recurse(win->window_node->children->next->data, &child2, spacing);
248 glui32 division = win->split_method & winmethod_DivisionMask;
249 glui32 direction = win->split_method & winmethod_DirMask;
251 /* If the split is fixed, get the size of the fixed child */
252 if(division == winmethod_Fixed)
257 child1.width = win->key_window?
258 win->constraint_size * win->key_window->unit_width
261 case winmethod_Right:
262 child2.width = win->key_window?
263 win->constraint_size * win->key_window->unit_width
266 case winmethod_Above:
267 child1.height = win->key_window?
268 win->constraint_size * win->key_window->unit_height
271 case winmethod_Below:
272 child2.height = win->key_window?
273 win->constraint_size * win->key_window->unit_height
279 /* Add the children's requests */
283 case winmethod_Right:
284 requisition->width = child1.width + child2.width + spacing;
285 requisition->height = MAX(child1.height, child2.height);
287 case winmethod_Above:
288 case winmethod_Below:
289 requisition->width = MAX(child1.width, child2.width);
290 requisition->height = child1.height + child2.height + spacing;
295 /* For non-pair windows, just use the size that GTK requests */
297 gtk_widget_size_request(win->frame, requisition);
300 /* Overrides gtk_widget_size_request */
302 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
304 g_return_if_fail(widget);
305 g_return_if_fail(requisition);
306 g_return_if_fail(CHIMARA_IS_GLK(widget));
308 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
310 /* For now, just pass the size request on to the root Glk window */
311 if(priv->root_window)
313 request_recurse(priv->root_window->data, requisition, priv->spacing);
314 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
315 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
319 requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
320 requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
324 /* Recursively give the Glk windows their allocated space. Returns a window
325 containing all children of this window that must be redrawn, or NULL if there
326 are no children that require redrawing. */
328 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
330 if(win->type == wintype_Pair)
332 glui32 division = win->split_method & winmethod_DivisionMask;
333 glui32 direction = win->split_method & winmethod_DirMask;
335 /* If the space gets too small to honor the spacing property, then just
336 ignore spacing in this window and below. */
337 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
338 || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
341 GtkAllocation child1, child2;
342 child1.x = allocation->x;
343 child1.y = allocation->y;
345 if(division == winmethod_Fixed)
347 /* If the key window has been closed, then default to 0; otherwise
348 use the key window to determine the size */
352 child1.width = win->key_window?
353 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
356 case winmethod_Right:
357 child2.width = win->key_window?
358 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
361 case winmethod_Above:
362 child1.height = win->key_window?
363 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
366 case winmethod_Below:
367 child2.height = win->key_window?
368 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
373 else /* proportional */
375 gdouble fraction = win->constraint_size / 100.0;
379 child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
381 case winmethod_Right:
382 child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
384 case winmethod_Above:
385 child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
387 case winmethod_Below:
388 child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
393 /* Fill in the rest of the size requisitions according to the child specified above */
397 child2.width = MAX(0, allocation->width - spacing - child1.width);
398 child2.x = child1.x + child1.width + spacing;
400 child1.height = child2.height = allocation->height;
402 case winmethod_Right:
403 child1.width = MAX(0, allocation->width - spacing - child2.width);
404 child2.x = child1.x + child1.width + spacing;
406 child1.height = child2.height = allocation->height;
408 case winmethod_Above:
409 child2.height = MAX(0, allocation->height - spacing - child1.height);
411 child2.y = child1.y + child1.height + spacing;
412 child1.width = child2.width = allocation->width;
414 case winmethod_Below:
415 child1.height = MAX(0, allocation->height - spacing - child2.height);
417 child2.y = child1.y + child1.height + spacing;
418 child1.width = child2.width = allocation->width;
423 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
424 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
432 else if(win->type == wintype_TextGrid)
434 /* Pass the size allocation on to the framing widget */
435 gtk_widget_size_allocate(win->frame, allocation);
436 /* It says in the spec that when a text grid window is resized smaller,
437 the bottom or right area is thrown away; when it is resized larger, the
438 bottom or right area is filled with blanks. */
439 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
440 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
442 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
443 GtkTextIter start, end;
445 for(line = 0; line < win->height; line++)
447 gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
448 /* If this line is going to fall off the bottom, delete it */
449 if(line >= newheight)
452 gtk_text_iter_forward_to_line_end(&end);
453 gtk_text_iter_forward_char(&end);
454 gtk_text_buffer_delete(textbuffer, &start, &end);
457 /* If this line is not long enough, add spaces on the end */
458 if(newwidth > win->width)
460 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
461 gtk_text_iter_forward_to_line_end(&start);
462 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
465 /* But if it's too long, delete characters from the end */
466 else if(newwidth < win->width)
469 gtk_text_iter_forward_chars(&start, newwidth);
470 gtk_text_iter_forward_to_line_end(&end);
471 gtk_text_buffer_delete(textbuffer, &start, &end);
473 /* Note: if the widths are equal, do nothing */
475 /* Add blank lines if there aren't enough lines to fit the new size */
476 if(newheight > win->height)
478 gchar *blanks = g_strnfill(win->width, ' ');
479 gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
481 for(count = 0; count < newheight - win->height; count++)
482 blanklines[count] = blanks;
483 blanklines[newheight - win->height] = NULL;
484 gchar *text = g_strjoinv("\n", blanklines);
485 g_free(blanklines); /* not g_strfreev() */
488 gtk_text_buffer_get_end_iter(textbuffer, &start);
489 gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
490 gtk_text_buffer_insert(textbuffer, &start, text, -1);
494 gboolean arrange = !(win->width == newwidth && win->height == newheight);
495 win->width = newwidth;
496 win->height = newheight;
497 return arrange? win : NULL;
500 /* For non-pair, non-text-grid windows, just give them the size */
501 gtk_widget_size_allocate(win->frame, allocation);
505 /* Overrides gtk_widget_size_allocate */
507 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
509 g_return_if_fail(widget);
510 g_return_if_fail(allocation);
511 g_return_if_fail(CHIMARA_IS_GLK(widget));
513 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
515 widget->allocation = *allocation;
517 if(priv->root_window) {
519 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
520 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
521 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
522 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
523 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
525 /* arrange points to a window that contains all text grid and graphics
526 windows which have been resized */
527 g_mutex_lock(priv->arrange_lock);
528 if(!priv->ignore_next_arrange_event)
531 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
534 priv->ignore_next_arrange_event = FALSE;
535 priv->needs_rearrange = FALSE;
536 g_cond_signal(priv->rearranged);
537 g_mutex_unlock(priv->arrange_lock);
541 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
543 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
545 if(win->type == wintype_Pair)
547 forall_recurse(win->window_node->children->data, callback, callback_data);
548 forall_recurse(win->window_node->children->next->data, callback, callback_data);
551 (*callback)(win->frame, callback_data);
554 /* Overrides gtk_container_forall */
556 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
558 g_return_if_fail(container);
559 g_return_if_fail(CHIMARA_IS_GLK(container));
561 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
563 /* All the children are "internal" */
564 if(!include_internals)
567 if(priv->root_window)
568 forall_recurse(priv->root_window->data, callback, callback_data);
572 chimara_glk_stopped(ChimaraGlk *self)
574 CHIMARA_GLK_USE_PRIVATE(self, priv);
575 printf("stopped signal received\n");
576 priv->running = FALSE;
578 /* Free the plugin */
579 if( priv->program && !g_module_close(priv->program) )
580 g_warning( "Error closing module: %s", g_module_error() );
584 chimara_glk_started(ChimaraGlk *self)
586 CHIMARA_GLK_USE_PRIVATE(self, priv);
587 printf("started signal received\n");
588 priv->running = TRUE;
592 chimara_glk_waiting(ChimaraGlk *self)
594 /* Default signal handler */
598 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
600 /* Default signal handler */
604 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
606 /* Default signal handler */
610 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
612 /* Default signal handler */
615 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
616 #ifndef G_PARAM_STATIC_STRINGS
617 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
621 chimara_glk_class_init(ChimaraGlkClass *klass)
623 /* Override methods of parent classes */
624 GObjectClass *object_class = G_OBJECT_CLASS(klass);
625 object_class->set_property = chimara_glk_set_property;
626 object_class->get_property = chimara_glk_get_property;
627 object_class->finalize = chimara_glk_finalize;
629 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
630 widget_class->size_request = chimara_glk_size_request;
631 widget_class->size_allocate = chimara_glk_size_allocate;
633 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
634 container_class->forall = chimara_glk_forall;
637 klass->stopped = chimara_glk_stopped;
638 klass->started = chimara_glk_started;
639 klass->waiting = chimara_glk_waiting;
640 klass->char_input = chimara_glk_char_input;
641 klass->line_input = chimara_glk_line_input;
642 klass->text_buffer_output = chimara_glk_text_buffer_output;
644 * ChimaraGlk::stopped:
645 * @glk: The widget that received the signal
647 * Emitted when the a Glk program finishes executing in the widget, whether
648 * it ended normally, or was interrupted.
650 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
651 G_OBJECT_CLASS_TYPE(klass), 0,
652 /* FIXME: Should be G_SIGNAL_RUN_CLEANUP but that segfaults??! */
653 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
654 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
656 * ChimaraGlk::started:
657 * @glk: The widget that received the signal
659 * Emitted when a Glk program starts executing in the widget.
661 chimara_glk_signals[STARTED] = g_signal_new ("started",
662 G_OBJECT_CLASS_TYPE(klass), 0,
663 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
664 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
666 * ChimaraGlk::waiting:
667 * @glk: The widget that received the signal
669 * Emitted when glk_select() is called by the Glk program and the event
670 * queue is empty, which means that the widget is waiting for input.
672 chimara_glk_signals[WAITING] = g_signal_new("waiting",
673 G_OBJECT_CLASS_TYPE(klass), 0,
674 G_STRUCT_OFFSET(ChimaraGlkClass, waiting), NULL, NULL,
675 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
677 * ChimaraGlk::char-input:
678 * @glk: The widget that received the signal
679 * @window_rock: The rock value of the window that received character input
680 * (see <link linkend="chimara-Rocks">Rocks</link>)
681 * @keysym: The key that was typed, in the form of a key symbol from
682 * <filename class="headerfile">gdk/gdkkeysyms.h</filename>
684 * Emitted when a Glk window receives character input.
686 chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
687 G_OBJECT_CLASS_TYPE(klass), 0,
688 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
689 chimara_marshal_VOID__UINT_UINT,
690 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
692 * ChimaraGlk::line-input:
693 * @glk: The widget that received the signal
694 * @window_rock: The rock value of the window that received line input (see
695 * <link linkend="chimara-Rocks">Rocks</link>)
696 * @text: The text that was typed
698 * Emitted when a Glk window receives line input.
700 chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
701 G_OBJECT_CLASS_TYPE(klass), 0,
702 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
703 chimara_marshal_VOID__UINT_STRING,
704 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
706 * ChimaraGlk::text-buffer-output:
707 * @glk: The widget that received the signal
708 * @window_rock: The rock value of the window that was printed to (see <link
709 * linkend="chimara-Rocks">Rocks</link>)
711 * Emitted when text is printed to a text buffer window.
713 chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
714 G_OBJECT_CLASS_TYPE(klass), 0,
715 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
716 chimara_marshal_VOID__UINT_STRING,
717 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
721 * ChimaraGlk:interactive:
723 * Sets whether the widget is interactive. A Glk widget is normally
724 * interactive, but in non-interactive mode, keyboard and mouse input are
725 * ignored and the Glk program is controlled by chimara_glk_feed_text().
726 * <quote>More</quote> prompts when a lot of text is printed to a text
727 * buffer are also disabled. This is typically used when you wish to control
728 * an interpreter program by feeding it a predefined list of commands.
730 g_object_class_install_property( object_class, PROP_INTERACTIVE,
731 g_param_spec_boolean("interactive", _("Interactive"),
732 _("Whether user input is expected in the Glk program"),
734 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
737 * ChimaraGlk:protect:
739 * Sets whether the Glk program is allowed to do file operations. In protect
740 * mode, all file operations will fail.
742 g_object_class_install_property(object_class, PROP_PROTECT,
743 g_param_spec_boolean("protect", _("Protected"),
744 _("Whether the Glk program is barred from doing file operations"),
746 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
748 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
749 initialize them with NULL */
751 * ChimaraGlk:default-font-description:
753 * Pointer to a #PangoFontDescription describing the default proportional
754 * font, to be used in text buffer windows for example.
756 * Default value: font description created from the string
757 * <quote>Sans</quote>
759 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION,
760 g_param_spec_pointer("default-font-description", _("Default Font"),
761 _("Font description of the default proportional font"),
762 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
765 * ChimaraGlk:monospace-font-description:
767 * Pointer to a #PangoFontDescription describing the default monospace font,
768 * to be used in text grid windows and %style_Preformatted, for example.
770 * Default value: font description created from the string
771 * <quote>Monospace</quote>
773 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION,
774 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
775 _("Font description of the default monospace font"),
776 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
779 * ChimaraGlk:spacing:
781 * The amount of space between the Glk windows.
783 g_object_class_install_property(object_class, PROP_SPACING,
784 g_param_spec_uint("spacing", _("Spacing"),
785 _("The amount of space between Glk windows"),
787 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
790 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
793 /* PUBLIC FUNCTIONS */
796 * chimara_error_quark:
798 * The error domain for errors from Chimara widgets.
800 * Returns: The string <quote>chimara-error-quark</quote> as a <link
801 * linkend="GQuark">GQuark</link>.
804 chimara_error_quark(void)
806 chimara_init(); /* This is a library entry point */
807 return g_quark_from_static_string("chimara-error-quark");
813 * Creates and initializes a new #ChimaraGlk widget.
815 * Return value: a #ChimaraGlk widget, with a floating reference.
818 chimara_glk_new(void)
820 /* This is a library entry point; initialize the library */
823 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
827 * chimara_glk_set_interactive:
828 * @glk: a #ChimaraGlk widget
829 * @interactive: whether the widget should expect user input
831 * Sets the #ChimaraGlk:interactive property of @glk.
834 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
836 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
838 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
839 priv->interactive = interactive;
843 * chimara_glk_get_interactive:
844 * @glk: a #ChimaraGlk widget
846 * Returns whether @glk is interactive (expecting user input). See
847 * #ChimaraGlk:interactive.
849 * Return value: %TRUE if @glk is interactive.
852 chimara_glk_get_interactive(ChimaraGlk *glk)
854 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
856 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
857 return priv->interactive;
861 * chimara_glk_set_protect:
862 * @glk: a #ChimaraGlk widget
863 * @protect: whether the widget should allow the Glk program to do file
866 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
867 * program is not allowed to do file operations.
870 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
872 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
874 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
875 priv->protect = protect;
879 * chimara_glk_get_protect:
880 * @glk: a #ChimaraGlk widget
882 * Returns whether @glk is in protect mode (banned from doing file operations).
883 * See #ChimaraGlk:protect.
885 * Return value: %TRUE if @glk is in protect mode.
888 chimara_glk_get_protect(ChimaraGlk *glk)
890 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
892 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
893 return priv->protect;
897 * chimara_glk_set_default_font_description:
898 * @glk: a #ChimaraGlk widget
899 * @font: a #PangoFontDescription
901 * Sets @glk's default proportional font. See
902 * #ChimaraGlk:default-font-description.
905 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
907 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
908 g_return_if_fail(font);
910 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
911 pango_font_description_free(priv->default_font_desc);
912 priv->default_font_desc = pango_font_description_copy(font);
914 /* TODO: Apply the font description to all the windows and recalculate the sizes */
918 * chimara_glk_set_default_font_string:
919 * @glk: a #ChimaraGlk widget
920 * @font: string representation of a font description
922 * Sets @glk's default proportional font according to the string @font, which
923 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
924 * [<replaceable>STYLE-OPTIONS</replaceable>]
925 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
926 * Italic 12</quote> or <quote>Sans</quote>. See
927 * #ChimaraGlk:default-font-description.
930 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
932 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
933 g_return_if_fail(font || *font);
935 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
936 g_return_if_fail(fontdesc);
938 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
939 pango_font_description_free(priv->default_font_desc);
940 priv->default_font_desc = fontdesc;
942 /* TODO: Apply the font description to all the windows and recalculate the sizes */
946 * chimara_glk_get_default_font_description:
947 * @glk: a #ChimaraGlk widget
949 * Returns @glk's default proportional font.
951 * Return value: a newly-allocated #PangoFontDescription which must be freed
952 * using pango_font_description_free(), or %NULL on error.
954 PangoFontDescription *
955 chimara_glk_get_default_font_description(ChimaraGlk *glk)
957 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
959 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
960 return pango_font_description_copy(priv->default_font_desc);
964 * chimara_glk_set_monospace_font_description:
965 * @glk: a #ChimaraGlk widget
966 * @font: a #PangoFontDescription
968 * Sets @glk's default monospace font. See
969 * #ChimaraGlk:monospace-font-description.
972 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
974 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
975 g_return_if_fail(font);
977 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
978 pango_font_description_free(priv->monospace_font_desc);
979 priv->monospace_font_desc = pango_font_description_copy(font);
981 /* TODO: Apply the font description to all the windows and recalculate the sizes */
985 * chimara_glk_set_monospace_font_string:
986 * @glk: a #ChimaraGlk widget
987 * @font: string representation of a font description
989 * Sets @glk's default monospace font according to the string @font, which must
990 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
991 * [<replaceable>STYLE-OPTIONS</replaceable>]
992 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
993 * Bold 12</quote> or <quote>Monospace</quote>. See
994 * #ChimaraGlk:monospace-font-description.
997 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
999 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1000 g_return_if_fail(font || *font);
1002 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
1003 g_return_if_fail(fontdesc);
1005 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1006 pango_font_description_free(priv->monospace_font_desc);
1007 priv->monospace_font_desc = fontdesc;
1009 /* TODO: Apply the font description to all the windows and recalculate the sizes */
1013 * chimara_glk_get_monospace_font_description:
1014 * @glk: a #ChimaraGlk widget
1016 * Returns @glk's default monospace font.
1018 * Return value: a newly-allocated #PangoFontDescription which must be freed
1019 * using pango_font_description_free(), or %NULL on error.
1021 PangoFontDescription *
1022 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
1024 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
1026 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1027 return pango_font_description_copy(priv->monospace_font_desc);
1031 * chimara_glk_set_spacing:
1032 * @glk: a #ChimaraGlk widget
1033 * @spacing: the number of pixels to put between Glk windows
1035 * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
1036 * pixels between Glk windows.
1039 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
1041 g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
1043 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1044 priv->spacing = spacing;
1048 * chimara_glk_get_spacing:
1049 * @glk: a #ChimaraGlk widget
1051 * Gets the value set by chimara_glk_set_spacing().
1053 * Return value: pixels of spacing between Glk windows
1056 chimara_glk_get_spacing(ChimaraGlk *glk)
1058 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
1060 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1061 return priv->spacing;
1064 struct StartupData {
1065 glk_main_t glk_main;
1066 glkunix_startup_code_t glkunix_startup_code;
1067 glkunix_startup_t args;
1068 ChimaraGlkPrivate *glk_data;
1071 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
1073 glk_enter(struct StartupData *startup)
1075 extern GPrivate *glk_data_key;
1076 g_private_set(glk_data_key, startup->glk_data);
1078 g_async_queue_ref(startup->glk_data->char_input_queue);
1079 g_async_queue_ref(startup->glk_data->line_input_queue);
1081 /* Run startup function */
1082 if(startup->glkunix_startup_code) {
1083 startup->glk_data->in_startup = TRUE;
1084 int result = startup->glkunix_startup_code(&startup->args);
1085 startup->glk_data->in_startup = FALSE;
1088 while(i < startup->args.argc)
1089 g_free(startup->args.argv[i++]);
1090 g_free(startup->args.argv);
1096 /* Run main function */
1097 g_signal_emit_by_name(startup->glk_data->self, "started");
1098 /* FIXME: hack. should be done by the signal above but for some reason
1099 * this doesn't work */
1100 chimara_glk_started(startup->glk_data->self);
1102 (startup->glk_main)();
1104 g_signal_emit_by_name(startup->glk_data->self, "stopped");
1105 /* FIXME: hack. should be done by the signal above but for some reason
1106 * this doesn't work */
1107 chimara_glk_stopped(startup->glk_data->self);
1109 g_slice_free(struct StartupData, startup);
1115 * @glk: a #ChimaraGlk widget
1116 * @plugin: path to a plugin module compiled with <filename
1117 * class="header">glk.h</filename>
1118 * @argc: Number of command line arguments in @argv
1119 * @argv: Array of command line arguments to pass to the plugin
1120 * @error: location to store a <link linkend="glib-GError">GError</link>, or
1123 * Opens a Glk program compiled as a plugin. Sorts out its command line
1124 * arguments from #glkunix_arguments, calls its startup function
1125 * glkunix_startup_code(), and then calls its main function glk_main() in
1126 * a separate thread. On failure, returns %FALSE and sets @error.
1128 * The plugin must at least export a glk_main() function; #glkunix_arguments and
1129 * glkunix_startup_code() are optional.
1131 * Return value: %TRUE if the Glk program was started successfully.
1134 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1136 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1137 g_return_val_if_fail(plugin, FALSE);
1139 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1140 struct StartupData *startup = g_slice_new0(struct StartupData);
1142 /* Open the module to run */
1143 g_assert( g_module_supported() );
1144 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1148 g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1151 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1153 g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1157 if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1159 glkunix_argumentlist_t *glkunix_arguments;
1161 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments)
1162 && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1164 /* arguments could not be parsed, so create data ourselves */
1165 startup->args.argc = 1;
1166 startup->args.argv = g_new0(gchar *, 1);
1169 /* Set the program name */
1170 startup->args.argv[0] = g_strdup(plugin);
1172 startup->glk_data = priv;
1174 /* Run in a separate thread */
1175 priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1177 return !(priv->thread == NULL);
1182 * @glk: a #ChimaraGlk widget
1184 * Signals the Glk program running in @glk to abort. Note that if the program is
1185 * caught in an infinite loop in which glk_tick() is not called, this may not
1189 chimara_glk_stop(ChimaraGlk *glk)
1191 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1192 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1194 printf("stopping (%d)...\n", priv->running);
1195 /* Don't do anything if not running a program */
1199 if(priv->abort_lock) {
1200 g_mutex_lock(priv->abort_lock);
1201 priv->abort_signalled = TRUE;
1202 g_mutex_unlock(priv->abort_lock);
1203 /* Stop blocking on the event queue condition */
1204 event_throw(glk, evtype_Abort, NULL, 0, 0);
1210 * @glk: a #ChimaraGlk widget
1212 * Holds up the main thread and waits for the Glk program running in @glk to
1216 chimara_glk_wait(ChimaraGlk *glk)
1218 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1219 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1220 /* Don't do anything if not running a program */
1223 g_thread_join(priv->thread);
1227 * chimara_glk_get_running:
1228 * @glk: a #ChimaraGlk widget
1230 * Use this function to tell whether a program is currently running in the
1233 * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1236 chimara_glk_get_running(ChimaraGlk *glk)
1238 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1239 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1240 return priv->running;
1244 * chimara_glk_feed_char_input:
1245 * @glk: a #ChimaraGlk widget
1246 * @keyval: a key symbol as defined in <filename
1247 * class="headerfile">gdk/gdkkeysyms.h</filename>
1249 * Pretend that a key was pressed in the Glk program as a response to a
1250 * character input request. You can call this function even when no window has
1251 * requested character input, in which case the key will be saved for the
1252 * following window that requests character input. This has the disadvantage
1253 * that if more than one window has requested character input, it is arbitrary
1254 * which one gets the key press.
1257 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1259 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1260 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1261 g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1262 event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1266 * chimara_glk_feed_line_input:
1267 * @glk: a #ChimaraGlk widget
1268 * @text: text to pass to the next line input request
1270 * Pretend that @text was typed in the Glk program as a response to a line input
1271 * request. @text does not need to end with a newline. You can call this
1272 * function even when no window has requested line input, in which case the text
1273 * will be saved for the following window that requests line input. This has the
1274 * disadvantage that if more than one window has requested character input, it
1275 * is arbitrary which one gets the text.
1278 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1280 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1281 g_return_if_fail(text);
1282 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1283 g_async_queue_push(priv->line_input_queue, g_strdup(text));
1284 event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);