]> git.lizzy.rs Git - irrlicht.git/blobdiff - source/Irrlicht/CIrrDeviceSDL.cpp
Avoid undefined arithmetic on nullptr in buffer_offset function
[irrlicht.git] / source / Irrlicht / CIrrDeviceSDL.cpp
index ea309688892328e714311ddb5b483e5d9657b55b..26683a72a347337fbe6c8dc80a4c5b6f3be3ce4b 100644 (file)
@@ -2,12 +2,13 @@
 // This file is part of the "Irrlicht Engine".\r
 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
 \r
-#include "IrrCompileConfig.h"\r
 \r
 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_\r
 \r
 #include "CIrrDeviceSDL.h"\r
 #include "IEventReceiver.h"\r
+#include "IGUIElement.h"\r
+#include "IGUIEnvironment.h"\r
 #include "os.h"\r
 #include "CTimer.h"\r
 #include "irrString.h"\r
@@ -105,13 +106,105 @@ EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEv
 }\r
 #endif\r
 \r
+\r
+bool CIrrDeviceSDL::keyIsKnownSpecial(EKEY_CODE key)\r
+{\r
+       switch ( key )\r
+       {\r
+               // keys which are known to have safe special character interpretation\r
+               // could need changes over time (removals and additions!)\r
+               case KEY_RETURN:\r
+               case KEY_PAUSE:\r
+               case KEY_ESCAPE:\r
+               case KEY_PRIOR:\r
+               case KEY_NEXT:\r
+               case KEY_HOME:\r
+               case KEY_END:\r
+               case KEY_LEFT:\r
+               case KEY_UP:\r
+               case KEY_RIGHT:\r
+               case KEY_DOWN:\r
+               case KEY_TAB:\r
+               case KEY_PRINT:\r
+               case KEY_SNAPSHOT:\r
+               case KEY_INSERT:\r
+               case KEY_BACK:\r
+               case KEY_DELETE:\r
+               case KEY_HELP:\r
+               case KEY_APPS:\r
+               case KEY_SLEEP:\r
+               case KEY_F1:\r
+               case KEY_F2:\r
+               case KEY_F3:\r
+               case KEY_F4:\r
+               case KEY_F5:\r
+               case KEY_F6:\r
+               case KEY_F7:\r
+               case KEY_F8:\r
+               case KEY_F9:\r
+               case KEY_F10:\r
+               case KEY_F11:\r
+               case KEY_F12:\r
+               case KEY_F13:\r
+               case KEY_F14:\r
+               case KEY_F15:\r
+               case KEY_F16:\r
+               case KEY_F17:\r
+               case KEY_F18:\r
+               case KEY_F19:\r
+               case KEY_F20:\r
+               case KEY_F21:\r
+               case KEY_F22:\r
+               case KEY_F23:\r
+               case KEY_F24:\r
+               case KEY_NUMLOCK:\r
+               case KEY_SCROLL:\r
+               case KEY_LCONTROL:\r
+               case KEY_RCONTROL:\r
+                       return true;\r
+\r
+               default:\r
+                       return false;\r
+       }\r
+}\r
+\r
+int CIrrDeviceSDL::findCharToPassToIrrlicht(int assumedChar, EKEY_CODE key) {\r
+       // SDL in-place ORs values with no character representation with 1<<30\r
+       // https://wiki.libsdl.org/SDL2/SDLKeycodeLookup\r
+       if (assumedChar & (1<<30))\r
+               return 0;\r
+\r
+       switch (key) {\r
+               case KEY_PRIOR:\r
+               case KEY_NEXT:\r
+               case KEY_HOME:\r
+               case KEY_END:\r
+               case KEY_LEFT:\r
+               case KEY_UP:\r
+               case KEY_RIGHT:\r
+               case KEY_DOWN:\r
+               case KEY_NUMLOCK:\r
+                       return 0;\r
+               default:\r
+                       return assumedChar;\r
+       }\r
+}\r
+\r
+void CIrrDeviceSDL::resetReceiveTextInputEvents() {\r
+       gui::IGUIElement *elem = GUIEnvironment->getFocus();\r
+       if (elem && elem->acceptsIME())\r
+               SDL_StartTextInput();\r
+       else\r
+               SDL_StopTextInput();\r
+}\r
+\r
 //! constructor\r
 CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)\r
        : CIrrDeviceStub(param),\r
        Window((SDL_Window*)param.WindowId), SDL_Flags(0),\r
        MouseX(0), MouseY(0), MouseXRel(0), MouseYRel(0), MouseButtonStates(0),\r
        Width(param.WindowSize.Width), Height(param.WindowSize.Height),\r
