wrap_Window.cpp 15 KB

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