src/events/SDL_events.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 23 Aug 2010 23:44:28 -0700
changeset 4865 fff50e86c891
parent 4729 1f7ad083fd3c
child 5062 e8916fe9cfc8
permissions -rw-r--r--
Update VS2010 project to add new files; update new files so code builds on Win32/Win64
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@4429
    40
slouken@4429
    41
typedef struct {
slouken@4429
    42
    Uint32 bits[8];
slouken@4429
    43
} SDL_DisabledEventBlock;
slouken@4429
    44
slouken@4429
    45
static SDL_DisabledEventBlock *SDL_disabled_events[256];
slouken@4429
    46
static Uint32 SDL_userevents = SDL_USEREVENT;
slouken@0
    47
slouken@0
    48
/* Private data -- event queue */
slouken@0
    49
#define MAXEVENTS	128
slouken@1895
    50
static struct
slouken@1895
    51
{
slouken@1895
    52
    SDL_mutex *lock;
slouken@1895
    53
    int active;
slouken@1895
    54
    int head;
slouken@1895
    55
    int tail;
slouken@1895
    56
    SDL_Event event[MAXEVENTS];
slouken@1895
    57
    int wmmsg_next;
slouken@1895
    58
    struct SDL_SysWMmsg wmmsg[MAXEVENTS];
slouken@0
    59
} SDL_EventQ;
slouken@0
    60
slouken@0
    61
/* Private data -- event locking structure */
slouken@1895
    62
static struct
slouken@1895
    63
{
slouken@1895
    64
    SDL_mutex *lock;
slouken@1895
    65
    int safe;
slouken@0
    66
} SDL_EventLock;
slouken@0
    67
slouken@0
    68
/* Thread functions */
slouken@1895
    69
static SDL_Thread *SDL_EventThread = NULL;      /* Thread handle */
slouken@3578
    70
static SDL_threadID event_thread;     /* The event thread id */
slouken@0
    71
slouken@1895
    72
void
slouken@1895
    73
SDL_Lock_EventThread(void)
slouken@0
    74
{
slouken@1895
    75
    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
slouken@1895
    76
        /* Grab lock and spin until we're sure event thread stopped */
slouken@1895
    77
        SDL_mutexP(SDL_EventLock.lock);
slouken@1895
    78
        while (!SDL_EventLock.safe) {
slouken@1895
    79
            SDL_Delay(1);
slouken@1895
    80
        }
slouken@1895
    81
    }
slouken@0
    82
}
slouken@2735
    83
slouken@1895
    84
void
slouken@1895
    85
SDL_Unlock_EventThread(void)
slouken@0
    86
{
slouken@1895
    87
    if (SDL_EventThread && (SDL_ThreadID() != event_thread)) {
slouken@1895
    88
        SDL_mutexV(SDL_EventLock.lock);
slouken@1895
    89
    }
slouken@0
    90
}
slouken@0
    91
slouken@4429
    92
static __inline__ SDL_bool
slouken@4429
    93
SDL_ShouldPollJoystick()
slouken@4429
    94
{
paul@4698
    95
#if !SDL_JOYSTICK_DISABLED
slouken@4429
    96
    if (SDL_numjoysticks &&
slouken@4429
    97
        (!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] ||
slouken@4429
    98
         SDL_JoystickEventState(SDL_QUERY))) {
slouken@4429
    99
        return SDL_TRUE;
slouken@4429
   100
    }
paul@4698
   101
#endif
slouken@4429
   102
    return SDL_FALSE;
slouken@4429
   103
}
slouken@4429
   104
slouken@1895
   105
static int SDLCALL
slouken@1895
   106
