Skip to content

Commit

Permalink
Add winesync param
Browse files Browse the repository at this point in the history
  • Loading branch information
flightlessmango committed Dec 5, 2023
1 parent 669f6f9 commit f11b850
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ Parameters that are enabled by default have to be explicitly disabled. These (cu
| `width=`<br>`height=` | Customizeable HUD dimensions (in pixels) |
| `wine_color` | Change color of the wine/proton text |
| `wine` | Show current Wine or Proton version in use |
| `winesync` | Show wine sync method in use |

Example: `MANGOHUD_CONFIG=cpu_temp,gpu_temp,position=top-right,height=500,font_size=32`
Because comma is also used as option delimiter and needs to be escaped for values with a backslash, you can use `+` like `MANGOHUD_CONFIG=fps_limit=60+30+0` instead.
Expand Down
1 change: 1 addition & 0 deletions data/MangoHud.conf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ throttling_status
# vulkan_driver
# wine
# exec_name
# winesync

### Display loaded MangoHud architecture
# arch
Expand Down
17 changes: 16 additions & 1 deletion src/hud_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,18 @@ void HudElements::refresh_rate() {
}
}

void HudElements::winesync() {
if (!HUDElements.winesync_ptr)
HUDElements.winesync_ptr = std::make_unique<WineSync>();

if (HUDElements.winesync_ptr->valid()) {
ImguiNextColumnFirstItem();
HUDElements.TextColored(HUDElements.colors.engine, "%s", "WSYNC");
ImguiNextColumnOrNewRow();
right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", HUDElements.winesync_ptr->get_method().c_str());
}
}

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 @@ -1436,7 +1448,8 @@ void HudElements::sort_elements(const std::pair<std::string, std::string>& optio
{"graphs", {graphs}},
{"fps_metrics", {fps_metrics}},
{"hdr", {hdr}},
{"refresh_rate", {refresh_rate}}
{"refresh_rate", {refresh_rate}},
{"winesync", {winesync}}
};

auto check_param = display_params.find(param);
Expand Down Expand Up @@ -1548,6 +1561,8 @@ void HudElements::legacy_elements(){
ordered_functions.push_back({exec_name, "exec_name", value});
if (params->enabled[OVERLAY_PARAM_ENABLED_duration])
ordered_functions.push_back({duration, "duration", value});
if (params->enabled[OVERLAY_PARAM_ENABLED_winesync])
ordered_functions.push_back({winesync, "winesync", value});
}

void HudElements::update_exec(){
Expand Down
3 changes: 3 additions & 0 deletions src/hud_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <imgui.h>
#include "timing.hpp"
#include <functional>
#include "winesync.h"

struct Function {
std::function<void()> run; // Using std::function instead of a raw function pointer for more flexibility
Expand Down Expand Up @@ -46,6 +47,7 @@ class HudElements{
uint32_t vendorID;
int hdr_status = 0;
int refresh = 0;
std::unique_ptr<WineSync> winesync_ptr = nullptr;

void sort_elements(const std::pair<std::string, std::string>& option);
void legacy_elements();
Expand Down Expand Up @@ -89,6 +91,7 @@ class HudElements{
static void fps_metrics();
static void hdr();
static void refresh_rate();
static void winesync();

void convert_colors(const struct overlay_params& params);
void convert_colors(bool do_conv, const struct overlay_params& params);
Expand Down
1 change: 1 addition & 0 deletions src/overlay_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_BOOL(inherit) \
OVERLAY_PARAM_BOOL(hdr) \
OVERLAY_PARAM_BOOL(refresh_rate) \
OVERLAY_PARAM_BOOL(winesync) \
OVERLAY_PARAM_CUSTOM(fps_sampling_period) \
OVERLAY_PARAM_CUSTOM(output_folder) \
OVERLAY_PARAM_CUSTOM(output_file) \
Expand Down
81 changes: 81 additions & 0 deletions src/winesync.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "file_utils.h"
#include <filesystem.h>
#include <string>

namespace fs = ghc::filesystem;

class WineSync {
private:
enum syncMethods {
NONE,
WINESERVER,
ESYNC,
FSYNC,
NTSYNC
};

int method = 0;
bool inside_wine = true;

const char* methods[5] = {
"NONE",
"Wserver",
"Esync",
"Fsync",
"NTsync"
};

public:
WineSync() {
#ifdef __linux__
// check that's were inside wine
std::string wineProcess = get_exe_path();
auto n = wineProcess.find_last_of('/');
std::string preloader = wineProcess.substr(n + 1);
if (preloader != "wine-preloader" && preloader != "wine64-preloader"){
inside_wine = false;
return;
}

const char* paths[2] {
"/proc/self/map_files",
"/proc/self/fd"
};

// check which sync wine is using, if any.
fs::path path;
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
path = paths[i];
for (auto& p : fs::directory_iterator(path)) {
auto filename = p.path().string().c_str();
auto sym = read_symlink(filename);
if (sym.find("winesync") != std::string::npos)
method = syncMethods::NTSYNC;
else if (sym.find("fsync") != std::string::npos)
method = syncMethods::FSYNC;
else if (sym.find("ntsync") != std::string::npos)
method = syncMethods::NTSYNC;
else if (sym.find("esync") != std::string::npos)
method = syncMethods::ESYNC;

if (method)
break;

}
if (method)
break;
}
#endif
};

bool valid() {
return inside_wine;
}

// return sync method as display name
std::string get_method() {
return methods[method];
}
};

extern std::unique_ptr<WineSync> winesync_ptr;

0 comments on commit f11b850

Please sign in to comment.