2017-03-11 04:41:24 +00:00
|
|
|
#define _XOPEN_SOURCE 500
|
2015-11-18 13:22:37 +00:00
|
|
|
#include <wayland-client.h>
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include "client/buffer.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
static int create_pool_file(size_t size, char **name) {
|
2015-12-15 21:56:48 +00:00
|
|
|
static const char template[] = "sway-client-XXXXXX";
|
2015-11-18 13:36:08 +00:00
|
|
|
const char *path = getenv("XDG_RUNTIME_DIR");
|
|
|
|
if (!path) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
int ts = (path[strlen(path) - 1] == '/');
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
*name = malloc(
|
2015-11-18 13:22:37 +00:00
|
|
|
strlen(template) +
|
|
|
|
strlen(path) +
|
2015-12-15 21:56:48 +00:00
|
|
|
(ts ? 0 : 1) + 1);
|
2015-11-18 13:36:08 +00:00
|
|
|
sprintf(*name, "%s%s%s", path, ts ? "" : "/", template);
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
int fd = mkstemp(*name);
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
if (ftruncate(fd, size) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
return fd;
|
2015-11-18 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_release(void *data, struct wl_buffer *wl_buffer) {
|
|
|
|
struct buffer *buffer = data;
|
|
|
|
buffer->busy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_buffer_listener buffer_listener = {
|
|
|
|
.release = buffer_release
|
|
|
|
};
|
|
|
|
|
2015-11-19 12:58:57 +00:00
|
|
|
static struct buffer *create_buffer(struct window *window, struct buffer *buf,
|
2016-09-05 15:36:48 +00:00
|
|
|
int32_t width, int32_t height, int32_t scale, uint32_t format) {
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2016-09-05 15:36:48 +00:00
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
2015-11-18 13:22:37 +00:00
|
|
|
uint32_t stride = width * 4;
|
|
|
|
uint32_t size = stride * height;
|
|
|
|
|
2015-11-18 13:36:08 +00:00
|
|
|
char *name;
|
|
|
|
int fd = create_pool_file(size, &name);
|
2015-11-19 00:58:38 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
sway_abort("Unable to allocate buffer");
|
2015-11-19 00:59:47 +00:00
|
|
|
return NULL; // never reached
|
2015-11-19 00:58:38 +00:00
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2015-11-19 12:58:57 +00:00
|
|
|
struct wl_shm_pool *pool = wl_shm_create_pool(window->registry->shm, fd, size);
|
2016-09-05 15:36:48 +00:00
|
|
|
buf->buffer = wl_shm_pool_create_buffer(pool, 0,
|
|
|
|
width, height, stride, format);
|
2015-11-18 13:22:37 +00:00
|
|
|
wl_shm_pool_destroy(pool);
|
|
|
|
close(fd);
|
2015-11-18 13:36:08 +00:00
|
|
|
unlink(name);
|
|
|
|
free(name);
|
2015-11-18 13:22:37 +00:00
|
|
|
fd = -1;
|
|
|
|
|
2015-11-18 14:55:55 +00:00
|
|
|
buf->width = width;
|
|
|
|
buf->height = height;
|
2016-09-05 15:36:48 +00:00
|
|
|
buf->surface = cairo_image_surface_create_for_data(data,
|
|
|
|
CAIRO_FORMAT_ARGB32, width, height, stride);
|
2015-11-18 13:22:37 +00:00
|
|
|
buf->cairo = cairo_create(buf->surface);
|
|
|
|
buf->pango = pango_cairo_create_context(buf->cairo);
|
|
|
|
|
2015-11-19 12:58:57 +00:00
|
|
|
wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
|
2015-11-18 13:22:37 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_buffer(struct buffer *buffer) {
|
2015-11-18 13:44:08 +00:00
|
|
|
if (buffer->buffer) {
|
|
|
|
wl_buffer_destroy(buffer->buffer);
|
|
|
|
}
|
|
|
|
if (buffer->cairo) {
|
|
|
|
cairo_destroy(buffer->cairo);
|
|
|
|
}
|
|
|
|
if (buffer->surface) {
|
|
|
|
cairo_surface_destroy(buffer->surface);
|
|
|
|
}
|
2016-11-28 13:45:27 +00:00
|
|
|
if (buffer->pango) {
|
|
|
|
g_object_unref(buffer->pango);
|
|
|
|
}
|
2015-11-18 13:44:08 +00:00
|
|
|
memset(buffer, 0, sizeof(struct buffer));
|
2015-11-18 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 12:58:57 +00:00
|
|
|
struct buffer *get_next_buffer(struct window *window) {
|
2015-11-18 13:44:08 +00:00
|
|
|
struct buffer *buffer = NULL;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 2; ++i) {
|
2015-11-19 12:58:57 +00:00
|
|
|
if (window->buffers[i].busy) {
|
2015-11-18 13:44:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-19 12:58:57 +00:00
|
|
|
buffer = &window->buffers[i];
|
2015-11-18 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 13:44:08 +00:00
|
|
|
if (!buffer) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-19 12:58:57 +00:00
|
|
|
if (buffer->width != window->width || buffer->height != window->height) {
|
2015-11-18 13:44:08 +00:00
|
|
|
destroy_buffer(buffer);
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
|
2015-11-18 13:44:08 +00:00
|
|
|
if (!buffer->buffer) {
|
2016-09-05 15:36:48 +00:00
|
|
|
if (!create_buffer(window, buffer,
|
|
|
|
window->width, window->height, window->scale,
|
|
|
|
WL_SHM_FORMAT_ARGB8888)) {
|
2015-11-18 13:44:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-18 13:22:37 +00:00
|
|
|
}
|
2015-11-18 13:44:08 +00:00
|
|
|
|
2015-11-19 12:58:57 +00:00
|
|
|
window->cairo = buffer->cairo;
|
|
|
|
window->buffer = buffer;
|
2015-11-18 13:44:08 +00:00
|
|
|
return buffer;
|
2015-11-18 13:22:37 +00:00
|
|
|
}
|