src/events/SDL_events.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 12267 14426507701c
child 12503 806492103856
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

This makes an unchanged set of hardware always report devices in the same
order on each run.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 /* General event handling code for SDL */
    24 
    25 #include "SDL.h"
    26 #include "SDL_events.h"
    27 #include "SDL_thread.h"
    28 #include "SDL_events_c.h"
    29 #include "../timer/SDL_timer_c.h"
    30 #if !SDL_JOYSTICK_DISABLED
    31 #include "../joystick/SDL_joystick_c.h"
    32 #endif
    33 #include "../video/SDL_sysvideo.h"
    34 #include "SDL_syswm.h"
    35 
    36 /*#define SDL_DEBUG_EVENTS 1*/
    37 
    38 /* An arbitrary limit so we don't have unbounded growth */
    39 #define SDL_MAX_QUEUED_EVENTS   65535
    40 
    41 typedef struct SDL_EventWatcher {
    42     SDL_EventFilter callback;
    43     void *userdata;
    44     SDL_bool removed;
    45 } SDL_EventWatcher;
    46 
    47 static SDL_mutex *SDL_event_watchers_lock;
    48 static SDL_EventWatcher SDL_EventOK;
    49 static SDL_EventWatcher *SDL_event_watchers = NULL;
    50 static int SDL_event_watchers_count = 0;
    51 static SDL_bool SDL_event_watchers_dispatching = SDL_FALSE;
    52 static SDL_bool SDL_event_watchers_removed = SDL_FALSE;
    53 
    54 typedef struct {
    55     Uint32 bits[8];
    56 } SDL_DisabledEventBlock;
    57 
    58 static SDL_DisabledEventBlock *SDL_disabled_events[256];
    59 static Uint32 SDL_userevents = SDL_USEREVENT;
    60 
    61 /* Private data -- event queue */
    62 typedef struct _SDL_EventEntry
    63 {
    64     SDL_Event event;
    65     SDL_SysWMmsg msg;
    66     struct _SDL_EventEntry *prev;
    67     struct _SDL_EventEntry *next;
    68 } SDL_EventEntry;
    69 
    70 typedef struct _SDL_SysWMEntry
    71 {
    72     SDL_SysWMmsg msg;
    73     struct _SDL_SysWMEntry *next;
    74 } SDL_SysWMEntry;
    75 
    76 static struct
    77 {
    78     SDL_mutex *lock;
    79     SDL_atomic_t active;
    80     SDL_atomic_t count;
    81     int max_events_seen;
    82     SDL_EventEntry *head;
    83     SDL_EventEntry *tail;
    84     SDL_EventEntry *free;
    85     SDL_SysWMEntry *wmmsg_used;
    86     SDL_SysWMEntry *wmmsg_free;
    87 } SDL_EventQ = { NULL, { 1 }, { 0 }, 0, NULL, NULL, NULL, NULL, NULL };
    88 
    89 
    90 #ifdef SDL_DEBUG_EVENTS
    91 
    92 /* this is to make printf() calls cleaner. */
    93 #define uint unsigned int
    94 
    95 static void
    96 SDL_DebugPrintEvent(const SDL_Event *event)
    97 {
    98     /* !!! FIXME: This code is kinda ugly, sorry. */
    99     printf("SDL EVENT: ");
   100 
   101     if ((event->type >= SDL_USEREVENT) && (event->type <= SDL_LASTEVENT)) {
   102         printf("SDL_USEREVENT");
   103         if (event->type > SDL_USEREVENT) {
   104             printf("+%u", ((uint) event->type) - SDL_USEREVENT);
   105         }
   106         printf(" (timestamp=%u windowid=%u code=%d data1=%p data2=%p)",
   107                 (uint) event->user.timestamp, (uint) event->user.windowID,
   108                 (int) event->user.code, event->user.data1, event->user.data2);
   109         return;
   110     }
   111 
   112     switch (event->type) {
   113         #define SDL_EVENT_CASE(x) case x: printf("%s", #x);
   114         SDL_EVENT_CASE(SDL_FIRSTEVENT) printf("(THIS IS PROBABLY A BUG!)"); break;
   115         SDL_EVENT_CASE(SDL_QUIT) printf("(timestamp=%u)", (uint) event->quit.timestamp); break;
   116         SDL_EVENT_CASE(SDL_APP_TERMINATING) break;
   117         SDL_EVENT_CASE(SDL_APP_LOWMEMORY) break;
   118         SDL_EVENT_CASE(SDL_APP_WILLENTERBACKGROUND) break;
   119         SDL_EVENT_CASE(SDL_APP_DIDENTERBACKGROUND) break;
   120         SDL_EVENT_CASE(SDL_APP_WILLENTERFOREGROUND) break;
   121         SDL_EVENT_CASE(SDL_APP_DIDENTERFOREGROUND) break;
   122         SDL_EVENT_CASE(SDL_KEYMAPCHANGED) break;
   123         SDL_EVENT_CASE(SDL_CLIPBOARDUPDATE) break;
   124         SDL_EVENT_CASE(SDL_RENDER_TARGETS_RESET) break;
   125         SDL_EVENT_CASE(SDL_RENDER_DEVICE_RESET) break;
   126         #undef SDL_EVENT_CASE
   127 
   128         #define SDL_EVENT_CASE(x) case x: printf("%s ", #x);
   129 
   130         SDL_EVENT_CASE(SDL_WINDOWEVENT)
   131             printf("(timestamp=%u windowid=%u event=", (uint) event->window.timestamp, (uint) event->window.windowID);
   132             switch(event->window.event) {
   133                 case SDL_WINDOWEVENT_NONE: printf("none(THIS IS PROBABLY A BUG!)"); break;
   134                 #define SDL_WINDOWEVENT_CASE(x) case x: printf("%s", #x); break
   135                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_SHOWN);
   136                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_HIDDEN);
   137                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_EXPOSED);
   138                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_MOVED);
   139                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_RESIZED);
   140                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_SIZE_CHANGED);
   141                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_MINIMIZED);
   142                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_MAXIMIZED);
   143                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_RESTORED);
   144                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_ENTER);
   145                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_LEAVE);
   146                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_FOCUS_GAINED);
   147                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_FOCUS_LOST);
   148                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_CLOSE);
   149                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_TAKE_FOCUS);
   150                 SDL_WINDOWEVENT_CASE(SDL_WINDOWEVENT_HIT_TEST);
   151                 #undef SDL_WINDOWEVENT_CASE
   152                 default: printf("UNKNOWN(bug? fixme?)"); break;
   153             }
   154             printf(" data1=%d data2=%d)", (int) event->window.data1, (int) event->window.data2);
   155             break;
   156 
   157         SDL_EVENT_CASE(SDL_SYSWMEVENT)
   158             printf("(timestamp=%u)", (uint) event->syswm.timestamp);
   159             /* !!! FIXME: we don't delve further at the moment. */
   160             break;
   161 
   162         #define PRINT_KEY_EVENT(event) \
   163             printf("(timestamp=%u windowid=%u state=%s repeat=%s scancode=%u keycode=%u mod=%u)", \
   164                 (uint) event->key.timestamp, (uint) event->key.windowID, \
   165                 event->key.state == SDL_PRESSED ? "pressed" : "released", \
   166                 event->key.repeat ? "true" : "false", \
   167                 (uint) event->key.keysym.scancode, \
   168                 (uint) event->key.keysym.sym, \
   169                 (uint) event->key.keysym.mod)
   170         SDL_EVENT_CASE(SDL_KEYDOWN) PRINT_KEY_EVENT(event); break;
   171         SDL_EVENT_CASE(SDL_KEYUP) PRINT_KEY_EVENT(event); break;
   172         #undef PRINT_KEY_EVENT
   173 
   174         SDL_EVENT_CASE(SDL_TEXTEDITING)
   175             printf("(timestamp=%u windowid=%u text='%s' start=%d length=%d)",
   176                 (uint) event->edit.timestamp, (uint) event->edit.windowID,
   177                 event->edit.text, (int) event->edit.start, (int) event->edit.length);
   178             break;
   179 
   180         SDL_EVENT_CASE(SDL_TEXTINPUT)
   181             printf("(timestamp=%u windowid=%u text='%s')", (uint) event->text.timestamp, (uint) event->text.windowID, event->text.text);
   182             break;
   183 
   184 
   185         SDL_EVENT_CASE(SDL_MOUSEMOTION)
   186             printf("(timestamp=%u windowid=%u which=%u state=%u x=%d y=%d xrel=%d yrel=%d)",
   187                     (uint) event->motion.timestamp, (uint) event->motion.windowID,
   188                     (uint) event->motion.which, (uint) event->motion.state,
   189                     (int) event->motion.x, (int) event->motion.y,
   190                     (int) event->motion.xrel, (int) event->motion.yrel);
   191             break;
   192 
   193         #define PRINT_MBUTTON_EVENT(event) \
   194             printf("(timestamp=%u windowid=%u which=%u button=%u state=%s clicks=%u x=%d y=%d)", \
   195                     (uint) event->button.timestamp, (uint) event->button.windowID, \
   196                     (uint) event->button.which, (uint) event->button.button, \
   197                     event->button.state == SDL_PRESSED ? "pressed" : "released", \
   198                     (uint) event->button.clicks, (int) event->button.x, (int) event->button.y)
   199         SDL_EVENT_CASE(SDL_MOUSEBUTTONDOWN) PRINT_MBUTTON_EVENT(event); break;
   200         SDL_EVENT_CASE(SDL_MOUSEBUTTONUP) PRINT_MBUTTON_EVENT(event); break;
   201         #undef PRINT_MBUTTON_EVENT
   202 
   203 
   204         SDL_EVENT_CASE(SDL_MOUSEWHEEL)
   205             printf("(timestamp=%u windowid=%u which=%u x=%d y=%d direction=%s)",
   206                     (uint) event->wheel.timestamp, (uint) event->wheel.windowID,
   207                     (uint) event->wheel.which, (int) event->wheel.x, (int) event->wheel.y,
   208                     event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
   209             break;
   210 
   211         SDL_EVENT_CASE(SDL_JOYAXISMOTION)
   212             printf("(timestamp=%u which=%d axis=%u value=%d)",
   213                 (uint) event->jaxis.timestamp, (int) event->jaxis.which,
   214                 (uint) event->jaxis.axis, (int) event->jaxis.value);
   215             break;
   216 
   217         SDL_EVENT_CASE(SDL_JOYBALLMOTION)
   218             printf("(timestamp=%u which=%d ball=%u xrel=%d yrel=%d)",
   219                 (uint) event->jball.timestamp, (int) event->jball.which,
   220                 (uint) event->jball.ball, (int) event->jball.xrel, (int) event->jball.yrel);
   221             break;
   222 
   223         SDL_EVENT_CASE(SDL_JOYHATMOTION)
   224             printf("(timestamp=%u which=%d hat=%u value=%u)",
   225                 (uint) event->jhat.timestamp, (int) event->jhat.which,
   226                 (uint) event->jhat.hat, (uint) event->jhat.value);
   227             break;
   228 
   229         #define PRINT_JBUTTON_EVENT(event) \
   230             printf("(timestamp=%u which=%d button=%u state=%s)", \
   231                 (uint) event->jbutton.timestamp, (int) event->jbutton.which, \
   232                 (uint) event->jbutton.button, event->jbutton.state == SDL_PRESSED ? "pressed" : "released")
   233         SDL_EVENT_CASE(SDL_JOYBUTTONDOWN) PRINT_JBUTTON_EVENT(event); break;
   234         SDL_EVENT_CASE(SDL_JOYBUTTONUP) PRINT_JBUTTON_EVENT(event); break;
   235         #undef PRINT_JBUTTON_EVENT
   236 
   237         #define PRINT_JOYDEV_EVENT(event) printf("(timestamp=%u which=%d)", (uint) event->jdevice.timestamp, (int) event->jdevice.which)
   238         SDL_EVENT_CASE(SDL_JOYDEVICEADDED) PRINT_JOYDEV_EVENT(event); break;
   239         SDL_EVENT_CASE(SDL_JOYDEVICEREMOVED) PRINT_JOYDEV_EVENT(event); break;
   240         #undef PRINT_JOYDEV_EVENT
   241 
   242         SDL_EVENT_CASE(SDL_CONTROLLERAXISMOTION)
   243             printf("(timestamp=%u which=%d axis=%u value=%d)",
   244                 (uint) event->caxis.timestamp, (int) event->caxis.which,
   245                 (uint) event->caxis.axis, (int) event->caxis.value);
   246             break;
   247 
   248         #define PRINT_CBUTTON_EVENT(event) \
   249             printf("(timestamp=%u which=%d button=%u state=%s)", \
   250                 (uint) event->cbutton.timestamp, (int) event->cbutton.which, \
   251                 (uint) event->cbutton.button, event->cbutton.state == SDL_PRESSED ? "pressed" : "released")
   252         SDL_EVENT_CASE(SDL_CONTROLLERBUTTONDOWN) PRINT_CBUTTON_EVENT(event); break;
   253         SDL_EVENT_CASE(SDL_CONTROLLERBUTTONUP) PRINT_CBUTTON_EVENT(event); break;
   254         #undef PRINT_CBUTTON_EVENT
   255 
   256         #define PRINT_CONTROLLERDEV_EVENT(event) printf("(timestamp=%u which=%d)", (uint) event->cdevice.timestamp, (int) event->cdevice.which)
   257         SDL_EVENT_CASE(SDL_CONTROLLERDEVICEADDED) PRINT_CONTROLLERDEV_EVENT(event); break;
   258         SDL_EVENT_CASE(SDL_CONTROLLERDEVICEREMOVED) PRINT_CONTROLLERDEV_EVENT(event); break;
   259         SDL_EVENT_CASE(SDL_CONTROLLERDEVICEREMAPPED) PRINT_CONTROLLERDEV_EVENT(event); break;
   260         #undef PRINT_CONTROLLERDEV_EVENT
   261 
   262         #define PRINT_FINGER_EVENT(event) \
   263             printf("(timestamp=%u touchid=%lld fingerid=%lld x=%f y=%f dx=%f dy=%f pressure=%f)", \
   264                 (uint) event->tfinger.timestamp, (long long) event->tfinger.touchId, \
   265                 (long long) event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, \
   266                 event->tfinger.dx, event->tfinger.dy, event->tfinger.pressure)
   267         SDL_EVENT_CASE(SDL_FINGERDOWN) PRINT_FINGER_EVENT(event); break;
   268         SDL_EVENT_CASE(SDL_FINGERUP) PRINT_FINGER_EVENT(event); break;
   269         SDL_EVENT_CASE(SDL_FINGERMOTION) PRINT_FINGER_EVENT(event); break;
   270         #undef PRINT_FINGER_EVENT
   271 
   272         #define PRINT_DOLLAR_EVENT(event) \
   273             printf("(timestamp=%u touchid=%lld gestureid=%lld numfingers=%u error=%f x=%f y=%f)", \
   274                 (uint) event->dgesture.timestamp, (long long) event->dgesture.touchId, \
   275                 (long long) event->dgesture.gestureId, (uint) event->dgesture.numFingers, \
   276                 event->dgesture.error, event->dgesture.x, event->dgesture.y);
   277         SDL_EVENT_CASE(SDL_DOLLARGESTURE) PRINT_DOLLAR_EVENT(event); break;
   278         SDL_EVENT_CASE(SDL_DOLLARRECORD) PRINT_DOLLAR_EVENT(event); break;
   279         #undef PRINT_DOLLAR_EVENT
   280 
   281         SDL_EVENT_CASE(SDL_MULTIGESTURE)
   282             printf("(timestamp=%u touchid=%lld dtheta=%f ddist=%f x=%f y=%f numfingers=%u)",
   283                 (uint) event->mgesture.timestamp, (long long) event->mgesture.touchId,
   284                 event->mgesture.dTheta, event->mgesture.dDist,
   285                 event->mgesture.x, event->mgesture.y, (uint) event->mgesture.numFingers);
   286             break;
   287 
   288         #define PRINT_DROP_EVENT(event) printf("(file='%s' timestamp=%u windowid=%u)", event->drop.file, (uint) event->drop.timestamp, (uint) event->drop.windowID)
   289         SDL_EVENT_CASE(SDL_DROPFILE) PRINT_DROP_EVENT(event); break;
   290         SDL_EVENT_CASE(SDL_DROPTEXT) PRINT_DROP_EVENT(event); break;
   291         SDL_EVENT_CASE(SDL_DROPBEGIN) PRINT_DROP_EVENT(event); break;
   292         SDL_EVENT_CASE(SDL_DROPCOMPLETE) PRINT_DROP_EVENT(event); break;
   293         #undef PRINT_DROP_EVENT
   294 
   295         #define PRINT_AUDIODEV_EVENT(event) printf("(timestamp=%u which=%u iscapture=%s)", (uint) event->adevice.timestamp, (uint) event->adevice.which, event->adevice.iscapture ? "true" : "false");
   296         SDL_EVENT_CASE(SDL_AUDIODEVICEADDED) PRINT_AUDIODEV_EVENT(event); break;
   297         SDL_EVENT_CASE(SDL_AUDIODEVICEREMOVED) PRINT_AUDIODEV_EVENT(event); break;
   298         #undef PRINT_AUDIODEV_EVENT
   299 
   300         #undef SDL_EVENT_CASE
   301 
   302         default:
   303             printf("UNKNOWN SDL EVENT #%u! (Bug? FIXME?)", (uint) event->type);
   304             break;
   305     }
   306 
   307     printf("\n");
   308 }
   309 #undef uint
   310 #endif
   311 
   312 
   313 
   314 /* Public functions */
   315 
   316 void
   317 SDL_StopEventLoop(void)
   318 {
   319     const char *report = SDL_GetHint("SDL_EVENT_QUEUE_STATISTICS");
   320     int i;
   321     SDL_EventEntry *entry;
   322     SDL_SysWMEntry *wmmsg;
   323 
   324     if (SDL_EventQ.lock) {
   325         SDL_LockMutex(SDL_EventQ.lock);
   326     }
   327 
   328     SDL_AtomicSet(&SDL_EventQ.active, 0);
   329 
   330     if (report && SDL_atoi(report)) {
   331         SDL_Log("SDL EVENT QUEUE: Maximum events in-flight: %d\n",
   332                 SDL_EventQ.max_events_seen);
   333     }
   334 
   335     /* Clean out EventQ */
   336     for (entry = SDL_EventQ.head; entry; ) {
   337         SDL_EventEntry *next = entry->next;
   338         SDL_free(entry);
   339         entry = next;
   340     }
   341     for (entry = SDL_EventQ.free; entry; ) {
   342         SDL_EventEntry *next = entry->next;
   343         SDL_free(entry);
   344         entry = next;
   345     }
   346     for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg; ) {
   347         SDL_SysWMEntry *next = wmmsg->next;
   348         SDL_free(wmmsg);
   349         wmmsg = next;
   350     }
   351     for (wmmsg = SDL_EventQ.wmmsg_free; wmmsg; ) {
   352         SDL_SysWMEntry *next = wmmsg->next;
   353         SDL_free(wmmsg);
   354         wmmsg = next;
   355     }
   356 
   357     SDL_AtomicSet(&SDL_EventQ.count, 0);
   358     SDL_EventQ.max_events_seen = 0;
   359     SDL_EventQ.head = NULL;
   360     SDL_EventQ.tail = NULL;
   361     SDL_EventQ.free = NULL;
   362     SDL_EventQ.wmmsg_used = NULL;
   363     SDL_EventQ.wmmsg_free = NULL;
   364 
   365     /* Clear disabled event state */
   366     for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
   367         SDL_free(SDL_disabled_events[i]);
   368         SDL_disabled_events[i] = NULL;
   369     }
   370 
   371     if (SDL_event_watchers_lock) {
   372         SDL_DestroyMutex(SDL_event_watchers_lock);
   373         SDL_event_watchers_lock = NULL;
   374     }
   375     if (SDL_event_watchers) {
   376         SDL_free(SDL_event_watchers);
   377         SDL_event_watchers = NULL;
   378         SDL_event_watchers_count = 0;
   379     }
   380     SDL_zero(SDL_EventOK);
   381 
   382     if (SDL_EventQ.lock) {
   383         SDL_UnlockMutex(SDL_EventQ.lock);
   384         SDL_DestroyMutex(SDL_EventQ.lock);
   385         SDL_EventQ.lock = NULL;
   386     }
   387 }
   388 
   389 /* This function (and associated calls) may be called more than once */
   390 int
   391 SDL_StartEventLoop(void)
   392 {
   393     /* We'll leave the event queue alone, since we might have gotten
   394        some important events at launch (like SDL_DROPFILE)
   395 
   396        FIXME: Does this introduce any other bugs with events at startup?
   397      */
   398 
   399     /* Create the lock and set ourselves active */
   400 #if !SDL_THREADS_DISABLED
   401     if (!SDL_EventQ.lock) {
   402         SDL_EventQ.lock = SDL_CreateMutex();
   403         if (SDL_EventQ.lock == NULL) {
   404             return -1;
   405         }
   406     }
   407 
   408     if (!SDL_event_watchers_lock) {
   409         SDL_event_watchers_lock = SDL_CreateMutex();
   410         if (SDL_event_watchers_lock == NULL) {
   411             return -1;
   412         }
   413     }
   414 #endif /* !SDL_THREADS_DISABLED */
   415 
   416     /* Process most event types */
   417     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
   418     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
   419     SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
   420 #if 0 /* Leave these events enabled so apps can respond to items being dragged onto them at startup */
   421     SDL_EventState(SDL_DROPFILE, SDL_DISABLE);
   422     SDL_EventState(SDL_DROPTEXT, SDL_DISABLE);
   423 #endif
   424 
   425     SDL_AtomicSet(&SDL_EventQ.active, 1);
   426 
   427     return 0;
   428 }
   429 
   430 
   431 /* Add an event to the event queue -- called with the queue locked */
   432 static int
   433 SDL_AddEvent(SDL_Event * event)
   434 {
   435     SDL_EventEntry *entry;
   436     const int initial_count = SDL_AtomicGet(&SDL_EventQ.count);
   437     int final_count;
   438 
   439     if (initial_count >= SDL_MAX_QUEUED_EVENTS) {
   440         SDL_SetError("Event queue is full (%d events)", initial_count);
   441         return 0;
   442     }
   443 
   444     if (SDL_EventQ.free == NULL) {
   445         entry = (SDL_EventEntry *)SDL_malloc(sizeof(*entry));
   446         if (!entry) {
   447             return 0;
   448         }
   449     } else {
   450         entry = SDL_EventQ.free;
   451         SDL_EventQ.free = entry->next;
   452     }
   453 
   454     #ifdef SDL_DEBUG_EVENTS
   455     SDL_DebugPrintEvent(event);
   456     #endif
   457 
   458     entry->event = *event;
   459     if (event->type == SDL_SYSWMEVENT) {
   460         entry->msg = *event->syswm.msg;
   461         entry->event.syswm.msg = &entry->msg;
   462     }
   463 
   464     if (SDL_EventQ.tail) {
   465         SDL_EventQ.tail->next = entry;
   466         entry->prev = SDL_EventQ.tail;
   467         SDL_EventQ.tail = entry;
   468         entry->next = NULL;
   469     } else {
   470         SDL_assert(!SDL_EventQ.head);
   471         SDL_EventQ.head = entry;
   472         SDL_EventQ.tail = entry;
   473         entry->prev = NULL;
   474         entry->next = NULL;
   475     }
   476 
   477     final_count = SDL_AtomicAdd(&SDL_EventQ.count, 1) + 1;
   478     if (final_count > SDL_EventQ.max_events_seen) {
   479         SDL_EventQ.max_events_seen = final_count;
   480     }
   481 
   482     return 1;
   483 }
   484 
   485 /* Remove an event from the queue -- called with the queue locked */
   486 static void
   487 SDL_CutEvent(SDL_EventEntry *entry)
   488 {
   489     if (entry->prev) {
   490         entry->prev->next = entry->next;
   491     }
   492     if (entry->next) {
   493         entry->next->prev = entry->prev;
   494     }
   495 
   496     if (entry == SDL_EventQ.head) {
   497         SDL_assert(entry->prev == NULL);
   498         SDL_EventQ.head = entry->next;
   499     }
   500     if (entry == SDL_EventQ.tail) {
   501         SDL_assert(entry->next == NULL);
   502         SDL_EventQ.tail = entry->prev;
   503     }
   504 
   505     entry->next = SDL_EventQ.free;
   506     SDL_EventQ.free = entry;
   507     SDL_assert(SDL_AtomicGet(&SDL_EventQ.count) > 0);
   508     SDL_AtomicAdd(&SDL_EventQ.count, -1);
   509 }
   510 
   511 /* Lock the event queue, take a peep at it, and unlock it */
   512 int
   513 SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
   514                Uint32 minType, Uint32 maxType)
   515 {
   516     int i, used;
   517 
   518     /* Don't look after we've quit */
   519     if (!SDL_AtomicGet(&SDL_EventQ.active)) {
   520         /* We get a few spurious events at shutdown, so don't warn then */
   521         if (action != SDL_ADDEVENT) {
   522             SDL_SetError("The event system has been shut down");
   523         }
   524         return (-1);
   525     }
   526     /* Lock the event queue */
   527     used = 0;
   528     if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
   529         if (action == SDL_ADDEVENT) {
   530             for (i = 0; i < numevents; ++i) {
   531                 used += SDL_AddEvent(&events[i]);
   532             }
   533         } else {
   534             SDL_EventEntry *entry, *next;
   535             SDL_SysWMEntry *wmmsg, *wmmsg_next;
   536             Uint32 type;
   537 
   538             if (action == SDL_GETEVENT) {
   539                 /* Clean out any used wmmsg data
   540                    FIXME: Do we want to retain the data for some period of time?
   541                  */
   542                 for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg; wmmsg = wmmsg_next) {
   543                     wmmsg_next = wmmsg->next;
   544                     wmmsg->next = SDL_EventQ.wmmsg_free;
   545                     SDL_EventQ.wmmsg_free = wmmsg;
   546                 }
   547                 SDL_EventQ.wmmsg_used = NULL;
   548             }
   549 
   550             for (entry = SDL_EventQ.head; entry && (!events || used < numevents); entry = next) {
   551                 next = entry->next;
   552                 type = entry->event.type;
   553                 if (minType <= type && type <= maxType) {
   554                     if (events) {
   555                         events[used] = entry->event;
   556                         if (entry->event.type == SDL_SYSWMEVENT) {
   557                             /* We need to copy the wmmsg somewhere safe.
   558                                For now we'll guarantee it's valid at least until
   559                                the next call to SDL_PeepEvents()
   560                              */
   561                             if (SDL_EventQ.wmmsg_free) {
   562                                 wmmsg = SDL_EventQ.wmmsg_free;
   563                                 SDL_EventQ.wmmsg_free = wmmsg->next;
   564                             } else {
   565                                 wmmsg = (SDL_SysWMEntry *)SDL_malloc(sizeof(*wmmsg));
   566                             }
   567                             wmmsg->msg = *entry->event.syswm.msg;
   568                             wmmsg->next = SDL_EventQ.wmmsg_used;
   569                             SDL_EventQ.wmmsg_used = wmmsg;
   570                             events[used].syswm.msg = &wmmsg->msg;
   571                         }
   572 
   573                         if (action == SDL_GETEVENT) {
   574                             SDL_CutEvent(entry);
   575                         }
   576                     }
   577                     ++used;
   578                 }
   579             }
   580         }
   581         if (SDL_EventQ.lock) {
   582             SDL_UnlockMutex(SDL_EventQ.lock);
   583         }
   584     } else {
   585         return SDL_SetError("Couldn't lock event queue");
   586     }
   587     return (used);
   588 }
   589 
   590 SDL_bool
   591 SDL_HasEvent(Uint32 type)
   592 {
   593     return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0);
   594 }
   595 
   596 SDL_bool
   597 SDL_HasEvents(Uint32 minType, Uint32 maxType)
   598 {
   599     return (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0);
   600 }
   601 
   602 void
   603 SDL_FlushEvent(Uint32 type)
   604 {
   605     SDL_FlushEvents(type, type);
   606 }
   607 
   608 void
   609 SDL_FlushEvents(Uint32 minType, Uint32 maxType)
   610 {
   611     /* !!! FIXME: we need to manually SDL_free() the strings in TEXTINPUT and
   612        drag'n'drop events if we're flushing them without passing them to the
   613        app, but I don't know if this is the right place to do that. */
   614 
   615     /* Don't look after we've quit */
   616     if (!SDL_AtomicGet(&SDL_EventQ.active)) {
   617         return;
   618     }
   619 
   620     /* Make sure the events are current */
   621 #if 0
   622     /* Actually, we can't do this since we might be flushing while processing
   623        a resize event, and calling this might trigger further resize events.
   624     */
   625     SDL_PumpEvents();
   626 #endif
   627 
   628     /* Lock the event queue */
   629     if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
   630         SDL_EventEntry *entry, *next;
   631         Uint32 type;
   632         for (entry = SDL_EventQ.head; entry; entry = next) {
   633             next = entry->next;
   634             type = entry->event.type;
   635             if (minType <= type && type <= maxType) {
   636                 SDL_CutEvent(entry);
   637             }
   638         }
   639         if (SDL_EventQ.lock) {
   640             SDL_UnlockMutex(SDL_EventQ.lock);
   641         }
   642     }
   643 }
   644 
   645 /* Run the system dependent event loops */
   646 void
   647 SDL_PumpEvents(void)
   648 {
   649     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   650 
   651     /* Get events from the video subsystem */
   652     if (_this) {
   653         _this->PumpEvents(_this);
   654     }
   655 #if !SDL_JOYSTICK_DISABLED
   656     /* Check for joystick state change */
   657     if ((!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] || SDL_JoystickEventState(SDL_QUERY))) {
   658         SDL_JoystickUpdate();
   659     }
   660 #endif
   661 
   662 #if !SDL_SENSOR_DISABLED
   663     /* Check for sensor state change */
   664     if (!SDL_disabled_events[SDL_SENSORUPDATE >> 8]) {
   665         SDL_SensorUpdate();
   666     }
   667 #endif
   668 
   669     SDL_SendPendingQuit();  /* in case we had a signal handler fire, etc. */
   670 }
   671 
   672 /* Public functions */
   673 
   674 int
   675 SDL_PollEvent(SDL_Event * event)
   676 {
   677     return SDL_WaitEventTimeout(event, 0);
   678 }
   679 
   680 int
   681 SDL_WaitEvent(SDL_Event * event)
   682 {
   683     return SDL_WaitEventTimeout(event, -1);
   684 }
   685 
   686 int
   687 SDL_WaitEventTimeout(SDL_Event * event, int timeout)
   688 {
   689     Uint32 expiration = 0;
   690 
   691     if (timeout > 0)
   692         expiration = SDL_GetTicks() + timeout;
   693 
   694     for (;;) {
   695         SDL_PumpEvents();
   696         switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
   697         case -1:
   698             return 0;
   699         case 0:
   700             if (timeout == 0) {
   701                 /* Polling and no events, just return */
   702                 return 0;
   703             }
   704             if (timeout > 0 && SDL_TICKS_PASSED(SDL_GetTicks(), expiration)) {
   705                 /* Timeout expired and no events */
   706                 return 0;
   707             }
   708             SDL_Delay(10);
   709             break;
   710         default:
   711             /* Has events */
   712             return 1;
   713         }
   714     }
   715 }
   716 
   717 int
   718 SDL_PushEvent(SDL_Event * event)
   719 {
   720     event->common.timestamp = SDL_GetTicks();
   721 
   722     if (SDL_EventOK.callback || SDL_event_watchers_count > 0) {
   723         if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
   724             if (SDL_EventOK.callback && !SDL_EventOK.callback(SDL_EventOK.userdata, event)) {
   725                 if (SDL_event_watchers_lock) {
   726                     SDL_UnlockMutex(SDL_event_watchers_lock);
   727                 }
   728                 return 0;
   729             }
   730 
   731             if (SDL_event_watchers_count > 0) {
   732                 /* Make sure we only dispatch the current watcher list */
   733                 int i, event_watchers_count = SDL_event_watchers_count;
   734 
   735                 SDL_event_watchers_dispatching = SDL_TRUE;
   736                 for (i = 0; i < event_watchers_count; ++i) {
   737                     if (!SDL_event_watchers[i].removed) {
   738                         SDL_event_watchers[i].callback(SDL_event_watchers[i].userdata, event);
   739                     }
   740                 }
   741                 SDL_event_watchers_dispatching = SDL_FALSE;
   742 
   743                 if (SDL_event_watchers_removed) {
   744                     for (i = SDL_event_watchers_count; i--; ) {
   745                         if (SDL_event_watchers[i].removed) {
   746                             --SDL_event_watchers_count;
   747                             if (i < SDL_event_watchers_count) {
   748                                 SDL_memmove(&SDL_event_watchers[i], &SDL_event_watchers[i+1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
   749                             }
   750                         }
   751                     }
   752                     SDL_event_watchers_removed = SDL_FALSE;
   753                 }
   754             }
   755 
   756             if (SDL_event_watchers_lock) {
   757                 SDL_UnlockMutex(SDL_event_watchers_lock);
   758             }
   759         }
   760     }
   761 
   762     if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
   763         return -1;
   764     }
   765 
   766     SDL_GestureProcessEvent(event);
   767 
   768     return 1;
   769 }
   770 
   771 void
   772 SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
   773 {
   774     if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
   775         /* Set filter and discard pending events */
   776         SDL_EventOK.callback = filter;
   777         SDL_EventOK.userdata = userdata;
   778         SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
   779 
   780         if (SDL_event_watchers_lock) {
   781             SDL_UnlockMutex(SDL_event_watchers_lock);
   782         }
   783     }
   784 }
   785 
   786 SDL_bool
   787 SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
   788 {
   789     SDL_EventWatcher event_ok;
   790 
   791     if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
   792         event_ok = SDL_EventOK;
   793 
   794         if (SDL_event_watchers_lock) {
   795             SDL_UnlockMutex(SDL_event_watchers_lock);
   796         }
   797     } else {
   798         SDL_zero(event_ok);
   799     }
   800 
   801     if (filter) {
   802         *filter = event_ok.callback;
   803     }
   804     if (userdata) {
   805         *userdata = event_ok.userdata;
   806     }
   807     return event_ok.callback ? SDL_TRUE : SDL_FALSE;
   808 }
   809 
   810 void
   811 SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
   812 {
   813     if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
   814         SDL_EventWatcher *event_watchers;
   815 
   816         event_watchers = SDL_realloc(SDL_event_watchers, (SDL_event_watchers_count + 1) * sizeof(*event_watchers));
   817         if (event_watchers) {
   818             SDL_EventWatcher *watcher;
   819 
   820             SDL_event_watchers = event_watchers;
   821             watcher = &SDL_event_watchers[SDL_event_watchers_count];
   822             watcher->callback = filter;
   823             watcher->userdata = userdata;
   824             watcher->removed = SDL_FALSE;
   825             ++SDL_event_watchers_count;
   826         }
   827 
   828         if (SDL_event_watchers_lock) {
   829             SDL_UnlockMutex(SDL_event_watchers_lock);
   830         }
   831     }
   832 }
   833 
   834 void
   835 SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
   836 {
   837     if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
   838         int i;
   839 
   840         for (i = 0; i < SDL_event_watchers_count; ++i) {
   841             if (SDL_event_watchers[i].callback == filter && SDL_event_watchers[i].userdata == userdata) {
   842                 if (SDL_event_watchers_dispatching) {
   843                     SDL_event_watchers[i].removed = SDL_TRUE;
   844                     SDL_event_watchers_removed = SDL_TRUE;
   845                 } else {
   846                     --SDL_event_watchers_count;
   847                     if (i < SDL_event_watchers_count) {
   848                         SDL_memmove(&SDL_event_watchers[i], &SDL_event_watchers[i+1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
   849                     }
   850                 }
   851                 break;
   852             }
   853         }
   854 
   855         if (SDL_event_watchers_lock) {
   856             SDL_UnlockMutex(SDL_event_watchers_lock);
   857         }
   858     }
   859 }
   860 
   861 void
   862 SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
   863 {
   864     if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
   865         SDL_EventEntry *entry, *next;
   866         for (entry = SDL_EventQ.head; entry; entry = next) {
   867             next = entry->next;
   868             if (!filter(userdata, &entry->event)) {
   869                 SDL_CutEvent(entry);
   870             }
   871         }
   872         if (SDL_EventQ.lock) {
   873             SDL_UnlockMutex(SDL_EventQ.lock);
   874         }
   875     }
   876 }
   877 
   878 Uint8
   879 SDL_EventState(Uint32 type, int state)
   880 {
   881     const SDL_bool isdnd = ((state == SDL_DISABLE) || (state == SDL_ENABLE)) &&
   882                            ((type == SDL_DROPFILE) || (type == SDL_DROPTEXT));
   883     Uint8 current_state;
   884     Uint8 hi = ((type >> 8) & 0xff);
   885     Uint8 lo = (type & 0xff);
   886 
   887     if (SDL_disabled_events[hi] &&
   888         (SDL_disabled_events[hi]->bits[lo/32] & (1 << (lo&31)))) {
   889         current_state = SDL_DISABLE;
   890     } else {
   891         current_state = SDL_ENABLE;
   892     }
   893 
   894     if (state != current_state)
   895     {
   896         switch (state) {
   897         case SDL_DISABLE:
   898             /* Disable this event type and discard pending events */
   899             if (!SDL_disabled_events[hi]) {
   900                 SDL_disabled_events[hi] = (SDL_DisabledEventBlock*) SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
   901                 if (!SDL_disabled_events[hi]) {
   902                     /* Out of memory, nothing we can do... */
   903                     break;
   904                 }
   905             }
   906             SDL_disabled_events[hi]->bits[lo/32] |= (1 << (lo&31));
   907             SDL_FlushEvent(type);
   908             break;
   909         case SDL_ENABLE:
   910             SDL_disabled_events[hi]->bits[lo/32] &= ~(1 << (lo&31));
   911             break;
   912         default:
   913             /* Querying state... */
   914             break;
   915         }
   916     }
   917 
   918     /* turn off drag'n'drop support if we've disabled the events.
   919        This might change some UI details at the OS level. */
   920     if (isdnd) {
   921         SDL_ToggleDragAndDropSupport();
   922     }
   923 
   924     return current_state;
   925 }
   926 
   927 Uint32
   928 SDL_RegisterEvents(int numevents)
   929 {
   930     Uint32 event_base;
   931 
   932     if ((numevents > 0) && (SDL_userevents+numevents <= SDL_LASTEVENT)) {
   933         event_base = SDL_userevents;
   934         SDL_userevents += numevents;
   935     } else {
   936         event_base = (Uint32)-1;
   937     }
   938     return event_base;
   939 }
   940 
   941 int
   942 SDL_SendAppEvent(SDL_EventType eventType)
   943 {
   944     int posted;
   945 
   946     posted = 0;
   947     if (SDL_GetEventState(eventType) == SDL_ENABLE) {
   948         SDL_Event event;
   949         event.type = eventType;
   950         posted = (SDL_PushEvent(&event) > 0);
   951     }
   952     return (posted);
   953 }
   954 
   955 int
   956 SDL_SendSysWMEvent(SDL_SysWMmsg * message)
   957 {
   958     int posted;
   959 
   960     posted = 0;
   961     if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
   962         SDL_Event event;
   963         SDL_memset(&event, 0, sizeof(event));
   964         event.type = SDL_SYSWMEVENT;
   965         event.syswm.msg = message;
   966         posted = (SDL_PushEvent(&event) > 0);
   967     }
   968     /* Update internal event state */
   969     return (posted);
   970 }
   971 
   972 int
   973 SDL_SendKeymapChangedEvent(void)
   974 {
   975     return SDL_SendAppEvent(SDL_KEYMAPCHANGED);
   976 }
   977 
   978 /* vi: set ts=4 sw=4 expandtab: */