Changed build system to Automake. Split Glk code off into a GTK widget.
[rodin/chimara.git] / src / abort.c
1 #include "event.h"
2 #include <glib.h>
3 #include <gtk/gtk.h>
4
5 #include "chimara-glk-private.h"
6
7 extern ChimaraGlkPrivate *glk_data;
8
9 /**
10  * glk_set_interrupt_handler:
11  * @func: A pointer to a function which takes no argument and returns no result.
12  *
13  * Specifies an interrupt handler function for cleaning up critical resources.
14  * If Glk receives an interrupt, and you have set an interrupt handler, your
15  * handler will be called, before the process is shut down.
16  * 
17  * Initially there is no interrupt handler. You can reset to not having any by
18  * calling glk_set_interrupt_handler(%NULL).
19  * 
20  * If you call glk_set_interrupt_handler() with a new handler function while an
21  * older one is set, the new one replaces the old one. Glk does not try to queue
22  * interrupt handlers.
23  *
24  * You should not try to interact with the player in your interrupt handler. Do
25  * not call glk_select() or glk_select_poll(). Anything you print to a window
26  * may not be visible to the player. 
27  */
28 void
29 glk_set_interrupt_handler(void (*func)(void))
30 {
31         glk_data->interrupt_handler = func;
32 }
33
34 /* Internal function: abort this Glk program, freeing resources and calling the
35 user's interrupt handler. */
36 void
37 abort_glk()
38 {
39         if(glk_data->interrupt_handler)
40                 (*(glk_data->interrupt_handler))();
41         g_thread_exit(NULL);
42 }
43
44 /* Internal function: Signal this Glk thread to abort. Does nothing if the abort
45 mutex has already been freed. (That means the thread already ended.) */
46 void
47 signal_abort()
48 {
49         if(glk_data->abort_lock) {
50                 g_mutex_lock(glk_data->abort_lock);
51                 glk_data->abort_signalled = TRUE;
52                 g_mutex_unlock(glk_data->abort_lock);
53                 /* Stop blocking on the event queue condition */
54                 event_throw(evtype_Abort, NULL, 0, 0);
55         }
56 }
57
58 /* Internal function: check if the Glk program has been interrupted. */
59 void
60 check_for_abort()
61 {
62         g_mutex_lock(glk_data->abort_lock);
63         if(glk_data->abort_signalled) 
64         {
65                 g_mutex_unlock(glk_data->abort_lock);
66                 abort_glk();
67         }
68         g_mutex_unlock(glk_data->abort_lock);
69 }
70
71