fixed #27
[rodin/chimara.git] / libchimara / abort.c
1 #include "abort.h"
2 #include "event.h"
3 #include <glib.h>
4 #include <gtk/gtk.h>
5
6 #include "chimara-glk-private.h"
7
8 extern GPrivate *glk_data_key;
9
10 /**
11  * glk_set_interrupt_handler:
12  * @func: A pointer to an interrupt handler function.
13  *
14  * Sets @func to be the interrupt handler. @func should be a pointer to a 
15  * function which takes no argument and returns no result. If Glk receives an
16  * interrupt, and you have set an interrupt handler, your handler will be 
17  * called, before the process is shut down.
18  * 
19  * Initially there is no interrupt handler. You can reset to not having any by
20  * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
21  * 
22  * If you call glk_set_interrupt_handler() with a new handler function while an
23  * older one is set, the new one replaces the old one. Glk does not try to queue
24  * interrupt handlers.
25  *
26  * You should not try to interact with the player in your interrupt handler. Do
27  * not call glk_select() or glk_select_poll(). Anything you print to a window
28  * may not be visible to the player. 
29  */
30 void
31 glk_set_interrupt_handler(void (*func)(void))
32 {
33         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
34         glk_data->interrupt_handler = func;
35 }
36
37 /* Internal function: abort this Glk program, freeing resources and calling the
38 user's interrupt handler. */
39 static void
40 abort_glk(void)
41 {
42         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
43         if(glk_data->interrupt_handler)
44                 (*(glk_data->interrupt_handler))();
45         shutdown_glk();
46         g_thread_exit(NULL);
47 }
48
49 /* Internal function: check if the Glk program has been interrupted. */
50 void
51 check_for_abort(void)
52 {
53         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
54         g_mutex_lock(glk_data->abort_lock);
55         if(glk_data->abort_signalled) 
56         {
57                 g_mutex_unlock(glk_data->abort_lock);
58                 abort_glk();
59         }
60         g_mutex_unlock(glk_data->abort_lock);
61 }
62
63 /* Internal function: do any cleanup for shutting down the Glk library. */
64 void
65 shutdown_glk(void)
66 {
67         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
68                 
69         if(!glk_data->in_startup)
70                 g_signal_emit_by_name(glk_data->self, "stopped");
71
72         /* Stop any timers */
73         glk_request_timer_events(0);
74
75         /* Close any open resource files */
76         if(glk_data->resource_map != NULL) {
77                 giblorb_destroy_map(glk_data->resource_map);
78                 glk_stream_close(glk_data->resource_file, NULL);
79         }
80         
81         /* Unref the input queues */
82         g_async_queue_unref(glk_data->char_input_queue);
83         g_async_queue_unref(glk_data->line_input_queue);
84
85         printf("cleaning up...\n");
86         chimara_glk_reset(glk_data->self);
87 }