src/events/SDL_events.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 1978 542c78b6fb12
     1.1 --- a/src/events/SDL_events.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/events/SDL_events.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -34,46 +34,51 @@
     1.4  
     1.5  /* Public data -- the event filter */
     1.6  SDL_EventFilter SDL_EventOK = NULL;
     1.7 +void *SDL_EventOKParam;
     1.8  Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
     1.9  static Uint32 SDL_eventstate = 0;
    1.10  
    1.11  /* Private data -- event queue */
    1.12  #define MAXEVENTS	128
    1.13 -static struct {
    1.14 -	SDL_mutex *lock;
    1.15 -	int active;
    1.16 -	int head;
    1.17 -	int tail;
    1.18 -	SDL_Event event[MAXEVENTS];
    1.19 -	int wmmsg_next;
    1.20 -	struct SDL_SysWMmsg wmmsg[MAXEVENTS];
    1.21 +static struct
    1.22 +{
    1.23 +    SDL_mutex *lock;
    1.24 +    int active;
    1.25 +    int head;
    1.26 +    int tail;
    1.27 +    SDL_Event event[MAXEVENTS];
    1.28 +    int wmmsg_next;
    1.29 +    struct SDL_SysWMmsg wmmsg[MAXEVENTS];
    1.30  } SDL_EventQ;
    1.31  
    1.32  /* Private data -- event locking structure */
    1.33 -static struct {
    1.34 -	SDL_mutex *lock;
    1.35 -	int safe;
    1.36 +static struct
    1.37 +{
    1.38 +    SDL_mutex *lock;
    1.39 +    int safe;
    1.40  } SDL_EventLock;
    1.41  
    1.42  /* Thread functions */
    1.43 -static SDL_Thread *SDL_EventThread = NULL;	/* Thread handle */
    1.44 -static Uint32 event_thread;			/* The event thread id */
    1.45 +static SDL_Thread *SDL_EventThread = NULL;      /* Thread handle */
    1.46 +static Uint32 event_thread;     /* The event thread id */
    1.47  
    1.48 -void SDL_Lock_EventThread(void)
    1.49 +void
    1.50 +SDL_Lock_EventThread(void)
    1.51  {
    1.52 -	if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
    1.53 -		/* Grab lock and spin until we're sure event thread stopped */
    1.54 -		SDL_mutexP(SDL_EventLock.lock);
    1.55 -		while ( ! SDL_EventLock.safe ) {
    1.56 -			SDL_Delay(1);
    1.57 -		}
    1.58 -	}
    1.59 +    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
    1.60 +        /* Grab lock and spin until we're sure event thread stopped */
    1.61 +        SDL_mutexP(SDL_EventLock.lock);
    1.62 +        while (!SDL_EventLock.safe) {
    1.63 +            SDL_Delay(1);
    1.64 +        }
    1.65 +    }
    1.66  }
    1.67 -void SDL_Unlock_EventThread(void)
    1.68 +void
    1.69 +SDL_Unlock_EventThread(void)
    1.70  {
    1.71 -	if ( SDL_EventThread && (SDL_ThreadID() != event_thread) ) {
    1.72 -		SDL_mutexV(SDL_EventLock.lock);
    1.73 -	}
    1.74 +    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
    1.75 +        SDL_mutexV(SDL_EventLock.lock);
    1.76 +    }
    1.77  }
    1.78  
    1.79  #ifdef __OS2__
    1.80 @@ -87,414 +92,461 @@
    1.81  #include <time.h>
    1.82  #endif
    1.83  
    1.84 -static int SDLCALL SDL_GobbleEvents(void *unused)
    1.85 +static int SDLCALL
    1.86 +SDL_GobbleEvents(void *unused)
    1.87  {
    1.88 -	event_thread = SDL_ThreadID();
    1.89 +    event_thread = SDL_ThreadID();
    1.90  
    1.91  #ifdef __OS2__
    1.92  #ifdef USE_DOSSETPRIORITY
    1.93 -	/* Increase thread priority, so it will process events in time for sure! */
    1.94 -	DosSetPriority(PRTYS_THREAD, PRTYC_REGULAR, +16, 0);
    1.95 +    /* Increase thread priority, so it will process events in time for sure! */
    1.96 +    DosSetPriority(PRTYS_THREAD, PRTYC_REGULAR, +16, 0);
    1.97  #endif
    1.98  #endif
    1.99  
   1.100 -	while ( SDL_EventQ.active ) {
   1.101 -		SDL_VideoDevice *video = current_video;
   1.102 -		SDL_VideoDevice *this  = current_video;
   1.103 +    while (SDL_EventQ.active) {
   1.104 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.105  
   1.106 -		/* Get events from the video subsystem */
   1.107 -		if ( video ) {
   1.108 -			video->PumpEvents(this);
   1.109 -		}
   1.110 +        /* Get events from the video subsystem */
   1.111 +        if (_this) {
   1.112 +            _this->PumpEvents(_this);
   1.113 +        }
   1.114  
   1.115 -		/* Queue pending key-repeat events */
   1.116 -		SDL_CheckKeyRepeat();
   1.117 +        /* Queue pending key-repeat events */
   1.118 +        SDL_CheckKeyRepeat();
   1.119  
   1.120  #if !SDL_JOYSTICK_DISABLED
   1.121 -		/* Check for joystick state change */
   1.122 -		if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
   1.123 -			SDL_JoystickUpdate();
   1.124 -		}
   1.125 +        /* Check for joystick state change */
   1.126 +        if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
   1.127 +            SDL_JoystickUpdate();
   1.128 +        }
   1.129  #endif
   1.130  
   1.131 -		/* Give up the CPU for the rest of our timeslice */
   1.132 -		SDL_EventLock.safe = 1;
   1.133 -		if ( SDL_timer_running ) {
   1.134 -			SDL_ThreadedTimerCheck();
   1.135 -		}
   1.136 -		SDL_Delay(1);
   1.137 +        /* Give up the CPU for the rest of our timeslice */
   1.138 +        SDL_EventLock.safe = 1;
   1.139 +        if (SDL_timer_running) {
   1.140 +            SDL_ThreadedTimerCheck();
   1.141 +        }
   1.142 +        SDL_Delay(1);
   1.143  
   1.144 -		/* Check for event locking.
   1.145 -		   On the P of the lock mutex, if the lock is held, this thread
   1.146 -		   will wait until the lock is released before continuing.  The
   1.147 -		   safe flag will be set, meaning that the other thread can go
   1.148 -		   about it's business.  The safe flag is reset before the V,
   1.149 -		   so as soon as the mutex is free, other threads can see that
   1.150 -		   it's not safe to interfere with the event thread.
   1.151 -		 */
   1.152 -		SDL_mutexP(SDL_EventLock.lock);
   1.153 -		SDL_EventLock.safe = 0;
   1.154 -		SDL_mutexV(SDL_EventLock.lock);
   1.155 -	}
   1.156 -	SDL_SetTimerThreaded(0);
   1.157 -	event_thread = 0;
   1.158 -	return(0);
   1.159 +        /* Check for event locking.
   1.160 +           On the P of the lock mutex, if the lock is held, this thread
   1.161 +           will wait until the lock is released before continuing.  The
   1.162 +           safe flag will be set, meaning that the other thread can go
   1.163 +           about it's business.  The safe flag is reset before the V,
   1.164 +           so as soon as the mutex is free, other threads can see that
   1.165 +           it's not safe to interfere with the event thread.
   1.166 +         */
   1.167 +        SDL_mutexP(SDL_EventLock.lock);
   1.168 +        SDL_EventLock.safe = 0;
   1.169 +        SDL_mutexV(SDL_EventLock.lock);
   1.170 +    }
   1.171 +    SDL_SetTimerThreaded(0);
   1.172 +    event_thread = 0;
   1.173 +    return (0);
   1.174  }
   1.175  
   1.176 -static int SDL_StartEventThread(Uint32 flags)
   1.177 +static int
   1.178 +SDL_StartEventThread(Uint32 flags)
   1.179  {
   1.180 -	/* Reset everything to zero */
   1.181 -	SDL_EventThread = NULL;
   1.182 -	SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
   1.183 +    /* Reset everything to zero */
   1.184 +    SDL_EventThread = NULL;
   1.185 +    SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
   1.186  
   1.187 -	/* Create the lock and set ourselves active */
   1.188 +    /* Create the lock and set ourselves active */
   1.189  #if !SDL_THREADS_DISABLED
   1.190 -	SDL_EventQ.lock = SDL_CreateMutex();
   1.191 -	if ( SDL_EventQ.lock == NULL ) {
   1.192 -#ifdef __MACOS__ /* MacOS classic you can't multithread, so no lock needed */
   1.193 -		;
   1.194 +    SDL_EventQ.lock = SDL_CreateMutex();
   1.195 +    if (SDL_EventQ.lock == NULL) {
   1.196 +#ifdef __MACOS__                /* MacOS classic you can't multithread, so no lock needed */
   1.197 +        ;
   1.198  #else
   1.199 -		return(-1);
   1.200 +        return (-1);
   1.201  #endif
   1.202 -	}
   1.203 +    }
   1.204  #endif /* !SDL_THREADS_DISABLED */
   1.205 -	SDL_EventQ.active = 1;
   1.206 +    SDL_EventQ.active = 1;
   1.207  
   1.208 -	if ( (flags&SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
   1.209 -		SDL_EventLock.lock = SDL_CreateMutex();
   1.210 -		if ( SDL_EventLock.lock == NULL ) {
   1.211 -			return(-1);
   1.212 -		}
   1.213 -		SDL_EventLock.safe = 0;
   1.214 +    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   1.215 +        SDL_EventLock.lock = SDL_CreateMutex();
   1.216 +        if (SDL_EventLock.lock == NULL) {
   1.217 +            return (-1);
   1.218 +        }
   1.219 +        SDL_EventLock.safe = 0;
   1.220  
   1.221 -		/* The event thread will handle timers too */
   1.222 -		SDL_SetTimerThreaded(2);
   1.223 +        /* The event thread will handle timers too */
   1.224 +        SDL_SetTimerThreaded(2);
   1.225  #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
   1.226  #undef SDL_CreateThread
   1.227 -		SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
   1.228 +        SDL_EventThread =
   1.229 +            SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
   1.230  #else
   1.231 -		SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
   1.232 +        SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
   1.233  #endif
   1.234 -		if ( SDL_EventThread == NULL ) {
   1.235 -			return(-1);
   1.236 -		}
   1.237 -	} else {
   1.238 -		event_thread = 0;
   1.239 -	}
   1.240 -	return(0);
   1.241 +        if (SDL_EventThread == NULL) {
   1.242 +            return (-1);
   1.243 +        }
   1.244 +    } else {
   1.245 +        event_thread = 0;
   1.246 +    }
   1.247 +    return (0);
   1.248  }
   1.249  
   1.250 -static void SDL_StopEventThread(void)
   1.251 +static void
   1.252 +SDL_StopEventThread(void)
   1.253  {
   1.254 -	SDL_EventQ.active = 0;
   1.255 -	if ( SDL_EventThread ) {
   1.256 -		SDL_WaitThread(SDL_EventThread, NULL);
   1.257 -		SDL_EventThread = NULL;
   1.258 -		SDL_DestroyMutex(SDL_EventLock.lock);
   1.259 -	}
   1.260 +    SDL_EventQ.active = 0;
   1.261 +    if (SDL_EventThread) {
   1.262 +        SDL_WaitThread(SDL_EventThread, NULL);
   1.263 +        SDL_EventThread = NULL;
   1.264 +        SDL_DestroyMutex(SDL_EventLock.lock);
   1.265 +    }
   1.266  #ifndef IPOD
   1.267 -	SDL_DestroyMutex(SDL_EventQ.lock);
   1.268 +    SDL_DestroyMutex(SDL_EventQ.lock);
   1.269  #endif
   1.270  }
   1.271  
   1.272 -Uint32 SDL_EventThreadID(void)
   1.273 +Uint32
   1.274 +SDL_EventThreadID(void)
   1.275  {
   1.276 -	return(event_thread);
   1.277 +    return (event_thread);
   1.278  }
   1.279  
   1.280  /* Public functions */
   1.281  
   1.282 -void SDL_StopEventLoop(void)
   1.283 +void
   1.284 +SDL_StopEventLoop(void)
   1.285  {
   1.286 -	/* Halt the event thread, if running */
   1.287 -	SDL_StopEventThread();
   1.288 +    /* Halt the event thread, if running */
   1.289 +    SDL_StopEventThread();
   1.290  
   1.291 -	/* Shutdown event handlers */
   1.292 -	SDL_AppActiveQuit();
   1.293 -	SDL_KeyboardQuit();
   1.294 -	SDL_MouseQuit();
   1.295 -	SDL_QuitQuit();
   1.296 +    /* Shutdown event handlers */
   1.297 +    SDL_KeyboardQuit();
   1.298 +    SDL_MouseQuit();
   1.299 +    SDL_QuitQuit();
   1.300  
   1.301 -	/* Clean out EventQ */
   1.302 -	SDL_EventQ.head = 0;
   1.303 -	SDL_EventQ.tail = 0;
   1.304 -	SDL_EventQ.wmmsg_next = 0;
   1.305 +    /* Clean out EventQ */
   1.306 +    SDL_EventQ.head = 0;
   1.307 +    SDL_EventQ.tail = 0;
   1.308 +    SDL_EventQ.wmmsg_next = 0;
   1.309  }
   1.310  
   1.311  /* This function (and associated calls) may be called more than once */
   1.312 -int SDL_StartEventLoop(Uint32 flags)
   1.313 +int
   1.314 +SDL_StartEventLoop(Uint32 flags)
   1.315  {
   1.316 -	int retcode;
   1.317 +    int retcode;
   1.318  
   1.319 -	/* Clean out the event queue */
   1.320 -	SDL_EventThread = NULL;
   1.321 -	SDL_EventQ.lock = NULL;
   1.322 -	SDL_StopEventLoop();
   1.323 +    /* Clean out the event queue */
   1.324 +    SDL_EventThread = NULL;
   1.325 +    SDL_EventQ.lock = NULL;
   1.326 +    SDL_StopEventLoop();
   1.327  
   1.328 -	/* No filter to start with, process most event types */
   1.329 -	SDL_EventOK = NULL;
   1.330 -	SDL_memset(SDL_ProcessEvents,SDL_ENABLE,sizeof(SDL_ProcessEvents));
   1.331 -	SDL_eventstate = ~0;
   1.332 -	/* It's not save to call SDL_EventState() yet */
   1.333 -	SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
   1.334 -	SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
   1.335 +    /* No filter to start with, process most event types */
   1.336 +    SDL_EventOK = NULL;
   1.337 +    SDL_memset(SDL_ProcessEvents, SDL_ENABLE, sizeof(SDL_ProcessEvents));
   1.338 +    SDL_eventstate = ~0;
   1.339 +    /* It's not save to call SDL_EventState() yet */
   1.340 +    SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
   1.341 +    SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
   1.342  
   1.343 -	/* Initialize event handlers */
   1.344 -	retcode = 0;
   1.345 -	retcode += SDL_AppActiveInit();
   1.346 -	retcode += SDL_KeyboardInit();
   1.347 -	retcode += SDL_MouseInit();
   1.348 -	retcode += SDL_QuitInit();
   1.349 -	if ( retcode < 0 ) {
   1.350 -		/* We don't expect them to fail, but... */
   1.351 -		return(-1);
   1.352 -	}
   1.353 +    /* Initialize event handlers */
   1.354 +    retcode = 0;
   1.355 +    retcode += SDL_KeyboardInit();
   1.356 +    retcode += SDL_MouseInit();
   1.357 +    retcode += SDL_QuitInit();
   1.358 +    if (retcode < 0) {
   1.359 +        /* We don't expect them to fail, but... */
   1.360 +        return (-1);
   1.361 +    }
   1.362  
   1.363 -	/* Create the lock and event thread */
   1.364 -	if ( SDL_StartEventThread(flags) < 0 ) {
   1.365 -		SDL_StopEventLoop();
   1.366 -		return(-1);
   1.367 -	}
   1.368 -	return(0);
   1.369 +    /* Create the lock and event thread */
   1.370 +    if (SDL_StartEventThread(flags) < 0) {
   1.371 +        SDL_StopEventLoop();
   1.372 +        return (-1);
   1.373 +    }
   1.374 +    return (0);
   1.375  }
   1.376  
   1.377  
   1.378  /* Add an event to the event queue -- called with the queue locked */
   1.379 -static int SDL_AddEvent(SDL_Event *event)
   1.380 +static int
   1.381 +SDL_AddEvent(SDL_Event * event)
   1.382  {
   1.383 -	int tail, added;
   1.384 +    int tail, added;
   1.385  
   1.386 -	tail = (SDL_EventQ.tail+1)%MAXEVENTS;
   1.387 -	if ( tail == SDL_EventQ.head ) {
   1.388 -		/* Overflow, drop event */
   1.389 -		added = 0;
   1.390 -	} else {
   1.391 -		SDL_EventQ.event[SDL_EventQ.tail] = *event;
   1.392 -		if (event->type == SDL_SYSWMEVENT) {
   1.393 -			/* Note that it's possible to lose an event */
   1.394 -			int next = SDL_EventQ.wmmsg_next;
   1.395 -			SDL_EventQ.wmmsg[next] = *event->syswm.msg;
   1.396 -		        SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
   1.397 -						&SDL_EventQ.wmmsg[next];
   1.398 -			SDL_EventQ.wmmsg_next = (next+1)%MAXEVENTS;
   1.399 -		}
   1.400 -		SDL_EventQ.tail = tail;
   1.401 -		added = 1;
   1.402 -	}
   1.403 -	return(added);
   1.404 +    tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
   1.405 +    if (tail == SDL_EventQ.head) {
   1.406 +        /* Overflow, drop event */
   1.407 +        added = 0;
   1.408 +    } else {
   1.409 +        SDL_EventQ.event[SDL_EventQ.tail] = *event;
   1.410 +        if (event->type == SDL_SYSWMEVENT) {
   1.411 +            /* Note that it's possible to lose an event */
   1.412 +            int next = SDL_EventQ.wmmsg_next;
   1.413 +            SDL_EventQ.wmmsg[next] = *event->syswm.msg;
   1.414 +            SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
   1.415 +                &SDL_EventQ.wmmsg[next];
   1.416 +            SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
   1.417 +        }
   1.418 +        SDL_EventQ.tail = tail;
   1.419 +        added = 1;
   1.420 +    }
   1.421 +    return (added);
   1.422  }
   1.423  
   1.424  /* Cut an event, and return the next valid spot, or the tail */
   1.425  /*                           -- called with the queue locked */
   1.426 -static int SDL_CutEvent(int spot)
   1.427 +static int
   1.428 +SDL_CutEvent(int spot)
   1.429  {
   1.430 -	if ( spot == SDL_EventQ.head ) {
   1.431 -		SDL_EventQ.head = (SDL_EventQ.head+1)%MAXEVENTS;
   1.432 -		return(SDL_EventQ.head);
   1.433 -	} else
   1.434 -	if ( (spot+1)%MAXEVENTS == SDL_EventQ.tail ) {
   1.435 -		SDL_EventQ.tail = spot;
   1.436 -		return(SDL_EventQ.tail);
   1.437 -	} else
   1.438 -	/* We cut the middle -- shift everything over */
   1.439 -	{
   1.440 -		int here, next;
   1.441 +    if (spot == SDL_EventQ.head) {
   1.442 +        SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
   1.443 +        return (SDL_EventQ.head);
   1.444 +    } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
   1.445 +        SDL_EventQ.tail = spot;
   1.446 +        return (SDL_EventQ.tail);
   1.447 +    } else
   1.448 +        /* We cut the middle -- shift everything over */
   1.449 +    {
   1.450 +        int here, next;
   1.451  
   1.452 -		/* This can probably be optimized with SDL_memcpy() -- careful! */
   1.453 -		if ( --SDL_EventQ.tail < 0 ) {
   1.454 -			SDL_EventQ.tail = MAXEVENTS-1;
   1.455 -		}
   1.456 -		for ( here=spot; here != SDL_EventQ.tail; here = next ) {
   1.457 -			next = (here+1)%MAXEVENTS;
   1.458 -			SDL_EventQ.event[here] = SDL_EventQ.event[next];
   1.459 -		}
   1.460 -		return(spot);
   1.461 -	}
   1.462 -	/* NOTREACHED */
   1.463 +        /* This can probably be optimized with SDL_memcpy() -- careful! */
   1.464 +        if (--SDL_EventQ.tail < 0) {
   1.465 +            SDL_EventQ.tail = MAXEVENTS - 1;
   1.466 +        }
   1.467 +        for (here = spot; here != SDL_EventQ.tail; here = next) {
   1.468 +            next = (here + 1) % MAXEVENTS;
   1.469 +            SDL_EventQ.event[here] = SDL_EventQ.event[next];
   1.470 +        }
   1.471 +        return (spot);
   1.472 +    }
   1.473 +    /* NOTREACHED */
   1.474  }
   1.475  
   1.476  /* Lock the event queue, take a peep at it, and unlock it */
   1.477 -int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
   1.478 -								Uint32 mask)
   1.479 +int
   1.480 +SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
   1.481 +               Uint32 mask)
   1.482  {
   1.483 -	int i, used;
   1.484 +    int i, used;
   1.485  
   1.486 -	/* Don't look after we've quit */
   1.487 -	if ( ! SDL_EventQ.active ) {
   1.488 -		return(-1);
   1.489 -	}
   1.490 -	/* Lock the event queue */
   1.491 -	used = 0;
   1.492 -	if ( SDL_mutexP(SDL_EventQ.lock) == 0 ) {
   1.493 -		if ( action == SDL_ADDEVENT ) {
   1.494 -			for ( i=0; i<numevents; ++i ) {
   1.495 -				used += SDL_AddEvent(&events[i]);
   1.496 -			}
   1.497 -		} else {
   1.498 -			SDL_Event tmpevent;
   1.499 -			int spot;
   1.500 +    /* Don't look after we've quit */
   1.501 +    if (!SDL_EventQ.active) {
   1.502 +        return (-1);
   1.503 +    }
   1.504 +    /* Lock the event queue */
   1.505 +    used = 0;
   1.506 +    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
   1.507 +        if (action == SDL_ADDEVENT) {
   1.508 +            for (i = 0; i < numevents; ++i) {
   1.509 +                used += SDL_AddEvent(&events[i]);
   1.510 +            }
   1.511 +        } else {
   1.512 +            SDL_Event tmpevent;
   1.513 +            int spot;
   1.514  
   1.515 -			/* If 'events' is NULL, just see if they exist */
   1.516 -			if ( events == NULL ) {
   1.517 -				action = SDL_PEEKEVENT;
   1.518 -				numevents = 1;
   1.519 -				events = &tmpevent;
   1.520 -			}
   1.521 -			spot = SDL_EventQ.head;
   1.522 -			while ((used < numevents)&&(spot != SDL_EventQ.tail)) {
   1.523 -				if ( mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type) ) {
   1.524 -					events[used++] = SDL_EventQ.event[spot];
   1.525 -					if ( action == SDL_GETEVENT ) {
   1.526 -						spot = SDL_CutEvent(spot);
   1.527 -					} else {
   1.528 -						spot = (spot+1)%MAXEVENTS;
   1.529 -					}
   1.530 -				} else {
   1.531 -					spot = (spot+1)%MAXEVENTS;
   1.532 -				}
   1.533 -			}
   1.534 -		}
   1.535 -		SDL_mutexV(SDL_EventQ.lock);
   1.536 -	} else {
   1.537 -		SDL_SetError("Couldn't lock event queue");
   1.538 -		used = -1;
   1.539 -	}
   1.540 -	return(used);
   1.541 +            /* If 'events' is NULL, just see if they exist */
   1.542 +            if (events == NULL) {
   1.543 +                action = SDL_PEEKEVENT;
   1.544 +                numevents = 1;
   1.545 +                events = &tmpevent;
   1.546 +            }
   1.547 +            spot = SDL_EventQ.head;
   1.548 +            while ((used < numevents) && (spot != SDL_EventQ.tail)) {
   1.549 +                if (mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type)) {
   1.550 +                    events[used++] = SDL_EventQ.event[spot];
   1.551 +                    if (action == SDL_GETEVENT) {
   1.552 +                        spot = SDL_CutEvent(spot);
   1.553 +                    } else {
   1.554 +                        spot = (spot + 1) % MAXEVENTS;
   1.555 +                    }
   1.556 +                } else {
   1.557 +                    spot = (spot + 1) % MAXEVENTS;
   1.558 +                }
   1.559 +            }
   1.560 +        }
   1.561 +        SDL_mutexV(SDL_EventQ.lock);
   1.562 +    } else {
   1.563 +        SDL_SetError("Couldn't lock event queue");
   1.564 +        used = -1;
   1.565 +    }
   1.566 +    return (used);
   1.567 +}
   1.568 +
   1.569 +SDL_bool
   1.570 +SDL_HasEvent(Uint32 mask)
   1.571 +{
   1.572 +    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask) > 0);
   1.573  }
   1.574  
   1.575  /* Run the system dependent event loops */
   1.576 -void SDL_PumpEvents(void)
   1.577 +void
   1.578 +SDL_PumpEvents(void)
   1.579  {
   1.580 -	if ( !SDL_EventThread ) {
   1.581 -		SDL_VideoDevice *video = current_video;
   1.582 -		SDL_VideoDevice *this  = current_video;
   1.583 +    if (!SDL_EventThread) {
   1.584 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.585  
   1.586 -		/* Get events from the video subsystem */
   1.587 -		if ( video ) {
   1.588 -			video->PumpEvents(this);
   1.589 -		}
   1.590 +        /* Get events from the video subsystem */
   1.591 +        if (_this) {
   1.592 +            _this->PumpEvents(_this);
   1.593 +        }
   1.594  
   1.595 -		/* Queue pending key-repeat events */
   1.596 -		SDL_CheckKeyRepeat();
   1.597 +        /* Queue pending key-repeat events */
   1.598 +        SDL_CheckKeyRepeat();
   1.599  
   1.600  #if !SDL_JOYSTICK_DISABLED
   1.601 -		/* Check for joystick state change */
   1.602 -		if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
   1.603 -			SDL_JoystickUpdate();
   1.604 -		}
   1.605 +        /* Check for joystick state change */
   1.606 +        if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
   1.607 +            SDL_JoystickUpdate();
   1.608 +        }
   1.609  #endif
   1.610 -	}
   1.611 +    }
   1.612  }
   1.613  
   1.614  /* Public functions */
   1.615  
   1.616 -int SDL_PollEvent (SDL_Event *event)
   1.617 +int
   1.618 +SDL_PollEvent(SDL_Event * event)
   1.619  {
   1.620 -	SDL_PumpEvents();
   1.621 +    SDL_PumpEvents();
   1.622  
   1.623 -	/* We can't return -1, just return 0 (no event) on error */
   1.624 -	if ( SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0 )
   1.625 -		return 0;
   1.626 -	return 1;
   1.627 +    /* We can't return -1, just return 0 (no event) on error */
   1.628 +    if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0)
   1.629 +        return 0;
   1.630 +    return 1;
   1.631  }
   1.632  
   1.633 -int SDL_WaitEvent (SDL_Event *event)
   1.634 +int
   1.635 +SDL_WaitEvent(SDL_Event * event)
   1.636  {
   1.637 -	while ( 1 ) {
   1.638 -		SDL_PumpEvents();
   1.639 -		switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
   1.640 -		    case -1: return 0;
   1.641 -		    case 1: return 1;
   1.642 -		    case 0: SDL_Delay(10);
   1.643 -		}
   1.644 -	}
   1.645 +    while (1) {
   1.646 +        SDL_PumpEvents();
   1.647 +        switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
   1.648 +        case -1:
   1.649 +            return 0;
   1.650 +        case 1:
   1.651 +            return 1;
   1.652 +        case 0:
   1.653 +            SDL_Delay(10);
   1.654 +        }
   1.655 +    }
   1.656  }
   1.657  
   1.658 -int SDL_PushEvent(SDL_Event *event)
   1.659 +int
   1.660 +SDL_PushEvent(SDL_Event * event)
   1.661  {
   1.662 -	if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
   1.663 -		return -1;
   1.664 -	return 0;
   1.665 +    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
   1.666 +        return 0;
   1.667 +    }
   1.668 +    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0) {
   1.669 +        return -1;
   1.670 +    }
   1.671 +    return 1;
   1.672  }
   1.673  
   1.674 -void SDL_SetEventFilter (SDL_EventFilter filter)
   1.675 +void
   1.676 +SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
   1.677  {
   1.678 -	SDL_Event bitbucket;
   1.679 +    SDL_Event bitbucket;
   1.680  
   1.681 -	/* Set filter and discard pending events */
   1.682 -	SDL_EventOK = filter;
   1.683 -	while ( SDL_PollEvent(&bitbucket) > 0 )
   1.684 -		;
   1.685 +    /* Set filter and discard pending events */
   1.686 +    SDL_EventOK = filter;
   1.687 +    SDL_EventOKParam = userdata;
   1.688 +    while (SDL_PollEvent(&bitbucket) > 0);
   1.689  }
   1.690  
   1.691 -SDL_EventFilter SDL_GetEventFilter(void)
   1.692 +SDL_bool
   1.693 +SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
   1.694  {
   1.695 -	return(SDL_EventOK);
   1.696 +    if (filter) {
   1.697 +        *filter = SDL_EventOK;
   1.698 +    }
   1.699 +    if (userdata) {
   1.700 +        *userdata = SDL_EventOKParam;
   1.701 +    }
   1.702 +    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
   1.703  }
   1.704  
   1.705 -Uint8 SDL_EventState (Uint8 type, int state)
   1.706 +void
   1.707 +SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
   1.708  {
   1.709 -	SDL_Event bitbucket;
   1.710 -	Uint8 current_state;
   1.711 +    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
   1.712 +        int spot;
   1.713  
   1.714 -	/* If SDL_ALLEVENTS was specified... */
   1.715 -	if ( type == 0xFF ) {
   1.716 -		current_state = SDL_IGNORE;
   1.717 -		for ( type=0; type<SDL_NUMEVENTS; ++type ) {
   1.718 -			if ( SDL_ProcessEvents[type] != SDL_IGNORE ) {
   1.719 -				current_state = SDL_ENABLE;
   1.720 -			}
   1.721 -			SDL_ProcessEvents[type] = state;
   1.722 -			if ( state == SDL_ENABLE ) {
   1.723 -				SDL_eventstate |= (0x00000001 << (type));
   1.724 -			} else {
   1.725 -				SDL_eventstate &= ~(0x00000001 << (type));
   1.726 -			}
   1.727 -		}
   1.728 -		while ( SDL_PollEvent(&bitbucket) > 0 )
   1.729 -			;
   1.730 -		return(current_state);
   1.731 -	}
   1.732 +        spot = SDL_EventQ.head;
   1.733 +        while (spot != SDL_EventQ.tail) {
   1.734 +            if (filter(userdata, &SDL_EventQ.event[spot])) {
   1.735 +                spot = (spot + 1) % MAXEVENTS;
   1.736 +            } else {
   1.737 +                spot = SDL_CutEvent(spot);
   1.738 +            }
   1.739 +        }
   1.740 +    }
   1.741 +    SDL_mutexV(SDL_EventQ.lock);
   1.742 +}
   1.743  
   1.744 -	/* Just set the state for one event type */
   1.745 -	current_state = SDL_ProcessEvents[type];
   1.746 -	switch (state) {
   1.747 -		case SDL_IGNORE:
   1.748 -		case SDL_ENABLE:
   1.749 -			/* Set state and discard pending events */
   1.750 -			SDL_ProcessEvents[type] = state;
   1.751 -			if ( state == SDL_ENABLE ) {
   1.752 -				SDL_eventstate |= (0x00000001 << (type));
   1.753 -			} else {
   1.754 -				SDL_eventstate &= ~(0x00000001 << (type));
   1.755 -			}
   1.756 -			while ( SDL_PollEvent(&bitbucket) > 0 )
   1.757 -				;
   1.758 -			break;
   1.759 -		default:
   1.760 -			/* Querying state? */
   1.761 -			break;
   1.762 -	}
   1.763 -	return(current_state);
   1.764 +Uint8
   1.765 +SDL_EventState(Uint8 type, int state)
   1.766 +{
   1.767 +    SDL_Event bitbucket;
   1.768 +    Uint8 current_state;
   1.769 +
   1.770 +    /* If SDL_ALLEVENTS was specified... */
   1.771 +    if (type == 0xFF) {
   1.772 +        current_state = SDL_IGNORE;
   1.773 +        for (type = 0; type < SDL_NUMEVENTS; ++type) {
   1.774 +            if (SDL_ProcessEvents[type] != SDL_IGNORE) {
   1.775 +                current_state = SDL_ENABLE;
   1.776 +            }
   1.777 +            SDL_ProcessEvents[type] = state;
   1.778 +            if (state == SDL_ENABLE) {
   1.779 +                SDL_eventstate |= (0x00000001 << (type));
   1.780 +            } else {
   1.781 +                SDL_eventstate &= ~(0x00000001 << (type));
   1.782 +            }
   1.783 +        }
   1.784 +        while (SDL_PollEvent(&bitbucket) > 0);
   1.785 +        return (current_state);
   1.786 +    }
   1.787 +
   1.788 +    /* Just set the state for one event type */
   1.789 +    current_state = SDL_ProcessEvents[type];
   1.790 +    switch (state) {
   1.791 +    case SDL_IGNORE:
   1.792 +    case SDL_ENABLE:
   1.793 +        /* Set state and discard pending events */
   1.794 +        SDL_ProcessEvents[type] = state;
   1.795 +        if (state == SDL_ENABLE) {
   1.796 +            SDL_eventstate |= (0x00000001 << (type));
   1.797 +        } else {
   1.798 +            SDL_eventstate &= ~(0x00000001 << (type));
   1.799 +        }
   1.800 +        while (SDL_PollEvent(&bitbucket) > 0);
   1.801 +        break;
   1.802 +    default:
   1.803 +        /* Querying state? */
   1.804 +        break;
   1.805 +    }
   1.806 +    return (current_state);
   1.807  }
   1.808  
   1.809  /* This is a generic event handler.
   1.810   */
   1.811 -int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message)
   1.812 +int
   1.813 +SDL_SendSysWMEvent(SDL_SysWMmsg * message)
   1.814  {
   1.815 -	int posted;
   1.816 +    int posted;
   1.817  
   1.818 -	posted = 0;
   1.819 -	if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   1.820 -		SDL_Event event;
   1.821 -		SDL_memset(&event, 0, sizeof(event));
   1.822 -		event.type = SDL_SYSWMEVENT;
   1.823 -		event.syswm.msg = message;
   1.824 -		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   1.825 -			posted = 1;
   1.826 -			SDL_PushEvent(&event);
   1.827 -		}
   1.828 -	}
   1.829 -	/* Update internal event state */
   1.830 -	return(posted);
   1.831 +    posted = 0;
   1.832 +    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
   1.833 +        SDL_Event event;
   1.834 +        SDL_memset(&event, 0, sizeof(event));
   1.835 +        event.type = SDL_SYSWMEVENT;
   1.836 +        event.syswm.msg = message;
   1.837 +        posted = (SDL_PushEvent(&event) > 0);
   1.838 +    }
   1.839 +    /* Update internal event state */
   1.840 +    return (posted);
   1.841  }
   1.842 +
   1.843 +/* vi: set ts=4 sw=4 expandtab: */