Moved all waybar info into a single struct

This commit is contained in:
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 waybar::modules {
namespace ffi {
extern "C" { 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 { struct wbcffi_config_entry {
const char* key; const char* key;
const char* value; const char* value;
}; };
} }
} // namespace ffi
class CFFI : public AModule { class CFFI : public AModule {
public: public:
@ -30,9 +40,8 @@ class CFFI : public AModule {
/// ///
void* cffi_instance_ = nullptr; void* cffi_instance_ = nullptr;
typedef void*(InitFn)(GtkContainer* root_widget, const void (*trigger_update)(void*), typedef void*(InitFn)(const ffi::wbcffi_init_info* init_info,
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries, const ffi::wbcffi_config_entry* config_entries, size_t config_entries_len);
size_t config_entries_len);
typedef void(DenitFn)(void* instance); typedef void(DenitFn)(void* instance);
typedef void(RefreshFn)(void* instance, int signal); typedef void(RefreshFn)(void* instance, int signal);
typedef void(DoActionFn)(void* instance, const char* name); typedef void(DoActionFn)(void* instance, const char* name);

View File

@ -2,10 +2,10 @@
#include "waybar_cffi_module.h" #include "waybar_cffi_module.h"
typedef struct { typedef struct {
GtkContainer* root; wbcffi_module* waybar_module;
GtkBox* container; GtkBox* container;
GtkButton* button; GtkButton* button;
} Instance; } ExampleMod;
// This static variable is shared between all instances of this module // This static variable is shared between all instances of this module
static int instance_count = 0; static int instance_count = 0;
@ -19,8 +19,7 @@ void onclicked(GtkButton* button) {
// You must // You must
const size_t wbcffi_version = 1; const size_t wbcffi_version = 1;
void* wbcffi_init(GtkContainer* root_widget, void (*trigger_update)(void*), void* wbcffi_init(const wbcffi_init_info* init_info, const wbcffi_config_entry* config_entries,
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries,
size_t config_entries_len) { size_t config_entries_len) {
printf("cffi_example: init config:\n"); printf("cffi_example: init config:\n");
for (size_t i = 0; i < config_entries_len; i++) { 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 // Allocate the instance object
Instance* inst = malloc(sizeof(Instance)); ExampleMod* inst = malloc(sizeof(ExampleMod));
inst->root = root_widget; inst->waybar_module = init_info->obj;
GtkContainer* root = init_info->get_root_widget(init_info->obj);
// Add a container for displaying the next widgets // Add a container for displaying the next widgets
inst->container = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5)); 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 // Add a label
GtkLabel* label = GTK_LABEL(gtk_label_new("[Example C FFI Module:")); 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 /// Waybar ABI version. 1 is the latest version
extern const size_t wbcffi_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 /// Config key-value pair
struct wbcffi_config_entry { typedef struct {
/// Entry key /// Entry key
const char* key; const char* key;
/// Entry value as string. JSON object and arrays are serialized. /// Entry value as string. JSON object and arrays are serialized.
const char* value; const char* value;
}; } wbcffi_config_entry;
/// Module init/new function, called on module instantiation /// Module init/new function, called on module instantiation
/// ///
/// MANDATORY CFFI function /// MANDATORY CFFI function
/// ///
/// @param root_widget Root GTK widget instantiated by Waybar /// @param init_info Waybar module information
/// @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 config_entries Flat representation of the module JSON config. The data only available /// @param config_entries Flat representation of the module JSON config. The data only available
/// during wbcffi_init call. /// during wbcffi_init call.
/// @param config_entries_len Number of entries in `config_entries` /// @param config_entries_len Number of entries in `config_entries`
/// ///
/// @return A untyped pointer to module data, NULL if the module failed to load. /// @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* wbcffi_init(const wbcffi_init_info* init_info, const wbcffi_config_entry* config_entries,
void* trigger_update_arg, const struct wbcffi_config_entry* config_entries,
size_t config_entries_len); size_t config_entries_len);
/// Module deinit/delete function, called when Waybar is closed or when the module is removed /// 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 // 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++) { for (size_t i = 0; i < keys.size(); i++) {
config_entries.push_back( config_entries.push_back({keys[i].c_str(), config_entries_stringstor[i].c_str()});
wbcffi_config_entry{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 // Call init
cffi_instance_ = hooks_.init( cffi_instance_ = hooks_.init(&init_info, config_entries.data(), config_entries.size());
dynamic_cast<Gtk::Container*>(&event_box_)->gobj(),
[](void* dp) { ((Glib::Dispatcher*)dp)->emit(); }, &dp, config_entries.data(),
config_entries.size());
// Handle init failures // Handle init failures
if (cffi_instance_ == nullptr) { if (cffi_instance_ == nullptr) {