wrap_System.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2006-2022 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 "wrap_System.h"
  22. #include "sdl/System.h"
  23. namespace love
  24. {
  25. namespace system
  26. {
  27. #define instance() (Module::getInstance<System>(Module::M_SYSTEM))
  28. int w_getOS(lua_State *L)
  29. {
  30. luax_pushstring(L, instance()->getOS());
  31. return 1;
  32. }
  33. int w_getProcessorCount(lua_State *L)
  34. {
  35. lua_pushinteger(L, instance()->getProcessorCount());
  36. return 1;
  37. }
  38. int w_setClipboardText(lua_State *L)
  39. {
  40. const char *text = luaL_checkstring(L, 1);
  41. luax_catchexcept(L, [&]() { instance()->setClipboardText(text); });
  42. return 0;
  43. }
  44. int w_getClipboardText(lua_State *L)
  45. {
  46. std::string text;
  47. luax_catchexcept(L, [&]() { text = instance()->getClipboardText(); });
  48. luax_pushstring(L, text);
  49. return 1;
  50. }
  51. int w_getPowerInfo(lua_State *L)
  52. {
  53. int seconds = -1, percent = -1;
  54. const char *str;
  55. System::PowerState state = instance()->getPowerInfo(seconds, percent);
  56. if (!System::getConstant(state, str))
  57. str = "unknown";
  58. lua_pushstring(L, str);
  59. if (percent >= 0)
  60. lua_pushinteger(L, percent);
  61. else
  62. lua_pushnil(L);
  63. if (seconds >= 0)
  64. lua_pushinteger(L, seconds);
  65. else
  66. lua_pushnil(L);
  67. return 3;
  68. }
  69. int w_openURL(lua_State *L)
  70. {
  71. std::string url = luax_checkstring(L, 1);
  72. luax_pushboolean(L, instance()->openURL(url));
  73. return 1;
  74. }
  75. int w_vibrate(lua_State *L)
  76. {
  77. double seconds = luaL_optnumber(L, 1, 0.5);
  78. instance()->vibrate(seconds);
  79. return 0;
  80. }
  81. int w_hasBackgroundMusic(lua_State *L)
  82. {
  83. lua_pushboolean(L, instance()->hasBackgroundMusic());
  84. return 1;
  85. }
  86. int w_getPreferredLocales(lua_State* L)
  87. {
  88. int i = 1;
  89. std::vector<std::string> locales = instance()->getPreferredLocales();
  90. lua_createtable(L, locales.size(), 0);
  91. for (const std::string& str: locales)
  92. {
  93. luax_pushstring(L, str);
  94. lua_rawseti(L, -2, i++);
  95. }
  96. return 1;
  97. }
  98. static const luaL_Reg functions[] =
  99. {
  100. { "getOS", w_getOS },
  101. { "getProcessorCount", w_getProcessorCount },
  102. { "setClipboardText", w_setClipboardText },
  103. { "getClipboardText", w_getClipboardText },
  104. { "getPowerInfo", w_getPowerInfo },
  105. { "openURL", w_openURL },
  106. { "vibrate", w_vibrate },
  107. { "hasBackgroundMusic", w_hasBackgroundMusic },
  108. { "getPreferredLocales", w_getPreferredLocales },
  109. { 0, 0 }
  110. };
  111. extern "C" int luaopen_love_system(lua_State *L)
  112. {
  113. System *instance = instance();
  114. if (instance == nullptr)
  115. {
  116. instance = new love::system::sdl::System();
  117. }
  118. else
  119. instance->retain();
  120. WrappedModule w;
  121. w.module = instance;
  122. w.name = "system";
  123. w.type = &Module::type;
  124. w.functions = functions;
  125. w.types = nullptr;
  126. return luax_register_module(L, w);
  127. }
  128. } // system
  129. } // love