SDL_GobbleEvents(void *unused)
slouken@0
   107
{
slouken@1895
   108
    event_thread = SDL_ThreadID();
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@4429
   119
        if (SDL_ShouldPollJoystick()) {
slouken@1895
   120
            SDL_JoystickUpdate();
slouken@1895
   121
        }
slouken@0
   122
#endif
slouken@0
   123
slouken@1895
   124
        /* Give up the CPU for the rest of our timeslice */
slouken@1895
   125
        SDL_EventLock.safe = 1;
slouken@1895
   126
        if (SDL_timer_running) {
slouken@1895
   127
            SDL_ThreadedTimerCheck();
slouken@1895
   128
        }
slouken@1895
   129
        SDL_Delay(1);
slouken@0
   130
slouken@1895
   131
        /* Check for event locking.
slouken@1895
   132
           On the P of the lock mutex, if the lock is held, this thread
slouken@1895
   133
           will wait until the lock is released before continuing.  The
slouken@1895
   134
           safe flag will be set, meaning that the other thread can go
slouken@1895
   135
           about it's business.  The safe flag is reset before the V,
slouken@1895
   136
           so as soon as the mutex is free, other threads can see that
slouken@1895
   137
           it's not safe to interfere with the event thread.
slouken@1895
   138
         */
slouken@1895
   139
        SDL_mutexP(SDL_EventLock.lock);
slouken@1895
   140
        SDL_EventLock.safe = 0;
slouken@1895
   141
        SDL_mutexV(SDL_EventLock.lock);
slouken@1895
   142
    }
slouken@1895
   143
    SDL_SetTimerThreaded(0);
slouken@1895
   144
    event_thread = 0;
slouken@1895
   145
    return (0);
slouken@0
   146
}
slouken@0
   147
slouken@1895
   148
static int
slouken@1895
   149
SDL_StartEventThread(Uint32 flags)
slouken@0
   150
{
slouken@1895
   151
    /* Reset everything to zero */
slouken@1895
   152
    SDL_EventThread = NULL;
slouken@1895
   153
    SDL_memset(&SDL_EventLock, 0, sizeof(SDL_EventLock));
slouken@0
   154
slouken@1895
   155
    /* Create the lock and set ourselves active */
slouken@1361
   156
#if !SDL_THREADS_DISABLED
slouken@1895
   157
    SDL_EventQ.lock = SDL_CreateMutex();
slouken@1895
   158
    if (SDL_EventQ.lock == NULL) {
slouken@1895
   159
        return (-1);
slouken@1895
   160
    }
slouken@1361
   161
#endif /* !SDL_THREADS_DISABLED */
slouken@1895
   162
    SDL_EventQ.active = 1;
slouken@0
   163
slouken@1895
   164
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@1895
   165
        SDL_EventLock.lock = SDL_CreateMutex();
slouken@1895
   166
        if (SDL_EventLock.lock == NULL) {
slouken@1895
   167
            return (-1);
slouken@1895
   168
        }
slouken@1895
   169
        SDL_EventLock.safe = 0;
slouken@0
   170
slouken@1895
   171
        /* The event thread will handle timers too */
slouken@1895
   172
        SDL_SetTimerThreaded(2);
slouken@1402
   173
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
slouken@1330
   174
#undef SDL_CreateThread
slouken@1895
   175
        SDL_EventThread =
slouken@1895
   176
            SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
slouken@1330
   177
#else
slouken@1895
   178
        SDL_EventThread = SDL_CreateThread(SDL_GobbleEvents, NULL);
slouken@1330
   179
#endif
slouken@1895
   180
        if (SDL_EventThread == NULL) {
slouken@1895
   181
            return (-1);
slouken@1895
   182
        }
slouken@1895
   183
    } else {
slouken@1895
   184
        event_thread = 0;
slouken@1895
   185
    }
slouken@1895
   186
    return (0);
slouken@0
   187
}
slouken@0
   188
slouken@1895
   189
static void
slouken@1895
   190
SDL_StopEventThread(void)
slouken@0
   191
{
slouken@1895
   192
    SDL_EventQ.active = 0;
slouken@1895
   193
    if (SDL_EventThread) {
slouken@1895
   194
        SDL_WaitThread(SDL_EventThread, NULL);
slouken@1895
   195
        SDL_EventThread = NULL;
slouken@1895
   196
        SDL_DestroyMutex(SDL_EventLock.lock);
icculus@3611
   197
        SDL_EventLock.lock = NULL;
slouken@1895
   198
    }
slouken@3259
   199
    if (SDL_EventQ.lock) {
slouken@3259
   200
        SDL_DestroyMutex(SDL_EventQ.lock);
slouken@3259
   201
        SDL_EventQ.lock = NULL;
slouken@3259
   202
    }
slouken@0
   203
}
slouken@0
   204
