Hopefully solved the problem of waiting for windows to draw themselves once and for...
[projects/chimara/chimara.git] / libchimara / chimara-glk.c
1 /* licensing and copyright information here */
2
3 #include <math.h>
4 #include <gtk/gtk.h>
5 #include <glib/gi18n.h>
6 #include <gmodule.h>
7 #include <pango/pango.h>
8 #include "chimara-glk.h"
9 #include "chimara-glk-private.h"
10 #include "glk.h"
11 #include "abort.h"
12 #include "window.h"
13 #include "glkstart.h"
14
15 #define CHIMARA_GLK_MIN_WIDTH 0
16 #define CHIMARA_GLK_MIN_HEIGHT 0
17
18 /**
19  * SECTION:chimara-glk
20  * @short_description: Widget which executes a Glk program
21  * @stability: Unstable
22  * @include: chimara/chimara-glk.h
23  * 
24  * The ChimaraGlk widget opens and runs a Glk program. The program must be
25  * compiled as a plugin module, with a function <function>glk_main()</function>
26  * that the Glk library can hook into.
27  *
28  * On Linux systems, this is a file with a name like 
29  * <filename>plugin.so</filename>. For portability, you can use libtool and 
30  * automake:
31  * |[
32  * pkglib_LTLIBRARIES = plugin.la
33  * plugin_la_SOURCES = plugin.c foo.c bar.c
34  * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
35  * ]|
36  * This will produce <filename>plugin.la</filename> which is a text file 
37  * containing the correct plugin file to open (see the relevant section of the
38  * <ulink 
39  * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
40  * Libtool manual</ulink>).
41  */
42
43 typedef void (* glk_main_t) (void);
44 typedef void (* glkunix_startup_code_t) (glkunix_startup_t*);
45
46 enum {
47     PROP_0,
48     PROP_INTERACTIVE,
49     PROP_PROTECT,
50         PROP_DEFAULT_FONT_DESCRIPTION,
51         PROP_MONOSPACE_FONT_DESCRIPTION,
52         PROP_SPACING
53 };
54
55 enum {
56         STOPPED,
57         STARTED,
58
59         LAST_SIGNAL
60 };
61
62 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
63
64 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
65
66 static void
67 chimara_glk_init(ChimaraGlk *self)
68 {
69     GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
70
71     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
72     
73     priv->self = self;
74     priv->interactive = TRUE;
75     priv->protect = FALSE;
76         priv->default_font_desc = pango_font_description_from_string("Sans");
77         priv->monospace_font_desc = pango_font_description_from_string("Monospace");
78     priv->program = NULL;
79     priv->thread = NULL;
80     priv->event_queue = NULL;
81     priv->event_lock = NULL;
82     priv->event_queue_not_empty = NULL;
83     priv->event_queue_not_full = NULL;
84     priv->abort_lock = NULL;
85     priv->abort_signalled = FALSE;
86         priv->arrange_lock = NULL;
87         priv->rearranged = NULL;
88         priv->needs_rearrange = FALSE;
89         priv->ignore_next_arrange_event = FALSE;
90     priv->interrupt_handler = NULL;
91     priv->root_window = NULL;
92     priv->fileref_list = NULL;
93     priv->current_stream = NULL;
94     priv->stream_list = NULL;
95         priv->timer_id = 0;
96 }
97
98 static void
99 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
100 {
101     ChimaraGlk *glk = CHIMARA_GLK(object);
102     
103     switch(prop_id) 
104     {
105         case PROP_INTERACTIVE:
106             chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
107             break;
108         case PROP_PROTECT:
109             chimara_glk_set_protect( glk, g_value_get_boolean(value) );
110             break;
111                 case PROP_DEFAULT_FONT_DESCRIPTION:
112                         chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
113                         break;
114                 case PROP_MONOSPACE_FONT_DESCRIPTION:
115                         chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
116                         break;
117                 case PROP_SPACING:
118                         chimara_glk_set_spacing( glk, g_value_get_uint(value) );
119                         break;
120         default:
121             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
122     }
123 }
124
125 static void
126 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
127 {
128     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
129     
130     switch(prop_id)
131     {
132         case PROP_INTERACTIVE:
133             g_value_set_boolean(value, priv->interactive);
134             break;
135         case PROP_PROTECT:
136             g_value_set_boolean(value, priv->protect);
137             break;
138                 case PROP_DEFAULT_FONT_DESCRIPTION:
139                         g_value_set_pointer(value, priv->default_font_desc);
140                         break;
141                 case PROP_MONOSPACE_FONT_DESCRIPTION:
142                         g_value_set_pointer(value, priv->monospace_font_desc);
143                         break;
144                 case PROP_SPACING:
145                         g_value_set_uint(value, priv->spacing);
146                         break;
147         default:
148             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
149     }
150 }
151
152 static void
153 chimara_glk_finalize(GObject *object)
154 {
155     ChimaraGlk *self = CHIMARA_GLK(object);
156     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
157     
158     /* Free the event queue */
159     g_mutex_lock(priv->event_lock);
160         g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
161         g_queue_free(priv->event_queue);
162         g_cond_free(priv->event_queue_not_empty);
163         g_cond_free(priv->event_queue_not_full);
164         priv->event_queue = NULL;
165         g_mutex_unlock(priv->event_lock);
166         g_mutex_free(priv->event_lock);
167         
168         /* Free the abort signalling mechanism */
169         g_mutex_lock(priv->abort_lock);
170         /* Make sure no other thread is busy with this */
171         g_mutex_unlock(priv->abort_lock);
172         g_mutex_free(priv->abort_lock);
173         priv->abort_lock = NULL;
174
175         /* Free the window arrangement signalling */
176         g_mutex_lock(priv->arrange_lock);
177         g_cond_free(priv->rearranged);
178         g_mutex_unlock(priv->arrange_lock);
179         g_mutex_free(priv->arrange_lock);
180         priv->arrange_lock = NULL;
181         
182         /* Free private data */
183         pango_font_description_free(priv->default_font_desc);
184         pango_font_description_free(priv->monospace_font_desc);
185         
186     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
187 }
188
189 /* Internal function: Recursively get the Glk window tree's size request */
190 static void
191 request_recurse(winid_t win, GtkRequisition *requisition, guint spacing)
192 {
193         if(win->type == wintype_Pair)
194         {
195                 /* Get children's size requests */
196                 GtkRequisition child1, child2;
197                 request_recurse(win->window_node->children->data, &child1, spacing);
198                 request_recurse(win->window_node->children->next->data, &child2, spacing);
199                 
200                 /* If the split is fixed, get the size of the fixed child */
201                 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
202                 {
203                         switch(win->split_method & winmethod_DirMask)
204                         {
205                                 case winmethod_Left:
206                                         child1.width = win->key_window?
207                                                 win->constraint_size * win->key_window->unit_width
208                                                 : 0;
209                                         break;
210                                 case winmethod_Right:
211                                         child2.width = win->key_window?
212                                                 win->constraint_size * win->key_window->unit_width
213                                                 : 0;
214                                         break;
215                                 case winmethod_Above:
216                                         child1.height = win->key_window?
217                                                 win->constraint_size * win->key_window->unit_height
218                                                 : 0;
219                                         break;
220                                 case winmethod_Below:
221                                         child2.height = win->key_window?
222                                                 win->constraint_size * win->key_window->unit_height
223                                                 : 0;
224                                         break;
225                         }
226                 }
227                 
228                 /* Add the children's requests */
229                 switch(win->split_method & winmethod_DirMask)
230                 {
231                         case winmethod_Left:
232                         case winmethod_Right:
233                                 requisition->width = child1.width + child2.width + spacing;
234                                 requisition->height = MAX(child1.height, child2.height);
235                                 break;
236                         case winmethod_Above:
237                         case winmethod_Below:
238                                 requisition->width = MAX(child1.width, child2.width);
239                                 requisition->height = child1.height + child2.height + spacing;
240                                 break;
241                 }
242         }
243         
244         /* For non-pair windows, just use the size that GTK requests */
245         else
246                 gtk_widget_size_request(win->frame, requisition);
247 }
248
249 /* Overrides gtk_widget_size_request */
250 static void
251 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
252 {
253     g_return_if_fail(widget);
254     g_return_if_fail(requisition);
255     g_return_if_fail(CHIMARA_IS_GLK(widget));
256     
257     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
258     
259     /* For now, just pass the size request on to the root Glk window */
260     if(priv->root_window) 
261         {
262                 request_recurse(priv->root_window->data, requisition, priv->spacing);
263                 requisition->width += 2 * GTK_CONTAINER(widget)->border_width;
264                 requisition->height += 2 * GTK_CONTAINER(widget)->border_width;
265         } 
266         else 
267         {
268         requisition->width = CHIMARA_GLK_MIN_WIDTH + 2 * GTK_CONTAINER(widget)->border_width;
269         requisition->height = CHIMARA_GLK_MIN_HEIGHT + 2 * GTK_CONTAINER(widget)->border_width;
270     }
271 }
272
273 /* Recursively give the Glk windows their allocated space. Returns a window
274  containing all children of this window that must be redrawn, or NULL if there 
275  are no children that require redrawing. */
276 static winid_t
277 allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
278 {
279         if(win->type == wintype_Pair)
280         {
281                 GtkAllocation child1, child2;
282                 child1.x = allocation->x;
283                 child1.y = allocation->y;
284                 
285                 if((win->split_method & winmethod_DivisionMask) == winmethod_Fixed)
286                 {
287                         /* If the key window has been closed, then default to 0; otherwise
288                          use the key window to determine the size */
289                         switch(win->split_method & winmethod_DirMask)
290                         {
291                                 case winmethod_Left:
292                                         child1.width = win->key_window? 
293                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing) 
294                                                 : 0;
295                                         break;
296                                 case winmethod_Right:
297                                         child2.width = win->key_window? 
298                                                 CLAMP(win->constraint_size * win->key_window->unit_width, 0, allocation->width - spacing)
299                                                 : 0;
300                                         break;
301                                 case winmethod_Above:
302                                         child1.height = win->key_window? 
303                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
304                                                 : 0;
305                                         break;
306                                 case winmethod_Below:
307                                         child2.height = win->key_window?
308                                                 CLAMP(win->constraint_size * win->key_window->unit_height, 0, allocation->height - spacing)
309                                                 : 0;
310                                         break;
311                         }
312                 }
313                 else /* proportional */
314                 {
315                         gdouble fraction = win->constraint_size / 100.0;
316                         switch(win->split_method & winmethod_DirMask)
317                         {
318                                 case winmethod_Left:
319                                         child1.width = (glui32) ceil( fraction * (allocation->width - spacing) );
320                                         break;
321                                 case winmethod_Right:
322                                         child2.width = (glui32) ceil( fraction * (allocation->width - spacing) );
323                                         break;
324                                 case winmethod_Above:
325                                         child1.height = (glui32) ceil( fraction * (allocation->height - spacing) );
326                                         break;
327                                 case winmethod_Below:
328                                         child2.height = (glui32) ceil( fraction * (allocation->height - spacing) );
329                                         break;
330                         }
331                 }
332                 
333                 /* Fill in the rest of the size requisitions according to the child specified above */
334                 switch(win->split_method & winmethod_DirMask)
335                 {
336                         case winmethod_Left:
337                                 child2.width = allocation->width - spacing - child1.width;
338                                 child2.x = child1.x + child1.width + spacing;
339                                 child2.y = child1.y;
340                                 child1.height = child2.height = allocation->height;
341                                 break;
342                         case winmethod_Right:
343                                 child1.width = allocation->width - spacing - child2.width;
344                                 child2.x = child1.x + child1.width + spacing;
345                                 child2.y = child1.y;
346                                 child1.height = child2.height = allocation->height;
347                                 break;
348                         case winmethod_Above:
349                                 child2.height = allocation->height - spacing - child1.height;
350                                 child2.x = child1.x;
351                                 child2.y = child1.y + child1.height + spacing;
352                                 child1.width = child2.width = allocation->width;
353                                 break;
354                         case winmethod_Below:
355                                 child1.height = allocation->height - spacing - child2.height;
356                                 child2.x = child1.x;
357                                 child2.y = child1.y + child1.height + spacing;
358                                 child1.width = child2.width = allocation->width;
359                                 break;
360                 }
361                 
362                 /* Recurse */
363                 winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
364                 winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
365                 if(arrange1 == NULL)
366                         return arrange2;
367                 if(arrange2 == NULL)
368                         return arrange1;
369                 return win;
370         }
371         
372         else if(win->type == wintype_TextGrid)
373         {
374                 /* Pass the size allocation on to the framing widget */
375                 gtk_widget_size_allocate(win->frame, allocation);
376                 /* It says in the spec that when a text grid window is resized smaller,
377                  the bottom or right area is thrown away; when it is resized larger, the
378                  bottom or right area is filled with blanks. */
379                 glui32 newwidth = (glui32)(win->widget->allocation.width / win->unit_width);
380                 glui32 newheight = (glui32)(win->widget->allocation.height / win->unit_height);
381                 gint line;
382                 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
383                 GtkTextIter start, end;
384         
385                 for(line = 0; line < win->height; line++)
386                 {
387                         gtk_text_buffer_get_iter_at_line(textbuffer, &start, line);
388                         /* If this line is going to fall off the bottom, delete it */
389                         if(line >= newheight)
390                         {
391                                 end = start;
392                                 gtk_text_iter_forward_to_line_end(&end);
393                                 gtk_text_iter_forward_char(&end);
394                                 gtk_text_buffer_delete(textbuffer, &start, &end);
395                                 break;
396                         }
397                         /* If this line is not long enough, add spaces on the end */
398                         if(newwidth > win->width)
399                         {
400                                 gchar *spaces = g_strnfill(newwidth - win->width, ' ');
401                                 gtk_text_iter_forward_to_line_end(&start);
402                                 gtk_text_buffer_insert(textbuffer, &start, spaces, -1);
403                                 g_free(spaces);
404                         }
405                         /* But if it's too long, delete characters from the end */
406                         else if(newwidth < win->width)
407                         {
408                                 end = start;
409                                 gtk_text_iter_forward_chars(&start, newwidth);
410                                 gtk_text_iter_forward_to_line_end(&end);
411                                 gtk_text_buffer_delete(textbuffer, &start, &end);
412                         }
413                         /* Note: if the widths are equal, do nothing */
414                 }
415                 /* Add blank lines if there aren't enough lines to fit the new size */
416                 if(newheight > win->height)
417                 {
418                         gchar *blanks = g_strnfill(win->width, ' ');
419                     gchar **blanklines = g_new0(gchar *, (newheight - win->height) + 1);
420                     int count;
421                     for(count = 0; count < newheight - win->height; count++)
422                         blanklines[count] = blanks;
423                     blanklines[newheight - win->height] = NULL;
424                     gchar *text = g_strjoinv("\n", blanklines);
425                     g_free(blanklines); /* not g_strfreev() */
426                     g_free(blanks);
427                     
428                         gtk_text_buffer_get_end_iter(textbuffer, &start);
429                         gtk_text_buffer_insert(textbuffer, &start, "\n", -1);
430                     gtk_text_buffer_insert(textbuffer, &start, text, -1);
431                     g_free(text);
432                 }
433         
434                 gboolean arrange = !(win->width == newwidth && win->height == newheight);
435                 win->width = newwidth;
436                 win->height = newheight;
437                 return arrange? win : NULL;
438         }
439         
440         /* For non-pair, non-text-grid windows, just give them the size */
441         gtk_widget_size_allocate(win->frame, allocation);
442         return NULL;
443 }
444
445 /* Overrides gtk_widget_size_allocate */
446 static void
447 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
448 {
449     g_return_if_fail(widget);
450     g_return_if_fail(allocation);
451     g_return_if_fail(CHIMARA_IS_GLK(widget));
452     
453     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
454     
455     widget->allocation = *allocation;
456             
457     if(priv->root_window) {
458                 GtkAllocation child;
459                 child.x = allocation->x + GTK_CONTAINER(widget)->border_width;
460                 child.y = allocation->y + GTK_CONTAINER(widget)->border_width;
461                 child.width = CLAMP(allocation->width - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->width);
462                 child.height = CLAMP(allocation->height - 2 * GTK_CONTAINER(widget)->border_width, 0, allocation->height);
463                 winid_t arrange = allocate_recurse(priv->root_window->data, &child, priv->spacing);
464                 
465                 /* arrange points to a window that contains all text grid and graphics
466                  windows which have been resized */
467                 g_mutex_lock(priv->arrange_lock);
468                 if(!priv->ignore_next_arrange_event)
469                 {
470                         if(arrange)
471                                 event_throw(evtype_Arrange, arrange == priv->root_window->data? NULL : arrange, 0, 0);
472                 }
473                 else
474                         priv->ignore_next_arrange_event = FALSE;
475                 priv->needs_rearrange = FALSE;
476                 g_cond_signal(priv->rearranged);
477                 g_mutex_unlock(priv->arrange_lock);
478         }
479 }
480
481 /* Recursively invoke callback() on the GtkWidget of each non-pair window in the tree */
482 static void
483 forall_recurse(winid_t win, GtkCallback callback, gpointer callback_data)
484 {
485         if(win->type == wintype_Pair)
486         {
487                 forall_recurse(win->window_node->children->data, callback, callback_data);
488                 forall_recurse(win->window_node->children->next->data, callback, callback_data);
489         }
490         else
491                 (*callback)(win->frame, callback_data);
492 }
493
494 /* Overrides gtk_container_forall */
495 static void
496 chimara_glk_forall(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data)
497 {
498     g_return_if_fail(container);
499     g_return_if_fail(CHIMARA_IS_GLK(container));
500     
501     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
502     
503         /* All the children are "internal" */
504         if(!include_internals)
505                 return;
506         
507     if(priv->root_window)
508                 forall_recurse(priv->root_window->data, callback, callback_data);
509 }
510
511 static void
512 chimara_glk_stopped(ChimaraGlk *self)
513 {
514     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
515
516     /* Free the plugin */
517         if( priv->program && !g_module_close(priv->program) )
518             g_warning( "Error closing module: %s", g_module_error() );
519 }
520
521 static void
522 chimara_glk_started(ChimaraGlk *self)
523 {
524         /* TODO: Add default signal handler implementation here */
525 }
526
527 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
528 #ifndef G_PARAM_STATIC_STRINGS
529 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
530 #endif
531
532 static void
533 chimara_glk_class_init(ChimaraGlkClass *klass)
534 {
535     /* Override methods of parent classes */
536     GObjectClass *object_class = G_OBJECT_CLASS(klass);
537     object_class->set_property = chimara_glk_set_property;
538     object_class->get_property = chimara_glk_get_property;
539     object_class->finalize = chimara_glk_finalize;
540     
541     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
542     widget_class->size_request = chimara_glk_size_request;
543     widget_class->size_allocate = chimara_glk_size_allocate;
544
545     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
546     container_class->forall = chimara_glk_forall;
547
548     /* Signals */
549     klass->stopped = chimara_glk_stopped;
550     klass->started = chimara_glk_started;
551     /**
552      * ChimaraGlk::stopped:
553      * @glk: The widget that received the signal
554      *
555      * The ::stopped signal is emitted when the a Glk program finishes
556      * executing in the widget, whether it ended normally, or was interrupted.
557      */ 
558     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
559         G_OBJECT_CLASS_TYPE(klass), 0, 
560         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
561                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
562         /**
563          * ChimaraGlk::started:
564          * @glk: The widget that received the signal
565          *
566          * The ::started signal is emitted when a Glk program starts executing in
567          * the widget.
568          */
569         chimara_glk_signals[STARTED] = g_signal_new ("started",
570                 G_OBJECT_CLASS_TYPE (klass), 0,
571                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
572                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
573
574     /* Properties */
575     /**
576      * ChimaraGlk:interactive:
577      *
578      * Sets whether the widget is interactive. A Glk widget is normally 
579      * interactive, but in non-interactive mode, keyboard and mouse input are 
580      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
581      * <quote>More</quote> prompts when a lot of text is printed to a text 
582          * buffer are also disabled. This is typically used when you wish to control
583          * an interpreter program by feeding it a predefined list of commands.
584      */
585     g_object_class_install_property( object_class, PROP_INTERACTIVE, 
586                 g_param_spec_boolean("interactive", _("Interactive"),
587         _("Whether user input is expected in the Glk program"),
588         TRUE,
589         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
590
591         /**
592      * ChimaraGlk:protect:
593      *
594      * Sets whether the Glk program is allowed to do file operations. In protect
595      * mode, all file operations will fail.
596      */
597     g_object_class_install_property(object_class, PROP_PROTECT, 
598                 g_param_spec_boolean("protect", _("Protected"),
599         _("Whether the Glk program is barred from doing file operations"),
600         FALSE,
601         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
602
603         /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
604          initialize them with NULL */
605         /**
606          * ChimaraGlk:default-font-description:
607          * 
608          * Pointer to a #PangoFontDescription describing the default proportional 
609          * font, to be used in text buffer windows for example.
610          *
611          * Default value: font description created from the string 
612          * <quote>Sans</quote>
613          */
614         g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, 
615                 g_param_spec_pointer("default-font-description", _("Default Font"),
616                 _("Font description of the default proportional font"),
617                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
618
619         /**
620          * ChimaraGlk:monospace-font-description:
621          *
622          * Pointer to a #PangoFontDescription describing the default monospace font,
623          * to be used in text grid windows and #style_Preformatted, for example.
624          *
625          * Default value: font description created from the string 
626          * <quote>Monospace</quote>
627          */
628         g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, 
629                 g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
630                 _("Font description of the default monospace font"),
631                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
632
633         /**
634          * ChimaraGlk:spacing:
635          *
636          * The amount of space between the Glk windows.
637          */
638         g_object_class_install_property(object_class, PROP_SPACING,
639                 g_param_spec_uint("spacing", _("Spacing"),
640                 _("The amount of space between Glk windows"),
641                 0, G_MAXUINT, 0,
642                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS) );
643         
644     /* Private data */
645     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
646 }
647
648 /* PUBLIC FUNCTIONS */
649
650 /**
651  * chimara_glk_new:
652  *
653  * Creates and initializes a new #ChimaraGlk widget.
654  *
655  * Return value: a #ChimaraGlk widget, with a floating reference.
656  */
657 GtkWidget *
658 chimara_glk_new(void)
659 {
660     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
661     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
662     
663     priv->event_queue = g_queue_new();
664     priv->event_lock = g_mutex_new();
665     priv->event_queue_not_empty = g_cond_new();
666     priv->event_queue_not_full = g_cond_new();
667     priv->abort_lock = g_mutex_new();
668         priv->arrange_lock = g_mutex_new();
669         priv->rearranged = g_cond_new();
670     
671     return GTK_WIDGET(self);
672 }
673
674 /**
675  * chimara_glk_set_interactive:
676  * @glk: a #ChimaraGlk widget
677  * @interactive: whether the widget should expect user input
678  *
679  * Sets the #ChimaraGlk:interactive property of @glk. 
680  */
681 void 
682 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
683 {
684     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
685     
686     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
687     priv->interactive = interactive;
688 }
689
690 /**
691  * chimara_glk_get_interactive:
692  * @glk: a #ChimaraGlk widget
693  *
694  * Returns whether @glk is interactive (expecting user input). See 
695  * #ChimaraGlk:interactive.
696  *
697  * Return value: %TRUE if @glk is interactive.
698  */
699 gboolean 
700 chimara_glk_get_interactive(ChimaraGlk *glk)
701 {
702     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
703     
704     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
705     return priv->interactive;
706 }
707
708 /**
709  * chimara_glk_set_protect:
710  * @glk: a #ChimaraGlk widget
711  * @protect: whether the widget should allow the Glk program to do file 
712  * operations
713  *
714  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
715  * program is not allowed to do file operations.
716  */
717 void 
718 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
719 {
720     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
721     
722     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
723     priv->protect = protect;
724 }
725
726 /**
727  * chimara_glk_get_protect:
728  * @glk: a #ChimaraGlk widget
729  *
730  * Returns whether @glk is in protect mode (banned from doing file operations).
731  * See #ChimaraGlk:protect.
732  *
733  * Return value: %TRUE if @glk is in protect mode.
734  */
735 gboolean 
736 chimara_glk_get_protect(ChimaraGlk *glk)
737 {
738     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
739     
740     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
741     return priv->protect;
742 }
743
744 /**
745  * chimara_glk_set_default_font_description:
746  * @glk: a #ChimaraGlk widget
747  * @font: a #PangoFontDescription
748  *
749  * Sets @glk's default proportional font. See 
750  * #ChimaraGlk:default-font-description.
751  */
752 void 
753 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
754 {
755         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
756         g_return_if_fail(font);
757         
758         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
759         pango_font_description_free(priv->default_font_desc);
760         priv->default_font_desc = pango_font_description_copy(font);
761         
762         /* TODO: Apply the font description to all the windows and recalculate the sizes */
763 }
764
765 /**
766  * chimara_glk_set_default_font_string:
767  * @glk: a #ChimaraGlk widget
768  * @font: string representation of a font description
769  *
770  * Sets @glk's default proportional font according to the string @font, which
771  * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
772  * [<replaceable>STYLE-OPTIONS</replaceable>] 
773  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia 
774  * Italic 12</quote> or <quote>Sans</quote>. See 
775  * #ChimaraGlk:default-font-description.
776  */
777 void 
778 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
779 {
780         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
781         g_return_if_fail(font || *font);
782         
783         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
784         g_return_if_fail(fontdesc);
785         
786         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
787         pango_font_description_free(priv->default_font_desc);
788         priv->default_font_desc = fontdesc;
789         
790         /* TODO: Apply the font description to all the windows and recalculate the sizes */
791 }
792         
793 /**
794  * chimara_glk_get_default_font_description:
795  * @glk: a #ChimaraGlk widget
796  * 
797  * Returns @glk's default proportional font.
798  *
799  * Return value: a newly-allocated #PangoFontDescription which must be freed
800  * using pango_font_description_free(), or %NULL on error.
801  */
802 PangoFontDescription *
803 chimara_glk_get_default_font_description(ChimaraGlk *glk)
804 {
805         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
806         
807         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
808         return pango_font_description_copy(priv->default_font_desc);
809 }
810
811 /**
812  * chimara_glk_set_monospace_font_description:
813  * @glk: a #ChimaraGlk widget
814  * @font: a #PangoFontDescription
815  *
816  * Sets @glk's default monospace font. See 
817  * #ChimaraGlk:monospace-font-description.
818  */
819 void 
820 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
821 {
822         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
823         g_return_if_fail(font);
824         
825         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
826         pango_font_description_free(priv->monospace_font_desc);
827         priv->monospace_font_desc = pango_font_description_copy(font);
828         
829         /* TODO: Apply the font description to all the windows and recalculate the sizes */
830 }
831
832 /**
833  * chimara_glk_set_monospace_font_string:
834  * @glk: a #ChimaraGlk widget
835  * @font: string representation of a font description
836  *
837  * Sets @glk's default monospace font according to the string @font, which must
838  * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable> 
839  * [<replaceable>STYLE-OPTIONS</replaceable>] 
840  * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier 
841  * Bold 12</quote> or <quote>Monospace</quote>. See 
842  * #ChimaraGlk:monospace-font-description.
843  */
844 void 
845 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
846 {
847         g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
848         g_return_if_fail(font || *font);
849         
850         PangoFontDescription *fontdesc = pango_font_description_from_string(font);
851         g_return_if_fail(fontdesc);
852         
853         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
854         pango_font_description_free(priv->monospace_font_desc);
855         priv->monospace_font_desc = fontdesc;
856         
857         /* TODO: Apply the font description to all the windows and recalculate the sizes */
858 }
859         
860 /**
861  * chimara_glk_get_monospace_font_description:
862  * @glk: a #ChimaraGlk widget
863  * 
864  * Returns @glk's default monospace font.
865  *
866  * Return value: a newly-allocated #PangoFontDescription which must be freed
867  * using pango_font_description_free(), or %NULL on error.
868  */
869 PangoFontDescription *
870 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
871 {
872         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
873         
874         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
875         return pango_font_description_copy(priv->monospace_font_desc);
876 }
877
878 /**
879  * chimara_glk_set_spacing:
880  * @glk: a #ChimaraGlk widget
881  * @spacing: the number of pixels to put between Glk windows
882  *
883  * Sets the #ChimaraGlk:spacing property of @glk, which is the border width in
884  * pixels between Glk windows.
885  */
886 void 
887 chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing)
888 {
889         g_return_if_fail( glk || CHIMARA_IS_GLK(glk) );
890         
891         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
892         priv->spacing = spacing;
893 }
894
895 /**
896  * chimara_glk_get_spacing:
897  * @glk: a #ChimaraGlk widget
898  *
899  * Gets the value set by chimara_glk_set_spacing().
900  *
901  * Return value: pixels of spacing between Glk windows
902  */
903 guint 
904 chimara_glk_get_spacing(ChimaraGlk *glk)
905 {
906         g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), 0);
907         
908         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
909         return priv->spacing;
910 }
911
912 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
913 static gpointer
914 glk_enter(gpointer glk_main)
915 {
916     extern ChimaraGlkPrivate *glk_data;
917
918     g_signal_emit_by_name(glk_data->self, "started");
919         ((glk_main_t)glk_main)();
920         g_signal_emit_by_name(glk_data->self, "stopped");
921         return NULL;
922 }
923
924 /**
925  * chimara_glk_run:
926  * @glk: a #ChimaraGlk widget
927  * @plugin: path to a plugin module compiled with <filename 
928  * class="header">glk.h</filename>
929  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
930  * %NULL
931  *
932  * Opens a Glk program compiled as a plugin and runs its glk_main() function in
933  * a separate thread. On failure, returns %FALSE and sets @error.
934  *
935  * Return value: %TRUE if the Glk program was started successfully.
936  */
937 gboolean
938 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError **error)
939 {
940     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
941     g_return_val_if_fail(plugin, FALSE);
942     
943     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
944     
945
946     /* Open the module to run */
947     glk_main_t glk_main;
948         glkunix_startup_code_t glkunix_startup_code;
949     g_assert( g_module_supported() );
950     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
951     
952     if(!priv->program)
953     {
954         g_warning( "Error opening module: %s", g_module_error() );
955         return FALSE;
956     }
957     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
958     {
959         g_warning( "Error finding glk_main(): %s", g_module_error() );
960         return FALSE;
961     }
962
963     extern ChimaraGlkPrivate *glk_data;
964     /* Set the thread's private data */
965     /* TODO: Do this with a GPrivate */
966     glk_data = priv;
967
968     if( g_module_symbol(priv->program, "glkunix_startup_code", (gpointer *) &glkunix_startup_code) )
969     {
970                 glkunix_startup_t data;
971                 data.argc = argc;
972                 data.argv = argv;
973
974                 glkunix_startup_code(&data);
975     }
976
977     /* Run in a separate thread */
978         priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
979         
980         return !(priv->thread == NULL);
981 }
982
983 /**
984  * chimara_glk_stop:
985  * @glk: a #ChimaraGlk widget
986  *
987  * Signals the Glk program running in @glk to abort. Note that if the program is
988  * caught in an infinite loop in which glk_tick() is not called, this may not
989  * work.
990  */
991 void
992 chimara_glk_stop(ChimaraGlk *glk)
993 {
994     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
995     /* TODO: check if glk is actually running a program */
996     signal_abort();
997 }
998
999 /**
1000  * chimara_glk_wait:
1001  * @glk: a #ChimaraGlk widget
1002  *
1003  * Holds up the main thread and waits for the Glk program running in @glk to 
1004  * finish.
1005  */
1006 void
1007 chimara_glk_wait(ChimaraGlk *glk)
1008 {
1009     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
1010     
1011     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
1012     g_thread_join(priv->thread);
1013 }