blob: e023199449caae71118394da3357434978d6e1c8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050023#include <stdint.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <fcntl.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040028#include <unistd.h>
Scott Moreau2074f1d2012-04-20 13:37:35 -060029#include <limits.h>
30#include <sys/param.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040031#include <sys/mman.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040032#include <cairo.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-client.h>
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040035#include "screenshooter-client-protocol.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050036
37/* The screenshooter is a good example of a custom object exposed by
38 * the compositor and serves as a test bed for implementing client
39 * side marshalling outside libwayland.so */
40
Kristian Høgsberg85449032011-05-02 12:11:07 -040041static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040042static struct screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060043static struct wl_list output_list;
Scott Moreau2074f1d2012-04-20 13:37:35 -060044int min_x, min_y, max_x, max_y;
Scott Moreau062be7e2012-04-20 13:37:33 -060045int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060046
47struct screenshooter_output {
48 struct wl_output *output;
49 struct wl_buffer *buffer;
50 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060051 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060052 struct wl_list link;
53};
Kristian Høgsberg85449032011-05-02 12:11:07 -040054
55static void
56display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040057 struct wl_output *wl_output,
58 int x,
59 int y,
60 int physical_width,
61 int physical_height,
62 int subpixel,
63 const char *make,
64 const char *model)
65{
Scott Moreau80d27b72012-04-04 11:49:21 -060066 struct screenshooter_output *output;
67
68 output = wl_output_get_user_data(wl_output);
69
70 if (wl_output == output->output) {
71 output->offset_x = x;
72 output->offset_y = y;
73 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074}
75
76static void
77display_handle_mode(void *data,
78 struct wl_output *wl_output,
79 uint32_t flags,
80 int width,
81 int height,
82 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040083{
Scott Moreau80d27b72012-04-04 11:49:21 -060084 struct screenshooter_output *output;
85
86 output = wl_output_get_user_data(wl_output);
87
Kristian Høgsberg1a361562012-04-04 14:52:35 -040088 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060089 output->width = width;
90 output->height = height;
91 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040092}
93
94static const struct wl_output_listener output_listener = {
95 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040096 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040097};
98
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040099static void
Scott Moreau062be7e2012-04-20 13:37:33 -0600100screenshot_done(void *data, struct screenshooter *screenshooter)
101{
102 buffer_copy_done = 1;
103}
104
105static const struct screenshooter_listener screenshooter_listener = {
106 screenshot_done
107};
108
109static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400110handle_global(struct wl_display *display, uint32_t id,
111 const char *interface, uint32_t version, void *data)
112{
Scott Moreau80d27b72012-04-04 11:49:21 -0600113 static struct screenshooter_output *output;
114
Kristian Høgsberg85449032011-05-02 12:11:07 -0400115 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600116 output = malloc(sizeof *output);
117 output->output = wl_display_bind(display, id, &wl_output_interface);
118 wl_list_insert(&output_list, &output->link);
119 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400120 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400121 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400122 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400123 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400124 }
125}
126
Kristian Høgsberg85449032011-05-02 12:11:07 -0400127static struct wl_buffer *
128create_shm_buffer(int width, int height, void **data_out)
129{
130 char filename[] = "/tmp/wayland-shm-XXXXXX";
Kristian Høgsberg16626282012-04-03 11:21:27 -0400131 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400132 struct wl_buffer *buffer;
133 int fd, size, stride;
134 void *data;
135
136 fd = mkstemp(filename);
137 if (fd < 0) {
138 fprintf(stderr, "open %s failed: %m\n", filename);
139 return NULL;
140 }
141 stride = width * 4;
142 size = stride * height;
143 if (ftruncate(fd, size) < 0) {
144 fprintf(stderr, "ftruncate failed: %m\n");
145 close(fd);
146 return NULL;
147 }
148
149 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
150 unlink(filename);
151
152 if (data == MAP_FAILED) {
153 fprintf(stderr, "mmap failed: %m\n");
154 close(fd);
155 return NULL;
156 }
157
Kristian Høgsberg16626282012-04-03 11:21:27 -0400158 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400159 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400160 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
161 WL_SHM_FORMAT_XRGB8888);
162 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400163
164 *data_out = data;
165
166 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500167}
168
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300169static void
Scott Moreau72c23722012-04-20 13:37:34 -0600170write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700171{
Scott Moreau72c23722012-04-20 13:37:34 -0600172 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400173 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600174 void *data, *d, *s;
175 struct screenshooter_output *output, *next;
176
177 buffer_stride = width * 4;
178
179 data = malloc(buffer_stride * height);
180 if (!data)
181 return;
182
183 wl_list_for_each_safe(output, next, &output_list, link) {
184 output_stride = output->width * 4;
185 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600186 d = data + (output->offset_y - min_y) * buffer_stride +
187 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600188
189 for (i = 0; i < output->height; i++) {
190 memcpy(d, s, output_stride);
191 d += buffer_stride;
192 s += output_stride;
193 }
194
195 free(output);
196 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300197
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400198 surface = cairo_image_surface_create_for_data(data,
199 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600200 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400201 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
202 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600203 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300204}
205
Scott Moreau2074f1d2012-04-20 13:37:35 -0600206static int
207set_buffer_size(int *width, int *height)
208{
209 struct screenshooter_output *output;
210 min_x = min_y = INT_MAX;
211 max_x = max_y = INT_MIN;
212
213 wl_list_for_each(output, &output_list, link) {
214 min_x = MIN(min_x, output->offset_x);
215 min_y = MIN(min_y, output->offset_y);
216 max_x = MAX(max_x, output->offset_x + output->width);
217 max_y = MAX(max_y, output->offset_y + output->height);
218 }
219
220 if (max_x <= min_x || max_y <= min_y)
221 return -1;
222
223 *width = max_x - min_x;
224 *height = max_y - min_y;
225
226 return 0;
227}
228
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500229int main(int argc, char *argv[])
230{
231 struct wl_display *display;
Scott Moreau72c23722012-04-20 13:37:34 -0600232 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600233 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500234
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500235 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500236 if (display == NULL) {
237 fprintf(stderr, "failed to create display: %m\n");
238 return -1;
239 }
240
Scott Moreau80d27b72012-04-04 11:49:21 -0600241 wl_list_init(&output_list);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400242 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500243 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400244 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400245 if (screenshooter == NULL) {
246 fprintf(stderr, "display doesn't support screenshooter\n");
247 return -1;
248 }
249
Scott Moreau062be7e2012-04-20 13:37:33 -0600250 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
251
Scott Moreau2074f1d2012-04-20 13:37:35 -0600252 if (set_buffer_size(&width, &height))
253 return -1;
254
Scott Moreau062be7e2012-04-20 13:37:33 -0600255
Scott Moreau80d27b72012-04-04 11:49:21 -0600256 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600257 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
258 screenshooter_shoot(screenshooter, output->output, output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600259 buffer_copy_done = 0;
260 while (!buffer_copy_done)
261 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600262 }
263
Scott Moreau72c23722012-04-20 13:37:34 -0600264 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500265
266 return 0;
267}