slouken@3578
   205
SDL_threadID
slouken@1895
   206
SDL_EventThreadID(void)
slouken@0
   207
{
slouken@1895
   208
    return (event_thread);
slouken@0
   209
}
slouken@0
   210
slouken@0
   211
/* Public functions */
slouken@0
   212
slouken@1895
   213
void
slouken@1895
   214
SDL_StopEventLoop(void)
slouken@0
   215
{
slouken@4429
   216
    int i;
slouken@4429
   217
slouken@1895
   218
    /* Halt the event thread, if running */
slouken@1895
   219
    SDL_StopEventThread();
slouken@0
   220
slouken@1895
   221
    /* Shutdown event handlers */
slouken@1895
   222
    SDL_KeyboardQuit();
slouken@1895
   223
    SDL_MouseQuit();
slouken@1895
   224
    SDL_QuitQuit();
slouken@1123
   225
slouken@1895
   226
    /* Clean out EventQ */
slouken@1895
   227
    SDL_EventQ.head = 0;
slouken@1895
   228
    SDL_EventQ.tail = 0;
slouken@1895
   229
    SDL_EventQ.wmmsg_next = 0;
slouken@4429
   230
slouken@4429
   231
    /* Clear disabled event state */
slouken@4429
   232
    for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
slouken@4429
   233
        if (SDL_disabled_events[i]) {
slouken@4429
   234
            SDL_free(SDL_disabled_events[i]);
slouken@4429
   235
            SDL_disabled_events[i] = NULL;
slouken@4429
   236
        }
slouken@4429
   237
    }
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
/* This function (and associated calls) may be called more than once */
slouken@1895
   241
int
slouken@1895
   242
SDL_StartEventLoop(Uint32 flags)
slouken@0
   243
{
slouken@1895
   244
    int retcode;
slouken@0
   245
slouken@1895
   246
    /* Clean out the event queue */
slouken@1895
   247
    SDL_EventThread = NULL;
slouken@1895
   248
    SDL_EventQ.lock = NULL;
slouken@1895
   249
    SDL_StopEventLoop();
slouken@0
   250
slouken@1895
   251
    /* No filter to start with, process most event types */
slouken@1895
   252
    SDL_EventOK = NULL;
slouken@4429
   253
    SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
slouken@0
   254
slouken@1895
   255
    /* Initialize event handlers */
slouken@1895
   256
    retcode = 0;
slouken@1895
   257
    retcode += SDL_KeyboardInit();
slouken@1895
   258
    retcode += SDL_MouseInit();
jim@4642
   259
    retcode += SDL_TouchInit();
slouken@1895
   260
    retcode += SDL_QuitInit();
slouken@1895
   261
    if (retcode < 0) {
slouken@1895
   262
        /* We don't expect them to fail, but... */
slouken@1895
   263
        return (-1);
slouken@1895
   264
    }
slouken@0
   265
slouken@1895
   266
    /* Create the lock and event thread */
slouken@1895
   267
    if (SDL_StartEventThread(flags) < 0) {
slouken@1895
   268
        SDL_StopEventLoop();
slouken@1895
   269
        return (-1);
slouken@1895
   270
    }
slouken@1895
   271
    return (0);
slouken@0
   272
}
slouken@0
   273
slouken@0
   274
slouken@0
   275
/* Add an event to the event queue -- called with the queue locked */
slouken@1895
   276
static int
slouken@1895
   277
