System.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2006-2016 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 "common/config.h"
  22. #include "System.h"
  23. #if defined(LOVE_MACOSX)
  24. #include <CoreServices/CoreServices.h>
  25. #elif defined(LOVE_IOS)
  26. #include "common/ios.h"
  27. #elif defined(LOVE_LINUX) || defined(LOVE_ANDROID)
  28. #include <signal.h>
  29. #include <sys/wait.h>
  30. #include <errno.h>
  31. #elif defined(LOVE_WINDOWS)
  32. #include "common/utf8.h"
  33. #include <shlobj.h>
  34. #include <shellapi.h>
  35. #pragma comment(lib, "shell32.lib")
  36. #endif
  37. #if defined(LOVE_ANDROID)
  38. #include "common/android.h"
  39. #elif defined(LOVE_LINUX)
  40. #include <spawn.h>
  41. #endif
  42. #if defined(LOVE_LINUX)
  43. static void sigchld_handler(int sig)
  44. {
  45. // Because waitpid can set errno, we need to save it.
  46. auto old = errno;
  47. // Reap whilst there are children waiting to be reaped.
  48. while (waitpid(-1, nullptr, WNOHANG) > 0)
  49. ;
  50. errno = old;
  51. }
  52. #endif
  53. namespace love
  54. {
  55. namespace system
  56. {
  57. System::System()
  58. {
  59. #if defined(LOVE_LINUX)
  60. // Enable automatic cleanup of zombie processes
  61. // NOTE: We're using our own handler, instead of SA_NOCLDWAIT because the
  62. // latter breaks wait, and thus os.execute.
  63. // NOTE: This isn't perfect, due to multithreading our SIGCHLD can happen
  64. // on a different thread than the one calling wait(), thus causing a race.
  65. struct sigaction act = {0};
  66. sigemptyset(&act.sa_mask);
  67. act.sa_handler = sigchld_handler;
  68. act.sa_flags = SA_RESTART;
  69. sigaction(SIGCHLD, &act, nullptr);
  70. #endif
  71. }
  72. std::string System::getOS() const
  73. {
  74. #if defined(LOVE_MACOSX)
  75. return "OS X";
  76. #elif defined(LOVE_IOS)
  77. return "iOS";
  78. #elif defined(LOVE_WINDOWS)
  79. return "Windows";
  80. #elif defined(LOVE_ANDROID)
  81. return "Android";
  82. #elif defined(LOVE_LINUX)
  83. return "Linux";
  84. #else
  85. return "Unknown";
  86. #endif
  87. }
  88. extern "C"
  89. {
  90. extern char **environ; // The environment, always available
  91. }
  92. bool System::openURL(const std::string &url) const
  93. {
  94. #if defined(LOVE_MACOSX)
  95. bool success = false;
  96. CFURLRef cfurl = CFURLCreateWithBytes(nullptr,
  97. (const UInt8 *) url.c_str(),
  98. url.length(),
  99. kCFStringEncodingUTF8,
  100. nullptr);
  101. success = LSOpenCFURLRef(cfurl, nullptr) == noErr;
  102. CFRelease(cfurl);
  103. return success;
  104. #elif defined(LOVE_IOS)
  105. return love::ios::openURL(url);
  106. #elif defined(LOVE_ANDROID)
  107. return love::android::openURL(url);
  108. #elif defined(LOVE_LINUX)
  109. pid_t pid;
  110. const char *argv[] = {"xdg-open", url.c_str(), nullptr};
  111. // Note: at the moment this process inherits our file descriptors.
  112. // Note: the below const_cast is really ugly as well.
  113. if (posix_spawnp(&pid, "xdg-open", nullptr, nullptr, const_cast<char **>(argv), environ) != 0)
  114. return false;
  115. // Check if xdg-open already completed (or failed.)
  116. int status = 0;
  117. if (waitpid(pid, &status, WNOHANG) > 0)
  118. return (status == 0);
  119. else
  120. // We can't tell what actually happens without waiting for
  121. // the process to finish, which could take forever (literally).
  122. return true;
  123. #elif defined(LOVE_WINDOWS)
  124. // Unicode-aware WinAPI functions don't accept UTF-8, so we need to convert.
  125. std::wstring wurl = to_widestr(url);
  126. HINSTANCE result = ShellExecuteW(nullptr,
  127. L"open",
  128. wurl.c_str(),
  129. nullptr,
  130. nullptr,
  131. SW_SHOW);
  132. return (int) result > 32;
  133. #endif
  134. }
  135. void System::vibrate(double seconds) const
  136. {
  137. #ifdef LOVE_ANDROID
  138. love::android::vibrate(seconds);
  139. #elif defined(LOVE_IOS)
  140. love::ios::vibrate();
  141. #else
  142. LOVE_UNUSED(seconds);
  143. #endif
  144. }
  145. bool System::getConstant(const char *in, System::PowerState &out)
  146. {
  147. return powerStates.find(in, out);
  148. }
  149. bool System::getConstant(System::PowerState in, const char *&out)
  150. {
  151. return powerStates.find(in, out);
  152. }
  153. StringMap<System::PowerState, System::POWER_MAX_ENUM>::Entry System::powerEntries[] =
  154. {
  155. {"unknown", System::POWER_UNKNOWN},
  156. {"battery", System::POWER_BATTERY},
  157. {"nobattery", System::POWER_NO_BATTERY},
  158. {"charging", System::POWER_CHARGING},
  159. {"charged", System::POWER_CHARGED},
  160. };
  161. StringMap<System::PowerState, System::POWER_MAX_ENUM> System::powerStates(System::powerEntries, sizeof(System::powerEntries));
  162. } // system
  163. } // love