1 /* licensing and copyright information here */
4 #include <glib/gi18n.h>
6 #include "chimara-glk.h"
7 #include "chimara-glk-private.h"
12 #define CHIMARA_GLK_MIN_WIDTH 0
13 #define CHIMARA_GLK_MIN_HEIGHT 0
17 * @short_description: Widget which executes a Glk program
18 * @stability: Unstable
19 * @include: chimara/chimara-glk.h
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.
25 * On Linux systems, this is a file with a name like
26 * <filename>plugin.so</filename>. For portability, you can use libtool and
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
36 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
37 * Libtool manual</ulink>).
40 typedef void (* glk_main_t) (void);
55 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
57 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
60 chimara_glk_init(ChimaraGlk *self)
62 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
64 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
67 priv->interactive = TRUE;
68 priv->protect = FALSE;
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;
85 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
87 ChimaraGlk *glk = CHIMARA_GLK(object);
91 case PROP_INTERACTIVE:
92 chimara_glk_set_interactive(glk, g_value_get_boolean(value));
95 chimara_glk_set_protect(glk, g_value_get_boolean(value));
98 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
103 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
105 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
109 case PROP_INTERACTIVE:
110 g_value_set_boolean(value, priv->interactive);
113 g_value_set_boolean(value, priv->protect);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
121 chimara_glk_finalize(GObject *object)
123 ChimaraGlk *self = CHIMARA_GLK(object);
124 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
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);
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;
143 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
147 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
149 g_return_if_fail(widget);
150 g_return_if_fail(requisition);
151 g_return_if_fail(CHIMARA_IS_GLK(widget));
153 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
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);
161 requisition->width = CHIMARA_GLK_MIN_WIDTH;
162 requisition->height = CHIMARA_GLK_MIN_HEIGHT;
167 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
169 g_return_if_fail(widget);
170 g_return_if_fail(allocation);
171 g_return_if_fail(CHIMARA_IS_GLK(widget));
173 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
175 widget->allocation = *allocation;
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);
185 chimara_glk_forall(GtkContainer *container, gboolean include_internals,
186 GtkCallback callback, gpointer callback_data)
188 g_return_if_fail(container);
189 g_return_if_fail(CHIMARA_IS_GLK(container));
191 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
193 if(priv->root_window) {
194 GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
195 (*callback)(child, callback_data);
200 chimara_glk_stopped(ChimaraGlk *self)
202 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
204 /* Free the plugin */
205 if( priv->program && !g_module_close(priv->program) )
206 g_warning( "Error closing module: %s", g_module_error() );
210 chimara_glk_started(ChimaraGlk *self)
212 /* TODO: Add default signal handler implementation here */
216 chimara_glk_class_init(ChimaraGlkClass *klass)
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;
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;
228 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
229 container_class->forall = chimara_glk_forall;
232 klass->stopped = chimara_glk_stopped;
233 klass->started = chimara_glk_started;
235 * ChimaraGlk::stopped:
236 * @glk: The widget that received the signal
238 * The ::stopped signal is emitted when the a Glk program finishes
239 * executing in the widget, whether it ended normally, or was interrupted.
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);
246 * ChimaraGlk::started:
247 * @glk: The widget that received the signal
249 * The ::started signal is emitted when a Glk program starts executing in
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);
259 pspec = g_param_spec_boolean("interactive", _("Interactive"),
260 _("Whether user input is expected in the Glk program"),
262 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION |
263 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
265 * ChimaraGlk:interactive:
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.
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"),
278 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION |
279 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
281 * ChimaraGlk:protect:
283 * Sets whether the Glk program is allowed to do file operations. In protect
284 * mode, all file operations will fail.
286 g_object_class_install_property(object_class, PROP_PROTECT, pspec);
289 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
292 /* PUBLIC FUNCTIONS */
297 * Creates and initializes a new #ChimaraGlk widget.
299 * Return value: a #ChimaraGlk widget, with a floating reference.
302 chimara_glk_new(void)
304 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
305 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
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();
313 return GTK_WIDGET(self);
317 * chimara_glk_set_interactive:
318 * @glk: a #ChimaraGlk widget
319 * @interactive: whether the widget should expect user input
321 * Sets the #ChimaraGlk:interactive property of @glk.
324 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
326 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
328 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
329 priv->interactive = interactive;
333 * chimara_glk_get_interactive:
334 * @glk: a #ChimaraGlk widget
336 * Returns whether @glk is interactive (expecting user input). See
337 * #ChimaraGlk:interactive.
339 * Return value: %TRUE if @glk is interactive.
342 chimara_glk_get_interactive(ChimaraGlk *glk)
344 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
346 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
347 return priv->interactive;
351 * chimara_glk_set_protect:
352 * @glk: a #ChimaraGlk widget
353 * @protect: whether the widget should allow the Glk program to do file
356 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
357 * program is not allowed to do file operations.
360 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
362 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
364 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
365 priv->protect = protect;
369 * chimara_glk_get_protect:
370 * @glk: a #ChimaraGlk widget
372 * Returns whether @glk is in protect mode (banned from doing file operations).
373 * See #ChimaraGlk:protect.
375 * Return value: %TRUE if @glk is in protect mode.
378 chimara_glk_get_protect(ChimaraGlk *glk)
380 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
382 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
383 return priv->protect;
386 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
388 glk_enter(gpointer glk_main)
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");
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
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.
408 * Return value: %TRUE if the Glk program was started successfully.
411 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
413 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
414 g_return_val_if_fail(plugin, FALSE);
416 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
418 /* Open the module to run */
420 g_assert( g_module_supported() );
421 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
425 g_warning( "Error opening module: %s", g_module_error() );
428 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
430 g_warning( "Error finding glk_main(): %s", g_module_error() );
434 extern ChimaraGlkPrivate *glk_data;
435 /* Set the thread's private data */
436 /* TODO: Do this with a GPrivate */
439 /* Run in a separate thread */
440 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
442 return !(priv->thread == NULL);
447 * @glk: a #ChimaraGlk widget
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
454 chimara_glk_stop(ChimaraGlk *glk)
456 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
457 /* TODO: check if glk is actually running a program */
463 * @glk: a #ChimaraGlk widget
465 * Holds up the main thread and waits for the Glk program running in @glk to
469 chimara_glk_wait(ChimaraGlk *glk)
471 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
473 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
474 g_thread_join(priv->thread);