diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e4221ef --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "drwl"] + path = drwl + url = https://codeberg.org/sewn/drwl diff --git a/Makefile b/Makefile index ffc0187..925cf81 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ INCS != $(PKG_CONFIG) --cflags $(PKGS) LIBS != $(PKG_CONFIG) --libs $(PKGS) CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE -CFLAGS += -pedantic -Wall $(INCS) +CFLAGS += -pedantic -Wall -Wno-unused-function -Wno-strict-prototypes $(INCS) LDLIBS = $(LIBS) PROTO = xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h \ diff --git a/bufpool.h b/bufpool.h deleted file mode 100644 index af672c4..0000000 --- a/bufpool.h +++ /dev/null @@ -1,163 +0,0 @@ -/* - * bufpool - https://codeberg.org/sewn/drwl - * - * Copyright (c) 2023-2024 sewn - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#include -#include -#include -#include -#include -#include -#include -#ifdef __linux__ -#include -#endif - -#include "drwl.h" - -typedef struct { - struct wl_buffer *wl_buf; - int32_t size; - int busy; - void *mmapped; - Img *image; -} DrwBuf; - -typedef struct { - DrwBuf bufs[2]; -} BufPool; - -static void -drwbuf_cleanup(DrwBuf *buf) -{ - if (buf->wl_buf) - wl_buffer_destroy(buf->wl_buf); - if (buf->mmapped) - munmap(buf->mmapped, buf->size); - if (buf->image) - drwl_image_destroy(buf->image); - *buf = (DrwBuf){0}; -} - -static void -drwbuf_handle_release(void *data, struct wl_buffer *wl_buffer) -{ - DrwBuf *buf = data; - buf->busy = 0; -} - -static struct wl_buffer_listener drwbuf_buffer_listener = { - .release = drwbuf_handle_release, -}; - -static DrwBuf * -bufpool_getbuf(BufPool *pool, struct wl_shm *shm, - int32_t width, int32_t height) -{ - int i; - int fd; - void *mmapped; - struct wl_shm_pool *shm_pool; - struct wl_buffer *wl_buf; - int32_t size = width * 4 * height; - Img *image; - DrwBuf *buf = NULL; - - for (i = 0; i < 2; i++) { - if (pool->bufs[i].busy) - continue; - if (pool->bufs[i].wl_buf && pool->bufs[i].size != size) - drwbuf_cleanup(&pool->bufs[i]); - buf = &pool->bufs[i]; - } - if (!buf) - return NULL; - if (buf->wl_buf && buf->size == size) - return buf; - -#if defined(__linux__) || \ - ((defined(__FreeBSD__) && (__FreeBSD_version >= 1300048))) - fd = memfd_create("drwbuf-shm-buffer-pool", - MFD_CLOEXEC | MFD_ALLOW_SEALING | -#if defined(MFD_NOEXEC_SEAL) - MFD_NOEXEC_SEAL -#else - 0 -#endif - ); -#else - char template[] = "/tmp/drwbuf-XXXXXX"; -#if defined(__OpenBSD__) - fd = shm_mkstemp(template); -#else - fd = mkostemp(template, O_CLOEXEC); -#endif - if (fd < 0) - return NULL; -#if defined(__OpenBSD__) - shm_unlink(template); -#else - unlink(template); -#endif -#endif - - if ((ftruncate(fd, size)) < 0) { - close(fd); - return NULL; - } - - mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (mmapped == MAP_FAILED) { - close(fd); - return NULL; - } - -#if defined(__linux__) || \ - ((defined(__FreeBSD__) && (__FreeBSD_version >= 1300048))) - fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL); -#endif - - shm_pool = wl_shm_create_pool(shm, fd, size); - wl_buf = wl_shm_pool_create_buffer(shm_pool, 0, - width, height, width * 4, WL_SHM_FORMAT_ARGB8888); - wl_shm_pool_destroy(shm_pool); - close(fd); - - buf->wl_buf = wl_buf; - buf->size = size; - buf->mmapped = mmapped; - buf->busy = 1; - if (!(image = drwl_image_create(NULL, width, height, buf->mmapped))) - drwbuf_cleanup(buf); - buf->image = image; - wl_buffer_add_listener(wl_buf, &drwbuf_buffer_listener, buf); - return buf; -} - -static void -bufpool_cleanup(BufPool *pool) -{ - int i; - for (i = 0; i < 2; i++) - drwbuf_cleanup(&pool->bufs[i]); -} diff --git a/dam.c b/dam.c index ce19928..08c7911 100644 --- a/dam.c +++ b/dam.c @@ -13,8 +13,8 @@ #include #include -#include "drwl.h" -#include "bufpool.h" +#include "drwl/drwl.h" +#include "drwl/bufpool.h" #include "river-status-unstable-v1-protocol.h" #include "river-control-unstable-v1-protocol.h" #include "wlr-layer-shell-unstable-v1-protocol.h" diff --git a/drwl b/drwl new file mode 160000 index 0000000..28785d1 --- /dev/null +++ b/drwl @@ -0,0 +1 @@ +Subproject commit 28785d10a350217b4cd067a0175e6ff6d06cc821 diff --git a/drwl.h b/drwl.h deleted file mode 100644 index 4e7a65e..0000000 --- a/drwl.h +++ /dev/null @@ -1,311 +0,0 @@ -/* - * drwl - https://codeberg.org/sewn/drwl - * - * Copyright (c) 2023-2024 sewn - * Copyright (c) 2024 notchoc - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * The UTF-8 Decoder included is from Bjoern Hoehrmann: - * Copyright (c) 2008-2010 Bjoern Hoehrmann - * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. - */ -#pragma once - -#include -#include -#include - -enum { ColFg, ColBg }; /* colorscheme index */ - -typedef struct fcft_font Fnt; -typedef pixman_image_t Img; - -typedef struct { - Img *image; - Fnt *font; - uint32_t *scheme; -} Drwl; - -#define UTF8_ACCEPT 0 -#define UTF8_REJECT 12 -#define UTF8_INVALID 0xFFFD - -static const uint8_t utf8d[] = { - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, - 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, - - 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12, - 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12, - 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12, - 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12, - 12,36,12,12,12,12,12,12,12,12,12,12, -}; - -static inline uint32_t -utf8decode(uint32_t *state, uint32_t *codep, uint8_t byte) -{ - uint32_t type = utf8d[byte]; - - *codep = (*state != UTF8_ACCEPT) ? - (byte & 0x3fu) | (*codep << 6) : - (0xff >> type) & (byte); - - *state = utf8d[256 + *state + type]; - return *state; -} - -static int -drwl_init(void) -{ - fcft_set_scaling_filter(FCFT_SCALING_FILTER_LANCZOS3); - return fcft_init(FCFT_LOG_COLORIZE_AUTO, 0, FCFT_LOG_CLASS_ERROR); -} - -static Drwl * -drwl_create(void) -{ - Drwl *drwl; - - if (!(drwl = calloc(1, sizeof(Drwl)))) - return NULL; - - return drwl; -} - -static void -drwl_setfont(Drwl *drwl, Fnt *font) -{ - if (drwl) - drwl->font = font; -} - -static void -drwl_setimage(Drwl *drwl, Img *image) -{ - if (drwl) - drwl->image = image; -} - -static Fnt * -drwl_font_create(Drwl *drwl, size_t count, - const char *names[static count], const char *attributes) -{ - Fnt *font = fcft_from_name(count, names, attributes); - if (drwl) - drwl_setfont(drwl, font); - return font; -} - -static void -drwl_font_destroy(Fnt *font) -{ - fcft_destroy(font); -} - -static inline pixman_color_t -convert_color(uint32_t clr) -{ - return (pixman_color_t){ - ((clr >> 24) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF, - ((clr >> 16) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF, - ((clr >> 8) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF, - (clr & 0xFF) * 0x101 - }; -} - -static void -drwl_setscheme(Drwl *drwl, uint32_t *scm) -{ - if (drwl) - drwl->scheme = scm; -} - -static Img * -drwl_image_create(Drwl *drwl, unsigned int w, unsigned int h, uint32_t *bits) -{ - Img *image; - pixman_region32_t clip; - - image = pixman_image_create_bits_no_clear( - PIXMAN_a8r8g8b8, w, h, bits, w * 4); - if (!image) - return NULL; - pixman_region32_init_rect(&clip, 0, 0, w, h); - pixman_image_set_clip_region32(image, &clip); - pixman_region32_fini(&clip); - - if (drwl) - drwl_setimage(drwl, image); - return image; -} - -static void -drwl_rect(Drwl *drwl, - int x, int y, unsigned int w, unsigned int h, - int filled, int invert) -{ - pixman_color_t clr; - if (!drwl || !drwl->scheme || !drwl->image) - return; - - clr = convert_color(drwl->scheme[invert ? ColBg : ColFg]); - if (filled) - pixman_image_fill_rectangles(PIXMAN_OP_SRC, drwl->image, &clr, 1, - &(pixman_rectangle16_t){x, y, w, h}); - else - pixman_image_fill_rectangles(PIXMAN_OP_SRC, drwl->image, &clr, 4, - (pixman_rectangle16_t[4]){ - { x, y, w, 1 }, - { x, y + h - 1, w, 1 }, - { x, y, 1, h }, - { x + w - 1, y, 1, h }}); -} - -static int -drwl_text(Drwl *drwl, - int x, int y, unsigned int w, unsigned int h, - unsigned int lpad, const char *text, int invert) -{ - int ty; - int render = x || y || w || h; - long x_kern; - uint32_t cp = 0, last_cp = 0, state; - pixman_color_t clr; - pixman_image_t *fg_pix = NULL; - int noellipsis = 0; - const struct fcft_glyph *glyph, *eg = NULL; - int fcft_subpixel_mode = FCFT_SUBPIXEL_DEFAULT; - - if (!drwl || (render && (!drwl->scheme || !w || !drwl->image)) || !text || !drwl->font) - return 0; - - if (!render) { - w = invert ? invert : ~invert; - } else { - clr = convert_color(drwl->scheme[invert ? ColBg : ColFg]); - fg_pix = pixman_image_create_solid_fill(&clr); - - drwl_rect(drwl, x, y, w, h, 1, !invert); - - x += lpad; - w -= lpad; - } - - if (render && (drwl->scheme[ColBg] & 0xFF) != 0xFF) - fcft_subpixel_mode = FCFT_SUBPIXEL_NONE; - - if (render) - eg = fcft_rasterize_char_utf32(drwl->font, 0x2026 /* … */, fcft_subpixel_mode); - - for (const char *p = text, *pp; pp = p, *p; p++) { - for (state = UTF8_ACCEPT; *p && - utf8decode(&state, &cp, *p) > UTF8_REJECT; p++) - ; - if (!*p || state == UTF8_REJECT) { - cp = UTF8_INVALID; - if (p > pp) - p--; - } - - glyph = fcft_rasterize_char_utf32(drwl->font, cp, fcft_subpixel_mode); - if (!glyph) - continue; - - x_kern = 0; - if (last_cp) - fcft_kerning(drwl->font, last_cp, cp, &x_kern, NULL); - last_cp = cp; - - ty = y + (h - drwl->font->height) / 2 + drwl->font->ascent; - - if (render && !noellipsis && x_kern + glyph->advance.x + eg->advance.x > w && - *(p + 1) != '\0') { - /* cannot fit ellipsis after current codepoint */ - if (drwl_text(drwl, 0, 0, 0, 0, 0, pp, 0) + x_kern <= w) { - noellipsis = 1; - } else { - w -= eg->advance.x; - pixman_image_composite32( - PIXMAN_OP_OVER, fg_pix, eg->pix, drwl->image, 0, 0, 0, 0, - x + eg->x, ty - eg->y, eg->width, eg->height); - } - } - - if ((x_kern + glyph->advance.x) > w) - break; - - x += x_kern; - - if (render && pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8) - /* pre-rendered glyphs (eg. emoji) */ - pixman_image_composite32( - PIXMAN_OP_OVER, glyph->pix, NULL, drwl->image, 0, 0, 0, 0, - x + glyph->x, ty - glyph->y, glyph->width, glyph->height); - else if (render) - pixman_image_composite32( - PIXMAN_OP_OVER, fg_pix, glyph->pix, drwl->image, 0, 0, 0, 0, - x + glyph->x, ty - glyph->y, glyph->width, glyph->height); - - x += glyph->advance.x; - w -= glyph->advance.x; - } - - if (render) - pixman_image_unref(fg_pix); - - return x + (render ? w : 0); -} - -static unsigned int -drwl_font_getwidth(Drwl *drwl, const char *text) -{ - if (!drwl || !drwl->font || !text) - return 0; - return drwl_text(drwl, 0, 0, 0, 0, 0, text, 0); -} - -static void -drwl_image_destroy(Img *image) -{ - pixman_image_unref(image); -} - -static void -drwl_destroy(Drwl *drwl) -{ - if (drwl->font) - drwl_font_destroy(drwl->font); - if (drwl->image) - drwl_image_destroy(drwl->image); - free(drwl); -} - -static void -drwl_fini(void) -{ - fcft_fini(); -}