src/events/SDL_events.c
author Ryan C. Gordon
Sun, 03 Jan 2016 06:50:50 -0500
changeset 10003 d91a2c45825e
parent 9998 f67cf37e9cd4
child 10060 739bc5c7d339
permissions -rw-r--r--
Remove almost all instances of "volatile" keyword.

As Tiffany pointed out in Bugzilla, volatile is not useful for thread safety:

https://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/

Some of these volatiles didn't need to be, some were otherwise protected by
spinlocks or mutexes, and some got moved over to SDL_atomic_t data, etc.

Fixes Bugzilla #3220.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* General event handling code for SDL */
slouken@0
    24
slouken@0
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_events.h"
slouken@0
    27
#include "SDL_syswm.h"
slouken@2984
    28
#include "SDL_thread.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@5123
    34
#include "../video/SDL_sysvideo.h"
slouken@0
    35
slouken@7306
    36
/* An arbitrary limit so we don't have unbounded growth */
slouken@7306
    37
#define SDL_MAX_QUEUED_EVENTS   65535
slouken@7306
    38
slouken@0
    39
/* Public data -- the event filter */
slouken@0
    40
SDL_EventFilter SDL_EventOK = NULL;
slouken@1895
    41
void *SDL_EventOKParam;
slouken@4429
    42
slouken@5146
    43
typedef struct SDL_EventWatcher {
slouken@5146
    44
    SDL_EventFilter callback;
slouken@5146
    45
    void *userdata;
slouken@5146
    46
    struct SDL_EventWatcher *next;
slouken@5146
    47
} SDL_EventWatcher;
slouken@5146
    48
slouken@5146
    49
static SDL_EventWatcher *SDL_event_watchers = NULL;
slouken@5146
    50
slouken@4429
    51
typedef struct {
slouken@4429
    52
    Uint32 bits[8];
slouken@4429
    53
} SDL_DisabledEventBlock;
slouken@4429
    54
slouken@4429
    55
static SDL_DisabledEventBlock *SDL_disabled_events[256];
slouken@4429
    56
static Uint32 SDL_userevents = SDL_USEREVENT;
slouken@0
    57
slouken@0
    58
/* Private data -- event queue */
slouken@7304
    59
typedef struct _SDL_EventEntry
slouken@7304
    60
{
slouken@7304
    61
    SDL_Event event;
slouken@7304
    62
    SDL_SysWMmsg msg;
slouken@7304
    63
    struct _SDL_EventEntry *prev;
slouken@7304
    64
    struct _SDL_EventEntry *next;
slouken@7304
    65
} SDL_EventEntry;
slouken@7304
    66
slouken@7304
    67
typedef struct _SDL_SysWMEntry
slouken@7304
    68
{
slouken@7304
    69
    SDL_SysWMmsg msg;
slouken@7304
    70
    struct _SDL_SysWMEntry *next;
slouken@7304
    71
} SDL_SysWMEntry;
slouken@7304
    72
slouken@1895
    73
static struct
slouken@1895
    74
{
slouken@1895
    75
    SDL_mutex *lock;
icculus@10003
    76
    SDL_atomic_t active;
icculus@10003
    77
    SDL_atomic_t count;
icculus@10003
    78
    int max_events_seen;
slouken@7304
    79
    SDL_EventEntry *head;
slouken@7304
    80
    SDL_EventEntry *tail;
slouken@7304
    81
    SDL_EventEntry *free;
slouken@7304
    82
    SDL_SysWMEntry *wmmsg_used;
slouken@7304
    83
    SDL_SysWMEntry *wmmsg_free;
icculus@10003
    84
} SDL_EventQ = { NULL, { 1 }, { 0 }, 0, NULL, NULL, NULL, NULL, NULL };
slouken@0
    85
slouken@0
    86
slouken@0
    87
/* Public functions */
slouken@0
    88
slouken@1895
    89
void
slouken@1895
    90
