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