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
|
#include "PluginSystem.hpp"
#include <dlfcn.h>
#include <ranges>
#include "../Compositor.hpp"
CPluginSystem::CPluginSystem() {
g_pFunctionHookSystem = std::make_unique<CHookSystem>();
}
CPlugin* CPluginSystem::loadPlugin(const std::string& path) {
m_szLastError = "";
if (getPluginByPath(path)) {
m_szLastError = "Cannot load a plugin twice!";
Debug::log(ERR, " [PluginSystem] Cannot load a plugin twice!");
return nullptr;
}
auto* const PLUGIN = m_vLoadedPlugins.emplace_back(std::make_unique<CPlugin>()).get();
PLUGIN->path = path;
HANDLE MODULE = dlopen(path.c_str(), RTLD_LAZY);
if (!MODULE) {
std::string strerr = dlerror();
m_szLastError = std::format("Plugin {} could not be loaded: {}", path, strerr);
Debug::log(ERR, " [PluginSystem] Plugin {} could not be loaded: {}", path, strerr);
m_vLoadedPlugins.pop_back();
return nullptr;
}
PLUGIN->m_pHandle = MODULE;
PPLUGIN_API_VERSION_FUNC apiVerFunc = (PPLUGIN_API_VERSION_FUNC)dlsym(MODULE, PLUGIN_API_VERSION_FUNC_STR);
PPLUGIN_INIT_FUNC initFunc = (PPLUGIN_INIT_FUNC)dlsym(MODULE, PLUGIN_INIT_FUNC_STR);
if (!apiVerFunc || !initFunc) {
m_szLastError = std::format("Plugin {} could not be loaded: {}", path, "missing apiver/init func");
Debug::log(ERR, " [PluginSystem] Plugin {} could not be loaded. (No apiver/init func)", path);
dlclose(MODULE);
m_vLoadedPlugins.pop_back();
return nullptr;
}
const std::string PLUGINAPIVER = apiVerFunc();
if (PLUGINAPIVER != HYPRLAND_API_VERSION) {
m_szLastError = std::format("Plugin {} could not be loaded: {}", path, "API version mismatch");
Debug::log(ERR, " [PluginSystem] Plugin {} could not be loaded. (API version mismatch)", path);
dlclose(MODULE);
m_vLoadedPlugins.pop_back();
return nullptr;
}
PLUGIN_DESCRIPTION_INFO PLUGINDATA;
try {
if (!setjmp(m_jbPluginFaultJumpBuf)) {
m_bAllowConfigVars = true;
PLUGINDATA = initFunc(MODULE);
} else {
// this module crashed.
throw std::runtime_error("received a fatal signal");
}
} catch (std::exception& e) {
m_bAllowConfigVars = false;
m_szLastError = std::format("Plugin {} could not be loaded: plugin crashed/threw in main: {}", path, e.what());
Debug::log(ERR, " [PluginSystem] Plugin {} (Handle {:x}) crashed in init. Unloading.", path, (uintptr_t)MODULE);
unloadPlugin(PLUGIN, true); // Plugin could've already hooked/done something
return nullptr;
}
m_bAllowConfigVars = false;
PLUGIN->author = PLUGINDATA.author;
PLUGIN->description = PLUGINDATA.description;
PLUGIN->version = PLUGINDATA.version;
PLUGIN->name = PLUGINDATA.name;
Debug::log(LOG, " [PluginSystem] Plugin {} loaded. Handle: {:x}, path: \"{}\", author: \"{}\", description: \"{}\", version: \"{}\"", PLUGINDATA.name, (uintptr_t)MODULE, path,
PLUGINDATA.author, PLUGINDATA.description, PLUGINDATA.version);
return PLUGIN;
}
void CPluginSystem::unloadPlugin(const CPlugin* plugin, bool eject) {
if (!plugin)
return;
if (!eject) {
PPLUGIN_EXIT_FUNC exitFunc = (PPLUGIN_EXIT_FUNC)dlsym(plugin->m_pHandle, PLUGIN_EXIT_FUNC_STR);
if (exitFunc)
exitFunc();
}
for (auto& [k, v] : plugin->registeredCallbacks) {
if (const auto SP = v.lock())
g_pHookSystem->unhook(SP);
}
const auto ls = plugin->registeredLayouts;
for (auto& l : ls)
g_pLayoutManager->removeLayout(l);
g_pFunctionHookSystem->removeAllHooksFrom(plugin->m_pHandle);
const auto rd = plugin->registeredDecorations;
for (auto& d : rd)
HyprlandAPI::removeWindowDecoration(plugin->m_pHandle, d);
const auto rdi = plugin->registeredDispatchers;
for (auto& d : rdi)
HyprlandAPI::removeDispatcher(plugin->m_pHandle, d);
const auto rhc = plugin->registeredHyprctlCommands;
for (auto& c : rhc)
HyprlandAPI::unregisterHyprCtlCommand(plugin->m_pHandle, c);
g_pConfigManager->removePluginConfig(plugin->m_pHandle);
// save these two for dlclose and a log,
// as erase_if will kill the pointer
const auto PLNAME = plugin->name;
const auto PLHANDLE = plugin->m_pHandle;
std::erase_if(m_vLoadedPlugins, [&](const auto& other) { return other->m_pHandle == PLHANDLE; });
dlclose(PLHANDLE);
Debug::log(LOG, " [PluginSystem] Plugin {} unloaded.", PLNAME);
// reload config to fix some stuf like e.g. unloadedPluginVars
g_pConfigManager->m_bForceReload = true;
}
void CPluginSystem::unloadAllPlugins() {
for (auto& p : m_vLoadedPlugins | std::views::reverse)
unloadPlugin(p.get(), false); // Unload remaining plugins gracefully
}
std::vector<std::string> CPluginSystem::updateConfigPlugins(const std::vector<std::string>& plugins, bool& changed) {
std::vector<std::string> failures;
// unload all plugins that are no longer present
for (auto& p : m_vLoadedPlugins | std::views::reverse) {
if (p->m_bLoadedWithConfig && std::find(plugins.begin(), plugins.end(), p->path) == plugins.end()) {
Debug::log(LOG, "Unloading plugin {} which is no longer present in config", p->path);
unloadPlugin(p.get(), false);
changed = true;
}
}
// load all new plugins
for (auto& path : plugins) {
if (std::find_if(m_vLoadedPlugins.begin(), m_vLoadedPlugins.end(), [&](const auto& other) { return other->path == path; }) == m_vLoadedPlugins.end()) {
Debug::log(LOG, "Loading plugin {} which is now present in config", path);
const auto plugin = loadPlugin(path);
if (plugin) {
plugin->m_bLoadedWithConfig = true;
changed = true;
} else
failures.push_back(path);
}
}
return failures;
}
CPlugin* CPluginSystem::getPluginByPath(const std::string& path) {
for (auto& p : m_vLoadedPlugins) {
if (p->path == path)
return p.get();
}
return nullptr;
}
CPlugin* CPluginSystem::getPluginByHandle(HANDLE handle) {
for (auto& p : m_vLoadedPlugins) {
if (p->m_pHandle == handle)
return p.get();
}
return nullptr;
}
std::vector<CPlugin*> CPluginSystem::getAllPlugins() {
std::vector<CPlugin*> results(m_vLoadedPlugins.size());
for (size_t i = 0; i < m_vLoadedPlugins.size(); ++i)
results[i] = m_vLoadedPlugins[i].get();
return results;
}
|