SDL_StopEventLoop(void)
slouken@0
    91
{
icculus@9437
    92
    const char *report = SDL_GetHint("SDL_EVENT_QUEUE_STATISTICS");
slouken@4429
    93
    int i;
slouken@7304
    94
    SDL_EventEntry *entry;
slouken@7304
    95
    SDL_SysWMEntry *wmmsg;
slouken@6495
    96
slouken@5123
    97
    if (SDL_EventQ.lock) {
slouken@7304
    98
        SDL_LockMutex(SDL_EventQ.lock);
slouken@5123
    99
    }
slouken@1123
   100
icculus@10003
   101
    SDL_AtomicSet(&SDL_EventQ.active, 0);
slouken@7304
   102
icculus@9437
   103
    if (report && SDL_atoi(report)) {
icculus@9437
   104
        SDL_Log("SDL EVENT QUEUE: Maximum events in-flight: %d\n",
icculus@9437
   105
                SDL_EventQ.max_events_seen);
icculus@9437
   106
    }
icculus@9437
   107
slouken@1895
   108
    /* Clean out EventQ */
slouken@7304
   109
    for (entry = SDL_EventQ.head; entry; ) {
slouken@7304
   110
        SDL_EventEntry *next = entry->next;
slouken@7304
   111
        SDL_free(entry);
slouken@7304
   112
        entry = next;
slouken@7304
   113
    }
slouken@7304
   114
    for (entry = SDL_EventQ.free; entry; ) {
slouken@7304
   115
        SDL_EventEntry *next = entry->next;
slouken@7304
   116
        SDL_free(entry);
slouken@7304
   117
        entry = next;
slouken@7304
   118
    }
slouken@7304
   119
    for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg; ) {
slouken@7304
   120
        SDL_SysWMEntry *next = wmmsg->next;
slouken@7304
   121
        SDL_free(wmmsg);
slouken@7304
   122
        wmmsg = next;
slouken@7304
   123
    }
slouken@7304
   124
    for (wmmsg = SDL_EventQ.wmmsg_free; wmmsg; ) {
slouken@7304
   125
        SDL_SysWMEntry *next = wmmsg->next;
slouken@7304
   126
        SDL_free(wmmsg);
slouken@7304
   127
        wmmsg = next;
slouken@7304
   128
    }
icculus@9437
   129
icculus@10003
   130
    SDL_AtomicSet(&SDL_EventQ.count, 0);
icculus@9437
   131
    SDL_EventQ.max_events_seen = 0;
slouken@7304
   132
    SDL_EventQ.head = NULL;
slouken@7304
   133
    SDL_EventQ.tail = NULL;
slouken@7304
   134
    SDL_EventQ.free = NULL;
slouken@7304
   135
    SDL_EventQ.wmmsg_used = NULL;
slouken@7304
   136
    SDL_EventQ.wmmsg_free = NULL;
slouken@4429
   137
slouken@4429
   138
    /* Clear disabled event state */
slouken@4429
   139
    for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
slouken@7719
   140
        SDL_free(SDL_disabled_events[i]);
slouken@7719
   141
        SDL_disabled_events[i] = NULL;
slouken@4429
   142
    }
slouken@5146
   143
slouken@5146
   144
    while (SDL_event_watchers) {
slouken@5146
   145
        SDL_EventWatcher *tmp = SDL_event_watchers;
slouken@5146
   146
        SDL_event_watchers = tmp->next;
slouken@5146
   147
        SDL_free(tmp);
slouken@5146
   148
    }
slouken@7190
   149
    SDL_EventOK = NULL;
slouken@7304
   150
slouken@7304
   151
    if (SDL_EventQ.lock) {
slouken@7304
   152
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@7304
   153
        SDL_DestroyMutex(SDL_EventQ.lock);
slouken@7304
   154
        SDL_EventQ.lock = NULL;
slouken@7304
   155
    }
slouken@0
   156
}
slouken@0
   157
slouken@0
   158
/* This function (and associated calls) may be called more than once */
slouken@1895
   159
int
slouken@5123
   160
