2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 #include <sys/param.h>
34 #include <wayland-client.h>
35 #include "screenshooter-client-protocol.h"
36 #include "../shared/os-compatibility.h"
38 /* The screenshooter is a good example of a custom object exposed by
39 * the compositor and serves as a test bed for implementing client
40 * side marshalling outside libwayland.so */
42 static struct wl_shm *shm;
43 static struct screenshooter *screenshooter;
44 static struct wl_list output_list;
45 int min_x, min_y, max_x, max_y;
48 struct screenshooter_output {
49 struct wl_output *output;
50 struct wl_buffer *buffer;
51 int width, height, offset_x, offset_y;
57 display_handle_geometry(void *data,
58 struct wl_output *wl_output,
68 struct screenshooter_output *output;
70 output = wl_output_get_user_data(wl_output);
72 if (wl_output == output->output) {
79 display_handle_mode(void *data,
80 struct wl_output *wl_output,
86 struct screenshooter_output *output;
88 output = wl_output_get_user_data(wl_output);
90 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
91 output->width = width;
92 output->height = height;
96 static const struct wl_output_listener output_listener = {
97 display_handle_geometry,
102 screenshot_done(void *data, struct screenshooter *screenshooter)
104 buffer_copy_done = 1;
107 static const struct screenshooter_listener screenshooter_listener = {
112 handle_global(void *data, struct wl_registry *registry,
113 uint32_t name, const char *interface, uint32_t version)
115 static struct screenshooter_output *output;
117 if (strcmp(interface, "wl_output") == 0) {
118 output = malloc(sizeof *output);
119 output->output = wl_registry_bind(registry, name,
120 &wl_output_interface, 1);
121 wl_list_insert(&output_list, &output->link);
122 wl_output_add_listener(output->output, &output_listener, output);
123 } else if (strcmp(interface, "wl_shm") == 0) {
124 shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
125 } else if (strcmp(interface, "screenshooter") == 0) {
126 screenshooter = wl_registry_bind(registry, name,
127 &screenshooter_interface, 1);
131 static const struct wl_registry_listener registry_listener = {
135 static struct wl_buffer *
136 create_shm_buffer(int width, int height, void **data_out)
138 struct wl_shm_pool *pool;
139 struct wl_buffer *buffer;
140 int fd, size, stride;
144 size = stride * height;
146 fd = os_create_anonymous_file(size);
148 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
153 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
154 if (data == MAP_FAILED) {
155 fprintf(stderr, "mmap failed: %m\n");
160 pool = wl_shm_create_pool(shm, fd, size);
162 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
163 WL_SHM_FORMAT_XRGB8888);
164 wl_shm_pool_destroy(pool);
172 write_png(int width, int height)
174 int output_stride, buffer_stride, i;
175 cairo_surface_t *surface;
177 struct screenshooter_output *output, *next;
179 buffer_stride = width * 4;
181 data = malloc(buffer_stride * height);
185 wl_list_for_each_safe(output, next, &output_list, link) {
186 output_stride = output->width * 4;
188 d = data + (output->offset_y - min_y) * buffer_stride +
189 (output->offset_x - min_x) * 4;
191 for (i = 0; i < output->height; i++) {
192 memcpy(d, s, output_stride);
200 surface = cairo_image_surface_create_for_data(data,
202 width, height, buffer_stride);
203 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
204 cairo_surface_destroy(surface);
209 set_buffer_size(int *width, int *height)
211 struct screenshooter_output *output;
212 min_x = min_y = INT_MAX;
213 max_x = max_y = INT_MIN;
216 wl_list_for_each_reverse(output, &output_list, link) {
217 output->offset_x = position;
218 position += output->width;
221 wl_list_for_each(output, &output_list, link) {
222 min_x = MIN(min_x, output->offset_x);
223 min_y = MIN(min_y, output->offset_y);
224 max_x = MAX(max_x, output->offset_x + output->width);
225 max_y = MAX(max_y, output->offset_y + output->height);
228 if (max_x <= min_x || max_y <= min_y)
231 *width = max_x - min_x;
232 *height = max_y - min_y;
237 int main(int argc, char *argv[])
239 struct wl_display *display;
240 struct wl_registry *registry;
241 struct screenshooter_output *output;
244 display = wl_display_connect(NULL);
245 if (display == NULL) {
246 fprintf(stderr, "failed to create display: %m\n");
250 wl_list_init(&output_list);
251 registry = wl_display_get_registry(display);
252 wl_registry_add_listener(registry, ®istry_listener, NULL);
253 wl_display_dispatch(display);
254 wl_display_roundtrip(display);
255 if (screenshooter == NULL) {
256 fprintf(stderr, "display doesn't support screenshooter\n");
260 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
262 if (set_buffer_size(&width, &height))
266 wl_list_for_each(output, &output_list, link) {
267 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
268 screenshooter_shoot(screenshooter, output->output, output->buffer);
269 buffer_copy_done = 0;
270 while (!buffer_copy_done)
271 wl_display_roundtrip(display);
274 write_png(width, height);