From 2d0c5ec78eb975981b1d194fa9d4bc80b894748d Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 5 Nov 2018 22:51:23 +0100 Subject: Use _POSIX_C_SOURCE, use shm_open --- examples/screenshot.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'examples/screenshot.c') diff --git a/examples/screenshot.c b/examples/screenshot.c index aa4dcaa0..914f3994 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -21,8 +21,7 @@ * DEALINGS IN THE SOFTWARE. */ -#define _XOPEN_SOURCE 700 -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include #include #include @@ -35,7 +34,6 @@ #include #include #include -#include #include "screenshooter-client-protocol.h" static struct wl_shm *shm = NULL; @@ -111,12 +109,18 @@ static const struct wl_registry_listener registry_listener = { .global_remove = handle_global_remove, }; -static int backingfile(off_t size) { - char template[] = "/tmp/wlroots-shared-XXXXXX"; - int fd = mkstemp(template); +static struct wl_buffer *create_shm_buffer(int width, int height, + void **data_out) { + int stride = width * 4; + int size = stride * height; + + const char shm_name[] = "/wlroots-screenshot"; + int fd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0); if (fd < 0) { - return -1; + fprintf(stderr, "shm_open failed\n"); + return NULL; } + shm_unlink(shm_name); int ret; while ((ret = ftruncate(fd, size)) == EINTR) { @@ -124,21 +128,7 @@ static int backingfile(off_t size) { } if (ret < 0) { close(fd); - return -1; - } - - unlink(template); - return fd; -} - -static struct wl_buffer *create_shm_buffer(int width, int height, - void **data_out) { - int stride = width * 4; - int size = stride * height; - - int fd = backingfile(size); - if (fd < 0) { - fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); + fprintf(stderr, "ftruncate failed\n"); return NULL; } @@ -200,8 +190,6 @@ static void write_image(const char *filename, int width, int height, } int main(int argc, char *argv[]) { - wlr_log_init(WLR_DEBUG, NULL); - struct wl_display * display = wl_display_connect(NULL); if (display == NULL) { fprintf(stderr, "failed to create display: %m\n"); -- cgit v1.2.3