2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-11-24 07:41:08 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2011-05-02 20:11:07 +04:00
|
|
|
#include <unistd.h>
|
2012-04-20 23:37:35 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/param.h>
|
2011-05-02 20:11:07 +04:00
|
|
|
#include <sys/mman.h>
|
2012-03-12 09:05:25 +04:00
|
|
|
#include <cairo.h>
|
2008-11-24 07:41:08 +03:00
|
|
|
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2010-09-15 00:23:24 +04:00
|
|
|
#include "screenshooter-client-protocol.h"
|
2012-06-06 17:59:43 +04:00
|
|
|
#include "../shared/os-compatibility.h"
|
2008-11-24 07:41:08 +03: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 20:11:07 +04:00
|
|
|
static struct wl_shm *shm;
|
|
|
|
static struct screenshooter *screenshooter;
|
2012-04-04 21:49:21 +04:00
|
|
|
static struct wl_list output_list;
|
2012-04-20 23:37:35 +04:00
|
|
|
int min_x, min_y, max_x, max_y;
|
2012-04-20 23:37:33 +04:00
|
|
|
int buffer_copy_done;
|
2012-04-04 21:49:21 +04:00
|
|
|
|
|
|
|
struct screenshooter_output {
|
|
|
|
struct wl_output *output;
|
|
|
|
struct wl_buffer *buffer;
|
|
|
|
int width, height, offset_x, offset_y;
|
2012-04-20 23:37:34 +04:00
|
|
|
void *data;
|
2012-04-04 21:49:21 +04:00
|
|
|
struct wl_list link;
|
|
|
|
};
|
2011-05-02 20:11:07 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
display_handle_geometry(void *data,
|
2011-06-21 19:16:58 +04:00
|
|
|
struct wl_output *wl_output,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int physical_width,
|
|
|
|
int physical_height,
|
|
|
|
int subpixel,
|
|
|
|
const char *make,
|
2012-07-22 23:49:57 +04:00
|
|
|
const char *model,
|
|
|
|
int transform)
|
2011-06-21 19:16:58 +04:00
|
|
|
{
|
2012-04-04 21:49:21 +04: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 19:16:58 +04: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 20:11:07 +04:00
|
|
|
{
|
2012-04-04 21:49:21 +04:00
|
|
|
struct screenshooter_output *output;
|
|
|
|
|
|
|
|
output = wl_output_get_user_data(wl_output);
|
|
|
|
|
2012-04-04 22:52:35 +04:00
|
|
|
if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
|
2012-04-04 21:49:21 +04:00
|
|
|
output->width = width;
|
|
|
|
output->height = height;
|
|
|
|
}
|
2011-05-02 20:11:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_output_listener output_listener = {
|
|
|
|
display_handle_geometry,
|
2011-06-21 19:16:58 +04:00
|
|
|
display_handle_mode
|
2011-05-02 20:11:07 +04:00
|
|
|
};
|
|
|
|
|
2012-04-20 23:37:33 +04:00
|
|
|
static void
|
|
|
|
screenshot_done(void *data, struct screenshooter *screenshooter)
|
|
|
|
{
|
|
|
|
buffer_copy_done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct screenshooter_listener screenshooter_listener = {
|
|
|
|
screenshot_done
|
|
|
|
};
|
|
|
|
|
2008-11-24 07:41:08 +03:00
|
|
|
static void
|
2012-10-11 05:34:26 +04:00
|
|
|
handle_global(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name, const char *interface, uint32_t version)
|
2008-11-24 07:41:08 +03:00
|
|
|
{
|
2012-04-04 21:49:21 +04:00
|
|
|
static struct screenshooter_output *output;
|
|
|
|
|
2011-05-02 20:11:07 +04:00
|
|
|
if (strcmp(interface, "wl_output") == 0) {
|
2012-04-04 21:49:21 +04:00
|
|
|
output = malloc(sizeof *output);
|
2012-10-11 05:34:26 +04:00
|
|
|
output->output = wl_registry_bind(registry, name,
|
|
|
|
&wl_output_interface, 1);
|
2012-04-04 21:49:21 +04:00
|
|
|
wl_list_insert(&output_list, &output->link);
|
|
|
|
wl_output_add_listener(output->output, &output_listener, output);
|
2011-05-02 20:11:07 +04:00
|
|
|
} else if (strcmp(interface, "wl_shm") == 0) {
|
2012-10-11 05:34:26 +04:00
|
|
|
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
2011-05-02 20:11:07 +04:00
|
|
|
} else if (strcmp(interface, "screenshooter") == 0) {
|
2012-10-11 05:34:26 +04:00
|
|
|
screenshooter = wl_registry_bind(registry, name,
|
|
|
|
&screenshooter_interface, 1);
|
2011-05-02 20:11:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:34:26 +04:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
handle_global
|
|
|
|
};
|
|
|
|
|
2011-05-02 20:11:07 +04:00
|
|
|
static struct wl_buffer *
|
|
|
|
create_shm_buffer(int width, int height, void **data_out)
|
|
|
|
{
|
2012-04-03 19:21:27 +04:00
|
|
|
struct wl_shm_pool *pool;
|
2011-05-02 20:11:07 +04:00
|
|
|
struct wl_buffer *buffer;
|
|
|
|
int fd, size, stride;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
stride = width * 4;
|
|
|
|
size = stride * height;
|
2012-06-06 17:59:43 +04: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 20:11:07 +04: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 19:21:27 +04:00
|
|
|
pool = wl_shm_create_pool(shm, fd, size);
|
2011-05-02 20:11:07 +04:00
|
|
|
close(fd);
|
2012-04-03 19:21:27 +04:00
|
|
|
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
|
|
|
|
WL_SHM_FORMAT_XRGB8888);
|
|
|
|
wl_shm_pool_destroy(pool);
|
2011-05-02 20:11:07 +04:00
|
|
|
|
|
|
|
*data_out = data;
|
|
|
|
|
|
|
|
return buffer;
|
2008-11-24 07:41:08 +03:00
|
|
|
}
|
|
|
|
|
2011-07-26 12:42:59 +04:00
|
|
|
static void
|
2012-04-20 23:37:34 +04:00
|
|
|
write_png(int width, int height)
|
2011-07-27 16:58:57 +04:00
|
|
|
{
|
2012-04-20 23:37:34 +04:00
|
|
|
int output_stride, buffer_stride, i;
|
2012-03-12 09:05:25 +04:00
|
|
|
cairo_surface_t *surface;
|
2012-04-20 23:37:34 +04:00
|
|
|
void *data, *d, *s;
|
|
|
|
struct screenshooter_output *output, *next;
|
|
|
|
|
|
|
|
buffer_stride = width * 4;
|
|
|
|
|
|
|
|
data = malloc(buffer_stride * height);
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wl_list_for_each_safe(output, next, &output_list, link) {
|
|
|
|
output_stride = output->width * 4;
|
|
|
|
s = output->data;
|
2012-04-20 23:37:35 +04:00
|
|
|
d = data + (output->offset_y - min_y) * buffer_stride +
|
|
|
|
(output->offset_x - min_x) * 4;
|
2012-04-20 23:37:34 +04:00
|
|
|
|
|
|
|
for (i = 0; i < output->height; i++) {
|
|
|
|
memcpy(d, s, output_stride);
|
|
|
|
d += buffer_stride;
|
|
|
|
s += output_stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(output);
|
|
|
|
}
|
2012-03-12 09:05:25 +04:00
|
|
|
|
|
|
|
surface = cairo_image_surface_create_for_data(data,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
2012-04-20 23:37:34 +04:00
|
|
|
width, height, buffer_stride);
|
2012-03-12 09:05:25 +04:00
|
|
|
cairo_surface_write_to_png(surface, "wayland-screenshot.png");
|
|
|
|
cairo_surface_destroy(surface);
|
2012-04-20 23:37:34 +04:00
|
|
|
free(data);
|
2011-07-26 12:42:59 +04:00
|
|
|
}
|
|
|
|
|
2012-04-20 23:37:35 +04: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 05:52:42 +04:00
|
|
|
int position = 0;
|
|
|
|
|
|
|
|
wl_list_for_each_reverse(output, &output_list, link) {
|
|
|
|
output->offset_x = position;
|
|
|
|
position += output->width;
|
|
|
|
}
|
2012-04-20 23:37:35 +04: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 07:41:08 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct wl_display *display;
|
2012-10-11 05:34:26 +04:00
|
|
|
struct wl_registry *registry;
|
2012-04-20 23:37:34 +04:00
|
|
|
struct screenshooter_output *output;
|
2012-04-20 23:37:35 +04:00
|
|
|
int width, height;
|
2008-11-24 07:41:08 +03:00
|
|
|
|
2010-12-01 23:36:20 +03:00
|
|
|
display = wl_display_connect(NULL);
|
2008-11-24 07:41:08 +03:00
|
|
|
if (display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-04 21:49:21 +04:00
|
|
|
wl_list_init(&output_list);
|
2012-10-11 05:34:26 +04:00
|
|
|
registry = wl_display_get_registry(display);
|
|
|
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
|
|
|
wl_display_dispatch(display);
|
2011-08-24 02:14:06 +04:00
|
|
|
wl_display_roundtrip(display);
|
2010-08-10 22:02:48 +04:00
|
|
|
if (screenshooter == NULL) {
|
|
|
|
fprintf(stderr, "display doesn't support screenshooter\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-20 23:37:33 +04:00
|
|
|
screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
|
|
|
|
|
2012-04-20 23:37:35 +04:00
|
|
|
if (set_buffer_size(&width, &height))
|
|
|
|
return -1;
|
|
|
|
|
2012-04-20 23:37:33 +04:00
|
|
|
|
2012-04-04 21:49:21 +04:00
|
|
|
wl_list_for_each(output, &output_list, link) {
|
2012-04-20 23:37:34 +04:00
|
|
|
output->buffer = create_shm_buffer(output->width, output->height, &output->data);
|
|
|
|
screenshooter_shoot(screenshooter, output->output, output->buffer);
|
2012-04-20 23:37:33 +04:00
|
|
|
buffer_copy_done = 0;
|
|
|
|
while (!buffer_copy_done)
|
|
|
|
wl_display_roundtrip(display);
|
2012-04-04 21:49:21 +04:00
|
|
|
}
|
|
|
|
|
2012-04-20 23:37:34 +04:00
|
|
|
write_png(width, height);
|
2008-11-24 07:41:08 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|