src/events/SDL_events.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Sep 2009 06:43:45 +0000
changeset 3269 a67a961e2171
parent 3268 82ce350be859
child 3578 0d1b16ee0bca
permissions -rw-r--r--
Removed outdated OS/2 support
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General event handling code for SDL */
slouken@0
    25
slouken@0
    26
#include "SDL.h"
slouken@2984
    27
#include "SDL_events.h"
slouken@0
    28
#include "SDL_syswm.h"
slouken@2984
    29
#include "SDL_thread.h"
slouken@0
    30
#include "SDL_sysevents.h"
slouken@1361
    31
#include "SDL_events_c.h"
slouken@1361
    32
#include "../timer/SDL_timer_c.h"
slouken@1361
    33
#if !SDL_JOYSTICK_DISABLED
slouken@1361
    34
#include "../joystick/SDL_joystick_c.h"
slouken@1361
    35
#endif
slouken@0
    36
slouken@0
    37
/* Public data -- the event filter */
slouken@0
    38
SDL_EventFilter SDL_EventOK = NULL;
slouken@1895
    39
void *SDL_EventOKParam;
slouken@0
    40
Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
slouken@0
    41
static Uint32 SDL_eventstate = 0;
slouken@0
    42
slouken@0
    43
/* Private data -- event queue */
slouken@0
    44
#define MAXEVENTS	128
slouken@1895
    45
static struct
slouken@1895
    46
{
slouken@1895
    47
    SDL_mutex *lock;
slouken@1895
    48
    int active;
slouken@1895
    49
    int head;
slouken@1895
    50
    int tail;
slouken@1895
    51
    SDL_Event event[MAXEVENTS];
slouken@1895
    52
    int wmmsg_next;
slouken@1895
    53
    struct SDL_SysWMmsg wmmsg[MAXEVENTS];
slouken@0
    54
} SDL_EventQ;
slouken@0
    55
slouken@0
    56
/* Private data -- event locking structure */
slouken@1895
    57
static struct
slouken@1895
    58
{
slouken@1895
    59
    SDL_mutex *lock;
slouken@1895
    60
    int safe;
slouken@0
    61
} SDL_EventLock;
slouken@0
    62
slouken@0
    63
/* Thread functions */
slouken@1895
    64
static SDL_Thread *SDL_EventThread = NULL;      /* Thread handle */
slouken@1895
    65
static Uint32 event_thread;     /* The event thread id */
slouken@0
    66
slouken@1895
    67
void
slouken@1895
    68
SDL_Lock_EventThread(void)
slouken@0
    69
{
slouken@1895
    70
    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
slouken@1895
    71
        /* Grab lock and spin until we're sure event thread stopped */
slouken@1895
    72
        SDL_mutexP(SDL_EventLock.lock);
slouken@1895
    73
        while (!SDL_EventLock.safe) {
slouken@1895
    74
            SDL_Delay(1);
slouken@1895
    75
        }
slouken@1895
    76
    }
slouken@0
    77
}
slouken@2735
    78
slouken@1895
    79
void
slouken@1895
    80
SDL_Unlock_EventThread(void)
slouken@0
    81
{
slouken@1895
    82
    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
slouken@1895
    83
        SDL_mutexV(SDL_EventLock.lock);
slouken@1895
    84
    }
slouken@0
    85
}
slouken@0
    86
slouken@1895
    87
static int SDLCALL
slouken@1895
    88