-       Resizable(param.WindowResizable == 1 ? true : false), WindowMinimized(false)\r
+       Resizable(param.WindowResizable == 1 ? true : false)\r
 {\r
        #ifdef _DEBUG\r
        setDebugName("CIrrDeviceSDL");\r
@@ -127,7 +220,7 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
 #endif\r
                if (SDL_Init(flags) < 0)\r
                {\r
-                       os::Printer::log( "Unable to initialize SDL!", SDL_GetError());\r
+                       os::Printer::log("Unable to initialize SDL!", SDL_GetError());\r
                        Close = true;\r
                }\r
                else\r
@@ -139,10 +232,18 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
        // create keymap\r
        createKeyMap();\r
 \r
-       if ( CreationParams.Fullscreen )\r
+       if (CreationParams.Fullscreen) {\r
+#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
                SDL_Flags |= SDL_WINDOW_FULLSCREEN;\r
-       else if ( Resizable )\r
+#else\r
+               SDL_Flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;\r
+#endif\r
+       }\r
+       if (Resizable)\r
                SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
+       if (CreationParams.WindowMaximized)\r
+               SDL_Flags |= SDL_WINDOW_MAXIMIZED;\r
+\r
        if (CreationParams.DriverType == video::EDT_OPENGL)\r
        {\r
                SDL_Flags |= SDL_WINDOW_OPENGL;\r
@@ -160,6 +261,7 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
                createWindow();\r
        }\r
 \r
+\r
        SDL_VERSION(&Info.version);\r
 \r
 #ifndef _IRR_EMSCRIPTEN_PLATFORM_\r
@@ -270,7 +372,7 @@ bool CIrrDeviceSDL::createWindow()
        {\r
                SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);\r
                SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);\r
-       }       \r
+       }\r
 \r
        SDL_CreateWindowAndRenderer(0, 0, SDL_Flags, &Window, &Renderer); // 0,0 will use the canvas size\r
 \r
@@ -345,7 +447,7 @@ bool CIrrDeviceSDL::createWindow()
        }\r
        if ( !Window )\r
        {\r
-               os::Printer::log( "Could not initialize display!" );\r
+               os::Printer::log("Could not initialize display!" );\r
                return false;\r
        }\r
 \r
@@ -372,22 +474,6 @@ void CIrrDeviceSDL::createDriver()
 \r
                break;\r
 \r
-       case video::EDT_SOFTWARE:\r
-               #ifdef _IRR_COMPILE_WITH_SOFTWARE_\r
-               VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);\r
-               #else\r
-               os::Printer::log("No Software driver support compiled in.", ELL_ERROR);\r
-               #endif\r
-               break;\r
-\r
-       case video::EDT_BURNINGSVIDEO:\r
-               #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_\r
-               VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);\r
-               #else\r
-               os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);\r
-               #endif\r
-               break;\r
-\r
        case video::EDT_OPENGL:\r
                #ifdef _IRR_COMPILE_WITH_OPENGL_\r
                ContextManager = new video::CSDLManager(this);\r
