blob: 894c4aa716c0e71cd627f234fe4b1cfa3e46b82e [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,
65 const char *model)
66{
Scott Moreau80d27b72012-04-04 11:49:21 -060067 struct screenshooter_output *output;
68
69 output = wl_output_get_user_data(wl_output);
70
71 if (wl_output == output->output) {
72 output->offset_x = x;
73 output->offset_y = y;
74 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040075}
76
77static void
78display_handle_mode(void *data,
79 struct wl_output *wl_output,
80 uint32_t flags,
81 int width,
82 int height,
83 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040084{
Scott Moreau80d27b72012-04-04 11:49:21 -060085 struct screenshooter_output *output;
86
87 output = wl_output_get_user_data(wl_output);
88
Kristian Høgsberg1a361562012-04-04 14:52:35 -040089 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060090 output->width = width;
91 output->height = height;
92 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040093}
94
95static const struct wl_output_listener output_listener = {
96 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040097 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040098};
99
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400100static void
Scott Moreau062be7e2012-04-20 13:37:33 -0600101screenshot_done(void *data, struct screenshooter *screenshooter)
102{
103 buffer_copy_done = 1;
104}
105
106static const struct screenshooter_listener screenshooter_listener = {
107 screenshot_done
108};
109
110static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400111handle_global(struct wl_display *display, uint32_t id,
112 const char *interface, uint32_t version, void *data)
113{
Scott Moreau80d27b72012-04-04 11:49:21 -0600114 static struct screenshooter_output *output;
115
Kristian Høgsberg85449032011-05-02 12:11:07 -0400116 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600117 output = malloc(sizeof *output);
118 output->output = wl_display_bind(display, id, &wl_output_interface);
119 wl_list_insert(&output_list, &output->link);
120 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400121 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400122 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400123 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400124 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400125 }
126}
127
Kristian Høgsberg85449032011-05-02 12:11:07 -0400128static struct wl_buffer *
129create_shm_buffer(int width, int height, void **data_out)
130{
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
Kristian Høgsberg85449032011-05-02 12:11:07 -0400136 stride = width * 4;
137 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300138
139 fd = os_create_anonymous_file(size);
140 if (fd < 0) {
141 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
142 size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400143 return NULL;
144 }
145
146 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400147 if (data == MAP_FAILED) {
148 fprintf(stderr, "mmap failed: %m\n");
149 close(fd);
150 return NULL;
151 }
152
Kristian Høgsberg16626282012-04-03 11:21:27 -0400153 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400154 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400155 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
156 WL_SHM_FORMAT_XRGB8888);
157 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400158
159 *data_out = data;
160
161 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500162}
163
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300164static void
Scott Moreau72c23722012-04-20 13:37:34 -0600165write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700166{
Scott Moreau72c23722012-04-20 13:37:34 -0600167 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400168 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600169 void *data, *d, *s;
170 struct screenshooter_output *output, *next;
171
172 buffer_stride = width * 4;
173
174 data = malloc(buffer_stride * height);
175 if (!data)
176 return;
177
178 wl_list_for_each_safe(output, next, &output_list, link) {
179 output_stride = output->width * 4;
180 s = output->data;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600181 d = data + (output->offset_y - min_y) * buffer_stride +
182 (output->offset_x - min_x) * 4;
Scott Moreau72c23722012-04-20 13:37:34 -0600183
184 for (i = 0; i < output->height; i++) {
185 memcpy(d, s, output_stride);
186 d += buffer_stride;
187 s += output_stride;
188 }
189
190 free(output);
191 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300192
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400193 surface = cairo_image_surface_create_for_data(data,
194 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600195 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400196 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
197 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600198 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300199}
200
Scott Moreau2074f1d2012-04-20 13:37:35 -0600201static int
202set_buffer_size(int *width, int *height)
203{
204 struct screenshooter_output *output;
205 min_x = min_y = INT_MAX;
206 max_x = max_y = INT_MIN;
207
208 wl_list_for_each(output, &output_list, link) {
209 min_x = MIN(min_x, output->offset_x);
210 min_y = MIN(min_y, output->offset_y);
211 max_x = MAX(max_x, output->offset_x + output->width);
212 max_y = MAX(max_y, output->offset_y + output->height);
213 }
214
215 if (max_x <= min_x || max_y <= min_y)
216 return -1;
217
218 *width = max_x - min_x;
219 *height = max_y - min_y;
220
221 return 0;
222}
223
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500224int main(int argc, char *argv[])
225{
226 struct wl_display *display;
Scott Moreau72c23722012-04-20 13:37:34 -0600227 struct screenshooter_output *output;
Scott Moreau2074f1d2012-04-20 13:37:35 -0600228 int width, height;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500229
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500230 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500231 if (display == NULL) {
232 fprintf(stderr, "failed to create display: %m\n");
233 return -1;
234 }
235
Scott Moreau80d27b72012-04-04 11:49:21 -0600236 wl_list_init(&output_list);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400237 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500238 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400239 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400240 if (screenshooter == NULL) {
241 fprintf(stderr, "display doesn't support screenshooter\n");
242 return -1;
243 }
244
Scott Moreau062be7e2012-04-20 13:37:33 -0600245 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
246
Scott Moreau2074f1d2012-04-20 13:37:35 -0600247 if (set_buffer_size(&width, &height))
248 return -1;
249
Scott Moreau062be7e2012-04-20 13:37:33 -0600250
Scott Moreau80d27b72012-04-04 11:49:21 -0600251 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600252 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
253 screenshooter_shoot(screenshooter, output->output, output->buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600254 buffer_copy_done = 0;
255 while (!buffer_copy_done)
256 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600257 }
258
Scott Moreau72c23722012-04-20 13:37:34 -0600259 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500260
261 return 0;
262}