diff --git a/gfx/display_servers/dispserv_android.c b/gfx/display_servers/dispserv_android.c index c36bfd8f14..cfdf70c8b1 100644 --- a/gfx/display_servers/dispserv_android.c +++ b/gfx/display_servers/dispserv_android.c @@ -66,5 +66,6 @@ const video_display_server_t dispserv_android = { NULL, /* get_resolution_list */ NULL, /* get_output_options */ android_display_server_set_screen_orientation, + NULL, /* get_screen_orientation */ "android" }; diff --git a/gfx/display_servers/dispserv_null.c b/gfx/display_servers/dispserv_null.c index d22d477e2e..e4d3e38a09 100644 --- a/gfx/display_servers/dispserv_null.c +++ b/gfx/display_servers/dispserv_null.c @@ -53,5 +53,6 @@ const video_display_server_t dispserv_null = { NULL, /* get_resolution_list */ NULL, /* get_output_options */ NULL, /* set_screen_orientation */ + NULL, /* get_screen_orientation */ "null" }; diff --git a/gfx/display_servers/dispserv_win32.c b/gfx/display_servers/dispserv_win32.c index 545ca7bbdb..d5845d20e5 100644 --- a/gfx/display_servers/dispserv_win32.c +++ b/gfx/display_servers/dispserv_win32.c @@ -351,6 +351,33 @@ void *win32_display_server_get_resolution_list(void *data, } #if _WIN32_WINNT >= 0x0500 +enum rotation win32_display_server_get_screen_orientation(void) +{ + DEVMODE dm = {0}; + enum rotation rotation; + + win32_get_video_output(&dm, -1, sizeof(dm)); + + switch (dm.dmDisplayOrientation) + { + case DMDO_DEFAULT: + default: + rotation = ORIENTATION_NORMAL; + break; + case DMDO_90: + rotation = ORIENTATION_FLIPPED_ROTATED; + break; + case DMDO_180: + rotation = ORIENTATION_FLIPPED; + break; + case DMDO_270: + rotation = ORIENTATION_VERTICAL; + break; + } + + return rotation; +} + void win32_display_server_set_screen_orientation(enum rotation rotation) { DEVMODE dm = {0}; @@ -433,8 +460,10 @@ const video_display_server_t dispserv_win32 = { NULL, /* get_output_options */ #if _WIN32_WINNT >= 0x0500 win32_display_server_set_screen_orientation, + win32_display_server_get_screen_orientation, #else NULL, + NULL, #endif "win32" }; diff --git a/gfx/display_servers/dispserv_x11.c b/gfx/display_servers/dispserv_x11.c index 15701579a6..5e61d53c48 100644 --- a/gfx/display_servers/dispserv_x11.c +++ b/gfx/display_servers/dispserv_x11.c @@ -362,19 +362,20 @@ const char *x11_display_server_get_output_options(void *data) #ifdef HAVE_XRANDR static void x11_display_server_set_screen_orientation(enum rotation rotation) { - int i, j, num_sizes = 0; + int i, j; XRRScreenResources *screen; - XRRScreenConfiguration *config = XRRGetScreenInfo(g_x11_dpy, DefaultRootWindow(g_x11_dpy)); - XRRScreenSize *sizes = XRRConfigSizes(config, &num_sizes); - double dpi = (25.4 * DisplayHeight(g_x11_dpy, DefaultScreen(g_x11_dpy))) / DisplayHeightMM(g_x11_dpy, DefaultScreen(g_x11_dpy)); + /* switched to using XOpenDisplay() due to deinit order issue with g_x11_dpy when restoring original rotation on exit */ + Display *dpy = XOpenDisplay(0); + XRRScreenConfiguration *config = XRRGetScreenInfo(dpy, DefaultRootWindow(dpy)); + double dpi = (25.4 * DisplayHeight(dpy, DefaultScreen(dpy))) / DisplayHeightMM(dpy, DefaultScreen(dpy)); - XGrabServer(g_x11_dpy); + XGrabServer(dpy); - screen = XRRGetScreenResources(g_x11_dpy, DefaultRootWindow(g_x11_dpy)); + screen = XRRGetScreenResources(dpy, DefaultRootWindow(dpy)); for (i = 0; i < screen->noutput; i++) { - XRROutputInfo *info = XRRGetOutputInfo(g_x11_dpy, screen, screen->outputs[i]); + XRROutputInfo *info = XRRGetOutputInfo(dpy, screen, screen->outputs[i]); if (info->connection != RR_Connected) { @@ -384,7 +385,7 @@ static void x11_display_server_set_screen_orientation(enum rotation rotation) for (j = 0; j < info->ncrtc; j++) { - XRRCrtcInfo *crtc = XRRGetCrtcInfo(g_x11_dpy, screen, screen->crtcs[j]); + XRRCrtcInfo *crtc = XRRGetCrtcInfo(dpy, screen, screen->crtcs[j]); Rotation new_rotation = RR_Rotate_0; if (crtc->width == 0 || crtc->height == 0) @@ -414,7 +415,7 @@ static void x11_display_server_set_screen_orientation(enum rotation rotation) break; } - XRRSetCrtcConfig(g_x11_dpy, screen, screen->crtcs[j], CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); + XRRSetCrtcConfig(dpy, screen, screen->crtcs[j], CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); if ((crtc->rotation & RR_Rotate_0 || crtc->rotation & RR_Rotate_180) && (rotation == ORIENTATION_VERTICAL || rotation == ORIENTATION_FLIPPED_ROTATED)) { @@ -431,9 +432,9 @@ static void x11_display_server_set_screen_orientation(enum rotation rotation) crtc->rotation = new_rotation; - XRRSetScreenSize(g_x11_dpy, DefaultRootWindow(g_x11_dpy), crtc->width, crtc->height, (25.4 * crtc->width) / dpi, (25.4 * crtc->height) / dpi); + XRRSetScreenSize(dpy, DefaultRootWindow(dpy), crtc->width, crtc->height, (25.4 * crtc->width) / dpi, (25.4 * crtc->height) / dpi); - XRRSetCrtcConfig(g_x11_dpy, screen, screen->crtcs[j], CurrentTime, crtc->x, crtc->y, crtc->mode, crtc->rotation, crtc->outputs, crtc->noutput); + XRRSetCrtcConfig(dpy, screen, screen->crtcs[j], CurrentTime, crtc->x, crtc->y, crtc->mode, crtc->rotation, crtc->outputs, crtc->noutput); XRRFreeCrtcInfo(crtc); } @@ -443,9 +444,68 @@ static void x11_display_server_set_screen_orientation(enum rotation rotation) XRRFreeScreenResources(screen); - XUngrabServer(g_x11_dpy); - XSync(g_x11_dpy, False); + XUngrabServer(dpy); + XSync(dpy, False); XRRFreeScreenConfigInfo(config); + XCloseDisplay(dpy); +} +#endif + +#ifdef HAVE_XRANDR +static enum rotation x11_display_server_get_screen_orientation(void) +{ + int i, j; + XRRScreenResources *screen = XRRGetScreenResources(g_x11_dpy, DefaultRootWindow(g_x11_dpy)); + XRRScreenConfiguration *config = XRRGetScreenInfo(g_x11_dpy, DefaultRootWindow(g_x11_dpy)); + enum rotation rotation = ORIENTATION_NORMAL; + + for (i = 0; i < screen->noutput; i++) + { + XRROutputInfo *info = XRRGetOutputInfo(g_x11_dpy, screen, screen->outputs[i]); + + if (info->connection != RR_Connected) + { + XRRFreeOutputInfo(info); + continue; + } + + for (j = 0; j < info->ncrtc; j++) + { + XRRCrtcInfo *crtc = XRRGetCrtcInfo(g_x11_dpy, screen, screen->crtcs[j]); + + if (crtc->width == 0 || crtc->height == 0) + { + XRRFreeCrtcInfo(crtc); + continue; + } + + switch (crtc->rotation) + { + case RR_Rotate_0: + default: + rotation = ORIENTATION_NORMAL; + break; + case RR_Rotate_270: + rotation = ORIENTATION_VERTICAL; + break; + case RR_Rotate_180: + rotation = ORIENTATION_FLIPPED; + break; + case RR_Rotate_90: + rotation = ORIENTATION_FLIPPED_ROTATED; + break; + } + + XRRFreeCrtcInfo(crtc); + } + + XRRFreeOutputInfo(info); + } + + XRRFreeScreenResources(screen); + XRRFreeScreenConfigInfo(config); + + return rotation; } #endif @@ -460,8 +520,10 @@ const video_display_server_t dispserv_x11 = { x11_display_server_get_output_options, #ifdef HAVE_XRANDR x11_display_server_set_screen_orientation, + x11_display_server_get_screen_orientation, #else NULL, + NULL, #endif "x11" }; diff --git a/gfx/video_display_server.c b/gfx/video_display_server.c index f48e615b05..f85faa41ce 100644 --- a/gfx/video_display_server.c +++ b/gfx/video_display_server.c @@ -22,6 +22,8 @@ static const video_display_server_t *current_display_server = &dispserv_null; static void *current_display_server_data = NULL; +static enum rotation initial_screen_orientation = ORIENTATION_NORMAL; +static enum rotation current_screen_orientation = ORIENTATION_NORMAL; const char *video_display_server_get_ident(void) { @@ -62,11 +64,16 @@ void* video_display_server_init(void) RARCH_LOG("[Video]: Found display server: %s\n", current_display_server->ident); + initial_screen_orientation = video_display_server_get_screen_orientation(); + return current_display_server_data; } void video_display_server_destroy(void) { + if (initial_screen_orientation != current_screen_orientation) + video_display_server_set_screen_orientation(initial_screen_orientation); + if (current_display_server && current_display_server->destroy) if (current_display_server_data) current_display_server->destroy(current_display_server_data); @@ -120,6 +127,7 @@ void video_display_server_set_screen_orientation(enum rotation rotation) if (current_display_server && current_display_server->set_screen_orientation) { RARCH_LOG("[Video]: Setting screen orientation to %d.\n", rotation); + current_screen_orientation = rotation; current_display_server->set_screen_orientation(rotation); } } @@ -130,3 +138,10 @@ bool video_display_server_can_set_screen_orientation(void) return true; return false; } + +enum rotation video_display_server_get_screen_orientation(void) +{ + if (current_display_server && current_display_server->get_screen_orientation) + return current_display_server->get_screen_orientation(); + return ORIENTATION_NORMAL; +} diff --git a/gfx/video_display_server.h b/gfx/video_display_server.h index 08846c631f..3a58ef3506 100644 --- a/gfx/video_display_server.h +++ b/gfx/video_display_server.h @@ -48,6 +48,7 @@ typedef struct video_display_server unsigned *size); const char *(*get_output_options)(void *data); void (*set_screen_orientation)(enum rotation rotation); + enum rotation (*get_screen_orientation)(void); const char *ident; } video_display_server_t; @@ -75,6 +76,8 @@ void video_display_server_set_screen_orientation(enum rotation rotation); bool video_display_server_can_set_screen_orientation(void); +enum rotation video_display_server_get_screen_orientation(void); + extern const video_display_server_t dispserv_win32; extern const video_display_server_t dispserv_x11; extern const video_display_server_t dispserv_android; diff --git a/retroarch.c b/retroarch.c index 9b846922d9..0a39cdf98f 100644 --- a/retroarch.c +++ b/retroarch.c @@ -2502,7 +2502,6 @@ void retroarch_fail(int error_code, const char *error) bool retroarch_main_quit(void) { - #ifdef HAVE_DISCORD if (discord_is_inited) { diff --git a/ui/ui_companion_driver.c b/ui/ui_companion_driver.c index 204db1ebc8..e0c4494442 100644 --- a/ui/ui_companion_driver.c +++ b/ui/ui_companion_driver.c @@ -295,7 +295,7 @@ void ui_companion_driver_log_msg(const char *msg) #ifdef HAVE_QT settings_t *settings = config_get_ptr(); - if (settings->bools.desktop_menu_enable) + if (settings && settings->bools.desktop_menu_enable) if (ui_companion_qt_data && qt_is_inited) ui_companion_qt.log_msg(ui_companion_qt_data, msg); #endif