@@ -463,7 +549,9 @@ bool CIrrDeviceSDL::run()
 \r
                switch ( SDL_event.type )\r
                {\r
-               case SDL_MOUSEMOTION:\r
+               case SDL_MOUSEMOTION: {\r
+                       SDL_Keymod keymod = SDL_GetModState();\r
+\r
                        irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
                        irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
                        MouseX = irrevent.MouseInput.X = SDL_event.motion.x;\r
@@ -471,20 +559,35 @@ bool CIrrDeviceSDL::run()
                        MouseXRel = SDL_event.motion.xrel;\r
                        MouseYRel = SDL_event.motion.yrel;\r
                        irrevent.MouseInput.ButtonStates = MouseButtonStates;\r
+                       irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
+                       irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
 \r
                        postEventFromUser(irrevent);\r
                        break;\r
-               case SDL_MOUSEWHEEL:\r
+               }\r
+               case SDL_MOUSEWHEEL: {\r
+                       SDL_Keymod keymod = SDL_GetModState();\r
+\r
                        irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
                        irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;\r
                        irrevent.MouseInput.Wheel = static_cast<float>(SDL_event.wheel.y);\r
+                       irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
+                       irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
+                       irrevent.MouseInput.X = MouseX;\r
+                       irrevent.MouseInput.Y = MouseY;\r
+\r
                        postEventFromUser(irrevent);\r
                        break;\r
+               }\r
                case SDL_MOUSEBUTTONDOWN:\r
-               case SDL_MOUSEBUTTONUP:\r
+               case SDL_MOUSEBUTTONUP: {\r
+                       SDL_Keymod keymod = SDL_GetModState();\r
+\r
                        irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
                        irrevent.MouseInput.X = SDL_event.button.x;\r
                        irrevent.MouseInput.Y = SDL_event.button.y;\r
+                       irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
+                       irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
 \r
                        irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
 \r
@@ -579,6 +682,7 @@ bool CIrrDeviceSDL::run()
                                }\r
                        }\r
                        break;\r
+               }\r
 \r
                case SDL_TEXTINPUT:\r
                        {\r
@@ -604,6 +708,10 @@ bool CIrrDeviceSDL::run()
                                else\r
                                        key = (EKEY_CODE)KeyMap[idx].Win32Key;\r
 \r
+                               // Make sure to only input special characters if something is in focus, as SDL_TEXTINPUT handles normal unicode already\r
+                               if (SDL_IsTextInputActive() && !keyIsKnownSpecial(key) && (SDL_event.key.keysym.mod & KMOD_CTRL) == 0)\r
+                                       break;\r
+\r
 #ifdef _IRR_WINDOWS_API_\r
                                // handle alt+f4 in Windows, because SDL seems not to\r
                                if ( (SDL_event.key.keysym.mod & KMOD_LALT) && key == KEY_F4)\r
@@ -617,12 +725,7 @@ bool CIrrDeviceSDL::run()
                                irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);\r
                                irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;\r
                                irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;\r
-                               // These keys are handled differently in CGUIEditBox.cpp (may become out of date!)\r
-                               // Control key is used in special character combinations, so keep that too\r
-                               // Pass through the keysym only then so no extra text gets input\r
-                               irrevent.KeyInput.Char = 0;\r
-                               if (mp.SDLKey == SDLK_DELETE || mp.SDLKey == SDLK_RETURN || mp.SDLKey == SDLK_BACKSPACE || irrevent.KeyInput.Control)\r
-                                       irrevent.KeyInput.Char = mp.SDLKey;\r
+                               irrevent.KeyInput.Char = findCharToPassToIrrlicht(mp.SDLKey, key);\r
                                postEventFromUser(irrevent);\r
                        }\r
                        break;\r
@@ -634,12 +737,6 @@ bool CIrrDeviceSDL::run()
                case SDL_WINDOWEVENT:\r
                        switch (SDL_event.window.event)\r
                        {\r
-                       case SDL_WINDOWEVENT_MAXIMIZED:\r
-                               WindowMinimized = true;\r
-                               break;\r
-                       case SDL_WINDOWEVENT_RESTORED:\r
-                               WindowMinimized = false;\r
-                               break;\r
                        case SDL_WINDOWEVENT_RESIZED:\r
                                if ((SDL_event.window.data1 != (int)Width) || (SDL_event.window.data2 != (int)Height))\r
                                {\r
@@ -662,7 +759,7 @@ bool CIrrDeviceSDL::run()
                default:\r
                        break;\r
                } // end switch\r
-\r
+       resetReceiveTextInputEvents();\r
        } // end while\r
 \r
 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
