desktop: add x11 support (thanks krizej)

This commit is contained in:
jacekpoz 2024-02-11 18:32:00 +01:00
parent 1640be4666
commit 6b8e1087bd
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C

View file

@ -7,6 +7,9 @@
#include "../mod.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef MF_DESKTOP_X11
#include <X11/Xlib.h>
#endif
static const semver api_ver = {
.major = 0,
@ -36,7 +39,27 @@ uint8_t init(char **config) {
const char *get(void) {
char *ret;
if (asprintf(&ret, "Desktop: %s", getenv("XDG_CURRENT_DESKTOP")) < 0) {
char *current_desktop = getenv("XDG_CURRENT_DESKTOP");
if (current_desktop == NULL) {
#ifdef MF_DESKTOP_X11
Display *disp = XOpenDisplay(NULL);
Window root = XDefaultRootWindow(disp);
Atom atom = XInternAtom(disp, "_NET_WM_NAME", 0);
Atom type;
int fmt;
unsigned long num_items;
unsigned long bytes_after;
unsigned char *prop;
XGetWindowProperty(disp, root, atom, 0, 4096 / sizeof(int32_t), 0, AnyPropertyType,
&type, &fmt, &num_items, &bytes_after, &prop);
current_desktop = prop;
#else
current_desktop = "Unknown";
#endif
}
if (asprintf(&ret, "Desktop: %s", current_desktop) < 0) {
fprintf(stderr, "error: failed formatting (this shouldn't happen)");
exit(EXIT_FAILURE);
}