Skip to content

Commit 67597e4

Browse files
committed
WM (Linux): add sway version detection
1 parent 611de77 commit 67597e4

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

src/detection/wm/wm_linux.c

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,93 @@
22

33
#include "common/processing.h"
44
#include "detection/displayserver/displayserver.h"
5+
#include "util/binary.h"
6+
#include "util/path.h"
7+
#include "util/stringUtils.h"
58

69
const char* ffDetectWMPlugin(FF_MAYBE_UNUSED FFstrbuf* pluginName)
710
{
811
return "Not supported on this platform";
912
}
1013

11-
static void getHyprland(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
14+
static bool extractHyprlandVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
1215
{
16+
if (line[0] != 'v') return true;
17+
int count = 0;
18+
sscanf(line + 1, "%*d.%*d.%*d%n", &count);
19+
if (count == 0) return true;
20+
21+
ffStrbufSetNS((FFstrbuf*) userdata, len - 1, line + 1);
22+
return false;
23+
}
24+
25+
static const char* getHyprland(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
26+
{
27+
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
28+
const char* error = ffFindExecutableInPath("Hyprland", &path);
29+
if (error) return "Failed to find Hyprland executable path";
30+
31+
if (ffBinaryExtractStrings(path.chars, extractHyprlandVersion, result, (uint32_t) strlen("v0.0.0")) == NULL)
32+
return NULL;
33+
1334
if (ffProcessAppendStdOut(result, (char* const[]){
14-
"Hyprland",
35+
path.chars,
1536
"--version",
1637
NULL
17-
}) == NULL){ // Hyprland 0.46.2 built from branch v0.46.2-b at... long and multi line
38+
}) == NULL)
39+
{ // Hyprland 0.46.2 built from branch v0.46.2-b at... long and multi line
1840
ffStrbufSubstrAfterFirstC(result, ' ');
1941
ffStrbufSubstrBeforeFirstC(result, ' ');
42+
return NULL;
2043
}
44+
45+
return "Failed to run command `Hyprland --version`";
46+
}
47+
48+
static bool extractSwayVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
49+
{
50+
if (!ffStrStartsWith(line, "sway version ")) return true;
51+
52+
ffStrbufSetNS((FFstrbuf*) userdata, len - (uint32_t) strlen("sway version "), line + strlen("sway version "));
53+
return false;
54+
}
55+
56+
static const char* getSway(FFstrbuf* result, FF_MAYBE_UNUSED FFWMOptions* options)
57+
{
58+
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
59+
const char* error = ffFindExecutableInPath("sway", &path);
60+
if (error) return "Failed to find sway executable path";
61+
62+
if (ffBinaryExtractStrings(path.chars, extractSwayVersion, result, (uint32_t) strlen("v0.0.0")) == NULL)
63+
{
64+
ffStrbufTrimRightSpace(result);
65+
return NULL;
66+
}
67+
68+
if (ffProcessAppendStdOut(result, (char* const[]){
69+
path.chars,
70+
"--version",
71+
NULL
72+
}) == NULL)
73+
{ // sway version 1.10
74+
ffStrbufSubstrAfterLastC(result, ' ');
75+
ffStrbufTrimRightSpace(result);
76+
return NULL;
77+
}
78+
79+
return "Failed to run command `sway --version`";
2180
}
2281

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

28-
if (ffStrbufEqualS(wmName, FF_WM_PRETTY_HYPRLAND))
29-
getHyprland(result, options);
30-
else
31-
return "Unsupported WM";
87+
if (ffStrbufEqualS(wmName, "Hyprland"))
88+
return getHyprland(result, options);
89+
90+
if (ffStrbufEqualS(wmName, "sway"))
91+
return getSway(result, options);
3292

33-
return NULL;
93+
return "Unsupported WM";
3494
}

0 commit comments

Comments
 (0)