SDL_AddEvent(SDL_Event * event)
slouken@0
   278
{
slouken@1895
   279
    int tail, added;
slouken@0
   280
slouken@1895
   281
    tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
slouken@1895
   282
    if (tail == SDL_EventQ.head) {
slouken@1895
   283
        /* Overflow, drop event */
slouken@1895
   284
        added = 0;
slouken@1895
   285
    } else {
slouken@1895
   286
        SDL_EventQ.event[SDL_EventQ.tail] = *event;
slouken@1895
   287
        if (event->type == SDL_SYSWMEVENT) {
slouken@1895
   288
            /* Note that it's possible to lose an event */
slouken@1895
   289
            int next = SDL_EventQ.wmmsg_next;
slouken@1895
   290
            SDL_EventQ.wmmsg[next] = *event->syswm.msg;
slouken@1895
   291
            SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
slouken@1895
   292
                &SDL_EventQ.wmmsg[next];
slouken@1895
   293
            SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
slouken@1895
   294
        }
slouken@1895
   295
        SDL_EventQ.tail = tail;
slouken@1895
   296
        added = 1;
slouken@1895
   297
    }
slouken@1895
   298
    return (added);
slouken@0
   299
}
slouken@0
   300
slouken@0
   301
/* Cut an event, and return the next valid spot, or the tail */
slouken@0
   302
/*                           -- called with the queue locked */
slouken@1895
   303
static int
slouken@1895
   304
SDL_CutEvent(int spot)
slouken@0
   305
{
slouken@1895
   306
    if (spot == SDL_EventQ.head) {
slouken@1895
   307
        SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
slouken@1895
   308
        return (SDL_EventQ.head);
slouken@1895
   309
    } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
slouken@1895
   310
        SDL_EventQ.tail = spot;
slouken@1895
   311
        return (SDL_EventQ.tail);
slouken@1895
   312
    } else
slouken@1895
   313
        /* We cut the middle -- shift everything over */
slouken@1895
   314
    {
slouken@1895
   315
        int here, next;
slouken@0
   316
slouken@1895
   317
        /* This can probably be optimized with SDL_memcpy() -- careful! */
slouken@1895
   318
        if (--SDL_EventQ.tail < 0) {
slouken@1895
   319
            SDL_EventQ.tail = MAXEVENTS - 1;
slouken@1895
   320
        }
slouken@1895
   321
        for (here = spot; here != SDL_EventQ.tail; here = next) {
slouken@1895
   322
            next = (here + 1) % MAXEVENTS;
slouken@1895
   323
            SDL_EventQ.event[here] = SDL_EventQ.event[next];
slouken@1895
   324
        }
slouken@1895
   325
        return (spot);
slouken@1895
   326
    }
slouken@1895
   327
    /* NOTREACHED */
slouken@0
   328
}
slouken@0
   329
slouken@0
   330
/* Lock the event queue, take a peep at it, and unlock it */
slouken@1895
   331
int
slouken@1895
   332
SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
slouken@4429
   333
               Uint32 minType, Uint32 maxType)
slouken@0
   334
{
slouken@1895
   335
    int i, used;
slouken@0
   336
slouken@1895
   337
    /* Don't look after we've quit */
slouken@1895
   338
    if (!SDL_EventQ.active) {
slouken@1895
   339
        return (-1);
slouken@1895
   340
    }
slouken@1895
   341
    /* Lock the event queue */
slouken@1895
   342
    used = 0;
slouken@1895
   343
    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
slouken@1895
   344
        if (action == SDL_ADDEVENT) {
slouken@1895
   345
            for (i = 0; i < numevents; ++i) {
slouken@1895
   346
                used += SDL_AddEvent(&events[i]);
slouken@1895
   347
            }
slouken@1895
   348
        } else {
slouken@1895
   349
            SDL_Event tmpevent;
slouken@1895
   350
            int spot;
slouken@0
   351
slouken@1895
   352
            /* If 'events' is NULL, just see if they exist */
slouken@1895
   353
            if (events == NULL) {
slouken@1895
   354
                action = SDL_PEEKEVENT;
slouken@1895
   355
                numevents = 1;
slouken@1895
   356
                events = &tmpevent;
slouken@1895
   357
            }
slouken@1895
   358
            spot = SDL_EventQ.head;
slouken@1895
   359
            while ((used < numevents) && (spot != SDL_EventQ.tail)) {
slouken@4429
   360
                Uint32 type = SDL_EventQ.event[spot].type;
slouken@4429
   361
                if (minType <= type && type <= maxType) {
slouken@1895
   362
                    events[used++] = SDL_EventQ.event[spot];
slouken@1895
   363
                    if (action == SDL_GETEVENT) {
slouken@1895
   364
                        spot = SDL_CutEvent(spot);
slouken@1895
   365
                    } else {
slouken@1895
   366
                        spot = (spot + 1) % MAXEVENTS;
slouken@1895
   367
                    }
slouken@1895
   368
                } else {
slouken@1895
   369
                    spot = (spot + 1) % MAXEVENTS;
slouken@1895
   370
                }
slouken@1895
   371
            }
slouken@1895
   372
        }
slouken@1895
   373
        SDL_mutexV(SDL_EventQ.lock);
slouken@1895
   374
    } else {
slouken@1895
   375
        SDL_SetError("Couldn't lock event queue");
slouken@1895
   376
        used = -1;
slouken@1895
   377
    }
