1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 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 | #include "SDL_eventwatch_c.h" |
24 | |
25 | |
26 | bool SDL_InitEventWatchList(SDL_EventWatchList *list) |
27 | { |
28 | if (list->lock == NULL) { |
29 | list->lock = SDL_CreateMutex(); |
30 | if (list->lock == NULL) { |
31 | return false; |
32 | } |
33 | } |
34 | return true; |
35 | } |
36 | |
37 | void SDL_QuitEventWatchList(SDL_EventWatchList *list) |
38 | { |
39 | if (list->lock) { |
40 | SDL_DestroyMutex(list->lock); |
41 | list->lock = NULL; |
42 | } |
43 | if (list->watchers) { |
44 | SDL_free(list->watchers); |
45 | list->watchers = NULL; |
46 | list->count = 0; |
47 | } |
48 | SDL_zero(list->filter); |
49 | } |
50 | |
51 | bool SDL_DispatchEventWatchList(SDL_EventWatchList *list, SDL_Event *event) |
52 | { |
53 | SDL_EventWatcher *filter = &list->filter; |
54 | |
55 | if (!filter->callback && list->count == 0) { |
56 | return true; |
57 | } |
58 | |
59 | SDL_LockMutex(list->lock); |
60 | { |
61 | // Make sure we only dispatch the current watcher list |
62 | int i, count = list->count; |
63 | |
64 | if (filter->callback && !filter->callback(filter->userdata, event)) { |
65 | SDL_UnlockMutex(list->lock); |
66 | return false; |
67 | } |
68 | |
69 | list->dispatching = true; |
70 | for (i = 0; i < count; ++i) { |
71 | if (!list->watchers[i].removed) { |
72 | list->watchers[i].callback(list->watchers[i].userdata, event); |
73 | } |
74 | } |
75 | list->dispatching = false; |
76 | |
77 | if (list->removed) { |
78 | for (i = list->count; i--;) { |
79 | if (list->watchers[i].removed) { |
80 | --list->count; |
81 | if (i < list->count) { |
82 | SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i])); |
83 | } |
84 | } |
85 | } |
86 | list->removed = false; |
87 | } |
88 | } |
89 | SDL_UnlockMutex(list->lock); |
90 | |
91 | return true; |
92 | } |
93 | |
94 | bool SDL_AddEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata) |
95 | { |
96 | bool result = true; |
97 | |
98 | SDL_LockMutex(list->lock); |
99 | { |
100 | SDL_EventWatcher *watchers; |
101 | |
102 | watchers = (SDL_EventWatcher *)SDL_realloc(list->watchers, (list->count + 1) * sizeof(*watchers)); |
103 | if (watchers) { |
104 | SDL_EventWatcher *watcher; |
105 | |
106 | list->watchers = watchers; |
107 | watcher = &list->watchers[list->count]; |
108 | watcher->callback = filter; |
109 | watcher->userdata = userdata; |
110 | watcher->removed = false; |
111 | ++list->count; |
112 | } else { |
113 | result = false; |
114 | } |
115 | } |
116 | SDL_UnlockMutex(list->lock); |
117 | |
118 | return result; |
119 | } |
120 | |
121 | void SDL_RemoveEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata) |
122 | { |
123 | SDL_LockMutex(list->lock); |
124 | { |
125 | int i; |
126 | |
127 | for (i = 0; i < list->count; ++i) { |
128 | if (list->watchers[i].callback == filter && list->watchers[i].userdata == userdata) { |
129 | if (list->dispatching) { |
130 | list->watchers[i].removed = true; |
131 | list->removed = true; |
132 | } else { |
133 | --list->count; |
134 | if (i < list->count) { |
135 | SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i])); |
136 | } |
137 | } |
138 | break; |
139 | } |
140 | } |
141 | } |
142 | SDL_UnlockMutex(list->lock); |
143 | } |
144 | |