2
0

wrap_Window.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * Copyright (c) 2006-2015 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 desktop fullscreen mode.
  79. settings.fstype = Window::FULLSCREEN_DESKTOP;
  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. lua_getfield(L, 3, settingName(Window::SETTING_X));
  94. lua_getfield(L, 3, settingName(Window::SETTING_Y));
  95. settings.useposition = !(lua_isnoneornil(L, -2) && lua_isnoneornil(L, -1));
  96. if (settings.useposition)
  97. {
  98. settings.x = luaL_optint(L, -2, 0);
  99. settings.y = luaL_optint(L, -1, 0);
  100. }
  101. lua_pop(L, 2);
  102. // We don't explicitly set the refresh rate, it's "read-only".
  103. luax_catchexcept(L,
  104. [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
  105. );
  106. return 1;
  107. }
  108. int w_getMode(lua_State *L)
  109. {
  110. int w, h;
  111. WindowSettings settings;
  112. instance()->getWindow(w, h, settings);
  113. lua_pushnumber(L, w);
  114. lua_pushnumber(L, h);
  115. lua_newtable(L);
  116. const char *fstypestr = "desktop";
  117. Window::getConstant(settings.fstype, fstypestr);
  118. lua_pushstring(L, fstypestr);
  119. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN_TYPE));
  120. luax_pushboolean(L, settings.fullscreen);
  121. lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
  122. luax_pushboolean(L, settings.vsync);
  123. lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
  124. lua_pushinteger(L, settings.msaa);
  125. lua_setfield(L, -2, settingName(Window::SETTING_MSAA));
  126. luax_pushboolean(L, settings.resizable);
  127. lua_setfield(L, -2, settingName(Window::SETTING_RESIZABLE));
  128. lua_pushinteger(L, settings.minwidth);
  129. lua_setfield(L, -2, settingName(Window::SETTING_MIN_WIDTH));
  130. lua_pushinteger(L, settings.minheight);
  131. lua_setfield(L, -2, settingName(Window::SETTING_MIN_HEIGHT));
  132. luax_pushboolean(L, settings.borderless);
  133. lua_setfield(L, -2, settingName(Window::SETTING_BORDERLESS));
  134. luax_pushboolean(L, settings.centered);
  135. lua_setfield(L, -2, settingName(Window::SETTING_CENTERED));
  136. // Display index is 0-based internally and 1-based in Lua.
  137. lua_pushinteger(L, settings.display + 1);
  138. lua_setfield(L, -2, settingName(Window::SETTING_DISPLAY));
  139. luax_pushboolean(L, settings.highdpi);
  140. lua_setfield(L, -2, settingName(Window::SETTING_HIGHDPI));
  141. luax_pushboolean(L, settings.sRGB);
  142. lua_setfield(L, -2, settingName(Window::SETTING_SRGB));
  143. lua_pushnumber(L, settings.refreshrate);
  144. lua_setfield(L, -2, settingName(Window::SETTING_REFRESHRATE));
  145. lua_pushinteger(L, settings.x);
  146. lua_setfield(L, -2, settingName(Window::SETTING_X));
  147. lua_pushinteger(L, settings.y);
  148. lua_setfield(L, -2, settingName(Window::SETTING_Y));
  149. return 3;
  150. }
  151. int w_getFullscreenModes(lua_State *L)
  152. {
  153. int displayindex = luaL_optint(L, 1, 1) - 1;
  154. std::vector<Window::WindowSize> modes = instance()->getFullscreenSizes(displayindex);
  155. lua_createtable(L, (int) modes.size(), 0);
  156. for (size_t i = 0; i < modes.size(); i++)
  157. {
  158. lua_pushinteger(L, i+1);
  159. lua_createtable(L, 0, 2);
  160. // Inner table attribs.
  161. lua_pushinteger(L, modes[i].width);
  162. lua_setfield(L, -2, "width");
  163. lua_pushinteger(L, modes[i].height);
  164. lua_setfield(L, -2, "height");
  165. // Inner table attribs end.
  166. lua_settable(L, -3);
  167. }
  168. return 1;
  169. }
  170. int w_setFullscreen(lua_State *L)
  171. {
  172. bool fullscreen = luax_toboolean(L, 1);
  173. Window::FullscreenType fstype = Window::FULLSCREEN_MAX_ENUM;
  174. const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
  175. if (typestr && !Window::getConstant(typestr, fstype))
  176. return luaL_error(L, "Invalid fullscreen type: %s", typestr);
  177. bool success = false;
  178. if (fstype == Window::FULLSCREEN_MAX_ENUM)
  179. success = instance()->setFullscreen(fullscreen);
  180. else
  181. success = instance()->setFullscreen(fullscreen, fstype);
  182. luax_pushboolean(L, success);
  183. return 1;
  184. }
  185. int w_getFullscreen(lua_State *L)
  186. {
  187. int w, h;
  188. WindowSettings settings;
  189. instance()->getWindow(w, h, settings);
  190. const char *typestr;
  191. if (!Window::getConstant(settings.fstype, typestr))
  192. luaL_error(L, "Unknown fullscreen type.");
  193. luax_pushboolean(L, settings.fullscreen);
  194. lua_pushstring(L, typestr);
  195. return 2;
  196. }
  197. int w_isCreated(lua_State *L)
  198. {
  199. luax_pushboolean(L, instance()->isCreated());
  200. return 1;
  201. }
  202. int w_getDesktopDimensions(lua_State *L)
  203. {
  204. int width = 0, height = 0;
  205. int displayindex = luaL_optint(L, 1, 1) - 1;
  206. instance()->getDesktopDimensions(displayindex, width, height);
  207. lua_pushinteger(L, width);
  208. lua_pushinteger(L, height);
  209. return 2;
  210. }
  211. int w_setPosition(lua_State *L)
  212. {
  213. int x = luaL_checkint(L, 1);
  214. int y = luaL_checkint(L, 2);
  215. int displayindex = luaL_optint(L, 3, 1) - 1;
  216. instance()->setPosition(x, y, displayindex);
  217. return 0;
  218. }
  219. int w_getPosition(lua_State *L)
  220. {
  221. int x = 0;
  222. int y = 0;
  223. int displayindex = 0;
  224. instance()->getPosition(x, y, displayindex);
  225. lua_pushinteger(L, x);
  226. lua_pushinteger(L, y);
  227. lua_pushinteger(L, displayindex + 1);
  228. return 3;
  229. }
  230. int w_setIcon(lua_State *L)
  231. {
  232. image::ImageData *i = luax_checktype<image::ImageData>(L, 1, IMAGE_IMAGE_DATA_ID);
  233. luax_pushboolean(L, instance()->setIcon(i));
  234. return 1;
  235. }
  236. int w_getIcon(lua_State *L)
  237. {
  238. image::ImageData *i = instance()->getIcon();
  239. luax_pushtype(L, IMAGE_IMAGE_DATA_ID, i);
  240. return 1;
  241. }
  242. int w_setTitle(lua_State *L)
  243. {
  244. std::string title = luax_checkstring(L, 1);
  245. instance()->setWindowTitle(title);
  246. return 0;
  247. }
  248. int w_getTitle(lua_State *L)
  249. {
  250. luax_pushstring(L, instance()->getWindowTitle());
  251. return 1;
  252. }
  253. int w_hasFocus(lua_State *L)
  254. {
  255. luax_pushboolean(L, instance()->hasFocus());
  256. return 1;
  257. }
  258. int w_hasMouseFocus(lua_State *L)
  259. {
  260. luax_pushboolean(L, instance()->hasMouseFocus());
  261. return 1;
  262. }
  263. int w_isVisible(lua_State *L)
  264. {
  265. luax_pushboolean(L, instance()->isVisible());
  266. return 1;
  267. }
  268. int w_getPixelScale(lua_State *L)
  269. {
  270. lua_pushnumber(L, instance()->getPixelScale());
  271. return 1;
  272. }
  273. int w_toPixels(lua_State *L)
  274. {
  275. double wx = luaL_checknumber(L, 1);
  276. if (lua_isnoneornil(L, 2))
  277. {
  278. lua_pushnumber(L, instance()->toPixels(wx));
  279. return 1;
  280. }
  281. double wy = luaL_checknumber(L, 2);
  282. double px = 0.0, py = 0.0;
  283. instance()->toPixels(wx, wy, px, py);
  284. lua_pushnumber(L, px);
  285. lua_pushnumber(L, py);
  286. return 2;
  287. }
  288. int w_fromPixels(lua_State *L)
  289. {
  290. double px = luaL_checknumber(L, 1);
  291. if (lua_isnoneornil(L, 2))
  292. {
  293. lua_pushnumber(L, instance()->fromPixels(px));
  294. return 1;
  295. }
  296. double py = luaL_checknumber(L, 2);
  297. double wx = 0.0, wy = 0.0;
  298. instance()->fromPixels(px, py, wx, wy);
  299. lua_pushnumber(L, wx);
  300. lua_pushnumber(L, wy);
  301. return 2;
  302. }
  303. int w_minimize(lua_State* /*L*/)
  304. {
  305. instance()->minimize();
  306. return 0;
  307. }
  308. int w_maximize(lua_State *)
  309. {
  310. instance()->maximize();
  311. return 0;
  312. }
  313. int w_showMessageBox(lua_State *L)
  314. {
  315. Window::MessageBoxData data = {};
  316. data.type = Window::MESSAGEBOX_INFO;
  317. data.title = luaL_checkstring(L, 1);
  318. data.message = luaL_checkstring(L, 2);
  319. // If we have a table argument, we assume a list of button names, which
  320. // means we should use the more complex message box API.
  321. if (lua_istable(L, 3))
  322. {
  323. size_t numbuttons = lua_objlen(L, 3);
  324. if (numbuttons == 0)
  325. return luaL_error(L, "Must have at least one messagebox button.");
  326. // Array of button names.
  327. for (size_t i = 0; i < numbuttons; i++)
  328. {
  329. lua_rawgeti(L, 3, (int) i + 1);
  330. data.buttons.push_back(luax_checkstring(L, -1));
  331. lua_pop(L, 1);
  332. }
  333. // Optional table entry specifying the button to use when enter is pressed.
  334. lua_getfield(L, 3, "enterbutton");
  335. if (!lua_isnoneornil(L, -1))
  336. data.enterButtonIndex = luaL_checkint(L, -1) - 1;
  337. else
  338. data.enterButtonIndex = 0;
  339. lua_pop(L, 1);
  340. // Optional table entry specifying the button to use when esc is pressed.
  341. lua_getfield(L, 3, "escapebutton");
  342. if (!lua_isnoneornil(L, -1))
  343. data.escapeButtonIndex = luaL_checkint(L, -1) - 1;
  344. else
  345. data.escapeButtonIndex = (int) data.buttons.size() - 1;
  346. lua_pop(L, 1);
  347. const char *typestr = lua_isnoneornil(L, 4) ? nullptr : luaL_checkstring(L, 4);
  348. if (typestr && !Window::getConstant(typestr, data.type))
  349. return luaL_error(L, "Invalid messagebox type: %s", typestr);
  350. data.attachToWindow = luax_optboolean(L, 5, true);
  351. int pressedbutton = instance()->showMessageBox(data);
  352. lua_pushinteger(L, pressedbutton + 1);
  353. }
  354. else
  355. {
  356. const char *typestr = lua_isnoneornil(L, 3) ? nullptr : luaL_checkstring(L, 3);
  357. if (typestr && !Window::getConstant(typestr, data.type))
  358. return luaL_error(L, "Invalid messagebox type: %s", typestr);
  359. data.attachToWindow = luax_optboolean(L, 4, true);
  360. // Display a simple message box.
  361. bool success = instance()->showMessageBox(data.title, data.message, data.type, data.attachToWindow);
  362. luax_pushboolean(L, success);
  363. }
  364. return 1;
  365. }
  366. static const luaL_Reg functions[] =
  367. {
  368. { "getDisplayCount", w_getDisplayCount },
  369. { "getDisplayName", w_getDisplayName },
  370. { "setMode", w_setMode },
  371. { "getMode", w_getMode },
  372. { "getFullscreenModes", w_getFullscreenModes },
  373. { "setFullscreen", w_setFullscreen },
  374. { "getFullscreen", w_getFullscreen },
  375. { "isCreated", w_isCreated },
  376. { "getDesktopDimensions", w_getDesktopDimensions },
  377. { "setPosition", w_setPosition },
  378. { "getPosition", w_getPosition },
  379. { "setIcon", w_setIcon },
  380. { "getIcon", w_getIcon },
  381. { "setTitle", w_setTitle },
  382. { "getTitle", w_getTitle },
  383. { "hasFocus", w_hasFocus },
  384. { "hasMouseFocus", w_hasMouseFocus },
  385. { "isVisible", w_isVisible },
  386. { "getPixelScale", w_getPixelScale },
  387. { "toPixels", w_toPixels },
  388. { "fromPixels", w_fromPixels },
  389. { "minimize", w_minimize },
  390. { "maximize", w_maximize },
  391. { "showMessageBox", w_showMessageBox },
  392. { 0, 0 }
  393. };
  394. extern "C" int luaopen_love_window(lua_State *L)
  395. {
  396. Window *instance = nullptr;
  397. luax_catchexcept(L, [&](){ instance = sdl::Window::createSingleton(); });
  398. WrappedModule w;
  399. w.module = instance;
  400. w.name = "window";
  401. w.type = MODULE_ID;
  402. w.functions = functions;
  403. w.types = 0;
  404. return luax_register_module(L, w);
  405. }
  406. } // window
  407. } // love