| 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 "../video/SDL_sysvideo.h" |
| 24 | #include "../events/SDL_events_c.h" |
| 25 | #include "SDL_tray_utils.h" |
| 26 | |
| 27 | |
| 28 | static int active_trays = 0; |
| 29 | |
| 30 | void SDL_RegisterTray(SDL_Tray *tray) |
| 31 | { |
| 32 | SDL_SetObjectValid(tray, SDL_OBJECT_TYPE_TRAY, true); |
| 33 | |
| 34 | ++active_trays; |
| 35 | } |
| 36 | |
| 37 | void SDL_UnregisterTray(SDL_Tray *tray) |
| 38 | { |
| 39 | SDL_assert(SDL_ObjectValid(tray, SDL_OBJECT_TYPE_TRAY)); |
| 40 | |
| 41 | SDL_SetObjectValid(tray, SDL_OBJECT_TYPE_TRAY, false); |
| 42 | |
| 43 | --active_trays; |
| 44 | if (active_trays > 0) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (!SDL_GetHintBoolean(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, true)) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int toplevel_count = 0; |
| 53 | SDL_Window **windows = SDL_GetWindows(NULL); |
| 54 | if (windows) { |
| 55 | for (int i = 0; windows[i]; ++i) { |
| 56 | SDL_Window *window = windows[i]; |
| 57 | if (!window->parent && !(window->flags & SDL_WINDOW_HIDDEN)) { |
| 58 | ++toplevel_count; |
| 59 | } |
| 60 | } |
| 61 | SDL_free(windows); |
| 62 | } |
| 63 | |
| 64 | if (toplevel_count == 0) { |
| 65 | SDL_SendQuit(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void SDL_CleanupTrays(void) |
| 70 | { |
| 71 | if (active_trays == 0) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | void **trays = (void **)SDL_malloc(active_trays * sizeof(*trays)); |
| 76 | if (!trays) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | int count = SDL_GetObjects(SDL_OBJECT_TYPE_TRAY, trays, active_trays); |
| 81 | SDL_assert(count == active_trays); |
| 82 | for (int i = 0; i < count; ++i) { |
| 83 | SDL_DestroyTray((SDL_Tray *)trays[i]); |
| 84 | } |
| 85 | SDL_free(trays); |
| 86 | } |
| 87 | |
| 88 | bool SDL_HasActiveTrays(void) |
| 89 | { |
| 90 | return (active_trays > 0); |
| 91 | } |
| 92 | |