97673ac4ca11e13bb6d2450f193affbd8a544850
[rodin/chimara.git] / src / glk.c
1 #include <gtk/gtk.h>
2
3 #include "glk.h"
4 #include "abort.h"
5 #include "chimara-glk.h"
6 #include "chimara-glk-private.h"
7
8 ChimaraGlkPrivate *glk_data = NULL;
9
10 /**
11  * glk_exit:
12  * 
13  * Shuts down the Glk program. This function does not return.
14  *
15  * If you print some text to a window and then shut down your program, you can
16  * assume that the player will be able to read it.
17  *
18  * <note><para>
19  *  You should only shut down your program with glk_exit() or by returning from
20  *  your glk_main() function. If you call the ANSI <function>exit()</function> 
21  *  function, bad things may happen. This Glk library is designed for multiple 
22  *  sessions, for example, and you would be cutting off all the sessions instead
23  *  of just yours. You would also prevent final text from being visible to the 
24  *  player.
25  * </para></note>
26  */
27 void
28 glk_exit(void)
29 {
30     g_signal_emit_by_name(glk_data->self, "stopped");
31     glk_data = NULL;
32         g_thread_exit(NULL);
33 }
34
35 /**
36  * glk_tick:
37  *
38  * Many platforms have some annoying thing that has to be done every so often,
39  * or the gnurrs come from the voodvork out and eat your computer.
40  * 
41  * Well, not really. But you should call glk_tick() every so often, just in
42  * case. It may be necessary to yield time to other applications in a
43  * cooperative-multitasking OS, or to check for player interrupts in an infinite
44  * loop.
45  * 
46  * This call is fast; in fact, on average, it does nothing at all. So you can
47  * call it often. (In a virtual machine interpreter, once per opcode is
48  * appropriate. In a program with lots of computation, pick a comparable rate.)
49  * 
50  * glk_tick() does not try to update the screen, or check for player input, or
51  * any other interface task. For that, you should call glk_select() or 
52  * glk_select_poll().
53  * 
54  * Basically, you must ensure there's some fixed upper bound on the amount of
55  * computation that can occur before a glk_tick() (or glk_select()) occurs. In a
56  * VM interpreter, where the VM code might contain an infinite loop, this is
57  * critical. In a C program, you can often eyeball it.
58  */
59 void
60 glk_tick()
61 {
62         check_for_abort();
63         
64         /* Do one iteration of the main loop if there are any events */
65         gdk_threads_enter();
66         if(gtk_events_pending())
67                 gtk_main_iteration();
68         gdk_threads_leave();
69 }