src/events/SDL_events.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 15 Apr 2002 04:51:26 +0000
changeset 352 b49fc922e7f6
parent 297 f6ffac90895c
child 353 d47637068e52
permissions -rw-r--r--
Fixed SDL_PollEvent() so it only returns a boolean value (not -1)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* General event handling code for SDL */
    29 
    30 #include <stdio.h>
    31 #include <string.h>
    32 
    33 #include "SDL.h"
    34 #include "SDL_thread.h"
    35 #include "SDL_mutex.h"
    36 #include "SDL_events.h"
    37 #include "SDL_events_c.h"
    38 #include "SDL_timer_c.h"
    39 #ifndef DISABLE_JOYSTICK
    40 #include "SDL_joystick_c.h"
    41 #endif
    42 #ifndef ENABLE_X11
    43 #define DISABLE_X11
    44 #endif
    45 #include "SDL_syswm.h"
    46 #include "SDL_sysevents.h"
    47 
    48 /* Public data -- the event filter */
    49 SDL_EventFilter SDL_EventOK = NULL;
    50 Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
    51 static Uint32 SDL_eventstate = 0;
    52 
    53 /* Private data -- event queue */
    54 #define MAXEVENTS	128
    55 static struct {
    56 	SDL_mutex *lock;
    57 	int active;
    58 	int head;
    59 	int tail;
    60 	SDL_Event event[MAXEVENTS];
    61 	int wmmsg_next;
    62 	struct SDL_SysWMmsg wmmsg[MAXEVENTS];
    63 } SDL_EventQ;
    64 
    65 /* Private data -- event locking structure */
    66 static struct {
    67 	SDL_mutex *lock;
    68 	int safe;
    69 } SDL_EventLock;
    70 
    71 /* Thread functions */
    72 static SDL_Thread *SDL_EventThread = NULL;	/* Thread handle */
    73 static Uint32 event_thread;			/* The event thread id */
    74 
    75 void SDL_Lock_EventThread(void)
    76 {
    77 	if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
    78 		/* Grab lock and spin until we're sure event thread stopped */
    79 		SDL_mutexP(SDL_EventLock.lock);
    80 		while ( ! SDL_EventLock.safe ) {
    81 			SDL_Delay(1);
    82 		}
    83 	}
    84 }
    85 void SDL_Unlock_EventThread(void)
    86 {
    87 	if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
    88 		SDL_mutexV(SDL_EventLock.lock);
    89 	}
    90 }
    91 
    92 static int SDL_GobbleEvents(void *unused)
    93 {
    94 	SDL_SetTimerThreaded(2);
    95 	event_thread = SDL_ThreadID();
    96 	while ( SDL_EventQ.active ) {
    97 		SDL_VideoDevice *video = current_video;
    98 		SDL_VideoDevice *this  = current_video;
    99 
   100 		/* Get events from the video subsystem */
   101 		if ( video ) {
   102 			video->PumpEvents(this);
   103 		}
   104 
   105 		/* Queue pending key-repeat events */
   106 		SDL_CheckKeyRepeat();
   107 
   108 #ifndef DISABLE_JOYSTICK
   109 		/* Check for joystick state change */
   110 		if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
   111 			SDL_JoystickUpdate();
   112 		}
   113 #endif
   114 
   115 		/* Give up the CPU for the rest of our timeslice */
   116 		SDL_EventLock.safe = 1;
   117 		if( SDL_timer_running ) {
   118 			SDL_ThreadedTimerCheck();
   119 		}
   120 		SDL_Delay(1);
   121 
   122 		/* Check for event locking.
   123 		   On the P of the lock mutex, if the lock is held, this thread
   124 		   will wait until the lock is released before continuing.  The
   125 		   safe flag will be set, meaning that the other thread can go
   126 		   about it's business.  The safe flag is reset before the V,
   127 		   so as soon as the mutex is free, other threads can see that
   128 		   it's not safe to interfere with the event thread.
   129 		 */
   130 		SDL_mutexP(SDL_EventLock.lock);
   131 		SDL_EventLock.safe = 0;
   132 		SDL_mutexV(SDL_EventLock.lock);
   133 	}
   134 	SDL_SetTimerThreaded(0);
   135 	event_thread = 0;
   136 	return(0);
   137 }
   138 
   139 static int SDL_StartEventThread(Uint32 flags)
   140 {
   141 	/* Reset everything to zero */
   142 	SDL_EventThread = NULL;
   143 	memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
   144 
   145 	/* Create the lock and set ourselves active */
   146 #ifndef DISABLE_THREADS
   147 	SDL_EventQ.lock = SDL_CreateMutex();
   148 	if ( SDL_EventQ.lock == NULL ) {
   149 #ifdef macintosh /* On MacOS 7/8, you can't multithread, so no lock needed */
   150 		;
   151 #else
   152 		return(-1);
   153 #endif
   154 	}
   155 #endif /* !DISABLE_THREADS */
   156 	SDL_EventQ.active = 1;
   157 
   158 	if ( (flags&SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
   159 		SDL_EventLock.lock = SDL_CreateMutex();
   160 		if ( SDL_EventLock.lock == NULL ) {
   161 			return(-1);
   162 		}
   163 		SDL_EventLock.safe = 0;
   164 
   165 		SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
   166 		if ( SDL_EventThread == NULL ) {
   167 			return(-1);
   168 		}
   169 	} else {
   170 		event_thread = 0;
   171 	}
   172 	return(0);
   173 }
   174 
   175 static void SDL_StopEventThread(void)
   176 {
   177 	SDL_EventQ.active = 0;
   178 	if ( SDL_EventThread ) {
   179 		SDL_WaitThread(SDL_EventThread, NULL);
   180 		SDL_EventThread = NULL;
   181 		SDL_DestroyMutex(SDL_EventLock.lock);
   182 	}
   183 	SDL_DestroyMutex(SDL_EventQ.lock);
   184 }
   185 
   186 Uint32 SDL_EventThreadID(void)
   187 {
   188 	return(event_thread);
   189 }
   190 
   191 /* Public functions */
   192 
   193 void SDL_StopEventLoop(void)
   194 {
   195 	/* Halt the event thread, if running */
   196 	SDL_StopEventThread();
   197 
   198 	/* Clean out EventQ */
   199 	SDL_EventQ.head = 0;
   200 	SDL_EventQ.tail = 0;
   201 	SDL_EventQ.wmmsg_next = 0;
   202 }
   203 
   204 /* This function (and associated calls) may be called more than once */
   205 int SDL_StartEventLoop(Uint32 flags)
   206 {
   207 	int retcode;
   208 
   209 	/* Clean out the event queue */
   210 	SDL_EventThread = NULL;
   211 	SDL_EventQ.lock = NULL;
   212 	SDL_StopEventLoop();
   213 
   214 	/* No filter to start with, process most event types */
   215 	SDL_EventOK = NULL;
   216 	memset(SDL_ProcessEvents,SDL_ENABLE,sizeof(SDL_ProcessEvents));
   217 	SDL_eventstate = ~0;
   218 	/* It's not save to call SDL_EventState() yet */
   219 	SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
   220 	SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
   221 
   222 	/* Initialize event handlers */
   223 	retcode = 0;
   224 	retcode += SDL_AppActiveInit();
   225 	retcode += SDL_KeyboardInit();
   226 	retcode += SDL_MouseInit();
   227 	retcode += SDL_QuitInit();
   228 	if ( retcode < 0 ) {
   229 		/* We don't expect them to fail, but... */
   230 		return(-1);
   231 	}
   232 
   233 	/* Create the lock and event thread */
   234 	if ( SDL_StartEventThread(flags) < 0 ) {
   235 		SDL_StopEventLoop();
   236 		return(-1);
   237 	}
   238 	return(0);
   239 }
   240 
   241 
   242 /* Add an event to the event queue -- called with the queue locked */
   243 static int SDL_AddEvent(SDL_Event *event)
   244 {
   245 	int tail, added;
   246 
   247 	tail = (SDL_EventQ.tail+1)%MAXEVENTS;
   248 	if ( tail == SDL_EventQ.head ) {
   249 		/* Overflow, drop event */
   250 		added = 0;
   251 	} else {
   252 		SDL_EventQ.event[SDL_EventQ.tail] = *event;
   253 		if (event->type == SDL_SYSWMEVENT) {
   254 			/* Note that it's possible to lose an event */
   255 			int next = SDL_EventQ.wmmsg_next;
   256 			SDL_EventQ.wmmsg[next] = *event->syswm.msg;
   257 		        SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
   258 						&SDL_EventQ.wmmsg[next];
   259 			SDL_EventQ.wmmsg_next = (next+1)%MAXEVENTS;
   260 		}
   261 		SDL_EventQ.tail = tail;
   262 		added = 1;
   263 	}
   264 	return(added);
   265 }
   266 
   267 /* Cut an event, and return the next valid spot, or the tail */
   268 /*                           -- called with the queue locked */
   269 static int SDL_CutEvent(int spot)
   270 {
   271 	if ( spot == SDL_EventQ.head ) {
   272 		SDL_EventQ.head = (SDL_EventQ.head+1)%MAXEVENTS;
   273 		return(SDL_EventQ.head);
   274 	} else
   275 	if ( (spot+1)%MAXEVENTS == SDL_EventQ.tail ) {
   276 		SDL_EventQ.tail = spot;
   277 		return(SDL_EventQ.tail);
   278 	} else
   279 	/* We cut the middle -- shift everything over */
   280 	{
   281 		int here, next;
   282 
   283 		/* This can probably be optimized with memcpy() -- careful! */
   284 		if ( --SDL_EventQ.tail < 0 ) {
   285 			SDL_EventQ.tail = MAXEVENTS-1;
   286 		}
   287 		for ( here=spot; here != SDL_EventQ.tail; here = next ) {
   288 			next = (here+1)%MAXEVENTS;
   289 			SDL_EventQ.event[here] = SDL_EventQ.event[next];
   290 		}
   291 		return(spot);
   292 	}
   293 	/* NOTREACHED */
   294 }
   295 
   296 /* Lock the event queue, take a peep at it, and unlock it */
   297 int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
   298 								Uint32 mask)
   299 {
   300 	int i, used;
   301 
   302 	/* Don't look after we've quit */
   303 	if ( ! SDL_EventQ.active ) {
   304 		return(-1);
   305 	}
   306 	/* Lock the event queue */
   307 	used = 0;
   308 	if ( SDL_mutexP(SDL_EventQ.lock) == 0 ) {
   309 		if ( action == SDL_ADDEVENT ) {
   310 			for ( i=0; i<numevents; ++i ) {
   311 				used += SDL_AddEvent(&events[i]);
   312 			}
   313 		} else {
   314 			SDL_Event tmpevent;
   315 			int spot;
   316 
   317 			/* If 'events' is NULL, just see if they exist */
   318 			if ( events == NULL ) {
   319 				action = SDL_PEEKEVENT;
   320 				numevents = 1;
   321 				events = &tmpevent;
   322 			}
   323 			spot = SDL_EventQ.head;
   324 			while ((used < numevents)&&(spot != SDL_EventQ.tail)) {
   325 				if ( mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type) ) {
   326 					events[used++] = SDL_EventQ.event[spot];
   327 					if ( action == SDL_GETEVENT ) {
   328 						spot = SDL_CutEvent(spot);
   329 					} else {
   330 						spot = (spot+1)%MAXEVENTS;
   331 					}
   332 				} else {
   333 					spot = (spot+1)%MAXEVENTS;
   334 				}
   335 			}
   336 		}
   337 		SDL_mutexV(SDL_EventQ.lock);
   338 	} else {
   339 		SDL_SetError("Couldn't lock event queue");
   340 		used = -1;
   341 	}
   342 	return(used);
   343 }
   344 
   345 /* Run the system dependent event loops */
   346 void SDL_PumpEvents(void)
   347 {
   348 	if ( !SDL_EventThread ) {
   349 		SDL_VideoDevice *video = current_video;
   350 		SDL_VideoDevice *this  = current_video;
   351 
   352 		/* Get events from the video subsystem */
   353 		if ( video ) {
   354 			video->PumpEvents(this);
   355 		}
   356 
   357 		/* Queue pending key-repeat events */
   358 		SDL_CheckKeyRepeat();
   359 
   360 #ifndef DISABLE_JOYSTICK
   361 		/* Check for joystick state change */
   362 		if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
   363 			SDL_JoystickUpdate();
   364 		}
   365 #endif
   366 	}
   367 }
   368 
   369 /* Public functions */
   370 
   371 int SDL_PollEvent (SDL_Event *event)
   372 {
   373 	SDL_PumpEvents();
   374 
   375 	/* We can't return -1, just return 0 (no event) on error */
   376 	if ( SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0 )
   377 		return 0;
   378 	return 1;
   379 }
   380 
   381 int SDL_WaitEvent (SDL_Event *event)
   382 {
   383 	while ( 1 ) {
   384 		SDL_PumpEvents();
   385 		switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
   386 		    case -1: return -1; 
   387 		    case 1: return 1;
   388 		    case 0: SDL_Delay(10);
   389 		}
   390 	}
   391 }
   392 
   393 int SDL_PushEvent(SDL_Event *event)
   394 {
   395 	return(SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0));
   396 }
   397 
   398 void SDL_SetEventFilter (SDL_EventFilter filter)
   399 {
   400 	SDL_Event bitbucket;
   401 
   402 	/* Set filter and discard pending events */
   403 	SDL_EventOK = filter;
   404 	while ( SDL_PollEvent(&bitbucket) > 0 )
   405 		;
   406 }
   407 
   408 SDL_EventFilter SDL_GetEventFilter(void)
   409 {
   410 	return(SDL_EventOK);
   411 }
   412 
   413 Uint8 SDL_EventState (Uint8 type, int state)
   414 {
   415 	SDL_Event bitbucket;
   416 	Uint8 current_state;
   417 
   418 	/* If SDL_ALLEVENTS was specified... */
   419 	if ( type == 0xFF ) {
   420 		current_state = SDL_IGNORE;
   421 		for ( type=0; type<SDL_NUMEVENTS; ++type ) {
   422 			if ( SDL_ProcessEvents[type] != SDL_IGNORE ) {
   423 				current_state = SDL_ENABLE;
   424 			}
   425 			SDL_ProcessEvents[type] = state;
   426 			if ( state == SDL_ENABLE ) {
   427 				SDL_eventstate |= (0x00000001 << (type));
   428 			} else {
   429 				SDL_eventstate &= ~(0x00000001 << (type));
   430 			}
   431 		}
   432 		while ( SDL_PollEvent(&bitbucket) > 0 )
   433 			;
   434 		return(current_state);
   435 	}
   436 
   437 	/* Just set the state for one event type */
   438 	current_state = SDL_ProcessEvents[type];
   439 	switch (state) {
   440 		case SDL_IGNORE:
   441 		case SDL_ENABLE:
   442 			/* Set state and discard pending events */
   443 			SDL_ProcessEvents[type] = state;
   444 			if ( state == SDL_ENABLE ) {
   445 				SDL_eventstate |= (0x00000001 << (type));
   446 			} else {
   447 				SDL_eventstate &= ~(0x00000001 << (type));
   448 			}
   449 			while ( SDL_PollEvent(&bitbucket) > 0 )
   450 				;
   451 			break;
   452 		default:
   453 			/* Querying state? */
   454 			break;
   455 	}
   456 	return(current_state);
   457 }
   458 
   459 /* This is a generic event handler.
   460  */
   461 int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message)
   462 {
   463 	int posted;
   464 
   465 	posted = 0;
   466 	if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   467 		SDL_Event event;
   468 		memset(&event, 0, sizeof(event));
   469 		event.type = SDL_SYSWMEVENT;
   470 		event.syswm.msg = message;
   471 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   472 			posted = 1;
   473 			SDL_PushEvent(&event);
   474 		}
   475 	}
   476 	/* Update internal event state */
   477 	return(posted);
   478 }