RetroArch/gfx/common/x11_common.c

522 lines
12 KiB
C
Raw Normal View History

2012-09-26 13:52:25 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 17:06:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2012-09-26 13:52:25 +00:00
*
* 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/>.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
2015-09-16 09:24:03 +00:00
#include <sys/types.h>
#include <sys/wait.h>
2015-09-05 18:34:22 +00:00
#include <X11/Xatom.h>
#include "x11_common.h"
2012-09-26 13:52:25 +00:00
#include "../../general.h"
static Atom XA_NET_WM_STATE;
static Atom XA_NET_WM_STATE_FULLSCREEN;
static Atom XA_NET_MOVERESIZE_WINDOW;
2015-11-19 08:51:20 +00:00
Atom g_x11_quit_atom;
2015-11-19 10:08:38 +00:00
static volatile sig_atomic_t g_x11_quit;
2015-11-19 08:51:20 +00:00
bool g_x11_has_focus;
Window g_x11_win;
XIC g_x11_xic;
Display *g_x11_dpy;
2015-11-19 09:13:09 +00:00
bool g_x11_true_full;
#define XA_INIT(x) XA##x = XInternAtom(dpy, #x, False)
#define _NET_WM_STATE_ADD 1
#define MOVERESIZE_GRAVITY_CENTER 5
#define MOVERESIZE_X_SHIFT 8
#define MOVERESIZE_Y_SHIFT 9
2013-03-29 12:46:11 +00:00
static void x11_hide_mouse(Display *dpy, Window win)
2012-09-26 13:52:25 +00:00
{
2015-01-10 02:30:23 +00:00
static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
2012-09-26 13:52:25 +00:00
Cursor no_ptr;
Pixmap bm_no;
XColor black, dummy;
2015-01-10 02:30:23 +00:00
Colormap colormap = DefaultColormap(dpy, DefaultScreen(dpy));
2015-06-12 23:18:13 +00:00
2012-09-26 13:52:25 +00:00
if (!XAllocNamedColor(dpy, colormap, "black", &black, &dummy))
return;
bm_no = XCreateBitmapFromData(dpy, win, bm_no_data, 8, 8);
no_ptr = XCreatePixmapCursor(dpy, bm_no, bm_no, &black, &black, 0, 0);
XDefineCursor(dpy, win, no_ptr);
XFreeCursor(dpy, no_ptr);
if (bm_no != None)
XFreePixmap(dpy, bm_no);
XFreeColors(dpy, colormap, &black.pixel, 1, 0);
}
2013-03-29 12:46:11 +00:00
void x11_show_mouse(Display *dpy, Window win, bool state)
{
if (state)
XUndefineCursor(dpy, win);
else
x11_hide_mouse(dpy, win);
}
2012-09-26 13:52:25 +00:00
void x11_windowed_fullscreen(Display *dpy, Window win)
{
2015-06-26 15:46:13 +00:00
XEvent xev = {0};
2012-09-26 13:52:25 +00:00
XA_INIT(_NET_WM_STATE);
XA_INIT(_NET_WM_STATE_FULLSCREEN);
xev.xclient.type = ClientMessage;
xev.xclient.send_event = True;
xev.xclient.message_type = XA_NET_WM_STATE;
xev.xclient.window = win;
xev.xclient.format = 32;
xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
2012-10-12 17:05:29 +00:00
XSendEvent(dpy, DefaultRootWindow(dpy), False,
SubstructureRedirectMask | SubstructureNotifyMask,
&xev);
}
2015-01-12 04:05:56 +00:00
/* Try to be nice to tiling WMs if possible. */
2012-10-12 17:05:29 +00:00
void x11_move_window(Display *dpy, Window win, int x, int y,
unsigned width, unsigned height)
{
XEvent xev = {0};
2015-01-10 02:30:23 +00:00
XA_INIT(_NET_MOVERESIZE_WINDOW);
2012-10-12 17:05:29 +00:00
xev.xclient.type = ClientMessage;
xev.xclient.send_event = True;
xev.xclient.message_type = XA_NET_MOVERESIZE_WINDOW;
xev.xclient.window = win;
xev.xclient.format = 32;
xev.xclient.data.l[0] = (1 << MOVERESIZE_X_SHIFT) | (1 << MOVERESIZE_Y_SHIFT);
xev.xclient.data.l[1] = x;
xev.xclient.data.l[2] = y;
2012-09-26 13:52:25 +00:00
XSendEvent(dpy, DefaultRootWindow(dpy), False,
SubstructureRedirectMask | SubstructureNotifyMask,
&xev);
}
2012-10-26 21:01:32 +00:00
static void x11_set_window_class(Display *dpy, Window win)
{
XClassHint hint = {0};
2015-01-10 02:30:23 +00:00
2015-01-12 04:05:56 +00:00
hint.res_name = (char*)"retroarch"; /* Broken header. */
hint.res_class = (char*)"retroarch";
2012-10-26 21:01:32 +00:00
XSetClassHint(dpy, win, &hint);
}
void x11_set_window_attr(Display *dpy, Window win)
{
x11_set_window_class(dpy, win);
}
2012-09-26 13:52:25 +00:00
void x11_suspend_screensaver(Window wnd)
{
2015-01-10 02:30:23 +00:00
int ret;
2015-06-12 23:18:13 +00:00
char cmd[64] = {0};
2015-01-10 02:30:23 +00:00
2015-01-18 21:32:14 +00:00
RARCH_LOG("Suspending screensaver (X11).\n");
2012-09-26 13:52:25 +00:00
snprintf(cmd, sizeof(cmd), "xdg-screensaver suspend %d", (int)wnd);
2015-01-10 02:30:23 +00:00
ret = system(cmd);
if (ret == -1)
RARCH_WARN("Failed to launch xdg-screensaver.\n");
else if (WEXITSTATUS(ret))
2012-09-26 13:52:25 +00:00
RARCH_WARN("Could not suspend screen saver.\n");
}
static bool get_video_mode(Display *dpy, unsigned width, unsigned height,
XF86VidModeModeInfo *mode, XF86VidModeModeInfo *desktop_mode)
2012-09-29 08:47:55 +00:00
{
2015-01-10 02:30:23 +00:00
float refresh_mod;
int i, num_modes = 0;
bool ret = false;
float minimum_fps_diff = 0.0f;
2013-10-22 19:26:33 +00:00
XF86VidModeModeInfo **modes = NULL;
2015-03-20 21:08:36 +00:00
settings_t *settings = config_get_ptr();
2015-01-10 02:30:23 +00:00
2012-09-29 08:47:55 +00:00
XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy), &num_modes, &modes);
if (!num_modes)
{
XFree(modes);
return false;
}
*desktop_mode = *modes[0];
/* If we use black frame insertion, we fake a 60 Hz monitor
* for 120 Hz one, etc, so try to match that. */
2015-03-20 21:08:36 +00:00
refresh_mod = settings->video.black_frame_insertion ? 0.5f : 1.0f;
2013-10-22 19:26:33 +00:00
for (i = 0; i < num_modes; i++)
2012-09-29 08:47:55 +00:00
{
2015-01-18 20:49:00 +00:00
float refresh, diff;
const XF86VidModeModeInfo *m = modes[i];
2015-01-10 02:30:23 +00:00
if (!m)
continue;
2015-01-18 20:49:00 +00:00
if (m->hdisplay != width)
continue;
if (m->vdisplay != height)
continue;
refresh = refresh_mod * m->dotclock * 1000.0f / (m->htotal * m->vtotal);
2015-03-20 21:08:36 +00:00
diff = fabsf(refresh - settings->video.refresh_rate);
2015-01-18 20:49:00 +00:00
if (!ret || diff < minimum_fps_diff)
2012-09-29 08:47:55 +00:00
{
2015-01-18 20:49:00 +00:00
*mode = *m;
minimum_fps_diff = diff;
2012-09-29 08:47:55 +00:00
}
2015-01-18 20:49:00 +00:00
ret = true;
2012-09-29 08:47:55 +00:00
}
XFree(modes);
return ret;
}
bool x11_enter_fullscreen(Display *dpy, unsigned width,
unsigned height, XF86VidModeModeInfo *desktop_mode)
2012-09-29 08:47:55 +00:00
{
XF86VidModeModeInfo mode;
2014-10-27 13:35:23 +00:00
2015-01-18 20:49:00 +00:00
if (!get_video_mode(dpy, width, height, &mode, desktop_mode))
return false;
2014-10-27 13:35:23 +00:00
2015-01-18 20:49:00 +00:00
if (!XF86VidModeSwitchToMode(dpy, DefaultScreen(dpy), &mode))
return false;
XF86VidModeSetViewPort(dpy, DefaultScreen(dpy), 0, 0);
return true;
2012-09-29 08:47:55 +00:00
}
void x11_exit_fullscreen(Display *dpy, XF86VidModeModeInfo *desktop_mode)
{
XF86VidModeSwitchToMode(dpy, DefaultScreen(dpy), desktop_mode);
XF86VidModeSetViewPort(dpy, DefaultScreen(dpy), 0, 0);
}
2012-10-12 17:05:29 +00:00
#ifdef HAVE_XINERAMA
static XineramaScreenInfo *x11_query_screens(Display *dpy, int *num_screens)
{
int major, minor;
2015-01-10 02:30:23 +00:00
2012-10-12 17:05:29 +00:00
if (!XineramaQueryExtension(dpy, &major, &minor))
2012-10-21 21:24:25 +00:00
return NULL;
2012-10-12 17:05:29 +00:00
XineramaQueryVersion(dpy, &major, &minor);
RARCH_LOG("[X11]: Xinerama version: %d.%d.\n", major, minor);
if (!XineramaIsActive(dpy))
2012-10-21 21:24:25 +00:00
return NULL;
2012-10-12 17:05:29 +00:00
return XineramaQueryScreens(dpy, num_screens);
}
bool x11_get_xinerama_coord(Display *dpy, int screen,
int *x, int *y, unsigned *w, unsigned *h)
{
2015-01-10 02:30:23 +00:00
int i, num_screens = 0;
2012-10-12 17:05:29 +00:00
bool ret = false;
XineramaScreenInfo *info = x11_query_screens(dpy, &num_screens);
RARCH_LOG("[X11]: Xinerama screens: %d.\n", num_screens);
2013-10-22 19:26:33 +00:00
for (i = 0; i < num_screens; i++)
2012-10-12 17:05:29 +00:00
{
2015-01-18 20:49:00 +00:00
if (info[i].screen_number != screen)
continue;
*x = info[i].x_org;
*y = info[i].y_org;
*w = info[i].width;
*h = info[i].height;
ret = true;
break;
2012-10-12 17:05:29 +00:00
}
XFree(info);
return ret;
}
unsigned x11_get_xinerama_monitor(Display *dpy, int x, int y,
int w, int h)
{
2015-01-10 02:30:23 +00:00
int i, num_screens = 0;
unsigned monitor = 0;
int largest_area = 0;
2012-10-12 17:05:29 +00:00
XineramaScreenInfo *info = x11_query_screens(dpy, &num_screens);
RARCH_LOG("[X11]: Xinerama screens: %d.\n", num_screens);
2013-10-22 19:26:33 +00:00
for (i = 0; i < num_screens; i++)
2012-10-12 17:05:29 +00:00
{
2015-01-10 02:30:23 +00:00
int area;
2012-10-12 17:05:29 +00:00
int max_lx = max(x, info[i].x_org);
int min_rx = min(x + w, info[i].x_org + info[i].width);
int max_ty = max(y, info[i].y_org);
int min_by = min(y + h, info[i].y_org + info[i].height);
int len_x = min_rx - max_lx;
int len_y = min_by - max_ty;
2015-01-12 04:05:56 +00:00
2015-01-18 20:49:00 +00:00
/* The whole window is outside the screen. */
if (len_x < 0 || len_y < 0)
2012-10-12 17:05:29 +00:00
continue;
2015-01-10 02:30:23 +00:00
area = len_x * len_y;
2015-01-12 04:05:56 +00:00
2012-10-12 17:05:29 +00:00
if (area > largest_area)
{
monitor = i;
largest_area = area;
}
}
XFree(info);
return monitor;
}
#endif
bool x11_create_input_context(Display *dpy, Window win, XIM *xim, XIC *xic)
{
*xim = XOpenIM(dpy, NULL, NULL, NULL);
2015-01-12 04:05:56 +00:00
if (!*xim)
{
RARCH_ERR("[X11]: Failed to open input method.\n");
return false;
}
*xic = XCreateIC(*xim, XNInputStyle,
XIMPreeditNothing | XIMStatusNothing, XNClientWindow, win, NULL);
2015-01-12 04:05:56 +00:00
if (!*xic)
{
RARCH_ERR("[X11]: Failed to create input context.\n");
return false;
}
XSetICFocus(*xic);
return true;
}
void x11_destroy_input_context(XIM *xim, XIC *xic)
{
if (*xic)
{
XDestroyIC(*xic);
*xic = NULL;
}
if (*xim)
{
XCloseIM(*xim);
*xim = NULL;
}
}
2015-04-09 03:05:29 +00:00
bool x11_get_metrics(void *data,
enum display_metric_types type, float *value)
{
int pixels_x, pixels_y, physical_width, physical_height;
unsigned screen_no = 0;
Display *dpy = (Display*)XOpenDisplay(NULL);
pixels_x = DisplayWidth(dpy, screen_no);
pixels_y = DisplayHeight(dpy, screen_no);
physical_width = DisplayWidthMM(dpy, screen_no);
physical_height = DisplayHeightMM(dpy, screen_no);
(void)pixels_y;
XCloseDisplay(dpy);
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
*value = (float)physical_width;
break;
case DISPLAY_METRIC_MM_HEIGHT:
*value = (float)physical_height;
break;
case DISPLAY_METRIC_DPI:
*value = ((((float)pixels_x) * 25.4) / ((float)physical_width));
break;
case DISPLAY_METRIC_NONE:
default:
*value = 0;
return false;
}
return true;
}
2015-11-19 09:02:53 +00:00
void x_input_poll_wheel(void *data, XButtonEvent *event, bool latch);
bool x11_alive(void *data)
2015-11-19 09:02:53 +00:00
{
XEvent event;
driver_t *driver = driver_get_ptr();
2015-11-19 09:02:53 +00:00
while (XPending(g_x11_dpy))
{
bool filter;
/* Can get events from older windows. Check this. */
XNextEvent(g_x11_dpy, &event);
filter = XFilterEvent(&event, g_x11_win);
switch (event.type)
{
case ClientMessage:
2015-11-19 09:05:54 +00:00
if (event.xclient.window == g_x11_win &&
(Atom)event.xclient.data.l[0] == g_x11_quit_atom)
2015-11-19 09:02:53 +00:00
g_x11_quit = true;
break;
case DestroyNotify:
if (event.xdestroywindow.window == g_x11_win)
g_x11_quit = true;
break;
case MapNotify:
if (event.xmap.window == g_x11_win)
g_x11_has_focus = true;
break;
case UnmapNotify:
if (event.xunmap.window == g_x11_win)
g_x11_has_focus = false;
break;
case ButtonPress:
x_input_poll_wheel(driver->input_data, &event.xbutton, true);
break;
case ButtonRelease:
break;
case KeyPress:
case KeyRelease:
if (event.xkey.window == g_x11_win)
x11_handle_key_event(&event, g_x11_xic, filter);
break;
}
}
return !g_x11_quit;
}
void x11_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
unsigned new_width = *width;
unsigned new_height = *height;
x11_get_video_size(data, &new_width, &new_height);
if (new_width != *width || new_height != *height)
{
*resize = true;
*width = new_width;
*height = new_height;
}
x11_alive(data);
2015-11-19 09:02:53 +00:00
*quit = g_x11_quit;
}
2015-11-19 09:09:19 +00:00
void x11_get_video_size(void *data, unsigned *width, unsigned *height)
2015-11-19 09:09:19 +00:00
{
if (!g_x11_dpy || g_x11_win == None)
{
Display *dpy = (Display*)XOpenDisplay(NULL);
*width = 0;
*height = 0;
if (dpy)
{
int screen = DefaultScreen(dpy);
*width = DisplayWidth(dpy, screen);
*height = DisplayHeight(dpy, screen);
XCloseDisplay(dpy);
}
}
else
{
XWindowAttributes target;
XGetWindowAttributes(g_x11_dpy, g_x11_win, &target);
*width = target.width;
*height = target.height;
}
}
2015-11-19 09:13:09 +00:00
2015-11-19 09:25:07 +00:00
bool x11_has_focus(void *data)
2015-11-19 09:13:09 +00:00
{
Window win;
int rev;
XGetInputFocus(g_x11_dpy, &win, &rev);
return (win == g_x11_win && g_x11_has_focus) || g_x11_true_full;
}
2015-11-19 10:04:17 +00:00
static void x11_sighandler(int sig)
{
(void)sig;
g_x11_quit = 1;
}
void x11_install_sighandlers(void)
{
struct sigaction sa = {{0}};
sa.sa_handler = x11_sighandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
2015-11-19 10:07:52 +00:00
bool x11_connect(void)
{
g_x11_quit = 0;
/* Keep one g_x11_dpy alive the entire process lifetime.
* This is necessary for nVidia's EGL implementation for now. */
if (!g_x11_dpy)
{
g_x11_dpy = XOpenDisplay(NULL);
if (!g_x11_dpy)
return false;
}
return true;
}