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