- Feeding input programmatically to the Glk program now works: chimara_glk_feed_line_...
[projects/chimara/chimara.git] / libchimara / chimara-glk.c
1 /* licensing and copyright information here */
2
3 #include <math.h>
4 #include <gtk/gtk.h>
5 #include <config.h>
6 #include <glib/gi18n-lib.h>
7 #include <gmodule.h>
8 #include <pango/pango.h>
9 #include "chimara-glk.h"
10 #include "chimara-glk-private.h"
11 #include "chimara-marshallers.h"
12 #include "glk.h"
13 #include "abort.h"
14 #include "window.h"
15 #include "glkstart.h"
16 #include "glkunix.h"
17 #include "init.h"
18
19 #define CHIMARA_GLK_MIN_WIDTH 0
20 #define CHIMARA_GLK_MIN_HEIGHT 0
21
22 /**
23  * SECTION:chimara-glk
24  * @short_description: Widget which executes a Glk program
25  * @stability: Unstable
26  * @include: chimara/chimara-glk.h
27  * 
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.
31  *
32  * On Linux systems, this is a file with a name like 
33  * <filename>plugin.so</filename>. For portability, you can use libtool and 
34  * automake:
35  * |[
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$$"
39  * ]|
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
42  * <ulink 
43  * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
44  * Libtool manual</ulink>).
45  */
46
47 typedef void (* glk_main_t) (void);
48 typedef int (* glkunix_startup_code_t) (glkunix_startup_t*);
49
50 enum {
51     PROP_0,
52     PROP_INTERACTIVE,
53     PROP_PROTECT,
54         PROP_DEFAULT_FONT_DESCRIPTION,
55         PROP_MONOSPACE_FONT_DESCRIPTION,
56         PROP_SPACING
57 };
58
59 enum {
60         STOPPED,
61         STARTED,
62         WAITING,
63         CHAR_INPUT,
64         LINE_INPUT,
65         TEXT_BUFFER_OUTPUT,
66
67         LAST_SIGNAL
68 };
69
70 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
71
72 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
73
74 static void
75 chimara_glk_init(ChimaraGlk *self)
76 {
77     GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
78
79     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
80     
81     priv->self = self;
82     priv->interactive = TRUE;
83     priv->protect = FALSE;
84         priv->default_font_desc = pango_font_description_from_string("Sans");
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;
90     priv->program = NULL;
91     priv->thread = NULL;
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;
110         priv->timer_id = 0;
111         priv->in_startup = FALSE;
112         priv->current_dir = NULL;
113 }
114
115 static void
116 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
117 {
118     ChimaraGlk *glk = CHIMARA_GLK(object);
119     
120     switch(prop_id) 
121     {
122         case PROP_INTERACTIVE:
123             chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
124             break;
125         case PROP_PROTECT:
126             chimara_glk_set_protect( glk, g_value_get_boolean(value) );
127             break;
128                 case PROP_DEFAULT_FONT_DESCRIPTION:
129                         chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
130                         break;
131                 case PROP_MONOSPACE_FONT_DESCRIPTION:
132                         chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
133                         break;
134                 case PROP_SPACING:
135                         chimara_glk_set_spacing( glk, g_value_get_uint(value) );
136                         break;
137         default:
138             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
139     }
140 }
141
142 static void
143 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
144 {
145     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
146     
147     switch(prop_id)
148     {
149         case PROP_INTERACTIVE:
150             g_value_set_boolean(value, priv->interactive);
151             break;
152         case PROP_PROTECT:
153             g_value_set_boolean(value, priv->protect);
154             break;
155                 case PROP_DEFAULT_FONT_DESCRIPTION:
156                         g_value_set_pointer(value, priv->default_font_desc);
157                         break;
158                 case PROP_MONOSPACE_FONT_DESCRIPTION:
159                         g_value_set_pointer(value, priv->monospace_font_desc);
160                         break;
161                 case PROP_SPACING:
162                         g_value_set_uint(value, priv->spacing);
163                         break;
164         default:
165             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
166     }
167 }
168
169 static void
170 chimara_glk_finalize(GObject *object)
171 {
172     ChimaraGlk *self = CHIMARA_GLK(object);
173     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
174     
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);
184         
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;
191
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;
198         
199         /* Unref input queues */
200         g_async_queue_unref(priv->char_input_queue);
201         g_async_queue_unref(priv->line_input_queue);
202         
203         /* Free private data */
204         pango_font_description_free(priv->default_font_desc);
205         pango_font_description_free(priv->monospace_font_desc);
206         g_free(priv->current_dir);
207         g_hash_table_destroy(priv->default_styles->text_buffer);
208         g_hash_table_destroy(priv->default_styles->text_grid);
209         g_hash_table_destroy(priv->current_styles->text_buffer);
210         g_hash_table_destroy(priv->current_styles->text_grid);
211         
212     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
213 }
214
215 /* Internal function: Recursively get the Glk window tree's size request */
216 static void
217 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
218 {
219         if(win->type == wintype_Pair)
220         {
221                 /* Get children's size requests */
222                 GtkRequisition child1, child2;
223                 request_recurse(win->window_node->children->data, &child1, spacing);
224                 request_recurse(win->window_node->children->next->data, &child2, spacing);
225
226                 glui32 division = win->split_method & winmethod_DivisionMask;
227                 glui32 direction = win->split_method & winmethod_DirMask;
228                 
229                 /* If the split is fixed, get the size of the fixed child */
230                 if(division == winmethod_Fixed)
231                 {
232                         switch(direction)
233                         {
234                                 case winmethod_Left:
235                                         child1.width = win->key_window?
236                                                 win->constraint_size * win->key_window->unit_width
237                                                 : 0;
238                                         break;
239                                 case winmethod_Right:
240                                         child2.width = win->key_window?
241                                                 win->constraint_size * win->key_window->unit_width
242                                                 : 0;
243                                         break;
244                                 case winmethod_Above:
245                                         child1.height = win->key_window?
246                                                 win->constraint_size * win->key_window->unit_height
247                                                 : 0;
248                                         break;
249                                 case winmethod_Below:
250                                         child2.height = win->key_window?
251                                                 win->constraint_size * win->key_window->unit_height
252                                                 : 0;
253                                         break;
254                         }
255                 }
256                 
257                 /* Add the children's requests */
258                 switch(direction)
259                 {
260                         case winmethod_Left:
261                         case winmethod_Right:
262                                 requisition->width = child1.width + child2.width + spacing;
263                                 requisition->height = MAX(child1.height, child2.height);
264                                 break;
265                         case winmethod_Above:
266                         case winmethod_Below:
267                                 requisition->width = MAX(child1.width, child2.width);
268                                 requisition->height = child1.height + child2.height + spacing;
269                                 break;
270                 }
271         }
272         
273         /* For non-pair windows, just use the size that GTK requests */
274         else
275                 gtk_widget_size_request(win->frame, requisition);
276 }
277
278 /* Overrides gtk_widget_size_request */
279 static void
280 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
281 {
282     g_return_if_fail(widget);
283     g_return_if_fail(requisition);
284     g_return_if_fail(CHIMARA_IS_GLK(widget));
285     
286     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
287     
288     /* For now, just pass the size request on to the root Glk window */
289     if(priv->root_window) 
290         {
291                 request_recurse(priv->root_window->data, requisition, priv->spacing);
292                 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
293                 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
294         } 
295         else 
296         {
297         requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
298         requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
299     }
300 }
301
302 /* Recursively give the Glk windows their allocated space. Returns a window
303  containing all children of this window that must be redrawn, or NULL if there 
304  are no children that require redrawing. */
305 static winid_t
306 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
307 {
308         if(win->type == wintype_Pair)
309         {
310                 glui32 division = win->split_method & winmethod_DivisionMask;
311                 glui32 direction = win->split_method & winmethod_DirMask;
312
313                 /* If the space gets too small to honor the spacing property, then just 
314                  ignore spacing in this window and below. */
315                 if( (spacing > allocation->width && (direction == winmethod_Left || direction == winmethod_Right))
316                    || (spacing > allocation->height && (direction == winmethod_Above || direction == winmethod_Below)) )
317                         spacing = 0;
318                 
319                 GtkAllocation child1, child2;
320                 child1.x = allocation->x;
321                 child1.y = allocation->y;
322                 
323                 if(division == winmethod_Fixed)
324                 {
325                         /* If the key window has been closed, then default to 0; otherwise
326                          use the key window to determine the size */
327                         switch(direction)
328                         {
329                                 case winmethod_Left:
330                                         child1.width = win->key_window? 
331                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing) 
332                                                 : 0;
333                                         break;
334                                 case winmethod_Right:
335                                         child2.width = win->key_window? 
336                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
337                                                 : 0;
338                                         break;
339                                 case winmethod_Above:
340                                         child1.height = win->key_window? 
341                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
342                                                 : 0;
343                                         break;
344                                 case winmethod_Below:
345                                         child2.height = win->key_window?
346                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
347                                                 : 0;
348                                         break;
349                         }
350                 }
351                 else /* proportional */
352                 {
353                         gdouble fraction = win->constraint_size / 100.0;
354                         switch(direction)
355                         {
356                                 case winmethod_Left:
357                                         child1.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
358                                         break;
359                                 case winmethod_Right:
360                                         child2.width = MAX(0, (gint)ceil(fraction * (allocation->width - spacing)) );
361                                         break;
362                                 case winmethod_Above:
363                                         child1.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
364                                         break;
365                                 case winmethod_Below:
366                                         child2.height = MAX(0, (gint)ceil(fraction * (allocation->height - spacing)) );
367                                         break;
368                         }
369                 }
370                 
371                 /* Fill in the rest of the size requisitions according to the child specified above */
372                 switch(direction)
373                 {
374                         case winmethod_Left:
375                                 child2.width = MAX(0, allocation->width - spacing - child1.width);
376                                 child2.x = child1.x + child1.width + spacing;
377                                 child2.y = child1.y;
378                                 child1.height = child2.height = allocation->height;
379                                 break;
380                         case winmethod_Right:
381                                 child1.width = MAX(0, allocation->width - spacing - child2.width);
382                                 child2.x = child1.x + child1.width + spacing;
383                                 child2.y = child1.y;
384                                 child1.height = child2.height = allocation->height;
385                                 break;
386                         case winmethod_Above:
387                                 child2.height = MAX(0, allocation->height - spacing - child1.height);
388                                 child2.x = child1.x;
389                                 child2.y = child1.y + child1.height + spacing;
390                                 child1.width = child2.width = allocation->width;
391                                 break;
392                         case winmethod_Below:
393                                 child1.height = MAX(0, allocation->height - spacing - child2.height);
394                                 child2.x = child1.x;
395                                 child2.y = child1.y + child1.height + spacing;
396                                 child1.width = child2.width = allocation->width;
397                                 break;
398                 }
399                 
400                 /* Recurse */
401                 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
402                 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
403                 if(arrange1 == NULL)
404                         return arrange2;
405                 if(arrange2 == NULL)
406                         return arrange1;
407                 return win;
408         }
409         
410         else if(win->type == wintype_TextGrid)
411         {
412                 /* Pass the size allocation on to the framing widget */
413                 gtk_widget_size_allocate(win->frame, allocation);
414                 /* It says in the spec that when a text grid window is resized smaller,
415                  the bottom or right area is thrown away; when it is resized larger, the
416                  bottom or right area is filled with blanks. */
417                 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
418                 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
419                 gint line;
420                 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
421                 GtkTextIter start, end;
422         
423                 for(line = 0; line < win->height; line++)
424                 {
425                         gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
426                         /* If this line is going to fall off the bottom, delete it */
427                         if(line >= newheight)
428                         {
429                                 end = start;
430                                 gtk_text_iter_forward_to_line_end(&end);
431                                 gtk_text_iter_forward_char(&end);
432                                 gtk_text_buffer_delete(textbuffer, &start, &end);
433                                 break;
434                         }
435                         /* If this line is not long enough, add spaces on the end */
436                         if(newwidth > win->width)
437                         {
438                                 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
439                                 gtk_text_iter_forward_to_line_end(&start);
440                                 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
441                                 g_free(spaces);
442                         }
443                         /* But if it's too long, delete characters from the end */
444                         else if(newwidth < win->width)
445                         {
446                                 end = start;
447                                 gtk_text_iter_forward_chars(&start, newwidth);
448                                 gtk_text_iter_forward_to_line_end(&end);
449                                 gtk_text_buffer_delete(textbuffer, &start, &end);
450                         }
451                         /* Note: if the widths are equal, do nothing */
452                 }
453                 /* Add blank lines if there aren't enough lines to fit the new size */
454                 if(newheight > win->height)
455                 {
456                         gchar *blanks = g_strnfill(win->width, ' ');
457                     gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
458                     int count;
459                     for(count = 0; count < newheight - win->height; count++)
460                         blanklines[count] = blanks;
461                     blanklines[newheight - win->height] = NULL;
462                     gchar *text = g_strjoinv("\n", blanklines);
463                     g_free(blanklines); /* not g_strfreev() */
464                     g_free(blanks);
465                     
466                         gtk_text_buffer_get_end_iter(textbuffer, &start);
467                         gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
468                     gtk_text_buffer_insert(textbuffer, &start, text, -1);
469                     g_free(text);
470                 }
471         
472                 gboolean arrange = !(win->width == newwidth && win->height == newheight);
473                 win->width = newwidth;
474                 win->height = newheight;
475                 return arrange? win : NULL;
476         }
477         
478         /* For non-pair, non-text-grid windows, just give them the size */
479         gtk_widget_size_allocate(win->frame, allocation);
480         return NULL;
481 }
482
483 /* Overrides gtk_widget_size_allocate */
484 static void
485 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
486 {
487     g_return_if_fail(widget);
488     g_return_if_fail(allocation);
489     g_return_if_fail(CHIMARA_IS_GLK(widget));
490     
491     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
492     
493     widget->allocation = *allocation;
494             
495     if(priv->root_window) {
496                 GtkAllocation child;
497                 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
498                 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
499                 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
500                 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
501                 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
502                 
503                 /* arrange points to a window that contains all text grid and graphics
504                  windows which have been resized */
505                 g_mutex_lock(priv->arrange_lock);
506                 if(!priv->ignore_next_arrange_event)
507                 {
508                         if(arrange)
509                                 event_throw(CHIMARA_GLK(widget), evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
510                 }
511                 else
512                         priv->ignore_next_arrange_event = FALSE;
513                 priv->needs_rearrange = FALSE;
514                 g_cond_signal(priv->rearranged);
515                 g_mutex_unlock(priv->arrange_lock);
516         }
517 }
518
519 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
520 static void
521 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
522 {
523         if(win->type == wintype_Pair)
524         {
525                 forall_recurse(win->window_node->children->data, callback, callback_data);
526                 forall_recurse(win->window_node->children->next->data, callback, callback_data);
527         }
528         else
529                 (*callback)(win->frame, callback_data);
530 }
531
532 /* Overrides gtk_container_forall */
533 static void
534 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
535 {
536     g_return_if_fail(container);
537     g_return_if_fail(CHIMARA_IS_GLK(container));
538     
539     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
540     
541         /* All the children are "internal" */
542         if(!include_internals)
543                 return;
544         
545     if(priv->root_window)
546                 forall_recurse(priv->root_window->data, callback, callback_data);
547 }
548
549 static void
550 chimara_glk_stopped(ChimaraGlk *self)
551 {
552     CHIMARA_GLK_USE_PRIVATE(self, priv);
553     priv->running = FALSE;
554
555     /* Free the plugin */
556         if( priv->program && !g_module_close(priv->program) )
557             g_warning( "Error closing module: %s", g_module_error() );
558 }
559
560 static void
561 chimara_glk_started(ChimaraGlk *self)
562 {
563         CHIMARA_GLK_USE_PRIVATE(self, priv);
564         priv->running = TRUE;
565 }
566
567 static void
568 chimara_glk_waiting(ChimaraGlk *self)
569 {
570         /* Default signal handler */
571 }
572
573 static void
574 chimara_glk_char_input(ChimaraGlk *self, guint window_rock, guint keysym)
575 {
576         /* Default signal handler */
577 }
578
579 static void
580 chimara_glk_line_input(ChimaraGlk *self, guint window_rock, gchar *text)
581 {
582         /* Default signal handler */
583 }
584
585 static void
586 chimara_glk_text_buffer_output(ChimaraGlk *self, guint window_rock, gchar *text)
587 {
588         /* Default signal handler */
589 }
590
591 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
592 #ifndef G_PARAM_STATIC_STRINGS
593 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
594 #endif
595
596 static void
597 chimara_glk_class_init(ChimaraGlkClass *klass)
598 {
599     /* Override methods of parent classes */
600     GObjectClass *object_class = G_OBJECT_CLASS(klass);
601     object_class->set_property = chimara_glk_set_property;
602     object_class->get_property = chimara_glk_get_property;
603     object_class->finalize = chimara_glk_finalize;
604     
605     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
606     widget_class->size_request = chimara_glk_size_request;
607     widget_class->size_allocate = chimara_glk_size_allocate;
608
609     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
610     container_class->forall = chimara_glk_forall;
611
612     /* Signals */
613     klass->stopped = chimara_glk_stopped;
614     klass->started = chimara_glk_started;
615     klass->waiting = chimara_glk_waiting;
616     klass->char_input = chimara_glk_char_input;
617     klass->line_input = chimara_glk_line_input;
618     klass->text_buffer_output = chimara_glk_text_buffer_output;
619     /**
620      * ChimaraGlk::stopped:
621      * @glk: The widget that received the signal
622      *
623      * Emitted when the a Glk program finishes executing in the widget, whether
624      * it ended normally, or was interrupted.
625      */ 
626     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
627         G_OBJECT_CLASS_TYPE(klass), 0, 
628         /* FIXME: Should be G_SIGNAL_RUN_CLEANUP but that segfaults??! */
629         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
630                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
631         /**
632          * ChimaraGlk::started:
633          * @glk: The widget that received the signal
634          *
635          * Emitted when a Glk program starts executing in the widget.
636          */
637         chimara_glk_signals[STARTED] = g_signal_new ("started",
638                 G_OBJECT_CLASS_TYPE(klass), 0,
639                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
640                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
641         /**
642          * ChimaraGlk::waiting:
643          * @glk: The widget that received the signal
644          * 
645          * Emitted when glk_select() is called by the Glk program and the event
646          * queue is empty, which means that the widget is waiting for input.
647          */
648         chimara_glk_signals[WAITING] = g_signal_new("waiting",
649                 G_OBJECT_CLASS_TYPE(klass), 0,
650                 G_STRUCT_OFFSET(ChimaraGlkClass, waiting), NULL, NULL,
651                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
652         /**
653          * ChimaraGlk::char-input:
654          * @glk: The widget that received the signal
655          * @window_rock: The rock value of the window that received character input
656          * (see <link linkend="chimara-Rocks">Rocks</link>)
657          * @keysym: The key that was typed, in the form of a key symbol from 
658          * <filename class="headerfile">gdk/gdkkeysyms.h</filename>
659          * 
660          * Emitted when a Glk window receives character input.
661          */
662         chimara_glk_signals[CHAR_INPUT] = g_signal_new("char-input",
663                 G_OBJECT_CLASS_TYPE(klass), 0,
664                 G_STRUCT_OFFSET(ChimaraGlkClass, char_input), NULL, NULL,
665                 chimara_marshal_VOID__UINT_UINT,
666                 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
667         /**
668          * ChimaraGlk::line-input:
669          * @glk: The widget that received the signal
670          * @window_rock: The rock value of the window that received line input (see
671          * <link linkend="chimara-Rocks">Rocks</link>)
672          * @text: The text that was typed
673          * 
674          * Emitted when a Glk window receives line input.
675          */
676         chimara_glk_signals[LINE_INPUT] = g_signal_new("line-input",
677                 G_OBJECT_CLASS_TYPE(klass), 0,
678                 G_STRUCT_OFFSET(ChimaraGlkClass, line_input), NULL, NULL,
679                 chimara_marshal_VOID__UINT_STRING,
680                 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
681         /**
682          * ChimaraGlk::text-buffer-output:
683          * @glk: The widget that received the signal
684          * @window_rock: The rock value of the window that was printed to (see <link
685          * linkend="chimara-Rocks">Rocks</link>)
686          * 
687          * Emitted when text is printed to a text buffer window.
688          */
689         chimara_glk_signals[TEXT_BUFFER_OUTPUT] = g_signal_new("text-buffer-output",
690                 G_OBJECT_CLASS_TYPE(klass), 0,
691                 G_STRUCT_OFFSET(ChimaraGlkClass, text_buffer_output), NULL, NULL,
692                 chimara_marshal_VOID__UINT_STRING,
693                 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
694
695     /* Properties */
696     /**
697      * ChimaraGlk:interactive:
698      *
699      * Sets whether the widget is interactive. A Glk widget is normally 
700      * interactive, but in non-interactive mode, keyboard and mouse input are 
701      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
702      * <quote>More</quote> prompts when a lot of text is printed to a text 
703          * buffer are also disabled. This is typically used when you wish to control
704          * an interpreter program by feeding it a predefined list of commands.
705      */
706     g_object_class_install_property( object_class, PROP_INTERACTIVE, 
707                 g_param_spec_boolean("interactive", _("Interactive"),
708         _("Whether user input is expected in the Glk program"),
709         TRUE,
710         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
711
712         /**
713      * ChimaraGlk:protect:
714      *
715      * Sets whether the Glk program is allowed to do file operations. In protect
716      * mode, all file operations will fail.
717      */
718     g_object_class_install_property(object_class, PROP_PROTECT, 
719                 g_param_spec_boolean("protect", _("Protected"),
720         _("Whether the Glk program is barred from doing file operations"),
721         FALSE,
722         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
723
724         /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
725          initialize them with NULL */
726         /**
727          * ChimaraGlk:default-font-description:
728          * 
729          * Pointer to a #PangoFontDescription describing the default proportional 
730          * font, to be used in text buffer windows for example.
731          *
732          * Default value: font description created from the string 
733          * <quote>Sans</quote>
734          */
735         g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, 
736                 g_param_spec_pointer("default-font-description", _("Default Font"),
737                 _("Font description of the default proportional font"),
738                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
739
740         /**
741          * ChimaraGlk:monospace-font-description:
742          *
743          * Pointer to a #PangoFontDescription describing the default monospace font,
744          * to be used in text grid windows and %style_Preformatted, for example.
745          *
746          * Default value: font description created from the string 
747          * <quote>Monospace</quote>
748          */
749         g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, 
750                 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
751                 _("Font description of the default monospace font"),
752                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
753
754         /**
755          * ChimaraGlk:spacing:
756          *
757          * The amount of space between the Glk windows.
758          */
759         g_object_class_install_property(object_class, PROP_SPACING,
760                 g_param_spec_uint("spacing", _("Spacing"),
761                 _("The amount of space between Glk windows"),
762                 0, G_MAXUINT, 0,
763                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
764         
765     /* Private data */
766     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
767 }
768
769 /* PUBLIC FUNCTIONS */
770
771 /**
772  * chimara_error_quark:
773  *
774  * The error domain for errors from Chimara widgets.
775  *
776  * Returns: The string <quote>chimara-error-quark</quote> as a <link 
777  * linkend="GQuark">GQuark</link>.
778  */
779 GQuark
780 chimara_error_quark(void)
781 {
782         chimara_init(); /* This is a library entry point */
783         return g_quark_from_static_string("chimara-error-quark");
784 }
785
786 /**
787  * chimara_glk_new:
788  *
789  * Creates and initializes a new #ChimaraGlk widget.
790  *
791  * Return value: a #ChimaraGlk widget, with a floating reference.
792  */
793 GtkWidget *
794 chimara_glk_new(void)
795 {
796         /* This is a library entry point; initialize the library */
797         chimara_init();
798
799     return GTK_WIDGET(g_object_new(CHIMARA_TYPE_GLK, NULL));
800 }
801
802 /**
803  * chimara_glk_set_interactive:
804  * @glk: a #ChimaraGlk widget
805  * @interactive: whether the widget should expect user input
806  *
807  * Sets the #ChimaraGlk:interactive property of @glk. 
808  */
809 void 
810 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
811 {
812     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
813     
814     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
815     priv->interactive = interactive;
816 }
817
818 /**
819  * chimara_glk_get_interactive:
820  * @glk: a #ChimaraGlk widget
821  *
822  * Returns whether @glk is interactive (expecting user input). See 
823  * #ChimaraGlk:interactive.
824  *
825  * Return value: %TRUE if @glk is interactive.
826  */
827 gboolean 
828 chimara_glk_get_interactive(ChimaraGlk *glk)
829 {
830     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
831     
832     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
833     return priv->interactive;
834 }
835
836 /**
837  * chimara_glk_set_protect:
838  * @glk: a #ChimaraGlk widget
839  * @protect: whether the widget should allow the Glk program to do file 
840  * operations
841  *
842  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
843  * program is not allowed to do file operations.
844  */
845 void 
846 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
847 {
848     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
849     
850     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
851     priv->protect = protect;
852 }
853
854 /**
855  * chimara_glk_get_protect:
856  * @glk: a #ChimaraGlk widget
857  *
858  * Returns whether @glk is in protect mode (banned from doing file operations).
859  * See #ChimaraGlk:protect.
860  *
861  * Return value: %TRUE if @glk is in protect mode.
862  */
863 gboolean 
864 chimara_glk_get_protect(ChimaraGlk *glk)
865 {
866     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
867     
868     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
869     return priv->protect;
870 }
871
872 /**
873  * chimara_glk_set_default_font_description:
874  * @glk: a #ChimaraGlk widget
875  * @font: a #PangoFontDescription
876  *
877  * Sets @glk's default proportional font. See 
878  * #ChimaraGlk:default-font-description.
879  */
880 void 
881 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
882 {
883         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
884         g_return_if_fail(font);
885         
886         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
887         pango_font_description_free(priv->default_font_desc);
888         priv->default_font_desc = pango_font_description_copy(font);
889         
890         /* TODO: Apply the font description to all the windows and recalculate the sizes */
891 }
892
893 /**
894  * chimara_glk_set_default_font_string:
895  * @glk: a #ChimaraGlk widget
896  * @font: string representation of a font description
897  *
898  * Sets @glk's default proportional font according to the string @font, which
899  * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
900  * [<replaceable>STYLE-OPTIONS</replaceable>] 
901  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia 
902  * Italic 12</quote> or <quote>Sans</quote>. See 
903  * #ChimaraGlk:default-font-description.
904  */
905 void 
906 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
907 {
908         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
909         g_return_if_fail(font || *font);
910         
911         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
912         g_return_if_fail(fontdesc);
913         
914         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
915         pango_font_description_free(priv->default_font_desc);
916         priv->default_font_desc = fontdesc;
917         
918         /* TODO: Apply the font description to all the windows and recalculate the sizes */
919 }
920         
921 /**
922  * chimara_glk_get_default_font_description:
923  * @glk: a #ChimaraGlk widget
924  * 
925  * Returns @glk's default proportional font.
926  *
927  * Return value: a newly-allocated #PangoFontDescription which must be freed
928  * using pango_font_description_free(), or %NULL on error.
929  */
930 PangoFontDescription *
931 chimara_glk_get_default_font_description(ChimaraGlk *glk)
932 {
933         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
934         
935         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
936         return pango_font_description_copy(priv->default_font_desc);
937 }
938
939 /**
940  * chimara_glk_set_monospace_font_description:
941  * @glk: a #ChimaraGlk widget
942  * @font: a #PangoFontDescription
943  *
944  * Sets @glk's default monospace font. See 
945  * #ChimaraGlk:monospace-font-description.
946  */
947 void 
948 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
949 {
950         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
951         g_return_if_fail(font);
952         
953         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
954         pango_font_description_free(priv->monospace_font_desc);
955         priv->monospace_font_desc = pango_font_description_copy(font);
956         
957         /* TODO: Apply the font description to all the windows and recalculate the sizes */
958 }
959
960 /**
961  * chimara_glk_set_monospace_font_string:
962  * @glk: a #ChimaraGlk widget
963  * @font: string representation of a font description
964  *
965  * Sets @glk's default monospace font according to the string @font, which must
966  * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
967  * [<replaceable>STYLE-OPTIONS</replaceable>] 
968  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier 
969  * Bold 12</quote> or <quote>Monospace</quote>. See 
970  * #ChimaraGlk:monospace-font-description.
971  */
972 void 
973 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
974 {
975         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
976         g_return_if_fail(font || *font);
977         
978         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
979         g_return_if_fail(fontdesc);
980         
981         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
982         pango_font_description_free(priv->monospace_font_desc);
983         priv->monospace_font_desc = fontdesc;
984         
985         /* TODO: Apply the font description to all the windows and recalculate the sizes */
986 }
987         
988 /**
989  * chimara_glk_get_monospace_font_description:
990  * @glk: a #ChimaraGlk widget
991  * 
992  * Returns @glk's default monospace font.
993  *
994  * Return value: a newly-allocated #PangoFontDescription which must be freed
995  * using pango_font_description_free(), or %NULL on error.
996  */
997 PangoFontDescription *
998 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
999 {
1000         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
1001         
1002         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1003         return pango_font_description_copy(priv->monospace_font_desc);
1004 }
1005
1006 /**
1007  * chimara_glk_set_spacing:
1008  * @glk: a #ChimaraGlk widget
1009  * @spacing: the number of pixels to put between Glk windows
1010  *
1011  * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
1012  * pixels between Glk windows.
1013  */
1014 void 
1015 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
1016 {
1017         g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
1018         
1019         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1020         priv->spacing = spacing;
1021 }
1022
1023 /**
1024  * chimara_glk_get_spacing:
1025  * @glk: a #ChimaraGlk widget
1026  *
1027  * Gets the value set by chimara_glk_set_spacing().
1028  *
1029  * Return value: pixels of spacing between Glk windows
1030  */
1031 guint 
1032 chimara_glk_get_spacing(ChimaraGlk *glk)
1033 {
1034         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
1035         
1036         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1037         return priv->spacing;
1038 }
1039
1040 struct StartupData {
1041         glk_main_t glk_main;
1042         glkunix_startup_code_t glkunix_startup_code;
1043         glkunix_startup_t args;
1044         ChimaraGlkPrivate *glk_data;
1045 };
1046
1047 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
1048 static gpointer
1049 glk_enter(struct StartupData *startup)
1050 {
1051         extern GPrivate *glk_data_key;
1052         g_private_set(glk_data_key, startup->glk_data);
1053         
1054         g_async_queue_ref(startup->glk_data->char_input_queue);
1055         g_async_queue_ref(startup->glk_data->line_input_queue);
1056         
1057         /* Run startup function */
1058         if(startup->glkunix_startup_code) {
1059                 startup->glk_data->in_startup = TRUE;
1060                 int result = startup->glkunix_startup_code(&startup->args);
1061                 startup->glk_data->in_startup = FALSE;
1062                 
1063                 int i = 0;
1064                 while(i < startup->args.argc)
1065                         g_free(startup->args.argv[i++]);
1066                 g_free(startup->args.argv);
1067                 
1068                 if(!result)
1069                         return NULL;
1070         }
1071         
1072         /* Run main function */
1073     g_signal_emit_by_name(startup->glk_data->self, "started");
1074         (startup->glk_main)();
1075         g_signal_emit_by_name(startup->glk_data->self, "stopped");
1076         g_slice_free(struct StartupData, startup);
1077         return NULL;
1078 }
1079
1080 /**
1081  * chimara_glk_run:
1082  * @glk: a #ChimaraGlk widget
1083  * @plugin: path to a plugin module compiled with <filename 
1084  * class="header">glk.h</filename>
1085  * @argc: Number of command line arguments in @argv
1086  * @argv: Array of command line arguments to pass to the plugin
1087  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
1088  * %NULL
1089  *
1090  * Opens a Glk program compiled as a plugin. Sorts out its command line
1091  * arguments from #glkunix_arguments, calls its startup function
1092  * glkunix_startup_code(), and then calls its main function glk_main() in
1093  * a separate thread. On failure, returns %FALSE and sets @error.
1094  *
1095  * The plugin must at least export a glk_main() function; #glkunix_arguments and
1096  * glkunix_startup_code() are optional.
1097  *
1098  * Return value: %TRUE if the Glk program was started successfully.
1099  */
1100 gboolean
1101 chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error)
1102 {
1103     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1104     g_return_val_if_fail(plugin, FALSE);
1105     
1106     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1107         struct StartupData *startup = g_slice_new0(struct StartupData);
1108     
1109     /* Open the module to run */
1110     g_assert( g_module_supported() );
1111     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
1112     
1113     if(!priv->program)
1114     {
1115         g_set_error(error, CHIMARA_ERROR, CHIMARA_LOAD_MODULE_ERROR, _("Error opening module: %s"), g_module_error());
1116         return FALSE;
1117     }
1118     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &startup->glk_main) )
1119     {
1120         g_set_error(error, CHIMARA_ERROR, CHIMARA_NO_GLK_MAIN, _("Error finding glk_main(): %s"), g_module_error());
1121         return FALSE;
1122     }
1123
1124     if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &startup->glkunix_startup_code) )
1125     {
1126                 glkunix_argumentlist_t *glkunix_arguments;
1127
1128                 if( !(g_module_symbol(priv->program, "glkunix_arguments", (gpointer *) &glkunix_arguments) 
1129                           && parse_command_line(glkunix_arguments, argc, argv, &startup->args)) )
1130                 {
1131                         /* arguments could not be parsed, so create data ourselves */
1132                         startup->args.argc = 1;
1133                         startup->args.argv = g_new0(gchar *, 1);
1134                 }
1135
1136                 /* Set the program name */
1137                 startup->args.argv[0] = g_strdup(plugin);
1138     }
1139         startup->glk_data = priv;
1140         
1141     /* Run in a separate thread */
1142         priv->thread = g_thread_create((GThreadFunc)glk_enter, startup, TRUE, error);
1143         
1144         return !(priv->thread == NULL);
1145 }
1146
1147 /**
1148  * chimara_glk_stop:
1149  * @glk: a #ChimaraGlk widget
1150  *
1151  * Signals the Glk program running in @glk to abort. Note that if the program is
1152  * caught in an infinite loop in which glk_tick() is not called, this may not
1153  * work.
1154  */
1155 void
1156 chimara_glk_stop(ChimaraGlk *glk)
1157 {
1158     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1159     CHIMARA_GLK_USE_PRIVATE(glk, priv);
1160     /* Don't do anything if not running a program */
1161     if(!priv->running)
1162         return;
1163     
1164         if(priv->abort_lock) {
1165                 g_mutex_lock(priv->abort_lock);
1166                 priv->abort_signalled = TRUE;
1167                 g_mutex_unlock(priv->abort_lock);
1168                 /* Stop blocking on the event queue condition */
1169                 event_throw(glk, evtype_Abort, NULL, 0, 0);
1170         }
1171 }
1172
1173 /**
1174  * chimara_glk_wait:
1175  * @glk: a #ChimaraGlk widget
1176  *
1177  * Holds up the main thread and waits for the Glk program running in @glk to 
1178  * finish.
1179  */
1180 void
1181 chimara_glk_wait(ChimaraGlk *glk)
1182 {
1183     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1184     CHIMARA_GLK_USE_PRIVATE(glk, priv);
1185     /* Don't do anything if not running a program */
1186     if(!priv->running)
1187         return;
1188     g_thread_join(priv->thread);
1189 }
1190
1191 /**
1192  * chimara_glk_get_running:
1193  * @glk: a #ChimaraGlk widget
1194  * 
1195  * Use this function to tell whether a program is currently running in the
1196  * widget.
1197  * 
1198  * Returns: %TRUE if @glk is executing a Glk program, %FALSE otherwise.
1199  */
1200 gboolean
1201 chimara_glk_get_running(ChimaraGlk *glk)
1202 {
1203         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
1204         CHIMARA_GLK_USE_PRIVATE(glk, priv);
1205         return priv->running;
1206 }
1207
1208 /**
1209  * chimara_glk_feed_char_input:
1210  * @glk: a #ChimaraGlk widget
1211  * @keyval: a key symbol as defined in <filename 
1212  * class="headerfile">gdk/gdkkeysyms.h</filename>
1213  * 
1214  * Pretend that a key was pressed in the Glk program as a response to a 
1215  * character input request. You can call this function even when no window has
1216  * requested character input, in which case the key will be saved for the 
1217  * following window that requests character input. This has the disadvantage 
1218  * that if more than one window has requested character input, it is arbitrary 
1219  * which one gets the key press.
1220  */
1221 void 
1222 chimara_glk_feed_char_input(ChimaraGlk *glk, guint keyval)
1223 {
1224         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1225         CHIMARA_GLK_USE_PRIVATE(glk, priv);
1226         g_async_queue_push(priv->char_input_queue, GUINT_TO_POINTER(keyval));
1227         event_throw(glk, evtype_ForcedCharInput, NULL, 0, 0);
1228 }
1229
1230 /**
1231  * chimara_glk_feed_line_input:
1232  * @glk: a #ChimaraGlk widget
1233  * @text: text to pass to the next line input request
1234  * 
1235  * Pretend that @text was typed in the Glk program as a response to a line input
1236  * request. @text does not need to end with a newline. You can call this 
1237  * function even when no window has requested line input, in which case the text
1238  * will be saved for the following window that requests line input. This has the 
1239  * disadvantage that if more than one window has requested character input, it 
1240  * is arbitrary which one gets the text.
1241  */
1242 void 
1243 chimara_glk_feed_line_input(ChimaraGlk *glk, const gchar *text)
1244 {
1245         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1246         g_return_if_fail(text);
1247         CHIMARA_GLK_USE_PRIVATE(glk, priv);
1248         g_async_queue_push(priv->line_input_queue, g_strdup(text));
1249         event_throw(glk, evtype_ForcedLineInput, NULL, 0, 0);
1250 }