]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add common/array.h with wl_array_len()
authorJohan Malm <jgm323@gmail.com>
Sun, 20 Aug 2023 10:56:50 +0000 (11:56 +0100)
committerConsolatis <35009135+Consolatis@users.noreply.github.com>
Sun, 20 Aug 2023 11:08:45 +0000 (13:08 +0200)
include/common/array.h [new file with mode: 0644]
src/osd.c

diff --git a/include/common/array.h b/include/common/array.h
new file mode 100644 (file)
index 0000000..4f6e623
--- /dev/null
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef LABWC_ARRAY_H
+#define LABWC_ARRAY_H
+#include <wayland-server-core.h>
+
+/*
+ * Wayland's wl_array API is a bit sparse consisting only of
+ *  - init
+ *  - release
+ *  - add
+ *  - copy
+ *  - for_each
+ *
+ * The purpose of this header is the gather any generic wl_array helpers we
+ * create.
+ *
+ * We take the liberty of using the wl_ suffix here to make it look a bit
+ * prettier. If Wayland extend the API in future, we will sort the clash then.
+ */
+
+/**
+ * wl_array_len() - return length of wl_array
+ * @array: wl_array for which to calculate length
+ * Note: The pointer type might not be 'char' but this is the approach that
+ * wl_array_for_each() takes, so we align with their style.
+ */
+static inline size_t
+wl_array_len(struct wl_array *array)
+{
+       return array->size / sizeof(const char *);
+}
+
+#endif /* LABWC_ARRAY_H */
index de7f3009c58801278a956421eb9b620ae6bd3de5..f6d6766b12020572121e2d70b6082983188d66b2 100644 (file)
--- a/src/osd.c
+++ b/src/osd.c
@@ -7,6 +7,7 @@
 #include <wlr/util/log.h>
 #include <wlr/util/box.h>
 #include "buffer.h"
+#include "common/array.h"
 #include "common/buf.h"
 #include "common/font.h"
 #include "common/graphic-helpers.h"
@@ -411,7 +412,7 @@ display_osd(struct output *output)
 
        float scale = output->wlr_output->scale;
        int w = theme->osd_window_switcher_width;
-       int h = views.size / sizeof(struct view *) * rc.theme->osd_window_switcher_item_height
+       int h = wl_array_len(&views) * rc.theme->osd_window_switcher_item_height
                + 2 * rc.theme->osd_border_width
                + 2 * rc.theme->osd_window_switcher_padding;
        if (show_workspace) {