2008-12-02 20:15:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
2015-06-11 05:48:59 +00:00
|
|
|
* 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:
|
2008-12-02 20:15:01 +00:00
|
|
|
*
|
2015-06-11 05:48:59 +00:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) 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.
|
2008-12-02 20:15:01 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-13 04:45:19 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-11-24 04:41:08 +00:00
|
|
|
#include <stdint.h>
|
2013-08-13 04:45:19 +00:00
|
|
|
#include <errno.h>
|
2008-11-24 04:41:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2011-05-02 16:11:07 +00:00
|
|
|
#include <unistd.h>
|
2012-04-20 19:37:35 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/param.h>
|
2011-05-02 16:11:07 +00:00
|
|
|
#include <sys/mman.h>
|
2012-03-12 05:05:25 +00:00
|
|
|
#include <cairo.h>
|
2008-11-24 04:41:08 +00:00
|
|
|
|
2011-11-22 12:18:50 +00:00
|
|
|
#include <wayland-client.h>
|
2015-11-17 08:00:34 +00:00
|
|
|
#include "weston-screenshooter-client-protocol.h"
|
2015-06-15 22:37:07 +00:00
|
|
|
#include "shared/os-compatibility.h"
|
2016-03-16 21:15:18 +00:00
|
|
|
#include "shared/xalloc.h"
|
2008-11-24 04:41:08 +00:00
|
|
|
|
|
|
|
/* The screenshooter is a good example of a custom object exposed by
|
|
|
|
* the compositor and serves as a test bed for implementing client
|
|
|
|
* side marshalling outside libwayland.so */
|
|
|
|
|
2011-05-02 16:11:07 +00:00
|
|
|
static struct wl_shm *shm;
|
2015-11-17 08:00:34 +00:00
|
|
|
static struct weston_screenshooter *screenshooter;
|
2012-04-04 17:49:21 +00:00
|
|
|
static struct wl_list output_list;
|
2012-04-20 19:37:35 +00:00
|
|
|
int min_x, min_y, max_x, max_y;
|
2012-04-20 19:37:33 +00:00
|
|
|
int buffer_copy_done;
|
2012-04-04 17:49:21 +00:00
|
|
|
|
|
|
|
struct screenshooter_output {
|
|
|
|
struct wl_output *output;
|
|
|
|
struct wl_buffer *buffer;
|
|
|
|
int width, height, offset_x, offset_y;
|
2012-04-20 19:37:34 +00:00
|
|
|
void *data;
|
2012-04-04 17:49:21 +00:00
|
|
|
struct wl_list link;
|
|
|
|
};
|
2011-05-02 16:11:07 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
display_handle_geometry(void *data,
|
2011-06-21 15:16:58 +00:00
|
|
|
struct wl_output *wl_output,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int physical_width,
|
|
|
|
int physical_height,
|
|
|
|
int subpixel,
|
|
|
|
const char *make,
|
2012-07-22 19:49:57 +00:00
|
|
|
const char *model,
|
|
|
|
int transform)
|
2011-06-21 15:16:58 +00:00
|
|
|
{
|
2012-04-04 17:49:21 +00:00
|
|
|
struct screenshooter_output *output;
|
|
|
|
|
|
|
|
output = wl_output_get_user_data(wl_output);
|
|
|
|
|
|
|
|
if (wl_output == output->output) {
|
|
|
|
output->offset_x = x;
|
|
|
|
output->offset_y = y;
|
|
|
|
}
|
2011-06-21 15:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_handle_mode(void *data,
|
|
|
|
struct wl_output *wl_output,
|
|
|
|
uint32_t flags,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int refresh)
|
2011-05-02 16:11:07 +00:00
|
|
|
{
|
2012-04-04 17:49:21 +00:00
|
|
|
struct screenshooter_output *output;
|
|
|
|
|
|
|
|
output = wl_output_get_user_data(wl_output);
|
|
|
|
|
2012-04-04 18:52:35 +00:00
|
|
|
if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
|
2012-04-04 17:49:21 +00:00
|
|
|
output->width = width;
|
|
|
|
output->height = height;
|
|
|
|
}
|
2011-05-02 16:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_output_listener output_listener = {
|
|
|
|
display_handle_geometry,
|
2011-06-21 15:16:58 +00:00
|
|
|
display_handle_mode
|
2011-05-02 16:11:07 +00:00
|
|
|
};
|
|
|
|
|
2012-04-20 19:37:33 +00:00
|
|
|
static void
|
2015-11-17 08:00:34 +00:00
|
|
|
screenshot_done(void *data, struct weston_screenshooter *screenshooter)
|
2012-04-20 19:37:33 +00:00
|
|
|
{
|
|
|
|
buffer_copy_done = 1;
|
|
|
|
}
|
|
|
|
|
2015-11-17 08:00:34 +00:00
|
|
|
static const struct weston_screenshooter_listener screenshooter_listener = {
|
2012-04-20 19:37:33 +00:00
|
|
|
screenshot_done
|
|
|
|
};
|
|
|
|
|
2008-11-24 04:41:08 +00:00
|
|
|
static void
|
2012-10-11 01:34:26 +00:00
|
|
|
handle_global(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name, const char *interface, uint32_t version)
|
2008-11-24 04:41:08 +00:00
|
|
|
{
|
2012-04-04 17:49:21 +00:00
|
|
|
static struct screenshooter_output *output;
|
|
|
|
|
2011-05-02 16:11:07 +00:00
|
|
|
if (strcmp(interface, "wl_output") == 0) {
|
2013-08-07 22:34:59 +00:00
|
|
|
output = xmalloc(sizeof *output);
|
2012-10-11 01:34:26 +00:00
|
|
|
output->output = wl_registry_bind(registry, name,
|
|
|
|
&wl_output_interface, 1);
|
2012-04-04 17:49:21 +00:00
|
|
|
wl_list_insert(&output_list, &output->link);
|
|
|
|
wl_output_add_listener(output->output, &output_listener, output);
|
2011-05-02 16:11:07 +00:00
|
|
|
} else if (strcmp(interface, "wl_shm") == 0) {
|
2012-10-11 01:34:26 +00:00
|
|
|
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
2015-11-17 08:00:34 +00:00
|
|
|
} else if (strcmp(interface, "weston_screenshooter") == 0) {
|
2012-10-11 01:34:26 +00:00
|
|
|
screenshooter = wl_registry_bind(registry, name,
|
2015-11-17 08:00:34 +00:00
|
|
|
&weston_screenshooter_interface,
|
|
|
|
1);
|
2011-05-02 16:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 12:53:55 +00:00
|
|
|
static void
|
|
|
|
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
|
|
|
|
{
|
|
|
|
/* XXX: unimplemented */
|
|
|
|
}
|
|
|
|
|
2012-10-11 01:34:26 +00:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
2013-01-22 12:53:55 +00:00
|
|
|
handle_global,
|
|
|
|
handle_global_remove
|
2012-10-11 01:34:26 +00:00
|
|
|
};
|
|
|
|
|
2011-05-02 16:11:07 +00:00
|
|
|
static struct wl_buffer *
|
|
|
|
create_shm_buffer(int width, int height, void **data_out)
|
|
|
|
{
|
2012-04-03 15:21:27 +00:00
|
|
|
struct wl_shm_pool *pool;
|
2011-05-02 16:11:07 +00:00
|
|
|
struct wl_buffer *buffer;
|
|
|
|
int fd, size, stride;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
stride = width * 4;
|
|
|
|
size = stride * height;
|
2012-06-06 13:59:43 +00:00
|
|
|
|
|
|
|
fd = os_create_anonymous_file(size);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
|
|
|
size);
|
2011-05-02 16:11:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (data == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "mmap failed: %m\n");
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-03 15:21:27 +00:00
|
|
|
pool = wl_shm_create_pool(shm, fd, size);
|
2011-05-02 16:11:07 +00:00
|
|
|
close(fd);
|
2012-04-03 15:21:27 +00:00
|
|
|
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
|
|
|
|
WL_SHM_FORMAT_XRGB8888);
|
|
|
|
wl_shm_pool_destroy(pool);
|
2011-05-02 16:11:07 +00:00
|
|
|
|
|
|
|
*data_out = data;
|
|
|
|
|
|
|
|
return buffer;
|
2008-11-24 04:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 08:42:59 +00:00
|
|
|
static void
|
2012-04-20 19:37:34 +00:00
|
|
|
write_png(int width, int height)
|
2011-07-27 12:58:57 +00:00
|
|
|
{
|
2012-04-20 19:37:34 +00:00
|
|
|
int output_stride, buffer_stride, i;
|
2012-03-12 05:05:25 +00:00
|
|
|
cairo_surface_t *surface;
|
2012-04-20 19:37:34 +00:00
|
|
|
void *data, *d, *s;
|
|
|
|
struct screenshooter_output *output, *next;
|
|
|
|
|
|
|
|
buffer_stride = width * 4;
|
|
|
|
|
2013-08-13 04:45:19 +00:00
|
|
|
data = xmalloc(buffer_stride * height);
|
2012-04-20 19:37:34 +00:00
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wl_list_for_each_safe(output, next, &output_list, link) {
|
|
|
|
output_stride = output->width * 4;
|
|
|
|
s = output->data;
|
2012-04-20 19:37:35 +00:00
|
|
|
d = data + (output->offset_y - min_y) * buffer_stride +
|
|
|
|
(output->offset_x - min_x) * 4;
|
2012-04-20 19:37:34 +00:00
|
|
|
|
|
|
|
for (i = 0; i < output->height; i++) {
|
|
|
|
memcpy(d, s, output_stride);
|
|
|
|
d += buffer_stride;
|
|
|
|
s += output_stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(output);
|
|
|
|
}
|
2012-03-12 05:05:25 +00:00
|
|
|
|
|
|
|
surface = cairo_image_surface_create_for_data(data,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
2012-04-20 19:37:34 +00:00
|
|
|
width, height, buffer_stride);
|
2012-03-12 05:05:25 +00:00
|
|
|
cairo_surface_write_to_png(surface, "wayland-screenshot.png");
|
|
|
|
cairo_surface_destroy(surface);
|
2012-04-20 19:37:34 +00:00
|
|
|
free(data);
|
2011-07-26 08:42:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 19:37:35 +00:00
|
|
|
static int
|
|
|
|
set_buffer_size(int *width, int *height)
|
|
|
|
{
|
|
|
|
struct screenshooter_output *output;
|
|
|
|
min_x = min_y = INT_MAX;
|
|
|
|
max_x = max_y = INT_MIN;
|
2012-08-19 01:52:42 +00:00
|
|
|
int position = 0;
|
|
|
|
|
|
|
|
wl_list_for_each_reverse(output, &output_list, link) {
|
|
|
|
output->offset_x = position;
|
|
|
|
position += output->width;
|
|
|
|
}
|
2012-04-20 19:37:35 +00:00
|
|
|
|
|
|
|
wl_list_for_each(output, &output_list, link) {
|
|
|
|
min_x = MIN(min_x, output->offset_x);
|
|
|
|
min_y = MIN(min_y, output->offset_y);
|
|
|
|
max_x = MAX(max_x, output->offset_x + output->width);
|
|
|
|
max_y = MAX(max_y, output->offset_y + output->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_x <= min_x || max_y <= min_y)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*width = max_x - min_x;
|
|
|
|
*height = max_y - min_y;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-24 04:41:08 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct wl_display *display;
|
2012-10-11 01:34:26 +00:00
|
|
|
struct wl_registry *registry;
|
2012-04-20 19:37:34 +00:00
|
|
|
struct screenshooter_output *output;
|
2012-04-20 19:37:35 +00:00
|
|
|
int width, height;
|
2008-11-24 04:41:08 +00:00
|
|
|
|
2013-02-13 18:40:58 +00:00
|
|
|
if (getenv("WAYLAND_SOCKET") == NULL) {
|
2013-07-23 21:53:26 +00:00
|
|
|
fprintf(stderr, "%s must be launched by weston.\n"
|
2013-08-13 05:15:38 +00:00
|
|
|
"Use the MOD+S shortcut to take a screenshot.\n",
|
|
|
|
program_invocation_short_name);
|
2013-02-13 18:40:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-12-01 20:36:20 +00:00
|
|
|
display = wl_display_connect(NULL);
|
2008-11-24 04:41:08 +00:00
|
|
|
if (display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-04 17:49:21 +00:00
|
|
|
wl_list_init(&output_list);
|
2012-10-11 01:34:26 +00:00
|
|
|
registry = wl_display_get_registry(display);
|
|
|
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
|
|
|
wl_display_dispatch(display);
|
2011-08-23 22:14:06 +00:00
|
|
|
wl_display_roundtrip(display);
|
2010-08-10 18:02:48 +00:00
|
|
|
if (screenshooter == NULL) {
|
|
|
|
fprintf(stderr, "display doesn't support screenshooter\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-17 08:00:34 +00:00
|
|
|
weston_screenshooter_add_listener(screenshooter,
|
|
|
|
&screenshooter_listener,
|
|
|
|
screenshooter);
|
2012-04-20 19:37:33 +00:00
|
|
|
|
2012-04-20 19:37:35 +00:00
|
|
|
if (set_buffer_size(&width, &height))
|
|
|
|
return -1;
|
|
|
|
|
2012-04-20 19:37:33 +00:00
|
|
|
|
2012-04-04 17:49:21 +00:00
|
|
|
wl_list_for_each(output, &output_list, link) {
|
2012-04-20 19:37:34 +00:00
|
|
|
output->buffer = create_shm_buffer(output->width, output->height, &output->data);
|
2015-11-17 08:00:34 +00:00
|
|
|
weston_screenshooter_shoot(screenshooter,
|
|
|
|
output->output,
|
|
|
|
output->buffer);
|
2012-04-20 19:37:33 +00:00
|
|
|
buffer_copy_done = 0;
|
|
|
|
while (!buffer_copy_done)
|
|
|
|
wl_display_roundtrip(display);
|
2012-04-04 17:49:21 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 19:37:34 +00:00
|
|
|
write_png(width, height);
|
2008-11-24 04:41:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|