6 #include "chimara-glk-private.h"
8 extern ChimaraGlkPrivate *glk_data;
10 #define EVENT_TIMEOUT_MICROSECONDS (3000000)
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. */
16 event_throw(glui32 type, winid_t win, glui32 val1, glui32 val2)
18 if(!glk_data->event_queue)
22 g_get_current_time(&timeout);
23 g_time_val_add(&timeout, EVENT_TIMEOUT_MICROSECONDS);
25 g_mutex_lock(glk_data->event_lock);
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) )
31 /* Drop the event after 3 seconds */
32 g_mutex_unlock(glk_data->event_lock);
36 event_t *event = g_new0(event_t, 1);
41 g_queue_push_head(glk_data->event_queue, event);
43 /* Signal that there is an event */
44 g_cond_signal(glk_data->event_queue_not_empty);
46 g_mutex_unlock(glk_data->event_lock);
51 * @event: Pointer to an #event_t.
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.
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
63 glk_select(event_t *event)
65 g_return_if_fail(event != NULL);
67 g_mutex_lock(glk_data->event_lock);
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);
73 event_t *retrieved_event = g_queue_pop_tail(glk_data->event_queue);
74 if(retrieved_event == NULL)
76 g_mutex_unlock(glk_data->event_lock);
77 WARNING("Retrieved NULL event from non-empty event queue");
80 memcpy(event, retrieved_event, sizeof(event_t));
81 g_free(retrieved_event);
83 /* Signal that the event queue is no longer full */
84 g_cond_signal(glk_data->event_queue_not_full);
86 g_mutex_unlock(glk_data->event_lock);
88 /* Check for interrupt */
91 /* If an abort event was generated, the thread should have exited by now */
92 g_assert(event->type != evtype_Abort);
97 * @event: Return location for an event.
99 * You can also inquire if an event is available, without stopping to wait for
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
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>.)
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>.
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.)
128 * Similarly, on multitasking platforms, glk_select() may yield time to other
129 * processes; and glk_select_poll() does this as well.
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
136 * For example, in a virtual machine interpreter, you should not call
137 * glk_select_poll() after every opcode.
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().
147 * However, you should call glk_tick() often — once per opcode in a VM
148 * interpreter. See <link linkend="chimara-The-Tick-Thing">The Tick
153 glk_select_poll(event_t *event)
155 g_return_if_fail(event != NULL);
157 event->type = evtype_None;
159 g_mutex_lock(glk_data->event_lock);
161 if( !g_queue_is_empty(glk_data->event_queue) )
165 for(count = 0; (link = g_queue_peek_nth_link(glk_data->event_queue, count)) != NULL; count++)
167 glui32 type = ((event_t *)link->data)->type;
168 if(type != evtype_CharInput && type != evtype_LineInput && type != evtype_MouseInput && type != evtype_Hyperlink)
170 memcpy(event, link->data, sizeof(event_t));
172 g_queue_delete_link(glk_data->event_queue, link);
173 g_cond_signal(glk_data->event_queue_not_full);
179 g_mutex_unlock(glk_data->event_lock);
181 /* Check for interrupt */
184 /* If an abort event was generated, the thread should have exited by now */
185 g_assert(event->type != evtype_Abort);