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