update drwl & move to bufpool, makes dam work again in river

This commit is contained in:
sewn 2024-08-30 17:43:36 +03:00
parent 0e94de9304
commit 5d159d7818
No known key found for this signature in database
6 changed files with 306 additions and 236 deletions

163
bufpool.h Normal file
View file

@ -0,0 +1,163 @@
/*
* bufpool - https://codeberg.org/sewn/drwl
*
* Copyright (c) 2023-2024 sewn <sewn@disroot.org>
*
* 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 <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <wayland-client.h>
#ifdef __linux__
#include <linux/memfd.h>
#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]);
}

View file

@ -15,6 +15,7 @@ pub fn build(b: *Build) void {
dam.addIncludePath(b.path(""));
dam.addCSourceFile(.{
.file = b.path("dam.c"),
.flags = &.{"-D_GNU_SOURCE"},
});
dam.linkLibC();

View file

@ -1,11 +0,0 @@
.{
.name = "dam",
.version = "0.0.0",
.dependencies = .{
.@"zig-wayland" = .{
.url = "https://codeberg.org/ifreund/zig-wayland/archive/v0.2.0.tar.gz",
.hash = "1220687c8c47a48ba285d26a05600f8700d37fc637e223ced3aa8324f3650bf52242",
},
},
.paths = .{""},
}

20
dam.c
View file

@ -14,7 +14,7 @@
#include <wayland-client.h>
#include "drwl.h"
#include "poolbuf.h"
#include "bufpool.h"
#include "river-status-unstable-v1-protocol.h"
#include "river-control-unstable-v1-protocol.h"
#include "wlr-layer-shell-unstable-v1-protocol.h"
@ -34,6 +34,7 @@ typedef struct {
struct zwlr_layer_surface_v1 *layer_surface;
struct zriver_output_status_v1 *river_output_status;
Drwl *drw;
BufPool pool;
uint32_t width, height, scale;
int lrpad;
@ -110,9 +111,9 @@ bar_load_fonts(Bar *bar)
{
char fontattrs[12];
drwl_destroy_font(bar->drw->font);
drwl_font_destroy(bar->drw->font);
snprintf(fontattrs, sizeof(fontattrs), "dpi=%d", 96 * bar->scale);
if (!(drwl_load_font(bar->drw, LENGTH(fonts), fonts, fontattrs)))
if (!(drwl_font_create(bar->drw, LENGTH(fonts), fonts, fontattrs)))
die("failed to load fonts");
bar->lrpad = bar->drw->font->height;
bar->height = bar->drw->font->height + 2;
@ -126,15 +127,15 @@ bar_draw(Bar *bar)
int x = 0, w;
int boxs = bar->drw->font->height / 9;
int boxw = bar->drw->font->height / 6 + 2;
PoolBuf *buf;
DrwBuf *buf;
Seat *seat;
if (bar->width < 1 || bar->height < 1)
return;
if (!(buf = poolbuf_create(shm, bar->width, bar->height)))
die("poolbuf_create:");
drwl_prepare_drawing(bar->drw, bar->width, bar->height, buf->data, buf->stride);
if (!(buf = bufpool_getbuf(&bar->pool, shm, bar->width, bar->height)))
die(errno ? "bufpool_getbuf:" : "no buffer available");
drwl_setimage(bar->drw, buf->image);
drwl_setscheme(bar->drw, colors[SchemeNorm]);
/* draw status first so it can be overdrawn by tags later */
@ -178,11 +179,10 @@ bar_draw(Bar *bar)
}
}
drwl_finish_drawing(bar->drw);
drwl_setimage(bar->drw, NULL);
wl_surface_set_buffer_scale(bar->surface, bar->scale);
wl_surface_attach(bar->surface, buf->wl_buf, 0, 0);
wl_surface_damage_buffer(bar->surface, 0, 0, bar->width, bar->height);
poolbuf_destroy(buf);
wl_surface_commit(bar->surface);
}
@ -198,9 +198,11 @@ bars_draw()
static void
bar_destroy(Bar *bar)
{
bufpool_cleanup(&bar->pool);
wl_list_remove(&bar->link);
free(bar->layout);
free(bar->title);
drwl_setimage(bar->drw, NULL);
drwl_destroy(bar->drw);
zriver_output_status_v1_destroy(bar->river_output_status);
zwlr_layer_surface_v1_destroy(bar->layer_surface);

235
drwl.h
View file

@ -1,6 +1,31 @@
/*
* drwl - https://codeberg.org/sewn/drwl
* See LICENSE file for copyright and license details.
*
* Copyright (c) 2023-2024 sewn <sewn@disroot.org>
* Copyright (c) 2024 notchoc <notchoc@disroot.org>
*
* 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 <bjoern@hoehrmann.de>
* See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
*/
#pragma once
@ -8,56 +33,49 @@
#include <fcft/fcft.h>
#include <pixman-1/pixman.h>
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
enum { ColFg, ColBg }; /* colorscheme index */
typedef struct fcft_font Fnt;
typedef pixman_image_t Img;
typedef struct {
pixman_image_t *pix;
struct fcft_font *font;
Img *image;
Fnt *font;
uint32_t *scheme;
} Drwl;
#define UTF_INVALID 0xFFFD
#define UTF_SIZ 4
#define UTF8_ACCEPT 0
#define UTF8_REJECT 12
#define UTF8_INVALID 0xFFFD
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const uint32_t utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static const uint32_t utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
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
utf8decodebyte(const char c, size_t *i)
utf8decode(uint32_t *state, uint32_t *codep, uint8_t byte)
{
for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
return (unsigned char)c & ~utfmask[*i];
return 0;
}
uint32_t type = utf8d[byte];
static inline size_t
utf8decode(const char *c, uint32_t *u)
{
size_t i, j, len, type;
uint32_t udecoded;
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*u = UTF_INVALID;
udecoded = utf8decodebyte(c[0], &len);
if (!BETWEEN(len, 1, UTF_SIZ))
return 1;
for (i = 1, j = 1; i < UTF_SIZ && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
if (type)
return j;
}
if (j < len)
return 0;
*u = udecoded;
if (!BETWEEN(*u, utfmin[len], utfmax[len]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
for (i = 1; *u > utfmax[i]; ++i)
;
return len;
*state = utf8d[256 + *state + type];
return *state;
}
static int
@ -67,10 +85,6 @@ drwl_init(void)
return fcft_init(FCFT_LOG_COLORIZE_AUTO, 0, FCFT_LOG_CLASS_ERROR);
}
/*
* Caller must call drwl_init before drwl_create, and
* drwl_destroy on returned context after finalizing usage.
*/
static Drwl *
drwl_create(void)
{
@ -83,29 +97,31 @@ drwl_create(void)
}
static void
drwl_setfont(Drwl *drwl, struct fcft_font *font)
drwl_setfont(Drwl *drwl, Fnt *font)
{
if (drwl)
drwl->font = font;
}
/*
* Returned font is set within the drawing context if given.
* Caller must call drwl_destroy_font on returned font when done using it,
* otherwise use drwl_destroy when instead given a drwl context.
*/
static struct fcft_font *
drwl_load_font(Drwl *drwl, size_t fontcount,
const char *fonts[static fontcount], const char *attributes)
static void
drwl_setimage(Drwl *drwl, Img *image)
{
struct fcft_font *font = fcft_from_name(fontcount, fonts, attributes);
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_destroy_font(struct fcft_font *font)
drwl_font_destroy(Fnt *font)
{
fcft_destroy(font);
}
@ -114,9 +130,9 @@ static inline pixman_color_t
convert_color(uint32_t clr)
{
return (pixman_color_t){
((clr >> 24) & 0xFF) * 0x101,
((clr >> 16) & 0xFF) * 0x101,
((clr >> 8) & 0xFF) * 0x101,
((clr >> 24) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
((clr >> 16) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
((clr >> 8) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
(clr & 0xFF) * 0x101
};
}
@ -128,30 +144,23 @@ drwl_setscheme(Drwl *drwl, uint32_t *scm)
drwl->scheme = scm;
}
static inline int
drwl_stride(unsigned int width)
{
return (((PIXMAN_FORMAT_BPP(PIXMAN_a8r8g8b8) * width + 7) / 8 + 4 - 1) & -4);
}
/*
* Caller must call drwl_finish_drawing when finished drawing.
* Parameter stride can be calculated using drwl_stride.
*/
static void
drwl_prepare_drawing(Drwl *drwl, unsigned int w, unsigned int h,
uint32_t *bits, int stride)
static Img *
drwl_image_create(Drwl *drwl, unsigned int w, unsigned int h, uint32_t *bits)
{
Img *image;
pixman_region32_t clip;
if (!drwl)
return;
drwl->pix = pixman_image_create_bits_no_clear(
PIXMAN_a8r8g8b8, w, h, bits, stride);
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(drwl->pix, &clip);
pixman_image_set_clip_region32(image, &clip);
pixman_region32_fini(&clip);
if (drwl)
drwl_setimage(drwl, image);
return image;
}
static void
@ -160,15 +169,15 @@ drwl_rect(Drwl *drwl,
int filled, int invert)
{
pixman_color_t clr;
if (!drwl || !drwl->scheme || !drwl->pix)
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->pix, &clr, 1,
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->pix, &clr, 4,
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 },
@ -182,15 +191,16 @@ drwl_text(Drwl *drwl,
unsigned int lpad, const char *text, int invert)
{
int ty;
int utf8charlen, render = x || y || w || h;
int render = x || y || w || h;
long x_kern;
uint32_t cp = 0, last_cp = 0;
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;
const struct fcft_glyph *glyph, *eg = NULL;
int fcft_subpixel_mode = FCFT_SUBPIXEL_DEFAULT;
if (!drwl || (render && (!drwl->scheme || !w || !drwl->pix)) || !text || !drwl->font)
if (!drwl || (render && (!drwl->scheme || !w || !drwl->image)) || !text || !drwl->font)
return 0;
if (!render) {
@ -205,13 +215,23 @@ drwl_text(Drwl *drwl,
w -= lpad;
}
// U+2026 == …
eg = fcft_rasterize_char_utf32(drwl->font, 0x2026, FCFT_SUBPIXEL_DEFAULT);
if (render && (drwl->scheme[ColBg] & 0xFF) != 0xFF)
fcft_subpixel_mode = FCFT_SUBPIXEL_NONE;
while (*text) {
utf8charlen = utf8decode(text, &cp);
if (render)
eg = fcft_rasterize_char_utf32(drwl->font, 0x2026 /* … */, fcft_subpixel_mode);
glyph = fcft_rasterize_char_utf32(drwl->font, cp, FCFT_SUBPIXEL_DEFAULT);
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;
@ -222,15 +242,15 @@ drwl_text(Drwl *drwl,
ty = y + (h - drwl->font->height) / 2 + drwl->font->ascent;
/* draw ellipsis if remaining text doesn't fit */
if (!noellipsis && x_kern + glyph->advance.x + eg->advance.x > w && *(text + 1) != '\0') {
if (drwl_text(drwl, 0, 0, 0, 0, 0, text, 0)
- glyph->advance.x < eg->advance.x) {
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->pix, 0, 0, 0, 0,
PIXMAN_OP_OVER, fg_pix, eg->pix, drwl->image, 0, 0, 0, 0,
x + eg->x, ty - eg->y, eg->width, eg->height);
}
}
@ -241,16 +261,15 @@ drwl_text(Drwl *drwl,
x += x_kern;
if (render && pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)
// pre-rendered glyphs (eg. emoji)
/* pre-rendered glyphs (eg. emoji) */
pixman_image_composite32(
PIXMAN_OP_OVER, glyph->pix, NULL, drwl->pix, 0, 0, 0, 0,
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->pix, 0, 0, 0, 0,
PIXMAN_OP_OVER, fg_pix, glyph->pix, drwl->image, 0, 0, 0, 0,
x + glyph->x, ty - glyph->y, glyph->width, glyph->height);
text += utf8charlen;
x += glyph->advance.x;
w -= glyph->advance.x;
}
@ -269,20 +288,28 @@ drwl_font_getwidth(Drwl *drwl, const char *text)
return drwl_text(drwl, 0, 0, 0, 0, 0, text, 0);
}
static void
drwl_finish_drawing(Drwl *drwl)
static unsigned int
drwl_font_getwidth_clamp(Drwl *drwl, const char *text, unsigned int n)
{
if (drwl && drwl->pix)
pixman_image_unref(drwl->pix);
unsigned int tmp = 0;
if (drwl && drwl->font && text && n)
tmp = drwl_text(drwl, 0, 0, 0, 0, 0, text, n);
return tmp < n ? tmp : n;
}
static void
drwl_image_destroy(Img *image)
{
pixman_image_unref(image);
}
static void
drwl_destroy(Drwl *drwl)
{
if (drwl->pix)
pixman_image_unref(drwl->pix);
if (drwl->font)
drwl_destroy_font(drwl->font);
drwl_font_destroy(drwl->font);
if (drwl->image)
drwl_image_destroy(drwl->image);
free(drwl);
}

112
poolbuf.h
View file

@ -1,112 +0,0 @@
/*
* poolbuf - https://codeberg.org/sewn/drwl
* See LICENSE file for copyright and license details.
*/
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <wayland-client.h>
#include "drwl.h"
typedef struct {
struct wl_buffer *wl_buf;
int32_t stride;
int32_t size;
void *data;
} PoolBuf;
#if defined(__linux__) || defined(___FreeBSD__)
#ifdef __linux__
#include <linux/memfd.h>
#endif
#define __POOLBUF_HAS_MEMFD_CREATE
int memfd_create(const char *, unsigned);
#else
static void
randname(char *buf)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long r = ts.tv_nsec;
for (int i = 0; i < 6; ++i) {
buf[i] = 'A'+(r&15)+(r&16)*2;
r >>= 5;
}
}
static int
create_shm(void)
{
char name[] = "/drwl-XXXXXX";
int retries = 100;
do {
randname(name + sizeof(name) - 7);
--retries;
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);
return -1;
}
#endif
/* Caller must call poolbuf_destroy after finalizing usage */
static PoolBuf *
poolbuf_create(struct wl_shm *shm, int32_t width, int32_t height)
{
int fd;
void *data;
struct wl_shm_pool *shm_pool;
struct wl_buffer *wl_buf;
int32_t stride = drwl_stride(width);
int32_t size = stride * height;
PoolBuf *buf;
#ifdef __POOLBUF_HAS_MEMFD_CREATE
fd = memfd_create("drwl-shm-buffer-pool",
MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
#else
fd = create_shm();
#endif
if (fd < 0)
return NULL;
if ((ftruncate(fd, size)) < 0) {
close(fd);
return NULL;
}
data = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
close(fd);
return NULL;
}
shm_pool = wl_shm_create_pool(shm, fd, size);
wl_buf = wl_shm_pool_create_buffer(shm_pool, 0,
width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(shm_pool);
close(fd);
buf = calloc(1, sizeof(PoolBuf));
buf->wl_buf = wl_buf;
buf->stride = stride;
buf->size = size;
buf->data = data;
return buf;
}
static void
poolbuf_destroy(PoolBuf *buf)
{
wl_buffer_destroy(buf->wl_buf);
munmap(buf->data, buf->size);
free(buf);
}