2018-05-25 10:56:22 +01:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
|
|
|
* Copyright (C) 2016-2017 - Brad Parker
|
|
|
|
*
|
|
|
|
* 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-11-27 11:28:24 +01:00
|
|
|
|
|
|
|
#include <compat/strl.h>
|
|
|
|
|
2018-08-27 16:09:12 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2019-01-30 22:46:29 +00:00
|
|
|
#include <X11/Xlib.h>
|
2019-02-03 15:49:35 -08:00
|
|
|
#include <X11/extensions/Xrandr.h> // run pkg-config --static --libs xrandr
|
2019-01-30 22:46:29 +00:00
|
|
|
#include <X11/extensions/randr.h>
|
|
|
|
#include <X11/extensions/Xrender.h>
|
2018-05-25 10:56:22 +01:00
|
|
|
|
2018-11-27 11:28:24 +01:00
|
|
|
#include "../../config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_XRANDR
|
|
|
|
#include <X11/extensions/Xrandr.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
#include "../video_display_server.h"
|
|
|
|
#include "../common/x11_common.h"
|
|
|
|
#include "../../configuration.h"
|
|
|
|
#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 */
|
|
|
|
|
2018-09-08 22:11:31 +01:00
|
|
|
static unsigned orig_width = 0;
|
|
|
|
static unsigned orig_height = 0;
|
2018-09-10 19:36:57 +01:00
|
|
|
static char old_mode[250] = {0};
|
2019-01-30 22:28:46 +00:00
|
|
|
static char orig_output[250] = {0};
|
2018-09-10 19:36:57 +01:00
|
|
|
static char new_mode[250] = {0};
|
|
|
|
static char xrandr[250] = {0};
|
|
|
|
static char fbset[150] = {0};
|
2018-11-25 17:06:55 +01:00
|
|
|
static char output[500] = {0};
|
2019-01-30 22:34:18 +00:00
|
|
|
static char output4[500] = {0};
|
2018-09-10 19:36:57 +01:00
|
|
|
static bool crt_en = false;
|
2019-01-30 22:08:04 +00:00
|
|
|
static unsigned crtid = 20;
|
2019-01-30 22:41:13 +00:00
|
|
|
static XRRModeInfo crt_rrmode;
|
2018-05-25 10:56:22 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned opacity;
|
|
|
|
bool decorations;
|
|
|
|
} dispserv_x11_t;
|
|
|
|
|
|
|
|
static void* x11_display_server_init(void)
|
|
|
|
{
|
|
|
|
dispserv_x11_t *dispserv = (dispserv_x11_t*)calloc(1, sizeof(*dispserv));
|
|
|
|
|
|
|
|
if (!dispserv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return dispserv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void x11_display_server_destroy(void *data)
|
|
|
|
{
|
2018-10-02 16:56:28 -04:00
|
|
|
dispserv_x11_t *dispserv = (dispserv_x11_t*)data;
|
2018-09-08 22:11:31 +01:00
|
|
|
int i = 0;
|
2018-10-02 16:56:28 -04:00
|
|
|
|
|
|
|
if (crt_en)
|
2018-09-08 22:11:31 +01:00
|
|
|
{
|
2019-01-30 21:55:33 +00:00
|
|
|
snprintf(output, sizeof(output),
|
2019-01-31 15:13:36 +01:00
|
|
|
"xrandr --newmode \"700x480_59.94\" 13.849698 700 742 801 867 480 490 496 533 interlace -hsync -vsync");
|
2019-01-30 21:55:33 +00:00
|
|
|
system(output);
|
|
|
|
snprintf(output, sizeof(output),
|
2019-01-31 15:13:36 +01:00
|
|
|
"xrandr --addmode %s 700x480_59.94", orig_output);
|
2019-01-30 21:55:33 +00:00
|
|
|
system(output);
|
2018-11-25 17:09:00 +01:00
|
|
|
snprintf(output, sizeof(output),
|
2019-01-31 15:13:36 +01:00
|
|
|
"xrandr --output %s --mode 700x480_59.94", orig_output);
|
2018-09-08 22:11:31 +01:00
|
|
|
system(output);
|
2018-10-02 16:56:28 -04:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
snprintf(output, sizeof(output),
|
2019-02-01 20:41:01 +00:00
|
|
|
"xrandr --delmode %s %.s",orig_output, old_mode);
|
2019-01-30 21:55:33 +00:00
|
|
|
system(output);
|
|
|
|
|
2018-11-25 17:09:00 +01:00
|
|
|
snprintf(output, sizeof(output), "xrandr --rmmode %s", old_mode);
|
2018-10-02 16:56:28 -04:00
|
|
|
system(output);
|
2018-09-10 17:42:33 +01:00
|
|
|
}
|
2018-05-25 10:56:22 +01:00
|
|
|
|
|
|
|
if (dispserv)
|
|
|
|
free(dispserv);
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:40:17 +02:00
|
|
|
static bool x11_display_server_set_window_opacity(void *data, unsigned opacity)
|
2018-05-25 10:56:22 +01:00
|
|
|
{
|
|
|
|
dispserv_x11_t *serv = (dispserv_x11_t*)data;
|
|
|
|
Atom net_wm_opacity = XInternAtom(g_x11_dpy, "_NET_WM_WINDOW_OPACITY", False);
|
|
|
|
Atom cardinal = XInternAtom(g_x11_dpy, "CARDINAL", False);
|
|
|
|
|
|
|
|
serv->opacity = opacity;
|
|
|
|
|
|
|
|
opacity = opacity * ((unsigned)-1 / 100.0);
|
|
|
|
|
|
|
|
if (opacity == (unsigned)-1)
|
|
|
|
XDeleteProperty(g_x11_dpy, g_x11_win, net_wm_opacity);
|
2018-10-02 16:56:28 -04:00
|
|
|
else
|
2018-11-24 19:38:01 +01:00
|
|
|
XChangeProperty(g_x11_dpy, g_x11_win, net_wm_opacity, cardinal,
|
|
|
|
32, PropModeReplace, (const unsigned char*)&opacity, 1);
|
2018-05-25 10:56:22 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:40:17 +02:00
|
|
|
static bool x11_display_server_set_window_decorations(void *data, bool on)
|
2018-05-25 10:56:22 +01:00
|
|
|
{
|
|
|
|
dispserv_x11_t *serv = (dispserv_x11_t*)data;
|
|
|
|
|
2018-11-24 19:38:01 +01:00
|
|
|
if (serv)
|
|
|
|
serv->decorations = on;
|
2018-05-25 10:56:22 +01:00
|
|
|
|
2019-02-03 15:49:35 -08:00
|
|
|
/* menu_setting performs a reinit instead to properly apply
|
2018-11-24 19:38:01 +01:00
|
|
|
* decoration changes */
|
2018-05-25 10:56:22 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:40:17 +02:00
|
|
|
static bool x11_display_server_set_resolution(void *data,
|
2019-01-30 21:55:33 +00:00
|
|
|
unsigned width, unsigned height, int int_hz, float hz, int center, int monitor_index)
|
2018-05-25 10:56:22 +01:00
|
|
|
{
|
2019-01-31 15:13:36 +01:00
|
|
|
int screen;
|
|
|
|
Window window;
|
|
|
|
XRRScreenResources *res = NULL;
|
|
|
|
Display *dsp = NULL;
|
|
|
|
Screen *scrn = NULL;
|
|
|
|
int i = 0;
|
|
|
|
int hfp = 0;
|
|
|
|
int hsp = 0;
|
|
|
|
int hbp = 0;
|
|
|
|
int vfp = 0;
|
|
|
|
int vsp = 0;
|
|
|
|
int vbp = 0;
|
|
|
|
int hmax = 0;
|
|
|
|
int vmax = 0;
|
|
|
|
int pdefault = 8;
|
|
|
|
int pwidth = 0;
|
|
|
|
float roundw = 0.0f;
|
|
|
|
float roundh = 0.0f;
|
|
|
|
float pixel_clock = 0;
|
|
|
|
|
|
|
|
crt_en = true;
|
|
|
|
|
2019-02-03 15:49:35 -08:00
|
|
|
snprintf(old_mode, sizeof(old_mode), "%s", new_mode);
|
2019-01-31 15:13:36 +01:00
|
|
|
|
|
|
|
dsp = XOpenDisplay(NULL);
|
|
|
|
scrn = DefaultScreenOfDisplay(dsp);
|
|
|
|
screen = DefaultScreen ( dsp );
|
|
|
|
window = RootWindow ( dsp, screen );
|
2018-10-02 16:56:28 -04:00
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
/* set core refresh from hz */
|
2018-10-02 16:56:28 -04:00
|
|
|
video_monitor_set_refresh_rate(hz);
|
|
|
|
|
2018-11-24 19:38:01 +01:00
|
|
|
/* following code is the mode line generator */
|
2019-01-31 15:13:36 +01:00
|
|
|
hsp = width * 1.140;
|
|
|
|
hfp = width * 1.055;
|
2018-09-08 22:11:31 +01:00
|
|
|
pwidth = width;
|
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
if (height < 400 && width > 400)
|
|
|
|
pwidth = width / 2;
|
2018-09-08 22:11:31 +01:00
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
roundw = roundf((float)pwidth / (float)height * 100) / 100;
|
2018-09-08 22:11:31 +01:00
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
if (height > width)
|
|
|
|
roundw = roundf((float)height / (float)width * 100) / 100;
|
2018-05-25 10:56:22 +01:00
|
|
|
|
2018-09-08 22:11:31 +01:00
|
|
|
if (roundw > 1.35)
|
|
|
|
roundw = 1.25;
|
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
if (roundw < 1.20)
|
2018-09-08 22:11:31 +01:00
|
|
|
roundw = 1.34;
|
|
|
|
|
2019-01-31 15:13:36 +01:00
|
|
|
hbp = width * roundw - 8;
|
2018-05-25 10:56:22 +01:00
|
|
|
hmax = hbp;
|
2018-10-02 16:56:28 -04:00
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
if (height < 241)
|
|
|
|
vmax = 261;
|
|
|
|
if (height < 241 && hz > 56 && hz < 58)
|
|
|
|
vmax = 280;
|
2018-10-02 16:56:28 -04:00
|
|
|
if (height < 241 && hz < 55)
|
2018-05-25 10:56:22 +01:00
|
|
|
vmax = 313;
|
|
|
|
if (height > 250 && height < 260 && hz > 54)
|
|
|
|
vmax = 296;
|
|
|
|
if (height > 250 && height < 260 && hz > 52 && hz < 54)
|
|
|
|
vmax = 285;
|
|
|
|
if (height > 250 && height < 260 && hz < 52)
|
|
|
|
vmax = 313;
|
|
|
|
if (height > 260 && height < 300)
|
2018-09-08 22:11:31 +01:00
|
|
|
vmax = 318;
|
2018-05-25 10:56:22 +01:00
|
|
|
|
|
|
|
if (height > 400 && hz > 56)
|
2018-09-08 22:11:31 +01:00
|
|
|
vmax = 533;
|
2018-05-25 10:56:22 +01:00
|
|
|
if (height > 520 && hz < 57)
|
|
|
|
vmax = 580;
|
|
|
|
|
|
|
|
if (height > 300 && hz < 56)
|
2018-09-08 22:11:31 +01:00
|
|
|
vmax = 615;
|
|
|
|
if (height > 500 && hz < 56)
|
|
|
|
vmax = 624;
|
2018-10-02 16:56:28 -04:00
|
|
|
if (height > 300)
|
|
|
|
pdefault = pdefault * 2;
|
2018-05-25 10:56:22 +01:00
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
vfp = height + ((vmax - height) / 2) - pdefault;
|
2018-09-08 22:11:31 +01:00
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
if (height < 300)
|
2018-11-24 19:38:01 +01:00
|
|
|
vsp = vfp + 3; /* needs to be 3 for progressive */
|
2018-09-08 22:11:31 +01:00
|
|
|
if (height > 300)
|
2018-11-24 19:38:01 +01:00
|
|
|
vsp = vfp + 6; /* needs to be 6 for interlaced */
|
2018-10-02 16:56:28 -04:00
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
vbp = vmax;
|
|
|
|
|
|
|
|
if (height < 300)
|
2018-10-02 16:56:28 -04:00
|
|
|
pixel_clock = (hmax * vmax * hz) / 1000000;
|
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
if (height > 300)
|
2018-10-02 16:56:28 -04:00
|
|
|
pixel_clock = ((hmax * vmax * hz) / 1000000) / 2;
|
2018-11-24 19:38:01 +01:00
|
|
|
/* above code is the modeline generator */
|
2018-05-25 10:56:22 +01:00
|
|
|
|
2018-09-10 18:12:44 +01:00
|
|
|
/* create interlaced newmode from modline variables */
|
|
|
|
if (height < 300)
|
|
|
|
{
|
2019-02-01 00:22:00 +00:00
|
|
|
snprintf(xrandr, sizeof(xrandr), "xrandr --newmode \"CRT_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d -hsync -vsync", width, height, hz, pixel_clock,
|
2018-11-24 19:38:01 +01:00
|
|
|
width, hfp, hsp, hbp, height, vfp, vsp, vbp);
|
2018-09-10 18:12:44 +01:00
|
|
|
system(xrandr);
|
2018-05-25 10:56:22 +01:00
|
|
|
}
|
|
|
|
/* create interlaced newmode from modline variables */
|
|
|
|
if (height > 300)
|
2018-10-02 16:56:28 -04:00
|
|
|
{
|
2019-02-01 00:22:00 +00:00
|
|
|
snprintf(xrandr, sizeof(xrandr), "xrandr --newmode \"CRT_%dx%d_%0.2f\" %f %d %d %d %d %d %d %d %d interlace -hsync -vsync", width, height, hz, pixel_clock,
|
2018-11-24 19:38:01 +01:00
|
|
|
width, hfp, hsp, hbp, height, vfp, vsp, vbp);
|
2018-05-25 10:56:22 +01:00
|
|
|
system(xrandr);
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:56:28 -04:00
|
|
|
/* variable for new mode */
|
2019-02-01 00:22:00 +00:00
|
|
|
snprintf(new_mode, sizeof(new_mode), "CRT_%dx%d_%0.2f", width, height, hz);
|
2018-10-02 16:56:28 -04:00
|
|
|
|
2019-02-03 15:49:35 -08:00
|
|
|
/* need to run loops for DVI0 - DVI-2 and VGA0 - VGA-2 outputs to
|
2018-11-24 19:38:01 +01:00
|
|
|
* add and delete modes */
|
2019-01-30 21:55:33 +00:00
|
|
|
crt_rrmode.id = crtid;
|
|
|
|
crt_rrmode.width = width;
|
|
|
|
crt_rrmode.height = height;
|
|
|
|
crt_rrmode.dotClock = pixel_clock;
|
|
|
|
crt_rrmode.hSyncStart = hfp;
|
|
|
|
crt_rrmode.hSyncEnd = hsp;
|
|
|
|
crt_rrmode.hTotal = hmax;
|
|
|
|
crt_rrmode.hSkew = 0;
|
|
|
|
crt_rrmode.vSyncStart = vfp;
|
|
|
|
crt_rrmode.vSyncEnd = vsp;
|
|
|
|
crt_rrmode.vTotal = vmax;
|
|
|
|
crt_rrmode.name = new_mode;
|
|
|
|
crt_rrmode.nameLength = sizeof(new_mode);
|
|
|
|
crt_rrmode.modeFlags = 0;
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
res = XRRGetScreenResources (dsp, window);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
if (monitor_index == 0)
|
2018-10-02 16:56:28 -04:00
|
|
|
{
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
for (int i = 0; i < res->noutput; i++)
|
2019-02-03 15:49:35 -08:00
|
|
|
{
|
2019-02-01 20:21:53 +00:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
XRROutputInfo *outputs = XRRGetOutputInfo (dsp, res, res->outputs[i]);
|
2019-02-01 20:38:50 +00:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
if (outputs->connection == RR_Connected)
|
|
|
|
{
|
2019-02-01 20:38:50 +00:00
|
|
|
snprintf(orig_output, sizeof(orig_output),"%s", outputs->name);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
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);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (monitor_index > 0)
|
|
|
|
{
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
XRROutputInfo *outputs = XRRGetOutputInfo (dsp, res, res->outputs[monitor_index]);
|
|
|
|
if (outputs->connection == RR_Connected)
|
|
|
|
{
|
|
|
|
snprintf(orig_output, sizeof(orig_output),"%s", outputs->name);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
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);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
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);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2019-01-30 21:55:33 +00:00
|
|
|
}
|
2019-01-30 22:48:04 +00:00
|
|
|
}
|
2018-10-02 16:56:28 -04:00
|
|
|
return true;
|
2018-05-25 10:56:22 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 17:06:55 +01:00
|
|
|
const char *x11_display_server_get_output_options(void *data)
|
2018-09-17 03:40:17 +02:00
|
|
|
{
|
2018-11-27 11:28:24 +01:00
|
|
|
#ifdef HAVE_XRANDR
|
|
|
|
Display *dpy;
|
|
|
|
XRRScreenResources *res;
|
|
|
|
XRROutputInfo *info;
|
|
|
|
Window root;
|
|
|
|
int i;
|
|
|
|
static char s[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
if (!(dpy = XOpenDisplay(0)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
root = RootWindow(dpy, DefaultScreen(dpy));
|
|
|
|
|
|
|
|
if (!(res = XRRGetScreenResources(dpy, root)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < res->noutput; i++)
|
|
|
|
{
|
|
|
|
if (!(info = XRRGetOutputInfo(dpy, res, res->outputs[i])))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
strlcat(s, info->name, sizeof(s));
|
|
|
|
if ((i+1) < res->noutput)
|
|
|
|
strlcat(s, "|", sizeof(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
#else
|
2018-09-17 03:40:17 +02:00
|
|
|
/* TODO/FIXME - hardcoded for now; list should be built up dynamically later */
|
|
|
|
return "HDMI-0|HDMI-1|HDMI-2|HDMI-3|DVI-0|DVI-1|DVI-2|DVI-3|VGA-0|VGA-1|VGA-2|VGA-3|Config";
|
2018-11-27 11:28:24 +01:00
|
|
|
#endif
|
2018-09-17 03:40:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 10:56:22 +01:00
|
|
|
const video_display_server_t dispserv_x11 = {
|
|
|
|
x11_display_server_init,
|
|
|
|
x11_display_server_destroy,
|
2018-09-17 03:40:17 +02:00
|
|
|
x11_display_server_set_window_opacity,
|
2018-11-24 02:47:22 +01:00
|
|
|
NULL, /* set_window_progress */
|
2018-09-17 03:40:17 +02:00
|
|
|
x11_display_server_set_window_decorations,
|
|
|
|
x11_display_server_set_resolution,
|
2018-11-24 02:47:22 +01:00
|
|
|
NULL, /* get_resolution_list */
|
2018-09-17 03:40:17 +02:00
|
|
|
x11_display_server_get_output_options,
|
2018-05-25 10:56:22 +01:00
|
|
|
"x11"
|
|
|
|
};
|