6 #include "chimara-glk-private.h"
8 extern GPrivate *glk_data_key;
11 * glk_set_interrupt_handler:
12 * @func: A pointer to an interrupt handler function.
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.
19 * Initially there is no interrupt handler. You can reset to not having any by
20 * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
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
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.
31 glk_set_interrupt_handler(void (*func)(void))
33 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
34 glk_data->interrupt_handler = func;
37 /* Internal function: abort this Glk program, freeing resources and calling the
38 user's interrupt handler. */
42 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
43 if(glk_data->interrupt_handler)
44 (*(glk_data->interrupt_handler))();
49 /* Internal function: check if the Glk program has been interrupted. */
53 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
54 g_mutex_lock(glk_data->abort_lock);
55 if(glk_data->abort_signalled)
57 g_mutex_unlock(glk_data->abort_lock);
60 g_mutex_unlock(glk_data->abort_lock);
63 /* Internal function: do any cleanup for shutting down the Glk library. */
67 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
69 if(!glk_data->in_startup)
70 g_signal_emit_by_name(glk_data->self, "stopped");
73 glk_request_timer_events(0);
75 /* Cancel any pending input requests and flush all window buffers */
77 for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
79 switch(win->input_request_type)
81 case INPUT_REQUEST_CHARACTER:
82 case INPUT_REQUEST_CHARACTER_UNICODE:
83 glk_cancel_char_event(win);
85 case INPUT_REQUEST_LINE:
86 case INPUT_REQUEST_LINE_UNICODE:
87 glk_cancel_line_event(win, NULL);
89 case INPUT_REQUEST_NONE:
91 ; /* Handle mouse and hyperlink requests */
94 flush_window_buffer(win);
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);
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);
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);
113 chimara_glk_reset(glk_data->self);