src/events/SDL_events.c
author David Ludwig
Sun, 21 Apr 2013 12:38:44 -0400
changeset 8464 a2a909304cfe
parent 8461 ca8dad9be3d4
parent 7082 e9d6004d2978
child 8465 be2dce468a8c
permissions -rw-r--r--
WinRT: merged with latest, official, SDL 2.x sources
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
dludwig@8461
    36
/* HACK: Make sure the 'generic' field in SDL_Event works on
dludwig@8461
    37
   WinRT, whereby 'generic' is redefined as 'generic_', in order to
dludwig@8461
    38
   allow SDL.h to be included in code compiled with Microsoft's
dludwig@8461
    39
   C++/CX extension.
dludwig@8461
    40
*/
dludwig@8461
    41
#if defined(__WINRT__)
dludwig@8461
    42
#define generic generic_
dludwig@8461
    43
#endif
dludwig@8461
    44
slouken@0
    45
/* Public data -- the event filter */
slouken@0
    46
SDL_EventFilter SDL_EventOK = NULL;
slouken@1895
    47
void *SDL_EventOKParam;
slouken@4429
    48
slouken@5146
    49
typedef struct SDL_EventWatcher {
slouken@5146
    50
    SDL_EventFilter callback;
slouken@5146
    51
    void *userdata;
slouken@5146
    52
    struct SDL_EventWatcher *next;
slouken@5146
    53
} SDL_EventWatcher;
slouken@5146
    54
slouken@5146
    55
static SDL_EventWatcher *SDL_event_watchers = NULL;
slouken@5146
    56
slouken@4429
    57
typedef struct {
slouken@4429
    58
    Uint32 bits[8];
slouken@4429
    59
} SDL_DisabledEventBlock;
slouken@4429
    60
slouken@4429
    61
static SDL_DisabledEventBlock *SDL_disabled_events[256];
slouken@4429
    62
static Uint32 SDL_userevents = SDL_USEREVENT;
slouken@0
    63
slouken@0
    64
/* Private data -- event queue */
slouken@0
    65
#define MAXEVENTS	128
slouken@1895
    66
static struct
slouken@1895
    67
{
slouken@1895
    68
    SDL_mutex *lock;
slouken@1895
    69
    int active;
slouken@1895
    70
    int head;
slouken@1895
    71
    int tail;
slouken@1895
    72
    SDL_Event event[MAXEVENTS];
slouken@1895
    73
    int wmmsg_next;
slouken@1895
    74
    struct SDL_SysWMmsg wmmsg[MAXEVENTS];
slouken@6588
    75
} SDL_EventQ = { NULL, 1 };
slouken@0
    76
slouken@0
    77
slouken@4429
    78
static __inline__ SDL_bool
slouken@4429
    79
SDL_ShouldPollJoystick()
slouken@4429
    80
{
paul@4698
    81
#if !SDL_JOYSTICK_DISABLED
slouken@6753
    82
    if ((!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] ||
slouken@6753
    83
         SDL_JoystickEventState(SDL_QUERY)) &&
slouken@6753
    84
        SDL_PrivateJoystickNeedsPolling()) {
slouken@4429
    85
        return SDL_TRUE;
slouken@4429
    86
    }
paul@4698
    87
#endif
slouken@4429
    88
    return SDL_FALSE;
slouken@4429
    89
}
slouken@4429
    90
slouken@0
    91
/* Public functions */
slouken@0
    92
slouken@1895
    93
void
slouken@1895
    94
SDL_StopEventLoop(void)
slouken@0
    95
{
slouken@4429
    96
    int i;
slouken@4429
    97
slouken@6495
    98
    SDL_EventQ.active = 0;
slouken@6495
    99
slouken@5123
   100
    if (SDL_EventQ.lock) {
slouken@5123
   101
        SDL_DestroyMutex(SDL_EventQ.lock);
slouken@5123
   102
        SDL_EventQ.lock = NULL;
slouken@5123
   103
    }
slouken@1123
   104
slouken@1895
   105
    /* Clean out EventQ */
slouken@1895
   106
    SDL_EventQ.head = 0;
slouken@1895
   107
    SDL_EventQ.tail = 0;
slouken@1895
   108
    SDL_EventQ.wmmsg_next = 0;
slouken@4429
   109
slouken@4429
   110
    /* Clear disabled event state */
slouken@4429
   111
    for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
slouken@4429
   112
        if (SDL_disabled_events[i]) {
slouken@4429
   113
            SDL_free(SDL_disabled_events[i]);
slouken@4429
   114
            SDL_disabled_events[i] = NULL;
slouken@4429
   115
        }
slouken@4429
   116
    }
