mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-14 15:12:31 +02:00
Added cffi/* module for third-party advanced modules
This commit is contained in:
@ -1,50 +0,0 @@
|
||||
#include "modules/cabi.hpp"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
namespace waybar::modules {
|
||||
|
||||
CABI::CABI(const std::string& name, const std::string& id, const Json::Value& config)
|
||||
: AModule(config, name, id, true, true) {
|
||||
const auto dynlib_path = config_["path"].asString();
|
||||
|
||||
void* handle = dlopen(dynlib_path.c_str(), RTLD_LAZY);
|
||||
if (handle == nullptr) {
|
||||
throw std::runtime_error{dlerror()};
|
||||
}
|
||||
|
||||
// Fetch ABI version
|
||||
auto wbcabi_version = reinterpret_cast<size_t*>(dlsym(handle, "wbcabi_version"));
|
||||
if (wbcabi_version == nullptr) {
|
||||
throw std::runtime_error{"Missing wbcabi_version"};
|
||||
}
|
||||
|
||||
// Fetch functions
|
||||
if (*wbcabi_version == 1) {
|
||||
wbcabi_init_ = reinterpret_cast<void* (*)(GtkContainer*)>(dlsym(handle, "wbcabi_init"));
|
||||
if (wbcabi_init_ == nullptr) {
|
||||
throw std::runtime_error{"Missing wbcabi_init function"};
|
||||
}
|
||||
if (auto fn = reinterpret_cast<void (*)(void*)>(dlsym(handle, "wbcabi_deinit")))
|
||||
wbcabi_deinit_ = fn;
|
||||
if (auto fn = reinterpret_cast<const char* (*)(void*)>(dlsym(handle, "wbcabi_last_error_str")))
|
||||
wbcabi_last_error_str_ = fn;
|
||||
} else {
|
||||
throw std::runtime_error{"Unknown wbcabi_version " + std::to_string(*wbcabi_version)};
|
||||
}
|
||||
|
||||
cabi_instance_ = wbcabi_init_(dynamic_cast<Gtk::Container*>(&event_box_)->gobj());
|
||||
if (cabi_instance_ == nullptr) {
|
||||
const auto err_str = wbcabi_last_error_str_(cabi_instance_);
|
||||
throw std::runtime_error{std::string{"Failed to initialize C ABI plugin: "} +
|
||||
(err_str != nullptr ? err_str : "unknown error")};
|
||||
}
|
||||
}
|
||||
|
||||
CABI::~CABI() {
|
||||
if (cabi_instance_ != nullptr) {
|
||||
wbcabi_deinit_(cabi_instance_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace waybar::modules
|
108
src/modules/cffi.cpp
Normal file
108
src/modules/cffi.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "modules/cffi.hpp"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <json/value.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
namespace waybar::modules {
|
||||
|
||||
CFFI::CFFI(const std::string& name, const std::string& id, const Json::Value& config)
|
||||
: AModule(config, name, id, true, true) {
|
||||
const auto dynlib_path = config_["module_path"].asString();
|
||||
if (dynlib_path.empty()) {
|
||||
throw std::runtime_error{"Missing or empty 'module_path' in module config"};
|
||||
}
|
||||
|
||||
void* handle = dlopen(dynlib_path.c_str(), RTLD_LAZY);
|
||||
if (handle == nullptr) {
|
||||
throw std::runtime_error{std::string{"Failed to load CFFI module: "} + dlerror()};
|
||||
}
|
||||
|
||||
// Fetch ABI version
|
||||
auto wbcffi_version = reinterpret_cast<size_t*>(dlsym(handle, "wbcffi_version"));
|
||||
if (wbcffi_version == nullptr) {
|
||||
throw std::runtime_error{std::string{"Missing wbcffi_version function: "} + dlerror()};
|
||||
}
|
||||
|
||||
// Fetch functions
|
||||
if (*wbcffi_version == 1) {
|
||||
// Mandatory functions
|
||||
hooks_.init = reinterpret_cast<InitFn*>(dlsym(handle, "wbcffi_init"));
|
||||
if (!hooks_.init) {
|
||||
throw std::runtime_error{std::string{"Missing wbcffi_init function: "} + dlerror()};
|
||||
}
|
||||
hooks_.deinit = reinterpret_cast<DenitFn*>(dlsym(handle, "wbcffi_deinit"));
|
||||
if (!hooks_.init) {
|
||||
throw std::runtime_error{std::string{"Missing wbcffi_deinit function: "} + dlerror()};
|
||||
}
|
||||
// Optional functions
|
||||
if (auto fn = reinterpret_cast<RefreshFn*>(dlsym(handle, "wbcffi_refresh"))) {
|
||||
hooks_.refresh = fn;
|
||||
}
|
||||
if (auto fn = reinterpret_cast<DoActionFn*>(dlsym(handle, "wbcffi_doaction"))) {
|
||||
hooks_.doAction = fn;
|
||||
}
|
||||
} else {
|
||||
throw std::runtime_error{"Unknown wbcffi_version " + std::to_string(*wbcffi_version)};
|
||||
}
|
||||
|
||||
// Prepare init() arguments
|
||||
// Convert JSON values to string
|
||||
std::vector<std::string> config_entries_stringstor;
|
||||
const auto& keys = config.getMemberNames();
|
||||
for (size_t i = 0; i < keys.size(); i++) {
|
||||
const auto& value = config[keys[i]];
|
||||
if (value.isConvertibleTo(Json::ValueType::stringValue)) {
|
||||
config_entries_stringstor.push_back(config[keys[i]].asString());
|
||||
} else {
|
||||
config_entries_stringstor.push_back(config[keys[i]].toStyledString());
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare config_entries array
|
||||
std::vector<struct wbcffi_config_entry> config_entries;
|
||||
for (size_t i = 0; i < keys.size(); i++) {
|
||||
config_entries.push_back(
|
||||
wbcffi_config_entry{keys[i].c_str(), config_entries_stringstor[i].c_str()});
|
||||
}
|
||||
|
||||
// Call init
|
||||
cffi_instance_ = hooks_.init(dynamic_cast<Gtk::Container*>(&event_box_)->gobj(),
|
||||
config_entries.data(), config_entries.size());
|
||||
|
||||
// Handle init failures
|
||||
if (cffi_instance_ == nullptr) {
|
||||
throw std::runtime_error{"Failed to initialize C ABI module"};
|
||||
}
|
||||
}
|
||||
|
||||
CFFI::~CFFI() {
|
||||
if (cffi_instance_ != nullptr) {
|
||||
hooks_.deinit(cffi_instance_);
|
||||
}
|
||||
}
|
||||
|
||||
auto CFFI::refresh(int signal) -> void {
|
||||
assert(cffi_instance_ != nullptr);
|
||||
hooks_.refresh(cffi_instance_, signal);
|
||||
}
|
||||
|
||||
auto CFFI::doAction(const std::string& name) -> void {
|
||||
assert(cffi_instance_ != nullptr);
|
||||
if (!name.empty()) {
|
||||
// TODO: Make a decision
|
||||
// Calling AModule::doAction and hooks_.doAction will execute the action twice if it is
|
||||
// configured in AModule::eventActionMap_ and implemented in the CFFI module.
|
||||
//
|
||||
// Should we block AModule::doAction() if the action is implemented in hooks_.doAction() ?
|
||||
// (doAction could return true if the action has been processed)
|
||||
//
|
||||
hooks_.doAction(cffi_instance_, name.c_str());
|
||||
AModule::doAction(name);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace waybar::modules
|
Reference in New Issue
Block a user