From 30c8990d6b3d5218ed0a774b7f809f6c71db1efa Mon Sep 17 00:00:00 2001 From: myT Date: Wed, 25 Sep 2019 05:11:01 +0200 Subject: [PATCH] fixed sdl_UpdateMonitorIndexFromWindow made r_monitor 0-based on Linux the concept of primary monitor isn't universal on Linux --- changelog.txt | 5 +++++ code/linux/sdl_glimp.cpp | 35 ++++++++++++++++++----------------- code/linux/sdl_local.h | 9 +++++++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/changelog.txt b/changelog.txt index 06575ca..8e61000 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,9 @@ +DD Mmm 19 - 1.52 + +fix: r_monitor on Linux is now 0-based and the value doesn't change incorrectly on its own anymore + + 31 Mar 19 - 1.51 add: now showing the cvar's current and latched values in the help panel diff --git a/code/linux/sdl_glimp.cpp b/code/linux/sdl_glimp.cpp index 9585d45..7354a32 100644 --- a/code/linux/sdl_glimp.cpp +++ b/code/linux/sdl_glimp.cpp @@ -11,7 +11,7 @@ glImp_t glimp; cvar_t* r_fullscreen; -static cvar_t* r_monitor; // 1-based, 0 means use primary monitor +static cvar_t* r_monitor; static const cvarTableItem_t glimp_cvars[] = { { &r_fullscreen, "r_fullscreen", "0", CVAR_ARCHIVE | CVAR_LATCH, CVART_BOOL, NULL, NULL, "full-screen mode" }, @@ -36,10 +36,10 @@ static qbool sdl_IsMonitorListValid() } -static int sdl_CompareMonitorRects( const void* aPtr, const void* bPtr ) +static int sdl_CompareMonitors( const void* aPtr, const void* bPtr ) { - const SDL_Rect* const a = (const SDL_Rect*)aPtr; - const SDL_Rect* const b = (const SDL_Rect*)bPtr; + const SDL_Rect* const a = &((const monitor_t*)aPtr)->rect; + const SDL_Rect* const b = &((const monitor_t*)bPtr)->rect; const int dy = a->y - b->y; if (dy != 0) return dy; @@ -60,13 +60,15 @@ static void sdl_CreateMonitorList() for (int si = 0; si < count; ++si) { if (gi >= MAX_MONITOR_COUNT) break; - if (SDL_GetDisplayBounds(si, &glimp.monitorRects[gi]) == 0) + if (SDL_GetDisplayBounds(si, &glimp.monitors[gi].rect) == 0) { + glimp.monitors[gi].sdlIndex = si; ++gi; + } } glimp.monitorCount = gi; if (sdl_IsMonitorListValid()) - qsort(glimp.monitorRects, (size_t)glimp.monitorCount, sizeof(glimp.monitorRects[0]), &sdl_CompareMonitorRects); + qsort(glimp.monitors, (size_t)glimp.monitorCount, sizeof(glimp.monitors[0]), &sdl_CompareMonitors); else glimp.monitorCount = 0; } @@ -93,16 +95,15 @@ void sdl_UpdateMonitorIndexFromWindow() if (glimp.monitorCount <= 0) return; - // update the glimp index - const int current = SDL_GetWindowDisplayIndex(glimp.window); - if (current < 0 || current >= glimp.monitorCount) { - glimp.monitorCount = 0; - return; + // try to find the glimp index and update data accordingly + const int sdlIndex = SDL_GetWindowDisplayIndex(glimp.window); + for (int i = 0; i < glimp.monitorCount; ++i) { + if (glimp.monitors[i].sdlIndex == sdlIndex) { + glimp.monitor = i; + Cvar_Set("r_monitor", va("%d", i)); + break; + } } - glimp.monitor = current; - - // update the cvar index - Cvar_Set("r_monitor", va("%d", glimp.monitor)); } @@ -115,7 +116,7 @@ static void sdl_GetSafeDesktopRect( SDL_Rect* rect ) rect->h = 720; } - *rect = glimp.monitorRects[glimp.monitor]; + *rect = glimp.monitors[glimp.monitor].rect; } @@ -129,7 +130,7 @@ static void sdl_PrintMonitorList() Com_Printf("Monitors detected (left is " S_COLOR_CVAR "r_monitor ^7value):\n"); for (int i = 0; i < count; ++i) { - const SDL_Rect rect = glimp.monitorRects[i]; + const SDL_Rect rect = glimp.monitors[i].rect; Com_Printf(S_COLOR_VAL "%d ^7%dx%d at %d,%d\n", i, rect.w, rect.h, rect.x, rect.y); } } diff --git a/code/linux/sdl_local.h b/code/linux/sdl_local.h index 9d9433c..8c8b91f 100644 --- a/code/linux/sdl_local.h +++ b/code/linux/sdl_local.h @@ -4,13 +4,18 @@ #define MAX_MONITOR_COUNT 16 +struct monitor_t { + SDL_Rect rect; + int sdlIndex; +}; + struct glImp_t { SDL_Window* window; SDL_GLContext glContext; - SDL_Rect monitorRects[MAX_MONITOR_COUNT]; + monitor_t monitors[MAX_MONITOR_COUNT]; int monitorCount; - int monitor; // current monitor, 0-based + int monitor; // indexes monitors };