SDL_StartEventLoop(void)
slouken@0
   161
{
slouken@6588
   162
    /* We'll leave the event queue alone, since we might have gotten
slouken@6588
   163
       some important events at launch (like SDL_DROPFILE)
slouken@6588
   164
slouken@6588
   165
       FIXME: Does this introduce any other bugs with events at startup?
slouken@6588
   166
     */
slouken@0
   167
slouken@5123
   168
    /* Create the lock and set ourselves active */
slouken@5123
   169
#if !SDL_THREADS_DISABLED
slouken@6588
   170
    if (!SDL_EventQ.lock) {
slouken@6588
   171
        SDL_EventQ.lock = SDL_CreateMutex();
slouken@6588
   172
    }
slouken@5123
   173
    if (SDL_EventQ.lock == NULL) {
icculus@10003
   174
        return -1;
slouken@1895
   175
    }
slouken@5123
   176
#endif /* !SDL_THREADS_DISABLED */
icculus@7077
   177
slouken@7190
   178
    /* Process most event types */
icculus@7077
   179
    SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
icculus@7077
   180
    SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
icculus@7077
   181
    SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
icculus@7077
   182
icculus@10003
   183
    SDL_AtomicSet(&SDL_EventQ.active, 1);
slouken@1895
   184
icculus@10003
   185
    return 0;
slouken@0
   186
}
slouken@0
   187
slouken@0
   188
slouken@0
   189
/* Add an event to the event queue -- called with the queue locked */
slouken@1895
   190
static int
slouken@1895
   191
SDL_AddEvent(SDL_Event * event)
slouken@0
   192
{
slouken@7304
   193
    SDL_EventEntry *entry;
icculus@10003
   194
    const int initial_count = SDL_AtomicGet(&SDL_EventQ.count);
icculus@10003
   195
    int final_count;
slouken@0
   196
icculus@10003
   197
    if (initial_count >= SDL_MAX_QUEUED_EVENTS) {
icculus@10003
   198
        SDL_SetError("Event queue is full (%d events)", initial_count);
slouken@7306
   199
        return 0;
slouken@7306
   200
    }
slouken@7306
   201
slouken@7304
   202
    if (SDL_EventQ.free == NULL) {
slouken@7304
   203
        entry = (SDL_EventEntry *)SDL_malloc(sizeof(*entry));
slouken@7304
   204
        if (!entry) {
slouken@7304
   205
            return 0;
slouken@7304
   206
        }
slouken@1895
   207
    } else {
slouken@7304
   208
        entry = SDL_EventQ.free;
slouken@7304
   209
        SDL_EventQ.free = entry->next;
slouken@7304
   210
    }
slouken@7304
   211
slouken@7304
   212
    entry->event = *event;
slouken@7304
   213
    if (event->type == SDL_SYSWMEVENT) {
slouken@7304
   214
        entry->msg = *event->syswm.msg;
slouken@7305
   215
        entry->event.syswm.msg = &entry->msg;
slouken@1895
   216
    }
slouken@7304
   217
slouken@7304
   218
    if (SDL_EventQ.tail) {
slouken@7304
   219
        SDL_EventQ.tail->next = entry;
slouken@7304
   220
        entry->prev = SDL_EventQ.tail;
slouken@7304
   221
        SDL_EventQ.tail = entry;
slouken@7304
   222
        entry->next = NULL;
slouken@7304
   223
    } else {
slouken@7304
   224
        SDL_assert(!SDL_EventQ.head);
slouken@7304
   225
        SDL_EventQ.head = entry;
slouken@7304
   226
        SDL_EventQ.tail = entry;
slouken@7304
   227
        entry->prev = NULL;
slouken@7304
   228
        entry->next = NULL;
slouken@7304
   229
    }
slouken@7304
   230
icculus@10003
   231
    final_count = SDL_AtomicAdd(&SDL_EventQ.count, 1) + 1;
icculus@10003
   232
    if (final_count > SDL_EventQ.max_events_seen) {
icculus@10003
   233
        SDL_EventQ.max_events_seen = final_count;
icculus@9437
   234
    }
icculus@9437
   235
slouken@7304
   236
    return 1;
slouken@0
   237
}
slouken@0
   238
slouken@7304
   239
/* Remove an event from the queue -- called with the queue locked */
slouken@7304
   240
static void
slouken@7304
   241