slouken@5146
   117
slouken@5146
   118
    while (SDL_event_watchers) {
slouken@5146
   119
        SDL_EventWatcher *tmp = SDL_event_watchers;
slouken@5146
   120
        SDL_event_watchers = tmp->next;
slouken@5146
   121
        SDL_free(tmp);
slouken@5146
   122
    }
slouken@0
   123
}
slouken@0
   124
slouken@0
   125
/* This function (and associated calls) may be called more than once */
slouken@1895
   126
int
slouken@5123
   127
SDL_StartEventLoop(void)
slouken@0
   128
{
slouken@6588
   129
    /* We'll leave the event queue alone, since we might have gotten
slouken@6588
   130
       some important events at launch (like SDL_DROPFILE)
slouken@6588
   131
slouken@6588
   132
       FIXME: Does this introduce any other bugs with events at startup?
slouken@6588
   133
     */
slouken@0
   134
slouken@5123
   135
    /* Create the lock and set ourselves active */
slouken@5123
   136
#if !SDL_THREADS_DISABLED
slouken@6588
   137
    if (!SDL_EventQ.lock) {
slouken@6588
   138
        SDL_EventQ.lock = SDL_CreateMutex();
slouken@6588
   139
    }
slouken@5123
   140
    if (SDL_EventQ.lock == NULL) {
slouken@1895
   141
        return (-1);
slouken@1895
   142
    }
slouken@5123
   143
#endif /* !SDL_THREADS_DISABLED */
icculus@7077
   144
icculus@7077
   145
    /* No filter to start with, process most event types */
icculus@7077
   146
    SDL_EventOK = NULL;
icculus@7077
   147
    SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
icculus@7077
   148
    SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
icculus@7077
   149
    SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
icculus@7077
   150
slouken@5123
   151
    SDL_EventQ.active = 1;
slouken@1895
   152
slouken@1895
   153
    return (0);
slouken@0
   154
}
slouken@0
   155
slouken@0
   156
slouken@0
   157
/* Add an event to the event queue -- called with the queue locked */
slouken@1895
   158
static int
slouken@1895
   159
SDL_AddEvent(SDL_Event * event)
slouken@0
   160
{
slouken@1895
   161
    int tail, added;
slouken@0
   162
slouken@1895
   163
    tail = (SDL_EventQ.tail + 1) % MAXEVENTS;
slouken@1895
   164
    if (tail == SDL_EventQ.head) {
slouken@1895
   165
        /* Overflow, drop event */
slouken@1895
   166
        added = 0;
slouken@1895
   167
    } else {
slouken@1895
   168
        SDL_EventQ.event[SDL_EventQ.tail] = *event;
slouken@1895
   169
        if (event->type == SDL_SYSWMEVENT) {
slouken@1895
   170
            /* Note that it's possible to lose an event */
slouken@1895
   171
            int next = SDL_EventQ.wmmsg_next;
slouken@1895
   172
            SDL_EventQ.wmmsg[next] = *event->syswm.msg;
slouken@1895
   173
            SDL_EventQ.event[SDL_EventQ.tail].syswm.msg =
slouken@1895
   174
                &SDL_EventQ.wmmsg[next];
slouken@1895
   175
            SDL_EventQ.wmmsg_next = (next + 1) % MAXEVENTS;
slouken@1895
   176
        }
slouken@1895
   177
        SDL_EventQ.tail = tail;
slouken@1895
   178
        added = 1;
slouken@1895
   179
    }
slouken@1895
   180
    return (added);
slouken@0
   181
}
slouken@0
   182
slouken@0
   183
/* Cut an event, and return the next valid spot, or the tail */
slouken@0
   184
/*                           -- called with the queue locked */
slouken@1895
   185
static int
slouken@1895
   186
