Separated library source code from testing code, fixing #6
[projects/chimara/chimara.git] / libchimara / 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 an interrupt handler function.
12  *
13  * Sets @func to be the interrupt handler. @func should be a pointer to a 
14  * function which takes no argument and returns no result. If Glk receives an
15  * interrupt, and you have set an interrupt handler, your handler will be 
16  * called, before the process is shut down.
17  * 
18  * Initially there is no interrupt handler. You can reset to not having any by
19  * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
20  * 
21  * If you call glk_set_interrupt_handler() with a new handler function while an
22  * older one is set, the new one replaces the old one. Glk does not try to queue
23  * interrupt handlers.
24  *
25  * You should not try to interact with the player in your interrupt handler. Do
26  * not call glk_select() or glk_select_poll(). Anything you print to a window
27  * may not be visible to the player. 
28  */
29 void
30 glk_set_interrupt_handler(void (*func)(void))
31 {
32         glk_data->interrupt_handler = func;
33 }
34
35 /* Internal function: abort this Glk program, freeing resources and calling the
36 user's interrupt handler. */
37 static void
38 abort_glk()
39 {
40         if(glk_data->interrupt_handler)
41                 (*(glk_data->interrupt_handler))();
42         g_signal_emit_by_name(glk_data->self, "stopped");
43         g_thread_exit(NULL);
44 }
45
46 /* Internal function: Signal this Glk thread to abort. Does nothing if the abort
47 mutex has already been freed. (That means the thread already ended.) */
48 void
49 signal_abort()
50 {
51         if(glk_data && glk_data->abort_lock) {
52                 g_mutex_lock(glk_data->abort_lock);
53                 glk_data->abort_signalled = TRUE;
54                 g_mutex_unlock(glk_data->abort_lock);
55                 /* Stop blocking on the event queue condition */
56                 event_throw(evtype_Abort, NULL, 0, 0);
57         }
58 }
59
60 /* Internal function: check if the Glk program has been interrupted. */
61 void
62 check_for_abort()
63 {
64         g_mutex_lock(glk_data->abort_lock);
65         if(glk_data->abort_signalled) 
66         {
67                 g_mutex_unlock(glk_data->abort_lock);
68                 abort_glk();
69         }
70         g_mutex_unlock(glk_data->abort_lock);
71 }