SDL_CutEvent(SDL_EventEntry *entry)
slouken@0
   242
{
slouken@7304
   243
    if (entry->prev) {
slouken@7304
   244
        entry->prev->next = entry->next;
slouken@7304
   245
    }
slouken@7304
   246
    if (entry->next) {
slouken@7304
   247
        entry->next->prev = entry->prev;
slouken@7304
   248
    }
slouken@0
   249
slouken@7304
   250
    if (entry == SDL_EventQ.head) {
slouken@7304
   251
        SDL_assert(entry->prev == NULL);
slouken@7304
   252
        SDL_EventQ.head = entry->next;
slouken@1895
   253
    }
slouken@7304
   254
    if (entry == SDL_EventQ.tail) {
slouken@7304
   255
        SDL_assert(entry->next == NULL);
slouken@7304
   256
        SDL_EventQ.tail = entry->prev;
slouken@7304
   257
    }
slouken@7304
   258
slouken@7304
   259
    entry->next = SDL_EventQ.free;
slouken@7304
   260
    SDL_EventQ.free = entry;
icculus@10003
   261
    SDL_assert(SDL_AtomicGet(&SDL_EventQ.count) > 0);
icculus@10003
   262
    SDL_AtomicAdd(&SDL_EventQ.count, -1);
slouken@0
   263
}
slouken@0
   264
slouken@0
   265
/* Lock the event queue, take a peep at it, and unlock it */
slouken@1895
   266
int
slouken@1895
   267
SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
slouken@4429
   268
               Uint32 minType, Uint32 maxType)
slouken@0
   269
{
slouken@1895
   270
    int i, used;
slouken@0
   271
slouken@1895
   272
    /* Don't look after we've quit */
icculus@10003
   273
    if (!SDL_AtomicGet(&SDL_EventQ.active)) {
slouken@7304
   274
        /* We get a few spurious events at shutdown, so don't warn then */
slouken@7304
   275
        if (action != SDL_ADDEVENT) {
slouken@7304
   276
            SDL_SetError("The event system has been shut down");
slouken@7304
   277
        }
slouken@1895
   278
        return (-1);
slouken@1895
   279
    }
slouken@1895
   280
    /* Lock the event queue */
slouken@1895
   281
    used = 0;
slouken@6977
   282
    if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@1895
   283
        if (action == SDL_ADDEVENT) {
slouken@1895
   284
            for (i = 0; i < numevents; ++i) {
slouken@1895
   285
                used += SDL_AddEvent(&events[i]);
slouken@1895
   286
            }
slouken@1895
   287
        } else {
slouken@7304
   288
            SDL_EventEntry *entry, *next;
slouken@7304
   289
            SDL_SysWMEntry *wmmsg, *wmmsg_next;
slouken@1895
   290
            SDL_Event tmpevent;
slouken@7304
   291
            Uint32 type;
slouken@0
   292
slouken@1895
   293
            /* If 'events' is NULL, just see if they exist */
slouken@1895
   294
            if (events == NULL) {
slouken@1895
   295
                action = SDL_PEEKEVENT;
slouken@1895
   296
                numevents = 1;
slouken@1895
   297
                events = &tmpevent;
slouken@1895
   298
            }
slouken@7304
   299
slouken@7304
   300
            /* Clean out any used wmmsg data
slouken@7304
   301
               FIXME: Do we want to retain the data for some period of time?
slouken@7304
   302
             */
slouken@7304
   303
            for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg; wmmsg = wmmsg_next) {
slouken@7304
   304
                wmmsg_next = wmmsg->next;
slouken@7304
   305
                wmmsg->next = SDL_EventQ.wmmsg_free;
slouken@7304
   306
                SDL_EventQ.wmmsg_free = wmmsg;
slouken@7304
   307
            }
slouken@7304
   308
            SDL_EventQ.wmmsg_used = NULL;
slouken@7304
   309
slouken@7304
   310
            for (entry = SDL_EventQ.head; entry && used < numevents; entry = next) {
slouken@7304
   311
                next = entry->next;
slouken@7304
   312
                type = entry->event.type;
slouken@4429
   313
                if (minType <= type && type <= maxType) {
slouken@7304
   314
                    events[used] = entry->event;
slouken@7304
   315
                    if (entry->event.type == SDL_SYSWMEVENT) {
slouken@7304
   316
                        /* We need to copy the wmmsg somewhere safe.
slouken@7304
   317
                           For now we'll guarantee it's valid at least until
slouken@7304
   318
                           the next call to SDL_PeepEvents()
slouken@7304
   319
                         */
slouken@7304
   320
                        if (SDL_EventQ.wmmsg_free) {
slouken@7304
   321
                            wmmsg = SDL_EventQ.wmmsg_free;
slouken@7304
   322
                            SDL_EventQ.wmmsg_free = wmmsg->next;
slouken@7304
   323
                        } else {
slouken@7304
   324
                            wmmsg = (SDL_SysWMEntry *)SDL_malloc(sizeof(*wmmsg));
slouken@7304
   325
                        }
slouken@7304
   326
                        wmmsg->msg = *entry->event.syswm.msg;
slouken@7304
   327
                        wmmsg->next = SDL_EventQ.wmmsg_used;
slouken@7304
   328
                        SDL_EventQ.wmmsg_used = wmmsg;
slouken@7304
   329
                        events[used].syswm.msg = &wmmsg->msg;
slouken@7304
   330
                    }
slouken@7304
   331
                    ++used;
slouken@7304
   332
slouken@1895
   333
                    if (action == SDL_GETEVENT) {
slouken@7304
   334
                        SDL_CutEvent(entry);
slouken@1895
   335
                    }
slouken@1895
   336
                }
slouken@1895
   337
            }
slouken@1895
   338
        }
