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