aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Primak <vyivel@posteo.net>2021-08-09 17:57:19 +0300
committerSimon Ser <contact@emersion.fr>2021-08-10 19:15:48 +0200
commita6a80850b729eefaafac325256023ee92b21be34 (patch)
tree14d4f66a8fd3e06cc95668705b83763f5bb97153
parent604674dc54a3b2cb4b73de267c8c2bcae259c4b6 (diff)
util: add wlr_addon
-rw-r--r--include/wlr/util/addon.h43
-rw-r--r--util/addon.c49
-rw-r--r--util/meson.build1
3 files changed, 93 insertions, 0 deletions
diff --git a/include/wlr/util/addon.h b/include/wlr/util/addon.h
new file mode 100644
index 00000000..3ad71f54
--- /dev/null
+++ b/include/wlr/util/addon.h
@@ -0,0 +1,43 @@
+/*
+ * This an unstable interface of wlroots. No guarantees are made regarding the
+ * future consistency of this API.
+ */
+#ifndef WLR_USE_UNSTABLE
+#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
+#endif
+
+#ifndef WLR_UTIL_ADDON_H
+#define WLR_UTIL_ADDON_H
+
+#include <wayland-server-core.h>
+
+struct wlr_addon_set {
+ // private state
+ struct wl_list addons;
+};
+
+struct wlr_addon;
+
+struct wlr_addon_interface {
+ const char *name;
+ void (*destroy)(struct wlr_addon *addon);
+};
+
+struct wlr_addon {
+ const struct wlr_addon_interface *impl;
+ // private state
+ const void *owner;
+ struct wl_list link;
+};
+
+void wlr_addon_set_init(struct wlr_addon_set *set);
+void wlr_addon_set_finish(struct wlr_addon_set *set);
+
+void wlr_addon_init(struct wlr_addon *addon, struct wlr_addon_set *set,
+ const void *owner, const struct wlr_addon_interface *impl);
+void wlr_addon_finish(struct wlr_addon *addon);
+
+struct wlr_addon *wlr_addon_find_by_owner(struct wlr_addon_set *set,
+ const void *owner);
+
+#endif
diff --git a/util/addon.c b/util/addon.c
new file mode 100644
index 00000000..7de54a73
--- /dev/null
+++ b/util/addon.c
@@ -0,0 +1,49 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <wayland-server-core.h>
+
+#include "wlr/util/addon.h"
+
+void wlr_addon_set_init(struct wlr_addon_set *set) {
+ wl_list_init(&set->addons);
+}
+
+void wlr_addon_set_finish(struct wlr_addon_set *set) {
+ struct wlr_addon *addon, *tmp;
+ wl_list_for_each_safe(addon, tmp, &set->addons, link) {
+ wlr_addon_finish(addon);
+ addon->impl->destroy(addon);
+ }
+}
+
+void wlr_addon_init(struct wlr_addon *addon, struct wlr_addon_set *set,
+ const void *owner, const struct wlr_addon_interface *impl) {
+ assert(owner);
+ struct wlr_addon *iter;
+ wl_list_for_each(iter, &set->addons, link) {
+ if (iter->owner == addon->owner) {
+ assert(0 && "Can't have two addons with the same owner");
+ }
+ }
+ wl_list_insert(&set->addons, &addon->link);
+ addon->owner = owner;
+ addon->impl = impl;
+}
+
+void wlr_addon_finish(struct wlr_addon *addon) {
+ if (addon->owner) {
+ addon->owner = NULL;
+ wl_list_remove(&addon->link);
+ }
+}
+
+struct wlr_addon *wlr_addon_find_by_owner(struct wlr_addon_set *set,
+ const void *owner) {
+ struct wlr_addon *addon;
+ wl_list_for_each(addon, &set->addons, link) {
+ if (addon->owner == owner) {
+ return addon;
+ }
+ }
+ return NULL;
+}
diff --git a/util/meson.build b/util/meson.build
index c2b46749..a416490f 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -1,4 +1,5 @@
wlr_files += files(
+ 'addon.c',
'array.c',
'box.c',
'global.c',