SDL_GobbleEvents(void *unused)
slouken@0
    89
{
slouken@1895
    90
    event_thread = SDL_ThreadID();
icculus@1190
    91
slouken@1895
    92
    while (SDL_EventQ.active) {
slouken@1895
    93
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
    94
slouken@1895
    95
        /* Get events from the video subsystem */
slouken@1895
    96
        if (_this) {
slouken@1895
    97
            _this->PumpEvents(_this);
slouken@1895
    98
        }
slouken@1361
    99
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   100
        /* Check for joystick state change */
slouken@1895
   101
        if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
slouken@1895
   102
            SDL_JoystickUpdate();
slouken@1895
   103
        }
slouken@0
   104
#endif
slouken@0
   105
slouken@1895
   106
        /* Give up the CPU for the rest of our timeslice */
slouken@1895
   107
        SDL_EventLock.safe = 1;
slouken@1895
   108
        if (SDL_timer_running) {
slouken@1895
   109
            SDL_ThreadedTimerCheck();
slouken@1895
   110
        }
slouken@1895
   111
        SDL_Delay(1);
slouken@0
   112
slouken@1895
   113
        /* Check for event locking.
slouken@1895
   114
           On the P of the lock mutex, if the lock is held, this thread
slouken@1895
   115
           will wait until the lock is released before continuing.  The
slouken@1895
   116
           safe flag will be set, meaning that the other thread can go
slouken@1895
   117
           about it's business.  The safe flag is reset before the V,
slouken@1895
   118
           so as soon as the mutex is free, other threads can see that
slouken@1895
   119
           it's not safe to interfere with the event thread.
slouken@1895
   120
         */
slouken@1895
   121
        SDL_mutexP(SDL_EventLock.lock);
slouken@1895
   122
        SDL_EventLock.safe = 0;
slouken@1895
   123
        SDL_mutexV(SDL_EventLock.lock);
slouken@1895
   124
    }
slouken@1895
   125
    SDL_SetTimerThreaded(0);
slouken@1895
   126
    event_thread = 0;
slouken@1895
   127
    return (0);
slouken@0
   128
}
slouken@0
   129
slouken@1895
   130
static int
slouken@1895
   131
SDL_StartEventThread(Uint32 flags)
slouken@0
   132
{
slouken@1895
   133
    /* Reset everything to zero */
slouken@1895
   134
    SDL_EventThread = NULL;
slouken@1895
   135
    SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
slouken@0
   136
slouken@1895
   137
    /* Create the lock and set ourselves active */
slouken@1361
   138
#if !SDL_THREADS_DISABLED
slouken@1895
   139
    SDL_EventQ.lock = SDL_CreateMutex();
slouken@1895
   140
    if (SDL_EventQ.lock == NULL) {
slouken@1895
   141
        return (-1);
slouken@1895
   142
    }
slouken@1361
   143
#endif /* !SDL_THREADS_DISABLED */
slouken@1895
   144
    SDL_EventQ.active = 1;
slouken@0
   145
slouken@1895
   146
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@1895
   147
        SDL_EventLock.lock = SDL_CreateMutex();
slouken@1895
   148
        if (SDL_EventLock.lock == NULL) {
slouken@1895
   149
            return (-1);
slouken@1895
   150
        }
slouken@1895
   151
        SDL_EventLock.safe = 0;
slouken@0
   152
slouken@1895
   153
        /* The event thread will handle timers too */
slouken@1895
   154
        SDL_SetTimerThreaded(2);
slouken@1402
   155
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
slouken@1330
   156
#undef SDL_CreateThread
slouken@1895
   157
        SDL_EventThread =
slouken@1895
   158
            SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
slouken@1330
   159
#else
slouken@1895
   160
        SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
slouken@1330
   161
#endif
slouken@1895
   162
        if (SDL_EventThread == NULL) {
slouken@1895
   163
            return (-1);
slouken@1895
   164
        }
slouken@1895
   165
    } else {
slouken@1895
   166
        event_thread = 0;
slouken@1895
   167
    }
slouken@1895
   168
    return (0);
slouken@0
   169
}
slouken@0
   170
slouken@1895
   171
static void
slouken@1895
   172
SDL_StopEventThread(void)
slouken@0
   173
{
slouken@1895
   174
    SDL_EventQ.active = 0;
slouken@1895
   175
    if (SDL_EventThread) {
slouken@1895
   176
        SDL_WaitThread(SDL_EventThread, NULL);
slouken@1895
   177
        SDL_EventThread = NULL;
slouken@1895
   178
        SDL_DestroyMutex(SDL_EventLock.lock);
slouken@1895
   179
    }
slouken@3259
   180
    if (SDL_EventQ.lock) {
slouken@3259
   181
        SDL_DestroyMutex(SDL_EventQ.lock);
slouken@3259
   182
        SDL_EventQ.lock = NULL;
slouken@3259
   183
    }
slouken@0
   184
}
slouken@0
   185
slouken@1895
   186
Uint32
slouken@1895
   187
