(dispserv_x11.c) add missing defines and clean-up code

* Removed redundant and unused variables
* Use multiples of eight for array sizes
* Styled code for consistency with the rest
* Quoted string arguments in shell commands
* Added missing `HAVE_XRANDR` define checks
This commit is contained in:
Hugo Hromic 2019-02-22 16:57:20 +00:00
parent 97b7512420
commit 8984702ba8
1 changed files with 94 additions and 78 deletions

View File

@ -37,20 +37,17 @@
#include "../video_driver.h" /* needed to set refresh rate in set resolution */
#include "../video_crt_switch.h" /* needed to set aspect for low res in linux */
static unsigned orig_width = 0;
static unsigned orig_height = 0;
static char old_mode[250] = {0};
static char orig_output[250] = {0};
static char new_mode[250] = {0};
static char xrandr[250] = {0};
static char fbset[150] = {0};
static char output[500] = {0};
static char output4[500] = {0};
static char crt_name[10] = {0};
#ifdef HAVE_XRANDR
static char xrandr[1024] = {0};
static char crt_name[16] = {0};
static int crt_name_id = 0;
static bool crt_en = false;
static unsigned crtid = 20;
static unsigned crt_id = 20;
static char orig_output[256] = {0};
static char old_mode[256] = {0};
static char new_mode[256] = {0};
static XRRModeInfo crt_rrmode;
#endif
typedef struct
{
@ -71,27 +68,31 @@ static void* x11_display_server_init(void)
static void x11_display_server_destroy(void *data)
{
dispserv_x11_t *dispserv = (dispserv_x11_t*)data;
int i = 0;
#ifdef HAVE_XRANDR
if (crt_en)
{
snprintf(output, sizeof(output),
"xrandr --newmode \"700x480_59.94\" 13.849698 700 742 801 867 480 490 496 533 interlace -hsync -vsync");
system(output);
snprintf(output, sizeof(output),
"xrandr --addmode %s 700x480_59.94", orig_output);
system(output);
snprintf(output, sizeof(output),
"xrandr --output %s --mode 700x480_59.94", orig_output);
system(output);
snprintf(output, sizeof(output),
"xrandr --delmode %s %.s",orig_output, old_mode);
system(output);
snprintf(output, sizeof(output), "xrandr --rmmode %s", old_mode);
system(output);
snprintf(xrandr, sizeof(xrandr),
"xrandr --newmode 700x480_59.94 13.849698 700 742 801 867 480 490 496 533 interlace -hsync -vsync");
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --addmode \"%s\" 700x480_59.94",
orig_output);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --output \"%s\" --mode 700x480_59.94",
orig_output);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --delmode \"%s\" \"%s\"",
orig_output, old_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --rmmode \"%s\"",
old_mode);
system(xrandr);
}
#endif
if (dispserv)
free(dispserv);
@ -129,13 +130,14 @@ static bool x11_display_server_set_window_decorations(void *data, bool on)
return true;
}
#ifdef HAVE_XRANDR
static bool x11_display_server_set_resolution(void *data,
unsigned width, unsigned height, int int_hz, float hz, int center, int monitor_index, int xoffset)
{
int screen;
Window window;
XRRScreenResources *res = NULL;
Display *dsp = NULL;
Display *dpy = NULL;
int i = 0;
int hfp = 0;
int hsp = 0;
@ -147,21 +149,17 @@ static bool x11_display_server_set_resolution(void *data,
int vmax = 0;
int pdefault = 8;
int pwidth = 0;
int x_offset = center;
float roundw = 0.0f;
float roundh = 0.0f;
float pixel_clock = 0;
crt_en = true;
crt_en = true;
crt_name_id += 1;
snprintf(crt_name, sizeof(crt_name), "CRT%d", crt_name_id);
snprintf(old_mode, sizeof(old_mode), "%s", new_mode);
dsp = XOpenDisplay(NULL);
screen = DefaultScreen ( dsp );
window = RootWindow ( dsp, screen );
dpy = XOpenDisplay(0);
screen = DefaultScreen(dpy);
window = RootWindow(dpy, screen);
/* set core refresh from hz */
video_monitor_set_refresh_rate(hz);
@ -185,15 +183,17 @@ static bool x11_display_server_set_resolution(void *data,
if (width < 700)
{
hfp = width * 1.055;
hbp = width * roundw - 8;
}else {
hfp = (width * 1.055) + (width / 40);
hbp = (width * roundw) + (width /24);
hfp = width * 1.055;
hbp = width * roundw - 8;
}
else
{
hfp = (width * 1.055) + (width / 40);
hbp = (width * roundw) + (width /24);
xoffset = xoffset*2;
}
hsp = (width * 1.140) - (xoffset*4);
hsp = (width * 1.140) - (xoffset*4);
hmax = hbp;
@ -211,12 +211,10 @@ static bool x11_display_server_set_resolution(void *data,
vmax = 313;
if (height > 260 && height < 300)
vmax = 318;
if (height > 400 && hz > 56)
vmax = 533;
if (height > 520 && hz < 57)
vmax = 580;
if (height > 300 && hz < 56)
vmax = 615;
if (height > 500 && hz < 56)
@ -235,7 +233,6 @@ static bool x11_display_server_set_resolution(void *data,
if (height < 300)
pixel_clock = (hmax * vmax * hz) / 1000000;
if (height > 300)
pixel_clock = ((hmax * vmax * hz) / 1000000) / 2;
/* above code is the modeline generator */
@ -243,24 +240,27 @@ static bool x11_display_server_set_resolution(void *data,
/* create interlaced newmode from modline variables */
if (height < 300)
{
snprintf(xrandr, sizeof(xrandr), "xrandr --newmode \"%s_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d -hsync -vsync",crt_name, width, height, hz, pixel_clock,
width, hfp, hsp, hbp, height, vfp, vsp, vbp);
snprintf(xrandr, sizeof(xrandr),
"xrandr --newmode \"%s_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d -hsync -vsync",
crt_name, width, height, hz, pixel_clock, width, hfp, hsp, hbp, height, vfp, vsp, vbp);
system(xrandr);
}
/* create interlaced newmode from modline variables */
if (height > 300)
{
snprintf(xrandr, sizeof(xrandr), "xrandr --newmode \"%s_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d interlace -hsync -vsync",crt_name, width, height, hz, pixel_clock,
width, hfp, hsp, hbp, height, vfp, vsp, vbp);
snprintf(xrandr, sizeof(xrandr),
"xrandr --newmode \"%s_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d interlace -hsync -vsync",
crt_name, width, height, hz, pixel_clock, width, hfp, hsp, hbp, height, vfp, vsp, vbp);
system(xrandr);
}
/* variable for new mode */
snprintf(new_mode, sizeof(new_mode), "%s_%dx%d_%0.2f",crt_name, width, height, hz);
snprintf(new_mode, sizeof(new_mode), "%s_%dx%d_%0.2f", crt_name, width, height, hz);
/* need to run loops for DVI0 - DVI-2 and VGA0 - VGA-2 outputs to
* add and delete modes */
crt_rrmode.id = crtid;
crt_rrmode.id = crt_id;
crt_rrmode.width = width;
crt_rrmode.height = height;
crt_rrmode.dotClock = pixel_clock;
@ -275,54 +275,66 @@ static bool x11_display_server_set_resolution(void *data,
crt_rrmode.nameLength = sizeof(new_mode);
crt_rrmode.modeFlags = 0;
res = XRRGetScreenResources (dsp, window);
res = XRRGetScreenResources(dpy, window);
if (monitor_index == 0)
{
int i;
for (i = 0; i < res->noutput; i++)
{
XRROutputInfo *outputs = XRRGetOutputInfo (dsp, res, res->outputs[i]);
XRROutputInfo *outputs = XRRGetOutputInfo(dpy, res, res->outputs[i]);
if (outputs->connection == RR_Connected)
{
snprintf(orig_output, sizeof(orig_output), "%s", outputs->name);
snprintf(output4, sizeof(output4), "xrandr --addmode %s %s",outputs->name ,new_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --output %s --mode %s", outputs->name, new_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --delmode %s %s", outputs->name,old_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --rmmode %s", old_mode);
system(output4);
snprintf(xrandr, sizeof(xrandr),
"xrandr --addmode \"%s\" \"%s\"",
outputs->name, new_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --output \"%s\" --mode \"%s\"",
outputs->name, new_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --delmode \"%s\" \"%s\"",
outputs->name, old_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --rmmode \"%s\"",
old_mode);
system(xrandr);
}
}
}
else if (monitor_index > 0)
{
XRROutputInfo *outputs = XRRGetOutputInfo (dsp, res, res->outputs[monitor_index]);
XRROutputInfo *outputs = XRRGetOutputInfo(dpy, res, res->outputs[monitor_index]);
if (outputs->connection == RR_Connected)
{
snprintf(orig_output, sizeof(orig_output), "%s", outputs->name);
snprintf(output4, sizeof(output4), "xrandr --addmode %s %s",outputs->name ,new_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --output %s --mode %s", outputs->name, new_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --delmode %s %s", outputs->name, old_mode);
system(output4);
snprintf(output4, sizeof(output4), "xrandr --rmmode %s", old_mode);
system(output4);
snprintf(xrandr, sizeof(xrandr),
"xrandr --addmode \"%s\" \"%s\"",
outputs->name, new_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --output \"%s\" --mode \"%s\"",
outputs->name, new_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --delmode \"%s\" \"%s\"",
outputs->name, old_mode);
system(xrandr);
snprintf(xrandr, sizeof(xrandr),
"xrandr --rmmode \"%s\"",
old_mode);
system(xrandr);
}
}
return true;
}
#endif
const char *x11_display_server_get_output_options(void *data)
{
@ -515,15 +527,19 @@ const video_display_server_t dispserv_x11 = {
x11_display_server_set_window_opacity,
NULL, /* set_window_progress */
x11_display_server_set_window_decorations,
#ifdef HAVE_XRANDR
x11_display_server_set_resolution,
#else
NULL, /* set_resolution */
#endif
NULL, /* get_resolution_list */
x11_display_server_get_output_options,
#ifdef HAVE_XRANDR
x11_display_server_set_screen_orientation,
x11_display_server_get_screen_orientation,
#else
NULL,
NULL,
NULL, /* set_screen_orientation */
NULL, /* get_screen_orientation */
#endif
"x11"
};