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 -
   1.324 -	/* No filter to start with, process most event types */
   1.325 -	SDL_EventOK = NULL;
   1.326 -	SDL_memset(SDL_ProcessEvents,SDL_ENABLE,sizeof(SDL_ProcessEvents));
   1.327 -	SDL_eventstate = ~0;
   1.328 -	/* It's not save to call SDL_EventState() yet */
   1.329 -	SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
   1.330 -	SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
   1.331 +    /* Clean out the event queue */
   1.332 +    SDL_EventThread = NULL;
   1.333 +    SDL_EventQ.lock = NULL;
   1.334 +    SDL_StopEventLoop();
   1.335  
   1.336 -	/* Initialize event handlers */
   1.337 -	retcode = 0;
   1.338 -	retcode += SDL_AppActiveInit();
   1.339 -	retcode += SDL_KeyboardInit();
   1.340 -	retcode += SDL_MouseInit();
   1.341 -	retcode += SDL_QuitInit();
   1.342 -	if ( retcode < 0 ) {
   1.343 -		/* We don't expect them to fail, but... */
   1.344 -		return(-1);
   1.345 -	}
   1.346 +    /* No filter to start with, process most event types */
   1.347 +    SDL_EventOK = NULL;
   1.348 +    SDL_memset(SDL_ProcessEvents, SDL_ENABLE, sizeof(SDL_ProcessEvents));
   1.349 +    SDL_eventstate = ~0;
   1.350 +    /* It's not save to call SDL_EventState() yet */
   1.351 +    SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
   1.352 +    SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
   1.353  
   1.354 -	/* Create the lock and event thread */
   1.355 -	if ( SDL_StartEventThread(flags) < 0 ) {
   1.356 -		SDL_StopEventLoop();
   1.357 -		return(-1);
   1.358 -	}
   1.359 -	return(0);
   1.360 +    /* Initialize event handlers */
   1.361 +    retcode = 0;
   1.362 +    retcode += SDL_KeyboardInit();
   1.363 +    retcode += SDL_MouseInit();
   1.364 +    retcode += SDL_QuitInit();
   1.365 +    if (retcode < 0) {
   1.366 +        /* We don't expect them to fail, but... */
   1.367 +        return (-1);
   1.368 +    }
   1.369 +
   1.370 +    /* Create the lock and event thread */
   1.371 +    if (SDL_StartEventThread(flags) < 0) {
   1.372 +        SDL_StopEventLoop();
   1.373 +        return (-1);
   1.374 +    }
   1.375 +    return (0);
   1.376  }
   1.377  
   1.378  
   1.379  /* Add an event to the event queue -- called with the queue locked */
   1.380 -static int SDL_AddEvent(SDL_Event *event)
   1.381 +static int
   1.382 +SDL_AddEvent(SDL_Event * event)
   1.383  {
   1.384 -	int tail, added;
   1.385 +    int tail, added;
   1.386  
   1.387 -	tail = (SDL_EventQ.tail+1)%MAXEVENTS;
   1.388 -	if ( tail == SDL_EventQ.head ) {
   1.389 -		/* Overflow, drop event */
   1.390 -		added = 0;
   1.391 -	} else {
   1.392 -		SDL_EventQ.event[SDL_EventQ.tail] = *event;
   1.393 -		if (event->type == SDL_SYSWMEVENT) {
   1.394 -			/* Note that it's possible to lose an event */
   1.395 -			int next = SDL_EventQ.wmmsg_next;
   1.396 -			SDL_EventQ.wmmsg[next] = *event->syswm.msg;
   1.397 -		        SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
   1.398 -						&SDL_EventQ.wmmsg[next];
   1.399 -			SDL_EventQ.wmmsg_next = (next+1)%MAXEVENTS;
   1.400 -		}
   1.401 -		SDL_EventQ.tail = tail;
   1.402 -		added = 1;
   1.403 -	}
   1.404 -	return(added);
   1.405 +    tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
   1.406 +    if (tail == SDL_EventQ.head) {
   1.407 +        /* Overflow, drop event */
   1.408 +        added = 0;
   1.409 +    } else {
   1.410 +        SDL_EventQ.event[SDL_EventQ.tail] = *event;
   1.411 +        if (event->type == SDL_SYSWMEVENT) {
   1.412 +            /* Note that it's possible to lose an event */
   1.413 +            int next = SDL_EventQ.wmmsg_next;
   1.414 +            SDL_EventQ.wmmsg[next] = *event->syswm.msg;
   1.415 +            SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
   1.416 +                &SDL_EventQ.wmmsg[next];
   1.417 +            SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
   1.418 +        }
   1.419 +        SDL_EventQ.tail = tail;
   1.420 +        added = 1;
   1.421 +    }
   1.422 +    return (added);
   1.423  }
   1.424  
   1.425  /* Cut an event, and return the next valid spot, or the tail */
   1.426  /*                           -- called with the queue locked */
   1.427 -static int SDL_CutEvent(int spot)
   1.428 +static int
   1.429 +SDL_CutEvent(int spot)
   1.430  {
   1.431 -	if ( spot == SDL_EventQ.head ) {
   1.432 -		SDL_EventQ.head = (SDL_EventQ.head+1)%MAXEVENTS;
   1.433 -		return(SDL_EventQ.head);
   1.434 -	} else
   1.435 -	if ( (spot+1)%MAXEVENTS == SDL_EventQ.tail ) {
   1.436 -		SDL_EventQ.tail = spot;
   1.437 -		return(SDL_EventQ.tail);
   1.438 -	} else
   1.439 -	/* We cut the middle -- shift everything over */
   1.440 -	{
   1.441 -		int here, next;
   1.442 +    if (spot == SDL_EventQ.head) {
   1.443 +        SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
   1.444 +        return (SDL_EventQ.head);
   1.445 +    } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
   1.446 +        SDL_EventQ.tail = spot;
   1.447 +        return (SDL_EventQ.tail);
   1.448 +    } else
   1.449 +        /* We cut the middle -- shift everything over */
   1.450 +    {
   1.451 +        int here, next;
   1.452  
   1.453 -		/* This can probably be optimized with SDL_memcpy() -- careful! */
   1.454 -		if ( --SDL_EventQ.tail < 0 ) {
   1.455 -			SDL_EventQ.tail = MAXEVENTS-1;
   1.456 -		}
   1.457 -		for ( here=spot; here != SDL_EventQ.tail; here = next ) {
   1.458 -			next = (here+1)%MAXEVENTS;
   1.459 -			SDL_EventQ.event[here] = SDL_EventQ.event[next];
   1.460 -		}
   1.461 -		return(spot);
   1.462 -	}
   1.463 -	/* NOTREACHED */
   1.464 +        /* This can probably be optimized with SDL_memcpy() -- careful! */
   1.465 +        if (--SDL_EventQ.tail < 0) {
   1.466 +            SDL_EventQ.tail = MAXEVENTS - 1;
   1.467 +        }
   1.468 +        for (here = spot; here != SDL_EventQ.tail; here = next) {
   1.469 +            next = (here + 1) % MAXEVENTS;
   1.470 +            SDL_EventQ.event[here] = SDL_EventQ.event[next];
   1.471 +        }
   1.472 +        return (spot);
   1.473 +    }
   1.474 +    /* NOTREACHED */
   1.475  }
   1.476  
   1.477  /* Lock the event queue, take a peep at it, and unlock it */
   1.478 -int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
   1.479 -								Uint32 mask)
   1.480 +int
   1.481 +SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
   1.482 +               Uint32 mask)
   1.483  {
   1.484 -	int i, used;
   1.485 +    int i, used;
   1.486  
   1.487 -	/* Don't look after we've quit */
   1.488 -	if ( ! SDL_EventQ.active ) {
   1.489 -		return(-1);
   1.490 -	}
   1.491 -	/* Lock the event queue */
   1.492 -	used = 0;
   1.493 -	if ( SDL_mutexP(SDL_EventQ.lock) == 0 ) {
   1.494 -		if ( action == SDL_ADDEVENT ) {
   1.495 -			for ( i=0; i<numevents; ++i ) {
   1.496 -				used += SDL_AddEvent(&events[i]);
   1.497 -			}
   1.498 -		} else {
   1.499 -			SDL_Event tmpevent;
   1.500 -			int spot;
   1.501 +    /* Don't look after we've quit */
   1.502 +    if (!SDL_EventQ.active) {
   1.503 +        return (-1);
   1.504 +    }
   1.505 +    /* Lock the event queue */
   1.506 +    used = 0;
   1.507 +    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
   1.508 +        if (action == SDL_ADDEVENT) {
   1.509 +            for (i = 0; i < numevents; ++i) {
   1.510 +                used += SDL_AddEvent(&events[i]);
   1.511 +            }
   1.512 +        } else {
   1.513 +            SDL_Event tmpevent;
   1.514 +            int spot;
   1.515  
   1.516 -			/* If 'events' is NULL, just see if they exist */
   1.517 -			if ( events == NULL ) {
   1.518 -				action = SDL_PEEKEVENT;
   1.519 -				numevents = 1;
   1.520 -				events = &tmpevent;
   1.521 -			}
   1.522 -			spot = SDL_EventQ.head;
   1.523 -			while ((used < numevents)&&(spot != SDL_EventQ.tail)) {
   1.524 -				if ( mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type) ) {
   1.525 -					events[used++] = SDL_EventQ.event[spot];
   1.526 -					if ( action == SDL_GETEVENT ) {
   1.527 -						spot = SDL_CutEvent(spot);
   1.528 -					} else {
   1.529 -						spot = (spot+1)%MAXEVENTS;
   1.530 -					}
   1.531 -				} else {
   1.532 -					spot = (spot+1)%MAXEVENTS;
   1.533 -				}
   1.534 -			}
   1.535 -		}
   1.536 -		SDL_mutexV(SDL_EventQ.lock);
   1.537 -	} else {
   1.538 -		SDL_SetError("Couldn't lock event queue");
   1.539 -		used = -1;
   1.540 -	}
   1.541 -	return(used);
   1.542 +            /* If 'events' is NULL, just see if they exist */
   1.543 +            if (events == NULL) {
   1.544 +                action = SDL_PEEKEVENT;
   1.545 +                numevents = 1;
   1.546 +                events = &tmpevent;
   1.547 +            }
   1.548 +            spot = SDL_EventQ.head;
   1.549 +            while ((used < numevents) && (spot != SDL_EventQ.tail)) {
   1.550 +                if (mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type)) {
   1.551 +                    events[used++] = SDL_EventQ.event[spot];
   1.552 +                    if (action == SDL_GETEVENT) {
   1.553 +                        spot = SDL_CutEvent(spot);
   1.554 +                    } else {
   1.555 +                        spot = (spot + 1) % MAXEVENTS;
   1.556 +                    }
   1.557 +                } else {
   1.558 +                    spot = (spot + 1) % MAXEVENTS;
   1.559 +                }
   1.560 +            }
   1.561 +        }
   1.562 +        SDL_mutexV(SDL_EventQ.lock);
   1.563 +    } else {
   1.564 +        SDL_SetError("Couldn't lock event queue");
   1.565 +        used = -1;
   1.566 +    }
   1.567 +    return (used);
   1.568 +}
   1.569 +
   1.570 +SDL_bool
   1.571 +SDL_HasEvent(Uint32 mask)
   1.572 +{
   1.573 +    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask) > 0);
   1.574  }
   1.575  
   1.576  /* Run the system dependent event loops */
   1.577 -void SDL_PumpEvents(void)
   1.578 +void
   1.579 +SDL_PumpEvents(void)
   1.580  {
   1.581 -	if ( !SDL_EventThread ) {
   1.582 -		SDL_VideoDevice *video = current_video;
   1.583 -		SDL_VideoDevice *this  = current_video;
   1.584 +    if (!SDL_EventThread) {
   1.585 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.586  
   1.587 -		/* Get events from the video subsystem */
   1.588 -		if ( video ) {
   1.589 -			video->PumpEvents(this);
   1.590 -		}
   1.591 +        /* Get events from the video subsystem */
   1.592 +        if (_this) {
   1.593 +            _this->PumpEvents(_this);
   1.594 +        }
   1.595  
   1.596 -		/* Queue pending key-repeat events */
   1.597 -		SDL_CheckKeyRepeat();
   1.598 +        /* Queue pending key-repeat events */
   1.599 +        SDL_CheckKeyRepeat();
   1.600  
   1.601  #if !SDL_JOYSTICK_DISABLED
   1.602 -		/* Check for joystick state change */
   1.603 -		if ( SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK) ) {
   1.604 -			SDL_JoystickUpdate();
   1.605 -		}
   1.606 +        /* Check for joystick state change */
   1.607 +        if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
   1.608 +            SDL_JoystickUpdate();
   1.609 +        }
   1.610  #endif
   1.611 -	}
   1.612 +    }
   1.613  }
   1.614  
   1.615  /* Public functions */
   1.616  
   1.617 -int SDL_PollEvent (SDL_Event *event)
   1.618 +int
   1.619 +SDL_PollEvent(SDL_Event * event)
   1.620  {
   1.621 -	SDL_PumpEvents();
   1.622 +    SDL_PumpEvents();
   1.623 +
   1.624 +    /* We can't return -1, just return 0 (no event) on error */
   1.625 +    if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0)
   1.626 +        return 0;
   1.627 +    return 1;
   1.628 +}
   1.629  
   1.630 -	/* We can't return -1, just return 0 (no event) on error */
   1.631 -	if ( SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0 )
   1.632 -		return 0;
   1.633 -	return 1;
   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:
   1.641 +            return 0;
   1.642 +        case 1:
   1.643 +            return 1;
   1.644 +        case 0:
   1.645 +            SDL_Delay(10);
   1.646 +        }
   1.647 +    }
   1.648  }
   1.649  
   1.650 -int SDL_WaitEvent (SDL_Event *event)
   1.651 +int
   1.652 +SDL_PushEvent(SDL_Event * event)
   1.653  {
   1.654 -	while ( 1 ) {
   1.655 -		SDL_PumpEvents();
   1.656 -		switch(SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
   1.657 -		    case -1: return 0;
   1.658 -		    case 1: return 1;
   1.659 -		    case 0: SDL_Delay(10);
   1.660 -		}
   1.661 -	}
   1.662 +    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
   1.663 +        return 0;
   1.664 +    }
   1.665 +    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0) {
   1.666 +        return -1;
   1.667 +    }
   1.668 +    return 1;
   1.669  }
   1.670  
   1.671 -int SDL_PushEvent(SDL_Event *event)
   1.672 +void
   1.673 +SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
   1.674  {
   1.675 -	if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
   1.676 -		return -1;
   1.677 -	return 0;
   1.678 +    SDL_Event bitbucket;
   1.679 +
   1.680 +    /* Set filter and discard pending events */
   1.681 +    SDL_EventOK = filter;
   1.682 +    SDL_EventOKParam = userdata;
   1.683 +    while (SDL_PollEvent(&bitbucket) > 0);
   1.684  }
   1.685  
   1.686 -void SDL_SetEventFilter (SDL_EventFilter filter)
   1.687 +SDL_bool
   1.688 +SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
   1.689  {
   1.690 -	SDL_Event bitbucket;
   1.691 -
   1.692 -	/* Set filter and discard pending events */
   1.693 -	SDL_EventOK = filter;
   1.694 -	while ( SDL_PollEvent(&bitbucket) > 0 )
   1.695 -		;
   1.696 -}
   1.697 -
   1.698 -SDL_EventFilter SDL_GetEventFilter(void)
   1.699 -{
   1.700 -	return(SDL_EventOK);
   1.701 +    if (filter) {
   1.702 +        *filter = SDL_EventOK;
   1.703 +    }
   1.704 +    if (userdata) {
   1.705 +        *userdata = SDL_EventOKParam;
   1.706 +    }
   1.707 +    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
   1.708  }
   1.709  
   1.710 -Uint8 SDL_EventState (Uint8 type, int state)
   1.711 +void
   1.712 +SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
   1.713  {
   1.714 -	SDL_Event bitbucket;
   1.715 -	Uint8 current_state;
   1.716 +    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
   1.717 +        int spot;
   1.718 +
   1.719 +        spot = SDL_EventQ.head;
   1.720 +        while (spot != SDL_EventQ.tail) {
   1.721 +            if (filter(userdata, &SDL_EventQ.event[spot])) {
   1.722 +                spot = (spot + 1) % MAXEVENTS;
   1.723 +            } else {
   1.724 +                spot = SDL_CutEvent(spot);
   1.725 +            }
   1.726 +        }
   1.727 +    }
   1.728 +    SDL_mutexV(SDL_EventQ.lock);
   1.729 +}
   1.730 +
   1.731 +Uint8
   1.732 +SDL_EventState(Uint8 type, int state)
   1.733 +{
   1.734 +    SDL_Event bitbucket;
   1.735 +    Uint8 current_state;
   1.736  
   1.737 -	/* If SDL_ALLEVENTS was specified... */
   1.738 -	if ( type == 0xFF ) {
   1.739 -		current_state = SDL_IGNORE;
   1.740 -		for ( type=0; type<SDL_NUMEVENTS; ++type ) {
   1.741 -			if ( SDL_ProcessEvents[type] != SDL_IGNORE ) {
   1.742 -				current_state = SDL_ENABLE;
   1.743 -			}
   1.744 -			SDL_ProcessEvents[type] = state;
   1.745 -			if ( state == SDL_ENABLE ) {
   1.746 -				SDL_eventstate |= (0x00000001 << (type));
   1.747 -			} else {
   1.748 -				SDL_eventstate &= ~(0x00000001 << (type));
   1.749 -			}
   1.750 -		}
   1.751 -		while ( SDL_PollEvent(&bitbucket) > 0 )
   1.752 -			;
   1.753 -		return(current_state);
   1.754 -	}
   1.755 +    /* If SDL_ALLEVENTS was specified... */
   1.756 +    if (type == 0xFF) {
   1.757 +        current_state = SDL_IGNORE;
   1.758 +        for (type = 0; type < SDL_NUMEVENTS; ++type) {
   1.759 +            if (SDL_ProcessEvents[type] != SDL_IGNORE) {
   1.760 +                current_state = SDL_ENABLE;
   1.761 +            }
   1.762 +            SDL_ProcessEvents[type] = state;
   1.763 +            if (state == SDL_ENABLE) {
   1.764 +                SDL_eventstate |= (0x00000001 << (type));
   1.765 +            } else {
   1.766 +                SDL_eventstate &= ~(0x00000001 << (type));
   1.767 +            }
   1.768 +        }
   1.769 +        while (SDL_PollEvent(&bitbucket) > 0);
   1.770 +        return (current_state);
   1.771 +    }
   1.772  
   1.773 -	/* Just set the state for one event type */
   1.774 -	current_state = SDL_ProcessEvents[type];
   1.775 -	switch (state) {
   1.776 -		case SDL_IGNORE:
   1.777 -		case SDL_ENABLE:
   1.778 -			/* Set state and discard pending events */
   1.779 -			SDL_ProcessEvents[type] = state;
   1.780 -			if ( state == SDL_ENABLE ) {
   1.781 -				SDL_eventstate |= (0x00000001 << (type));
   1.782 -			} else {
   1.783 -				SDL_eventstate &= ~(0x00000001 << (type));
   1.784 -			}
   1.785 -			while ( SDL_PollEvent(&bitbucket) > 0 )
   1.786 -				;
   1.787 -			break;
   1.788 -		default:
   1.789 -			/* Querying state? */
   1.790 -			break;
   1.791 -	}
   1.792 -	return(current_state);
   1.793 +    /* Just set the state for one event type */
   1.794 +    current_state = SDL_ProcessEvents[type];
   1.795 +    switch (state) {
   1.796 +    case SDL_IGNORE:
   1.797 +    case SDL_ENABLE:
   1.798 +        /* Set state and discard pending events */
   1.799 +        SDL_ProcessEvents[type] = state;
   1.800 +        if (state == SDL_ENABLE) {
   1.801 +            SDL_eventstate |= (0x00000001 << (type));
   1.802 +        } else {
   1.803 +            SDL_eventstate &= ~(0x00000001 << (type));
   1.804 +        }
   1.805 +        while (SDL_PollEvent(&bitbucket) > 0);
   1.806 +        break;
   1.807 +    default:
   1.808 +        /* Querying state? */
   1.809 +        break;
   1.810 +    }
   1.811 +    return (current_state);
   1.812  }
   1.813  
   1.814  /* This is a generic event handler.
   1.815   */
   1.816 -int SDL_PrivateSysWMEvent(SDL_SysWMmsg *message)
   1.817 +int
   1.818 +SDL_SendSysWMEvent(SDL_SysWMmsg * message)
   1.819  {
   1.820 -	int posted;
   1.821 +    int posted;
   1.822  
   1.823 -	posted = 0;
   1.824 -	if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   1.825 -		SDL_Event event;
   1.826 -		SDL_memset(&event, 0, sizeof(event));
   1.827 -		event.type = SDL_SYSWMEVENT;
   1.828 -		event.syswm.msg = message;
   1.829 -		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   1.830 -			posted = 1;
   1.831 -			SDL_PushEvent(&event);
   1.832 -		}
   1.833 -	}
   1.834 -	/* Update internal event state */
   1.835 -	return(posted);
   1.836 +    posted = 0;
   1.837 +    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
   1.838 +        SDL_Event event;
   1.839 +        SDL_memset(&event, 0, sizeof(event));
   1.840 +        event.type = SDL_SYSWMEVENT;
   1.841 +        event.syswm.msg = message;
   1.842 +        posted = (SDL_PushEvent(&event) > 0);
   1.843 +    }
   1.844 +    /* Update internal event state */
   1.845 +    return (posted);
   1.846  }
   1.847 +
   1.848 +/* vi: set ts=4 sw=4 expandtab: */