blob: 2a6d9b32d090f80c329b1962b96ecd36d3ae6b22 [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øgsbergbdd83772013-08-12 21:45:19 -070023#include "config.h"
24
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050025#include <stdint.h>
Kristian Høgsbergbdd83772013-08-12 21:45:19 -070026#include <errno.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050027#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <fcntl.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040031#include <unistd.h>
Scott Moreau2074f1d2012-04-20 13:37:35 -060032#include <limits.h>
33#include <sys/param.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040034#include <sys/mman.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040035#include <cairo.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040038#include "screenshooter-client-protocol.h"
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030039#include "../shared/os-compatibility.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050040
41/* The screenshooter is a good example of a custom object exposed by
42 * the compositor and serves as a test bed for implementing client
43 * side marshalling outside libwayland.so */
44
Kristian Høgsberg85449032011-05-02 12:11:07 -040045static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040046static struct screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060047static struct wl_list output_list;
Scott Moreau2074f1d2012-04-20 13:37:35 -060048int min_x, min_y, max_x, max_y;
Scott Moreau062be7e2012-04-20 13:37:33 -060049int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060050
51struct screenshooter_output {
52 struct wl_output *output;
53 struct wl_buffer *buffer;
54 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060055 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060056 struct wl_list link;
57};
Kristian Høgsberg85449032011-05-02 12:11:07 -040058
59static void
60display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040061 struct wl_output *wl_output,
62 int x,
63 int y,
64 int physical_width,
65 int physical_height,
66 int subpixel,
67 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -040068 const char *model,
69 int transform)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040070{
Scott Moreau80d27b72012-04-04 11:49:21 -060071 struct screenshooter_output *output;
72
73 output = wl_output_get_user_data(wl_output);
74
75 if (wl_output == output->output) {
76 output->offset_x = x;
77 output->offset_y = y;
78 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040079}
80
Kristian Høgsbergbdd83772013-08-12 21:45:19 -070081static void *
82xmalloc(size_t size)
83{
84 void *p;
85
86 p = malloc(size);
87 if (p == NULL) {
88 fprintf(stderr, "%s: out of memory\n",
89 program_invocation_short_name);
90 exit(EXIT_FAILURE);
91 }
92
93 return p;
94}
95
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040096static void
97display_handle_mode(void *data,
98 struct wl_output *wl_output,
99 uint32_t flags,
100 int width,
101 int height,
102 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -0400103{
Scott Moreau80d27b72012-04-04 11:49:21 -0600104 struct screenshooter_output *output;
105
106 output = wl_output_get_user_data(wl_output);
107
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400108 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600109 output->width = width;
110 output->height = height;
111 }
Kristian Høgsberg85449032011-05-02 12:11:07 -0400112}
113
114static const struct wl_output_listener output_listener = {
115 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400116 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -0400117};
118
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400119static void
Scott Moreau062be7e2012-04-20 13:37:33 -0600120screenshot_done(void *data, struct screenshooter *screenshooter)
121{
122 buffer_copy_done = 1;
123}
124
125static const struct screenshooter_listener screenshooter_listener = {
126 screenshot_done
127};
128
129static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400130handle_global(void *data, struct wl_registry *registry,
131 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400132{
Scott Moreau80d27b72012-04-04 11:49:21 -0600133 static struct screenshooter_output *output;
134
Kristian Høgsberg85449032011-05-02 12:11:07 -0400135 if (strcmp(interface, "wl_output") == 0) {
Brian Lovinbc919262013-08-07 15:34:59 -0700136 output = xmalloc(sizeof *output);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400137 output->output = wl_registry_bind(registry, name,
138 &wl_output_interface, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -0600139 wl_list_insert(&output_list, &output->link);
140 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400141 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400142 shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400143 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400144 screenshooter = wl_registry_bind(registry, name,
145 &screenshooter_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400146 }
147}
148
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200149static void
150handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
151{
152 /* XXX: unimplemented */
153}
154
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400155static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200156 handle_global,
157 handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400158};
159
Kristian Høgsberg85449032011-05-02 12:11:07 -0400160static struct wl_buffer *
161create_shm_buffer(int width, int height, void **data_out)
162{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400163 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400164 struct wl_buffer *buffer;
165 int fd, size, stride;
166 void *data;
167
Kristian Høgsberg85449032011-05-02 12:11:07 -0400168 stride = width * 4;
169 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300170
171 fd = os_create_anonymous_file(size);
172 if (fd < 0) {
173 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
174 size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400175 return NULL;
176 }
177
178 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400179 if (data == MAP_FAILED) {
180 fprintf(stderr, "mmap failed: %m\n");
181 close(fd);
182 return NULL;
183 }
184
Kristian Høgsberg16626282012-04-03 11:21:27 -0400185 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400186 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400187 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
188 WL_SHM_FORMAT_XRGB8888);
189 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400190
191 *data_out = data;
192
193 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500194}
195
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300196static void
Scott Moreau72c23722012-04-20 13:37:34 -0600197write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700198{
Scott Moreau72c23722012-04-20 13:37:34 -0600199 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400200 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600201 void *data, *d, *s;
202 struct screenshooter_output *output, *next;
203
204 buffer_stride = width * 4;
205
Kristian Høgsbergbdd83772013-08-12 21:45:19 -0700206 data = xmalloc(buffer_stride * height);
Scott Moreau72c23722012-04-20 13:37:34 -0600207 if (!data)
208 return;
209
210 wl_list_for_each_safe(output, next, &output_list, link) {
211 output_stride = output->width * 4;
212 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600213 d = data + (output->offset_y - min_y) * buffer_stride +
214 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600215
216 for (i = 0; i < output->height; i++) {
217 memcpy(d, s, output_stride);
218 d += buffer_stride;
219 s += output_stride;
220 }
221
222 free(output);
223 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300224
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400225 surface = cairo_image_surface_create_for_data(data,
226 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600227 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400228 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
229 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600230 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300231}
232
Scott Moreau2074f1d2012-04-20 13:37:35 -0600233static int
234set_buffer_size(int *width, int *height)
235{
236 struct screenshooter_output *output;
237 min_x = min_y = INT_MAX;
238 max_x = max_y = INT_MIN;
Scott Moreaubb589832012-08-18 19:52:42 -0600239 int position = 0;
240
241 wl_list_for_each_reverse(output, &output_list, link) {
242 output->offset_x = position;
243 position += output->width;
244 }
Scott Moreau2074f1d2012-04-20 13:37:35 -0600245
246 wl_list_for_each(output, &output_list, link) {
247 min_x = MIN(min_x, output->offset_x);
248 min_y = MIN(min_y, output->offset_y);
249 max_x = MAX(max_x, output->offset_x + output->width);
250 max_y = MAX(max_y, output->offset_y + output->height);
251 }
252
253 if (max_x <= min_x || max_y <= min_y)
254 return -1;
255
256 *width = max_x - min_x;
257 *height = max_y - min_y;
258
259 return 0;
260}
261
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500262int main(int argc, char *argv[])
263{
264 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400265 struct wl_registry *registry;
Scott Moreau72c23722012-04-20 13:37:34 -0600266 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600267 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500268
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500269 if (getenv("WAYLAND_SOCKET") == NULL) {
Bryce W. Harrington3d2046e2013-07-23 21:53:26 +0000270 fprintf(stderr, "%s must be launched by weston.\n"
Kristian Høgsberg473f2482013-08-12 22:15:38 -0700271 "Use the MOD+S shortcut to take a screenshot.\n",
272 program_invocation_short_name);
Kristian Høgsbergc4a42ca2013-02-13 13:40:58 -0500273 return -1;
274 }
275
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500276 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500277 if (display == NULL) {
278 fprintf(stderr, "failed to create display: %m\n");
279 return -1;
280 }
281
Scott Moreau80d27b72012-04-04 11:49:21 -0600282 wl_list_init(&output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400283 registry = wl_display_get_registry(display);
284 wl_registry_add_listener(registry, &registry_listener, NULL);
285 wl_display_dispatch(display);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400286 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400287 if (screenshooter == NULL) {
288 fprintf(stderr, "display doesn't support screenshooter\n");
289 return -1;
290 }
291
Scott Moreau062be7e2012-04-20 13:37:33 -0600292 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
293
Scott Moreau2074f1d2012-04-20 13:37:35 -0600294 if (set_buffer_size(&width, &height))
295 return -1;
296
Scott Moreau062be7e2012-04-20 13:37:33 -0600297
Scott Moreau80d27b72012-04-04 11:49:21 -0600298 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600299 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
300 screenshooter_shoot(screenshooter, output->output, output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600301 buffer_copy_done = 0;
302 while (!buffer_copy_done)
303 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600304 }
305
Scott Moreau72c23722012-04-20 13:37:34 -0600306 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500307
308 return 0;
309}