Got Gtk-Doc working. Now all the fancy /** comments before the functions
[rodin/chimara.git] / src / chimara-glk.c
1 /* licensing and copyright information here */
2
3 #include <gtk/gtk.h>
4 #include <glib/gi18n.h>
5 #include <gmodule.h>
6 #include "chimara-glk.h"
7 #include "chimara-glk-private.h"
8 #include "glk.h"
9 #include "abort.h"
10 #include "window.h"
11
12 #define CHIMARA_GLK_MIN_WIDTH 0
13 #define CHIMARA_GLK_MIN_HEIGHT 0
14
15 /**
16  * SECTION:chimara-glk
17  * @short_description: Widget which executes a Glk program
18  * @stability: Unstable
19  * @include: chimara/chimara-glk.h
20  * 
21  * The ChimaraGlk widget opens and runs a Glk program. The program must be
22  * compiled as a plugin module, with a function <function>glk_main()</function>
23  * that the Glk library can hook into.
24  *
25  * On Linux systems, this is a file with a name like 
26  * <filename>plugin.so</filename>. For portability, you can use libtool and 
27  * automake:
28  * <informalexample><programlisting>
29  * pkglib_LTLIBRARIES = plugin.la
30  * plugin_la_SOURCES = plugin.c foo.c bar.c
31  * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
32  * </programlisting></informalexample>
33  * This will produce <filename>plugin.la</filename> which is a text file 
34  * containing the correct plugin file to open (see the relevant section of the
35  * <ulink 
36  * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
37  * Libtool manual</ulink>).
38  */
39
40 typedef void (* glk_main_t) (void);
41
42 enum {
43     PROP_0,
44     PROP_INTERACTIVE,
45     PROP_PROTECT
46 };
47
48 enum {
49         STOPPED,
50         STARTED,
51
52         LAST_SIGNAL
53 };
54
55 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
56
57 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
58
59 static void
60 chimara_glk_init(ChimaraGlk *self)
61 {
62     GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
63
64     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
65     
66     priv->self = self;
67     priv->interactive = TRUE;
68     priv->protect = FALSE;
69     priv->program = NULL;
70     priv->thread = NULL;
71     priv->event_queue = NULL;
72     priv->event_lock = NULL;
73     priv->event_queue_not_empty = NULL;
74     priv->event_queue_not_full = NULL;
75     priv->abort_lock = NULL;
76     priv->abort_signalled = FALSE;
77     priv->interrupt_handler = NULL;
78     priv->root_window = NULL;
79     priv->fileref_list = NULL;
80     priv->current_stream = NULL;
81     priv->stream_list = NULL;
82 }
83
84 static void
85 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
86 {
87     ChimaraGlk *glk = CHIMARA_GLK(object);
88     
89     switch(prop_id) 
90     {
91         case PROP_INTERACTIVE:
92             chimara_glk_set_interactive(glk, g_value_get_boolean(value));
93             break;
94         case PROP_PROTECT:
95             chimara_glk_set_protect(glk, g_value_get_boolean(value));
96             break;
97         default:
98             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
99     }
100 }
101
102 static void
103 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
104 {
105     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
106     
107     switch(prop_id)
108     {
109         case PROP_INTERACTIVE:
110             g_value_set_boolean(value, priv->interactive);
111             break;
112         case PROP_PROTECT:
113             g_value_set_boolean(value, priv->protect);
114             break;
115         default:
116             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
117     }
118 }
119
120 static void
121 chimara_glk_finalize(GObject *object)
122 {
123     ChimaraGlk *self = CHIMARA_GLK(object);
124     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
125     
126     /* Free the event queue */
127     g_mutex_lock(priv->event_lock);
128         g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
129         g_queue_free(priv->event_queue);
130         g_cond_free(priv->event_queue_not_empty);
131         g_cond_free(priv->event_queue_not_full);
132         priv->event_queue = NULL;
133         g_mutex_unlock(priv->event_lock);
134         g_mutex_free(priv->event_lock);
135         
136         /* Free the abort signalling mechanism */
137         g_mutex_lock(priv->abort_lock);
138         /* Make sure no other thread is busy with this */
139         g_mutex_unlock(priv->abort_lock);
140         g_mutex_free(priv->abort_lock);
141         priv->abort_lock = NULL;
142
143     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
144 }
145
146 static void
147 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
148 {
149     g_return_if_fail(widget);
150     g_return_if_fail(requisition);
151     g_return_if_fail(CHIMARA_IS_GLK(widget));
152     
153     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
154     
155     /* For now, just pass the size request on to the root Glk window */
156     if(priv->root_window) { 
157         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
158        if(GTK_WIDGET_VISIBLE(child))
159             gtk_widget_size_request(child, requisition);
160     } else {
161         requisition->width = CHIMARA_GLK_MIN_WIDTH;
162         requisition->height = CHIMARA_GLK_MIN_HEIGHT;
163     }
164 }
165
166 static void
167 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
168 {
169     g_return_if_fail(widget);
170     g_return_if_fail(allocation);
171     g_return_if_fail(CHIMARA_IS_GLK(widget));
172     
173     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
174     
175     widget->allocation = *allocation;
176             
177     if(priv->root_window) {
178         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
179         if(GTK_WIDGET_VISIBLE(child))
180             gtk_widget_size_allocate(child, allocation);
181     }
182 }
183
184 static void
185 chimara_glk_forall(GtkContainer *container, gboolean include_internals,
186     GtkCallback callback, gpointer callback_data)
187 {
188     g_return_if_fail(container);
189     g_return_if_fail(CHIMARA_IS_GLK(container));
190     
191     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
192     
193     if(priv->root_window) {
194         GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
195         (*callback)(child, callback_data);
196     }
197 }
198
199 static void
200 chimara_glk_stopped(ChimaraGlk *self)
201 {
202     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
203
204     /* Free the plugin */
205         if( priv->program && !g_module_close(priv->program) )
206             g_warning( "Error closing module: %s", g_module_error() );
207 }
208
209 static void
210 chimara_glk_started(ChimaraGlk *self)
211 {
212         /* TODO: Add default signal handler implementation here */
213 }
214
215 static void
216 chimara_glk_class_init(ChimaraGlkClass *klass)
217 {
218     /* Override methods of parent classes */
219     GObjectClass *object_class = G_OBJECT_CLASS(klass);
220     object_class->set_property = chimara_glk_set_property;
221     object_class->get_property = chimara_glk_get_property;
222     object_class->finalize = chimara_glk_finalize;
223     
224     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
225     widget_class->size_request = chimara_glk_size_request;
226     widget_class->size_allocate = chimara_glk_size_allocate;
227
228     GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
229     container_class->forall = chimara_glk_forall;
230
231     /* Signals */
232     klass->stopped = chimara_glk_stopped;
233     klass->started = chimara_glk_started;
234     /**
235      * ChimaraGlk::stopped:
236      * @glk: The widget that received the signal
237      *
238      * The ::stopped signal is emitted when the a Glk program finishes
239      * executing in the widget, whether it ended normally, or was interrupted.
240      */ 
241     chimara_glk_signals[STOPPED] = g_signal_new("stopped", 
242         G_OBJECT_CLASS_TYPE(klass), 0, 
243         G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
244                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
245         /**
246          * ChimaraGlk::started:
247          * @glk: The widget that received the signal
248          *
249          * The ::started signal is emitted when a Glk program starts executing in
250          * the widget.
251          */
252         chimara_glk_signals[STARTED] = g_signal_new ("started",
253                 G_OBJECT_CLASS_TYPE (klass), 0,
254                 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
255                 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
256
257     /* Properties */
258     GParamSpec *pspec;
259     pspec = g_param_spec_boolean("interactive", _("Interactive"),
260         _("Whether user input is expected in the Glk program"),
261         TRUE,
262         G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION |
263         G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
264     /**
265      * ChimaraGlk:interactive:
266      *
267      * Sets whether the widget is interactive. A Glk widget is normally 
268      * interactive, but in non-interactive mode, keyboard and mouse input are 
269      * ignored and the Glk program is controlled by chimara_glk_feed_text(). 
270      * "More" prompts when a lot of text is printed to a text buffer are also 
271      * disabled. This is typically used when you wish to control an interpreter 
272      * program by feeding it a predefined list of commands.
273      */
274     g_object_class_install_property(object_class, PROP_INTERACTIVE, pspec);
275     pspec = g_param_spec_boolean("protect", _("Protected"),
276         _("Whether the Glk program is barred from doing file operations"),
277         FALSE,
278         G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION |
279         G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
280     /**
281      * ChimaraGlk:protect:
282      *
283      * Sets whether the Glk program is allowed to do file operations. In protect
284      * mode, all file operations will fail.
285      */
286     g_object_class_install_property(object_class, PROP_PROTECT, pspec);
287     
288     /* Private data */
289     g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
290 }
291
292 /* PUBLIC FUNCTIONS */
293
294 /**
295  * chimara_glk_new:
296  *
297  * Creates and initializes a new #ChimaraGlk widget.
298  *
299  * Return value: a #ChimaraGlk widget, with a floating reference.
300  */
301 GtkWidget *
302 chimara_glk_new(void)
303 {
304     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
305     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
306     
307     priv->event_queue = g_queue_new();
308     priv->event_lock = g_mutex_new();
309     priv->event_queue_not_empty = g_cond_new();
310     priv->event_queue_not_full = g_cond_new();
311     priv->abort_lock = g_mutex_new();
312     
313     return GTK_WIDGET(self);
314 }
315
316 /**
317  * chimara_glk_set_interactive:
318  * @glk: a #ChimaraGlk widget
319  * @interactive: whether the widget should expect user input
320  *
321  * Sets the #ChimaraGlk:interactive property of @glk. 
322  */
323 void 
324 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
325 {
326     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
327     
328     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
329     priv->interactive = interactive;
330 }
331
332 /**
333  * chimara_glk_get_interactive:
334  * @glk: a #ChimaraGlk widget
335  *
336  * Returns whether @glk is interactive (expecting user input). See 
337  * #ChimaraGlk:interactive.
338  *
339  * Return value: %TRUE if @glk is interactive.
340  */
341 gboolean 
342 chimara_glk_get_interactive(ChimaraGlk *glk)
343 {
344     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
345     
346     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
347     return priv->interactive;
348 }
349
350 /**
351  * chimara_glk_set_protect:
352  * @glk: a #ChimaraGlk widget
353  * @protect: whether the widget should allow the Glk program to do file 
354  * operations
355  *
356  * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk 
357  * program is not allowed to do file operations.
358  */
359 void 
360 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
361 {
362     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
363     
364     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
365     priv->protect = protect;
366 }
367
368 /**
369  * chimara_glk_get_protect:
370  * @glk: a #ChimaraGlk widget
371  *
372  * Returns whether @glk is in protect mode (banned from doing file operations).
373  * See #ChimaraGlk:protect.
374  *
375  * Return value: %TRUE if @glk is in protect mode.
376  */
377 gboolean 
378 chimara_glk_get_protect(ChimaraGlk *glk)
379 {
380     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
381     
382     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
383     return priv->protect;
384 }
385
386 /* glk_enter() is the actual function called in the new thread in which glk_main() runs.  */
387 static gpointer
388 glk_enter(gpointer glk_main)
389 {
390     extern ChimaraGlkPrivate *glk_data;
391     g_signal_emit_by_name(glk_data->self, "started");
392         ((glk_main_t)glk_main)();
393         g_signal_emit_by_name(glk_data->self, "stopped");
394         return NULL;
395 }
396
397 /**
398  * chimara_glk_run:
399  * @glk: a #ChimaraGlk widget
400  * @plugin: path to a plugin module compiled with <filename 
401  * class="header">glk.h</filename>
402  * @error: location to store a <link linkend="glib-GError">GError</link>, or 
403  * %NULL
404  *
405  * Opens a Glk program compiled as a plugin and runs its glk_main() function in
406  * a separate thread. On failure, returns %FALSE and sets @error.
407  *
408  * Return value: %TRUE if the Glk program was started successfully.
409  */
410 gboolean
411 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
412 {
413     g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
414     g_return_val_if_fail(plugin, FALSE);
415     
416     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
417     
418     /* Open the module to run */
419     glk_main_t glk_main;
420     g_assert( g_module_supported() );
421     priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
422     
423     if(!priv->program)
424     {
425         g_warning( "Error opening module: %s", g_module_error() );
426         return FALSE;
427     }
428     if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
429     {
430         g_warning( "Error finding glk_main(): %s", g_module_error() );
431         return FALSE;
432     }
433
434     extern ChimaraGlkPrivate *glk_data;
435     /* Set the thread's private data */
436     /* TODO: Do this with a GPrivate */
437     glk_data = priv;
438     
439     /* Run in a separate thread */
440         priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
441         
442         return !(priv->thread == NULL);
443 }
444
445 /**
446  * chimara_glk_stop:
447  * @glk: a #ChimaraGlk widget
448  *
449  * Signals the Glk program running in @glk to abort. Note that if the program is
450  * caught in an infinite loop in which glk_tick() is not called, this may not
451  * work.
452  */
453 void
454 chimara_glk_stop(ChimaraGlk *glk)
455 {
456     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
457     /* TODO: check if glk is actually running a program */
458     signal_abort();
459 }
460
461 /**
462  * chimara_glk_wait:
463  * @glk: a #ChimaraGlk widget
464  *
465  * Holds up the main thread and waits for the Glk program running in @glk to 
466  * finish.
467  */
468 void
469 chimara_glk_wait(ChimaraGlk *glk)
470 {
471     g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
472     
473     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
474     g_thread_join(priv->thread);
475 }