SDL_CutEvent(int spot)
slouken@0
   187
{
slouken@1895
   188
    if (spot == SDL_EventQ.head) {
slouken@1895
   189
        SDL_EventQ.head = (SDL_EventQ.head + 1) % MAXEVENTS;
slouken@1895
   190
        return (SDL_EventQ.head);
slouken@1895
   191
    } else if ((spot + 1) % MAXEVENTS == SDL_EventQ.tail) {
slouken@1895
   192
        SDL_EventQ.tail = spot;
slouken@1895
   193
        return (SDL_EventQ.tail);
slouken@1895
   194
    } else
slouken@1895
   195
        /* We cut the middle -- shift everything over */
slouken@1895
   196
    {
slouken@1895
   197
        int here, next;
slouken@0
   198
slouken@1895
   199
        /* This can probably be optimized with SDL_memcpy() -- careful! */
slouken@1895
   200
        if (--SDL_EventQ.tail < 0) {
slouken@1895
   201
            SDL_EventQ.tail = MAXEVENTS - 1;
slouken@1895
   202
        }
slouken@1895
   203
        for (here = spot; here != SDL_EventQ.tail; here = next) {
slouken@1895
   204
            next = (here + 1) % MAXEVENTS;
slouken@1895
   205
            SDL_EventQ.event[here] = SDL_EventQ.event[next];
slouken@1895
   206
        }
slouken@1895
   207
        return (spot);
slouken@1895
   208
    }
slouken@1895
   209
    /* NOTREACHED */
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
/* Lock the event queue, take a peep at it, and unlock it */
slouken@1895
   213
int
slouken@1895
   214
SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
slouken@4429
   215
               Uint32 minType, Uint32 maxType)
slouken@0
   216
{
slouken@1895
   217
    int i, used;
slouken@0
   218
slouken@1895
   219
    /* Don't look after we've quit */
slouken@1895
   220
    if (!SDL_EventQ.active) {
slouken@1895
   221
        return (-1);
slouken@1895
   222
    }
slouken@1895
   223
    /* Lock the event queue */
slouken@1895
   224
    used = 0;
slouken@6977
   225
    if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@1895
   226
        if (action == SDL_ADDEVENT) {
slouken@1895
   227
            for (i = 0; i < numevents; ++i) {
slouken@1895
   228
                used += SDL_AddEvent(&events[i]);
slouken@1895
   229
            }
slouken@1895
   230
        } else {
slouken@1895
   231
            SDL_Event tmpevent;
slouken@1895
   232
            int spot;
slouken@0
   233
slouken@1895
   234
            /* If 'events' is NULL, just see if they exist */
slouken@1895
   235
            if (events == NULL) {
slouken@1895
   236
                action = SDL_PEEKEVENT;
slouken@1895
   237
                numevents = 1;
slouken@1895
   238
                events = &tmpevent;
slouken@1895
   239
            }
slouken@1895
   240
            spot = SDL_EventQ.head;
slouken@1895
   241
            while ((used < numevents) && (spot != SDL_EventQ.tail)) {
slouken@4429
   242
                Uint32 type = SDL_EventQ.event[spot].type;
slouken@4429
   243
                if (minType <= type && type <= maxType) {
slouken@1895
   244
                    events[used++] = SDL_EventQ.event[spot];
slouken@1895
   245
                    if (action == SDL_GETEVENT) {
slouken@1895
   246
                        spot = SDL_CutEvent(spot);
slouken@1895
   247
                    } else {
slouken@1895
   248
                        spot = (spot + 1) % MAXEVENTS;
slouken@1895
   249
                    }
slouken@1895
   250
                } else {
slouken@1895
   251
                    spot = (spot + 1) % MAXEVENTS;
slouken@1895
   252
                }
slouken@1895
   253
            }
slouken@1895
   254
        }
slouken@6977
   255
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@1895
   256
    } else {
icculus@7037
   257
        return SDL_SetError("Couldn't lock event queue");
slouken@1895
   258
    }
slouken@1895
   259
    return (used);
slouken@1895
   260
}
slouken@1895
   261
slouken@1895
   262
SDL_bool
slouken@4429
   263
SDL_HasEvent(Uint32 type)
slouken@4429
   264
{
slouken@4429
   265
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0);
slouken@4429
   266
}
slouken@4429
   267
slouken@4429
   268
SDL_bool
slouken@4429
   269
SDL_HasEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   270
{
slouken@4429
   271
    return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0);
slouken@4429
   272
}
slouken@4429
   273
