SDL_eventwatch.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_eventwatch_c.h"
  20. bool SDL_InitEventWatchList(SDL_EventWatchList *list)
  21. {
  22. if (list->lock == NULL) {
  23. list->lock = SDL_CreateMutex();
  24. if (list->lock == NULL) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. void SDL_QuitEventWatchList(SDL_EventWatchList *list)
  31. {
  32. if (list->lock) {
  33. SDL_DestroyMutex(list->lock);
  34. list->lock = NULL;
  35. }
  36. if (list->watchers) {
  37. SDL_free(list->watchers);
  38. list->watchers = NULL;
  39. list->count = 0;
  40. }
  41. SDL_zero(list->filter);
  42. }
  43. bool SDL_DispatchEventWatchList(SDL_EventWatchList *list, SDL_Event *event)
  44. {
  45. SDL_EventWatcher *filter = &list->filter;
  46. if (!filter->callback && list->count == 0) {
  47. return true;
  48. }
  49. SDL_LockMutex(list->lock);
  50. {
  51. // Make sure we only dispatch the current watcher list
  52. int i, count = list->count;
  53. if (filter->callback && !filter->callback(filter->userdata, event)) {
  54. SDL_UnlockMutex(list->lock);
  55. return false;
  56. }
  57. list->dispatching = true;
  58. for (i = 0; i < count; ++i) {
  59. if (!list->watchers[i].removed) {
  60. list->watchers[i].callback(list->watchers[i].userdata, event);
  61. }
  62. }
  63. list->dispatching = false;
  64. if (list->removed) {
  65. for (i = list->count; i--;) {
  66. if (list->watchers[i].removed) {
  67. --list->count;
  68. if (i < list->count) {
  69. SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i]));
  70. }
  71. }
  72. }
  73. list->removed = false;
  74. }
  75. }
  76. SDL_UnlockMutex(list->lock);
  77. return true;
  78. }
  79. bool SDL_AddEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata)
  80. {
  81. bool result = true;
  82. SDL_LockMutex(list->lock);
  83. {
  84. SDL_EventWatcher *watchers;
  85. watchers = (SDL_EventWatcher *)SDL_realloc(list->watchers, (list->count + 1) * sizeof(*watchers));
  86. if (watchers) {
  87. SDL_EventWatcher *watcher;
  88. list->watchers = watchers;
  89. watcher = &list->watchers[list->count];
  90. watcher->callback = filter;
  91. watcher->userdata = userdata;
  92. watcher->removed = false;
  93. ++list->count;
  94. } else {
  95. result = false;
  96. }
  97. }
  98. SDL_UnlockMutex(list->lock);
  99. return result;
  100. }
  101. void SDL_RemoveEventWatchList(SDL_EventWatchList *list, SDL_EventFilter filter, void *userdata)
  102. {
  103. SDL_LockMutex(list->lock);
  104. {
  105. int i;
  106. for (i = 0; i < list->count; ++i) {
  107. if (list->watchers[i].callback == filter && list->watchers[i].userdata == userdata) {
  108. if (list->dispatching) {
  109. list->watchers[i].removed = true;
  110. list->removed = true;
  111. } else {
  112. --list->count;
  113. if (i < list->count) {
  114. SDL_memmove(&list->watchers[i], &list->watchers[i + 1], (list->count - i) * sizeof(list->watchers[i]));
  115. }
  116. }
  117. break;
  118. }
  119. }
  120. }
  121. SDL_UnlockMutex(list->lock);
  122. }