|
| 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 | +#ifdef SDL_PLATFORM_LINUX |
| 24 | +#include <stdio.h> |
| 25 | +#include <unistd.h> |
| 26 | + |
| 27 | +bool SDL_IsUbuntuTouch(void) |
| 28 | +{ |
| 29 | + const char *xdg_session_desktop = SDL_getenv("XDG_SESSION_DESKTOP"); |
| 30 | + |
| 31 | + if (!xdg_session_desktop) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return SDL_strcmp(xdg_session_desktop, "ubuntu-touch") == 0; |
| 36 | +} |
| 37 | + |
| 38 | +#define UT_APPINFO_APPNAME 0 |
| 39 | +#define UT_APPINFO_HOOKNAME 1 |
| 40 | +#define UT_APPINFO_APPVERSION 2 |
| 41 | + |
| 42 | +static char **SDL_ubuntu_touch_get_app_info(void) |
| 43 | +{ |
| 44 | + static char info[3][257]; |
| 45 | + static bool set = false; |
| 46 | + char *it, *it2; |
| 47 | + |
| 48 | + if (set) { |
| 49 | + return info; |
| 50 | + } |
| 51 | + |
| 52 | + if (!SDL_IsUbuntuTouch()) { |
| 53 | + SDL_SetError("Not running under Ubuntu Touch"); |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + // Format is <appname>_<hookname>_<version>, like myapp.myname_myapp_1.0.0. |
| 58 | + // None of those are allowed to have underscores. |
| 59 | + const char *app_id = SDL_getenv("APP_ID"); |
| 60 | + |
| 61 | + if (!app_id) { |
| 62 | + SDL_SetError("Environment variable 'APP_ID' not set by the OS"); |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + |
| 66 | + it = SDL_strchr(app_id, '_'); |
| 67 | + |
| 68 | + if (!*it) { |
| 69 | + SDL_SetError("Malformed APP_ID"); |
| 70 | + return NULL; |
| 71 | + } |
| 72 | + |
| 73 | + it2 = SDL_strchr(it + 1, '_'); |
| 74 | + |
| 75 | + if (!*it2) { |
| 76 | + SDL_SetError("Malformed APP_ID"); |
| 77 | + return NULL; |
| 78 | + } |
| 79 | + |
| 80 | + if (SDL_snprintf(info[UT_APPINFO_APPNAME], sizeof(info[UT_APPINFO_APPNAME]), "%.*s", (int) (it - app_id) + 1, app_id) > 255) { |
| 81 | + SDL_SetError("APP_ID appname too long"); |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + |
| 85 | + if (SDL_snprintf(info[UT_APPINFO_HOOKNAME], sizeof(info[UT_APPINFO_HOOKNAME]), "%.*s", (int) (it2 - it) + 1, it) > 255) { |
| 86 | + SDL_SetError("APP_ID hook name too long"); |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + |
| 90 | + if (SDL_snprintf(info[UT_APPINFO_APPVERSION], sizeof(info[UT_APPINFO_APPVERSION]), "%s", it2) > 255) { |
| 91 | + SDL_SetError("APP_ID version too long"); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + set = true; |
| 96 | + return info; |
| 97 | +} |
| 98 | + |
| 99 | +const char *SDL_GetUbuntuTouchAppID(void) |
| 100 | +{ |
| 101 | + char **info = SDL_ubuntu_touch_get_app_info(); |
| 102 | + |
| 103 | + if (!info) { |
| 104 | + return NULL; |
| 105 | + } |
| 106 | + |
| 107 | + // On Ubuntu Touch, the ID of the app is called the "app name", whereas the |
| 108 | + // human-readable name is called the "app title". |
| 109 | + return info[UT_APPINFO_APPNAME]; |
| 110 | +} |
| 111 | + |
| 112 | +const char *SDL_GetUbuntuTouchAppHook(void) |
| 113 | +{ |
| 114 | + char **info = SDL_ubuntu_touch_get_app_info(); |
| 115 | + |
| 116 | + if (!info) { |
| 117 | + return NULL; |
| 118 | + } |
| 119 | + |
| 120 | + return info[UT_APPINFO_HOOKNAME]; |
| 121 | +} |
| 122 | + |
| 123 | +const char *SDL_GetUbuntuTouchAppVersion(void) |
| 124 | +{ |
| 125 | + char **info = SDL_ubuntu_touch_get_app_info(); |
| 126 | + |
| 127 | + if (!info) { |
| 128 | + return NULL; |
| 129 | + } |
| 130 | + |
| 131 | + return info[UT_APPINFO_APPVERSION]; |
| 132 | +} |
| 133 | + |
| 134 | +#endif |
0 commit comments