Fixed freeing data at stop & start. Previously the root window was set to NULL when...
[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 #include "window.h"
8
9 extern GPrivate *glk_data_key;
10
11 /**
12  * glk_set_interrupt_handler:
13  * @func: A pointer to an interrupt handler function.
14  *
15  * Sets @func to be the interrupt handler. @func should be a pointer to a
16  * function which takes no argument and returns no result. If Glk receives an
17  * interrupt, and you have set an interrupt handler, your handler will be
18  * called, before the process is shut down.
19  *
20  * Initially there is no interrupt handler. You can reset to not having any by
21  * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
22  *
23  * If you call glk_set_interrupt_handler() with a new handler function while an
24  * older one is set, the new one replaces the old one. Glk does not try to queue
25  * interrupt handlers.
26  *
27  * You should not try to interact with the player in your interrupt handler. Do
28  * not call glk_select() or glk_select_poll(). Anything you print to a window
29  * may not be visible to the player.
30  */
31 void
32 glk_set_interrupt_handler(void (*func)(void))
33 {
34         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
35         glk_data->interrupt_handler = func;
36 }
37
38 /* Internal function: abort this Glk program, freeing resources and calling the
39 user's interrupt handler. */
40 static void
41 abort_glk(void)
42 {
43         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
44         if(glk_data->interrupt_handler)
45                 (*(glk_data->interrupt_handler))();
46         shutdown_glk();
47         g_thread_exit(NULL);
48 }
49
50 /* Internal function: check if the Glk program has been interrupted. */
51 void
52 check_for_abort(void)
53 {
54         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
55         g_mutex_lock(glk_data->abort_lock);
56         if(glk_data->abort_signalled)
57         {
58                 g_mutex_unlock(glk_data->abort_lock);
59                 abort_glk();
60         }
61         g_mutex_unlock(glk_data->abort_lock);
62 }
63
64 /* Internal function: do any cleanup for shutting down the Glk library. */
65 void
66 shutdown_glk(void)
67 {
68         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
69
70         /* Stop any timers */
71         glk_request_timer_events(0);
72
73         /* Cancel any pending input requests and flush all window buffers */
74         winid_t win;
75         for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
76         {
77                 switch(win->input_request_type)
78                 {
79                         case INPUT_REQUEST_CHARACTER:
80                         case INPUT_REQUEST_CHARACTER_UNICODE:
81                                 glk_cancel_char_event(win);
82                                 break;
83                         case INPUT_REQUEST_LINE:
84                         case INPUT_REQUEST_LINE_UNICODE:
85                                 glk_cancel_line_event(win, NULL);
86                                 break;
87                         case INPUT_REQUEST_NONE:
88                         default:
89                                 ; /* Handle mouse and hyperlink requests */
90                 }
91
92                 flush_window_buffer(win);
93         }
94
95         /* Close any open resource files */
96         if(glk_data->resource_map != NULL) {
97                 giblorb_destroy_map(glk_data->resource_map);
98                 glk_stream_close(glk_data->resource_file, NULL);
99         }
100
101         /* Unref the input queues */
102         g_async_queue_unref(glk_data->char_input_queue);
103         g_async_queue_unref(glk_data->line_input_queue);
104
105         /* Wait for any pending window rearrange */
106         g_mutex_lock(glk_data->arrange_lock);
107         if(glk_data->needs_rearrange)
108                 g_cond_wait(glk_data->rearranged, glk_data->arrange_lock);
109         g_mutex_unlock(glk_data->arrange_lock);
110
111         /* Default handler for 'stopped' unloads the plugin, so be absolutely sure
112          we're not calling any dispatch callbacks after this point */
113         if(!glk_data->in_startup)
114                 g_signal_emit_by_name(glk_data->self, "stopped");
115         
116         _chimara_glk_free_nonwindow_private_data(glk_data);
117         glk_data->needs_reset = TRUE;
118 }