AtlatestRepositorysigil-ui

sigil-ui / tree / nativesurface-close.h

1/* CLOSEFB retires ownership without disabling an active scanout plane. */
2#ifndef SIGIL_UI_SURFACE_CLOSE_H
3#define SIGIL_UI_SURFACE_CLOSE_H
4
5#include <errno.h>
6#include <stdint.h>
7#include <sys/ioctl.h>
8#include <sys/mman.h>
9#include <unistd.h>
10#include <drm/drm.h>
11#include <drm/drm_mode.h>
13/* Linux 6.8 UAPI, also usable with older libdrm headers. */
14#ifndef DRM_IOCTL_MODE_CLOSEFB
15struct drm_mode_closefb { uint32_t fb_id, pad; };
16#define DRM_IOCTL_MODE_CLOSEFB DRM_IOWR(0xD0, struct drm_mode_closefb)
17#endif
19static int preserve_frame(int fd, uint32_t framebuffer)
21 if (!framebuffer) return 0;
22 struct drm_mode_closefb request = { .fb_id = framebuffer };
23 int result;
24 do {
25 result = ioctl(fd, DRM_IOCTL_MODE_CLOSEFB, &request);
26 } while (result < 0 && errno == EINTR);
27 return result;
30static int close_surface(int *fd, uint32_t *framebuffer, uint8_t **map, size_t size, int keep_frame)
32 if (*fd >= 0 && keep_frame && preserve_frame(*fd, *framebuffer) < 0) return -1;
33 if (*map) { munmap(*map, size); *map = NULL; }
34 if (*fd >= 0) { close(*fd); *fd = -1; }
35 *framebuffer = 0;
36 return 0;
38#endif