blob: 8681a41c6efaa950250f489551b56fb58b563898 [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"
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030036#include "../shared/os-compatibility.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050037
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 */
41
Kristian Høgsberg85449032011-05-02 12:11:07 -040042static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040043static struct screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060044static struct wl_list output_list;
Scott Moreau2074f1d2012-04-20 13:37:35 -060045int min_x, min_y, max_x, max_y;
Scott Moreau062be7e2012-04-20 13:37:33 -060046int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060047
48struct screenshooter_output {
49 struct wl_output *output;
50 struct wl_buffer *buffer;
51 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060052 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060053 struct wl_list link;
54};
Kristian Høgsberg85449032011-05-02 12:11:07 -040055
56static void
57display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040058 struct wl_output *wl_output,
59 int x,
60 int y,
61 int physical_width,
62 int physical_height,
63 int subpixel,
64 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -040065 const char *model,
66 int transform)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040067{
Scott Moreau80d27b72012-04-04 11:49:21 -060068 struct screenshooter_output *output;
69
70 output = wl_output_get_user_data(wl_output);
71
72 if (wl_output == output->output) {
73 output->offset_x = x;
74 output->offset_y = y;
75 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040076}
77
78static void
79display_handle_mode(void *data,
80 struct wl_output *wl_output,
81 uint32_t flags,
82 int width,
83 int height,
84 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040085{
Scott Moreau80d27b72012-04-04 11:49:21 -060086 struct screenshooter_output *output;
87
88 output = wl_output_get_user_data(wl_output);
89
Kristian Høgsberg1a361562012-04-04 14:52:35 -040090 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060091 output->width = width;
92 output->height = height;
93 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040094}
95
96static const struct wl_output_listener output_listener = {
97 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040098 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040099};
100
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400101static void
Scott Moreau062be7e2012-04-20 13:37:33 -0600102screenshot_done(void *data, struct screenshooter *screenshooter)
103{
104 buffer_copy_done = 1;
105}
106
107static const struct screenshooter_listener screenshooter_listener = {
108 screenshot_done
109};
110
111static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400112handle_global(void *data, struct wl_registry *registry,
113 uint32_t name, const char *interface, uint32_t version)
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400114{
Scott Moreau80d27b72012-04-04 11:49:21 -0600115 static struct screenshooter_output *output;
116
Kristian Høgsberg85449032011-05-02 12:11:07 -0400117 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600118 output = malloc(sizeof *output);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 output->output = wl_registry_bind(registry, name,
120 &wl_output_interface, 1);
Scott Moreau80d27b72012-04-04 11:49:21 -0600121 wl_list_insert(&output_list, &output->link);
122 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400123 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400124 shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400125 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 screenshooter = wl_registry_bind(registry, name,
127 &screenshooter_interface, 1);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400128 }
129}
130
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400131static const struct wl_registry_listener registry_listener = {
132 handle_global
133};
134
Kristian Høgsberg85449032011-05-02 12:11:07 -0400135static struct wl_buffer *
136create_shm_buffer(int width, int height, void **data_out)
137{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400138 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400139 struct wl_buffer *buffer;
140 int fd, size, stride;
141 void *data;
142
Kristian Høgsberg85449032011-05-02 12:11:07 -0400143 stride = width * 4;
144 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300145
146 fd = os_create_anonymous_file(size);
147 if (fd < 0) {
148 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
149 size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400150 return NULL;
151 }
152
153 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400154 if (data == MAP_FAILED) {
155 fprintf(stderr, "mmap failed: %m\n");
156 close(fd);
157 return NULL;
158 }
159
Kristian Høgsberg16626282012-04-03 11:21:27 -0400160 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400161 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400162 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
163 WL_SHM_FORMAT_XRGB8888);
164 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400165
166 *data_out = data;
167
168 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500169}
170
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300171static void
Scott Moreau72c23722012-04-20 13:37:34 -0600172write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700173{
Scott Moreau72c23722012-04-20 13:37:34 -0600174 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400175 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600176 void *data, *d, *s;
177 struct screenshooter_output *output, *next;
178
179 buffer_stride = width * 4;
180
181 data = malloc(buffer_stride * height);
182 if (!data)
183 return;
184
185 wl_list_for_each_safe(output, next, &output_list, link) {
186 output_stride = output->width * 4;
187 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600188 d = data + (output->offset_y - min_y) * buffer_stride +
189 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600190
191 for (i = 0; i < output->height; i++) {
192 memcpy(d, s, output_stride);
193 d += buffer_stride;
194 s += output_stride;
195 }
196
197 free(output);
198 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300199
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400200 surface = cairo_image_surface_create_for_data(data,
201 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600202 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400203 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
204 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600205 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300206}
207
Scott Moreau2074f1d2012-04-20 13:37:35 -0600208static int
209set_buffer_size(int *width, int *height)
210{
211 struct screenshooter_output *output;
212 min_x = min_y = INT_MAX;
213 max_x = max_y = INT_MIN;
Scott Moreaubb589832012-08-18 19:52:42 -0600214 int position = 0;
215
216 wl_list_for_each_reverse(output, &output_list, link) {
217 output->offset_x = position;
218 position += output->width;
219 }
Scott Moreau2074f1d2012-04-20 13:37:35 -0600220
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);
226 }
227
228 if (max_x <= min_x || max_y <= min_y)
229 return -1;
230
231 *width = max_x - min_x;
232 *height = max_y - min_y;
233
234 return 0;
235}
236
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500237int main(int argc, char *argv[])
238{
239 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400240 struct wl_registry *registry;
Scott Moreau72c23722012-04-20 13:37:34 -0600241 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600242 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500243
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500244 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500245 if (display == NULL) {
246 fprintf(stderr, "failed to create display: %m\n");
247 return -1;
248 }
249
Scott Moreau80d27b72012-04-04 11:49:21 -0600250 wl_list_init(&output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400251 registry = wl_display_get_registry(display);
252 wl_registry_add_listener(registry, &registry_listener, NULL);
253 wl_display_dispatch(display);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400254 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400255 if (screenshooter == NULL) {
256 fprintf(stderr, "display doesn't support screenshooter\n");
257 return -1;
258 }
259
Scott Moreau062be7e2012-04-20 13:37:33 -0600260 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
261
Scott Moreau2074f1d2012-04-20 13:37:35 -0600262 if (set_buffer_size(&width, &height))
263 return -1;
264
Scott Moreau062be7e2012-04-20 13:37:33 -0600265
Scott Moreau80d27b72012-04-04 11:49:21 -0600266 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600267 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
268 screenshooter_shoot(screenshooter, output->output, output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600269 buffer_copy_done = 0;
270 while (!buffer_copy_done)
271 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600272 }
273
Scott Moreau72c23722012-04-20 13:37:34 -0600274 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500275
276 return 0;
277}