slouken@1895
   378
    return (used);
slouken@1895
   379
}
slouken@1895
   380
slouken@1895
   381
SDL_bool
slouken@4429
   382
SDL_HasEvent(Uint32 type)
slouken@1895
   383
{
slouken@4429
   384
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0);
slouken@4429
   385
}
slouken@4429
   386
slouken@4429
   387
SDL_bool
slouken@4429
   388
SDL_HasEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   389
{
slouken@4429
   390
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0);
slouken@4429
   391
}
slouken@4429
   392
slouken@4429
   393
void
slouken@4429
   394
SDL_FlushEvent(Uint32 type)
slouken@4429
   395
{
slouken@4429
   396
    SDL_FlushEvents(type, type);
slouken@4429
   397
}
slouken@4429
   398
slouken@4429
   399
void
slouken@4429
   400
SDL_FlushEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   401
{
slouken@4429
   402
    /* Don't look after we've quit */
slouken@4429
   403
    if (!SDL_EventQ.active) {
slouken@4429
   404
        return;
slouken@4429
   405
    }
slouken@4429
   406
slouken@4460
   407
    /* Make sure the events are current */
slouken@4460
   408
    SDL_PumpEvents();
slouken@4460
   409
slouken@4429
   410
    /* Lock the event queue */
slouken@4429
   411
    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
slouken@4429
   412
        int spot = SDL_EventQ.head;
slouken@4429
   413
        while (spot != SDL_EventQ.tail) {
slouken@4429
   414
            Uint32 type = SDL_EventQ.event[spot].type;
slouken@4429
   415
            if (minType <= type && type <= maxType) {
slouken@4429
   416
                spot = SDL_CutEvent(spot);
slouken@4429
   417
            } else {
slouken@4429
   418
                spot = (spot + 1) % MAXEVENTS;
slouken@4429
   419
            }
slouken@4429
   420
        }
slouken@4429
   421
        SDL_mutexV(SDL_EventQ.lock);
slouken@4429
   422
    }
slouken@0
   423
}
slouken@0
   424
slouken@0
   425
/* Run the system dependent event loops */
slouken@1895
   426
void
slouken@1895
   427
SDL_PumpEvents(void)
slouken@0
   428
{
slouken@1895
   429
    if (!SDL_EventThread) {
slouken@1895
   430
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
   431
slouken@1895
   432
        /* Get events from the video subsystem */
slouken@1895
   433
        if (_this) {
slouken@1895
   434
            _this->PumpEvents(_this);
slouken@1895
   435
        }
slouken@1361
   436
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   437
        /* Check for joystick state change */
slouken@4429
   438
        if (SDL_ShouldPollJoystick()) {
slouken@1895
   439
            SDL_JoystickUpdate();
slouken@1895
   440
        }
slouken@0
   441
#endif
slouken@1895
   442
    }
slouken@0
   443
}
slouken@0
   444
slouken@0
   445
/* Public functions */
slouken@0
   446
