+
+/* Internal function: do any cleanup for shutting down the Glk library. */
+void
+shutdown_glk(void)
+{
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+
+ if(!glk_data->in_startup)
+ g_signal_emit_by_name(glk_data->self, "stopped");
+
+ /* Stop any timers */
+ glk_request_timer_events(0);
+
+ /* Cancel any pending input requests and flush all window buffers */
+ winid_t win;
+ for(win = glk_window_iterate(NULL, NULL); win; win = glk_window_iterate(win, NULL))
+ {
+ switch(win->input_request_type)
+ {
+ case INPUT_REQUEST_CHARACTER:
+ case INPUT_REQUEST_CHARACTER_UNICODE:
+ glk_cancel_char_event(win);
+ break;
+ case INPUT_REQUEST_LINE:
+ case INPUT_REQUEST_LINE_UNICODE:
+ glk_cancel_line_event(win, NULL);
+ break;
+ case INPUT_REQUEST_NONE:
+ default:
+ ; /* Handle mouse and hyperlink requests */
+ }
+
+ flush_window_buffer(win);
+ }
+
+ /* Close any open resource files */
+ if(glk_data->resource_map != NULL) {
+ giblorb_destroy_map(glk_data->resource_map);
+ glk_stream_close(glk_data->resource_file, NULL);
+ }
+
+ /* Unref the input queues */
+ g_async_queue_unref(glk_data->char_input_queue);
+ g_async_queue_unref(glk_data->line_input_queue);
+
+ /* Wait for any pending window rearrange */
+ g_mutex_lock(glk_data->arrange_lock);
+ if(glk_data->needs_rearrange)
+ g_cond_wait(glk_data->rearranged, glk_data->arrange_lock);
+ g_mutex_unlock(glk_data->arrange_lock);
+
+ chimara_glk_reset(glk_data->self);
+}