1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include "../displayserver_linux.h"
#include "common/io.h"
#include "common/edidHelper.h"
#include "common/strutil.h"
#include <stdlib.h>
#include <string.h>
#ifdef FF_HAVE_WAYLAND
#include <sys/socket.h>
#include "common/properties.h"
#include "wayland.h"
#include "wlr-output-management-unstable-v1-client-protocol.h"
#include "kde-output-device-v2-client-protocol.h"
#include "kde-output-order-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#if __FreeBSD__
#include <sys/un.h>
#include <sys/ucred.h>
#include <sys/sysctl.h>
#endif
static bool waylandDetectWM(int fd, FFDisplayServerResult* result) {
#if __linux__ || __GNU__ || (__FreeBSD__ && !__DragonFly__)
#if __linux__ || __GNU__
struct ucred ucred = {};
socklen_t len = sizeof(ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1 || ucred.pid <= 0) {
return false;
}
FF_STRBUF_AUTO_DESTROY procPath = ffStrbufCreate();
ffStrbufAppendF(&procPath, "/proc/%d/cmdline", ucred.pid); // We check the cmdline for the process name, because it is not trimmed.
if (!ffReadFileBuffer(procPath.chars, &result->wmProcessName)) {
return false;
}
#else
struct xucred ucred = {};
socklen_t len = sizeof(ucred);
if (getsockopt(fd, AF_UNSPEC, LOCAL_PEERCRED, &ucred, &len) == -1 || ucred.cr_pid <= 0) {
return false;
}
size_t size = 4096;
ffStrbufEnsureFixedLengthFree(&result->wmProcessName, (uint32_t) size);
if (sysctl((int[]) { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, ucred.cr_pid }, 4, result->wmProcessName.chars, &size, NULL, 0) != 0) {
return false;
}
result->wmProcessName.length = (uint32_t) size - 1;
#endif
// #1135: wl-restart is a special case
const char* filename = strrchr(result->wmProcessName.chars, '/');
if (filename) {
filename++;
} else {
filename = result->wmProcessName.chars;
}
if (ffStrEquals(filename, "wl-restart")) {
ffStrbufSubstrAfterLastC(&result->wmProcessName, '\0');
}
ffStrbufSubstrBeforeFirstC(&result->wmProcessName, '\0'); // Trim the arguments
ffStrbufSubstrAfterLastC(&result->wmProcessName, '/'); // Trim the path
return true;
#else
FF_UNUSED(fd, result);
return false;
#endif
}
static void waylandGlobalAddListener(void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
WaylandData* wldata = data;
if ((wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_NONE || wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_GLOBAL) && ffStrEquals(interface, wldata->ffwl_output_interface->name)) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_GLOBAL;
if (ffWaylandHandleGlobalOutput(wldata, registry, name, version) != NULL) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_NONE;
}
} else if ((wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_NONE || wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_ZWLR) && ffStrEquals(interface, zwlr_output_manager_v1_interface.name)) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_ZWLR;
if (ffWaylandHandleZwlrOutput(wldata, registry, name, version) != NULL) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_NONE;
}
} else if ((wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_NONE || wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_KDE) && ffStrEquals(interface, kde_output_device_v2_interface.name)) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_KDE;
if (ffWaylandHandleKdeOutput(wldata, registry, name, version) != NULL) {
wldata->protocolType = FF_WAYLAND_PROTOCOL_TYPE_NONE;
}
} else if (ffStrEquals(interface, kde_output_order_v1_interface.name)) {
ffWaylandHandleKdeOutputOrder(wldata, registry, name, version);
} else if ((wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_GLOBAL || wldata->protocolType == FF_WAYLAND_PROTOCOL_TYPE_NONE) && ffStrEquals(interface, zxdg_output_manager_v1_interface.name)) {
ffWaylandHandleZxdgOutput(wldata, registry, name, version);
}
}
static FF_A_UNUSED bool matchDrmConnector(const char* connName, WaylandDisplay* wldata) {
// https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output-event-name
// The doc says that "do not assume that the name is a reflection of an underlying DRM connector, X11 connection, etc."
// However I can't find a better method to get the edid data
const char* drmDirPath = "/sys/class/drm/";
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(drmDirPath);
if (dirp == NULL) {
return false;
}
struct dirent* entry;
while ((entry = readdir(dirp)) != NULL) {
const char* plainName = entry->d_name;
if (ffStrStartsWith(plainName, "card")) {
const char* tmp = strchr(plainName + strlen("card"), '-');
if (tmp) {
plainName = tmp + 1;
}
}
if (ffStrEquals(plainName, connName)) {
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateF("%s%s/edid", drmDirPath, entry->d_name);
uint8_t edidData[512];
ssize_t edidLength = ffReadFileData(path.chars, ARRAY_SIZE(edidData), edidData);
if (edidLength > 0 && edidLength % 128 == 0) {
ffEdidGetName(edidData, &wldata->edidName);
ffEdidGetHdrCompatible(edidData, (uint32_t) edidLength);
ffEdidGetSerialAndManufactureDate(edidData, &wldata->serial, &wldata->myear, &wldata->mweek);
wldata->hdrInfoAvailable = true;
return true;
}
break;
}
}
return false;
}
void ffWaylandOutputNameListener(void* data, FF_A_UNUSED void* output, const char* name) {
WaylandDisplay* display = data;
if (display->id) {
return;
}
display->type = ffdsGetDisplayType(name);
#if __linux__
if (!display->edidName.length) {
matchDrmConnector(name, display);
}
#endif
display->id = ffWaylandGenerateIdFromName(name);
ffStrbufAppendS(&display->name, name);
}
void ffWaylandOutputDescriptionListener(void* data, FF_A_UNUSED void* output, const char* description) {
WaylandDisplay* display = data;
if (display->description.length) {
return;
}
while (*description == ' ') {
++description;
}
if (!ffStrEquals(description, "Unknown Display") && !ffStrContains(description, "(null)")) {
ffStrbufAppendS(&display->description, description);
}
}
uint32_t ffWaylandHandleRotation(WaylandDisplay* display) {
uint32_t rotation;
switch (display->transform) {
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_90:
rotation = 90;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
case WL_OUTPUT_TRANSFORM_180:
rotation = 180;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
case WL_OUTPUT_TRANSFORM_270:
rotation = 270;
break;
default:
rotation = 0;
break;
}
switch (rotation) {
case 90:
case 270: {
int32_t temp = display->width;
display->width = display->height;
display->height = temp;
temp = display->physicalWidth;
display->physicalWidth = display->physicalHeight;
display->physicalHeight = temp;
break;
}
default:
break;
}
return rotation;
}
const char* ffdsConnectWayland(FFDisplayServerResult* result) {
if (getenv("XDG_RUNTIME_DIR") == NULL) {
return "Wayland requires $XDG_RUNTIME_DIR being set";
}
FF_LIBRARY_LOAD_MESSAGE(wayland, "libwayland-client" FF_LIBRARY_EXTENSION, 1)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(wayland, wl_display_connect)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(wayland, wl_display_get_fd)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(wayland, wl_proxy_marshal_constructor)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(wayland, wl_display_disconnect)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(wayland, wl_registry_interface)
WaylandData data = {};
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(wayland, data, wl_proxy_marshal_constructor_versioned)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(wayland, data, wl_proxy_add_listener)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(wayland, data, wl_proxy_destroy)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(wayland, data, wl_display_roundtrip)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(wayland, data, wl_output_interface)
data.display = ffwl_display_connect(NULL);
if (data.display == NULL) {
return "wl_display_connect returned NULL";
}
waylandDetectWM(ffwl_display_get_fd(data.display), result);
struct wl_proxy* registry = ffwl_proxy_marshal_constructor((struct wl_proxy*) data.display, WL_DISPLAY_GET_REGISTRY, ffwl_registry_interface, NULL);
if (registry == NULL) {
ffwl_display_disconnect(data.display);
return "wl_display_get_registry returned NULL";
}
data.result = result;
struct wl_registry_listener registry_listener = {
.global = waylandGlobalAddListener,
.global_remove = (void*) stubListener
};
data.ffwl_proxy_add_listener(registry, (void (**)(void)) ®istry_listener, &data);
data.ffwl_display_roundtrip(data.display);
if (data.zxdgOutputManager) {
data.ffwl_proxy_destroy(data.zxdgOutputManager);
}
data.ffwl_proxy_destroy(registry);
ffwl_display_disconnect(data.display);
if (data.primaryDisplayId == 0 && result->wmProcessName.length > 0) {
const char* fileName = ffStrbufEqualS(&result->wmProcessName, "gnome-shell")
? "monitors.xml"
: ffStrbufEqualS(&result->wmProcessName, "cinnamon")
? "cinnamon-monitors.xml"
: NULL;
if (fileName) {
FF_STRBUF_AUTO_DESTROY monitorsXml = ffStrbufCreate();
FF_LIST_FOR_EACH (FFstrbuf, basePath, instance.state.platform.configDirs) {
char path[1024];
snprintf(path, ARRAY_SIZE(path), "%s%s", basePath->chars, fileName);
if (ffReadFileBuffer(path, &monitorsXml)) {
break;
}
}
if (monitorsXml.length) {
// <monitors version="2">
// <configuration>
// <logicalmonitor>
// <x>0</x>
// <y>0</y>
// <scale>1.7489879131317139</scale>
// <primary>yes</primary>
// <monitor>
// <monitorspec>
// <connector>Virtual-1</connector>
// <vendor>unknown</vendor>
// <product>unknown</product>
// <serial>unknown</serial>
// </monitorspec>
// <mode>
// <width>3456</width>
// <height>2160</height>
// <rate>60.000068664550781</rate>
// </mode>
// </monitor>
// </logicalmonitor>
// </configuration>
// </monitors>
uint32_t start = ffStrbufFirstIndexS(&monitorsXml, "<primary>yes</primary>");
if (start < monitorsXml.length) {
start = ffStrbufNextIndexS(&monitorsXml, start, "<connector>");
if (start < monitorsXml.length) {
uint32_t end = ffStrbufNextIndexS(&monitorsXml, start, "</connector>");
if (end < monitorsXml.length) {
ffStrbufSubstrBefore(&monitorsXml, end);
const char* name = monitorsXml.chars + start + strlen("<connector>");
data.primaryDisplayId = ffWaylandGenerateIdFromName(name);
}
}
}
}
}
}
if (data.primaryDisplayId) {
FF_LIST_FOR_EACH (FFDisplayResult, d, data.result->displays) {
if (d->id == data.primaryDisplayId) {
d->primary = true;
break;
}
}
}
// We successfully connected to wayland and detected the display.
// So we can set set the session type to wayland.
// This is used as an indicator that we are running wayland by the x11 backends.
ffStrbufSetStatic(&result->wmProtocolName, FF_WM_PROTOCOL_WAYLAND);
return NULL;
}
#else
const char* ffdsConnectWayland(FF_A_UNUSED FFDisplayServerResult* result) {
return "Fastfetch was compiled without Wayland support";
}
#endif
|