SDL_EventThreadID(void)
slouken@0
   188
{
slouken@1895
   189
    return (event_thread);
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
/* Public functions */
slouken@0
   193
slouken@1895
   194
void
slouken@1895
   195
SDL_StopEventLoop(void)
slouken@0
   196
{
slouken@1895
   197
    /* Halt the event thread, if running */
slouken@1895
   198
    SDL_StopEventThread();
slouken@0
   199
slouken@1895
   200
    /* Shutdown event handlers */
slouken@1895
   201
    SDL_KeyboardQuit();
slouken@1895
   202
    SDL_MouseQuit();
slouken@1895
   203
    SDL_QuitQuit();
slouken@1123
   204
slouken@1895
   205
    /* Clean out EventQ */
slouken@1895
   206
    SDL_EventQ.head = 0;
slouken@1895
   207
    SDL_EventQ.tail = 0;
slouken@1895
   208
    SDL_EventQ.wmmsg_next = 0;
slouken@0
   209
}
slouken@0
   210
slouken@0
   211
/* This function (and associated calls) may be called more than once */
slouken@1895
   212
int
slouken@1895
   213
SDL_StartEventLoop(Uint32 flags)
slouken@0
   214
{
slouken@1895
   215
    int retcode;
slouken@0
   216
slouken@1895
   217
    /* Clean out the event queue */
slouken@1895
   218
    SDL_EventThread = NULL;
slouken@1895
   219
    SDL_EventQ.lock = NULL;
slouken@1895
   220
    SDL_StopEventLoop();
slouken@0
   221
slouken@1895
   222
    /* No filter to start with, process most event types */
slouken@1895
   223
    SDL_EventOK = NULL;
slouken@1895
   224
    SDL_memset(SDL_ProcessEvents, SDL_ENABLE, sizeof(SDL_ProcessEvents));
slouken@1895
   225
    SDL_eventstate = ~0;
slouken@1895
   226
    /* It's not save to call SDL_EventState() yet */
slouken@1895
   227
    SDL_eventstate &= ~(0x00000001 << SDL_SYSWMEVENT);
slouken@1895
   228
    SDL_ProcessEvents[SDL_SYSWMEVENT] = SDL_IGNORE;
slouken@0
   229
slouken@1895
   230
    /* Initialize event handlers */
slouken@1895
   231
    retcode = 0;
slouken@1895
   232
    retcode += SDL_KeyboardInit();
slouken@1895
   233
    retcode += SDL_MouseInit();
slouken@1895
   234
    retcode += SDL_QuitInit();
slouken@1895
   235
    if (retcode < 0) {
slouken@1895
   236
        /* We don't expect them to fail, but... */
slouken@1895
   237
        return (-1);
slouken@1895
   238
    }
slouken@0
   239
slouken@1895
   240
    /* Create the lock and event thread */
slouken@1895
   241
    if (SDL_StartEventThread(flags) < 0) {
slouken@1895
   242
        SDL_StopEventLoop();
slouken@1895
   243
        return (-1);
slouken@1895
   244
    }
slouken@1895
   245
    return (0);
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
slouken@0
   249
/* Add an event to the event queue -- called with the queue locked */
slouken@1895
   250
static int
slouken@1895
   251
SDL_AddEvent(SDL_Event * event)
slouken@0
   252
{
slouken@1895
   253
    int tail, added;
slouken@0
   254
slouken@1895
   255
    tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
slouken@1895
   256
    if (tail == SDL_EventQ.head) {
slouken@1895
   257
        /* Overflow, drop event */
slouken@1895
   258
        added = 0;
slouken@1895
   259
    } else {
slouken@1895
   260
        SDL_EventQ.event[SDL_EventQ.tail] = *event;
slouken@1895
   261
        if (event->type == SDL_SYSWMEVENT) {
slouken@1895
   262
            /* Note that it's possible to lose an event */
slouken@1895
   263
            int next = SDL_EventQ.wmmsg_next;
slouken@1895
   264
            SDL_EventQ.wmmsg[next] = *event->syswm.msg;
slouken@1895
   265
            SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
slouken@1895
   266
                &SDL_EventQ.wmmsg[next];
slouken@1895
   267
            SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
slouken@1895
   268
        }
slouken@1895
   269
        SDL_EventQ.tail = tail;
slouken@1895
   270
        added = 1;
slouken@1895
   271
    }
slouken@1895
   272
    return (added);
slouken@0
   273
}
slouken@0
   274
slouken@0
   275
/* Cut an event, and return the next valid spot, or the tail */
slouken@0
   276
/*                           -- called with the queue locked */
slouken@1895
   277
static int
slouken@1895
   278
SDL_CutEvent(int spot)
slouken@0
   279
{
slouken@1895
   280
    if (spot == SDL_EventQ.head) {
slouken@1895
   281
        SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
slouken@1895
   282
        return (SDL_EventQ.head);
slouken@1895
   283
    } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
slouken@1895
   284
        SDL_EventQ.tail = spot;
slouken@1895
   285
        return (SDL_EventQ.tail);
slouken@1895
   286
    } else
slouken@1895
   287
        /* We cut the middle -- shift everything over */
slouken@1895
   288
    {
slouken@1895
   289
        int here, next;
slouken@0
   290
slouken@1895
   291
        /* This can probably be optimized with SDL_memcpy() -- careful! */
slouken@1895
   292
        if (--SDL_EventQ.tail < 0) {
slouken@1895
   293
            SDL_EventQ.tail = MAXEVENTS - 1;
slouken@1895
   294
        }
slouken@1895
   295
        for (here = spot; here != SDL_EventQ.tail; here = next) {
slouken@1895
   296
            next = (here + 1) % MAXEVENTS;
slouken@1895
   297
            SDL_EventQ.event[here] = SDL_EventQ.event[next];
slouken@1895
   298
        }
slouken@1895
   299
        return (spot);
slouken@1895
   300
    }
slouken@1895
   301
    /* NOTREACHED */
slouken@0
   302
}
slouken@0
   303
slouken@0
   304
/* Lock the event queue, take a peep at it, and unlock it */
slouken@1895
   305
int
slouken@1895
   306
SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
slouken@1895
   307
               Uint32 mask)