slouken@4429
   274
void
slouken@4429
   275
SDL_FlushEvent(Uint32 type)
slouken@1895
   276
{
slouken@4429
   277
    SDL_FlushEvents(type, type);
slouken@4429
   278
}
slouken@4429
   279
slouken@4429
   280
void
slouken@4429
   281
SDL_FlushEvents(Uint32 minType, Uint32 maxType)
slouken@4429
   282
{
slouken@4429
   283
    /* Don't look after we've quit */
slouken@4429
   284
    if (!SDL_EventQ.active) {
slouken@4429
   285
        return;
slouken@4429
   286
    }
slouken@4429
   287
slouken@4460
   288
    /* Make sure the events are current */
slouken@5293
   289
#if 0
slouken@5293
   290
    /* Actually, we can't do this since we might be flushing while processing
slouken@5293
   291
       a resize event, and calling this might trigger further resize events.
slouken@5293
   292
    */
slouken@4460
   293
    SDL_PumpEvents();
slouken@5293
   294
#endif
slouken@4460
   295
slouken@4429
   296
    /* Lock the event queue */
slouken@6977
   297
    if (SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@4429
   298
        int spot = SDL_EventQ.head;
slouken@4429
   299
        while (spot != SDL_EventQ.tail) {
slouken@4429
   300
            Uint32 type = SDL_EventQ.event[spot].type;
slouken@4429
   301
            if (minType <= type && type <= maxType) {
slouken@4429
   302
                spot = SDL_CutEvent(spot);
slouken@4429
   303
            } else {
slouken@4429
   304
                spot = (spot + 1) % MAXEVENTS;
slouken@4429
   305
            }
slouken@4429
   306
        }
slouken@6977
   307
        SDL_UnlockMutex(SDL_EventQ.lock);
slouken@4429
   308
    }
slouken@0
   309
}
slouken@0
   310
slouken@0
   311
/* Run the system dependent event loops */
slouken@1895
   312
void
slouken@1895
   313
SDL_PumpEvents(void)
slouken@0
   314
{
slouken@5123
   315
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
   316
slouken@5123
   317
    /* Get events from the video subsystem */
slouken@5123
   318
    if (_this) {
slouken@5123
   319
        _this->PumpEvents(_this);
slouken@5123
   320
    }
slouken@1361
   321
#if !SDL_JOYSTICK_DISABLED
slouken@5123
   322
    /* Check for joystick state change */
slouken@5123
   323
    if (SDL_ShouldPollJoystick()) {
slouken@5123
   324
        SDL_JoystickUpdate();
slouken@5123
   325
    }
slouken@0
   326
#endif
slouken@0
   327
}
slouken@0
   328
slouken@0
   329
/* Public functions */
slouken@0
   330
slouken@1895
   331
int
slouken@1895
   332
SDL_PollEvent(SDL_Event * event)
slouken@0
   333
{
slouken@3072
   334
    return SDL_WaitEventTimeout(event, 0);
slouken@1895
   335
}
slouken@0
   336
slouken@1895
   337
int
slouken@1895
   338
SDL_WaitEvent(SDL_Event * event)
slouken@1895
   339
{
slouken@3072
   340
    return SDL_WaitEventTimeout(event, -1);
slouken@3072
   341
}
slouken@3072
   342
slouken@3072
   343
int
slouken@3072
   344
SDL_WaitEventTimeout(SDL_Event * event, int timeout)
slouken@3072
   345
{
slouken@3072
   346
    Uint32 expiration = 0;
slouken@3072
   347
slouken@3072
   348
    if (timeout > 0)
slouken@3072
   349
        expiration = SDL_GetTicks() + timeout;
slouken@3072
   350
slouken@3072
   351
    for (;;) {
slouken@1895
   352
        SDL_PumpEvents();
slouken@4429
   353
        switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
slouken@1895
   354
        case -1:
slouken@1895
   355
            return 0;
slouken@1895
   356
        case 1:
slouken@1895
   357
            return 1;
slouken@1895
   358
        case 0:
slouken@3072
   359
            if (timeout == 0) {
slouken@3072
   360
                /* Polling and no events, just return */
slouken@3072
   361
                return 0;
slouken@3072
   362
            }
slouken@3072
   363
            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
slouken@3072
   364
                /* Timeout expired and no events */
slouken@3072
   365
                return 0;
slouken@3072
   366
            }
slouken@1895
   367
            SDL_Delay(10);
slouken@3072
   368
            break;
slouken@1895
   369
        }