slouken@6977
   339
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@1895
   340
    } else {
icculus@7037
   341
        return SDL_SetError("Couldn't lock event queue");
slouken@1895
   342
    }
slouken@1895
   343
    return (used);
slouken@1895
   344
}
slouken@1895
   345
slouken@1895
   346
SDL_bool
slouken@4429
   347
SDL_HasEvent(Uint32 type)
slouken@4429
   348
{
slouken@4429
   349
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0);
slouken@4429
   350
}
slouken@4429
   351
slouken@4429
   352
SDL_bool
slouken@4429
   353
SDL_HasEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   354
{
slouken@4429
   355
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0);
slouken@4429
   356
}
slouken@4429
   357
slouken@4429
   358
void
slouken@4429
   359
SDL_FlushEvent(Uint32 type)
slouken@1895
   360
{
slouken@4429
   361
    SDL_FlushEvents(type, type);
slouken@4429
   362
}
slouken@4429
   363
slouken@4429
   364
void
slouken@4429
   365
SDL_FlushEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   366
{
slouken@4429
   367
    /* Don't look after we've quit */
icculus@10003
   368
    if (!SDL_AtomicGet(&SDL_EventQ.active)) {
slouken@4429
   369
        return;
slouken@4429
   370
    }
slouken@4429
   371
slouken@4460
   372
    /* Make sure the events are current */
slouken@5293
   373
#if 0
slouken@5293
   374
    /* Actually, we can't do this since we might be flushing while processing
slouken@5293
   375
       a resize event, and calling this might trigger further resize events.
slouken@5293
   376
    */
slouken@4460
   377
    SDL_PumpEvents();
slouken@5293
   378
#endif
slouken@4460
   379
slouken@4429
   380
    /* Lock the event queue */
slouken@6977
   381
    if (SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@7304
   382
        SDL_EventEntry *entry, *next;
slouken@7304
   383
        Uint32 type;
slouken@7304
   384
        for (entry = SDL_EventQ.head; entry; entry = next) {
slouken@7304
   385
            next = entry->next;
slouken@7304
   386
            type = entry->event.type;
slouken@4429
   387
            if (minType <= type && type <= maxType) {
slouken@7304
   388
                SDL_CutEvent(entry);
slouken@4429
   389
            }
slouken@4429
   390
        }
slouken@6977
   391
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@4429
   392
    }
slouken@0
   393
}
slouken@0
   394
slouken@0
   395
/* Run the system dependent event loops */
slouken@1895
   396
void
slouken@1895
   397