slouken@0
   308
{
slouken@1895
   309
    int i, used;
slouken@0
   310
slouken@1895
   311
    /* Don't look after we've quit */
slouken@1895
   312
    if (!SDL_EventQ.active) {
slouken@1895
   313
        return (-1);
slouken@1895
   314
    }
slouken@1895
   315
    /* Lock the event queue */
slouken@1895
   316
    used = 0;
slouken@1895
   317
    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
slouken@1895
   318
        if (action == SDL_ADDEVENT) {
slouken@1895
   319
            for (i = 0; i < numevents; ++i) {
slouken@1895
   320
                used += SDL_AddEvent(&events[i]);
slouken@1895
   321
            }
slouken@1895
   322
        } else {
slouken@1895
   323
            SDL_Event tmpevent;
slouken@1895
   324
            int spot;
slouken@0
   325
slouken@1895
   326
            /* If 'events' is NULL, just see if they exist */
slouken@1895
   327
            if (events == NULL) {
slouken@1895
   328
                action = SDL_PEEKEVENT;
slouken@1895
   329
                numevents = 1;
slouken@1895
   330
                events = &tmpevent;
slouken@1895
   331
            }
slouken@1895
   332
            spot = SDL_EventQ.head;
slouken@1895
   333
            while ((used < numevents) && (spot != SDL_EventQ.tail)) {
slouken@1895
   334
                if (mask & SDL_EVENTMASK(SDL_EventQ.event[spot].type)) {
slouken@1895
   335
                    events[used++] = SDL_EventQ.event[spot];
slouken@1895
   336
                    if (action == SDL_GETEVENT) {
slouken@1895
   337
                        spot = SDL_CutEvent(spot);
slouken@1895
   338
                    } else {
slouken@1895
   339
                        spot = (spot + 1) % MAXEVENTS;
slouken@1895
   340
                    }
slouken@1895
   341
                } else {
slouken@1895
   342
                    spot = (spot + 1) % MAXEVENTS;
slouken@1895
   343
                }
slouken@1895
   344
            }
slouken@1895
   345
        }
slouken@1895
   346
        SDL_mutexV(SDL_EventQ.lock);
slouken@1895
   347
    } else {
slouken@1895
   348
        SDL_SetError("Couldn't lock event queue");
slouken@1895
   349
        used = -1;
slouken@1895
   350
    }
slouken@1895
   351
    return (used);
slouken@1895
   352
}
slouken@1895
   353
slouken@1895
   354
SDL_bool
slouken@1895
   355
