Skip to content

Commit

Permalink
param: display_server
Browse files Browse the repository at this point in the history
This option shows wether the app is native wayland, running under xwayland
or regular X11
  • Loading branch information
flightlessmango committed Jul 16, 2024
1 parent 1abf530 commit 7cfca3f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/hud_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,20 @@ void HudElements::network() {
#endif
}

void HudElements::_display_session() {
ImGui::PushFont(HUDElements.sw_stats->font1);
ImguiNextColumnFirstItem();
HUDElements.TextColored(HUDElements.colors.engine, "%s", "display server");
ImguiNextColumnOrNewRow();
static std::map<display_servers, std::string> servers {
{WAYLAND, {"WAYLAND"}},
{XWAYLAND, {"XWAYLAND"}},
{XORG, {"XORG"}}
};
right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", servers[HUDElements.display_server].c_str());
ImGui::PopFont();
}

void HudElements::sort_elements(const std::pair<std::string, std::string>& option) {
const auto& param = option.first;
const auto& value = option.second;
Expand Down Expand Up @@ -1522,7 +1536,8 @@ void HudElements::sort_elements(const std::pair<std::string, std::string>& optio
{"refresh_rate", {refresh_rate}},
{"winesync", {winesync}},
{"present_mode", {present_mode}},
{"network", {network}}
{"network", {network}},
{"display_session", {_display_session}}

};

Expand Down Expand Up @@ -1650,6 +1665,8 @@ void HudElements::legacy_elements(){
ordered_functions.push_back({present_mode, "present_mode", value});
if (params->enabled[OVERLAY_PARAM_ENABLED_refresh_rate])
ordered_functions.push_back({refresh_rate, "refresh_rate", value});
if (params->enabled[OVERLAY_PARAM_ENABLED_display_server])
ordered_functions.push_back({_display_session, "display_session", value});
}

void HudElements::update_exec(){
Expand Down
11 changes: 11 additions & 0 deletions src/hud_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "net.h"
#include "overlay_params.h"
#include "shell.h"
#include "shared_x11.h"

struct Function {
std::function<void()> run; // Using std::function instead of a raw function pointer for more flexibility
Expand Down Expand Up @@ -53,6 +54,15 @@ class HudElements{
int hdr_status = 0;
int refresh = 0;
unsigned int vsync = 10;

enum display_servers {
UNKNOWN,
WAYLAND,
XWAYLAND,
XORG
};

display_servers display_server = UNKNOWN;
std::unique_ptr<WineSync> winesync_ptr = nullptr;
std::unique_ptr<Net> net = nullptr;
#ifdef __linux__
Expand Down Expand Up @@ -104,6 +114,7 @@ class HudElements{
static void winesync();
static void present_mode();
static void network();
static void _display_session();

void convert_colors(const struct overlay_params& params);
void convert_colors(bool do_conv, const struct overlay_params& params);
Expand Down
9 changes: 9 additions & 0 deletions src/loaders/loader_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ bool libx11_loader::Load(const std::string& library_name) {
return false;
}

XQueryExtension =
reinterpret_cast<decltype(this->XQueryExtension)>(
dlsym(library_, "XQueryExtension"));
if (!XQueryExtension) {
CleanUp(true);
return false;
}

loaded_ = true;
return true;
}
Expand All @@ -94,6 +102,7 @@ void libx11_loader::CleanUp(bool unload) {
XKeysymToKeycode = NULL;
XStringToKeysym = NULL;
XGetGeometry = NULL;
XQueryExtension = NULL;

}

Expand Down
1 change: 1 addition & 0 deletions src/loaders/loader_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class libx11_loader {
decltype(&::XKeysymToKeycode) XKeysymToKeycode;
decltype(&::XStringToKeysym) XStringToKeysym;
decltype(&::XGetGeometry) XGetGeometry;
decltype(&::XQueryExtension) XQueryExtension;


private:
Expand Down
1 change: 1 addition & 0 deletions src/overlay_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_BOOL(winesync) \
OVERLAY_PARAM_BOOL(present_mode) \
OVERLAY_PARAM_BOOL(time_no_label) \
OVERLAY_PARAM_BOOL(display_server) \
OVERLAY_PARAM_CUSTOM(fps_sampling_period) \
OVERLAY_PARAM_CUSTOM(output_folder) \
OVERLAY_PARAM_CUSTOM(output_file) \
Expand Down
10 changes: 10 additions & 0 deletions src/shared_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <spdlog/spdlog.h>
#include "shared_x11.h"
#include "loaders/loader_x11.h"
#include "hud_elements.h"

static std::unique_ptr<Display, std::function<void(Display*)>> display;

Expand Down Expand Up @@ -41,6 +42,15 @@ bool init_x11() {
if (!displayid)
SPDLOG_DEBUG("DISPLAY env is not set");

if (display && HUDElements.display_server == HUDElements.display_servers::UNKNOWN) {
int opcode, event, error;
if (libx11->XQueryExtension(display.get(), "XWAYLAND", &opcode, &event, &error))
HUDElements.display_server = HUDElements.display_servers::XWAYLAND;
else
HUDElements.display_server = HUDElements.display_servers::XORG;

}

return !!display;
}

Expand Down
1 change: 1 addition & 0 deletions src/vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,7 @@ static VkResult overlay_CreateWaylandSurfaceKHR(
if (!wl_handle)
wl_handle = real_dlopen("libwayland-client.so", RTLD_LAZY);
wl_display_ptr = pCreateInfo->display;
HUDElements.display_server = HUDElements.display_servers::WAYLAND;
init_wayland_data();
return instance_data->vtable.CreateWaylandSurfaceKHR(instance, pCreateInfo, pAllocator, pSurface);
}
Expand Down
3 changes: 3 additions & 0 deletions src/wayland_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdio>
#include "real_dlsym.h"
#include "wayland_hook.h"
#include "hud_elements.h"

EXPORT_C_(struct wl_display*) wl_display_connect(const char *name);
EXPORT_C_(struct wl_display*) wl_display_connect_to_fd(int fd);
Expand Down Expand Up @@ -32,6 +33,7 @@ EXPORT_C_(struct wl_display*) wl_display_connect(const char *name)

if (!wl_display_ptr) {
wl_display_ptr = ret;
HUDElements.display_server = HUDElements.display_servers::WAYLAND;
init_wayland_data();
}
}
Expand All @@ -55,6 +57,7 @@ EXPORT_C_(struct wl_display*) wl_display_connect_to_fd(int fd)

if (!wl_display_ptr) {
wl_display_ptr = ret;
HUDElements.display_server = HUDElements.display_servers::WAYLAND;
init_wayland_data();
}
}
Expand Down

2 comments on commit 7cfca3f

@luisalvarado
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flightlessmango To be consistent with the rest of the labels that show on the mangohud hud, what about changing this line:

HUDElements.TextColored(HUDElements.colors.engine, "%s", "display server");

To

HUDElements.TextColored(HUDElements.colors.engine, "%s", "Display Server");

(Capitalized words).

@flightlessmango
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here e248ee9

Please sign in to comment.