fixed sdl_UpdateMonitorIndexFromWindow

made r_monitor 0-based on Linux
the concept of primary monitor isn't universal on Linux
This commit is contained in:
myT 2019-09-25 05:11:01 +02:00
parent 5e4d41df8e
commit 30c8990d6b
3 changed files with 30 additions and 19 deletions

View File

@ -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 31 Mar 19 - 1.51
add: now showing the cvar's current and latched values in the help panel add: now showing the cvar's current and latched values in the help panel

View File

@ -11,7 +11,7 @@ glImp_t glimp;
cvar_t* r_fullscreen; 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[] = { static const cvarTableItem_t glimp_cvars[] = {
{ &r_fullscreen, "r_fullscreen", "0", CVAR_ARCHIVE | CVAR_LATCH, CVART_BOOL, NULL, NULL, "full-screen mode" }, { &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 a = &((const monitor_t*)aPtr)->rect;
const SDL_Rect* const b = (const SDL_Rect*)bPtr; const SDL_Rect* const b = &((const monitor_t*)bPtr)->rect;
const int dy = a->y - b->y; const int dy = a->y - b->y;
if (dy != 0) if (dy != 0)
return dy; return dy;
@ -60,13 +60,15 @@ static void sdl_CreateMonitorList()
for (int si = 0; si < count; ++si) { for (int si = 0; si < count; ++si) {
if (gi >= MAX_MONITOR_COUNT) if (gi >= MAX_MONITOR_COUNT)
break; break;
if (SDL_GetDisplayBounds(si, &glimp.monitorRects[gi]) == 0) if (SDL_GetDisplayBounds(si, &glimp.monitors[gi].rect) == 0) {
glimp.monitors[gi].sdlIndex = si;
++gi; ++gi;
}
} }
glimp.monitorCount = gi; glimp.monitorCount = gi;
if (sdl_IsMonitorListValid()) 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 else
glimp.monitorCount = 0; glimp.monitorCount = 0;
} }
@ -93,16 +95,15 @@ void sdl_UpdateMonitorIndexFromWindow()
if (glimp.monitorCount <= 0) if (glimp.monitorCount <= 0)
return; return;
// update the glimp index // try to find the glimp index and update data accordingly
const int current = SDL_GetWindowDisplayIndex(glimp.window); const int sdlIndex = SDL_GetWindowDisplayIndex(glimp.window);
if (current < 0 || current >= glimp.monitorCount) { for (int i = 0; i < glimp.monitorCount; ++i) {
glimp.monitorCount = 0; if (glimp.monitors[i].sdlIndex == sdlIndex) {
return; 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->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"); Com_Printf("Monitors detected (left is " S_COLOR_CVAR "r_monitor ^7value):\n");
for (int i = 0; i < count; ++i) { 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); Com_Printf(S_COLOR_VAL "%d ^7%dx%d at %d,%d\n", i, rect.w, rect.h, rect.x, rect.y);
} }
} }

View File

@ -4,13 +4,18 @@
#define MAX_MONITOR_COUNT 16 #define MAX_MONITOR_COUNT 16
struct monitor_t {
SDL_Rect rect;
int sdlIndex;
};
struct glImp_t { struct glImp_t {
SDL_Window* window; SDL_Window* window;
SDL_GLContext glContext; SDL_GLContext glContext;
SDL_Rect monitorRects[MAX_MONITOR_COUNT]; monitor_t monitors[MAX_MONITOR_COUNT];
int monitorCount; int monitorCount;
int monitor; // current monitor, 0-based int monitor; // indexes monitors
}; };