slouken@1895
   447
int
slouken@1895
   448
SDL_PollEvent(SDL_Event * event)
slouken@0
   449
{
slouken@3072
   450
    return SDL_WaitEventTimeout(event, 0);
slouken@0
   451
}
slouken@0
   452
slouken@1895
   453
int
slouken@1895
   454
SDL_WaitEvent(SDL_Event * event)
slouken@0
   455
{
slouken@3072
   456
    return SDL_WaitEventTimeout(event, -1);
slouken@3072
   457
}
slouken@3072
   458
slouken@3072
   459
int
slouken@3072
   460
SDL_WaitEventTimeout(SDL_Event * event, int timeout)
slouken@3072
   461
{
slouken@3072
   462
    Uint32 expiration = 0;
slouken@3072
   463
slouken@3072
   464
    if (timeout > 0)
slouken@3072
   465
        expiration = SDL_GetTicks() + timeout;
slouken@3072
   466
slouken@3072
   467
    for (;;) {
slouken@1895
   468
        SDL_PumpEvents();
slouken@4429
   469
        switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
slouken@1895
   470
        case -1:
slouken@1895
   471
            return 0;
slouken@1895
   472
        case 1:
slouken@1895
   473
            return 1;
slouken@1895
   474
        case 0:
slouken@3072
   475
            if (timeout == 0) {
slouken@3072
   476
                /* Polling and no events, just return */
slouken@3072
   477
                return 0;
slouken@3072
   478
            }
slouken@3072
   479
            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
slouken@3072
   480
                /* Timeout expired and no events */
slouken@3072
   481
                return 0;
slouken@3072
   482
            }
slouken@1895
   483
            SDL_Delay(10);
slouken@3072
   484
            break;
slouken@1895
   485
        }
slouken@1895
   486
    }
slouken@0
   487
}
slouken@0
   488
slouken@1895
   489
int
slouken@1895
   490
SDL_PushEvent(SDL_Event * event)
slouken@0
   491
{
slouken@1895
   492
    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
slouken@1895
   493
        return 0;
slouken@1895
   494
    }
slouken@4429
   495
    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
slouken@1895
   496
        return -1;
slouken@1895
   497
    }
jim@4657
   498
jim@4657
   499
    SDL_GestureProcessEvent(event);
jim@4657
   500
    
jim@4657
   501
slouken@1895
   502
    return 1;
slouken@0
   503
}
slouken@0
   504
slouken@1895
   505
void
slouken@1895
   506
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
slouken@0
   507
{
slouken@1895
   508
    SDL_Event bitbucket;
slouken@0
   509
slouken@1895
   510
    /* Set filter and discard pending events */
slouken@1895
   511
    SDL_EventOK = filter;
slouken@1895
   512
    SDL_EventOKParam = userdata;
slouken@1895
   513
    while (SDL_PollEvent(&bitbucket) > 0);
slouken@0
   514
}
slouken@0
   515
slouken@1895
   516
SDL_bool
slouken@1895
   517
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
slouken@0
   518
{
slouken@1895
   519
    if (filter) {
slouken@1895
   520
        *filter = SDL_EventOK;
slouken@1895
   521
    }
slouken@1895
   522
    if (userdata) {
slouken@1895
   523
        *userdata = SDL_EventOKParam;
slouken@1895
   524
    }
slouken@1895
   525
    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
slouken@0
   526
}
slouken@0
   527
slouken@1895
   528
void
slouken@1895
   529
SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
slouken@0
   530
{
slouken@1895
   531
    if (SDL_mutexP(SDL_EventQ.lock) == 0) {
slouken@1895
   532
        int spot;
slouken@0
   533
slouken@1895
   534
        spot = SDL_EventQ.head;
slouken@1895
   535
        while (spot != SDL_EventQ.tail) {
slouken@1895
   536
            if (filter(userdata, &SDL_EventQ.event[spot])) {
slouken@1895
   537
                spot = (spot + 1) % MAXEVENTS;
slouken@1895
   538
            } else {
slouken@1895
   539
                spot = SDL_CutEvent(spot);
slouken@1895
   540
            }
slouken@1895
   541
        }
slouken@1895
   542
    }
slouken@1895
   543
    SDL_mutexV(SDL_EventQ.lock);
slouken@1895
   544
}
slouken@0
   545
