aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-03-28 15:25:33 -0400
committerGitHub <noreply@github.com>2018-03-28 15:25:33 -0400
commit9070950eecded7bfa64e7bca3bb76b150ccc8b72 (patch)
tree509a9c669bf2679085e27a1ff1b0c95526abf14c /client
parent45a50d5afe013b31d9c94090d990bca49448d396 (diff)
parentd39bda76c4007c42452a81883fefc671b816a74b (diff)
Merge pull request #1638 from swaywm/swaybg-layers
Reimplement swaybg using surface layers
Diffstat (limited to 'client')
-rw-r--r--client/meson.build16
-rw-r--r--client/pool-buffer.c124
2 files changed, 140 insertions, 0 deletions
diff --git a/client/meson.build b/client/meson.build
new file mode 100644
index 00000000..2bdda457
--- /dev/null
+++ b/client/meson.build
@@ -0,0 +1,16 @@
+lib_sway_client = static_library(
+ 'sway-client',
+ files(
+ 'pool-buffer.c',
+ ),
+ dependencies: [
+ cairo,
+ gdk_pixbuf,
+ pango,
+ pangocairo,
+ wlroots,
+ wayland_client,
+ ],
+ link_with: [lib_sway_common],
+ include_directories: sway_inc
+)
diff --git a/client/pool-buffer.c b/client/pool-buffer.c
new file mode 100644
index 00000000..93cfcfc5
--- /dev/null
+++ b/client/pool-buffer.c
@@ -0,0 +1,124 @@
+#define _XOPEN_SOURCE 500
+#include <assert.h>
+#include <cairo/cairo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <pango/pangocairo.h>
+#include <unistd.h>
+#include <wayland-client.h>
+#include "config.h"
+#include "pool-buffer.h"
+
+static int create_pool_file(size_t size, char **name) {
+ static const char template[] = "sway-client-XXXXXX";
+ const char *path = getenv("XDG_RUNTIME_DIR");
+ if (!path) {
+ return -1;
+ }
+
+ int ts = (path[strlen(path) - 1] == '/');
+
+ *name = malloc(
+ strlen(template) +
+ strlen(path) +
+ (ts ? 0 : 1) + 1);
+ sprintf(*name, "%s%s%s", path, ts ? "" : "/", template);
+
+ int fd = mkstemp(*name);
+
+ if (fd < 0) {
+ return -1;
+ }
+
+ if (ftruncate(fd, size) < 0) {
+ close(fd);
+ return -1;
+ }
+
+ return fd;
+}
+
+static void buffer_release(void *data, struct wl_buffer *wl_buffer) {
+ struct pool_buffer *buffer = data;
+ buffer->busy = false;
+}
+
+static const struct wl_buffer_listener buffer_listener = {
+ .release = buffer_release
+};
+
+static struct pool_buffer *create_buffer(struct wl_shm *shm,
+ struct pool_buffer *buf, int32_t width, int32_t height,
+ uint32_t format) {
+ uint32_t stride = width * 4;
+ uint32_t size = stride * height;
+
+ char *name;
+ int fd = create_pool_file(size, &name);
+ assert(fd);
+ void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
+ buf->buffer = wl_shm_pool_create_buffer(pool, 0,
+ width, height, stride, format);
+ wl_shm_pool_destroy(pool);
+ close(fd);
+ unlink(name);
+ free(name);
+ fd = -1;
+
+ buf->width = width;
+ buf->height = height;
+ buf->surface = cairo_image_surface_create_for_data(data,
+ CAIRO_FORMAT_ARGB32, width, height, stride);
+ buf->cairo = cairo_create(buf->surface);
+ buf->pango = pango_cairo_create_context(buf->cairo);
+
+ wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
+ return buf;
+}
+
+static void destroy_buffer(struct pool_buffer *buffer) {
+ if (buffer->buffer) {
+ wl_buffer_destroy(buffer->buffer);
+ }
+ if (buffer->cairo) {
+ cairo_destroy(buffer->cairo);
+ }
+ if (buffer->surface) {
+ cairo_surface_destroy(buffer->surface);
+ }
+ if (buffer->pango) {
+ g_object_unref(buffer->pango);
+ }
+ memset(buffer, 0, sizeof(struct pool_buffer));
+}
+
+struct pool_buffer *get_next_buffer(struct wl_shm *shm,
+ struct pool_buffer pool[static 2], uint32_t width, uint32_t height) {
+ struct pool_buffer *buffer = NULL;
+
+ for (size_t i = 0; i < 2; ++i) {
+ if (pool[i].busy) {
+ continue;
+ }
+ buffer = &pool[i];
+ }
+
+ if (!buffer) {
+ return NULL;
+ }
+
+ if (buffer->width != width || buffer->height != height) {
+ destroy_buffer(buffer);
+ }
+
+ if (!buffer->buffer) {
+ if (!create_buffer(shm, buffer, width, height,
+ WL_SHM_FORMAT_ARGB8888)) {
+ return NULL;
+ }
+ }
+ return buffer;
+}