SDL_sysmain_runapp.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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. extern "C" {
  20. #include "../../core/gdk/SDL_gdk.h"
  21. #include "../../core/windows/SDL_windows.h"
  22. #include "../../events/SDL_events_c.h"
  23. }
  24. #include <XGameRuntime.h>
  25. #include <xsapi-c/services_c.h>
  26. #include <shellapi.h> // CommandLineToArgvW()
  27. #include <appnotify.h>
  28. // Pop up an out of memory message, returns to Windows
  29. static BOOL OutOfMemory(void)
  30. {
  31. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
  32. return FALSE;
  33. }
  34. /* Gets the arguments with GetCommandLine, converts them to argc and argv
  35. and calls SDL_main */
  36. extern "C"
  37. int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *reserved)
  38. {
  39. LPWSTR *argvw;
  40. char **argv;
  41. int i, argc, result;
  42. HRESULT hr;
  43. XTaskQueueHandle taskQueue;
  44. argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
  45. if (argvw == NULL) {
  46. return OutOfMemory();
  47. }
  48. /* Note that we need to be careful about how we allocate/free memory here.
  49. * If the application calls SDL_SetMemoryFunctions(), we can't rely on
  50. * SDL_free() to use the same allocator after SDL_main() returns.
  51. */
  52. // Parse it into argv and argc
  53. argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
  54. if (argv == NULL) {
  55. return OutOfMemory();
  56. }
  57. for (i = 0; i < argc; ++i) {
  58. const int utf8size = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
  59. if (!utf8size) { // uhoh?
  60. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
  61. return -1;
  62. }
  63. argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, utf8size); // this size includes the null-terminator character.
  64. if (!argv[i]) {
  65. return OutOfMemory();
  66. }
  67. if (WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argv[i], utf8size, NULL, NULL) == 0) { // failed? uhoh!
  68. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
  69. return -1;
  70. }
  71. }
  72. argv[i] = NULL;
  73. LocalFree(argvw);
  74. hr = XGameRuntimeInitialize();
  75. if (SUCCEEDED(hr) && SDL_GetGDKTaskQueue(&taskQueue)) {
  76. Uint32 titleid = 0;
  77. char scidBuffer[64];
  78. XblInitArgs xblArgs;
  79. XTaskQueueSetCurrentProcessTaskQueue(taskQueue);
  80. // Try to get the title ID and initialize Xbox Live
  81. hr = XGameGetXboxTitleId(&titleid);
  82. if (SUCCEEDED(hr)) {
  83. SDL_zero(xblArgs);
  84. xblArgs.queue = taskQueue;
  85. SDL_snprintf(scidBuffer, 64, "00000000-0000-0000-0000-0000%08X", titleid);
  86. xblArgs.scid = scidBuffer;
  87. hr = XblInitialize(&xblArgs);
  88. } else {
  89. SDL_SetError("[GDK] Unable to get titleid. Will not call XblInitialize. Check MicrosoftGame.config!");
  90. }
  91. SDL_SetMainReady();
  92. if (!GDK_RegisterChangeNotifications()) {
  93. return -1;
  94. }
  95. // Run the application main() code
  96. result = mainFunction(argc, argv);
  97. GDK_UnregisterChangeNotifications();
  98. // !!! FIXME: This follows the docs exactly, but for some reason still leaks handles on exit?
  99. // Terminate the task queue and dispatch any pending tasks
  100. XTaskQueueTerminate(taskQueue, false, nullptr, nullptr);
  101. while (XTaskQueueDispatch(taskQueue, XTaskQueuePort::Completion, 0))
  102. ;
  103. XTaskQueueCloseHandle(taskQueue);
  104. XGameRuntimeUninitialize();
  105. } else {
  106. #ifdef SDL_PLATFORM_WINGDK
  107. if (hr == E_GAMERUNTIME_DLL_NOT_FOUND) {
  108. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "[GDK] Gaming Runtime library not found (xgameruntime.dll)", NULL);
  109. } else {
  110. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "[GDK] Could not initialize - aborting", NULL);
  111. }
  112. #else
  113. SDL_assert_always(0 && "[GDK] Could not initialize - aborting");
  114. #endif
  115. result = -1;
  116. }
  117. // Free argv, to avoid memory leak
  118. for (i = 0; i < argc; ++i) {
  119. HeapFree(GetProcessHeap(), 0, argv[i]);
  120. }
  121. HeapFree(GetProcessHeap(), 0, argv);
  122. return result;
  123. }