SDL_PumpEvents(void)
slouken@0
   398
{
slouken@5123
   399
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
   400
slouken@5123
   401
    /* Get events from the video subsystem */
slouken@5123
   402
    if (_this) {
slouken@5123
   403
        _this->PumpEvents(_this);
slouken@5123
   404
    }
slouken@1361
   405
#if !SDL_JOYSTICK_DISABLED
slouken@5123
   406
    /* Check for joystick state change */
icculus@8860
   407
    if ((!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] || SDL_JoystickEventState(SDL_QUERY))) {
slouken@5123
   408
        SDL_JoystickUpdate();
slouken@5123
   409
    }
slouken@0
   410
#endif
icculus@9720
   411
icculus@9720
   412
    SDL_SendPendingQuit();  /* in case we had a signal handler fire, etc. */
slouken@0
   413
}
slouken@0
   414
slouken@0
   415
/* Public functions */
slouken@0
   416
slouken@1895
   417
int
slouken@1895
   418
SDL_PollEvent(SDL_Event * event)
slouken@0
   419
{
slouken@3072
   420
    return SDL_WaitEventTimeout(event, 0);
slouken@1895
   421
}
slouken@0
   422
slouken@1895
   423
int
slouken@1895
   424
SDL_WaitEvent(SDL_Event * event)
slouken@1895
   425
{
slouken@3072
   426
    return SDL_WaitEventTimeout(event, -1);
slouken@3072
   427
}
slouken@3072
   428
slouken@3072
   429
int
slouken@3072
   430
SDL_WaitEventTimeout(SDL_Event * event, int timeout)
slouken@3072
   431
{
slouken@3072
   432
    Uint32 expiration = 0;
slouken@3072
   433
slouken@3072
   434
    if (timeout > 0)
slouken@3072
   435
        expiration = SDL_GetTicks() + timeout;
slouken@3072
   436
slouken@3072
   437
    for (;;) {
slouken@1895
   438
        SDL_PumpEvents();
slouken@4429
   439
        switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
slouken@1895
   440
        case -1:
slouken@1895
   441
            return 0;
slouken@1895
   442
        case 1:
slouken@1895
   443
            return 1;
slouken@1895
   444
        case 0:
slouken@3072
   445
            if (timeout == 0) {
slouken@3072
   446
                /* Polling and no events, just return */
slouken@3072
   447
                return 0;
slouken@3072
   448
            }
slouken@7857
   449
            if (timeout > 0 && SDL_TICKS_PASSED(SDL_GetTicks(), expiration)) {
slouken@3072
   450
                /* Timeout expired and no events */
slouken@3072
   451
                return 0;
slouken@3072
   452
            }
slouken@1895
   453
            SDL_Delay(10);
slouken@3072
   454
            break;
slouken@1895
   455
        }
slouken@1895
   456
    }
slouken@0
   457
}
slouken@0
   458
slouken@1895
   459
int
slouken@1895
   460
SDL_PushEvent(SDL_Event * event)
slouken@0
   461
{
slouken@5146
   462
    SDL_EventWatcher *curr;
slouken@7190
   463
dludwig@7082
   464
    event->common.timestamp = SDL_GetTicks();
slouken@7190
   465
slouken@1895
   466
    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
slouken@1895
   467
        return 0;
slouken@1895
   468
    }
slouken@5146
   469
slouken@5146
   470
    for (curr = SDL_event_watchers; curr; curr = curr->next) {
slouken@5146
   471
        curr->callback(curr->userdata, event);
slouken@5146
   472
    }
slouken@5146
   473
slouken@4429
   474
    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
slouken@1895
   475
        return -1;
slouken@1895
   476
    }
jim@4657
   477
jim@4657
   478
    SDL_GestureProcessEvent(event);
jim@4657
   479
slouken@1895
   480
    return 1;
slouken@0
   481
}
slouken@0
   482
slouken@1895
   483
void
slouken@1895
   484
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
slouken@0
   485
{
slouken@1895
   486
    /* Set filter and discard pending events */
slouken@7201
   487
    SDL_EventOK = NULL;
slouken@7201
   488
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
slouken@1895
   489
    SDL_EventOKParam = userdata;
slouken@7201
   490
    SDL_EventOK = filter;
slouken@0
   491
}
slouken@0
   492
