1 /* licensing and copyright information here */
4 #include <glib/gi18n.h>
6 #include <pango/pango.h>
7 #include "chimara-glk.h"
8 #include "chimara-glk-private.h"
13 #define CHIMARA_GLK_MIN_WIDTH 0
14 #define CHIMARA_GLK_MIN_HEIGHT 0
18 * @short_description: Widget which executes a Glk program
19 * @stability: Unstable
20 * @include: chimara/chimara-glk.h
22 * The ChimaraGlk widget opens and runs a Glk program. The program must be
23 * compiled as a plugin module, with a function <function>glk_main()</function>
24 * that the Glk library can hook into.
26 * On Linux systems, this is a file with a name like
27 * <filename>plugin.so</filename>. For portability, you can use libtool and
29 * <informalexample><programlisting>
30 * pkglib_LTLIBRARIES = plugin.la
31 * plugin_la_SOURCES = plugin.c foo.c bar.c
32 * plugin_la_LDFLAGS = -module -shared -avoid-version -export-symbols-regex "^glk_main$$"
33 * </programlisting></informalexample>
34 * This will produce <filename>plugin.la</filename> which is a text file
35 * containing the correct plugin file to open (see the relevant section of the
37 * url="http://www.gnu.org/software/libtool/manual/html_node/Finding-the-dlname.html">
38 * Libtool manual</ulink>).
41 typedef void (* glk_main_t) (void);
47 PROP_DEFAULT_FONT_DESCRIPTION,
48 PROP_MONOSPACE_FONT_DESCRIPTION
58 static guint chimara_glk_signals[LAST_SIGNAL] = { 0 };
60 G_DEFINE_TYPE(ChimaraGlk, chimara_glk, GTK_TYPE_CONTAINER);
63 chimara_glk_init(ChimaraGlk *self)
65 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(self), GTK_NO_WINDOW);
67 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
70 priv->interactive = TRUE;
71 priv->protect = FALSE;
72 priv->default_font_desc = pango_font_description_from_string("Sans");
73 priv->monospace_font_desc = pango_font_description_from_string("Monospace");
76 priv->event_queue = NULL;
77 priv->event_lock = NULL;
78 priv->event_queue_not_empty = NULL;
79 priv->event_queue_not_full = NULL;
80 priv->abort_lock = NULL;
81 priv->abort_signalled = FALSE;
82 priv->interrupt_handler = NULL;
83 priv->root_window = NULL;
84 priv->fileref_list = NULL;
85 priv->current_stream = NULL;
86 priv->stream_list = NULL;
90 chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
92 ChimaraGlk *glk = CHIMARA_GLK(object);
96 case PROP_INTERACTIVE:
97 chimara_glk_set_interactive( glk, g_value_get_boolean(value) );
100 chimara_glk_set_protect( glk, g_value_get_boolean(value) );
102 case PROP_DEFAULT_FONT_DESCRIPTION:
103 chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
105 case PROP_MONOSPACE_FONT_DESCRIPTION:
106 chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) );
109 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
114 chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
116 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(object);
120 case PROP_INTERACTIVE:
121 g_value_set_boolean(value, priv->interactive);
124 g_value_set_boolean(value, priv->protect);
126 case PROP_DEFAULT_FONT_DESCRIPTION:
127 g_value_set_pointer(value, priv->default_font_desc);
129 case PROP_MONOSPACE_FONT_DESCRIPTION:
130 g_value_set_pointer(value, priv->monospace_font_desc);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
138 chimara_glk_finalize(GObject *object)
140 ChimaraGlk *self = CHIMARA_GLK(object);
141 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
143 /* Free the event queue */
144 g_mutex_lock(priv->event_lock);
145 g_queue_foreach(priv->event_queue, (GFunc)g_free, NULL);
146 g_queue_free(priv->event_queue);
147 g_cond_free(priv->event_queue_not_empty);
148 g_cond_free(priv->event_queue_not_full);
149 priv->event_queue = NULL;
150 g_mutex_unlock(priv->event_lock);
151 g_mutex_free(priv->event_lock);
153 /* Free the abort signalling mechanism */
154 g_mutex_lock(priv->abort_lock);
155 /* Make sure no other thread is busy with this */
156 g_mutex_unlock(priv->abort_lock);
157 g_mutex_free(priv->abort_lock);
158 priv->abort_lock = NULL;
160 /* Free private data */
161 pango_font_description_free(priv->default_font_desc);
162 pango_font_description_free(priv->monospace_font_desc);
164 G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
168 chimara_glk_size_request(GtkWidget *widget, GtkRequisition *requisition)
170 g_return_if_fail(widget);
171 g_return_if_fail(requisition);
172 g_return_if_fail(CHIMARA_IS_GLK(widget));
174 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
176 /* For now, just pass the size request on to the root Glk window */
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_request(child, requisition);
182 requisition->width = CHIMARA_GLK_MIN_WIDTH;
183 requisition->height = CHIMARA_GLK_MIN_HEIGHT;
188 chimara_glk_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
190 g_return_if_fail(widget);
191 g_return_if_fail(allocation);
192 g_return_if_fail(CHIMARA_IS_GLK(widget));
194 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(widget);
196 widget->allocation = *allocation;
198 if(priv->root_window) {
199 GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
200 if(GTK_WIDGET_VISIBLE(child))
201 gtk_widget_size_allocate(child, allocation);
206 chimara_glk_forall(GtkContainer *container, gboolean include_internals,
207 GtkCallback callback, gpointer callback_data)
209 g_return_if_fail(container);
210 g_return_if_fail(CHIMARA_IS_GLK(container));
212 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(container);
214 if(priv->root_window) {
215 GtkWidget *child = ((winid_t)(priv->root_window->data))->frame;
216 (*callback)(child, callback_data);
221 chimara_glk_stopped(ChimaraGlk *self)
223 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
225 /* Free the plugin */
226 if( priv->program && !g_module_close(priv->program) )
227 g_warning( "Error closing module: %s", g_module_error() );
231 chimara_glk_started(ChimaraGlk *self)
233 /* TODO: Add default signal handler implementation here */
236 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
237 #ifndef G_PARAM_STATIC_STRINGS
238 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
242 chimara_glk_class_init(ChimaraGlkClass *klass)
244 /* Override methods of parent classes */
245 GObjectClass *object_class = G_OBJECT_CLASS(klass);
246 object_class->set_property = chimara_glk_set_property;
247 object_class->get_property = chimara_glk_get_property;
248 object_class->finalize = chimara_glk_finalize;
250 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
251 widget_class->size_request = chimara_glk_size_request;
252 widget_class->size_allocate = chimara_glk_size_allocate;
254 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);
255 container_class->forall = chimara_glk_forall;
258 klass->stopped = chimara_glk_stopped;
259 klass->started = chimara_glk_started;
261 * ChimaraGlk::stopped:
262 * @glk: The widget that received the signal
264 * The ::stopped signal is emitted when the a Glk program finishes
265 * executing in the widget, whether it ended normally, or was interrupted.
267 chimara_glk_signals[STOPPED] = g_signal_new("stopped",
268 G_OBJECT_CLASS_TYPE(klass), 0,
269 G_STRUCT_OFFSET(ChimaraGlkClass, stopped), NULL, NULL,
270 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
272 * ChimaraGlk::started:
273 * @glk: The widget that received the signal
275 * The ::started signal is emitted when a Glk program starts executing in
278 chimara_glk_signals[STARTED] = g_signal_new ("started",
279 G_OBJECT_CLASS_TYPE (klass), 0,
280 G_STRUCT_OFFSET(ChimaraGlkClass, started), NULL, NULL,
281 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
285 pspec = g_param_spec_boolean("interactive", _("Interactive"),
286 _("Whether user input is expected in the Glk program"),
288 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
290 * ChimaraGlk:interactive:
292 * Sets whether the widget is interactive. A Glk widget is normally
293 * interactive, but in non-interactive mode, keyboard and mouse input are
294 * ignored and the Glk program is controlled by chimara_glk_feed_text().
295 * <quote>More</quote> prompts when a lot of text is printed to a text
296 * buffer are also disabled. This is typically used when you wish to control
297 * an interpreter program by feeding it a predefined list of commands.
299 g_object_class_install_property(object_class, PROP_INTERACTIVE, pspec);
300 pspec = g_param_spec_boolean("protect", _("Protected"),
301 _("Whether the Glk program is barred from doing file operations"),
303 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
305 * ChimaraGlk:protect:
307 * Sets whether the Glk program is allowed to do file operations. In protect
308 * mode, all file operations will fail.
310 g_object_class_install_property(object_class, PROP_PROTECT, pspec);
311 pspec = g_param_spec_pointer("default-font-description", _("Default Font"),
312 _("Font description of the default proportional font"),
313 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
314 /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will
315 initialize them with NULL */
317 * ChimaraGlk:default-font-description:
319 * Pointer to a #PangoFontDescription describing the default proportional
320 * font, to be used in text buffer windows for example.
322 * Default value: font description created from the string
323 * <quote>Sans</quote>
325 g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, pspec);
326 pspec = g_param_spec_pointer("monospace-font-description", _("Monospace Font"),
327 _("Font description of the default monospace font"),
328 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS);
330 * ChimaraGlk:monospace-font-description:
332 * Pointer to a #PangoFontDescription describing the default monospace font,
333 * to be used in text grid windows and #style_Preformatted, for example.
335 * Default value: font description created from the string
336 * <quote>Monospace</quote>
338 g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, pspec);
340 g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate));
343 /* PUBLIC FUNCTIONS */
348 * Creates and initializes a new #ChimaraGlk widget.
350 * Return value: a #ChimaraGlk widget, with a floating reference.
353 chimara_glk_new(void)
355 ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
356 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
358 priv->event_queue = g_queue_new();
359 priv->event_lock = g_mutex_new();
360 priv->event_queue_not_empty = g_cond_new();
361 priv->event_queue_not_full = g_cond_new();
362 priv->abort_lock = g_mutex_new();
364 return GTK_WIDGET(self);
368 * chimara_glk_set_interactive:
369 * @glk: a #ChimaraGlk widget
370 * @interactive: whether the widget should expect user input
372 * Sets the #ChimaraGlk:interactive property of @glk.
375 chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive)
377 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
379 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
380 priv->interactive = interactive;
384 * chimara_glk_get_interactive:
385 * @glk: a #ChimaraGlk widget
387 * Returns whether @glk is interactive (expecting user input). See
388 * #ChimaraGlk:interactive.
390 * Return value: %TRUE if @glk is interactive.
393 chimara_glk_get_interactive(ChimaraGlk *glk)
395 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
397 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
398 return priv->interactive;
402 * chimara_glk_set_protect:
403 * @glk: a #ChimaraGlk widget
404 * @protect: whether the widget should allow the Glk program to do file
407 * Sets the #ChimaraGlk:protect property of @glk. In protect mode, the Glk
408 * program is not allowed to do file operations.
411 chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect)
413 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
415 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
416 priv->protect = protect;
420 * chimara_glk_get_protect:
421 * @glk: a #ChimaraGlk widget
423 * Returns whether @glk is in protect mode (banned from doing file operations).
424 * See #ChimaraGlk:protect.
426 * Return value: %TRUE if @glk is in protect mode.
429 chimara_glk_get_protect(ChimaraGlk *glk)
431 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
433 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
434 return priv->protect;
438 * chimara_glk_set_default_font_description:
439 * @glk: a #ChimaraGlk widget
440 * @font: a #PangoFontDescription
442 * Sets @glk's default proportional font. See
443 * #ChimaraGlk:default-font-description.
446 chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font)
448 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
449 g_return_if_fail(font);
451 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
452 pango_font_description_free(priv->default_font_desc);
453 priv->default_font_desc = pango_font_description_copy(font);
455 /* TODO: Apply the font description to all the windows and recalculate the sizes */
459 * chimara_glk_set_default_font_string:
460 * @glk: a #ChimaraGlk widget
461 * @font: string representation of a font description
463 * Sets @glk's default proportional font according to the string @font, which
464 * must be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
465 * [<replaceable>STYLE-OPTIONS</replaceable>]
466 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Charter,Utopia
467 * Italic 12</quote> or <quote>Sans</quote>. See
468 * #ChimaraGlk:default-font-description.
471 chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font)
473 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
474 g_return_if_fail(font || *font);
476 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
477 g_return_if_fail(fontdesc);
479 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
480 pango_font_description_free(priv->default_font_desc);
481 priv->default_font_desc = fontdesc;
483 /* TODO: Apply the font description to all the windows and recalculate the sizes */
487 * chimara_glk_get_default_font_description:
489 * Returns @glk's default proportional font.
491 * Return value: a newly-allocated #PangoFontDescription which must be freed
492 * using pango_font_description_free(), or %NULL on error.
494 PangoFontDescription *
495 chimara_glk_get_default_font_description(ChimaraGlk *glk)
497 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
499 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
500 return pango_font_description_copy(priv->default_font_desc);
504 * chimara_glk_set_monospace_font_description:
505 * @glk: a #ChimaraGlk widget
506 * @font: a #PangoFontDescription
508 * Sets @glk's default monospace font. See
509 * #ChimaraGlk:monospace-font-description.
512 chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font)
514 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
515 g_return_if_fail(font);
517 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
518 pango_font_description_free(priv->monospace_font_desc);
519 priv->monospace_font_desc = pango_font_description_copy(font);
521 /* TODO: Apply the font description to all the windows and recalculate the sizes */
525 * chimara_glk_set_monospace_font_string:
526 * @glk: a #ChimaraGlk widget
527 * @font: string representation of a font description
529 * Sets @glk's default monospace font according to the string @font, which must
530 * be a string in the form <quote><replaceable>FAMILY-LIST</replaceable>
531 * [<replaceable>STYLE-OPTIONS</replaceable>]
532 * [<replaceable>SIZE</replaceable>]</quote>, such as <quote>Courier
533 * Bold 12</quote> or <quote>Monospace</quote>. See
534 * #ChimaraGlk:monospace-font-description.
537 chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font)
539 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
540 g_return_if_fail(font || *font);
542 PangoFontDescription *fontdesc = pango_font_description_from_string(font);
543 g_return_if_fail(fontdesc);
545 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
546 pango_font_description_free(priv->monospace_font_desc);
547 priv->monospace_font_desc = fontdesc;
549 /* TODO: Apply the font description to all the windows and recalculate the sizes */
553 * chimara_glk_get_monospace_font_description:
555 * Returns @glk's default monospace font.
557 * Return value: a newly-allocated #PangoFontDescription which must be freed
558 * using pango_font_description_free(), or %NULL on error.
560 PangoFontDescription *
561 chimara_glk_get_monospace_font_description(ChimaraGlk *glk)
563 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL);
565 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
566 return pango_font_description_copy(priv->monospace_font_desc);
569 /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */
571 glk_enter(gpointer glk_main)
573 extern ChimaraGlkPrivate *glk_data;
574 g_signal_emit_by_name(glk_data->self, "started");
575 ((glk_main_t)glk_main)();
576 g_signal_emit_by_name(glk_data->self, "stopped");
582 * @glk: a #ChimaraGlk widget
583 * @plugin: path to a plugin module compiled with <filename
584 * class="header">glk.h</filename>
585 * @error: location to store a <link linkend="glib-GError">GError</link>, or
588 * Opens a Glk program compiled as a plugin and runs its glk_main() function in
589 * a separate thread. On failure, returns %FALSE and sets @error.
591 * Return value: %TRUE if the Glk program was started successfully.
594 chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error)
596 g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), FALSE);
597 g_return_val_if_fail(plugin, FALSE);
599 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
601 /* Open the module to run */
603 g_assert( g_module_supported() );
604 priv->program = g_module_open(plugin, G_MODULE_BIND_LAZY);
608 g_warning( "Error opening module: %s", g_module_error() );
611 if( !g_module_symbol(priv->program, "glk_main", (gpointer *) &glk_main) )
613 g_warning( "Error finding glk_main(): %s", g_module_error() );
617 extern ChimaraGlkPrivate *glk_data;
618 /* Set the thread's private data */
619 /* TODO: Do this with a GPrivate */
622 /* Run in a separate thread */
623 priv->thread = g_thread_create(glk_enter, glk_main, TRUE, error);
625 return !(priv->thread == NULL);
630 * @glk: a #ChimaraGlk widget
632 * Signals the Glk program running in @glk to abort. Note that if the program is
633 * caught in an infinite loop in which glk_tick() is not called, this may not
637 chimara_glk_stop(ChimaraGlk *glk)
639 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
640 /* TODO: check if glk is actually running a program */
646 * @glk: a #ChimaraGlk widget
648 * Holds up the main thread and waits for the Glk program running in @glk to
652 chimara_glk_wait(ChimaraGlk *glk)
654 g_return_if_fail(glk || CHIMARA_IS_GLK(glk));
656 ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
657 g_thread_join(priv->thread);