| 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_dialog.h" |
| 24 | #include "./SDL_portaldialog.h" |
| 25 | #include "./SDL_zenitydialog.h" |
| 26 | |
| 27 | static void (*detected_function)(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props) = NULL; |
| 28 | |
| 29 | void SDLCALL hint_callback(void *userdata, const char *name, const char *oldValue, const char *newValue); |
| 30 | |
| 31 | static void set_callback(void) |
| 32 | { |
| 33 | static bool is_set = false; |
| 34 | |
| 35 | if (is_set == false) { |
| 36 | is_set = true; |
| 37 | SDL_AddHintCallback(SDL_HINT_FILE_DIALOG_DRIVER, hint_callback, NULL); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Returns non-zero on success, 0 on failure |
| 42 | static int detect_available_methods(const char *value) |
| 43 | { |
| 44 | const char *driver = value ? value : SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER); |
| 45 | |
| 46 | set_callback(); |
| 47 | |
| 48 | if (driver == NULL || SDL_strcmp(driver, "portal" ) == 0) { |
| 49 | if (SDL_Portal_detect()) { |
| 50 | detected_function = SDL_Portal_ShowFileDialogWithProperties; |
| 51 | return 1; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (driver == NULL || SDL_strcmp(driver, "zenity" ) == 0) { |
| 56 | if (SDL_Zenity_detect()) { |
| 57 | detected_function = SDL_Zenity_ShowFileDialogWithProperties; |
| 58 | return 2; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | SDL_SetError("File dialog driver unsupported (supported values for SDL_HINT_FILE_DIALOG_DRIVER are 'zenity' and 'portal')" ); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | void SDLCALL hint_callback(void *userdata, const char *name, const char *oldValue, const char *newValue) |
| 67 | { |
| 68 | detect_available_methods(newValue); |
| 69 | } |
| 70 | |
| 71 | void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props) |
| 72 | { |
| 73 | // Call detect_available_methods() again each time in case the situation changed |
| 74 | if (!detected_function && !detect_available_methods(NULL)) { |
| 75 | // SetError() done by detect_available_methods() |
| 76 | callback(userdata, NULL, -1); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | detected_function(type, callback, userdata, props); |
| 81 | } |
| 82 | |