]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_sdl.c
Fix several warnings with gcc-5 and an error with mingw (where
[xonotic/darkplaces.git] / vid_sdl.c
index 2016762df59d514c6cac69b51f295badc5b603c2..a1bef3b39c1149f613fad8b8b77f372454cc38ec 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "image.h"
 #include "dpsoftrast.h"
+#include "utf8lib.h"
 
 #ifndef __IPHONEOS__
 #ifdef MACOSX
@@ -86,7 +87,7 @@ static int video_bpp;
 static SDL_Surface *screen;
 static int video_flags;
 #else
-static SDL_GLContext *context;
+static SDL_GLContext context;
 static SDL_Window *window;
 static int window_flags;
 #endif
@@ -100,6 +101,7 @@ static vid_mode_t desktop_mode;
 
 #ifndef SDLK_PERCENT
 #define SDLK_PERCENT '%'
+#if SDL_MAJOR_VERSION == 1
 #define SDLK_PRINTSCREEN SDLK_PRINT
 #define SDLK_SCROLLLOCK SDLK_SCROLLOCK
 #define SDLK_NUMLOCKCLEAR SDLK_NUMLOCK
@@ -114,6 +116,7 @@ static vid_mode_t desktop_mode;
 #define SDLK_KP_9 SDLK_KP9
 #define SDLK_KP_0 SDLK_KP0
 #endif
+#endif
 
 static int MapKey( unsigned int sdlkey )
 {
@@ -203,10 +206,8 @@ static int MapKey( unsigned int sdlkey )
        case SDLK_F10:                return K_F10;
        case SDLK_F11:                return K_F11;
        case SDLK_F12:                return K_F12;
-#if SDL_MAJOR_VERSION == 1
        case SDLK_PRINTSCREEN:        return K_PRINTSCREEN;
        case SDLK_SCROLLLOCK:         return K_SCROLLOCK;
-#endif
        case SDLK_PAUSE:              return K_PAUSE;
        case SDLK_INSERT:             return K_INS;
        case SDLK_HOME:               return K_HOME;
@@ -222,15 +223,12 @@ static int MapKey( unsigned int sdlkey )
        case SDLK_LEFT:               return K_LEFTARROW;
        case SDLK_DOWN:               return K_DOWNARROW;
        case SDLK_UP:                 return K_UPARROW;
-#if SDL_MAJOR_VERSION == 1
        case SDLK_NUMLOCKCLEAR:       return K_NUMLOCK;
-#endif
        case SDLK_KP_DIVIDE:          return K_KP_DIVIDE;
        case SDLK_KP_MULTIPLY:        return K_KP_MULTIPLY;
        case SDLK_KP_MINUS:           return K_KP_MINUS;
        case SDLK_KP_PLUS:            return K_KP_PLUS;
        case SDLK_KP_ENTER:           return K_KP_ENTER;
-#if SDL_MAJOR_VERSION == 1
        case SDLK_KP_1:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_1 : K_END);
        case SDLK_KP_2:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_2 : K_DOWNARROW);
        case SDLK_KP_3:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_3 : K_PGDN);
@@ -241,7 +239,6 @@ static int MapKey( unsigned int sdlkey )
        case SDLK_KP_8:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_8 : K_UPARROW);
        case SDLK_KP_9:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_9 : K_PGUP);
        case SDLK_KP_0:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_0 : K_INS);
-#endif
        case SDLK_KP_PERIOD:          return ((SDL_GetModState() & KMOD_NUM) ? K_KP_PERIOD : K_DEL);
 //     case SDLK_APPLICATION:        return K_APPLICATION;
 //     case SDLK_POWER:              return K_POWER;
@@ -1217,8 +1214,7 @@ void Sys_SendKeyEvents( void )
        static qboolean sound_active = true;
        int keycode;
        int i;
-       int j;
-       int unicode;
+       Uchar unicode;
        SDL_Event event;
 
        VID_EnableJoystick(true);
@@ -1235,9 +1231,9 @@ void Sys_SendKeyEvents( void )
                        case SDL_KEYUP:
 #ifdef DEBUGSDLEVENTS
                                if (event.type == SDL_KEYDOWN)
-                                       Con_DPrintf("SDL_Event: SDL_KEYDOWN %i unicode %i\n", event.key.keysym.sym, event.key.keysym.unicode);
+                                       Con_DPrintf("SDL_Event: SDL_KEYDOWN %i\n", event.key.keysym.sym);
                                else
-                                       Con_DPrintf("SDL_Event: SDL_KEYUP %i unicode %i\n", event.key.keysym.sym, event.key.keysym.unicode);
+                                       Con_DPrintf("SDL_Event: SDL_KEYUP %i\n", event.key.keysym.sym);
 #endif
                                keycode = MapKey(event.key.keysym.sym);
                                if (!VID_JoyBlockEmulatedKeys(keycode))
@@ -1357,30 +1353,11 @@ void Sys_SendKeyEvents( void )
 #ifdef DEBUGSDLEVENTS
                                Con_DPrintf("SDL_Event: SDL_TEXTINPUT - text: %s\n", event.text.text);
 #endif
