From f6a9690a36d618a4e6fe9ffe71aeb86a6caf5c5b Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sun, 16 Jun 2019 00:50:15 -0400 Subject: [PATCH 1/6] (Multi-Monitor)Can Choose Which Monitor to Use -Adds option in the "VIDEO" menu for "display index" that lets you specify which monitor you would like to use. --- src/client/menu/videomenu.c | 21 +++++++++++ src/client/vid/glimp_sdl.c | 71 ++++++++++++++++++++++++++++++++++++- src/client/vid/header/vid.h | 3 ++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/client/menu/videomenu.c b/src/client/menu/videomenu.c index 6d3db754..9662d395 100644 --- a/src/client/menu/videomenu.c +++ b/src/client/menu/videomenu.c @@ -32,6 +32,7 @@ extern void M_ForceMenuOff(void); static cvar_t *r_mode; +static cvar_t *vid_displayindex; static cvar_t *r_hudscale; static cvar_t *r_consolescale; static cvar_t *r_menuscale; @@ -49,6 +50,7 @@ static menuframework_s s_opengl_menu; static menulist_s s_renderer_list; static menulist_s s_mode_list; +static menulist_s s_display_list; static menulist_s s_uiscale_list; static menuslider_s s_brightness_slider; static menuslider_s s_fov_slider; @@ -190,6 +192,12 @@ ApplyChanges(void *unused) Cvar_SetValue("r_mode", s_mode_list.curvalue); } + if (s_display_list.curvalue != GLimp_GetWindowDisplayIndex() ) + { + Cvar_SetValue( "vid_displayindex", s_display_list.curvalue ); + restart = true; + } + /* UI scaling */ if (s_uiscale_list.curvalue == 0) { @@ -333,6 +341,11 @@ VID_MenuInit(void) r_mode = Cvar_Get("r_mode", "4", 0); } + if (!vid_displayindex) + { + vid_displayindex = Cvar_Get("vid_displayindex", "0", CVAR_ARCHIVE); + } + if (!r_hudscale) { r_hudscale = Cvar_Get("r_hudscale", "-1", CVAR_ARCHIVE); @@ -414,6 +427,13 @@ VID_MenuInit(void) s_mode_list.curvalue = GetCustomValue(&s_mode_list); } + s_display_list.generic.type = MTYPE_SPINCONTROL; + s_display_list.generic.name = "display index"; + s_display_list.generic.x = 0; + s_display_list.generic.y = (y += 10); + s_display_list.itemnames = GLimp_GetDisplayIndices(); + s_display_list.curvalue = GLimp_GetWindowDisplayIndex(); + s_brightness_slider.generic.type = MTYPE_SLIDER; s_brightness_slider.generic.name = "brightness"; s_brightness_slider.generic.x = 0; @@ -519,6 +539,7 @@ VID_MenuInit(void) Menu_AddItem(&s_opengl_menu, (void *)&s_renderer_list); Menu_AddItem(&s_opengl_menu, (void *)&s_mode_list); + Menu_AddItem(&s_opengl_menu, (void *)&s_display_list); Menu_AddItem(&s_opengl_menu, (void *)&s_brightness_slider); Menu_AddItem(&s_opengl_menu, (void *)&s_fov_slider); Menu_AddItem(&s_opengl_menu, (void *)&s_uiscale_list); diff --git a/src/client/vid/glimp_sdl.c b/src/client/vid/glimp_sdl.c index 6452ab7a..4ef449d1 100644 --- a/src/client/vid/glimp_sdl.c +++ b/src/client/vid/glimp_sdl.c @@ -34,6 +34,7 @@ #include cvar_t *vid_displayrefreshrate; +static cvar_t *vid_displayindex; int glimp_refreshRate = -1; static int last_flags = 0; @@ -42,14 +43,77 @@ static int last_position_x = SDL_WINDOWPOS_UNDEFINED; static int last_position_y = SDL_WINDOWPOS_UNDEFINED; static SDL_Window* window = NULL; static qboolean initSuccessful = false; +static char **displayindices = NULL; +static int num_displays = 0; + +const char** +GLimp_GetDisplayIndices(void) +{ + return (const char**)displayindices; +} + +int +GLimp_GetWindowDisplayIndex(void) +{ + return last_display; +} + +int +GLimp_GetNumVideoDisplays(void) +{ + return num_displays; +} + +static void +ClearDisplayIndices(void) +{ + if ( displayindices ) + { + for ( int i = 0; i < num_displays; i++ ) + free( displayindices[ i ] ); + + free( displayindices ); + displayindices = NULL; + } +} + +static void +InitDisplayIndices(qboolean ClearExisting) +{ + if ( ClearExisting ) + ClearDisplayIndices(); + + displayindices = malloc( ( num_displays + 1 ) * sizeof( char* ) ); + for ( int i = 0; i < num_displays; i++ ) + { + displayindices[ i ] = malloc( 11 * sizeof( char ) ); // There are a maximum of 10 digits in 32 bit int + 1 for the NULL terminator + snprintf( displayindices[ i ], 11, "%d", i ); + } + + // The last entry is NULL to indicate the list of strings ends + displayindices[ num_displays ] = 0; +} // -------- static qboolean CreateSDLWindow(int flags, int w, int h) { + num_displays = SDL_GetNumVideoDisplays(); + int displayindex = 0; + + if ( vid_displayindex->value < 0 || vid_displayindex->value >= num_displays) + Cvar_SetValue( "vid_displayindex", 0 ); + else + { + displayindex = vid_displayindex->value; + } + + // last_position_x and last_position_y aren't really used... + const int windowpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY( displayindex ); + window = SDL_CreateWindow("Yamagi Quake II", - last_position_x, last_position_y, + windowpos, windowpos, w, h, flags); if (window) { @@ -57,6 +121,8 @@ CreateSDLWindow(int flags, int w, int h) last_display = SDL_GetWindowDisplayIndex(window); SDL_GetWindowPosition(window, &last_position_x, &last_position_y); + + InitDisplayIndices( true ); } return window != NULL; @@ -157,6 +223,8 @@ ShutdownGraphics(void) window = NULL; } + ClearDisplayIndices(); + // make sure that after vid_restart the refreshrate will be queried from SDL2 again. glimp_refreshRate = -1; @@ -172,6 +240,7 @@ qboolean GLimp_Init(void) { vid_displayrefreshrate = Cvar_Get("vid_displayrefreshrate", "-1", CVAR_ARCHIVE); + vid_displayindex = Cvar_Get("vid_displayindex", "0", CVAR_ARCHIVE); if (!SDL_WasInit(SDL_INIT_VIDEO)) { diff --git a/src/client/vid/header/vid.h b/src/client/vid/header/vid.h index 57ebdc57..63bdcf48 100644 --- a/src/client/vid/header/vid.h +++ b/src/client/vid/header/vid.h @@ -55,6 +55,9 @@ const char *VID_MenuKey(int); // Stuff provided by platform backend. extern int glimp_refreshRate; +const char **GLimp_GetDisplayIndices(void); +int GLimp_GetWindowDisplayIndex(void); +int GLimp_GetNumVideoDisplays(void); qboolean GLimp_Init(void); void GLimp_Shutdown(void); qboolean GLimp_InitGraphics(int fullscreen, int *pwidth, int *pheight); From 03e3c6bf718886dec979ef021e500c7a0f15c927 Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sun, 16 Jun 2019 01:02:53 -0400 Subject: [PATCH 2/6] Move 'num_displays' Initialization -SDL_GetNumVideoDisplays() will always remain the same after the call to SDL_Init(SDL_INIT_VIDEO), so it makes sense to set in GLimp_Init where we do this. --- src/client/vid/glimp_sdl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/vid/glimp_sdl.c b/src/client/vid/glimp_sdl.c index 4ef449d1..105b3a1e 100644 --- a/src/client/vid/glimp_sdl.c +++ b/src/client/vid/glimp_sdl.c @@ -99,7 +99,6 @@ InitDisplayIndices(qboolean ClearExisting) static qboolean CreateSDLWindow(int flags, int w, int h) { - num_displays = SDL_GetNumVideoDisplays(); int displayindex = 0; if ( vid_displayindex->value < 0 || vid_displayindex->value >= num_displays) @@ -256,6 +255,8 @@ GLimp_Init(void) SDL_GetVersion(&version); Com_Printf("SDL version is: %i.%i.%i\n", (int)version.major, (int)version.minor, (int)version.patch); Com_Printf("SDL video driver is \"%s\".\n", SDL_GetCurrentVideoDriver()); + + num_displays = SDL_GetNumVideoDisplays(); } return true; From 0da8099de8352691892e7fd92748a0a6e3b8efc1 Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sun, 16 Jun 2019 01:11:20 -0400 Subject: [PATCH 3/6] (Multi-Monitor)Code Cleanup -We only need to init the display indices once in GLimp_Init -We only need to clear the display indices once in GLimp_Shutdown -Remove extra 'displayindex' variable --- src/client/vid/glimp_sdl.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/client/vid/glimp_sdl.c b/src/client/vid/glimp_sdl.c index 105b3a1e..5254d724 100644 --- a/src/client/vid/glimp_sdl.c +++ b/src/client/vid/glimp_sdl.c @@ -78,11 +78,8 @@ ClearDisplayIndices(void) } static void -InitDisplayIndices(qboolean ClearExisting) +InitDisplayIndices() { - if ( ClearExisting ) - ClearDisplayIndices(); - displayindices = malloc( ( num_displays + 1 ) * sizeof( char* ) ); for ( int i = 0; i < num_displays; i++ ) { @@ -99,17 +96,12 @@ InitDisplayIndices(qboolean ClearExisting) static qboolean CreateSDLWindow(int flags, int w, int h) { - int displayindex = 0; - + // Reset display index Cvar if out of bounds if ( vid_displayindex->value < 0 || vid_displayindex->value >= num_displays) Cvar_SetValue( "vid_displayindex", 0 ); - else - { - displayindex = vid_displayindex->value; - } // last_position_x and last_position_y aren't really used... - const int windowpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY( displayindex ); + const int windowpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY( (int)vid_displayindex->value ); window = SDL_CreateWindow("Yamagi Quake II", windowpos, windowpos, @@ -120,8 +112,6 @@ CreateSDLWindow(int flags, int w, int h) last_display = SDL_GetWindowDisplayIndex(window); SDL_GetWindowPosition(window, &last_position_x, &last_position_y); - - InitDisplayIndices( true ); } return window != NULL; @@ -222,8 +212,6 @@ ShutdownGraphics(void) window = NULL; } - ClearDisplayIndices(); - // make sure that after vid_restart the refreshrate will be queried from SDL2 again. glimp_refreshRate = -1; @@ -257,6 +245,7 @@ GLimp_Init(void) Com_Printf("SDL video driver is \"%s\".\n", SDL_GetCurrentVideoDriver()); num_displays = SDL_GetNumVideoDisplays(); + InitDisplayIndices(); } return true; @@ -280,6 +269,8 @@ GLimp_Shutdown(void) { SDL_QuitSubSystem(SDL_INIT_VIDEO); } + + ClearDisplayIndices(); } /* From fc78aefee3099e7bd19c263cb313acdbc8420211 Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sun, 16 Jun 2019 17:34:00 -0400 Subject: [PATCH 4/6] (Multi-Monitor)Better Handling -Add back use of last_position_x and last_position_y -last_position_x and last_position_y will be set to undefined when the window is shutdown IF the current display used is not the desired display -last_display will be set to desired display at window shutdown if not the same -vid_displayindex clamped using ClampDisplayIndexCvar() at startup and window shutdown --- src/client/vid/glimp_sdl.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/client/vid/glimp_sdl.c b/src/client/vid/glimp_sdl.c index 5254d724..6f1fb50e 100644 --- a/src/client/vid/glimp_sdl.c +++ b/src/client/vid/glimp_sdl.c @@ -64,6 +64,16 @@ GLimp_GetNumVideoDisplays(void) return num_displays; } +/* + * Resets the display index Cvar if out of bounds + */ +static void +ClampDisplayIndexCvar(void) +{ + if (vid_displayindex->value < 0 || vid_displayindex->value >= num_displays) + Cvar_SetValue("vid_displayindex", 0); +} + static void ClearDisplayIndices(void) { @@ -96,15 +106,12 @@ InitDisplayIndices() static qboolean CreateSDLWindow(int flags, int w, int h) { - // Reset display index Cvar if out of bounds - if ( vid_displayindex->value < 0 || vid_displayindex->value >= num_displays) - Cvar_SetValue( "vid_displayindex", 0 ); - - // last_position_x and last_position_y aren't really used... - const int windowpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY( (int)vid_displayindex->value ); + if (SDL_WINDOWPOS_ISUNDEFINED(last_position_x) || SDL_WINDOWPOS_ISUNDEFINED(last_position_y)) { + last_position_x = last_position_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY((int)vid_displayindex->value); + } window = SDL_CreateWindow("Yamagi Quake II", - windowpos, windowpos, + last_position_x, last_position_y, w, h, flags); if (window) { @@ -199,12 +206,23 @@ void GLimp_GrabInput(qboolean grab); static void ShutdownGraphics(void) { + ClampDisplayIndexCvar(); + if (window) { /* save current display as default */ last_display = SDL_GetWindowDisplayIndex(window); + + /* or if current display isn't the desired default */ + if (last_display != vid_displayindex->value) { + last_position_x = last_position_y = SDL_WINDOWPOS_UNDEFINED; + last_display = vid_displayindex->value; + } + else { SDL_GetWindowPosition(window, &last_position_x, &last_position_y); + } + /* cleanly ungrab input (needs window) */ GLimp_GrabInput(false); SDL_DestroyWindow(window); @@ -246,6 +264,7 @@ GLimp_Init(void) num_displays = SDL_GetNumVideoDisplays(); InitDisplayIndices(); + ClampDisplayIndexCvar(); } return true; From 39728eb5d427878fc3c6dfbd60df649a5dfe0c6a Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sun, 16 Jun 2019 17:35:55 -0400 Subject: [PATCH 5/6] (Multi-Monitor)Hide "display index" Option If Single Display -The "display index" option will now no longer show up in the "VIDEO" menu if user only has a single display --- src/client/menu/videomenu.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/client/menu/videomenu.c b/src/client/menu/videomenu.c index 9662d395..46be5623 100644 --- a/src/client/menu/videomenu.c +++ b/src/client/menu/videomenu.c @@ -427,12 +427,15 @@ VID_MenuInit(void) s_mode_list.curvalue = GetCustomValue(&s_mode_list); } - s_display_list.generic.type = MTYPE_SPINCONTROL; - s_display_list.generic.name = "display index"; - s_display_list.generic.x = 0; - s_display_list.generic.y = (y += 10); - s_display_list.itemnames = GLimp_GetDisplayIndices(); - s_display_list.curvalue = GLimp_GetWindowDisplayIndex(); + if (GLimp_GetNumVideoDisplays() > 1) + { + s_display_list.generic.type = MTYPE_SPINCONTROL; + s_display_list.generic.name = "display index"; + s_display_list.generic.x = 0; + s_display_list.generic.y = (y += 10); + s_display_list.itemnames = GLimp_GetDisplayIndices(); + s_display_list.curvalue = GLimp_GetWindowDisplayIndex(); + } s_brightness_slider.generic.type = MTYPE_SLIDER; s_brightness_slider.generic.name = "brightness"; @@ -539,7 +542,13 @@ VID_MenuInit(void) Menu_AddItem(&s_opengl_menu, (void *)&s_renderer_list); Menu_AddItem(&s_opengl_menu, (void *)&s_mode_list); - Menu_AddItem(&s_opengl_menu, (void *)&s_display_list); + + // only show this option if we have multiple displays + if (GLimp_GetNumVideoDisplays() > 1) + { + Menu_AddItem(&s_opengl_menu, (void *)&s_display_list); + } + Menu_AddItem(&s_opengl_menu, (void *)&s_brightness_slider); Menu_AddItem(&s_opengl_menu, (void *)&s_fov_slider); Menu_AddItem(&s_opengl_menu, (void *)&s_uiscale_list); From 4f5de01d8e31be960b0a7c539032fa50a709e58f Mon Sep 17 00:00:00 2001 From: Spirrwell Date: Sat, 29 Jun 2019 13:54:59 -0400 Subject: [PATCH 6/6] (Code Formatting)Fix Indentation --- src/client/vid/glimp_sdl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/vid/glimp_sdl.c b/src/client/vid/glimp_sdl.c index 6f1fb50e..ef315baf 100644 --- a/src/client/vid/glimp_sdl.c +++ b/src/client/vid/glimp_sdl.c @@ -219,7 +219,7 @@ ShutdownGraphics(void) last_display = vid_displayindex->value; } else { - SDL_GetWindowPosition(window, + SDL_GetWindowPosition(window, &last_position_x, &last_position_y); }