Skip to content

Commit 22a3ab4

Browse files
Detect wayland WM _much_ faster
1 parent 44d6780 commit 22a3ab4

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/detection/displayserver/wayland.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define _GNU_SOURCE //required for struct ucred
2+
13
#include "displayServer.h"
24

35
#include <stdlib.h>
@@ -7,6 +9,7 @@
79
#include <pthread.h>
810
#include <dlfcn.h>
911
#include <wayland-client.h>
12+
#include <sys/socket.h>
1013

1114
typedef struct WaylandData
1215
{
@@ -19,6 +22,25 @@ typedef struct WaylandData
1922
struct wl_output_listener output_listener;
2023
} WaylandData;
2124

25+
static void waylandDetectWM(int fd, FFDisplayServerResult* result)
26+
{
27+
if(fd < 1)
28+
return;
29+
30+
struct ucred ucred;
31+
socklen_t len = sizeof(struct ucred);
32+
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1)
33+
return;
34+
35+
FFstrbuf procPath;
36+
ffStrbufInit(&procPath);
37+
ffStrbufAppendF(&procPath, "/proc/%d/cmdline", ucred.pid); //We check the cmdline for the process name, because it is not trimmed.
38+
ffReadFileBuffer(procPath.chars, &result->wmProcessName);
39+
ffStrbufSubstrBeforeFirstC(&result->wmProcessName, '\0'); //Trim the arguments
40+
ffStrbufSubstrAfterLastC(&result->wmProcessName, '/'); //Trim the path
41+
ffStrbufDestroy(&procPath);
42+
}
43+
2244
static void waylandGlobalRemoveListener(void* data, struct wl_registry* wl_registry, uint32_t name){
2345
FF_UNUSED(data, wl_registry, name);
2446
}
@@ -81,6 +103,7 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
81103
FF_LIBRARY_LOAD(wayland, instance->config.libWayland, false, "libwayland-client.so", 1)
82104

83105
FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_connect, false)
106+
FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_get_fd, false)
84107
FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_dispatch, false)
85108
FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_roundtrip, false)
86109
FF_LIBRARY_LOAD_SYMBOL(wayland, wl_proxy_marshal_constructor, false)
@@ -89,7 +112,7 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
89112

90113
WaylandData data;
91114

92-
FF_LIBRARY_LOAD_SYMBOL_ADRESS(wayland, data.ffwl_proxy_marshal_constructor_versioned, ffwl_proxy_marshal_constructor, false)
115+
FF_LIBRARY_LOAD_SYMBOL_ADRESS(wayland, data.ffwl_proxy_marshal_constructor_versioned, wl_proxy_marshal_constructor_versioned, false)
93116
FF_LIBRARY_LOAD_SYMBOL_ADRESS(wayland, data.ffwl_proxy_add_listener, wl_proxy_add_listener, false)
94117
FF_LIBRARY_LOAD_SYMBOL_ADRESS(wayland, data.ffwl_output_interface, wl_output_interface, false)
95118
FF_LIBRARY_LOAD_SYMBOL_ADRESS(wayland, data.ffwl_proxy_destroy, wl_proxy_destroy, false)
@@ -101,6 +124,8 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
101124
return false;
102125
}
103126

127+
waylandDetectWM(ffwl_display_get_fd(display), result);
128+
104129
struct wl_registry* registry = (struct wl_registry*) ffwl_proxy_marshal_constructor((struct wl_proxy*) display, WL_DISPLAY_GET_REGISTRY, ffwl_registry_interface, NULL);
105130
if(registry == NULL)
106131
{

src/detection/displayserver/wmde.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,21 @@ static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* proce
5656
if(!ffStrSet(processName))
5757
return;
5858

59-
if(strcasecmp(processName, "kwin_wayland") == 0 || strcasecmp(processName, "kwin_x11") == 0 || strcasecmp(processName, "kwin") == 0)
60-
ffStrbufSetS(&result->wmPrettyName, "KWin");
59+
if(
60+
strcasecmp(processName, "kwin_wayland") == 0 ||
61+
strcasecmp(processName, "kwin_wayland_wrapper") == 0 ||
62+
strcasecmp(processName, "kwin_x11") == 0 ||
63+
strcasecmp(processName, "kwin_x11_wrapper") == 0 ||
64+
strcasecmp(processName, "kwin") == 0
65+
) ffStrbufSetS(&result->wmPrettyName, "KWin");
66+
else if(
67+
strcasecmp(processName, "gnome-session-binary") == 0 ||
68+
strcasecmp(processName, "Mutter") == 0
69+
) ffStrbufSetS(&result->wmPrettyName, "Mutter");
70+
else if(
71+
strcasecmp(processName, "cinnamon-session") == 0 ||
72+
strcasecmp(processName, "Muffin") == 0
73+
) ffStrbufSetS(&result->wmPrettyName, "Muffin");
6174
else if(strcasecmp(processName, "sway") == 0)
6275
ffStrbufSetS(&result->wmPrettyName, "Sway");
6376
else if(strcasecmp(processName, "weston") == 0)
@@ -72,10 +85,6 @@ static void applyPrettyNameIfWM(FFDisplayServerResult* result, const char* proce
7285
ffStrbufSetS(&result->wmPrettyName, "Marco");
7386
else if(strcasecmp(processName, "xmonad") == 0)
7487
ffStrbufSetS(&result->wmPrettyName, "XMonad");
75-
else if(strcasecmp(processName, "gnome-session-binary") == 0 || strcasecmp(processName, "Mutter") == 0)
76-
ffStrbufSetS(&result->wmPrettyName, "Mutter");
77-
else if(strcasecmp(processName, "cinnamon-session") == 0 || strcasecmp(processName, "Muffin") == 0)
78-
ffStrbufSetS(&result->wmPrettyName, "Muffin");
7988
else if( // WMs where the pretty name matches the process name
8089
strcasecmp(processName, "dwm") == 0 ||
8190
strcasecmp(processName, "bspwm") == 0 ||
@@ -379,7 +388,7 @@ void ffdsDetectWMDE(const FFinstance* instance, FFDisplayServerResult* result)
379388
getWMProtocolNameFromEnv(result);
380389

381390
//We don't want to detect anything in TTY
382-
//This can't happen if a X11 connection succeeded, so we don't need to clear wmProcessName
391+
//This can't happen if a connection succeeded, so we don't need to clear wmProcessName
383392
if(ffStrbufIgnCaseCompS(&result->wmProtocolName, FF_DISPLAYSERVER_PROTOCOL_TTY) == 0)
384393
return;
385394

0 commit comments

Comments
 (0)