4580c0f6ad82d8ce6b7037046bccdefef857c26a
[projects/chimara/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_signal_emit_by_name(glk_data->self, "stopped");
42         g_thread_exit(NULL);
43 }
44
45 /* Internal function: Signal this Glk thread to abort. Does nothing if the abort
46 mutex has already been freed. (That means the thread already ended.) */
47 void
48 signal_abort()
49 {
50         if(glk_data && glk_data->abort_lock) {
51                 g_mutex_lock(glk_data->abort_lock);
52                 glk_data->abort_signalled = TRUE;
53                 g_mutex_unlock(glk_data->abort_lock);
54                 /* Stop blocking on the event queue condition */
55                 event_throw(evtype_Abort, NULL, 0, 0);
56         }
57 }
58
59 /* Internal function: check if the Glk program has been interrupted. */
60 void
61 check_for_abort()
62 {
63         g_mutex_lock(glk_data->abort_lock);
64         if(glk_data->abort_signalled) 
65         {
66                 g_mutex_unlock(glk_data->abort_lock);
67                 abort_glk();
68         }
69         g_mutex_unlock(glk_data->abort_lock);
70 }
71
72