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