RetroArch/gfx/video_crt_switch.c

268 lines
6.9 KiB
C
Raw Normal View History

/* CRT SwitchRes Core
* Copyright (C) 2018 Alphanu / Ben Templeman.
*
* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2018-03-26 15:40:17 +01:00
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#if defined(_WIN32)
2018-04-15 17:55:37 +02:00
#include <windows.h>
#include "common/win32_common.h"
#endif
2018-03-26 15:40:17 +01:00
#include "video_driver.h"
2018-03-26 15:40:17 +01:00
#include "video_crt_switch.h"
2018-04-15 18:55:16 +02:00
static unsigned ra_core_width = 0;
static unsigned ra_core_height = 0;
static unsigned ra_tmp_width = 0;
static unsigned ra_tmp_height = 0;
static unsigned ra_set_core_hz = 0;
static unsigned orig_width = 0;
static unsigned orig_height = 0;
2018-04-15 17:12:28 +02:00
2018-04-15 18:59:58 +02:00
static bool first_run = true;
static float ra_tmp_core_hz = 0.0f;
static float fly_aspect = 0.0f;
static float ra_core_hz = 0.0f;
2018-03-26 15:40:17 +01:00
static void crt_check_first_run(void)
2018-04-15 17:12:28 +02:00
{
2018-04-15 18:59:58 +02:00
if (!first_run)
return;
/* TODO/FIXME - do we want to set first_run back to true
* at some point in time or should it stay like this? */
/* Run of first boot to get current display resolution */
2018-04-15 17:12:28 +02:00
#if defined(_WIN32)
2018-04-15 18:59:58 +02:00
orig_height = GetSystemMetrics(SM_CYSCREEN);
orig_width = GetSystemMetrics(SM_CXSCREEN);
2018-04-15 17:12:28 +02:00
#endif
2018-04-15 18:59:58 +02:00
first_run = false;
}
2018-03-26 15:40:17 +01:00
2018-04-15 18:59:58 +02:00
static void crt_switch_res(unsigned width, unsigned height,
int f_restore, int ra_hz)
2018-04-15 17:12:28 +02:00
{
/* Windows function to switch resolutions */
2018-04-15 17:12:28 +02:00
#if defined(_WIN32)
LONG res;
DEVMODE curDevmode;
DEVMODE devmode;
2018-04-15 17:12:28 +02:00
int iModeNum;
int freq = 0;
2018-04-15 17:12:28 +02:00
DWORD flags = 0;
int depth = 0;
2018-04-15 17:12:28 +02:00
if (f_restore == 0)
2018-04-15 18:59:58 +02:00
freq = ra_hz;
2018-04-15 17:12:28 +02:00
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &curDevmode);
2018-04-15 17:12:28 +02:00
/* used to stop superresolution bug */
if (width == curDevmode.dmPelsWidth)
width = 0;
if (width == 0)
width = curDevmode.dmPelsWidth;
if (height == 0)
height = curDevmode.dmPelsHeight;
if (depth == 0)
depth = curDevmode.dmBitsPerPel;
if (freq == 0)
freq = curDevmode.dmDisplayFrequency;
2018-04-15 17:12:28 +02:00
for (iModeNum = 0; ; iModeNum++)
{
if (!EnumDisplaySettings(NULL, iModeNum, &devmode))
break;
2018-04-15 17:12:28 +02:00
if (devmode.dmPelsWidth != width)
continue;
2018-03-26 15:40:17 +01:00
2018-04-15 17:12:28 +02:00
if (devmode.dmPelsHeight != height)
continue;
2018-03-26 15:40:17 +01:00
2018-04-15 17:12:28 +02:00
if (devmode.dmBitsPerPel != depth)
continue;
2018-04-15 17:12:28 +02:00
if (devmode.dmDisplayFrequency != freq)
continue;
2018-04-15 17:55:37 +02:00
devmode.dmFields |=
DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
res =
win32_change_display_settings(NULL, &devmode, CDS_TEST);
2018-04-15 17:12:28 +02:00
switch (res)
{
case DISP_CHANGE_SUCCESSFUL:
2018-04-15 17:55:37 +02:00
res = win32_change_display_settings(NULL, &devmode, flags);
2018-04-15 17:12:28 +02:00
switch (res)
{
case DISP_CHANGE_SUCCESSFUL:
return;
case DISP_CHANGE_NOTUPDATED:
return;
default:
break;
}
break;
2018-04-15 17:12:28 +02:00
case DISP_CHANGE_RESTART:
break;
default:
break;
}
2018-04-15 17:12:28 +02:00
}
#elif defined(linux)
2018-04-15 17:12:28 +02:00
#endif
}
static void switch_crt_hz(void)
{
if (ra_core_hz == ra_tmp_core_hz)
return;
/* set hz float an int for windows switching */
if (ra_core_hz < 53)
ra_set_core_hz = 50;
if (ra_core_hz >= 53 && ra_core_hz < 57)
ra_set_core_hz = 55;
if (ra_core_hz >= 57)
ra_set_core_hz = 60;
video_monitor_set_refresh_rate(ra_set_core_hz);
ra_tmp_core_hz = ra_core_hz;
}
2018-04-15 18:59:58 +02:00
static void crt_aspect_ratio_switch(unsigned width, unsigned height)
{
/* send aspect float to videeo_driver */
fly_aspect = (float)width / height;
video_driver_set_aspect_ratio_value((float)fly_aspect);
}
2018-04-15 18:59:58 +02:00
static void switch_res_crt(unsigned width, unsigned height)
{
if (height > 100)
{
2018-04-15 18:59:58 +02:00
crt_switch_res(width, height, 0, ra_set_core_hz);
video_driver_apply_state_changes();
}
}
/* Create correct aspect to fit video if resolution does not exist */
2018-04-15 18:59:58 +02:00
static void crt_screen_setup_aspect(unsigned width, unsigned height)
{
switch_crt_hz();
/* get original resolution of core */
if (height == 4)
{
/* detect menu only */
if (width < 1920)
width = 640;
height = 480;
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
}
if (height < 191 && height != 144)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 200;
}
if (height > 191)
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
if (height == 144 && ra_set_core_hz == 50)
{
height = 288;
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
}
if (height > 200 && height < 224)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 224;
}
if (height > 224 && height < 240)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 240;
}
if (height > 240 && height < 255)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 254;
}
if (height == 528 && ra_set_core_hz == 60)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 480;
}
if (height >= 240 && height < 255 && ra_set_core_hz == 55)
{
2018-04-15 18:55:16 +02:00
crt_aspect_ratio_switch(width, height);
height = 254;
}
switch_res_crt(width, height);
}
2018-04-15 18:55:16 +02:00
void crt_switch_res_core(unsigned width, unsigned height, float hz)
{
/* ra_core_hz float passed from within
* void video_driver_monitor_adjust_system_rates(void) */
ra_core_width = width;
ra_core_height = height;
ra_core_hz = hz;
crt_check_first_run();
/* Detect resolution change and switch */
if (
(ra_tmp_height != ra_core_height) ||
(ra_core_width != ra_tmp_width)
)
2018-04-15 18:55:16 +02:00
crt_screen_setup_aspect(width, height);
ra_tmp_height = ra_core_height;
ra_tmp_width = ra_core_width;
/* Check if aspect is correct, if notchange */
if (video_driver_get_aspect_ratio() != fly_aspect)
{
video_driver_set_aspect_ratio_value((float)fly_aspect);
video_driver_apply_state_changes();
}
}
void crt_video_restore(void)
{
crt_switch_res(orig_width, orig_height, 0, 60);
}