Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cffi: always return config values as JSON #3969

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/custom_modules/cffi_example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void onclicked(GtkButton* button) {
}

// You must
const size_t wbcffi_version = 1;
const size_t wbcffi_version = 2;

void* wbcffi_init(const wbcffi_init_info* init_info, const wbcffi_config_entry* config_entries,
size_t config_entries_len) {
Expand Down Expand Up @@ -67,4 +67,4 @@ void wbcffi_refresh(void* instance, int signal) {

void wbcffi_doaction(void* instance, const char* name) {
printf("cffi_example inst=%p: doAction(%s)\n", instance, name);
}
}
10 changes: 8 additions & 2 deletions resources/custom_modules/cffi_example/waybar_cffi_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern "C" {
#endif

/// Waybar ABI version. 1 is the latest version
/// Waybar ABI version. 2 is the latest version
extern const size_t wbcffi_version;

/// Private Waybar CFFI module
Expand Down Expand Up @@ -35,7 +35,13 @@ typedef struct {
typedef struct {
/// Entry key
const char* key;
/// Entry value as string. JSON object and arrays are serialized.
/// Entry value
///
/// In ABI version 1, this may be either a bare string if the value is a
/// string, or the JSON representation of any other JSON object as a string.
///
/// From ABI version 2 onwards, this is always the JSON representation of the
/// value as a string.
const char* value;
} wbcffi_config_entry;

Expand Down
12 changes: 8 additions & 4 deletions src/modules/cffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CFFI::CFFI(const std::string& name, const std::string& id, const Json::Value& co
}

// Fetch functions
if (*wbcffi_version == 1) {
if (*wbcffi_version == 1 || *wbcffi_version == 2) {
// Mandatory functions
hooks_.init = reinterpret_cast<InitFn*>(dlsym(handle, "wbcffi_init"));
if (!hooks_.init) {
Expand Down Expand Up @@ -58,10 +58,14 @@ CFFI::CFFI(const std::string& name, const std::string& id, const Json::Value& co
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());
if (*wbcffi_version == 1) {
if (value.isConvertibleTo(Json::ValueType::stringValue)) {
config_entries_stringstor.push_back(value.asString());
} else {
config_entries_stringstor.push_back(value.toStyledString());
}
} else {
config_entries_stringstor.push_back(config[keys[i]].toStyledString());
config_entries_stringstor.push_back(value.toStyledString());
}
}

Expand Down