slouken@1895
   370
    }
slouken@0
   371
}
slouken@0
   372
slouken@1895
   373
int
slouken@1895
   374
SDL_PushEvent(SDL_Event * event)
slouken@0
   375
{
slouken@5146
   376
    SDL_EventWatcher *curr;
dludwig@7082
   377
    event->common.timestamp = SDL_GetTicks();
slouken@1895
   378
    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
slouken@1895
   379
        return 0;
slouken@1895
   380
    }
slouken@5146
   381
slouken@5146
   382
    for (curr = SDL_event_watchers; curr; curr = curr->next) {
slouken@5146
   383
        curr->callback(curr->userdata, event);
slouken@5146
   384
    }
slouken@5146
   385
slouken@4429
   386
    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
slouken@1895
   387
        return -1;
slouken@1895
   388
    }
jim@4657
   389
jim@4657
   390
    SDL_GestureProcessEvent(event);
jim@4657
   391
slouken@1895
   392
    return 1;
slouken@0
   393
}
slouken@0
   394
slouken@1895
   395
void
slouken@1895
   396
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
slouken@0
   397
{
slouken@1895
   398
    SDL_Event bitbucket;
slouken@1895
   399
slouken@1895
   400
    /* Set filter and discard pending events */
slouken@1895
   401
    SDL_EventOK = filter;
slouken@1895
   402
    SDL_EventOKParam = userdata;
slouken@1895
   403
    while (SDL_PollEvent(&bitbucket) > 0);
slouken@0
   404
}
slouken@0
   405
slouken@1895
   406
SDL_bool
slouken@1895
   407
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
slouken@0
   408
{
slouken@1895
   409
    if (filter) {
slouken@1895
   410
        *filter = SDL_EventOK;
slouken@1895
   411
    }
slouken@1895
   412
    if (userdata) {
slouken@1895
   413
        *userdata = SDL_EventOKParam;
slouken@1895
   414
    }
slouken@1895
   415
    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
slouken@0
   416
}
slouken@0
   417
slouken@5146
   418
/* FIXME: This is not thread-safe yet */
slouken@5146
   419
void
slouken@5146
   420
SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
slouken@5146
   421
{
slouken@5146
   422
    SDL_EventWatcher *watcher;
slouken@5146
   423
slouken@5146
   424
    watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
slouken@5146
   425
    if (!watcher) {
slouken@5146
   426
        /* Uh oh... */
slouken@5146
   427
        return;
slouken@5146
   428
    }
slouken@5146
   429
    watcher->callback = filter;
slouken@5146
   430
    watcher->userdata = userdata;
slouken@5146
   431
    watcher->next = SDL_event_watchers;
slouken@5146
   432
    SDL_event_watchers = watcher;
slouken@5146
   433
}
slouken@5146
   434
slouken@5146
   435
/* FIXME: This is not thread-safe yet */
slouken@5146
   436
void
slouken@5146
   437
SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
slouken@5146
   438
{
slouken@5146
   439
    SDL_EventWatcher *prev = NULL;
slouken@5146
   440
    SDL_EventWatcher *curr;
slouken@5146
   441
slouken@5146
   442
    for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
slouken@5146
   443
        if (curr->callback == filter && curr->userdata == userdata) {
slouken@5146
   444
            if (prev) {
slouken@5146
   445
                prev->next = curr->next;
slouken@5146
   446
            } else {
slouken@5146
   447
                SDL_event_watchers = curr->next;
slouken@5146
   448
            }
slouken@5146
   449
            SDL_free(curr);
slouken@5146
   450
            break;
slouken@5146
   451
        }
slouken@5146
   452
    }
slouken@5146
   453
}
slouken@5146
   454
slouken@1895
   455
void
slouken@1895
   456
SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
slouken@0
   457
{
slouken@6977
   458
    if (SDL_LockMutex(SDL_EventQ.lock) == 0) {
slouken@1895
   459
        int spot;
slouken@1895
   460
slouken@1895
   461
        spot = SDL_EventQ.head;
slouken@1895
   462
        while (spot != SDL_EventQ.tail) {
slouken@1895
   463
            if (filter(userdata, &SDL_EventQ.event[spot])) {
slouken@1895
   464
                spot = (spot + 1) % MAXEVENTS;
slouken@1895
   465
            } else {
slouken@1895
   466
                spot = SDL_CutEvent(spot);
slouken@1895
   467
            }
slouken@1895
   468
        }
slouken@1895
   469
    }
slouken@6977
   470
    SDL_UnlockMutex(SDL_EventQ.lock);
slouken@1895
   471
}
slouken@1895
   472
slouken@1895
   473
Uint8
slouken@4429
   474
SDL_EventState(Uint32 type, int state)
slouken@1895
   475
{
slouken@1895
   476
    Uint8 current_state;
slouken@4429
   477
    Uint8 hi = ((type >> 8) & 0xff);
slouken@4429
   478
    Uint8 lo = (type & 0xff);
slouken@0
   479
slouken@4429
   480
    if (SDL_disabled_events[hi] &&
slouken@4429
   481
        (SDL_disabled_events[hi]->bits[lo/32] & (1 << (lo&31)))) {
slouken@4429
   482
        current_state = SDL_DISABLE;
slouken@4429
   483
    } else {
slouken@4429
   484
        current_state = SDL_ENABLE;
slouken@1895
   485
    }
slouken@0
   486
slouken@4429
   487
    if (state != current_state)
slouken@4429
   488
    {
slouken@4429
   489
        switch (state) {
slouken@4429
   490
        case SDL_DISABLE:
slouken@4429
   491
            /* Disable this event type and discard pending events */
slouken@4429
   492
            if (!SDL_disabled_events[hi]) {
slouken@4429
   493
                SDL_disabled_events[hi] = (SDL_DisabledEventBlock*) SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
slouken@4429
   494
                if (!SDL_disabled_events[hi]) {
slouken@4429
   495
                    /* Out of memory, nothing we can do... */
slouken@4429
   496
                    break;
slouken@4429
   497
                }
slouken@4429
   498
            }
slouken@4429
   499
            SDL_disabled_events[hi]->bits[lo/32] |= (1 << (lo&31));
slouken@4429
   500
            SDL_FlushEvent(type);
slouken@4429
   501
            break;
slouken@4429
   502
        case SDL_ENABLE:
slouken@4429
   503
            SDL_disabled_events[hi]->bits[lo/32] &= ~(1 << (lo&31));
slouken@4429
   504
            break;
slouken@4429
   505
        default:
slouken@4429
   506
            /* Querying state... */
slouken@4429
   507
            break;
slouken@1895
   508
        }
slouken@1895
   509
    }
slouken@4429
   510
slouken@4429
   511
    return current_state;
slouken@4429
   512
}
slouken@4429
   513
slouken@4429
   514
Uint32
slouken@4429
   515
SDL_RegisterEvents(int numevents)
slouken@4429
   516
{
slouken@4429
   517
    Uint32 event_base;
slouken@4429
   518
slouken@4429
   519
    if (SDL_userevents+numevents <= SDL_LASTEVENT) {
slouken@4429
   520
        event_base = SDL_userevents;
slouken@4429
   521
        SDL_userevents += numevents;
slouken@4429
   522
    } else {
slouken@4429
   523
        event_base = (Uint32)-1;
slouken@4429
   524
    }
slouken@4429
   525
    return event_base;
slouken@0
   526
}
slouken@0
   527
slouken@0
   528
/* This is a generic event handler.
slouken@0
   529
 */
slouken@1895
   530
int
slouken@1895
   531
SDL_SendSysWMEvent(SDL_SysWMmsg * message)
slouken@0
   532
{
slouken@1895
   533
    int posted;
slouken@0
   534
slouken@1895
   535
    posted = 0;
slouken@4429
   536
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1895
   537
        SDL_Event event;
slouken@1895
   538
        SDL_memset(&event, 0, sizeof(event));
slouken@1895
   539
        event.type = SDL_SYSWMEVENT;
slouken@1895
   540
        event.syswm.msg = message;
slouken@1895
   541
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   542
    }
slouken@1895
   543
    /* Update internal event state */
slouken@1895
   544
    return (posted);
slouken@0
   545
}
slouken@1895
   546
slouken@1895
   547
/* vi: set ts=4 sw=4 expandtab: */