blob: 6e43d5ce40c7ad3a7c72ed8b8333557a4c79f42d [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>
Jonas Ådahlcf1efd22015-11-17 16:00:34 +080039#include "weston-screenshooter-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070040#include "shared/os-compatibility.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070041#include "shared/xalloc.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050042
43/* The screenshooter is a good example of a custom object exposed by
44 * the compositor and serves as a test bed for implementing client
45 * side marshalling outside libwayland.so */
46
Kristian Høgsberg85449032011-05-02 12:11:07 -040047static struct wl_shm *shm;
Jonas Ådahlcf1efd22015-11-17 16:00:34 +080048static struct weston_screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060049static struct wl_list output_list;
Scott Moreau2074f1d2012-04-20 13:37:35 -060050int min_x, min_y, max_x, max_y;
Scott Moreau062be7e2012-04-20 13:37:33 -060051int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060052
53struct screenshooter_output {
54 struct wl_output *output;
55 struct wl_buffer *buffer;
56 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060057 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060058 struct wl_list link;
59};
Kristian Høgsberg85449032011-05-02 12:11:07 -040060
61static void
62display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040063 struct wl_output *wl_output,
64 int x,
65 int y,
66 int physical_width,
67 int physical_height,
68 int subpixel,
69 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -040070 const char *model,
71 int transform)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040072{
Scott Moreau80d27b72012-04-04 11:49:21 -060073 struct screenshooter_output *output;
74
75 output = wl_output_get_user_data(wl_output);
76
77 if (wl_output == output->output) {
78 output->offset_x = x;
79 output->offset_y = y;
80 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040081}
82
83static void
84display_handle_mode(void *data,
85 struct wl_output *wl_output,
86 uint32_t flags,
87 int width,
88 int height,
89 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040090{
Scott Moreau80d27b72012-04-04 11:49:21 -060091 struct screenshooter_output *output;
92
93 output = wl_output_get_user_data(wl_output);
94
Kristian Høgsberg1a361562012-04-04 14:52:35 -040095 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060096 output->width = width;
97 output->height = height;
98 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040099}
100
101static const struct wl_output_listener output_listener = {
102 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400103 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -0400104};
105
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400106static void
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800107screenshot_done(void *data, struct weston_screenshooter *screenshooter)
Scott Moreau062be7e2012-04-20 13:37:33 -0600108{
109 buffer_copy_done = 1;
110}
111
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800112static const struct weston_screenshooter_listener screenshooter_listener = {
Scott Moreau062be7e2012-04-20 13:37:33 -0600113 screenshot_done
114};
115
116static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400117handle_global(void *data, struct wl_registry *registry,
118 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400119{
Scott Moreau80d27b72012-04-04 11:49:21 -0600120 static struct screenshooter_output *output;
121
Kristian Høgsberg85449032011-05-02 12:11:07 -0400122 if (strcmp(interface, "wl_output") == 0) {
Brian Lovinbc919262013-08-07 15:34:59 -0700123 output = xmalloc(sizeof *output);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400124 output->output = wl_registry_bind(registry, name,
125 &wl_output_interface, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -0600126 wl_list_insert(&output_list, &output->link);
127 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400128 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400129 shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800130 } else if (strcmp(interface, "weston_screenshooter") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400131 screenshooter = wl_registry_bind(registry, name,
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800132 &weston_screenshooter_interface,
133 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400134 }
135}
136
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200137static void
138handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
139{
140 /* XXX: unimplemented */
141}
142
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400143static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200144 handle_global,
145 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400146};
147
Kristian Høgsberg85449032011-05-02 12:11:07 -0400148static struct wl_buffer *
149create_shm_buffer(int width, int height, void **data_out)
150{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400151 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400152 struct wl_buffer *buffer;
153 int fd, size, stride;
154 void *data;
155
Kristian Høgsberg85449032011-05-02 12:11:07 -0400156 stride = width * 4;
157 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300158
159 fd = os_create_anonymous_file(size);
160 if (fd < 0) {
161 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
162 size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400163 return NULL;
164 }
165
166 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400167 if (data == MAP_FAILED) {
168 fprintf(stderr, "mmap failed: %m\n");
169 close(fd);
170 return NULL;
171 }
172
Kristian Høgsberg16626282012-04-03 11:21:27 -0400173 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400174 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400175 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
176 WL_SHM_FORMAT_XRGB8888);
177 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400178
179 *data_out = data;
180
181 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500182}
183
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300184static void
Scott Moreau72c23722012-04-20 13:37:34 -0600185write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700186{
Scott Moreau72c23722012-04-20 13:37:34 -0600187 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400188 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600189 void *data, *d, *s;
190 struct screenshooter_output *output, *next;
191
192 buffer_stride = width * 4;
193
Kristian Høgsbergbdd83772013-08-12 21:45:19 -0700194 data = xmalloc(buffer_stride * height);
Scott Moreau72c23722012-04-20 13:37:34 -0600195 if (!data)
196 return;
197
198 wl_list_for_each_safe(output, next, &output_list, link) {
199 output_stride = output->width * 4;
200 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600201 d = data + (output->offset_y - min_y) * buffer_stride +
202 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600203
204 for (i = 0; i < output->height; i++) {
205 memcpy(d, s, output_stride);
206 d += buffer_stride;
207 s += output_stride;
208 }
209
210 free(output);
211 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300212
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400213 surface = cairo_image_surface_create_for_data(data,
214 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600215 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400216 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
217 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600218 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300219}
220
Scott Moreau2074f1d2012-04-20 13:37:35 -0600221static int
222set_buffer_size(int *width, int *height)
223{
224 struct screenshooter_output *output;
225 min_x = min_y = INT_MAX;
226 max_x = max_y = INT_MIN;
Scott Moreaubb589832012-08-18 19:52:42 -0600227 int position = 0;
228
229 wl_list_for_each_reverse(output, &output_list, link) {
230 output->offset_x = position;
231 position += output->width;
232 }
Scott Moreau2074f1d2012-04-20 13:37:35 -0600233
234 wl_list_for_each(output, &output_list, link) {
235 min_x = MIN(min_x, output->offset_x);
236 min_y = MIN(min_y, output->offset_y);
237 max_x = MAX(max_x, output->offset_x + output->width);
238 max_y = MAX(max_y, output->offset_y + output->height);
239 }
240
241 if (max_x <= min_x || max_y <= min_y)
242 return -1;
243
244 *width = max_x - min_x;
245 *height = max_y - min_y;
246
247 return 0;
248}
249
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500250int main(int argc, char *argv[])
251{
252 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400253 struct wl_registry *registry;
Scott Moreau72c23722012-04-20 13:37:34 -0600254 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600255 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500256
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500257 if (getenv("WAYLAND_SOCKET") == NULL) {
Bryce W. Harrington3d2046e2013-07-23 21:53:26 +0000258 fprintf(stderr, "%s must be launched by weston.\n"
Kristian Høgsberg473f2482013-08-12 22:15:38 -0700259 "Use the MOD+S shortcut to take a screenshot.\n",
260 program_invocation_short_name);
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500261 return -1;
262 }
263
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500264 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500265 if (display == NULL) {
266 fprintf(stderr, "failed to create display: %m\n");
267 return -1;
268 }
269
Scott Moreau80d27b72012-04-04 11:49:21 -0600270 wl_list_init(&output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400271 registry = wl_display_get_registry(display);
272 wl_registry_add_listener(registry, &registry_listener, NULL);
273 wl_display_dispatch(display);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400274 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400275 if (screenshooter == NULL) {
276 fprintf(stderr, "display doesn't support screenshooter\n");
277 return -1;
278 }
279
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800280 weston_screenshooter_add_listener(screenshooter,
281 &screenshooter_listener,
282 screenshooter);
Scott Moreau062be7e2012-04-20 13:37:33 -0600283
Scott Moreau2074f1d2012-04-20 13:37:35 -0600284 if (set_buffer_size(&width, &height))
285 return -1;
286
Scott Moreau062be7e2012-04-20 13:37:33 -0600287
Scott Moreau80d27b72012-04-04 11:49:21 -0600288 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600289 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
Jonas Ådahlcf1efd22015-11-17 16:00:34 +0800290 weston_screenshooter_shoot(screenshooter,
291 output->output,
292 output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600293 buffer_copy_done = 0;
294 while (!buffer_copy_done)
295 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600296 }
297
Scott Moreau72c23722012-04-20 13:37:34 -0600298 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500299
300 return 0;
301}