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