wrap_Window.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #include "wrap_Window.h"
  21. #include "sdl/Window.h"
  22. namespace love
  23. {
  24. namespace window
  25. {
  26. #define instance() (Module::getInstance<Window>(Module::M_WINDOW))
  27. int w_getDisplayCount(lua_State *L)
  28. {
  29. lua_pushinteger(L, instance()->getDisplayCount());
  30. return 1;
  31. }
  32. int w_getDisplayName(lua_State *L)
  33. {
  34. int index = luaL_checkint(L, 1) - 1;
  35. const char *name = nullptr;
  36. luax_catchexcept(L, [&](){ name = instance()->getDisplayName(index); });
  37. lua_pushstring(L, name);
  38. return 1;
  39. }
  40. static const char *settingName(Window::Setting setting)
  41. {
  42. const char *name = nullptr;
  43. Window::getConstant(setting, name);
  44. return name;
  45. }
  46. int w_setMode(lua_State *L)
  47. {
  48. int w = luaL_checkint(L, 1);
  49. int h = luaL_checkint(L, 2);
  50. if (lua_isnoneornil(L, 3))
  51. {
  52. luax_pushboolean(L, instance()->setWindow(w, h, 0));
  53. return 1;
  54. }
  55. luaL_checktype(L, 3, LUA_TTABLE);
  56. // We want to error for invalid / misspelled window attributes.
  57. lua_pushnil(L);
  58. while (lua_next(L, 3))
  59. {
  60. if (lua_type(L, -2) != LUA_TSTRING)
  61. return luax_typerror(L, -2, "string");
  62. const char *key = luaL_checkstring(L, -2);
  63. Window::Setting setting;
  64. if (!Window::getConstant(key, setting))
  65. return luaL_error(L, "Invalid window setting: %s", key);
  66. lua_pop(L, 1);
  67. }
  68. WindowSettings settings;
  69. lua_getfield(L, 3, settingName(Window::SETTING_FULLSCREEN_TYPE));
  70. if (!lua_isnoneornil(L, -1))
  71. {
  72. const char *typestr = luaL_checkstring(L, -1);
  73. if (!Window::getConstant(typestr, settings.fstype))
  74. return luaL_error(L, "Invalid fullscreen type: %s", typestr);
  75. }
  76. else
  77. {
  78. // Default to "normal" fullscreen.
  79. settings.fstype = Window::FULLSCREEN_TYPE_NORMAL;
  80. }
  81. lua_pop(L, 1);
  82. settings.fullscreen = luax_boolflag(L, 3, settingName(Window::SETTING_FULLSCREEN), false);
  83. settings.vsync = luax_boolflag(L, 3, settingName(Window::SETTING_VSYNC), true);
  84. settings.msaa = luax_intflag(L, 3, settingName(Window::SETTING_MSAA), 0);
  85. settings.resizable = luax_boolflag(L, 3, settingName(Window::SETTING_RESIZABLE), false);
  86. settings.minwidth = luax_intflag(L, 3, settingName(Window::SETTING_MIN_WIDTH), 1);
  87. settings.minheight = luax_intflag(L, 3, settingName(Window::SETTING_MIN_HEIGHT), 1);
  88. settings.borderless = luax_boolflag(L, 3, settingName(Window::SETTING_BORDERLESS), false);
  89. settings.centered = luax_boolflag(L, 3, settingName(Window::SETTING_CENTERED), true);
  90. settings.display = luax_intflag(L, 3, settingName(Window::SETTING_DISPLAY), 1) - 1;
  91. settings.highdpi = luax_boolflag(L, 3, settingName(Window::SETTING_HIGHDPI), false);
  92. settings.sRGB = luax_boolflag(L, 3, settingName(Window::SETTING_SRGB), false);
  93. // We don't explicitly set the refresh rate, it's "read-only".
  94. // For backward-compatibility. TODO: remove!
  95. int fsaa = luax_intflag(L, 3, settingName(Window::SETTING_FSAA), 0);
  96. if (fsaa > settings.msaa) settings.msaa = fsaa;
  97. luax_catchexcept(L,
  98. [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
  99. );
  100. return 1;
  101. }
  102. int w_getMode(lua_State *L)
  103. {
  104. int w, h;
  105. WindowSettings settings;
  106. instance()->getWindow(w, h, settings);
  107. lua_pushnumber(L, w);
  108. lua_pushnumber(L, h);
  109. lua_newtable(L);
  110. const char *fstypestr = "normal";
  111. Window::getConstant(settings.fstype, fstypestr);
  112. lua_pushstring(L, fstypestr);
  113. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN_TYPE));
  114. luax_pushboolean(L, settings.fullscreen);
  115. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
  116. luax_pushboolean(L, settings.vsync);
  117. lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
  118. lua_pushinteger(L, settings.msaa);
  119. lua_setfield(L, -2, settingName(Window::SETTING_MSAA));
  120. lua_pushinteger(L, settings.msaa);
  121. lua_setfield(L, -2, settingName(Window::SETTING_FSAA)); // For backward-compatibility. TODO: remove!
  122. luax_pushboolean(L, settings.resizable);
  123. lua_setfield(L, -2, settingName(Window::SETTING_RESIZABLE));
  124. lua_pushinteger(L, settings.minwidth);
  125. lua_setfield(L, -2, settingName(Window::SETTING_MIN_WIDTH));
  126. lua_pushinteger(L, settings.minheight);
  127. lua_setfield(L, -2, settingName(Window::SETTING_MIN_HEIGHT));
  128. luax_pushboolean(L, settings.borderless);
  129. lua_setfield(L, -2, settingName(Window::SETTING_BORDERLESS));
  130. luax_pushboolean(L, settings.centered);
  131. lua_setfield(L, -2, settingName(Window::SETTING_CENTERED));
  132. // Display index is 0-based internally and 1-based in Lua.
  133. lua_pushinteger(L, settings.display + 1);
  134. lua_setfield(L, -2, settingName(Window::SETTING_DISPLAY));
  135. luax_pushboolean(L, settings.highdpi);
  136. lua_setfield(L, -2, settingName(Window::SETTING_HIGHDPI));
  137. luax_pushboolean(L, settings.sRGB);
  138. lua_setfield(L, -2, settingName(Window::SETTING_SRGB));
  139. lua_pushnumber(L, settings.refreshrate);
  140. lua_setfield(L, -2, settingName(Window::SETTING_REFRESHRATE));
  141. return 3;
  142. }
  143. int w_getFullscreenModes(lua_State *L)
  144. {
  145. int displayindex = luaL_optint(L, 1, 1) - 1;
  146. std::vector<Window::WindowSize> modes = instance()->getFullscreenSizes(displayindex);
  147. lua_createtable(L, modes.size(), 0);
  148. for (size_t i = 0; i < modes.size(); i++)
  149. {
  150. lua_pushinteger(L, i+1);
  151. lua_createtable(L, 0, 2);
  152. // Inner table attribs.
  153. lua_pushinteger(L, modes[i].width);
  154. lua_setfield(L, -2, "width");
  155. lua_pushinteger(L, modes[i].height);
  156. lua_setfield(L, -2, "height");
  157. // Inner table attribs end.
  158. lua_settable(L, -3);
  159. }
  160. return 1;
  161. }
  162. int w_setFullscreen(lua_State *L)
  163. {
  164. bool fullscreen = luax_toboolean(L, 1);
  165. Window::FullscreenType fstype = Window::FULLSCREEN_TYPE_MAX_ENUM;
  166. const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
  167. if (typestr && !Window::getConstant(typestr, fstype))
  168. return luaL_error(L, "Invalid fullscreen type: %s", typestr);
  169. bool success = false;
  170. if (fstype == Window::FULLSCREEN_TYPE_MAX_ENUM)
  171. success = instance()->setFullscreen(fullscreen);
  172. else
  173. success = instance()->setFullscreen(fullscreen, fstype);
  174. luax_pushboolean(L, success);
  175. return 1;
  176. }
  177. int w_getFullscreen(lua_State *L)
  178. {
  179. int w, h;
  180. WindowSettings settings;
  181. instance()->getWindow(w, h, settings);
  182. const char *typestr;
  183. if (!Window::getConstant(settings.fstype, typestr))
  184. luaL_error(L, "Unknown fullscreen type.");
  185. luax_pushboolean(L, settings.fullscreen);
  186. lua_pushstring(L, typestr);
  187. return 2;
  188. }
  189. int w_isCreated(lua_State *L)
  190. {
  191. luax_pushboolean(L, instance()->isCreated());
  192. return 1;
  193. }
  194. int w_getWidth(lua_State *L)
  195. {
  196. lua_pushinteger(L, instance()->getWidth());
  197. return 1;
  198. }
  199. int w_getHeight(lua_State *L)
  200. {
  201. lua_pushinteger(L, instance()->getHeight());
  202. return 1;
  203. }
  204. int w_getDimensions(lua_State *L)
  205. {
  206. lua_pushinteger(L, instance()->getWidth());
  207. lua_pushinteger(L, instance()->getHeight());
  208. return 2;
  209. }
  210. int w_getDesktopDimensions(lua_State *L)
  211. {
  212. int width = 0, height = 0;
  213. int displayindex = luaL_optint(L, 1, 1) - 1;
  214. instance()->getDesktopDimensions(displayindex, width, height);
  215. lua_pushinteger(L, width);
  216. lua_pushinteger(L, height);
  217. return 2;
  218. }
  219. int w_setIcon(lua_State *L)
  220. {
  221. image::ImageData *i = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  222. luax_pushboolean(L, instance()->setIcon(i));
  223. return 1;
  224. }
  225. int w_getIcon(lua_State *L)
  226. {
  227. image::ImageData *i = instance()->getIcon();
  228. if (i)
  229. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i);
  230. else
  231. lua_pushnil(L);
  232. return 1;
  233. }
  234. int w_setTitle(lua_State *L)
  235. {
  236. std::string title = luax_checkstring(L, 1);
  237. instance()->setWindowTitle(title);
  238. return 0;
  239. }
  240. int w_getTitle(lua_State *L)
  241. {
  242. luax_pushstring(L, instance()->getWindowTitle());
  243. return 1;
  244. }
  245. int w_hasFocus(lua_State *L)
  246. {
  247. luax_pushboolean(L, instance()->hasFocus());
  248. return 1;
  249. }
  250. int w_hasMouseFocus(lua_State *L)
  251. {
  252. luax_pushboolean(L, instance()->hasMouseFocus());
  253. return 1;
  254. }
  255. int w_isVisible(lua_State *L)
  256. {
  257. luax_pushboolean(L, instance()->isVisible());
  258. return 1;
  259. }
  260. int w_getPixelScale(lua_State *L)
  261. {
  262. lua_pushnumber(L, instance()->getPixelScale());
  263. return 1;
  264. }
  265. int w_minimize(lua_State* /*L*/)
  266. {
  267. instance()->minimize();
  268. return 0;
  269. }
  270. int w_showMessageBox(lua_State *L)
  271. {
  272. Window::MessageBoxData data = {};
  273. const char *typestr = luaL_checkstring(L, 1);
  274. if (!Window::getConstant(typestr, data.type))
  275. return luaL_error(L, "Invalid messagebox type: %s", typestr);
  276. data.title = luaL_checkstring(L, 2);
  277. data.message = luaL_checkstring(L, 3);
  278. // If we have a table argument, we assume a list of button names, which
  279. // means we should use the more complex message box API.
  280. if (lua_istable(L, 4))
  281. {
  282. size_t numbuttons = lua_objlen(L, 4);
  283. if (numbuttons == 0)
  284. return luaL_error(L, "Must have at least one messagebox button.");
  285. // Array of button names.
  286. for (size_t i = 0; i < numbuttons; i++)
  287. {
  288. lua_rawgeti(L, 4, i + 1);
  289. data.buttons.push_back(luax_checkstring(L, -1));
  290. lua_pop(L, 1);
  291. }
  292. // Optional table entry specifying the button to use when enter is pressed.
  293. lua_getfield(L, 4, "enterbutton");
  294. if (!lua_isnoneornil(L, -1))
  295. data.enterButtonIndex = luaL_checkint(L, -1) - 1;
  296. else
  297. data.enterButtonIndex = 0;
  298. lua_pop(L, 1);
  299. // Optional table entry specifying the button to use when esc is pressed.
  300. lua_getfield(L, 4, "escapebutton");
  301. if (!lua_isnoneornil(L, -1))
  302. data.escapeButtonIndex = luaL_checkint(L, -1) - 1;
  303. else
  304. data.escapeButtonIndex = (int) data.buttons.size() - 1;
  305. lua_pop(L, 1);
  306. data.attachToWindow = luax_optboolean(L, 5, true);
  307. int pressedbutton = instance()->showMessageBox(data);
  308. lua_pushinteger(L, pressedbutton + 1);
  309. }
  310. else
  311. {
  312. data.attachToWindow = luax_optboolean(L, 4, true);
  313. // Display a simple message box.
  314. bool success = instance()->showMessageBox(data.type, data.title, data.message, data.attachToWindow);
  315. luax_pushboolean(L, success);
  316. }
  317. return 1;
  318. }
  319. static const luaL_Reg functions[] =
  320. {
  321. { "getDisplayCount", w_getDisplayCount },
  322. { "getDisplayName", w_getDisplayName },
  323. { "setMode", w_setMode },
  324. { "getMode", w_getMode },
  325. { "getFullscreenModes", w_getFullscreenModes },
  326. { "setFullscreen", w_setFullscreen },
  327. { "getFullscreen", w_getFullscreen },
  328. { "isCreated", w_isCreated },
  329. { "getWidth", w_getWidth },
  330. { "getHeight", w_getHeight },
  331. { "getDimensions", w_getDimensions },
  332. { "getDesktopDimensions", w_getDesktopDimensions },
  333. { "setIcon", w_setIcon },
  334. { "getIcon", w_getIcon },
  335. { "setTitle", w_setTitle },
  336. { "getTitle", w_getTitle },
  337. { "hasFocus", w_hasFocus },
  338. { "hasMouseFocus", w_hasMouseFocus },
  339. { "isVisible", w_isVisible },
  340. { "getPixelScale", w_getPixelScale },
  341. { "minimize", w_minimize },
  342. { "showMessageBox", w_showMessageBox },
  343. { 0, 0 }
  344. };
  345. extern "C" int luaopen_love_window(lua_State *L)
  346. {
  347. Window *instance = nullptr;
  348. luax_catchexcept(L, [&](){ instance = sdl::Window::createSingleton(); });
  349. WrappedModule w;
  350. w.module = instance;
  351. w.name = "window";
  352. w.flags = MODULE_T;
  353. w.functions = functions;
  354. w.types = 0;
  355. return luax_register_module(L, w);
  356. }
  357. } // window
  358. } // love