slouken@1895
   493
SDL_bool
slouken@1895
   494
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
slouken@0
   495
{
slouken@1895
   496
    if (filter) {
slouken@1895
   497
        *filter = SDL_EventOK;
slouken@1895
   498
    }
slouken@1895
   499
    if (userdata) {
slouken@1895
   500
        *userdata = SDL_EventOKParam;
slouken@1895
   501
    }
slouken@1895
   502
    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
slouken@0
   503
}
slouken@0
   504
slouken@5146
   505
/* FIXME: This is not thread-safe yet */
slouken@5146
   506
void
slouken@5146
   507
SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
slouken@5146
   508
{
slouken@8234
   509
    SDL_EventWatcher *watcher, *tail;
slouken@5146
   510
slouken@5146
   511
    watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
slouken@5146
   512
    if (!watcher) {
slouken@5146
   513
        /* Uh oh... */
slouken@5146
   514
        return;
slouken@5146
   515
    }
slouken@8234
   516
slouken@8234
   517
    /* create the watcher */
slouken@5146
   518
    watcher->callback = filter;
slouken@5146
   519
    watcher->userdata = userdata;
slouken@8234
   520
    watcher->next = NULL;
slouken@8234
   521
slouken@8234
   522
    /* add the watcher to the end of the list */
slouken@8234
   523
    if (SDL_event_watchers) {
slouken@8234
   524
        for (tail = SDL_event_watchers; tail->next; tail = tail->next) {
slouken@8234
   525
            continue;
slouken@8234
   526
        }
slouken@8234
   527
        tail->next = watcher;
slouken@8234
   528
    } else {
slouken@8234
   529
        SDL_event_watchers = watcher;
slouken@8234
   530
    }
slouken@5146
   531
}
slouken@5146
   532
slouken@5146
   533
/* FIXME: This is not thread-safe yet */
slouken@5146
   534
void
slouken@5146
   535
SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
slouken@5146
   536
{
slouken@5146
   537
    SDL_EventWatcher *prev = NULL;
slouken@5146
   538
    SDL_EventWatcher *curr;
slouken@5146
   539
slouken@5146
   540
    for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
slouken@5146
   541
        if (curr->callback == filter && curr->userdata == userdata) {
slouken@5146
   542
            if (prev) {
slouken@5146
   543
                prev->next = curr->next;
slouken@5146
   544
            } else {
slouken@5146
   545
                SDL_event_watchers = curr->next;
slouken@5146
   546
            }
slouken@5146
   547
            SDL_free(curr);
slouken@5146
   548
            break;
slouken@5146
   549
        }
slouken@5146
   550
    }
slouken@5146
   551
}
slouken@5146
   552
slouken@1895
   553
void
slouken@1895
   554
SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
slouken@0
   555
{
slouken@8791
   556
    if (SDL_EventQ.lock && SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@7304
   557
        SDL_EventEntry *entry, *next;
slouken@7304
   558
        for (entry = SDL_EventQ.head; entry; entry = next) {
slouken@7304
   559
            next = entry->next;
slouken@7304
   560
            if (!filter(userdata, &entry->event)) {
slouken@7304
   561
                SDL_CutEvent(entry);
slouken@1895
   562
            }
slouken@1895
   563
        }
slouken@7304
   564
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@1895
   565
    }
slouken@1895
   566
}
slouken@1895
   567
slouken@1895
   568
Uint8
slouken@4429
   569
