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