SDL_HasEvent(Uint32 mask)
slouken@1895
   356
{
slouken@1895
   357
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask) > 0);
slouken@0
   358
}
slouken@0
   359
slouken@0
   360
/* Run the system dependent event loops */
slouken@1895
   361
void
slouken@1895
   362
SDL_PumpEvents(void)
slouken@0
   363
{
slouken@1895
   364
    if (!SDL_EventThread) {
slouken@1895
   365
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
   366
slouken@1895
   367
        /* Get events from the video subsystem */
slouken@1895
   368
        if (_this) {
slouken@1895
   369
            _this->PumpEvents(_this);
slouken@1895
   370
        }
slouken@1361
   371
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   372
        /* Check for joystick state change */
slouken@1895
   373
        if (SDL_numjoysticks && (SDL_eventstate & SDL_JOYEVENTMASK)) {
slouken@1895
   374
            SDL_JoystickUpdate();
slouken@1895
   375
        }
slouken@0
   376
#endif
slouken@1895
   377
    }
slouken@0
   378
}
slouken@0
   379
slouken@0
   380
/* Public functions */
slouken@0
   381
slouken@1895
   382
int
slouken@1895
   383
SDL_PollEvent(SDL_Event * event)
slouken@0
   384
{
slouken@3072
   385
    return SDL_WaitEventTimeout(event, 0);
slouken@0
   386
}
slouken@0
   387
slouken@1895
   388
int
slouken@1895
   389
SDL_WaitEvent(SDL_Event * event)
slouken@0
   390
{
slouken@3072
   391
    return SDL_WaitEventTimeout(event, -1);
slouken@3072
   392
}
slouken@3072
   393
slouken@3072
   394
int
slouken@3072
   395
SDL_WaitEventTimeout(SDL_Event * event, int timeout)
slouken@3072
   396
{
slouken@3072
   397
    Uint32 expiration = 0;
slouken@3072
   398
slouken@3072
   399
    if (timeout > 0)
slouken@3072
   400
        expiration = SDL_GetTicks() + timeout;
slouken@3072
   401
slouken@3072
   402
    for (;;) {
slouken@1895
   403
        SDL_PumpEvents();
slouken@1895
   404
        switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
slouken@1895
   405
        case -1:
slouken@1895
   406
            return 0;
slouken@1895
   407
        case 1:
slouken@1895
   408
            return 1;
slouken@1895
   409
        case 0:
slouken@3072
   410
            if (timeout == 0) {
slouken@3072
   411
                /* Polling and no events, just return */
slouken@3072
   412
                return 0;
slouken@3072
   413
            }
slouken@3072
   414
            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
slouken@3072
   415
                /* Timeout expired and no events */
slouken@3072
   416
                return 0;
slouken@3072
   417
            }
slouken@1895
   418
            SDL_Delay(10);
slouken@3072
   419
            break;
slouken@1895
   420
        }
slouken@1895
   421
    }
slouken@0
   422
}
slouken@0
   423
slouken@1895
   424
int
slouken@1895
   425
SDL_PushEvent(SDL_Event * event)
slouken@0
   426
{
slouken@1895
   427
    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
slouken@1895
   428
        return 0;
slouken@1895
   429
    }
slouken@1895
   430
    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0) {
slouken@1895
   431
        return -1;
slouken@1895
   432
    }
slouken@1895
   433
    return 1;
slouken@0
   434
}
slouken@0
   435
slouken@1895
   436
void
slouken@1895
   437
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
slouken@0
   438
{
slouken@1895
   439
    SDL_Event bitbucket;
slouken@0
   440
slouken@1895
   441
    /* Set filter and discard pending events */
slouken@1895
   442
    SDL_EventOK = filter;
slouken@1895
   443
    SDL_EventOKParam = userdata;
slouken@1895
   444
    while (SDL_PollEvent(&bitbucket) > 0);
slouken@0
   445
}
slouken@0
   446
slouken@1895
   447
SDL_bool
slouken@1895
   448
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
slouken@0
   449
{
slouken@1895
   450
    if (filter) {
slouken@1895
   451
        *filter = SDL_EventOK;
slouken@1895
   452
    }
slouken@1895
   453
    if (userdata) {
slouken@1895
   454
        *userdata = SDL_EventOKParam;
slouken@1895
   455
    }
slouken@1895
   456
    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
slouken@0
   457
}
slouken@0
   458
