master ohmu/fastfetch-openbsd / src / detection / wallpaper / wallpaper_haiku.cpp
ohmu  ·  2026-10-15
 1extern "C" {
 2#include "wallpaper.h"
 3#include "common/debug.h"
 4#include "common/mallocHelper.h"
 5}
 6
 7#include <Application.h>
 8#include <FindDirectory.h>
 9#include <InterfaceDefs.h>
10#include <Node.h>
11#include <Path.h>
12#include <Screen.h>
13#include <fs_attr.h>
14#include <be_apps/Tracker/Background.h>
15
16const char* ffDetectWallpaper(FFstrbuf* result) {
17    BMessage backgrounds;
18    BPath pDesktop;
19    BString path;
20
21    if (find_directory(B_DESKTOP_DIRECTORY, &pDesktop) < B_OK) {
22        FF_DEBUG("find_directory(B_DESKTOP_DIRECTORY) failed");
23        return "find_directory(B_DESKTOP_DIRECTORY) failed";
24    }
25
26    // We need a valid be_app to query the app_server here.
27    BApplication app("application/x-vnd.fastfetch-cli-fastfetch");
28
29    BNode nDesktop(pDesktop.Path());
30    if (nDesktop.InitCheck() == B_OK) {
31        struct attr_info ai;
32        if (nDesktop.GetAttrInfo(B_BACKGROUND_INFO, &ai) == B_OK && ai.size > 0) {
33            FF_AUTO_FREE char* pAttr = (char*) malloc(ai.size);
34            if (nDesktop.ReadAttr(B_BACKGROUND_INFO, ai.type, 0LL, pAttr, (size_t) ai.size) >= B_OK) {
35                if (backgrounds.Unflatten(pAttr) == B_OK) {
36                    for (int i = 0; backgrounds.FindString(B_BACKGROUND_IMAGE, i, &path) == B_OK; i++) {
37                        int32 ws;
38                        if (backgrounds.FindInt32(B_BACKGROUND_WORKSPACES, i, &ws) == B_OK) {
39                            if (ws & (1 << current_workspace())) {
40                                // We try to match the one for the current workspace
41                                break;
42                            }
43                        }
44                    }
45                }
46            }
47        }
48    }
49
50    if (path.Length() < 1) {
51        FF_DEBUG("Failed to detect the current wallpaper path");
52        return "Failed to detect the current wallpaper path";
53    }
54
55    ffStrbufAppendS(result, path.String());
56    return nullptr;
57}