/
evtsrc.c
288 lines (222 loc) · 7.21 KB
/
evtsrc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Event sources.
*
* By Peter Wang.
*
* See readme.txt for copyright information.
*/
/* Title: Event sources
*/
#include "allegro5/allegro.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_dtor.h"
#include "allegro5/internal/aintern_events.h"
#include "allegro5/internal/aintern_system.h"
ALLEGRO_STATIC_ASSERT(evtsrc,
sizeof(ALLEGRO_EVENT_SOURCE_REAL) <= sizeof(ALLEGRO_EVENT_SOURCE));
/* Internal function: _al_event_source_init
* Initialise an event source structure.
*/
void _al_event_source_init(ALLEGRO_EVENT_SOURCE *es)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
memset(es, 0, sizeof(*es));
_AL_MARK_MUTEX_UNINITED(this->mutex);
_al_mutex_init(&this->mutex);
_al_vector_init(&this->queues, sizeof(ALLEGRO_EVENT_QUEUE *));
this->data = 0;
}
/* Internal function: _al_event_source_free
* Free the resources using by an event source structure. It
* automatically unregisters the event source from all the event
* queues it is currently registered with.
*/
void _al_event_source_free(ALLEGRO_EVENT_SOURCE *es)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
/* Unregister from all queues. */
while (!_al_vector_is_empty(&this->queues)) {
ALLEGRO_EVENT_QUEUE **slot = _al_vector_ref_back(&this->queues);
al_unregister_event_source(*slot, es);
}
_al_vector_free(&this->queues);
_al_mutex_destroy(&this->mutex);
}
/* Internal function: _al_event_source_lock
* Lock the event source. See below for when you should call this function.
*/
void _al_event_source_lock(ALLEGRO_EVENT_SOURCE *es)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
_al_mutex_lock(&this->mutex);
}
/* Internal function: _al_event_source_unlock
* Unlock the event source.
*/
void _al_event_source_unlock(ALLEGRO_EVENT_SOURCE *es)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
_al_mutex_unlock(&this->mutex);
}
/* Internal function: _al_event_source_on_registration_to_queue
* This function is called by al_register_event_source() when an
* event source is registered to an event queue. This gives the
* event source a chance to remember which queues it is registered
* to.
*/
void _al_event_source_on_registration_to_queue(ALLEGRO_EVENT_SOURCE *es,
ALLEGRO_EVENT_QUEUE *queue)
{
_al_event_source_lock(es);
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
/* Add the queue to the source's list. */
ALLEGRO_EVENT_QUEUE **slot = _al_vector_alloc_back(&this->queues);
*slot = queue;
}
_al_event_source_unlock(es);
}
/* Internal function: _al_event_source_on_unregistration_from_queue
* This function is called by al_unregister_event_source() when an
* event source is unregistered from a queue.
*/
void _al_event_source_on_unregistration_from_queue(ALLEGRO_EVENT_SOURCE *es,
ALLEGRO_EVENT_QUEUE *queue)
{
_al_event_source_lock(es);
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
_al_vector_find_and_delete(&this->queues, &queue);
}
_al_event_source_unlock(es);
}
/* Internal function: _al_event_source_needs_to_generate_event
* This function is called by modules that implement event sources
* when some interesting thing happens. They call this to check if
* they should bother generating an event of the given type, i.e. if
* the given event source is actually registered with one or more
* event queues. This is an optimisation to avoid allocating and
* filling in unwanted event structures.
*
* The event source must be _locked_ before calling this function.
*
* [runs in background threads]
*/
bool _al_event_source_needs_to_generate_event(ALLEGRO_EVENT_SOURCE *es)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
/* We don't consider pausing of event queues, but it does not seem worth
* optimising for.
*/
return !_al_vector_is_empty(&this->queues);
}
/* Internal function: _al_event_source_emit_event
* After an event structure has been filled in, it is time for the
* event source to tell the event queues it knows of about the new
* event. Afterwards, the caller of this function should not touch
* the event any more.
*
* The event source must be _locked_ before calling this function.
*
* [runs in background threads]
*/
void _al_event_source_emit_event(ALLEGRO_EVENT_SOURCE *es, ALLEGRO_EVENT *event)
{
ALLEGRO_EVENT_SOURCE_REAL *this = (ALLEGRO_EVENT_SOURCE_REAL *)es;
event->any.source = es;
/* Push the event to all the queues that this event source is
* registered to.
*/
{
size_t num_queues = _al_vector_size(&this->queues);
unsigned int i;
ALLEGRO_EVENT_QUEUE **slot;
for (i = 0; i < num_queues; i++) {
slot = _al_vector_ref(&this->queues, i);
_al_event_queue_push_event(*slot, event);
}
}
}
/* Function: al_init_user_event_source
*/
void al_init_user_event_source(ALLEGRO_EVENT_SOURCE *src)
{
ASSERT(src);
_al_event_source_init(src);
}
/* Function: al_destroy_user_event_source
*/
void al_destroy_user_event_source(ALLEGRO_EVENT_SOURCE *src)
{
if (src) {
_al_event_source_free(src);
}
}
/* Function: al_emit_user_event
*/
bool al_emit_user_event(ALLEGRO_EVENT_SOURCE *src,
ALLEGRO_EVENT *event, void (*dtor)(ALLEGRO_USER_EVENT *))
{
size_t num_queues;
bool rc;
ASSERT(src);
ASSERT(event);
if (dtor) {
ALLEGRO_USER_EVENT_DESCRIPTOR *descr = al_malloc(sizeof(*descr));
descr->refcount = 0;
descr->dtor = dtor;
event->user.__internal__descr = descr;
}
else {
event->user.__internal__descr = NULL;
}
_al_event_source_lock(src);
{
ALLEGRO_EVENT_SOURCE_REAL *rsrc = (ALLEGRO_EVENT_SOURCE_REAL *)src;
num_queues = _al_vector_size(&rsrc->queues);
if (num_queues > 0) {
event->any.timestamp = al_get_time();
_al_event_source_emit_event(src, event);
rc = true;
}
else {
rc = false;
}
}
_al_event_source_unlock(src);
if (dtor && !rc) {
dtor(&event->user);
al_free(event->user.__internal__descr);
}
return rc;
}
/* Function: al_set_event_source_data
*/
void al_set_event_source_data(ALLEGRO_EVENT_SOURCE *source, intptr_t data)
{
ALLEGRO_EVENT_SOURCE_REAL *const rsource = (ALLEGRO_EVENT_SOURCE_REAL *)source;
rsource->data = data;
}
/* Function: al_get_event_source_data
*/
intptr_t al_get_event_source_data(const ALLEGRO_EVENT_SOURCE *source)
{
const ALLEGRO_EVENT_SOURCE_REAL *const rsource = (ALLEGRO_EVENT_SOURCE_REAL *)source;
return rsource->data;
}
/*
* Local Variables:
* c-basic-offset: 3
* indent-tabs-mode: nil
* End:
*/
/* vim: set sts=3 sw=3 et: */