-                               // we have some characters to parse
-                               {
-                                       unicode = 0;
-                                       for (i = 0;event.text.text[i];)
-                                       {
-                                               unicode = event.text.text[i++];
-                                               if (unicode & 0x80)
-                                               {
-                                                       // UTF-8 character
-                                                       // strip high bits (we could count these to validate character length but we don't)
-                                                       for (j = 0x80;unicode & j;j >>= 1)
-                                                               unicode ^= j;
-                                                       for (;(event.text.text[i] & 0xC0) == 0x80;i++)
-                                                               unicode = (unicode << 6) | (event.text.text[i] & 0x3F);
-                                                       // low characters are invalid and could be bad, so replace them
-                                                       if (unicode < 0x80)
-                                                               unicode = '?'; // we could use 0xFFFD instead, the unicode substitute character
-                                               }
-                                               //Con_DPrintf("SDL_TEXTINPUT: K_TEXT %i \n", unicode);
-
-                                               Key_Event(K_TEXT, unicode, true);
-                                               Key_Event(K_TEXT, unicode, false);
-                                       }
-                               }
+                               // convert utf8 string to char
+                               // NOTE: this code is supposed to run even if utf8enable is 0
+                               unicode = u8_getchar_utf8_enabled(event.text.text + (int)u8_bytelen(event.text.text, 0), NULL);
+                               Key_Event(K_TEXT, unicode, true);
+                               Key_Event(K_TEXT, unicode, false);
                                break;
                        case SDL_MOUSEMOTION:
                                break;
@@ -2437,6 +2414,50 @@ static void VID_OutputVersion(void)
                                        version.major, version.minor, version.patch );
 }
 
+#ifdef WIN32
+static void AdjustWindowBounds(viddef_mode_t *mode, RECT *rect)
+{
+       LONG width = mode->width; // vid_width
+       LONG height = mode->height; // vid_height
+
+       // adjust width and height for the space occupied by window decorators (title bar, borders)
+       rect->top = 0;
+       rect->left = 0;
+       rect->right = width;
+       rect->bottom = height;
+       AdjustWindowRectEx(rect, WS_CAPTION|WS_THICKFRAME, false, 0);
+
+       RECT workArea;
+       SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
+       int workWidth = workArea.right - workArea.left;
+       int workHeight = workArea.bottom - workArea.top;
+
+       // SDL forces the window height to be <= screen height - 27px (on Win8.1 - probably intended for the title bar) 
+       // If the task bar is docked to the the left screen border and we move the window to negative y,
+       // there would be some part of the regular desktop visible on the bottom of the screen.
+       int titleBarPixels = 2;
+       int screenHeight = GetSystemMetrics(SM_CYSCREEN);
+       if (screenHeight == workHeight)
+               titleBarPixels = -rect->top;
+
+       //Con_Printf("window mode: %dx%d, workArea: %d/%d-%d/%d (%dx%d), title: %d\n", width, height, workArea.left, workArea.top, workArea.right, workArea.bottom, workArea.right - workArea.left, workArea.bottom - workArea.top, titleBarPixels);
+
+       // if height and width matches the physical or previously adjusted screen height and width, adjust it to available desktop area
+       if ((width == GetSystemMetrics(SM_CXSCREEN) || width == workWidth) && (height == screenHeight || height == workHeight - titleBarPixels))
+       {
+               rect->left = workArea.left;
+               mode->width = workWidth;
+               rect->top = workArea.top + titleBarPixels;
+               mode->height = workHeight - titleBarPixels;
+       }
+       else 
+       {
+               rect->left = workArea.left + max(0, (workWidth - width) / 2);
+               rect->top = workArea.top + ((workHeight - height) / 2);
+       }
+}
+#endif
+
 static qboolean VID_InitModeGL(viddef_mode_t *mode)
 {
 #if SDL_MAJOR_VERSION == 1
@@ -2526,14 +2547,29 @@ static qboolean VID_InitModeGL(viddef_mode_t *mode)
                }
        }
 #else
+       int xPos = SDL_WINDOWPOS_UNDEFINED;
+       int yPos = SDL_WINDOWPOS_UNDEFINED;
        {
                if (mode->fullscreen) {
                        if (vid_desktopfullscreen.integer)
+                       {
+                               vid_mode_t *m = VID_GetDesktopMode();
+                               mode->width = m->width;
+                               mode->height = m->height;
                                windowflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
+                       }
                        else
                                windowflags |= SDL_WINDOW_FULLSCREEN;
                        vid_isfullscreen = true;
                }
+               else {
+#ifdef WIN32
+                       RECT rect;
+                       AdjustWindowBounds(mode, &rect);
+                       xPos = rect.left;
+                       yPos = rect.top;
+#endif
+               }
        }
 #endif
        //flags |= SDL_HWSURFACE;
@@ -2590,7 +2626,7 @@ static qboolean VID_InitModeGL(viddef_mode_t *mode)
        mode->height = screen->h;
 #else
        window_flags = windowflags;
-       window = SDL_CreateWindow(gamename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode->width, mode->height, windowflags);
+       window = SDL_CreateWindow(gamename, xPos, yPos, mode->width, mode->height, windowflags);
        if (window == NULL)
        {
                Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
@@ -2865,6 +2901,7 @@ void VID_Finish (void)
        vid_usevsync = (vid_vsync.integer && !cls.timedemo);
        if (vid_usingvsync != vid_usevsync)
        {
+               vid_usingvsync = vid_usevsync;
                if (SDL_GL_SetSwapInterval(vid_usevsync != 0) >= 0)
                        Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
                else