slouken@1895
   546
Uint8
slouken@4429
   547
SDL_EventState(Uint32 type, int state)
slouken@1895
   548
{
slouken@1895
   549
    Uint8 current_state;
slouken@4429
   550
    Uint8 hi = ((type >> 8) & 0xff);
slouken@4429
   551
    Uint8 lo = (type & 0xff);
slouken@1895
   552
slouken@4429
   553
    if (SDL_disabled_events[hi] &&
slouken@4429
   554
        (SDL_disabled_events[hi]->bits[lo/32] & (1 << (lo&31)))) {
slouken@4429
   555
        current_state = SDL_DISABLE;
slouken@4429
   556
    } else {
slouken@4429
   557
        current_state = SDL_ENABLE;
slouken@1895
   558
    }
slouken@1895
   559
slouken@4429
   560
    if (state != current_state)
slouken@4429
   561
    {
slouken@4429
   562
        switch (state) {
slouken@4429
   563
        case SDL_DISABLE:
slouken@4429
   564
            /* Disable this event type and discard pending events */
slouken@4429
   565
            if (!SDL_disabled_events[hi]) {
slouken@4429
   566
                SDL_disabled_events[hi] = (SDL_DisabledEventBlock*) SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
slouken@4429
   567
                if (!SDL_disabled_events[hi]) {
slouken@4429
   568
                    /* Out of memory, nothing we can do... */
slouken@4429
   569
                    break;
slouken@4429
   570
                }
slouken@4429
   571
            }
slouken@4429
   572
            SDL_disabled_events[hi]->bits[lo/32] |= (1 << (lo&31));
slouken@4429
   573
            SDL_FlushEvent(type);
slouken@4429
   574
            break;
slouken@4429
   575
        case SDL_ENABLE:
slouken@4429
   576
            SDL_disabled_events[hi]->bits[lo/32] &= ~(1 << (lo&31));
slouken@4429
   577
            break;
slouken@4429
   578
        default:
slouken@4429
   579
            /* Querying state... */
slouken@4429
   580
            break;
slouken@1895
   581
        }
slouken@1895
   582
    }
slouken@4429
   583
slouken@4429
   584
    return current_state;
slouken@4429
   585
}
slouken@4429
   586
slouken@4429
   587
Uint32
slouken@4429
   588
SDL_RegisterEvents(int numevents)
slouken@4429
   589
{
slouken@4429
   590
    Uint32 event_base;
slouken@4429
   591
slouken@4429
   592
    if (SDL_userevents+numevents <= SDL_LASTEVENT) {
slouken@4429
   593
        event_base = SDL_userevents;
slouken@4429
   594
        SDL_userevents += numevents;
slouken@4429
   595
    } else {
slouken@4429
   596
        event_base = (Uint32)-1;
slouken@4429
   597
    }
slouken@4429
   598
    return event_base;
slouken@0
   599
}
slouken@0
   600
slouken@0
   601
/* This is a generic event handler.
slouken@0
   602
 */
slouken@1895
   603
int
slouken@1895
   604
SDL_SendSysWMEvent(SDL_SysWMmsg * message)
slouken@0
   605
{
slouken@1895
   606
    int posted;
slouken@0
   607
slouken@1895
   608
    posted = 0;
slouken@4429
   609
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1895
   610
        SDL_Event event;
slouken@1895
   611
        SDL_memset(&event, 0, sizeof(event));
slouken@1895
   612
        event.type = SDL_SYSWMEVENT;
slouken@1895
   613
        event.syswm.msg = message;
slouken@1895
   614
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   615
    }
slouken@1895
   616
    /* Update internal event state */
slouken@1895
   617
    return (posted);
slouken@0
   618
}
slouken@1895
   619
slouken@1895
   620
/* vi: set ts=4 sw=4 expandtab: */