src/events/SDL_events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Feb 2002 23:40:13 +0000
changeset 276 8af85680ca0a
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
Updated the documentation for the SDL_PushEvent() call.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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 	return(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS));
   376 }
   377 
   378 int SDL_WaitEvent (SDL_Event *event)
   379 {
   380 	while ( 1 ) {
   381 		SDL_PumpEvents();
   382 		switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
   383 		    case -1: return -1; 
   384 		    case 1: return 1;
   385 		    case 0: SDL_Delay(10);
   386 		}
   387 	}
   388 }
   389 
   390 int SDL_PushEvent(SDL_Event *event)
   391 {
   392 	return(SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0));
   393 }
   394 
   395 void SDL_SetEventFilter (SDL_EventFilter filter)
   396 {
   397 	SDL_Event bitbucket;
   398 
   399 	/* Set filter and discard pending events */
   400 	SDL_EventOK = filter;
   401 	while ( SDL_PollEvent(&bitbucket) > 0 )
   402 		;
   403 }
   404 
   405 SDL_EventFilter SDL_GetEventFilter(void)
   406 {
   407 	return(SDL_EventOK);
   408 }
   409 
   410 Uint8 SDL_EventState (Uint8 type, int state)
   411 {
   412 	SDL_Event bitbucket;
   413 	Uint8 current_state;
   414 
   415 	/* If SDL_ALLEVENTS was specified... */
   416 	if ( type == 0xFF ) {
   417 		current_state = SDL_IGNORE;
   418 		for ( type=0; type<SDL_NUMEVENTS; ++type ) {
   419 			if ( SDL_ProcessEvents[type] != SDL_IGNORE ) {
   420 				current_state = SDL_ENABLE;
   421 			}
   422 			SDL_ProcessEvents[type] = state;
   423 			if ( state == SDL_ENABLE ) {
   424 				SDL_eventstate |= (0x00000001 << (type));
   425 			} else {
   426 				SDL_eventstate &= ~(0x00000001 << (type));
   427 			}
   428 		}
   429 		while ( SDL_PollEvent(&bitbucket) > 0 )
   430 			;
   431 		return(current_state);
   432 	}
   433 
   434 	/* Just set the state for one event type */
   435 	current_state = SDL_ProcessEvents[type];
   436 	switch (state) {
   437 		case SDL_IGNORE:
   438 		case SDL_ENABLE:
   439 			/* Set state and discard pending events */
   440 			SDL_ProcessEvents[type] = state;
   441 			if ( state == SDL_ENABLE ) {
   442 				SDL_eventstate |= (0x00000001 << (type));
   443 			} else {
   444 				SDL_eventstate &= ~(0x00000001 << (type));
   445 			}
   446 			while ( SDL_PollEvent(&bitbucket) > 0 )
   447 				;
   448 			break;
   449 		default:
   450 			/* Querying state? */
   451 			break;
   452 	}
   453 	return(current_state);
   454 }
   455 
   456 /* This is a generic event handler.
   457  */
   458 int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message)
   459 {
   460 	int posted;
   461 
   462 	posted = 0;
   463 	if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   464 		SDL_Event event;
   465 		memset(&event, 0, sizeof(event));
   466 		event.type = SDL_SYSWMEVENT;
   467 		event.syswm.msg = message;
   468 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   469 			posted = 1;
   470 			SDL_PushEvent(&event);
   471 		}
   472 	}
   473 	/* Update internal event state */
   474 	return(posted);
   475 }