Skip to content

Commit

Permalink
WM (Linux): add sway version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Jan 2, 2025
1 parent 611de77 commit 67597e4
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions src/detection/wm/wm_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,93 @@

#include "common/processing.h"
#include "detection/displayserver/displayserver.h"
#include "util/binary.h"
#include "util/path.h"
#include "util/stringUtils.h"

const char* ffDetectWMPlugin(FF_MAYBE_UNUSED FFstrbuf* pluginName)
{
return "Not supported on this platform";
}

static void getHyprland(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
static bool extractHyprlandVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
{
if (line[0] != 'v') return true;
int count = 0;
sscanf(line + 1, "%*d.%*d.%*d%n", &count);
if (count == 0) return true;

ffStrbufSetNS((FFstrbuf*) userdata, len - 1, line + 1);
return false;
}

static const char* getHyprland(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
{
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
const char* error = ffFindExecutableInPath("Hyprland", &path);
if (error) return "Failed to find Hyprland executable path";

if (ffBinaryExtractStrings(path.chars, extractHyprlandVersion, result, (uint32_t) strlen("v0.0.0")) == NULL)
return NULL;

if (ffProcessAppendStdOut(result, (char* const[]){
"Hyprland",
path.chars,
"--version",
NULL
}) == NULL){ // Hyprland 0.46.2 built from branch v0.46.2-b at... long and multi line
}) == NULL)
{ // Hyprland 0.46.2 built from branch v0.46.2-b at... long and multi line
ffStrbufSubstrAfterFirstC(result, ' ');
ffStrbufSubstrBeforeFirstC(result, ' ');
return NULL;
}

return "Failed to run command `Hyprland --version`";
}

static bool extractSwayVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
{
if (!ffStrStartsWith(line, "sway version ")) return true;

ffStrbufSetNS((FFstrbuf*) userdata, len - (uint32_t) strlen("sway version "), line + strlen("sway version "));
return false;
}

static const char* getSway(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
{
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
const char* error = ffFindExecutableInPath("sway", &path);
if (error) return "Failed to find sway executable path";

if (ffBinaryExtractStrings(path.chars, extractSwayVersion, result, (uint32_t) strlen("v0.0.0")) == NULL)
{
ffStrbufTrimRightSpace(result);
return NULL;
}

if (ffProcessAppendStdOut(result, (char* const[]){
path.chars,
"--version",
NULL
}) == NULL)
{ // sway version 1.10
ffStrbufSubstrAfterLastC(result, ' ');
ffStrbufTrimRightSpace(result);
return NULL;
}

return "Failed to run command `sway --version`";
}

const char* ffDetectWMVersion(const FFstrbuf* wmName, FFstrbuf* result, FFWMOptions* options)
{
if (!wmName)
return "No WM detected";

if (ffStrbufEqualS(wmName, FF_WM_PRETTY_HYPRLAND))
getHyprland(result, options);
else
return "Unsupported WM";
if (ffStrbufEqualS(wmName, "Hyprland"))
return getHyprland(result, options);

if (ffStrbufEqualS(wmName, "sway"))
return getSway(result, options);

return NULL;
return "Unsupported WM";
}

0 comments on commit 67597e4

Please sign in to comment.