517dec5649761308b2592be09f6c78feced3c94c
[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         /* Cancel any pending input requests and flush all window buffers */
76         winid_t win;
77         for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
78         {
79                 switch(win->input_request_type)
80                 {
81                         case INPUT_REQUEST_CHARACTER:
82                         case INPUT_REQUEST_CHARACTER_UNICODE:
83                                 glk_cancel_char_event(win);
84                                 break;
85                         case INPUT_REQUEST_LINE:
86                         case INPUT_REQUEST_LINE_UNICODE:
87                                 glk_cancel_line_event(win, NULL);
88                                 break;
89                         case INPUT_REQUEST_NONE:
90                         default:
91                                 ; /* Handle mouse and hyperlink requests */
92                 }
93
94                 flush_window_buffer(win);
95         }
96
97         /* Close any open resource files */
98         if(glk_data->resource_map != NULL) {
99                 giblorb_destroy_map(glk_data->resource_map);
100                 glk_stream_close(glk_data->resource_file, NULL);
101         }
102
103         /* Unref the input queues */
104         g_async_queue_unref(glk_data->char_input_queue);
105         g_async_queue_unref(glk_data->line_input_queue);
106
107         /* Wait for any pending window rearrange */
108         g_mutex_lock(glk_data->arrange_lock);
109         if(glk_data->needs_rearrange)
110                 g_cond_wait(glk_data->rearranged, glk_data->arrange_lock);
111         g_mutex_unlock(glk_data->arrange_lock);
112
113         chimara_glk_reset(glk_data->self);
114 }