slouken@1895
   459
void
slouken@1895
   460
SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
slouken@0
   461
{
slouken@1895
   462
    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
slouken@1895
   463
        int spot;
slouken@0
   464
slouken@1895
   465
        spot = SDL_EventQ.head;
slouken@1895
   466
        while (spot != SDL_EventQ.tail) {
slouken@1895
   467
            if (filter(userdata, &SDL_EventQ.event[spot])) {
slouken@1895
   468
                spot = (spot + 1) % MAXEVENTS;
slouken@1895
   469
            } else {
slouken@1895
   470
                spot = SDL_CutEvent(spot);
slouken@1895
   471
            }
slouken@1895
   472
        }
slouken@1895
   473
    }
slouken@1895
   474
    SDL_mutexV(SDL_EventQ.lock);
slouken@1895
   475
}
slouken@0
   476
slouken@1895
   477
Uint8
slouken@1895
   478
SDL_EventState(Uint8 type, int state)
slouken@1895
   479
{
slouken@1895
   480
    SDL_Event bitbucket;
slouken@1895
   481
    Uint8 current_state;
slouken@1895
   482
slouken@1895
   483
    /* If SDL_ALLEVENTS was specified... */
slouken@1895
   484
    if (type == 0xFF) {
slouken@1895
   485
        current_state = SDL_IGNORE;
slouken@1895
   486
        for (type = 0; type < SDL_NUMEVENTS; ++type) {
slouken@1895
   487
            if (SDL_ProcessEvents[type] != SDL_IGNORE) {
slouken@1895
   488
                current_state = SDL_ENABLE;
slouken@1895
   489
            }
slouken@1895
   490
            SDL_ProcessEvents[type] = state;
slouken@1895
   491
            if (state == SDL_ENABLE) {
slouken@1895
   492
                SDL_eventstate |= (0x00000001 << (type));
slouken@1895
   493
            } else {
slouken@1895
   494
                SDL_eventstate &= ~(0x00000001 << (type));
slouken@1895
   495
            }
slouken@1895
   496
        }
slouken@1895
   497
        while (SDL_PollEvent(&bitbucket) > 0);
slouken@1895
   498
        return (current_state);
slouken@1895
   499
    }
slouken@1895
   500
slouken@1895
   501
    /* Just set the state for one event type */
slouken@1895
   502
    current_state = SDL_ProcessEvents[type];
slouken@1895
   503
    switch (state) {
slouken@1895
   504
    case SDL_IGNORE:
slouken@1895
   505
    case SDL_ENABLE:
slouken@1895
   506
        /* Set state and discard pending events */
slouken@1895
   507
        SDL_ProcessEvents[type] = state;
slouken@1895
   508
        if (state == SDL_ENABLE) {
slouken@1895
   509
            SDL_eventstate |= (0x00000001 << (type));
slouken@1895
   510
        } else {
slouken@1895
   511
            SDL_eventstate &= ~(0x00000001 << (type));
slouken@1895
   512
        }
slouken@1895
   513
        while (SDL_PollEvent(&bitbucket) > 0);
slouken@1895
   514
        break;
slouken@1895
   515
    default:
slouken@1895
   516
        /* Querying state? */
slouken@1895
   517
        break;
slouken@1895
   518
    }
slouken@1895
   519
    return (current_state);
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
/* This is a generic event handler.
slouken@0
   523
 */
slouken@1895
   524
int
slouken@1895
   525
SDL_SendSysWMEvent(SDL_SysWMmsg * message)
slouken@0
   526
{
slouken@1895
   527
    int posted;
slouken@0
   528
slouken@1895
   529
    posted = 0;
slouken@1895
   530
    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
slouken@1895
   531
        SDL_Event event;
slouken@1895
   532
        SDL_memset(&event, 0, sizeof(event));
slouken@1895
   533
        event.type = SDL_SYSWMEVENT;
slouken@1895
   534
        event.syswm.msg = message;
slouken@1895
   535
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   536
    }
slouken@1895
   537
    /* Update internal event state */
slouken@1895
   538
    return (posted);
slouken@0
   539
}
slouken@1895
   540
slouken@1895
   541
/* vi: set ts=4 sw=4 expandtab: */