Path: blob/master/thirdparty/sdl/events/SDL_eventwatch.c
10278 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122#include "SDL_eventwatch_c.h"232425bool SDL_InitEventWatchList(SDL_EventWatchList *list)26{27if (list->lock == NULL) {28list->lock = SDL_CreateMutex();29if (list->lock == NULL) {30return false;31}32}33return true;34}3536void SDL_QuitEventWatchList(SDL_EventWatchList *list)37{38if (list->lock) {39SDL_DestroyMutex(list->lock);40list->lock = NULL;41}42if (list->watchers) {43SDL_free(list->watchers);44list->watchers = NULL;45list->count = 0;46}47SDL_zero(list->filter);48}4950bool SDL_DispatchEventWatchList(SDL_EventWatchList *list, SDL_Event *event)51{52SDL_EventWatcher *filter = &list->filter;5354if (!filter->callback && list->count == 0) {55return true;56}5758SDL_LockMutex(list->lock);59{60// Make sure we only dispatch the current watcher list61int i, count = list->count;6263if (filter->callback && !filter->callback(filter->userdata, event)) {64SDL_UnlockMutex(list->lock);65return false;66}6768list->dispatching = true;69for (i = 0; i < count; ++i) {70if (!list->watchers[i].removed) {71list->watchers[i].callback(list->watchers[i].userdata, event);72}73}74list->dispatching = false;7576if (list->removed) {77for (i = list->count; i--;) {78if (list->watchers[i].removed) {79--list->count;80if (i < list->count) {81SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i]));82}83}84}85list->removed = false;86}87}88SDL_UnlockMutex(list->lock);8990return true;91}9293bool SDL_AddEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata)94{95bool result = true;9697SDL_LockMutex(list->lock);98{99SDL_EventWatcher *watchers;100101watchers = (SDL_EventWatcher *)SDL_realloc(list->watchers, (list->count + 1) * sizeof(*watchers));102if (watchers) {103SDL_EventWatcher *watcher;104105list->watchers = watchers;106watcher = &list->watchers[list->count];107watcher->callback = filter;108watcher->userdata = userdata;109watcher->removed = false;110++list->count;111} else {112result = false;113}114}115SDL_UnlockMutex(list->lock);116117return result;118}119120void SDL_RemoveEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata)121{122SDL_LockMutex(list->lock);123{124int i;125126for (i = 0; i < list->count; ++i) {127if (list->watchers[i].callback == filter && list->watchers[i].userdata == userdata) {128if (list->dispatching) {129list->watchers[i].removed = true;130list->removed = true;131} else {132--list->count;133if (i < list->count) {134SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i]));135}136}137break;138}139}140}141SDL_UnlockMutex(list->lock);142}143144145