2023-02-27 13:32:38 +01:00
|
|
|
#include "PluginSystem.hpp"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2023-03-01 00:11:49 +01:00
|
|
|
#include <ranges>
|
2023-02-27 13:32:38 +01:00
|
|
|
#include "../Compositor.hpp"
|
|
|
|
|
|
|
|
CPluginSystem::CPluginSystem() {
|
|
|
|
g_pFunctionHookSystem = std::make_unique<CHookSystem>();
|
|
|
|
}
|
|
|
|
|
|
|
|
CPlugin* CPluginSystem::loadPlugin(const std::string& path) {
|
|
|
|
|
2023-11-17 23:22:31 +01:00
|
|
|
m_szLastError = "";
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
if (getPluginByPath(path)) {
|
2023-11-17 23:22:31 +01:00
|
|
|
m_szLastError = "Cannot load a plugin twice!";
|
2023-02-27 13:32:38 +01:00
|
|
|
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) {
|
2023-11-17 23:24:52 +01:00
|
|
|
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);
|
2023-02-27 13:32:38 +01:00
|
|
|
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) {
|
2023-11-17 23:22:31 +01:00
|
|
|
m_szLastError = std::format("Plugin {} could not be loaded: {}", path, "missing apiver/init func");
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(ERR, " [PluginSystem] Plugin {} could not be loaded. (No apiver/init func)", path);
|
2023-02-27 13:32:38 +01:00
|
|
|
dlclose(MODULE);
|
|
|
|
m_vLoadedPlugins.pop_back();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string PLUGINAPIVER = apiVerFunc();
|
|
|
|
|
|
|
|
if (PLUGINAPIVER != HYPRLAND_API_VERSION) {
|
2023-11-17 23:22:31 +01:00
|
|
|
m_szLastError = std::format("Plugin {} could not be loaded: {}", path, "API version mismatch");
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(ERR, " [PluginSystem] Plugin {} could not be loaded. (API version mismatch)", path);
|
2023-02-27 13:32:38 +01:00
|
|
|
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.
|
2023-11-17 23:22:31 +01:00
|
|
|
throw std::runtime_error("received a fatal signal");
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
m_bAllowConfigVars = false;
|
2023-11-17 23:22:31 +01:00
|
|
|
m_szLastError = std::format("Plugin {} could not be loaded: plugin crashed/threw in main: {}", path, e.what());
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(ERR, " [PluginSystem] Plugin {} (Handle {:x}) crashed in init. Unloading.", path, (uintptr_t)MODULE);
|
2023-02-27 13:32:38 +01:00
|
|
|
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;
|
2024-07-29 16:56:50 +02:00
|
|
|
|
|
|
|
g_pConfigManager->m_bForceReload = true;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(LOG, " [PluginSystem] Plugin {} loaded. Handle: {:x}, path: \"{}\", author: \"{}\", description: \"{}\", version: \"{}\"", PLUGINDATA.name, (uintptr_t)MODULE, path,
|
|
|
|
PLUGINDATA.author, PLUGINDATA.description, PLUGINDATA.version);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& [k, v] : plugin->registeredCallbacks) {
|
2024-04-27 13:43:12 +02:00
|
|
|
if (const auto SHP = v.lock())
|
|
|
|
g_pHookSystem->unhook(SHP);
|
2024-04-22 16:57:03 +02:00
|
|
|
}
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
const auto ls = plugin->registeredLayouts;
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& l : ls)
|
2023-02-27 13:32:38 +01:00
|
|
|
g_pLayoutManager->removeLayout(l);
|
|
|
|
|
|
|
|
g_pFunctionHookSystem->removeAllHooksFrom(plugin->m_pHandle);
|
|
|
|
|
|
|
|
const auto rd = plugin->registeredDecorations;
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& d : rd)
|
2023-02-27 13:32:38 +01:00
|
|
|
HyprlandAPI::removeWindowDecoration(plugin->m_pHandle, d);
|
|
|
|
|
|
|
|
const auto rdi = plugin->registeredDispatchers;
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& d : rdi)
|
2023-02-27 13:32:38 +01:00
|
|
|
HyprlandAPI::removeDispatcher(plugin->m_pHandle, d);
|
|
|
|
|
2024-02-05 02:56:38 +01:00
|
|
|
const auto rhc = plugin->registeredHyprctlCommands;
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& c : rhc)
|
2024-02-05 02:56:38 +01:00
|
|
|
HyprlandAPI::unregisterHyprCtlCommand(plugin->m_pHandle, c);
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
g_pConfigManager->removePluginConfig(plugin->m_pHandle);
|
|
|
|
|
2024-04-22 16:46:43 +02:00
|
|
|
// save these two for dlclose and a log,
|
|
|
|
// as erase_if will kill the pointer
|
2024-04-22 16:50:01 +02:00
|
|
|
const auto PLNAME = plugin->name;
|
2024-04-22 16:46:43 +02:00
|
|
|
const auto PLHANDLE = plugin->m_pHandle;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2024-04-22 16:46:43 +02:00
|
|
|
std::erase_if(m_vLoadedPlugins, [&](const auto& other) { return other->m_pHandle == PLHANDLE; });
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2024-04-22 16:46:43 +02:00
|
|
|
dlclose(PLHANDLE);
|
|
|
|
|
|
|
|
Debug::log(LOG, " [PluginSystem] Plugin {} unloaded.", PLNAME);
|
2023-07-10 14:13:23 +02:00
|
|
|
|
|
|
|
// reload config to fix some stuf like e.g. unloadedPluginVars
|
|
|
|
g_pConfigManager->m_bForceReload = true;
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 00:11:49 +01:00
|
|
|
void CPluginSystem::unloadAllPlugins() {
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& p : m_vLoadedPlugins | std::views::reverse)
|
2023-03-01 00:11:49 +01:00
|
|
|
unloadPlugin(p.get(), false); // Unload remaining plugins gracefully
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:10:53 +02:00
|
|
|
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
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& p : m_vLoadedPlugins | std::views::reverse) {
|
2023-05-01 16:10:53 +02:00
|
|
|
if (p->m_bLoadedWithConfig && std::find(plugins.begin(), plugins.end(), p->path) == plugins.end()) {
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(LOG, "Unloading plugin {} which is no longer present in config", p->path);
|
2023-05-01 16:10:53 +02:00
|
|
|
unloadPlugin(p.get(), false);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load all new plugins
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& path : plugins) {
|
2023-05-01 16:10:53 +02:00
|
|
|
if (std::find_if(m_vLoadedPlugins.begin(), m_vLoadedPlugins.end(), [&](const auto& other) { return other->path == path; }) == m_vLoadedPlugins.end()) {
|
2023-09-06 21:45:37 +02:00
|
|
|
Debug::log(LOG, "Loading plugin {} which is now present in config", path);
|
2023-05-01 16:10:53 +02:00
|
|
|
const auto plugin = loadPlugin(path);
|
|
|
|
|
|
|
|
if (plugin) {
|
|
|
|
plugin->m_bLoadedWithConfig = true;
|
|
|
|
changed = true;
|
|
|
|
} else
|
|
|
|
failures.push_back(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
CPlugin* CPluginSystem::getPluginByPath(const std::string& path) {
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& p : m_vLoadedPlugins) {
|
2023-02-27 13:32:38 +01:00
|
|
|
if (p->path == path)
|
|
|
|
return p.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPlugin* CPluginSystem::getPluginByHandle(HANDLE handle) {
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& p : m_vLoadedPlugins) {
|
2023-02-27 13:32:38 +01:00
|
|
|
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;
|
2023-03-01 00:11:49 +01:00
|
|
|
}
|
2024-04-27 18:38:48 +02:00
|
|
|
|
|
|
|
size_t CPluginSystem::pluginCount() {
|
|
|
|
return m_vLoadedPlugins.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPluginSystem::sig_getPlugins(CPlugin** data, size_t len) {
|
|
|
|
for (size_t i = 0; i < std::min(m_vLoadedPlugins.size(), len); i++) {
|
|
|
|
data[i] = m_vLoadedPlugins[i].get();
|
|
|
|
}
|
|
|
|
}
|