wrap_Window.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /**
  2. * Copyright (c) 2006-2018 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 = (int) luaL_checkinteger(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. static int readWindowSettings(lua_State *L, int idx, WindowSettings &settings)
  47. {
  48. luax_checktablefields<Window::Setting>(L, idx, "window setting", Window::getConstant);
  49. lua_getfield(L, idx, settingName(Window::SETTING_FULLSCREEN_TYPE));
  50. if (!lua_isnoneornil(L, -1))
  51. {
  52. const char *typestr = luaL_checkstring(L, -1);
  53. if (!Window::getConstant(typestr, settings.fstype))
  54. return luax_enumerror(L, "fullscreen type", Window::getConstants(settings.fstype), typestr);
  55. }
  56. lua_pop(L, 1);
  57. settings.fullscreen = luax_boolflag(L, idx, settingName(Window::SETTING_FULLSCREEN), settings.fullscreen);
  58. settings.msaa = luax_intflag(L, idx, settingName(Window::SETTING_MSAA), settings.msaa);
  59. settings.stencil = luax_boolflag(L, idx, settingName(Window::SETTING_STENCIL), settings.stencil);
  60. settings.depth = luax_intflag(L, idx, settingName(Window::SETTING_DEPTH), settings.depth);
  61. settings.resizable = luax_boolflag(L, idx, settingName(Window::SETTING_RESIZABLE), settings.resizable);
  62. settings.minwidth = luax_intflag(L, idx, settingName(Window::SETTING_MIN_WIDTH), settings.minwidth);
  63. settings.minheight = luax_intflag(L, idx, settingName(Window::SETTING_MIN_HEIGHT), settings.minheight);
  64. settings.borderless = luax_boolflag(L, idx, settingName(Window::SETTING_BORDERLESS), settings.borderless);
  65. settings.centered = luax_boolflag(L, idx, settingName(Window::SETTING_CENTERED), settings.centered);
  66. settings.display = luax_intflag(L, idx, settingName(Window::SETTING_DISPLAY), settings.display+1) - 1;
  67. settings.highdpi = luax_boolflag(L, idx, settingName(Window::SETTING_HIGHDPI), settings.highdpi);
  68. lua_getfield(L, idx, settingName(Window::SETTING_VSYNC));
  69. if (lua_isnumber(L, -1))
  70. settings.vsync = (int) lua_tointeger(L, -1);
  71. else if (lua_isboolean(L, -1))
  72. settings.vsync = lua_toboolean(L, -1);
  73. lua_pop(L, 1);
  74. lua_getfield(L, idx, settingName(Window::SETTING_X));
  75. lua_getfield(L, idx, settingName(Window::SETTING_Y));
  76. settings.useposition = !(lua_isnoneornil(L, -2) && lua_isnoneornil(L, -1));
  77. if (settings.useposition)
  78. {
  79. settings.x = (int) luaL_optinteger(L, -2, 0);
  80. settings.y = (int) luaL_optinteger(L, -1, 0);
  81. }
  82. lua_pop(L, 2);
  83. // We don't explicitly set the refresh rate, it's "read-only".
  84. return 0;
  85. }
  86. int w_setMode(lua_State *L)
  87. {
  88. int w = (int) luaL_checkinteger(L, 1);
  89. int h = (int) luaL_checkinteger(L, 2);
  90. if (lua_isnoneornil(L, 3))
  91. {
  92. luax_catchexcept(L, [&](){ luax_pushboolean(L, instance()->setWindow(w, h, nullptr)); });
  93. return 1;
  94. }
  95. // Defaults come from WindowSettings itself.
  96. WindowSettings settings;
  97. readWindowSettings(L, 3, settings);
  98. luax_catchexcept(L,
  99. [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
  100. );
  101. return 1;
  102. }
  103. int w_updateMode(lua_State *L)
  104. {
  105. int w, h;
  106. WindowSettings settings;
  107. instance()->getWindow(w, h, settings);
  108. if (lua_gettop(L) == 0)
  109. return luaL_error(L, "Expected at least one argument");
  110. int idx = 1;
  111. if (lua_isnumber(L, 1))
  112. {
  113. idx = 3;
  114. w = (int) luaL_checkinteger(L, 1);
  115. h = (int) luaL_checkinteger(L, 2);
  116. }
  117. if (!lua_isnoneornil(L, idx))
  118. readWindowSettings(L, idx, settings);
  119. luax_catchexcept(L,
  120. [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
  121. );
  122. return 1;
  123. }
  124. int w_getMode(lua_State *L)
  125. {
  126. int w, h;
  127. WindowSettings settings;
  128. instance()->getWindow(w, h, settings);
  129. lua_pushnumber(L, w);
  130. lua_pushnumber(L, h);
  131. if (lua_istable(L, 1))
  132. lua_pushvalue(L, 1);
  133. else
  134. lua_newtable(L);
  135. const char *fstypestr = "desktop";
  136. Window::getConstant(settings.fstype, fstypestr);
  137. lua_pushstring(L, fstypestr);
  138. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN_TYPE));
  139. luax_pushboolean(L, settings.fullscreen);
  140. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
  141. lua_pushinteger(L, settings.vsync);
  142. lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
  143. lua_pushinteger(L, settings.msaa);
  144. lua_setfield(L, -2, settingName(Window::SETTING_MSAA));
  145. luax_pushboolean(L, settings.stencil);
  146. lua_setfield(L, -2, settingName(Window::SETTING_STENCIL));
  147. lua_pushinteger(L, settings.depth);
  148. lua_setfield(L, -2, settingName(Window::SETTING_DEPTH));
  149. luax_pushboolean(L, settings.resizable);
  150. lua_setfield(L, -2, settingName(Window::SETTING_RESIZABLE));
  151. lua_pushinteger(L, settings.minwidth);
  152. lua_setfield(L, -2, settingName(Window::SETTING_MIN_WIDTH));
  153. lua_pushinteger(L, settings.minheight);
  154. lua_setfield(L, -2, settingName(Window::SETTING_MIN_HEIGHT));
  155. luax_pushboolean(L, settings.borderless);
  156. lua_setfield(L, -2, settingName(Window::SETTING_BORDERLESS));
  157. luax_pushboolean(L, settings.centered);
  158. lua_setfield(L, -2, settingName(Window::SETTING_CENTERED));
  159. // Display index is 0-based internally and 1-based in Lua.
  160. lua_pushinteger(L, settings.display + 1);
  161. lua_setfield(L, -2, settingName(Window::SETTING_DISPLAY));
  162. luax_pushboolean(L, settings.highdpi);
  163. lua_setfield(L, -2, settingName(Window::SETTING_HIGHDPI));
  164. lua_pushnumber(L, settings.refreshrate);
  165. lua_setfield(L, -2, settingName(Window::SETTING_REFRESHRATE));
  166. lua_pushinteger(L, settings.x);
  167. lua_setfield(L, -2, settingName(Window::SETTING_X));
  168. lua_pushinteger(L, settings.y);
  169. lua_setfield(L, -2, settingName(Window::SETTING_Y));
  170. return 3;
  171. }
  172. int w_getDisplayOrientation(lua_State *L)
  173. {
  174. int displayindex = 0;
  175. if (!lua_isnoneornil(L, 1))
  176. displayindex = (int) luaL_checkinteger(L, 1) - 1;
  177. else
  178. {
  179. int x, y;
  180. instance()->getPosition(x, y, displayindex);
  181. }
  182. const char *orientationstr = nullptr;
  183. if (!Window::getConstant(instance()->getDisplayOrientation(displayindex), orientationstr))
  184. return luaL_error(L, "Unknown display orientation type.");
  185. lua_pushstring(L, orientationstr);
  186. return 1;
  187. }
  188. int w_getFullscreenModes(lua_State *L)
  189. {
  190. int displayindex = 0;
  191. if (!lua_isnoneornil(L, 1))
  192. displayindex = (int) luaL_checkinteger(L, 1) - 1;
  193. else
  194. {
  195. int x, y;
  196. instance()->getPosition(x, y, displayindex);
  197. }
  198. std::vector<Window::WindowSize> modes = instance()->getFullscreenSizes(displayindex);
  199. lua_createtable(L, (int) modes.size(), 0);
  200. for (size_t i = 0; i < modes.size(); i++)
  201. {
  202. lua_pushinteger(L, i + 1);
  203. lua_createtable(L, 0, 2);
  204. // Inner table attribs.
  205. lua_pushinteger(L, modes[i].width);
  206. lua_setfield(L, -2, "width");
  207. lua_pushinteger(L, modes[i].height);
  208. lua_setfield(L, -2, "height");
  209. // Inner table attribs end.
  210. lua_settable(L, -3);
  211. }
  212. return 1;
  213. }
  214. int w_setFullscreen(lua_State *L)
  215. {
  216. bool fullscreen = luax_checkboolean(L, 1);
  217. Window::FullscreenType fstype = Window::FULLSCREEN_MAX_ENUM;
  218. const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
  219. if (typestr && !Window::getConstant(typestr, fstype))
  220. return luax_enumerror(L, "fullscreen type", Window::getConstants(fstype), typestr);
  221. bool success = false;
  222. luax_catchexcept(L, [&]() {
  223. if (fstype == Window::FULLSCREEN_MAX_ENUM)
  224. success = instance()->setFullscreen(fullscreen);
  225. else
  226. success = instance()->setFullscreen(fullscreen, fstype);
  227. });
  228. luax_pushboolean(L, success);
  229. return 1;
  230. }
  231. int w_getFullscreen(lua_State *L)
  232. {
  233. int w, h;
  234. WindowSettings settings;
  235. instance()->getWindow(w, h, settings);
  236. const char *typestr;
  237. if (!Window::getConstant(settings.fstype, typestr))
  238. luaL_error(L, "Unknown fullscreen type.");
  239. luax_pushboolean(L, settings.fullscreen);
  240. lua_pushstring(L, typestr);
  241. return 2;
  242. }
  243. int w_isOpen(lua_State *L)
  244. {
  245. luax_pushboolean(L, instance()->isOpen());
  246. return 1;
  247. }
  248. int w_close(lua_State *L)
  249. {
  250. luax_catchexcept(L, [&]() { instance()->close(); });
  251. return 0;
  252. }
  253. int w_getDesktopDimensions(lua_State *L)
  254. {
  255. int width = 0, height = 0;
  256. int displayindex = 0;
  257. if (!lua_isnoneornil(L, 1))
  258. displayindex = (int) luaL_checkinteger(L, 1) - 1;
  259. else
  260. {
  261. int x, y;
  262. instance()->getPosition(x, y, displayindex);
  263. }
  264. instance()->getDesktopDimensions(displayindex, width, height);
  265. lua_pushinteger(L, width);
  266. lua_pushinteger(L, height);
  267. return 2;
  268. }
  269. int w_setPosition(lua_State *L)
  270. {
  271. int x = (int) luaL_checkinteger(L, 1);
  272. int y = (int) luaL_checkinteger(L, 2);
  273. int displayindex = 0;
  274. if (!lua_isnoneornil(L, 3))
  275. displayindex = (int) luaL_checkinteger(L, 3) - 1;
  276. else
  277. {
  278. int x_unused, y_unused;
  279. instance()->getPosition(x_unused, y_unused, displayindex);
  280. }
  281. instance()->setPosition(x, y, displayindex);
  282. return 0;
  283. }
  284. int w_getPosition(lua_State *L)
  285. {
  286. int x = 0;
  287. int y = 0;
  288. int displayindex = 0;
  289. instance()->getPosition(x, y, displayindex);
  290. lua_pushinteger(L, x);
  291. lua_pushinteger(L, y);
  292. lua_pushinteger(L, displayindex + 1);
  293. return 3;
  294. }
  295. int w_setIcon(lua_State *L)
  296. {
  297. image::ImageData *i = luax_checktype<image::ImageData>(L, 1);
  298. bool success = false;
  299. luax_catchexcept(L, [&]() { success = instance()->setIcon(i); });
  300. luax_pushboolean(L, success);
  301. return 1;
  302. }
  303. int w_getIcon(lua_State *L)
  304. {
  305. image::ImageData *i = instance()->getIcon();
  306. luax_pushtype(L, i);
  307. return 1;
  308. }
  309. int w_setDisplaySleepEnabled(lua_State *L)
  310. {
  311. instance()->setDisplaySleepEnabled(luax_checkboolean(L, 1));
  312. return 0;
  313. }
  314. int w_isDisplaySleepEnabled(lua_State *L)
  315. {
  316. luax_pushboolean(L, instance()->isDisplaySleepEnabled());
  317. return 1;
  318. }
  319. int w_setTitle(lua_State *L)
  320. {
  321. std::string title = luax_checkstring(L, 1);
  322. instance()->setWindowTitle(title);
  323. return 0;
  324. }
  325. int w_getTitle(lua_State *L)
  326. {
  327. luax_pushstring(L, instance()->getWindowTitle());
  328. return 1;
  329. }
  330. int w_hasFocus(lua_State *L)
  331. {
  332. luax_pushboolean(L, instance()->hasFocus());
  333. return 1;
  334. }
  335. int w_hasMouseFocus(lua_State *L)
  336. {
  337. luax_pushboolean(L, instance()->hasMouseFocus());
  338. return 1;
  339. }
  340. int w_isVisible(lua_State *L)
  341. {
  342. luax_pushboolean(L, instance()->isVisible());
  343. return 1;
  344. }
  345. int w_getDPIScale(lua_State *L)
  346. {
  347. lua_pushnumber(L, instance()->getDPIScale());
  348. return 1;
  349. }
  350. int w_toPixels(lua_State *L)
  351. {
  352. double wx = luaL_checknumber(L, 1);
  353. if (lua_isnoneornil(L, 2))
  354. {
  355. lua_pushnumber(L, instance()->toPixels(wx));
  356. return 1;
  357. }
  358. double wy = luaL_checknumber(L, 2);
  359. double px = 0.0, py = 0.0;
  360. instance()->toPixels(wx, wy, px, py);
  361. lua_pushnumber(L, px);
  362. lua_pushnumber(L, py);
  363. return 2;
  364. }
  365. int w_fromPixels(lua_State *L)
  366. {
  367. double px = luaL_checknumber(L, 1);
  368. if (lua_isnoneornil(L, 2))
  369. {
  370. lua_pushnumber(L, instance()->fromPixels(px));
  371. return 1;
  372. }
  373. double py = luaL_checknumber(L, 2);
  374. double wx = 0.0, wy = 0.0;
  375. instance()->fromPixels(px, py, wx, wy);
  376. lua_pushnumber(L, wx);
  377. lua_pushnumber(L, wy);
  378. return 2;
  379. }
  380. int w_minimize(lua_State* /*L*/)
  381. {
  382. instance()->minimize();
  383. return 0;
  384. }
  385. int w_maximize(lua_State *)
  386. {
  387. instance()->maximize();
  388. return 0;
  389. }
  390. int w_restore(lua_State *)
  391. {
  392. instance()->restore();
  393. return 0;
  394. }
  395. int w_isMaximized(lua_State *L)
  396. {
  397. luax_pushboolean(L, instance()->isMaximized());
  398. return 1;
  399. }
  400. int w_isMinimized(lua_State *L)
  401. {
  402. luax_pushboolean(L, instance()->isMinimized());
  403. return 1;
  404. }
  405. int w_showMessageBox(lua_State *L)
  406. {
  407. Window::MessageBoxData data = {};
  408. data.type = Window::MESSAGEBOX_INFO;
  409. data.title = luaL_checkstring(L, 1);
  410. data.message = luaL_checkstring(L, 2);
  411. // If we have a table argument, we assume a list of button names, which
  412. // means we should use the more complex message box API.
  413. if (lua_istable(L, 3))
  414. {
  415. size_t numbuttons = luax_objlen(L, 3);
  416. if (numbuttons == 0)
  417. return luaL_error(L, "Must have at least one messagebox button.");
  418. // Array of button names.
  419. for (size_t i = 0; i < numbuttons; i++)
  420. {
  421. lua_rawgeti(L, 3, (int) i + 1);
  422. data.buttons.push_back(luax_checkstring(L, -1));
  423. lua_pop(L, 1);
  424. }
  425. // Optional table entry specifying the button to use when enter is pressed.
  426. lua_getfield(L, 3, "enterbutton");
  427. if (!lua_isnoneornil(L, -1))
  428. data.enterButtonIndex = (int) luaL_checkinteger(L, -1) - 1;
  429. else
  430. data.enterButtonIndex = 0;
  431. lua_pop(L, 1);
  432. // Optional table entry specifying the button to use when esc is pressed.
  433. lua_getfield(L, 3, "escapebutton");
  434. if (!lua_isnoneornil(L, -1))
  435. data.escapeButtonIndex = (int) luaL_checkinteger(L, -1) - 1;
  436. else
  437. data.escapeButtonIndex = (int) data.buttons.size() - 1;
  438. lua_pop(L, 1);
  439. const char *typestr = lua_isnoneornil(L, 4) ? nullptr : luaL_checkstring(L, 4);
  440. if (typestr && !Window::getConstant(typestr, data.type))
  441. return luax_enumerror(L, "messagebox type", Window::getConstants(data.type), typestr);
  442. data.attachToWindow = luax_optboolean(L, 5, true);
  443. int pressedbutton = instance()->showMessageBox(data);
  444. lua_pushinteger(L, pressedbutton + 1);
  445. }
  446. else
  447. {
  448. const char *typestr = lua_isnoneornil(L, 3) ? nullptr : luaL_checkstring(L, 3);
  449. if (typestr && !Window::getConstant(typestr, data.type))
  450. return luax_enumerror(L, "messagebox type", Window::getConstants(data.type), typestr);
  451. data.attachToWindow = luax_optboolean(L, 4, true);
  452. // Display a simple message box.
  453. bool success = instance()->showMessageBox(data.title, data.message, data.type, data.attachToWindow);
  454. luax_pushboolean(L, success);
  455. }
  456. return 1;
  457. }
  458. int w_requestAttention(lua_State *L)
  459. {
  460. bool continuous = luax_optboolean(L, 1, false);
  461. instance()->requestAttention(continuous);
  462. return 0;
  463. }
  464. static const luaL_Reg functions[] =
  465. {
  466. { "getDisplayCount", w_getDisplayCount },
  467. { "getDisplayName", w_getDisplayName },
  468. { "setMode", w_setMode },
  469. { "updateMode", w_updateMode },
  470. { "getMode", w_getMode },
  471. { "getDisplayOrientation", w_getDisplayOrientation },
  472. { "getFullscreenModes", w_getFullscreenModes },
  473. { "setFullscreen", w_setFullscreen },
  474. { "getFullscreen", w_getFullscreen },
  475. { "isOpen", w_isOpen },
  476. { "close", w_close },
  477. { "getDesktopDimensions", w_getDesktopDimensions },
  478. { "setPosition", w_setPosition },
  479. { "getPosition", w_getPosition },
  480. { "setIcon", w_setIcon },
  481. { "getIcon", w_getIcon },
  482. { "setDisplaySleepEnabled", w_setDisplaySleepEnabled },
  483. { "isDisplaySleepEnabled", w_isDisplaySleepEnabled },
  484. { "setTitle", w_setTitle },
  485. { "getTitle", w_getTitle },
  486. { "hasFocus", w_hasFocus },
  487. { "hasMouseFocus", w_hasMouseFocus },
  488. { "isVisible", w_isVisible },
  489. { "getDPIScale", w_getDPIScale },
  490. { "toPixels", w_toPixels },
  491. { "fromPixels", w_fromPixels },
  492. { "minimize", w_minimize },
  493. { "maximize", w_maximize },
  494. { "restore", w_restore },
  495. { "isMaximized", w_isMaximized },
  496. { "isMinimized", w_isMinimized },
  497. { "showMessageBox", w_showMessageBox },
  498. { "requestAttention", w_requestAttention },
  499. { 0, 0 }
  500. };
  501. extern "C" int luaopen_love_window(lua_State *L)
  502. {
  503. Window *instance = instance();
  504. if (instance == nullptr)
  505. luax_catchexcept(L, [&](){ instance = new love::window::sdl::Window(); });
  506. else
  507. instance->retain();
  508. WrappedModule w;
  509. w.module = instance;
  510. w.name = "window";
  511. w.type = &Module::type;
  512. w.functions = functions;
  513. w.types = 0;
  514. return luax_register_module(L, w);
  515. }
  516. } // window
  517. } // love