SDL_EventState(Uint32 type, int state)
slouken@1895
   570
{
slouken@1895
   571
    Uint8 current_state;
slouken@4429
   572
    Uint8 hi = ((type >> 8) & 0xff);
slouken@4429
   573
    Uint8 lo = (type & 0xff);
slouken@0
   574
slouken@4429
   575
    if (SDL_disabled_events[hi] &&
slouken@4429
   576
        (SDL_disabled_events[hi]->bits[lo/32] & (1 << (lo&31)))) {
slouken@4429
   577
        current_state = SDL_DISABLE;
slouken@4429
   578
    } else {
slouken@4429
   579
        current_state = SDL_ENABLE;
slouken@1895
   580
    }
slouken@0
   581
slouken@4429
   582
    if (state != current_state)
slouken@4429
   583
    {
slouken@4429
   584
        switch (state) {
slouken@4429
   585
        case SDL_DISABLE:
slouken@4429
   586
            /* Disable this event type and discard pending events */
slouken@4429
   587
            if (!SDL_disabled_events[hi]) {
slouken@4429
   588
                SDL_disabled_events[hi] = (SDL_DisabledEventBlock*) SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
slouken@4429
   589
                if (!SDL_disabled_events[hi]) {
slouken@4429
   590
                    /* Out of memory, nothing we can do... */
slouken@4429
   591
                    break;
slouken@4429
   592
                }
slouken@4429
   593
            }
slouken@4429
   594
            SDL_disabled_events[hi]->bits[lo/32] |= (1 << (lo&31));
slouken@4429
   595
            SDL_FlushEvent(type);
slouken@4429
   596
            break;
slouken@4429
   597
        case SDL_ENABLE:
slouken@4429
   598
            SDL_disabled_events[hi]->bits[lo/32] &= ~(1 << (lo&31));
slouken@4429
   599
            break;
slouken@4429
   600
        default:
slouken@4429
   601
            /* Querying state... */
slouken@4429
   602
            break;
slouken@1895
   603
        }
slouken@1895
   604
    }
slouken@4429
   605
slouken@4429
   606
    return current_state;
slouken@4429
   607
}
slouken@4429
   608
slouken@4429
   609
Uint32
slouken@4429
   610
SDL_RegisterEvents(int numevents)
slouken@4429
   611
{
slouken@4429
   612
    Uint32 event_base;
slouken@4429
   613
icculus@7611
   614
    if ((numevents > 0) && (SDL_userevents+numevents <= SDL_LASTEVENT)) {
slouken@4429
   615
        event_base = SDL_userevents;
slouken@4429
   616
        SDL_userevents += numevents;
slouken@4429
   617
    } else {
slouken@4429
   618
        event_base = (Uint32)-1;
slouken@4429
   619
    }
slouken@4429
   620
    return event_base;
slouken@0
   621
}
slouken@0
   622
slouken@7190
   623
int
slouken@7190
   624
SDL_SendAppEvent(SDL_EventType eventType)
slouken@7190
   625
{
slouken@7190
   626
    int posted;
slouken@7190
   627
slouken@7190
   628
    posted = 0;
slouken@7190
   629
    if (SDL_GetEventState(eventType) == SDL_ENABLE) {
slouken@7190
   630
        SDL_Event event;
slouken@7190
   631
        event.type = eventType;
slouken@7190
   632
        posted = (SDL_PushEvent(&event) > 0);
slouken@7190
   633
    }
slouken@7190
   634
    return (posted);
slouken@7190
   635
}
slouken@7190
   636
slouken@1895
   637
int
slouken@1895
   638
SDL_SendSysWMEvent(SDL_SysWMmsg * message)
slouken@0
   639
{
slouken@1895
   640
    int posted;
slouken@0
   641
slouken@1895
   642
    posted = 0;
slouken@4429
   643
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1895
   644
        SDL_Event event;
slouken@1895
   645
        SDL_memset(&event, 0, sizeof(event));
slouken@1895
   646
        event.type = SDL_SYSWMEVENT;
slouken@1895
   647
        event.syswm.msg = message;
slouken@1895
   648
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   649
    }
slouken@1895
   650
    /* Update internal event state */
slouken@1895
   651
    return (posted);
slouken@0
   652
}
slouken@1895
   653
slouken@9898
   654
int
slouken@9898
   655
SDL_SendKeymapChangedEvent(void)
slouken@9898
   656
{
slouken@9898
   657
    return SDL_SendAppEvent(SDL_KEYMAPCHANGED);
slouken@9898
   658
}
slouken@9898
   659
slouken@1895
   660
/* vi: set ts=4 sw=4 expandtab: */