# HG changeset patch # User Sam Lantinga # Date 1393111631 28800 # Node ID 80c193c7c8c8d7fedb2bc60d0e736b2af0cdb07c # Parent ffece8ab18a6629f41def9f961e6dabf377d2297 Fixed bug 2414 - Execute event watchers in the order they were added Leonardo Event watchers are being executed on the inverse order they are added because they are added to the head of the SDL_event_watchers list. Since watchers are allowed to change events before they are reported (they shouldn't, imo), this breaks code that rely on watcher execution order (such as distributed event handling). An easy scenario to see this behaving weird to the user is if you add an event watcher to check mouse coordinates and check them again in your event loop. If you add the watcher after renderer's one (which always happens after you have initialized renderer), you get the same event but different coordinates. The proposed patch adds the event watcher in the tail of the list, not in the beginning, and correctly fixes this problem. diff -r ffece8ab18a6 -r 80c193c7c8c8 src/events/SDL_events.c --- a/src/events/SDL_events.c Sat Feb 22 15:23:09 2014 -0800 +++ b/src/events/SDL_events.c Sat Feb 22 15:27:11 2014 -0800 @@ -503,17 +503,28 @@ void SDL_AddEventWatch(SDL_EventFilter filter, void *userdata) { - SDL_EventWatcher *watcher; + SDL_EventWatcher *watcher, *tail; watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher)); if (!watcher) { /* Uh oh... */ return; } + + /* create the watcher */ watcher->callback = filter; watcher->userdata = userdata; - watcher->next = SDL_event_watchers; - SDL_event_watchers = watcher; + watcher->next = NULL; + + /* add the watcher to the end of the list */ + if (SDL_event_watchers) { + for (tail = SDL_event_watchers; tail->next; tail = tail->next) { + continue; + } + tail->next = watcher; + } else { + SDL_event_watchers = watcher; + } } /* FIXME: This is not thread-safe yet */