Moved all waybar info into a single struct

pull/2573/head
Crom (Thibaut CHARLES) 2023-10-19 22:10:32 +02:00
parent d86059016e
commit 02c64f3f1e
No known key found for this signature in database
GPG Key ID: 45A3D5F880B9E6D0
4 changed files with 59 additions and 26 deletions

View File

@ -9,13 +9,23 @@
namespace waybar::modules {
namespace ffi {
extern "C" {
// C ABI representation of a config key/value pair
typedef struct wbcffi_module wbcffi_module;
typedef struct {
wbcffi_module* obj;
const char* waybar_version;
GtkContainer* (*get_root_widget)(wbcffi_module*);
void (*queue_update)(wbcffi_module*);
} wbcffi_init_info;
struct wbcffi_config_entry {
const char* key;
const char* value;
};
}
} // namespace ffi
class CFFI : public AModule {
public:
@ -30,9 +40,8 @@ class CFFI : public AModule {
///
void* cffi_instance_ = nullptr;
typedef void*(InitFn)(GtkContainer* root_widget, const void (*trigger_update)(void*),
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries,
size_t config_entries_len);
typedef void*(InitFn)(const ffi::wbcffi_init_info* init_info,
const ffi::wbcffi_config_entry* config_entries, size_t config_entries_len);
typedef void(DenitFn)(void* instance);
typedef void(RefreshFn)(void* instance, int signal);
typedef void(DoActionFn)(void* instance, const char* name);

View File

@ -2,10 +2,10 @@
#include "waybar_cffi_module.h"
typedef struct {
GtkContainer* root;
wbcffi_module* waybar_module;
GtkBox* container;
GtkButton* button;
} Instance;
} ExampleMod;
// This static variable is shared between all instances of this module
static int instance_count = 0;
@ -19,8 +19,7 @@ void onclicked(GtkButton* button) {
// You must
const size_t wbcffi_version = 1;
void* wbcffi_init(GtkContainer* root_widget, void (*trigger_update)(void*),
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries,
void* wbcffi_init(const wbcffi_init_info* init_info, const wbcffi_config_entry* config_entries,
size_t config_entries_len) {
printf("cffi_example: init config:\n");
for (size_t i = 0; i < config_entries_len; i++) {
@ -28,12 +27,14 @@ void* wbcffi_init(GtkContainer* root_widget, void (*trigger_update)(void*),
}
// Allocate the instance object
Instance* inst = malloc(sizeof(Instance));
inst->root = root_widget;
ExampleMod* inst = malloc(sizeof(ExampleMod));
inst->waybar_module = init_info->obj;
GtkContainer* root = init_info->get_root_widget(init_info->obj);
// Add a container for displaying the next widgets
inst->container = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5));
gtk_container_add(GTK_CONTAINER(inst->root), GTK_WIDGET(inst->container));
gtk_container_add(GTK_CONTAINER(root), GTK_WIDGET(inst->container));
// Add a label
GtkLabel* label = GTK_LABEL(gtk_label_new("[Example C FFI Module:"));

View File

@ -10,29 +10,46 @@ extern "C" {
/// Waybar ABI version. 1 is the latest version
extern const size_t wbcffi_version;
/// Private Waybar CFFI module
typedef struct wbcffi_module wbcffi_module;
/// Waybar module information
typedef struct {
/// Waybar CFFI object pointer
wbcffi_module* obj;
/// Waybar version string
const char* waybar_version;
/// Returns the waybar widget allocated for this module
/// @param obj Waybar CFFI object pointer
GtkContainer* (*get_root_widget)(wbcffi_module* obj);
/// Queues a request for calling wbcffi_update() on the next GTK main event
/// loop iteration
/// @param obj Waybar CFFI object pointer
void (*queue_update)(wbcffi_module*);
} wbcffi_init_info;
/// Config key-value pair
struct wbcffi_config_entry {
typedef struct {
/// Entry key
const char* key;
/// Entry value as string. JSON object and arrays are serialized.
const char* value;
};
} wbcffi_config_entry;
/// Module init/new function, called on module instantiation
///
/// MANDATORY CFFI function
///
/// @param root_widget Root GTK widget instantiated by Waybar
/// @param trigger_update Call this function with trigger_update_arg as argument to trigger
/// wbcffi_update() on the next GTK main event loop iteration
/// @param trigger_update_arg Argument for trigger_update call
/// @param init_info Waybar module information
/// @param config_entries Flat representation of the module JSON config. The data only available
/// during wbcffi_init call.
/// @param config_entries_len Number of entries in `config_entries`
///
/// @return A untyped pointer to module data, NULL if the module failed to load.
void* wbcffi_init(GtkContainer* root_widget, const void (*trigger_update)(void*),
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries,
void* wbcffi_init(const wbcffi_init_info* init_info, const wbcffi_config_entry* config_entries,
size_t config_entries_len);
/// Module deinit/delete function, called when Waybar is closed or when the module is removed

View File

@ -66,17 +66,23 @@ CFFI::CFFI(const std::string& name, const std::string& id, const Json::Value& co
}
// Prepare config_entries array
std::vector<struct wbcffi_config_entry> config_entries;
std::vector<ffi::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()});
config_entries.push_back({keys[i].c_str(), config_entries_stringstor[i].c_str()});
}
ffi::wbcffi_init_info init_info = {
.obj = (ffi::wbcffi_module*)this,
.waybar_version = VERSION,
.get_root_widget =
[](ffi::wbcffi_module* obj) {
return dynamic_cast<Gtk::Container*>(&((CFFI*)obj)->event_box_)->gobj();
},
.queue_update = [](ffi::wbcffi_module* obj) { ((CFFI*)obj)->dp.emit(); },
};
// Call init
cffi_instance_ = hooks_.init(
dynamic_cast<Gtk::Container*>(&event_box_)->gobj(),
[](void* dp) { ((Glib::Dispatcher*)dp)->emit(); }, &dp, config_entries.data(),
config_entries.size());
cffi_instance_ = hooks_.init(&init_info, config_entries.data(), config_entries.size());
// Handle init failures
if (cffi_instance_ == nullptr) {