-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (71 loc) · 2.8 KB
/
main.cpp
File metadata and controls
90 lines (71 loc) · 2.8 KB
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
#include <any>
#include <sstream>
#include <chrono>
#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include <random>
#include <iostream>
#define private public
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/event/EventBus.hpp>
#undef private
inline HANDLE PHANDLE = nullptr;
void reloadSplash() {
static auto* const SPLASH_FILE = (Hyprlang::STRING const*) HyprlandAPI::getConfigValue(PHANDLE, "plugin:custom-splashes:splash_file")->getDataStaticPtr();
const std::string splashFilePath(*SPLASH_FILE);
if (splashFilePath.empty()) {
g_pCompositor->setRandomSplash();
return;
}
std::ifstream file(splashFilePath);
if (!file.good()) {
HyprlandAPI::addNotification(PHANDLE, "[custom-splashes] Splash file not found: " + splashFilePath,
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
g_pCompositor->setRandomSplash();
return;
}
std::vector<std::string> lines;
while (file.peek() != std::ifstream::traits_type::eof()) {
std::string line;
std::getline(file, line);
if (!line.empty()) {
lines.push_back(line);
}
}
if (lines.empty()) {
HyprlandAPI::addNotification(PHANDLE, "[custom-splashes] Splash file is empty: " + splashFilePath,
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
g_pCompositor->setRandomSplash();
return;
}
std::random_device dev;
std::mt19937 engine(dev());
std::uniform_int_distribution<> distribution(0, lines.size() - 1);
std::string splash = lines.at(distribution(engine));
g_pCompositor->m_currentSplash = splash;
}
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle;
const std::string COMPOSITOR_HASH = __hyprland_api_get_hash();
const std::string CLIENT_HASH = __hyprland_api_get_client_hash();
if (COMPOSITOR_HASH != CLIENT_HASH) {
HyprlandAPI::addNotification(PHANDLE, "[custom-splashes] Mismatched headers! Can't proceed.",
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[custom-splashes] Version mismatch");
}
HyprlandAPI::addConfigValue(PHANDLE, "plugin:custom-splashes:splash_file", Hyprlang::STRING(""));
static auto P = Event::bus()->m_events.config.reloaded.listen([&] {
reloadSplash();
});
reloadSplash();
return {"custom-splashes", "A Hyprland plugin that allows users to load custom splash texts from a specified file.", "BigBrainRobin29", "1.0"};
}
APICALL EXPORT void PLUGIN_EXIT() {
g_pCompositor->setRandomSplash();
}