Debian Codegraph @

src/api.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <inttypes.h>
5
6 #include <picom/api.h>
7 #include <picom/backend.h>
8
9 #include <uthash.h>
10
11 #include "compiler.h"
12 #include "log.h"
13 #include "utils/list.h"
14 #include "utils/misc.h"
15
16 struct backend_plugins {
17 UT_hash_handle hh;
18 const char *backend_name;
19 struct list_node plugins;
20 } *backend_plugins;
21
22 struct backend_plugin {
23 const char *backend_name;
24 picom_backend_plugin_entrypoint entrypoint;
25 void *user_data;
26 struct list_node siblings;
27 };
28
29 static bool add_backend_plugin(const char *backend_name, uint64_t major, uint64_t minor,
30 picom_backend_plugin_entrypoint entrypoint, void *user_data) {
31 if (major != PICOM_BACKEND_MAJOR || minor > PICOM_BACKEND_MINOR) {
32 log_error("Cannot add plugin for backend %s, because the requested "
33 "version %" PRIu64 ".%" PRIu64 " is incompatible with the our "
34 "%lu.%lu",
35 backend_name, major, minor, PICOM_BACKEND_MAJOR,
36 PICOM_BACKEND_MINOR);
37 return false;
38 }
39
40 auto plugin = ccalloc(1, struct backend_plugin);
41 plugin->backend_name = backend_name;
42 plugin->entrypoint = entrypoint;
43 plugin->user_data = user_data;
44
45 struct backend_plugins *plugins = NULL;
46 HASH_FIND_STR(backend_plugins, backend_name, plugins);
47 if (!plugins) {
48 plugins = ccalloc(1, struct backend_plugins);
49 plugins->backend_name = strdup(backend_name);
50 list_init_head(&plugins->plugins);
51 HASH_ADD_STR(backend_plugins, backend_name, plugins);
52 }
53 list_insert_after(&plugins->plugins, &plugin->siblings);
54 return true;
55 }
56
57 void api_backend_plugins_invoke(const char *backend_name, struct backend_base *backend) {
58 struct backend_plugins *plugins = NULL;
59 HASH_FIND_STR(backend_plugins, backend_name, plugins);
60 if (!plugins) {
61 return;
62 }
63
64 list_foreach(struct backend_plugin, plugin, &plugins->plugins, siblings) {
65 plugin->entrypoint(backend, plugin->user_data);
66 }
67 }
68
69 static struct picom_api picom_api = {
70 .add_backend_plugin = add_backend_plugin,
71 };
72
73 PICOM_PUBLIC_API const struct picom_api *
74 picom_api_get_interfaces(uint64_t major, uint64_t minor, const char *context) {
75 if (major != PICOM_API_MAJOR || minor > PICOM_API_MINOR) {
76 log_error("Cannot provide API interfaces to %s, because the requested"
77 "version %" PRIu64 ".%" PRIu64 " is incompatible with our "
78 "%lu.%lu",
79 context, major, minor, PICOM_API_MAJOR, PICOM_API_MINOR);
80 return NULL;
81 }
82 return &picom_api;
83 }