123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- /**
- * Copyright (c) 2006-2017 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- #include "wrap_Window.h"
- #include "sdl/Window.h"
- namespace love
- {
- namespace window
- {
- #define instance() (Module::getInstance<Window>(Module::M_WINDOW))
- int w_getDisplayCount(lua_State *L)
- {
- lua_pushinteger(L, instance()->getDisplayCount());
- return 1;
- }
- int w_getDisplayName(lua_State *L)
- {
- int index = (int) luaL_checkinteger(L, 1) - 1;
- const char *name = nullptr;
- luax_catchexcept(L, [&](){ name = instance()->getDisplayName(index); });
- lua_pushstring(L, name);
- return 1;
- }
- static const char *settingName(Window::Setting setting)
- {
- const char *name = nullptr;
- Window::getConstant(setting, name);
- return name;
- }
- static int readWindowSettings(lua_State *L, int idx, WindowSettings &settings)
- {
- luaL_checktype(L, idx, LUA_TTABLE);
- // We want to error for invalid / misspelled window attributes.
- lua_pushnil(L);
- while (lua_next(L, idx))
- {
- if (lua_type(L, -2) != LUA_TSTRING)
- return luax_typerror(L, -2, "string");
- const char *key = luaL_checkstring(L, -2);
- Window::Setting setting;
- if (!Window::getConstant(key, setting))
- return luaL_error(L, "Invalid window setting: %s", key);
- lua_pop(L, 1);
- }
- lua_getfield(L, idx, settingName(Window::SETTING_FULLSCREEN_TYPE));
- if (!lua_isnoneornil(L, -1))
- {
- const char *typestr = luaL_checkstring(L, -1);
- if (!Window::getConstant(typestr, settings.fstype))
- return luaL_error(L, "Invalid fullscreen type: %s", typestr);
- }
- lua_pop(L, 1);
- settings.fullscreen = luax_boolflag(L, idx, settingName(Window::SETTING_FULLSCREEN), settings.fullscreen);
- settings.msaa = luax_intflag(L, idx, settingName(Window::SETTING_MSAA), settings.msaa);
- settings.stencil = luax_boolflag(L, idx, settingName(Window::SETTING_STENCIL), settings.stencil);
- settings.depth = luax_intflag(L, idx, settingName(Window::SETTING_DEPTH), settings.depth);
- settings.resizable = luax_boolflag(L, idx, settingName(Window::SETTING_RESIZABLE), settings.resizable);
- settings.minwidth = luax_intflag(L, idx, settingName(Window::SETTING_MIN_WIDTH), settings.minwidth);
- settings.minheight = luax_intflag(L, idx, settingName(Window::SETTING_MIN_HEIGHT), settings.minheight);
- settings.borderless = luax_boolflag(L, idx, settingName(Window::SETTING_BORDERLESS), settings.borderless);
- settings.centered = luax_boolflag(L, idx, settingName(Window::SETTING_CENTERED), settings.centered);
- settings.display = luax_intflag(L, idx, settingName(Window::SETTING_DISPLAY), settings.display+1) - 1;
- settings.highdpi = luax_boolflag(L, idx, settingName(Window::SETTING_HIGHDPI), settings.highdpi);
- lua_getfield(L, idx, settingName(Window::SETTING_VSYNC));
- if (lua_isnumber(L, -1))
- settings.vsync = (int) lua_tointeger(L, -1);
- else if (lua_isboolean(L, -1))
- settings.vsync = lua_toboolean(L, -1);
- lua_pop(L, 1);
- lua_getfield(L, idx, settingName(Window::SETTING_X));
- lua_getfield(L, idx, settingName(Window::SETTING_Y));
- settings.useposition = !(lua_isnoneornil(L, -2) && lua_isnoneornil(L, -1));
- if (settings.useposition)
- {
- settings.x = (int) luaL_optinteger(L, -2, 0);
- settings.y = (int) luaL_optinteger(L, -1, 0);
- }
- lua_pop(L, 2);
- // We don't explicitly set the refresh rate, it's "read-only".
- return 0;
- }
- int w_setMode(lua_State *L)
- {
- int w = (int) luaL_checkinteger(L, 1);
- int h = (int) luaL_checkinteger(L, 2);
- if (lua_isnoneornil(L, 3))
- {
- luax_catchexcept(L, [&](){ luax_pushboolean(L, instance()->setWindow(w, h, nullptr)); });
- return 1;
- }
- // Defaults come from WindowSettings itself.
- WindowSettings settings;
- readWindowSettings(L, 3, settings);
- luax_catchexcept(L,
- [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
- );
- return 1;
- }
- int w_updateMode(lua_State *L)
- {
- int w, h;
- WindowSettings settings;
- instance()->getWindow(w, h, settings);
- if (lua_gettop(L) == 0)
- return luaL_error(L, "Expected at least one argument");
- int idx = 1;
- if (lua_isnumber(L, 1))
- {
- idx = 3;
- w = (int) luaL_checkinteger(L, 1);
- h = (int) luaL_checkinteger(L, 2);
- }
- if (!lua_isnoneornil(L, idx))
- readWindowSettings(L, idx, settings);
- luax_catchexcept(L,
- [&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
- );
- return 1;
- }
- int w_getMode(lua_State *L)
- {
- int w, h;
- WindowSettings settings;
- instance()->getWindow(w, h, settings);
- lua_pushnumber(L, w);
- lua_pushnumber(L, h);
- if (lua_istable(L, 1))
- lua_pushvalue(L, 1);
- else
- lua_newtable(L);
- const char *fstypestr = "desktop";
- Window::getConstant(settings.fstype, fstypestr);
- lua_pushstring(L, fstypestr);
- lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN_TYPE));
- luax_pushboolean(L, settings.fullscreen);
- lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
- lua_pushinteger(L, settings.vsync);
- lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
- lua_pushinteger(L, settings.msaa);
- lua_setfield(L, -2, settingName(Window::SETTING_MSAA));
- luax_pushboolean(L, settings.stencil);
- lua_setfield(L, -2, settingName(Window::SETTING_STENCIL));
- lua_pushinteger(L, settings.depth);
- lua_setfield(L, -2, settingName(Window::SETTING_DEPTH));
- luax_pushboolean(L, settings.resizable);
- lua_setfield(L, -2, settingName(Window::SETTING_RESIZABLE));
- lua_pushinteger(L, settings.minwidth);
- lua_setfield(L, -2, settingName(Window::SETTING_MIN_WIDTH));
- lua_pushinteger(L, settings.minheight);
- lua_setfield(L, -2, settingName(Window::SETTING_MIN_HEIGHT));
- luax_pushboolean(L, settings.borderless);
- lua_setfield(L, -2, settingName(Window::SETTING_BORDERLESS));
- luax_pushboolean(L, settings.centered);
- lua_setfield(L, -2, settingName(Window::SETTING_CENTERED));
- // Display index is 0-based internally and 1-based in Lua.
- lua_pushinteger(L, settings.display + 1);
- lua_setfield(L, -2, settingName(Window::SETTING_DISPLAY));
- luax_pushboolean(L, settings.highdpi);
- lua_setfield(L, -2, settingName(Window::SETTING_HIGHDPI));
- lua_pushnumber(L, settings.refreshrate);
- lua_setfield(L, -2, settingName(Window::SETTING_REFRESHRATE));
- lua_pushinteger(L, settings.x);
- lua_setfield(L, -2, settingName(Window::SETTING_X));
- lua_pushinteger(L, settings.y);
- lua_setfield(L, -2, settingName(Window::SETTING_Y));
- return 3;
- }
- int w_getFullscreenModes(lua_State *L)
- {
- int displayindex = 0;
- if (!lua_isnoneornil(L, 1))
- displayindex = (int) luaL_checkinteger(L, 1) - 1;
- else
- {
- int x, y;
- instance()->getPosition(x, y, displayindex);
- }
- std::vector<Window::WindowSize> modes = instance()->getFullscreenSizes(displayindex);
- lua_createtable(L, (int) modes.size(), 0);
- for (size_t i = 0; i < modes.size(); i++)
- {
- lua_pushinteger(L, i + 1);
- lua_createtable(L, 0, 2);
- // Inner table attribs.
- lua_pushinteger(L, modes[i].width);
- lua_setfield(L, -2, "width");
- lua_pushinteger(L, modes[i].height);
- lua_setfield(L, -2, "height");
- // Inner table attribs end.
- lua_settable(L, -3);
- }
- return 1;
- }
- int w_setFullscreen(lua_State *L)
- {
- bool fullscreen = luax_checkboolean(L, 1);
- Window::FullscreenType fstype = Window::FULLSCREEN_MAX_ENUM;
- const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
- if (typestr && !Window::getConstant(typestr, fstype))
- return luaL_error(L, "Invalid fullscreen type: %s", typestr);
- bool success = false;
- luax_catchexcept(L, [&]() {
- if (fstype == Window::FULLSCREEN_MAX_ENUM)
- success = instance()->setFullscreen(fullscreen);
- else
- success = instance()->setFullscreen(fullscreen, fstype);
- });
- luax_pushboolean(L, success);
- return 1;
- }
- int w_getFullscreen(lua_State *L)
- {
- int w, h;
- WindowSettings settings;
- instance()->getWindow(w, h, settings);
- const char *typestr;
- if (!Window::getConstant(settings.fstype, typestr))
- luaL_error(L, "Unknown fullscreen type.");
- luax_pushboolean(L, settings.fullscreen);
- lua_pushstring(L, typestr);
- return 2;
- }
- int w_isOpen(lua_State *L)
- {
- luax_pushboolean(L, instance()->isOpen());
- return 1;
- }
- int w_close(lua_State *L)
- {
- luax_catchexcept(L, [&]() { instance()->close(); });
- return 0;
- }
- int w_getDesktopDimensions(lua_State *L)
- {
- int width = 0, height = 0;
- int displayindex = 0;
- if (!lua_isnoneornil(L, 1))
- displayindex = (int) luaL_checkinteger(L, 1) - 1;
- else
- {
- int x, y;
- instance()->getPosition(x, y, displayindex);
- }
- instance()->getDesktopDimensions(displayindex, width, height);
- lua_pushinteger(L, width);
- lua_pushinteger(L, height);
- return 2;
- }
- int w_setPosition(lua_State *L)
- {
- int x = (int) luaL_checkinteger(L, 1);
- int y = (int) luaL_checkinteger(L, 2);
- int displayindex = 0;
- if (!lua_isnoneornil(L, 3))
- displayindex = (int) luaL_checkinteger(L, 3) - 1;
- else
- {
- int x_unused, y_unused;
- instance()->getPosition(x_unused, y_unused, displayindex);
- }
- instance()->setPosition(x, y, displayindex);
- return 0;
- }
- int w_getPosition(lua_State *L)
- {
- int x = 0;
- int y = 0;
- int displayindex = 0;
- instance()->getPosition(x, y, displayindex);
- lua_pushinteger(L, x);
- lua_pushinteger(L, y);
- lua_pushinteger(L, displayindex + 1);
- return 3;
- }
- int w_setIcon(lua_State *L)
- {
- image::ImageData *i = luax_checktype<image::ImageData>(L, 1);
- luax_pushboolean(L, instance()->setIcon(i));
- return 1;
- }
- int w_getIcon(lua_State *L)
- {
- image::ImageData *i = instance()->getIcon();
- luax_pushtype(L, i);
- return 1;
- }
- int w_setDisplaySleepEnabled(lua_State *L)
- {
- instance()->setDisplaySleepEnabled(luax_checkboolean(L, 1));
- return 0;
- }
- int w_isDisplaySleepEnabled(lua_State *L)
- {
- luax_pushboolean(L, instance()->isDisplaySleepEnabled());
- return 1;
- }
- int w_setTitle(lua_State *L)
- {
- std::string title = luax_checkstring(L, 1);
- instance()->setWindowTitle(title);
- return 0;
- }
- int w_getTitle(lua_State *L)
- {
- luax_pushstring(L, instance()->getWindowTitle());
- return 1;
- }
- int w_hasFocus(lua_State *L)
- {
- luax_pushboolean(L, instance()->hasFocus());
- return 1;
- }
- int w_hasMouseFocus(lua_State *L)
- {
- luax_pushboolean(L, instance()->hasMouseFocus());
- return 1;
- }
- int w_isVisible(lua_State *L)
- {
- luax_pushboolean(L, instance()->isVisible());
- return 1;
- }
- int w_getDPIScale(lua_State *L)
- {
- lua_pushnumber(L, instance()->getDPIScale());
- return 1;
- }
- int w_toPixels(lua_State *L)
- {
- double wx = luaL_checknumber(L, 1);
- if (lua_isnoneornil(L, 2))
- {
- lua_pushnumber(L, instance()->toPixels(wx));
- return 1;
- }
- double wy = luaL_checknumber(L, 2);
- double px = 0.0, py = 0.0;
- instance()->toPixels(wx, wy, px, py);
- lua_pushnumber(L, px);
- lua_pushnumber(L, py);
- return 2;
- }
- int w_fromPixels(lua_State *L)
- {
- double px = luaL_checknumber(L, 1);
- if (lua_isnoneornil(L, 2))
- {
- lua_pushnumber(L, instance()->fromPixels(px));
- return 1;
- }
- double py = luaL_checknumber(L, 2);
- double wx = 0.0, wy = 0.0;
- instance()->fromPixels(px, py, wx, wy);
- lua_pushnumber(L, wx);
- lua_pushnumber(L, wy);
- return 2;
- }
- int w_minimize(lua_State* /*L*/)
- {
- instance()->minimize();
- return 0;
- }
- int w_maximize(lua_State *)
- {
- instance()->maximize();
- return 0;
- }
- int w_isMaximized(lua_State *L)
- {
- luax_pushboolean(L, instance()->isMaximized());
- return 0;
- }
- int w_showMessageBox(lua_State *L)
- {
- Window::MessageBoxData data = {};
- data.type = Window::MESSAGEBOX_INFO;
- data.title = luaL_checkstring(L, 1);
- data.message = luaL_checkstring(L, 2);
- // If we have a table argument, we assume a list of button names, which
- // means we should use the more complex message box API.
- if (lua_istable(L, 3))
- {
- size_t numbuttons = luax_objlen(L, 3);
- if (numbuttons == 0)
- return luaL_error(L, "Must have at least one messagebox button.");
- // Array of button names.
- for (size_t i = 0; i < numbuttons; i++)
- {
- lua_rawgeti(L, 3, (int) i + 1);
- data.buttons.push_back(luax_checkstring(L, -1));
- lua_pop(L, 1);
- }
- // Optional table entry specifying the button to use when enter is pressed.
- lua_getfield(L, 3, "enterbutton");
- if (!lua_isnoneornil(L, -1))
- data.enterButtonIndex = (int) luaL_checkinteger(L, -1) - 1;
- else
- data.enterButtonIndex = 0;
- lua_pop(L, 1);
- // Optional table entry specifying the button to use when esc is pressed.
- lua_getfield(L, 3, "escapebutton");
- if (!lua_isnoneornil(L, -1))
- data.escapeButtonIndex = (int) luaL_checkinteger(L, -1) - 1;
- else
- data.escapeButtonIndex = (int) data.buttons.size() - 1;
- lua_pop(L, 1);
- const char *typestr = lua_isnoneornil(L, 4) ? nullptr : luaL_checkstring(L, 4);
- if (typestr && !Window::getConstant(typestr, data.type))
- return luaL_error(L, "Invalid messagebox type: %s", typestr);
- data.attachToWindow = luax_optboolean(L, 5, true);
- int pressedbutton = instance()->showMessageBox(data);
- lua_pushinteger(L, pressedbutton + 1);
- }
- else
- {
- const char *typestr = lua_isnoneornil(L, 3) ? nullptr : luaL_checkstring(L, 3);
- if (typestr && !Window::getConstant(typestr, data.type))
- return luaL_error(L, "Invalid messagebox type: %s", typestr);
- data.attachToWindow = luax_optboolean(L, 4, true);
- // Display a simple message box.
- bool success = instance()->showMessageBox(data.title, data.message, data.type, data.attachToWindow);
- luax_pushboolean(L, success);
- }
- return 1;
- }
- int w_requestAttention(lua_State *L)
- {
- bool continuous = luax_optboolean(L, 1, false);
- instance()->requestAttention(continuous);
- return 0;
- }
- static const luaL_Reg functions[] =
- {
- { "getDisplayCount", w_getDisplayCount },
- { "getDisplayName", w_getDisplayName },
- { "setMode", w_setMode },
- { "updateMode", w_updateMode },
- { "getMode", w_getMode },
- { "getFullscreenModes", w_getFullscreenModes },
- { "setFullscreen", w_setFullscreen },
- { "getFullscreen", w_getFullscreen },
- { "isOpen", w_isOpen },
- { "close", w_close },
- { "getDesktopDimensions", w_getDesktopDimensions },
- { "setPosition", w_setPosition },
- { "getPosition", w_getPosition },
- { "setIcon", w_setIcon },
- { "getIcon", w_getIcon },
- { "setDisplaySleepEnabled", w_setDisplaySleepEnabled },
- { "isDisplaySleepEnabled", w_isDisplaySleepEnabled },
- { "setTitle", w_setTitle },
- { "getTitle", w_getTitle },
- { "hasFocus", w_hasFocus },
- { "hasMouseFocus", w_hasMouseFocus },
- { "isVisible", w_isVisible },
- { "getDPIScale", w_getDPIScale },
- { "toPixels", w_toPixels },
- { "fromPixels", w_fromPixels },
- { "minimize", w_minimize },
- { "maximize", w_maximize },
- { "isMaximized", w_isMaximized },
- { "showMessageBox", w_showMessageBox },
- { "requestAttention", w_requestAttention },
- { 0, 0 }
- };
- extern "C" int luaopen_love_window(lua_State *L)
- {
- Window *instance = instance();
- if (instance == nullptr)
- luax_catchexcept(L, [&](){ instance = new love::window::sdl::Window(); });
- else
- instance->retain();
- WrappedModule w;
- w.module = instance;
- w.name = "window";
- w.type = &Module::type;
- w.functions = functions;
- w.types = 0;
- return luax_register_module(L, w);
- }
- } // window
- } // love
|