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