System.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  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. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "System.h"
  22. #include "window/Window.h"
  23. // SDL
  24. #if __has_include(<SDL3/SDL_clipboard.h>)
  25. #include <SDL3/SDL_clipboard.h>
  26. #include <SDL3/SDL_cpuinfo.h>
  27. #include <SDL3/SDL_version.h>
  28. #include <SDL3/SDL_locale.h>
  29. #else
  30. #include <SDL_clipboard.h>
  31. #include <SDL_cpuinfo.h>
  32. #include <SDL_version.h>
  33. #if SDL_VERSION_ATLEAST(2, 0, 14)
  34. #include <SDL_locale.h>
  35. #endif
  36. #endif
  37. namespace love
  38. {
  39. namespace system
  40. {
  41. namespace sdl
  42. {
  43. System::System()
  44. : love::system::System("love.system.sdl")
  45. {
  46. }
  47. int System::getProcessorCount() const
  48. {
  49. #if SDL_VERSION_ATLEAST(3, 0, 0)
  50. return SDL_GetNumLogicalCPUCores();
  51. #else
  52. return SDL_GetCPUCount();
  53. #endif
  54. }
  55. bool System::isWindowOpen() const
  56. {
  57. auto window = Module::getInstance<window::Window>(M_WINDOW);
  58. return window != nullptr && window->isOpen();
  59. }
  60. void System::setClipboardText(const std::string &text) const
  61. {
  62. // SDL requires the video subsystem to be initialized and a window to be
  63. // opened in order for clipboard text to work, on at least some platforms.
  64. if (!isWindowOpen())
  65. throw love::Exception("A window must be created in order for setClipboardText to function properly.");
  66. SDL_SetClipboardText(text.c_str());
  67. }
  68. std::string System::getClipboardText() const
  69. {
  70. if (!isWindowOpen())
  71. throw love::Exception("A window must be created in order for getClipboardText to function properly.");
  72. std::string text("");
  73. char *ctext = SDL_GetClipboardText();
  74. if (ctext)
  75. {
  76. text = std::string(ctext);
  77. SDL_free(ctext);
  78. }
  79. return text;
  80. }
  81. love::system::System::PowerState System::getPowerInfo(int &seconds, int &percent) const
  82. {
  83. SDL_PowerState sdlstate = SDL_GetPowerInfo(&seconds, &percent);
  84. PowerState state = POWER_UNKNOWN;
  85. powerStates.find(sdlstate, state);
  86. return state;
  87. }
  88. std::vector<std::string> System::getPreferredLocales() const
  89. {
  90. std::vector<std::string> result;
  91. #if SDL_VERSION_ATLEAST(3, 0, 0)
  92. int count = 0;
  93. SDL_Locale **locales = SDL_GetPreferredLocales(&count);
  94. for (int i = 0; i < count; i++)
  95. {
  96. SDL_Locale *locale = locales[i];
  97. if (locale->country)
  98. result.push_back(std::string(locale->language) + "_" + std::string(locale->country));
  99. else
  100. result.push_back(locale->language);
  101. }
  102. SDL_free(locales);
  103. #elif SDL_VERSION_ATLEAST(2, 0, 14)
  104. SDL_Locale *locales = SDL_GetPreferredLocales();
  105. if (locales)
  106. {
  107. for (SDL_Locale* locale = locales; locale->language != nullptr; locale++)
  108. {
  109. if (locale->country)
  110. result.push_back(std::string(locale->language) + "_" + std::string(locale->country));
  111. else
  112. result.push_back(locale->language);
  113. }
  114. SDL_free(locales);
  115. }
  116. #endif
  117. return result;
  118. }
  119. EnumMap<System::PowerState, SDL_PowerState, System::POWER_MAX_ENUM>::Entry System::powerEntries[] =
  120. {
  121. {System::POWER_UNKNOWN, SDL_POWERSTATE_UNKNOWN},
  122. {System::POWER_BATTERY, SDL_POWERSTATE_ON_BATTERY},
  123. {System::POWER_NO_BATTERY, SDL_POWERSTATE_NO_BATTERY},
  124. {System::POWER_CHARGING, SDL_POWERSTATE_CHARGING},
  125. {System::POWER_CHARGED, SDL_POWERSTATE_CHARGED},
  126. };
  127. EnumMap<System::PowerState, SDL_PowerState, System::POWER_MAX_ENUM> System::powerStates(System::powerEntries, sizeof(System::powerEntries));
  128. } // sdl
  129. } // system
  130. } // love