Skip to content

Commit

Permalink
api: introduce the concept of backend specific plugins
Browse files Browse the repository at this point in the history
Allow loaded plugins to hook into specific backends.

Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed May 23, 2024
1 parent f197fd2 commit 1d52f0d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
21 changes: 20 additions & 1 deletion include/picom/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@

#pragma once

#include <stdbool.h>
#include <stdint.h>

#define PICOM_API_MAJOR (0UL)
#define PICOM_API_MINOR (1UL)

struct picom_api {};
struct backend_base;

/// The entry point of a backend plugin. Called after the backend is initialized.
typedef void (*picom_backend_plugin_entrypoint)(struct backend_base *backend, void *user_data);
struct picom_api {
/// Add a plugin for a specific backend. The plugin's entry point will be called
/// when the specified backend is initialized.
///
/// @param backend_name The name of the backend to add the plugin to.
/// @param major The major version of the backend API interface this plugin
/// is compatible with.
/// @param minor The minor version of the backend API interface this plugin
/// is compatible with.
/// @param entrypoint The entry point of the plugin.
/// @param user_data The user data to pass to the plugin's entry point.
bool (*add_backend_plugin)(const char *backend_name, uint64_t major, uint64_t minor,
picom_backend_plugin_entrypoint entrypoint,
void *user_data);
};

struct picom_api *
picom_api_get_interfaces(uint64_t major, uint64_t minor, const char *context);
61 changes: 60 additions & 1 deletion src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,69 @@
#include <picom/api.h>
#include <picom/backend.h>

#include <uthash.h>

#include "compiler.h"
#include "list.h"
#include "log.h"
#include "utils.h"

struct backend_plugins {
UT_hash_handle hh;
const char *backend_name;
struct list_node plugins;
} *backend_plugins;

struct backend_plugin {
const char *backend_name;
picom_backend_plugin_entrypoint entrypoint;
void *user_data;
struct list_node siblings;
};

static bool add_backend_plugin(const char *backend_name, uint64_t major, uint64_t minor,
picom_backend_plugin_entrypoint entrypoint, void *user_data) {
if (major != PICOM_BACKEND_MAJOR || minor > PICOM_BACKEND_MINOR) {
log_error("Cannot add plugin for backend %s, because the requested "
"version %lu.%lu is "
"incompatible with the our %lu.%lu",
backend_name, major, minor, PICOM_BACKEND_MAJOR,
PICOM_BACKEND_MINOR);
return false;
}

auto plugin = ccalloc(1, struct backend_plugin);
plugin->backend_name = backend_name;
plugin->entrypoint = entrypoint;
plugin->user_data = user_data;

struct backend_plugins *plugins = NULL;
HASH_FIND_STR(backend_plugins, backend_name, plugins);
if (!plugins) {
plugins = ccalloc(1, struct backend_plugins);
plugins->backend_name = strdup(backend_name);
list_init_head(&plugins->plugins);
HASH_ADD_STR(backend_plugins, backend_name, plugins);
}
list_insert_after(&plugins->plugins, &plugin->siblings);
return true;
}

void backend_plugins_invoke(const char *backend_name, struct backend_base *backend) {
struct backend_plugins *plugins = NULL;
HASH_FIND_STR(backend_plugins, backend_name, plugins);
if (!plugins) {
return;
}

list_foreach(struct backend_plugin, plugin, &plugins->plugins, siblings) {
plugin->entrypoint(backend, plugin->user_data);
}
}

static struct picom_api picom_api;
static struct picom_api picom_api = {
.add_backend_plugin = add_backend_plugin,
};

struct picom_api *PICOM_PUBLIC_API picom_api_get_interfaces(uint64_t major, uint64_t minor,
const char *context) {
Expand Down
2 changes: 2 additions & 0 deletions src/picom.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <picom/types.h>
#include <test.h>

#include "api_internal.h"
#include "common.h"
#include "compiler.h"
#include "config.h"
Expand Down Expand Up @@ -660,6 +661,7 @@ static bool initialize_backend(session_t *ps) {
// Reinitialize win_data
ps->backend_data =
backend_init(ps->o.backend, ps, session_get_target_window(ps));
backend_plugins_invoke(backend_name(ps->o.backend), ps->backend_data);
if (!ps->backend_data) {
log_fatal("Failed to initialize backend, aborting...");
quit(ps);
Expand Down

0 comments on commit 1d52f0d

Please sign in to comment.