mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2025-07-01 23:35:55 +00:00
Cache results of WMUtils::isRunningX11() and WMUtils::isRunningWayland()
This commit is contained in:
parent
6dab251055
commit
395eb5581c
47
app/wm.cpp
47
app/wm.cpp
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
#ifdef HAS_X11
|
#ifdef HAS_X11
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#endif
|
#endif
|
||||||
@ -10,14 +12,30 @@
|
|||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define VALUE_SET 0x01
|
||||||
|
#define VALUE_TRUE 0x02
|
||||||
|
|
||||||
bool WMUtils::isRunningX11()
|
bool WMUtils::isRunningX11()
|
||||||
{
|
{
|
||||||
#ifdef HAS_X11
|
#ifdef HAS_X11
|
||||||
Display* display = XOpenDisplay(nullptr);
|
static SDL_atomic_t isRunningOnX11;
|
||||||
if (display != nullptr) {
|
|
||||||
XCloseDisplay(display);
|
// If the value is not set yet, populate it now.
|
||||||
return true;
|
int val = SDL_AtomicGet(&isRunningOnX11);
|
||||||
|
if (!(val & VALUE_SET)) {
|
||||||
|
Display* display = XOpenDisplay(nullptr);
|
||||||
|
if (display != nullptr) {
|
||||||
|
XCloseDisplay(display);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate the value to return and have for next time.
|
||||||
|
// This can race with another thread populating the same data,
|
||||||
|
// but that's no big deal.
|
||||||
|
val = VALUE_SET | ((display != nullptr) ? VALUE_TRUE : 0);
|
||||||
|
SDL_AtomicSet(&isRunningOnX11, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return !!(val & VALUE_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -26,11 +44,24 @@ bool WMUtils::isRunningX11()
|
|||||||
bool WMUtils::isRunningWayland()
|
bool WMUtils::isRunningWayland()
|
||||||
{
|
{
|
||||||
#ifdef HAS_WAYLAND
|
#ifdef HAS_WAYLAND
|
||||||
struct wl_display* display = wl_display_connect(nullptr);
|
static SDL_atomic_t isRunningOnWayland;
|
||||||
if (display != nullptr) {
|
|
||||||
wl_display_disconnect(display);
|
// If the value is not set yet, populate it now.
|
||||||
return true;
|
int val = SDL_AtomicGet(&isRunningOnWayland);
|
||||||
|
if (!(val & VALUE_SET)) {
|
||||||
|
struct wl_display* display = wl_display_connect(nullptr);
|
||||||
|
if (display != nullptr) {
|
||||||
|
wl_display_disconnect(display);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate the value to return and have for next time.
|
||||||
|
// This can race with another thread populating the same data,
|
||||||
|
// but that's no big deal.
|
||||||
|
val = VALUE_SET | ((display != nullptr) ? VALUE_TRUE : 0);
|
||||||
|
SDL_AtomicSet(&isRunningOnWayland, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return !!(val & VALUE_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user