blob: 040a438948d4aa7ea4115b39eb796e3725528b33 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050022 */
23
Kristian Høgsbergbdd83772013-08-12 21:45:19 -070024#include "config.h"
25
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050026#include <stdint.h>
Kristian Høgsbergbdd83772013-08-12 21:45:19 -070027#include <errno.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050028#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <fcntl.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040032#include <unistd.h>
Scott Moreau2074f1d2012-04-20 13:37:35 -060033#include <limits.h>
34#include <sys/param.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040035#include <sys/mman.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040036#include <cairo.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050037
Pekka Paalanen50719bc2011-11-22 14:18:50 +020038#include <wayland-client.h>
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040039#include "screenshooter-client-protocol.h"
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030040#include "../shared/os-compatibility.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050041
42/* The screenshooter is a good example of a custom object exposed by
43 * the compositor and serves as a test bed for implementing client
44 * side marshalling outside libwayland.so */
45
Kristian Høgsberg85449032011-05-02 12:11:07 -040046static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040047static struct screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060048static struct wl_list output_list;
Scott Moreau2074f1d2012-04-20 13:37:35 -060049int min_x, min_y, max_x, max_y;
Scott Moreau062be7e2012-04-20 13:37:33 -060050int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060051
52struct screenshooter_output {
53 struct wl_output *output;
54 struct wl_buffer *buffer;
55 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060056 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060057 struct wl_list link;
58};
Kristian Høgsberg85449032011-05-02 12:11:07 -040059
60static void
61display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040062 struct wl_output *wl_output,
63 int x,
64 int y,
65 int physical_width,
66 int physical_height,
67 int subpixel,
68 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -040069 const char *model,
70 int transform)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040071{
Scott Moreau80d27b72012-04-04 11:49:21 -060072 struct screenshooter_output *output;
73
74 output = wl_output_get_user_data(wl_output);
75
76 if (wl_output == output->output) {
77 output->offset_x = x;
78 output->offset_y = y;
79 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040080}
81
Kristian Høgsbergbdd83772013-08-12 21:45:19 -070082static void *
83xmalloc(size_t size)
84{
85 void *p;
86
87 p = malloc(size);
88 if (p == NULL) {
89 fprintf(stderr, "%s: out of memory\n",
90 program_invocation_short_name);
91 exit(EXIT_FAILURE);
92 }
93
94 return p;
95}
96
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040097static void
98display_handle_mode(void *data,
99 struct wl_output *wl_output,
100 uint32_t flags,
101 int width,
102 int height,
103 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -0400104{
Scott Moreau80d27b72012-04-04 11:49:21 -0600105 struct screenshooter_output *output;
106
107 output = wl_output_get_user_data(wl_output);
108
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400109 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600110 output->width = width;
111 output->height = height;
112 }
Kristian Høgsberg85449032011-05-02 12:11:07 -0400113}
114
115static const struct wl_output_listener output_listener = {
116 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400117 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -0400118};
119
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400120static void
Scott Moreau062be7e2012-04-20 13:37:33 -0600121screenshot_done(void *data, struct screenshooter *screenshooter)
122{
123 buffer_copy_done = 1;
124}
125
126static const struct screenshooter_listener screenshooter_listener = {
127 screenshot_done
128};
129
130static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400131handle_global(void *data, struct wl_registry *registry,
132 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400133{
Scott Moreau80d27b72012-04-04 11:49:21 -0600134 static struct screenshooter_output *output;
135
Kristian Høgsberg85449032011-05-02 12:11:07 -0400136 if (strcmp(interface, "wl_output") == 0) {
Brian Lovinbc919262013-08-07 15:34:59 -0700137 output = xmalloc(sizeof *output);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400138 output->output = wl_registry_bind(registry, name,
139 &wl_output_interface, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -0600140 wl_list_insert(&output_list, &output->link);
141 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400142 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400143 shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400144 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400145 screenshooter = wl_registry_bind(registry, name,
146 &screenshooter_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400147 }
148}
149
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200150static void
151handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
152{
153 /* XXX: unimplemented */
154}
155
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400156static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200157 handle_global,
158 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400159};
160
Kristian Høgsberg85449032011-05-02 12:11:07 -0400161static struct wl_buffer *
162create_shm_buffer(int width, int height, void **data_out)
163{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400164 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400165 struct wl_buffer *buffer;
166 int fd, size, stride;
167 void *data;
168
Kristian Høgsberg85449032011-05-02 12:11:07 -0400169 stride = width * 4;
170 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300171
172 fd = os_create_anonymous_file(size);
173 if (fd < 0) {
174 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
175 size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400176 return NULL;
177 }
178
179 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400180 if (data == MAP_FAILED) {
181 fprintf(stderr, "mmap failed: %m\n");
182 close(fd);
183 return NULL;
184 }
185
Kristian Høgsberg16626282012-04-03 11:21:27 -0400186 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400187 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400188 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
189 WL_SHM_FORMAT_XRGB8888);
190 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400191
192 *data_out = data;
193
194 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500195}
196
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300197static void
Scott Moreau72c23722012-04-20 13:37:34 -0600198write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700199{
Scott Moreau72c23722012-04-20 13:37:34 -0600200 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400201 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600202 void *data, *d, *s;
203 struct screenshooter_output *output, *next;
204
205 buffer_stride = width * 4;
206
Kristian Høgsbergbdd83772013-08-12 21:45:19 -0700207 data = xmalloc(buffer_stride * height);
Scott Moreau72c23722012-04-20 13:37:34 -0600208 if (!data)
209 return;
210
211 wl_list_for_each_safe(output, next, &output_list, link) {
212 output_stride = output->width * 4;
213 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600214 d = data + (output->offset_y - min_y) * buffer_stride +
215 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600216
217 for (i = 0; i < output->height; i++) {
218 memcpy(d, s, output_stride);
219 d += buffer_stride;
220 s += output_stride;
221 }
222
223 free(output);
224 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300225
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400226 surface = cairo_image_surface_create_for_data(data,
227 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600228 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400229 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
230 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600231 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300232}
233
Scott Moreau2074f1d2012-04-20 13:37:35 -0600234static int
235set_buffer_size(int *width, int *height)
236{
237 struct screenshooter_output *output;
238 min_x = min_y = INT_MAX;
239 max_x = max_y = INT_MIN;
Scott Moreaubb589832012-08-18 19:52:42 -0600240 int position = 0;
241
242 wl_list_for_each_reverse(output, &output_list, link) {
243 output->offset_x = position;
244 position += output->width;
245 }
Scott Moreau2074f1d2012-04-20 13:37:35 -0600246
247 wl_list_for_each(output, &output_list, link) {
248 min_x = MIN(min_x, output->offset_x);
249 min_y = MIN(min_y, output->offset_y);
250 max_x = MAX(max_x, output->offset_x + output->width);
251 max_y = MAX(max_y, output->offset_y + output->height);
252 }
253
254 if (max_x <= min_x || max_y <= min_y)
255 return -1;
256
257 *width = max_x - min_x;
258 *height = max_y - min_y;
259
260 return 0;
261}
262
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500263int main(int argc, char *argv[])
264{
265 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400266 struct wl_registry *registry;
Scott Moreau72c23722012-04-20 13:37:34 -0600267 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600268 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500269
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500270 if (getenv("WAYLAND_SOCKET") == NULL) {
Bryce W. Harrington3d2046e2013-07-23 21:53:26 +0000271 fprintf(stderr, "%s must be launched by weston.\n"
Kristian Høgsberg473f2482013-08-12 22:15:38 -0700272 "Use the MOD+S shortcut to take a screenshot.\n",
273 program_invocation_short_name);
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500274 return -1;
275 }
276
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500277 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500278 if (display == NULL) {
279 fprintf(stderr, "failed to create display: %m\n");
280 return -1;
281 }
282
Scott Moreau80d27b72012-04-04 11:49:21 -0600283 wl_list_init(&output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400284 registry = wl_display_get_registry(display);
285 wl_registry_add_listener(registry, &registry_listener, NULL);
286 wl_display_dispatch(display);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400287 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400288 if (screenshooter == NULL) {
289 fprintf(stderr, "display doesn't support screenshooter\n");
290 return -1;
291 }
292
Scott Moreau062be7e2012-04-20 13:37:33 -0600293 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
294
Scott Moreau2074f1d2012-04-20 13:37:35 -0600295 if (set_buffer_size(&width, &height))
296 return -1;
297
Scott Moreau062be7e2012-04-20 13:37:33 -0600298
Scott Moreau80d27b72012-04-04 11:49:21 -0600299 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600300 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
301 screenshooter_shoot(screenshooter, output->output, output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600302 buffer_copy_done = 0;
303 while (!buffer_copy_done)
304 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600305 }
306
Scott Moreau72c23722012-04-20 13:37:34 -0600307 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500308
309 return 0;
310}