Started using thread-private data. Multisession still doesn't work, but regular opera...
[rodin/chimara.git] / libchimara / event.c
1 #include "event.h"
2 #include "magic.h"
3 #include "glk.h"
4 #include <string.h>
5
6 #include "chimara-glk.h"
7 #include "chimara-glk-private.h"
8
9 extern GPrivate *glk_data_key;
10
11 #define EVENT_TIMEOUT_MICROSECONDS (3000000)
12
13 /* Internal function: push an event onto the event queue. If the event queue is
14 full, wait for max three seconds and then drop the event. If the event queue is
15 NULL, i.e. freed, then fail silently. */
16 void
17 event_throw(ChimaraGlk *glk, glui32 type, winid_t win, glui32 val1, glui32 val2)
18 {
19         ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk);
20         
21         if(!priv->event_queue)
22                 return;
23
24         GTimeVal timeout;
25         g_get_current_time(&timeout);
26         g_time_val_add(&timeout, EVENT_TIMEOUT_MICROSECONDS);
27
28         g_mutex_lock(priv->event_lock);
29
30         /* Wait for room in the event queue */
31         while( g_queue_get_length(priv->event_queue) >= EVENT_QUEUE_MAX_LENGTH )
32                 if( !g_cond_timed_wait(priv->event_queue_not_full, priv->event_lock, &timeout) ) 
33                 {
34                         /* Drop the event after 3 seconds */
35                         g_mutex_unlock(priv->event_lock);
36                         return;
37                 }
38
39         event_t *event = g_new0(event_t, 1);
40         event->type = type;
41         event->win = win;
42         event->val1 = val1;
43         event->val2 = val2;
44         g_queue_push_head(priv->event_queue, event);
45
46         /* Signal that there is an event */
47         g_cond_signal(priv->event_queue_not_empty);
48
49         g_mutex_unlock(priv->event_lock);
50 }
51
52 /**
53  * glk_select:
54  * @event: Pointer to an #event_t.
55  *
56  * Causes the program to wait for an event, and then store it in the structure
57  * pointed to by @event. Unlike most Glk functions that take pointers, the
58  * argument of glk_select() may not be %NULL.
59  *
60  * Most of the time, you only get the events that you request. However, there
61  * are some events which can arrive at any time. This is why you must always
62  * call glk_select() in a loop, and continue the loop until you get the event
63  * you really want.
64  */
65 void
66 glk_select(event_t *event)
67 {
68         g_return_if_fail(event != NULL);
69
70         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
71         
72         g_mutex_lock(glk_data->event_lock);
73
74         /* Wait for an event */
75         while( g_queue_is_empty(glk_data->event_queue) )
76                 g_cond_wait(glk_data->event_queue_not_empty, glk_data->event_lock);
77
78         event_t *retrieved_event = g_queue_pop_tail(glk_data->event_queue);
79         if(retrieved_event == NULL)
80         {
81                 g_mutex_unlock(glk_data->event_lock);
82                 WARNING("Retrieved NULL event from non-empty event queue");
83                 return;
84         }
85         memcpy(event, retrieved_event, sizeof(event_t));
86         g_free(retrieved_event);
87
88         /* Signal that the event queue is no longer full */
89         g_cond_signal(glk_data->event_queue_not_full);
90
91         g_mutex_unlock(glk_data->event_lock);
92         
93         /* Check for interrupt */
94         glk_tick();
95         
96         /* If an abort event was generated, the thread should have exited by now */
97         g_assert(event->type != evtype_Abort);
98 }
99
100 /**
101  * glk_select_poll:
102  * @event: Return location for an event.
103  *
104  * You can also inquire if an event is available, without stopping to wait for 
105  * one to occur.
106  * 
107  * This checks if an internally-spawned event is available. If so, it stores it 
108  * in the structure pointed to by @event. If not, it sets
109  * <code>@event->type</code> to %evtype_None. Either way, it returns almost
110  * immediately.
111  * 
112  * The first question you now ask is, what is an internally-spawned event?
113  * glk_select_poll() does not check for or return %evtype_CharInput,
114  * %evtype_LineInput, %evtype_MouseInput, or %evtype_Hyperlink events. It is
115  * intended for you to test conditions which may have occurred while you are
116  * computing, and not interfacing with the player. For example, time may pass
117  * during slow computations; you can use glk_select_poll() to see if a 
118  * %evtype_Timer event has occured. (See <link 
119  * linkend="chimara-Timer-Events">Timer Events</link>.)
120  * 
121  * At the moment, glk_select_poll() checks for %evtype_Timer, %evtype_Arrange,
122  * %evtype_Redraw and %evtype_SoundNotify events. But see <link 
123  * linkend="chimara-Other-Events">Other Events</link>.
124  * 
125  * The second question is, what does it mean that glk_select_poll() returns 
126  * <quote>almost immediately</quote>? In some Glk libraries, text that you send 
127  * to a window is buffered; it does not actually appear until you request player
128  * input with glk_select(). glk_select_poll() attends to this buffer-flushing 
129  * task in the same way. (Although it does not do the <quote><computeroutput>Hit
130  * any key to scroll down</computeroutput></quote> waiting which may be done in
131  * glk_select(); that's a player-input task.)
132  * 
133  * Similarly, on multitasking platforms, glk_select() may yield time to other
134  * processes; and glk_select_poll() does this as well.
135  * 
136  * The upshot of this is that you should not call glk_select_poll() very often. 
137  * If you are not doing much work between player inputs, you should not need to
138  * call it at all.
139  *
140  * <note><para>
141  *  For example, in a virtual machine interpreter, you should not call
142  *  glk_select_poll() after every opcode.
143  * </para></note>
144  * 
145  * However, if you are doing intense computation, you may wish to call
146  * glk_select_poll() every so often to yield time to other processes. And if you
147  * are printing intermediate results during this computation, you should
148  * glk_select_poll() every so often, so that you can be certain your output will 
149  * be displayed before the next glk_select().
150  * 
151  * <note><para>
152  *  However, you should call glk_tick() often &mdash; once per opcode in a VM
153  *  interpreter. See <link linkend="chimara-The-Tick-Thing">The Tick 
154  *  Thing</link>.
155  * </para></note>
156  */
157 void
158 glk_select_poll(event_t *event)
159 {
160         g_return_if_fail(event != NULL);
161
162         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
163         
164         event->type = evtype_None;
165         
166         g_mutex_lock(glk_data->event_lock);
167         
168         if( !g_queue_is_empty(glk_data->event_queue) )
169         {
170                 GList *link;
171                 int count;
172                 for(count = 0; (link = g_queue_peek_nth_link(glk_data->event_queue, count)) != NULL; count++)
173                 {
174                         glui32 type = ((event_t *)link->data)->type;
175                         if(type != evtype_CharInput && type != evtype_LineInput && type != evtype_MouseInput && type != evtype_Hyperlink)
176                         {
177                                 memcpy(event, link->data, sizeof(event_t));
178                                 g_free(link->data);
179                                 g_queue_delete_link(glk_data->event_queue, link);
180                                 g_cond_signal(glk_data->event_queue_not_full);
181                                 break;
182                         }
183                 }
184         }
185         
186         g_mutex_unlock(glk_data->event_lock);
187         
188         /* Check for interrupt */
189         glk_tick();
190
191         /* If an abort event was generated, the thread should have exited by now */
192         g_assert(event->type != evtype_Abort);
193 }