Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
Use _POSIX_C_SOURCE, use shm_open
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Nov 6, 2018
1 parent d7b0100 commit 2d0c5ec
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 61 deletions.
1 change: 0 additions & 1 deletion examples/dmabuf-capture.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 199309L
#include <libavformat/avformat.h>
#include <libavutil/display.h>
Expand Down
4 changes: 2 additions & 2 deletions examples/meson.build
Expand Up @@ -55,7 +55,7 @@ examples = {
},
'screenshot': {
'src': 'screenshot.c',
'dep': [wayland_client, wlr_protos, wlroots],
'dep': [wayland_client, wlr_protos, rt],
},
'idle': {
'src': 'idle.c',
Expand Down Expand Up @@ -96,7 +96,7 @@ examples = {
},
'screencopy': {
'src': 'screencopy.c',
'dep': [libpng, wayland_client, wlr_protos, wlroots],
'dep': [libpng, wayland_client, wlr_protos, rt],
},
'toplevel-decoration': {
'src': 'toplevel-decoration.c',
Expand Down
1 change: 0 additions & 1 deletion examples/output-layout.c
@@ -1,5 +1,4 @@
#define _POSIX_C_SOURCE 200112L
#define _XOPEN_SOURCE 700
#include <GLES2/gl2.h>
#include <limits.h>
#include <math.h>
Expand Down
31 changes: 11 additions & 20 deletions examples/screencopy.c
Expand Up @@ -21,8 +21,7 @@
* DEALINGS IN THE SOFTWARE.
*/

#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 199309L
#define _POSIX_C_SOURCE 200112L
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
Expand Down Expand Up @@ -65,33 +64,25 @@ static const struct format formats[] = {
{WL_SHM_FORMAT_ABGR8888, false},
};

static int backingfile(off_t size) {
char template[] = "/tmp/wlroots-shared-XXXXXX";
int fd = mkstemp(template);
static struct wl_buffer *create_shm_buffer(enum wl_shm_format fmt,
int width, int height, int stride, void **data_out) {
int size = stride * height;

const char shm_name[] = "/wlroots-screencopy";
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) {
// No-op
}
if (ret < 0) {
close(fd);
return -1;
}

unlink(template);
return fd;
}

static struct wl_buffer *create_shm_buffer(enum wl_shm_format fmt,
int width, int height, int stride, void **data_out) {
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;
}

Expand Down
36 changes: 12 additions & 24 deletions examples/screenshot.c
Expand Up @@ -21,8 +21,7 @@
* DEALINGS IN THE SOFTWARE.
*/

#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 199309L
#define _POSIX_C_SOURCE 200112L
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
Expand All @@ -35,7 +34,6 @@
#include <sys/wait.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wlr/util/log.h>
#include "screenshooter-client-protocol.h"

static struct wl_shm *shm = NULL;
Expand Down Expand Up @@ -111,34 +109,26 @@ 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) {
// No-op
}
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;
}

Expand Down Expand Up @@ -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");
Expand Down
1 change: 0 additions & 1 deletion types/data_device/wlr_data_device.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down
1 change: 0 additions & 1 deletion types/data_device/wlr_data_offer.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <stdlib.h>
#include <strings.h>
Expand Down
2 changes: 1 addition & 1 deletion types/data_device/wlr_data_source.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down
1 change: 0 additions & 1 deletion types/data_device/wlr_drag.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion types/wlr_input_device.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <wayland-server.h>
Expand Down
2 changes: 1 addition & 1 deletion types/wlr_primary_selection.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion xcursor/wlr_xcursor.c
Expand Up @@ -23,7 +23,7 @@
* SOFTWARE.
*/

#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 200809L
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
1 change: 0 additions & 1 deletion xwayland/selection/dnd.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion xwayland/selection/incoming.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down
1 change: 0 additions & 1 deletion xwayland/selection/outgoing.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion xwayland/selection/selection.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion xwayland/sockets.c
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#ifdef __FreeBSD__
// for SOCK_CLOEXEC
#define __BSD_VISIBLE 1
Expand Down
1 change: 0 additions & 1 deletion xwayland/xwayland.c
@@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 700
#define _DEFAULT_SOURCE
#ifdef __FreeBSD__
// for SOCK_CLOEXEC
Expand Down

0 comments on commit 2d0c5ec

Please sign in to comment.