@@ -828,90 +925,6 @@ void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)
 }\r
 \r
 \r
-//! presents a surface in the client area\r
-bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s32>* srcClip)\r
-{\r
-#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-       return true;\r
-#else // !_IRR_EMSCRIPTEN_PLATFORM_\r
-       SDL_Surface *sdlSurface = SDL_CreateRGBSurfaceFrom(\r
-                       surface->getData(), surface->getDimension().Width, surface->getDimension().Height,\r
-                       surface->getBitsPerPixel(), surface->getPitch(),\r
-                       surface->getRedMask(), surface->getGreenMask(), surface->getBlueMask(), surface->getAlphaMask());\r
-       if (!sdlSurface)\r
-               return false;\r
-       SDL_SetSurfaceAlphaMod(sdlSurface, 0);\r
-       SDL_SetColorKey(sdlSurface, 0, 0);\r
-       sdlSurface->format->BitsPerPixel=surface->getBitsPerPixel();\r
-       sdlSurface->format->BytesPerPixel=surface->getBytesPerPixel();\r
-       if ((surface->getColorFormat()==video::ECF_R8G8B8) ||\r
-                       (surface->getColorFormat()==video::ECF_A8R8G8B8))\r
-       {\r
-               sdlSurface->format->Rloss=0;\r
-               sdlSurface->format->Gloss=0;\r
-               sdlSurface->format->Bloss=0;\r
-               sdlSurface->format->Rshift=16;\r
-               sdlSurface->format->Gshift=8;\r
-               sdlSurface->format->Bshift=0;\r
-               if (surface->getColorFormat()==video::ECF_R8G8B8)\r
-               {\r
-                       sdlSurface->format->Aloss=8;\r
-                       sdlSurface->format->Ashift=32;\r
-               }\r
-               else\r
-               {\r
-                       sdlSurface->format->Aloss=0;\r
-                       sdlSurface->format->Ashift=24;\r
-               }\r
-       }\r
-       else if (surface->getColorFormat()==video::ECF_R5G6B5)\r
-       {\r
-               sdlSurface->format->Rloss=3;\r
-               sdlSurface->format->Gloss=2;\r
-               sdlSurface->format->Bloss=3;\r
-               sdlSurface->format->Aloss=8;\r
-               sdlSurface->format->Rshift=11;\r
-               sdlSurface->format->Gshift=5;\r
-               sdlSurface->format->Bshift=0;\r
-               sdlSurface->format->Ashift=16;\r
-       }\r
-       else if (surface->getColorFormat()==video::ECF_A1R5G5B5)\r
-       {\r
-               sdlSurface->format->Rloss=3;\r
-               sdlSurface->format->Gloss=3;\r
-               sdlSurface->format->Bloss=3;\r
-               sdlSurface->format->Aloss=7;\r
-               sdlSurface->format->Rshift=10;\r
-               sdlSurface->format->Gshift=5;\r
-               sdlSurface->format->Bshift=0;\r
-               sdlSurface->format->Ashift=15;\r
-       }\r
-\r
-       SDL_Surface* scr = (SDL_Surface* )windowId;\r
-       if (!scr)\r
-               scr = SDL_GetWindowSurface(Window);\r
-       if (scr)\r
-       {\r
-               if (srcClip)\r
-               {\r
-                       SDL_Rect sdlsrcClip;\r
-                       sdlsrcClip.x = srcClip->UpperLeftCorner.X;\r
-                       sdlsrcClip.y = srcClip->UpperLeftCorner.Y;\r
-                       sdlsrcClip.w = srcClip->getWidth();\r
-                       sdlsrcClip.h = srcClip->getHeight();\r
-                       SDL_BlitSurface(sdlSurface, &sdlsrcClip, scr, NULL);\r
-               }\r
-               else\r
-                       SDL_BlitSurface(sdlSurface, NULL, scr, NULL);\r
-               SDL_RenderPresent(SDL_GetRenderer(Window));\r
-       }\r
-\r
-       SDL_FreeSurface(sdlSurface);\r
-       return (scr != 0);\r
-#endif // !_IRR_EMSCRIPTEN_PLATFORM_\r
-}\r
-\r
-\r
 //! notifies the device that it should close itself\r
 void CIrrDeviceSDL::closeDevice()\r
 {\r
@@ -926,7 +939,7 @@ void CIrrDeviceSDL::setResizable(bool resize)
        os::Printer::log("Resizable not available on the web." , ELL_WARNING);\r
        return;\r
 #else // !_IRR_EMSCRIPTEN_PLATFORM_\r
-       if (resize != Resizable) { \r
+       if (resize != Resizable) {\r
                if (resize)\r
                        SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
                else\r
@@ -944,16 +957,16 @@ void CIrrDeviceSDL::setResizable(bool resize)
 //! Minimizes window if possible\r
 void CIrrDeviceSDL::minimizeWindow()\r
 {\r
-       if (Window) {\r
+       if (Window)\r
                SDL_MinimizeWindow(Window);\r
-       }\r
 }\r
 \r
 \r
 //! Maximize window\r
 void CIrrDeviceSDL::maximizeWindow()\r
 {\r
-       // do nothing\r
+       if (Window)\r
+               SDL_MaximizeWindow(Window);\r
 }\r
 \r
 //! Get the position of this window on screen\r
@@ -966,7 +979,13 @@ core::position2di CIrrDeviceSDL::getWindowPosition()
 //! Restore original window size\r
 void CIrrDeviceSDL::restoreWindow()\r
 {\r
-       // do nothing\r
+       if (Window)\r
+               SDL_RestoreWindow(Window);\r
+}\r
+\r
+bool CIrrDeviceSDL::isWindowMaximized() const\r
+{\r
+       return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_MAXIMIZED) != 0;\r
 }\r
 \r
 bool CIrrDeviceSDL::isFullscreen() const\r
@@ -1001,14 +1020,14 @@ bool CIrrDeviceSDL::isWindowActive() const
 //! returns if window has focus.\r
 bool CIrrDeviceSDL::isWindowFocused() const\r
 {\r
-       return SDL_GetWindowFlags(Window) & SDL_WINDOW_INPUT_FOCUS;\r
+       return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_INPUT_FOCUS) != 0;\r
 }\r
 \r
 \r
 //! returns if window is minimized.\r
 bool CIrrDeviceSDL::isWindowMinimized() const\r
 {\r
-       return WindowMinimized;\r
+       return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_MINIMIZED) != 0;\r
 }\r
 \r
 \r
@@ -1179,6 +1198,25 @@ void CIrrDeviceSDL::createKeyMap()
        KeyMap.sort();\r
 }\r
 \r
+void CIrrDeviceSDL::CCursorControl::initCursors()\r
+{\r
+       Cursors.reserve(gui::ECI_COUNT);\r
+\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW));     // ECI_NORMAL\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR)); // ECI_CROSS\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND));      // ECI_HAND\r
+       Cursors.emplace_back(nullptr);                                             // ECI_HELP\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM));     // ECI_IBEAM\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO));        // ECI_NO\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT));      // ECI_WAIT\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL));   // ECI_SIZEALL\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW));  // ECI_SIZENESW\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE));  // ECI_SIZENWSE\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS));    // ECI_SIZENS\r
+       Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE));    // ECI_SIZEWE\r
+       Cursors.emplace_back(nullptr);                                             // ECI_UP\r
+}\r
+\r
 } // end namespace irr\r
 \r
 #endif // _IRR_COMPILE_WITH_SDL_DEVICE_\r