comparison src/events/SDL_events.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 6e7ec5cb83c3
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
37 Uint8 SDL_ProcessEvents[SDL_NUMEVENTS]; 37 Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
38 static Uint32 SDL_eventstate = 0; 38 static Uint32 SDL_eventstate = 0;
39 39
40 /* Private data -- event queue */ 40 /* Private data -- event queue */
41 #define MAXEVENTS 128 41 #define MAXEVENTS 128
42 static struct { 42 static struct
43 SDL_mutex *lock; 43 {
44 int active; 44 SDL_mutex *lock;
45 int head; 45 int active;
46 int tail; 46 int head;
47 SDL_Event event[MAXEVENTS]; 47 int tail;
48 int wmmsg_next; 48 SDL_Event event[MAXEVENTS];
49 struct SDL_SysWMmsg wmmsg[MAXEVENTS]; 49 int wmmsg_next;
50 struct SDL_SysWMmsg wmmsg[MAXEVENTS];
50 } SDL_EventQ; 51 } SDL_EventQ;
51 52
52 /* Private data -- event locking structure */ 53 /* Private data -- event locking structure */
53 static struct { 54 static struct
54 SDL_mutex *lock; 55 {
55 int safe; 56 SDL_mutex *lock;
57 int safe;
56 } SDL_EventLock; 58 } SDL_EventLock;
57 59
58 /* Thread functions */ 60 /* Thread functions */
59 static SDL_Thread *SDL_EventThread = NULL; /* Thread handle */ 61 static SDL_Thread *SDL_EventThread = NULL; /* Thread handle */
60 static Uint32 event_thread; /* The event thread id */ 62 static Uint32 event_thread; /* The event thread id */
61 63
62 void SDL_Lock_EventThread(void) 64 void
63 { 65 SDL_Lock_EventThread (void)
64 if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) { 66 {
65 /* Grab lock and spin until we're sure event thread stopped */ 67 if (SDL_EventThread && (SDL_ThreadID () != event_thread)) {
66 SDL_mutexP(SDL_EventLock.lock); 68 /* Grab lock and spin until we're sure event thread stopped */
67 while ( ! SDL_EventLock.safe ) { 69 SDL_mutexP (SDL_EventLock.lock);
68 SDL_Delay(1); 70 while (!SDL_EventLock.safe) {
69 } 71 SDL_Delay (1);
70 } 72 }
71 } 73 }
72 void SDL_Unlock_EventThread(void) 74 }
73 { 75 void
74 if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) { 76 SDL_Unlock_EventThread (void)
75 SDL_mutexV(SDL_EventLock.lock); 77 {
76 } 78 if (SDL_EventThread && (SDL_ThreadID () != event_thread)) {
79 SDL_mutexV (SDL_EventLock.lock);
80 }
77 } 81 }
78 82
79 #ifdef __OS2__ 83 #ifdef __OS2__
80 /* 84 /*
81 * We'll increase the priority of GobbleEvents thread, so it will process 85 * We'll increase the priority of GobbleEvents thread, so it will process
85 #define INCL_DOSPROCESS 89 #define INCL_DOSPROCESS
86 #include <os2.h> 90 #include <os2.h>
87 #include <time.h> 91 #include <time.h>
88 #endif 92 #endif
89 93
90 static int SDLCALL SDL_GobbleEvents(void *unused) 94 static int SDLCALL
91 { 95 SDL_GobbleEvents (void *unused)
92 event_thread = SDL_ThreadID(); 96 {
97 event_thread = SDL_ThreadID ();
93 98
94 #ifdef __OS2__ 99 #ifdef __OS2__
95 #ifdef USE_DOSSETPRIORITY 100 #ifdef USE_DOSSETPRIORITY
96 /* Increase thread priority, so it will process events in time for sure! */ 101 /* Increase thread priority, so it will process events in time for sure! */
97 DosSetPriority(PRTYS_THREAD, PRTYC_REGULAR, +16, 0); 102 DosSetPriority (PRTYS_THREAD, PRTYC_REGULAR, +16, 0);
98 #endif 103 #endif
99 #endif 104 #endif
100 105
101 while ( SDL_EventQ.active ) { 106 while (SDL_EventQ.active) {
102 SDL_VideoDevice *video = current_video; 107 SDL_VideoDevice *_this = SDL_GetVideoDevice ();
103 SDL_VideoDevice *this = current_video; 108
104 109 /* Get events from the video subsystem */
105 /* Get events from the video subsystem */ 110 if (_this) {
106 if ( video ) { 111 _this->PumpEvents (_this);
107 video->PumpEvents(this); 112 }
108 } 113
109 114 /* Queue pending key-repeat events */
110 /* Queue pending key-repeat events */ 115 SDL_CheckKeyRepeat ();
111 SDL_CheckKeyRepeat();
112 116
113 #if !SDL_JOYSTICK_DISABLED 117 #if !SDL_JOYSTICK_DISABLED
114 /* Check for joystick state change */ 118 /* Check for joystick state change */
115 if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) { 119 if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
116 SDL_JoystickUpdate(); 120 SDL_JoystickUpdate ();
117 } 121 }
118 #endif 122 #endif
119 123
120 /* Give up the CPU for the rest of our timeslice */ 124 /* Give up the CPU for the rest of our timeslice */
121 SDL_EventLock.safe = 1; 125 SDL_EventLock.safe = 1;
122 if ( SDL_timer_running ) { 126 if (SDL_timer_running) {
123 SDL_ThreadedTimerCheck(); 127 SDL_ThreadedTimerCheck ();
124 } 128 }
125 SDL_Delay(1); 129 SDL_Delay (1);
126 130
127 /* Check for event locking. 131 /* Check for event locking.
128 On the P of the lock mutex, if the lock is held, this thread 132 On the P of the lock mutex, if the lock is held, this thread
129 will wait until the lock is released before continuing. The 133 will wait until the lock is released before continuing. The
130 safe flag will be set, meaning that the other thread can go 134 safe flag will be set, meaning that the other thread can go
131 about it's business. The safe flag is reset before the V, 135 about it's business. The safe flag is reset before the V,
132 so as soon as the mutex is free, other threads can see that 136 so as soon as the mutex is free, other threads can see that
133 it's not safe to interfere with the event thread. 137 it's not safe to interfere with the event thread.
134 */ 138 */
135 SDL_mutexP(SDL_EventLock.lock); 139 SDL_mutexP (SDL_EventLock.lock);
136 SDL_EventLock.safe = 0; 140 SDL_EventLock.safe = 0;
137 SDL_mutexV(SDL_EventLock.lock); 141 SDL_mutexV (SDL_EventLock.lock);
138 } 142 }
139 SDL_SetTimerThreaded(0); 143 SDL_SetTimerThreaded (0);
140 event_thread = 0; 144 event_thread = 0;
141 return(0); 145 return (0);
142 } 146 }
143 147
144 static int SDL_StartEventThread(Uint32 flags) 148 static int
145 { 149 SDL_StartEventThread (Uint32 flags)
146 /* Reset everything to zero */ 150 {
147 SDL_EventThread = NULL; 151 /* Reset everything to zero */
148 SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock)); 152 SDL_EventThread = NULL;
149 153 SDL_memset (&SDL_EventLock, 0, sizeof (SDL_EventLock));
150 /* Create the lock and set ourselves active */ 154
155 /* Create the lock and set ourselves active */
151 #if !SDL_THREADS_DISABLED 156 #if !SDL_THREADS_DISABLED
152 SDL_EventQ.lock = SDL_CreateMutex(); 157 SDL_EventQ.lock = SDL_CreateMutex ();
153 if ( SDL_EventQ.lock == NULL ) { 158 if (SDL_EventQ.lock == NULL) {
154 #ifdef __MACOS__ /* MacOS classic you can't multithread, so no lock needed */ 159 #ifdef __MACOS__ /* MacOS classic you can't multithread, so no lock needed */
155 ; 160 ;
156 #else 161 #else
157 return(-1); 162 return (-1);
158 #endif 163 #endif
159 } 164 }
160 #endif /* !SDL_THREADS_DISABLED */ 165 #endif /* !SDL_THREADS_DISABLED */
161 SDL_EventQ.active = 1; 166 SDL_EventQ.active = 1;
162 167
163 if ( (flags&SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) { 168 if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
164 SDL_EventLock.lock = SDL_CreateMutex(); 169 SDL_EventLock.lock = SDL_CreateMutex ();
165 if ( SDL_EventLock.lock == NULL ) { 170 if (SDL_EventLock.lock == NULL) {
166 return(-1); 171 return (-1);
167 } 172 }
168 SDL_EventLock.safe = 0; 173 SDL_EventLock.safe = 0;
169 174
170 /* The event thread will handle timers too */ 175 /* The event thread will handle timers too */
171 SDL_SetTimerThreaded(2); 176 SDL_SetTimerThreaded (2);
172 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) 177 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
173 #undef SDL_CreateThread 178 #undef SDL_CreateThread
174 SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL); 179 SDL_EventThread =
180 SDL_CreateThread (SDL_GobbleEvents, NULL, NULL, NULL);
175 #else 181 #else
176 SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL); 182 SDL_EventThread = SDL_CreateThread (SDL_GobbleEvents, NULL);
177 #endif 183 #endif
178 if ( SDL_EventThread == NULL ) { 184 if (SDL_EventThread == NULL) {
179 return(-1); 185 return (-1);
180 } 186 }
181 } else { 187 } else {
182 event_thread = 0; 188 event_thread = 0;
183 } 189 }
184 return(0); 190 return (0);
185 } 191 }
186 192
187 static void SDL_StopEventThread(void) 193 static void
188 { 194 SDL_StopEventThread (void)
189 SDL_EventQ.active = 0; 195 {
190 if ( SDL_EventThread ) { 196 SDL_EventQ.active = 0;
191 SDL_WaitThread(SDL_EventThread, NULL); 197 if (SDL_EventThread) {
192 SDL_EventThread = NULL; 198 SDL_WaitThread (SDL_EventThread, NULL);
193 SDL_DestroyMutex(SDL_EventLock.lock); 199 SDL_EventThread = NULL;
194 } 200 SDL_DestroyMutex (SDL_EventLock.lock);
201 }
195 #ifndef IPOD 202 #ifndef IPOD
196 SDL_DestroyMutex(SDL_EventQ.lock); 203 SDL_DestroyMutex (SDL_EventQ.lock);
197 #endif 204 #endif
198 } 205 }
199 206
200 Uint32 SDL_EventThreadID(void) 207 Uint32
201 { 208 SDL_EventThreadID (void)
202 return(event_thread); 209 {
210 return (event_thread);
203 } 211 }
204 212
205 /* Public functions */ 213 /* Public functions */
206 214
207 void SDL_StopEventLoop(void) 215 void
208 { 216 SDL_StopEventLoop (void)
209 /* Halt the event thread, if running */ 217 {
210 SDL_StopEventThread(); 218 /* Halt the event thread, if running */
211 219 SDL_StopEventThread ();
212 /* Shutdown event handlers */ 220
213 SDL_AppActiveQuit(); 221 /* Shutdown event handlers */
214 SDL_KeyboardQuit(); 222 SDL_AppActiveQuit ();
215 SDL_MouseQuit(); 223 SDL_KeyboardQuit ();
216 SDL_QuitQuit(); 224 SDL_MouseQuit ();
217 225 SDL_QuitQuit ();
218 /* Clean out EventQ */ 226
219 SDL_EventQ.head = 0; 227 /* Clean out EventQ */
220 SDL_EventQ.tail = 0; 228 SDL_EventQ.head = 0;
221 SDL_EventQ.wmmsg_next = 0; 229 SDL_EventQ.tail = 0;
230 SDL_EventQ.wmmsg_next = 0;
222 } 231 }
223 232
224 /* This function (and associated calls) may be called more than once */ 233 /* This function (and associated calls) may be called more than once */
225 int SDL_StartEventLoop(Uint32 flags) 234 int
226 { 235 SDL_StartEventLoop (Uint32 flags)
227 int retcode; 236 {
228 237 int retcode;
229 /* Clean out the event queue */ 238
230 SDL_EventThread = NULL; 239 /* Clean out the event queue */
231 SDL_EventQ.lock = NULL; 240 SDL_EventThread = NULL;
232 SDL_StopEventLoop(); 241 SDL_EventQ.lock = NULL;
233 242 SDL_StopEventLoop ();
234 /* No filter to start with, process most event types */ 243
235 SDL_EventOK = NULL; 244 /* No filter to start with, process most event types */
236 SDL_memset(SDL_ProcessEvents,SDL_ENABLE,sizeof(SDL_ProcessEvents)); 245 SDL_EventOK = NULL;
237 SDL_eventstate = ~0; 246 SDL_memset (SDL_ProcessEvents, SDL_ENABLE, sizeof (SDL_ProcessEvents));
238 /* It's not save to call SDL_EventState() yet */ 247 SDL_eventstate = ~0;
239 SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT); 248 /* It's not save to call SDL_EventState() yet */
240 SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE; 249 SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
241 250 SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
242 /* Initialize event handlers */ 251
243 retcode = 0; 252 /* Initialize event handlers */
244 retcode += SDL_AppActiveInit(); 253 retcode = 0;
245 retcode += SDL_KeyboardInit(); 254 retcode += SDL_AppActiveInit ();
246 retcode += SDL_MouseInit(); 255 retcode += SDL_KeyboardInit ();
247 retcode += SDL_QuitInit(); 256 retcode += SDL_MouseInit ();
248 if ( retcode < 0 ) { 257 retcode += SDL_QuitInit ();
249 /* We don't expect them to fail, but... */ 258 if (retcode < 0) {
250 return(-1); 259 /* We don't expect them to fail, but... */
251 } 260 return (-1);
252 261 }
253 /* Create the lock and event thread */ 262
254 if ( SDL_StartEventThread(flags) < 0 ) { 263 /* Create the lock and event thread */
255 SDL_StopEventLoop(); 264 if (SDL_StartEventThread (flags) < 0) {
256 return(-1); 265 SDL_StopEventLoop ();
257 } 266 return (-1);
258 return(0); 267 }
268 return (0);
259 } 269 }
260 270
261 271
262 /* Add an event to the event queue -- called with the queue locked */ 272 /* Add an event to the event queue -- called with the queue locked */
263 static int SDL_AddEvent(SDL_Event *event) 273 static int
264 { 274 SDL_AddEvent (SDL_Event * event)
265 int tail, added; 275 {
266 276 int tail, added;
267 tail = (SDL_EventQ.tail+1)%MAXEVENTS; 277
268 if ( tail == SDL_EventQ.head ) { 278 tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
269 /* Overflow, drop event */ 279 if (tail == SDL_EventQ.head) {
270 added = 0; 280 /* Overflow, drop event */
271 } else { 281 added = 0;
272 SDL_EventQ.event[SDL_EventQ.tail] = *event; 282 } else {
273 if (event->type == SDL_SYSWMEVENT) { 283 SDL_EventQ.event[SDL_EventQ.tail] = *event;
274 /* Note that it's possible to lose an event */ 284 if (event->type == SDL_SYSWMEVENT) {
275 int next = SDL_EventQ.wmmsg_next; 285 /* Note that it's possible to lose an event */
276 SDL_EventQ.wmmsg[next] = *event->syswm.msg; 286 int next = SDL_EventQ.wmmsg_next;
277 SDL_EventQ.event[SDL_EventQ.tail].syswm.msg = 287 SDL_EventQ.wmmsg[next] = *event->syswm.msg;
278 &SDL_EventQ.wmmsg[next]; 288 SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
279 SDL_EventQ.wmmsg_next = (next+1)%MAXEVENTS; 289 &SDL_EventQ.wmmsg[next];
280 } 290 SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
281 SDL_EventQ.tail = tail; 291 }
282 added = 1; 292 SDL_EventQ.tail = tail;
283 } 293 added = 1;
284 return(added); 294 }
295 return (added);
285 } 296 }
286 297
287 /* Cut an event, and return the next valid spot, or the tail */ 298 /* Cut an event, and return the next valid spot, or the tail */
288 /* -- called with the queue locked */ 299 /* -- called with the queue locked */
289 static int SDL_CutEvent(int spot) 300 static int
290 { 301 SDL_CutEvent (int spot)
291 if ( spot == SDL_EventQ.head ) { 302 {
292 SDL_EventQ.head = (SDL_EventQ.head+1)%MAXEVENTS; 303 if (spot == SDL_EventQ.head) {
293 return(SDL_EventQ.head); 304 SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
294 } else 305 return (SDL_EventQ.head);
295 if ( (spot+1)%MAXEVENTS == SDL_EventQ.tail ) { 306 } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
296 SDL_EventQ.tail = spot; 307 SDL_EventQ.tail = spot;
297 return(SDL_EventQ.tail); 308 return (SDL_EventQ.tail);
298 } else 309 } else
299 /* We cut the middle -- shift everything over */ 310 /* We cut the middle -- shift everything over */
300 { 311 {
301 int here, next; 312 int here, next;
302 313
303 /* This can probably be optimized with SDL_memcpy() -- careful! */ 314 /* This can probably be optimized with SDL_memcpy() -- careful! */
304 if ( --SDL_EventQ.tail < 0 ) { 315 if (--SDL_EventQ.tail < 0) {
305 SDL_EventQ.tail = MAXEVENTS-1; 316 SDL_EventQ.tail = MAXEVENTS - 1;
306 } 317 }
307 for ( here=spot; here != SDL_EventQ.tail; here = next ) { 318 for (here = spot; here != SDL_EventQ.tail; here = next) {
308 next = (here+1)%MAXEVENTS; 319 next = (here + 1) % MAXEVENTS;
309 SDL_EventQ.event[here] = SDL_EventQ.event[next]; 320 SDL_EventQ.event[here] = SDL_EventQ.event[next];
310 } 321 }
311 return(spot); 322 return (spot);
312 } 323 }
313 /* NOTREACHED */ 324 /* NOTREACHED */
314 } 325 }
315 326
316 /* Lock the event queue, take a peep at it, and unlock it */ 327 /* Lock the event queue, take a peep at it, and unlock it */
317 int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, 328 int
318 Uint32 mask) 329 SDL_PeepEvents (SDL_Event * events, int numevents, SDL_eventaction action,
319 { 330 Uint32 mask)
320 int i, used; 331 {
321 332 int i, used;
322 /* Don't look after we've quit */ 333
323 if ( ! SDL_EventQ.active ) { 334 /* Don't look after we've quit */
324 return(-1); 335 if (!SDL_EventQ.active) {
325 } 336 return (-1);
326 /* Lock the event queue */ 337 }
327 used = 0; 338 /* Lock the event queue */
328 if ( SDL_mutexP(SDL_EventQ.lock) == 0 ) { 339 used = 0;
329 if ( action == SDL_ADDEVENT ) { 340 if (SDL_mutexP (SDL_EventQ.lock) == 0) {
330 for ( i=0; i<numevents; ++i ) { 341 if (action == SDL_ADDEVENT) {
331 used += SDL_AddEvent(&events[i]); 342 for (i = 0; i < numevents; ++i) {
332 } 343 used += SDL_AddEvent (&events[i]);
333 } else { 344 }
334 SDL_Event tmpevent; 345 } else {
335 int spot; 346 SDL_Event tmpevent;
336 347 int spot;
337 /* If 'events' is NULL, just see if they exist */ 348
338 if ( events == NULL ) { 349 /* If 'events' is NULL, just see if they exist */
339 action = SDL_PEEKEVENT; 350 if (events == NULL) {
340 numevents = 1; 351 action = SDL_PEEKEVENT;
341 events = &tmpevent; 352 numevents = 1;
342 } 353 events = &tmpevent;
343 spot = SDL_EventQ.head; 354 }
344 while ((used < numevents)&&(spot != SDL_EventQ.tail)) { 355 spot = SDL_EventQ.head;
345 if ( mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type) ) { 356 while ((used < numevents) && (spot != SDL_EventQ.tail)) {
346 events[used++] = SDL_EventQ.event[spot]; 357 if (mask & SDL_EVENTMASK (SDL_EventQ.event[spot].type)) {
347 if ( action == SDL_GETEVENT ) { 358 events[used++] = SDL_EventQ.event[spot];
348 spot = SDL_CutEvent(spot); 359 if (action == SDL_GETEVENT) {
349 } else { 360 spot = SDL_CutEvent (spot);
350 spot = (spot+1)%MAXEVENTS; 361 } else {
351 } 362 spot = (spot + 1) % MAXEVENTS;
352 } else { 363 }
353 spot = (spot+1)%MAXEVENTS; 364 } else {
354 } 365 spot = (spot + 1) % MAXEVENTS;
355 } 366 }
356 } 367 }
357 SDL_mutexV(SDL_EventQ.lock); 368 }
358 } else { 369 SDL_mutexV (SDL_EventQ.lock);
359 SDL_SetError("Couldn't lock event queue"); 370 } else {
360 used = -1; 371 SDL_SetError ("Couldn't lock event queue");
361 } 372 used = -1;
362 return(used); 373 }
374 return (used);
363 } 375 }
364 376
365 /* Run the system dependent event loops */ 377 /* Run the system dependent event loops */
366 void SDL_PumpEvents(void) 378 void
367 { 379 SDL_PumpEvents (void)
368 if ( !SDL_EventThread ) { 380 {
369 SDL_VideoDevice *video = current_video; 381 if (!SDL_EventThread) {
370 SDL_VideoDevice *this = current_video; 382 SDL_VideoDevice *_this = SDL_GetVideoDevice ();
371 383
372 /* Get events from the video subsystem */ 384 /* Get events from the video subsystem */
373 if ( video ) { 385 if (_this) {
374 video->PumpEvents(this); 386 _this->PumpEvents (_this);
375 } 387 }
376 388
377 /* Queue pending key-repeat events */ 389 /* Queue pending key-repeat events */
378 SDL_CheckKeyRepeat(); 390 SDL_CheckKeyRepeat ();
379 391
380 #if !SDL_JOYSTICK_DISABLED 392 #if !SDL_JOYSTICK_DISABLED
381 /* Check for joystick state change */ 393 /* Check for joystick state change */
382 if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) { 394 if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
383 SDL_JoystickUpdate(); 395 SDL_JoystickUpdate ();
384 } 396 }
385 #endif 397 #endif
386 } 398 }
387 } 399 }
388 400
389 /* Public functions */ 401 /* Public functions */
390 402
391 int SDL_PollEvent (SDL_Event *event) 403 int
392 { 404 SDL_PollEvent (SDL_Event * event)
393 SDL_PumpEvents(); 405 {
394 406 SDL_PumpEvents ();
395 /* We can't return -1, just return 0 (no event) on error */ 407
396 if ( SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0 ) 408 /* We can't return -1, just return 0 (no event) on error */
397 return 0; 409 if (SDL_PeepEvents (event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0)
398 return 1; 410 return 0;
399 } 411 return 1;
400 412 }
401 int SDL_WaitEvent (SDL_Event *event) 413
402 { 414 int
403 while ( 1 ) { 415 SDL_WaitEvent (SDL_Event * event)
404 SDL_PumpEvents(); 416 {
405 switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) { 417 while (1) {
406 case -1: return 0; 418 SDL_PumpEvents ();
407 case 1: return 1; 419 switch (SDL_PeepEvents (event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
408 case 0: SDL_Delay(10); 420 case -1:
409 } 421 return 0;
410 } 422 case 1:
411 } 423 return 1;
412 424 case 0:
413 int SDL_PushEvent(SDL_Event *event) 425 SDL_Delay (10);
414 { 426 }
415 if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 ) 427 }
416 return -1; 428 }
417 return 0; 429
418 } 430 int
419 431 SDL_PushEvent (SDL_Event * event)
420 void SDL_SetEventFilter (SDL_EventFilter filter) 432 {
421 { 433 if (SDL_PeepEvents (event, 1, SDL_ADDEVENT, 0) <= 0)
422 SDL_Event bitbucket; 434 return -1;
423 435 return 0;
424 /* Set filter and discard pending events */ 436 }
425 SDL_EventOK = filter; 437
426 while ( SDL_PollEvent(&bitbucket) > 0 ) 438 void
427 ; 439 SDL_SetEventFilter (SDL_EventFilter filter)
428 } 440 {
429 441 SDL_Event bitbucket;
430 SDL_EventFilter SDL_GetEventFilter(void) 442
431 { 443 /* Set filter and discard pending events */
432 return(SDL_EventOK); 444 SDL_EventOK = filter;
433 } 445 while (SDL_PollEvent (&bitbucket) > 0);
434 446 }
435 Uint8 SDL_EventState (Uint8 type, int state) 447
436 { 448 SDL_EventFilter
437 SDL_Event bitbucket; 449 SDL_GetEventFilter (void)
438 Uint8 current_state; 450 {
439 451 return (SDL_EventOK);
440 /* If SDL_ALLEVENTS was specified... */ 452 }
441 if ( type == 0xFF ) { 453
442 current_state = SDL_IGNORE; 454 Uint8
443 for ( type=0; type<SDL_NUMEVENTS; ++type ) { 455 SDL_EventState (Uint8 type, int state)
444 if ( SDL_ProcessEvents[type] != SDL_IGNORE ) { 456 {
445 current_state = SDL_ENABLE; 457 SDL_Event bitbucket;
446 } 458 Uint8 current_state;
447 SDL_ProcessEvents[type] = state; 459
448 if ( state == SDL_ENABLE ) { 460 /* If SDL_ALLEVENTS was specified... */
449 SDL_eventstate |= (0x00000001 << (type)); 461 if (type == 0xFF) {
450 } else { 462 current_state = SDL_IGNORE;
451 SDL_eventstate &= ~(0x00000001 << (type)); 463 for (type = 0; type < SDL_NUMEVENTS; ++type) {
452 } 464 if (SDL_ProcessEvents[type] != SDL_IGNORE) {
453 } 465 current_state = SDL_ENABLE;
454 while ( SDL_PollEvent(&bitbucket) > 0 ) 466 }
455 ; 467 SDL_ProcessEvents[type] = state;
456 return(current_state); 468 if (state == SDL_ENABLE) {
457 } 469 SDL_eventstate |= (0x00000001 << (type));
458 470 } else {
459 /* Just set the state for one event type */ 471 SDL_eventstate &= ~(0x00000001 << (type));
460 current_state = SDL_ProcessEvents[type]; 472 }
461 switch (state) { 473 }
462 case SDL_IGNORE: 474 while (SDL_PollEvent (&bitbucket) > 0);
463 case SDL_ENABLE: 475 return (current_state);
464 /* Set state and discard pending events */ 476 }
465 SDL_ProcessEvents[type] = state; 477
466 if ( state == SDL_ENABLE ) { 478 /* Just set the state for one event type */
467 SDL_eventstate |= (0x00000001 << (type)); 479 current_state = SDL_ProcessEvents[type];
468 } else { 480 switch (state) {
469 SDL_eventstate &= ~(0x00000001 << (type)); 481 case SDL_IGNORE:
470 } 482 case SDL_ENABLE:
471 while ( SDL_PollEvent(&bitbucket) > 0 ) 483 /* Set state and discard pending events */
472 ; 484 SDL_ProcessEvents[type] = state;
473 break; 485 if (state == SDL_ENABLE) {
474 default: 486 SDL_eventstate |= (0x00000001 << (type));
475 /* Querying state? */ 487 } else {
476 break; 488 SDL_eventstate &= ~(0x00000001 << (type));
477 } 489 }
478 return(current_state); 490 while (SDL_PollEvent (&bitbucket) > 0);
491 break;
492 default:
493 /* Querying state? */
494 break;
495 }
496 return (current_state);
479 } 497 }
480 498
481 /* This is a generic event handler. 499 /* This is a generic event handler.
482 */ 500 */
483 int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message) 501 int
484 { 502 SDL_PrivateSysWMEvent (SDL_SysWMmsg * message)
485 int posted; 503 {
486 504 int posted;
487 posted = 0; 505
488 if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) { 506 posted = 0;
489 SDL_Event event; 507 if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
490 SDL_memset(&event, 0, sizeof(event)); 508 SDL_Event event;
491 event.type = SDL_SYSWMEVENT; 509 SDL_memset (&event, 0, sizeof (event));
492 event.syswm.msg = message; 510 event.type = SDL_SYSWMEVENT;
493 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 511 event.syswm.msg = message;
494 posted = 1; 512 if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) {
495 SDL_PushEvent(&event); 513 posted = 1;
496 } 514 SDL_PushEvent (&event);
497 } 515 }
498 /* Update internal event state */ 516 }
499 return(posted); 517 /* Update internal event state */
500 } 518 return (posted);
519 }
520
521 /* vi: set ts=4 sw=4 expandtab: */