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