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