System.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (c) 2006-2014 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_LINUX)
  26. #include <spawn.h>
  27. //#include <stdlib.h>
  28. //#include <unistd.h>
  29. #include <signal.h>
  30. #include <sys/wait.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. namespace love
  38. {
  39. namespace system
  40. {
  41. System::System()
  42. {
  43. #if defined(LOVE_LINUX)
  44. // Enable automatic cleanup of zombie processes
  45. struct sigaction act = {0};
  46. sigemptyset(&act.sa_mask);
  47. act.sa_handler = SIG_DFL;
  48. act.sa_flags = SA_NOCLDWAIT;
  49. // Requires linux 2.6 or higher, so anything remotely modern
  50. sigaction(SIGCHLD, &act, nullptr);
  51. #endif
  52. }
  53. std::string System::getOS() const
  54. {
  55. #if defined(LOVE_MACOSX)
  56. return "OS X";
  57. #elif defined(LOVE_WINDOWS)
  58. return "Windows";
  59. #elif defined(LOVE_LINUX)
  60. return "Linux";
  61. #else
  62. return "Unknown";
  63. #endif
  64. }
  65. extern "C"
  66. {
  67. extern char **environ; // The environment, always available
  68. }
  69. bool System::openURL(const std::string &url) const
  70. {
  71. #if defined(LOVE_MACOSX)
  72. bool success = false;
  73. // We could be lazy and use system("open " + url), but this is safer.
  74. CFURLRef cfurl = CFURLCreateWithBytes(nullptr,
  75. (const UInt8 *) url.c_str(),
  76. url.length(),
  77. kCFStringEncodingUTF8,
  78. nullptr);
  79. success = LSOpenCFURLRef(cfurl, nullptr) == noErr;
  80. CFRelease(cfurl);
  81. return success;
  82. #elif defined(LOVE_LINUX)
  83. pid_t pid;
  84. const char *argv[] = {"xdg-open", url.c_str(), nullptr};
  85. // Note: at the moment this process inherits our file descriptors.
  86. // Note: the below const_cast is really ugly as well.
  87. if (posix_spawnp(&pid, "xdg-open", nullptr, nullptr, const_cast<char **>(argv), environ) != 0)
  88. return false;
  89. // Check if xdg-open already completed (or failed.)
  90. int status = 0;
  91. if (waitpid(pid, &status, WNOHANG) > 0)
  92. return (status == 0);
  93. else
  94. // We can't tell what actually happens without waiting for
  95. // the process to finish, which could take forever (literally).
  96. return true;
  97. #elif defined(LOVE_WINDOWS)
  98. // Unicode-aware WinAPI functions don't accept UTF-8, so we need to convert.
  99. std::wstring wurl = to_widestr(url);
  100. HINSTANCE result = ShellExecuteW(nullptr,
  101. L"open",
  102. wurl.c_str(),
  103. nullptr,
  104. nullptr,
  105. SW_SHOW);
  106. return (int) result > 32;
  107. #endif
  108. }
  109. bool System::getConstant(const char *in, System::PowerState &out)
  110. {
  111. return powerStates.find(in, out);
  112. }
  113. bool System::getConstant(System::PowerState in, const char *&out)
  114. {
  115. return powerStates.find(in, out);
  116. }
  117. StringMap<System::PowerState, System::POWER_MAX_ENUM>::Entry System::powerEntries[] =
  118. {
  119. {"unknown", System::POWER_UNKNOWN},
  120. {"battery", System::POWER_BATTERY},
  121. {"nobattery", System::POWER_NO_BATTERY},
  122. {"charging", System::POWER_CHARGING},
  123. {"charged", System::POWER_CHARGED},
  124. };
  125. StringMap<System::PowerState, System::POWER_MAX_ENUM> System::powerStates(System::powerEntries, sizeof(System::powerEntries));
  126. } // system
  127. } // love