Quit program when browser window is closed
[projects/chimara/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_pre();
47         shutdown_glk_post();
48         /* If program is terminated by g_thread_exit() instead of returning from the
49          glk_main() function, then the line in glk_exit() where the "stopped" 
50          signal is emitted will not be reached. So we have to emit it here. */
51         if(!glk_data->in_startup)
52                 g_signal_emit_by_name(glk_data->self, "stopped");
53         g_thread_exit(NULL);
54 }
55
56 /* Internal function: check if the Glk program has been interrupted. */
57 void
58 check_for_abort(void)
59 {
60         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
61         g_mutex_lock(glk_data->abort_lock);
62         if(glk_data->abort_signalled)
63         {
64                 g_mutex_unlock(glk_data->abort_lock);
65                 abort_glk();
66         }
67         g_mutex_unlock(glk_data->abort_lock);
68 }
69
70 /* Internal function: shut down all requests and anything not necessary while
71  showing the last displayed configuration of windows. */
72 void
73 shutdown_glk_pre(void)
74 {
75         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
76         
77         /* Stop any timers */
78         glk_request_timer_events(0);
79         
80         /* Cancel any pending input requests and flush all window buffers */
81         winid_t win;
82         for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
83         {
84                 switch(win->input_request_type)
85                 {
86                         case INPUT_REQUEST_CHARACTER:
87                         case INPUT_REQUEST_CHARACTER_UNICODE:
88                                 glk_cancel_char_event(win);
89                                 break;
90                         case INPUT_REQUEST_LINE:
91                         case INPUT_REQUEST_LINE_UNICODE:
92                                 glk_cancel_line_event(win, NULL);
93                                 break;
94                         case INPUT_REQUEST_NONE:
95                         default:
96                                 ; /* TODO: Handle mouse and hyperlink requests */
97                 }
98                 
99                 flush_window_buffer(win);
100         }
101         
102         /* Close any open resource files */
103         if(glk_data->resource_map != NULL) {
104                 giblorb_destroy_map(glk_data->resource_map);
105                 glk_data->resource_map = NULL;
106                 glk_stream_close(glk_data->resource_file, NULL);
107         }
108         
109         /* Empty the input queues */
110         while(g_async_queue_try_pop(glk_data->char_input_queue))
111                 ;
112         while(g_async_queue_try_pop(glk_data->line_input_queue))
113                 ;
114         
115         /* Wait for any pending window rearrange */
116         g_mutex_lock(glk_data->arrange_lock);
117         if(glk_data->needs_rearrange)
118                 g_cond_wait(glk_data->rearranged, glk_data->arrange_lock);
119         g_mutex_unlock(glk_data->arrange_lock);
120 }
121
122 /* Internal function: do any Glk-thread cleanup for shutting down the Glk library. */
123 void
124 shutdown_glk_post(void)
125 {
126         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
127         
128         /* Free all opaque objects; can't iterate normally, because the objects are
129          being removed from the global iteration lists */
130         if(glk_data->root_window)
131                 glk_window_close(glk_data->root_window->data, NULL);
132         g_assert(glk_data->root_window == NULL);
133         strid_t str;
134         while( (str = glk_stream_iterate(NULL, NULL)) )
135                 glk_stream_close(str, NULL);
136         frefid_t fref;
137         while( (fref = glk_fileref_iterate(NULL, NULL)) )
138                 glk_fileref_destroy(fref);
139         schanid_t sch;
140         while( (sch = glk_schannel_iterate(NULL, NULL)) )
141                 glk_schannel_destroy(sch);
142         
143         /* Empty the event queue */
144         g_mutex_lock(glk_data->event_lock);
145         g_queue_foreach(glk_data->event_queue, (GFunc)g_free, NULL);
146
147         /* COMPAT: g_queue_clear could be used here, but only appeared in 2.14 */
148         // g_queue_clear(glk_data->event_queue);
149         g_list_free(glk_data->event_queue->head);
150         glk_data->event_queue->head = glk_data->event_queue->tail = NULL;
151         glk_data->event_queue->length = 0;
152
153         g_mutex_unlock(glk_data->event_lock);
154         
155         /* Reset the abort signaling mechanism */
156         g_mutex_lock(glk_data->abort_lock);
157         glk_data->abort_signalled = FALSE;
158         g_mutex_unlock(glk_data->abort_lock);
159         
160         /* Reset arrangement mechanism */
161         g_mutex_lock(glk_data->arrange_lock);
162         glk_data->needs_rearrange = FALSE;
163         glk_data->ignore_next_arrange_event = FALSE;
164         g_mutex_unlock(glk_data->arrange_lock);
165         
166         /* Unref input queues (they are not destroyed because the main thread stil holds a ref */
167         g_async_queue_unref(glk_data->char_input_queue);
168         g_async_queue_unref(glk_data->line_input_queue);
169
170         /* Reset other stuff */
171         glk_data->interrupt_handler = NULL;
172         g_free(glk_data->current_dir);
173         glk_data->current_dir = NULL;
174         /* Remove the dispatch callbacks */
175         glk_data->register_obj = NULL;
176         glk_data->unregister_obj = NULL;
177         glk_data->register_arr = NULL;
178         glk_data->unregister_arr = NULL;
179 }