SDL_appid.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_appid.h"
  20. #include <unistd.h>
  21. const char *SDL_GetExeName(void)
  22. {
  23. static const char *proc_name = NULL;
  24. // TODO: Use a fallback if BSD has no mounted procfs (OpenBSD has no procfs at all)
  25. if (!proc_name) {
  26. #if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) || defined (SDL_PLATFORM_NETBSD)
  27. static char linkfile[1024];
  28. int linksize;
  29. #if defined(SDL_PLATFORM_LINUX)
  30. const char *proc_path = "/proc/self/exe";
  31. #elif defined(SDL_PLATFORM_FREEBSD)
  32. const char *proc_path = "/proc/curproc/file";
  33. #elif defined(SDL_PLATFORM_NETBSD)
  34. const char *proc_path = "/proc/curproc/exe";
  35. #endif
  36. linksize = readlink(proc_path, linkfile, sizeof(linkfile) - 1);
  37. if (linksize > 0) {
  38. linkfile[linksize] = '\0';
  39. proc_name = SDL_strrchr(linkfile, '/');
  40. if (proc_name) {
  41. ++proc_name;
  42. } else {
  43. proc_name = linkfile;
  44. }
  45. }
  46. #endif
  47. }
  48. return proc_name;
  49. }
  50. const char *SDL_GetAppID(void)
  51. {
  52. const char *id_str = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
  53. if (!id_str) {
  54. // If the hint isn't set, try to use the application's executable name
  55. id_str = SDL_GetExeName();
  56. }
  57. if (!id_str) {
  58. // Finally, use the default we've used forever
  59. id_str = "SDL_App";
  60. }
  61. return id_str;
  62. }