2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* General event handling code for SDL */
27 #include "SDL_syswm.h"
28 #include "SDL_sysevents.h"
29 #include "SDL_events_c.h"
30 #include "../timer/SDL_timer_c.h"
31 #if !SDL_JOYSTICK_DISABLED
32 #include "../joystick/SDL_joystick_c.h"
35 /* Public data -- the event filter */
36 SDL_EventFilter SDL_EventOK = NULL;
37 Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
38 static Uint32 SDL_eventstate = 0;
40 /* Private data -- event queue */
47 SDL_Event event[MAXEVENTS];
49 struct SDL_SysWMmsg wmmsg[MAXEVENTS];
52 /* Private data -- event locking structure */
58 /* Thread functions */
59 static SDL_Thread *SDL_EventThread = NULL; /* Thread handle */
60 static Uint32 event_thread; /* The event thread id */
62 void SDL_Lock_EventThread(void)
64 if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
65 /* Grab lock and spin until we're sure event thread stopped */
66 SDL_mutexP(SDL_EventLock.lock);
67 while ( ! SDL_EventLock.safe ) {
72 void SDL_Unlock_EventThread(void)
74 if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
75 SDL_mutexV(SDL_EventLock.lock);
81 * We'll increase the priority of GobbleEvents thread, so it will process
82 * events in time for sure! For this, we need the DosSetPriority() API
83 * from the os2.h include file.
85 #define INCL_DOSPROCESS
90 static int SDLCALL SDL_GobbleEvents(void *unused)
92 event_thread = SDL_ThreadID();
95 #ifdef USE_DOSSETPRIORITY
96 /* Increase thread priority, so it will process events in time for sure! */
97 DosSetPriority(PRTYS_THREAD, PRTYC_REGULAR, +16, 0);
101 while ( SDL_EventQ.active ) {
102 SDL_VideoDevice *video = current_video;
103 SDL_VideoDevice *this = current_video;
105 /* Get events from the video subsystem */
107 video->PumpEvents(this);
110 /* Queue pending key-repeat events */
111 SDL_CheckKeyRepeat();
113 #if !SDL_JOYSTICK_DISABLED
114 /* Check for joystick state change */
115 if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
116 SDL_JoystickUpdate();
120 /* Give up the CPU for the rest of our timeslice */
121 SDL_EventLock.safe = 1;
122 if ( SDL_timer_running ) {
123 SDL_ThreadedTimerCheck();
127 /* Check for event locking.
128 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
130 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,
132 so as soon as the mutex is free, other threads can see that
133 it's not safe to interfere with the event thread.
135 SDL_mutexP(SDL_EventLock.lock);
136 SDL_EventLock.safe = 0;
137 SDL_mutexV(SDL_EventLock.lock);
139 SDL_SetTimerThreaded(0);
144 static int SDL_StartEventThread(Uint32 flags)
146 /* Reset everything to zero */
147 SDL_EventThread = NULL;
148 SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
150 /* Create the lock and set ourselves active */
151 #if !SDL_THREADS_DISABLED
152 SDL_EventQ.lock = SDL_CreateMutex();
153 if ( SDL_EventQ.lock == NULL ) {
154 #ifdef __MACOS__ /* MacOS classic you can't multithread, so no lock needed */
160 #endif /* !SDL_THREADS_DISABLED */
161 SDL_EventQ.active = 1;
163 if ( (flags&SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
164 SDL_EventLock.lock = SDL_CreateMutex();
165 if ( SDL_EventLock.lock == NULL ) {
168 SDL_EventLock.safe = 0;
170 /* The event thread will handle timers too */
171 SDL_SetTimerThreaded(2);
172 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) && !defined(__SYMBIAN32__)
173 #undef SDL_CreateThread
174 SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
176 SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
178 if ( SDL_EventThread == NULL ) {
187 static void SDL_StopEventThread(void)
189 SDL_EventQ.active = 0;
190 if ( SDL_EventThread ) {
191 SDL_WaitThread(SDL_EventThread, NULL);
192 SDL_EventThread = NULL;
193 SDL_DestroyMutex(SDL_EventLock.lock);
194 SDL_EventLock.lock = NULL;
197 SDL_DestroyMutex(SDL_EventQ.lock);
198 SDL_EventQ.lock = NULL;
202 Uint32 SDL_EventThreadID(void)
204 return(event_thread);
207 /* Public functions */
209 void SDL_StopEventLoop(void)
211 /* Halt the event thread, if running */
212 SDL_StopEventThread();
214 /* Shutdown event handlers */
220 /* Clean out EventQ */
223 SDL_EventQ.wmmsg_next = 0;
226 /* This function (and associated calls) may be called more than once */
227 int SDL_StartEventLoop(Uint32 flags)
231 /* Clean out the event queue */
232 SDL_EventThread = NULL;
233 SDL_EventQ.lock = NULL;
236 /* No filter to start with, process most event types */
238 SDL_memset(SDL_ProcessEvents,SDL_ENABLE,sizeof(SDL_ProcessEvents));
240 /* It's not save to call SDL_EventState() yet */
241 SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
242 SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
244 /* Initialize event handlers */
246 retcode += SDL_AppActiveInit();
247 retcode += SDL_KeyboardInit();
248 retcode += SDL_MouseInit();
249 retcode += SDL_QuitInit();
251 /* We don't expect them to fail, but... */
255 /* Create the lock and event thread */
256 if ( SDL_StartEventThread(flags) < 0 ) {
264 /* Add an event to the event queue -- called with the queue locked */
265 static int SDL_AddEvent(SDL_Event *event)
269 tail = (SDL_EventQ.tail+1)%MAXEVENTS;
270 if ( tail == SDL_EventQ.head ) {
271 /* Overflow, drop event */
274 SDL_EventQ.event[SDL_EventQ.tail] = *event;
275 if (event->type == SDL_SYSWMEVENT) {
276 /* Note that it's possible to lose an event */
277 int next = SDL_EventQ.wmmsg_next;
278 SDL_EventQ.wmmsg[next] = *event->syswm.msg;
279 SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
280 &SDL_EventQ.wmmsg[next];
281 SDL_EventQ.wmmsg_next = (next+1)%MAXEVENTS;
283 SDL_EventQ.tail = tail;
289 /* Cut an event, and return the next valid spot, or the tail */
290 /* -- called with the queue locked */
291 static int SDL_CutEvent(int spot)
293 if ( spot == SDL_EventQ.head ) {
294 SDL_EventQ.head = (SDL_EventQ.head+1)%MAXEVENTS;
295 return(SDL_EventQ.head);
297 if ( (spot+1)%MAXEVENTS == SDL_EventQ.tail ) {
298 SDL_EventQ.tail = spot;
299 return(SDL_EventQ.tail);
301 /* We cut the middle -- shift everything over */
305 /* This can probably be optimized with SDL_memcpy() -- careful! */
306 if ( --SDL_EventQ.tail < 0 ) {
307 SDL_EventQ.tail = MAXEVENTS-1;
309 for ( here=spot; here != SDL_EventQ.tail; here = next ) {
310 next = (here+1)%MAXEVENTS;
311 SDL_EventQ.event[here] = SDL_EventQ.event[next];
318 /* Lock the event queue, take a peep at it, and unlock it */
319 int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
324 /* Don't look after we've quit */
325 if ( ! SDL_EventQ.active ) {
328 /* Lock the event queue */
330 if ( SDL_mutexP(SDL_EventQ.lock) == 0 ) {
331 if ( action == SDL_ADDEVENT ) {
332 for ( i=0; i<numevents; ++i ) {
333 used += SDL_AddEvent(&events[i]);
339 /* If 'events' is NULL, just see if they exist */
340 if ( events == NULL ) {
341 action = SDL_PEEKEVENT;
345 spot = SDL_EventQ.head;
346 while ((used < numevents)&&(spot != SDL_EventQ.tail)) {
347 if ( mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type) ) {
348 events[used++] = SDL_EventQ.event[spot];
349 if ( action == SDL_GETEVENT ) {
350 spot = SDL_CutEvent(spot);
352 spot = (spot+1)%MAXEVENTS;
355 spot = (spot+1)%MAXEVENTS;
359 SDL_mutexV(SDL_EventQ.lock);
361 SDL_SetError("Couldn't lock event queue");
367 /* Run the system dependent event loops */
368 void SDL_PumpEvents(void)
370 if ( !SDL_EventThread ) {
371 SDL_VideoDevice *video = current_video;
372 SDL_VideoDevice *this = current_video;
374 /* Get events from the video subsystem */
376 video->PumpEvents(this);
379 /* Queue pending key-repeat events */
380 SDL_CheckKeyRepeat();
382 #if !SDL_JOYSTICK_DISABLED
383 /* Check for joystick state change */
384 if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
385 SDL_JoystickUpdate();
391 /* Public functions */
393 int SDL_PollEvent (SDL_Event *event)
397 /* We can't return -1, just return 0 (no event) on error */
398 if ( SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0 )
403 int SDL_WaitEvent (SDL_Event *event)
407 switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
410 case 0: SDL_Delay(10);
415 int SDL_PushEvent(SDL_Event *event)
417 if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
422 void SDL_SetEventFilter (SDL_EventFilter filter)
426 /* Set filter and discard pending events */
427 SDL_EventOK = filter;
428 while ( SDL_PollEvent(&bitbucket) > 0 )
432 SDL_EventFilter SDL_GetEventFilter(void)
437 Uint8 SDL_EventState (Uint8 type, int state)
442 /* If SDL_ALLEVENTS was specified... */
443 if ( type == 0xFF ) {
444 current_state = SDL_IGNORE;
445 for ( type=0; type<SDL_NUMEVENTS; ++type ) {
446 if ( SDL_ProcessEvents[type] != SDL_IGNORE ) {
447 current_state = SDL_ENABLE;
449 SDL_ProcessEvents[type] = state;
450 if ( state == SDL_ENABLE ) {
451 SDL_eventstate |= (0x00000001 << (type));
453 SDL_eventstate &= ~(0x00000001 << (type));
456 while ( SDL_PollEvent(&bitbucket) > 0 )
458 return(current_state);
461 /* Just set the state for one event type */
462 current_state = SDL_ProcessEvents[type];
466 /* Set state and discard pending events */
467 SDL_ProcessEvents[type] = state;
468 if ( state == SDL_ENABLE ) {
469 SDL_eventstate |= (0x00000001 << (type));
471 SDL_eventstate &= ~(0x00000001 << (type));
473 while ( SDL_PollEvent(&bitbucket) > 0 )
477 /* Querying state? */
480 return(current_state);
483 /* This is a generic event handler.
485 int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message)
490 if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
492 SDL_memset(&event, 0, sizeof(event));
493 event.type = SDL_SYSWMEVENT;
494 event.syswm.msg = message;
495 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
497 SDL_PushEvent(&event);
500 /* Update internal event state */