toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / clients / screenshot.c
1 /*
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
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <sys/param.h>
31 #include <sys/mman.h>
32 #include <cairo.h>
33
34 #include <wayland-client.h>
35 #include "screenshooter-client-protocol.h"
36 #include "../shared/os-compatibility.h"
37
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
42 static struct wl_shm *shm;
43 static struct screenshooter *screenshooter;
44 static struct wl_list output_list;
45 int min_x, min_y, max_x, max_y;
46 int buffer_copy_done;
47
48 struct screenshooter_output {
49         struct wl_output *output;
50         struct wl_buffer *buffer;
51         int width, height, offset_x, offset_y;
52         void *data;
53         struct wl_list link;
54 };
55
56 static void
57 display_handle_geometry(void *data,
58                         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                         int transform)
67 {
68         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         }
76 }
77
78 static void
79 display_handle_mode(void *data,
80                     struct wl_output *wl_output,
81                     uint32_t flags,
82                     int width,
83                     int height,
84                     int refresh)
85 {
86         struct screenshooter_output *output;
87
88         output = wl_output_get_user_data(wl_output);
89
90         if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
91                 output->width = width;
92                 output->height = height;
93         }
94 }
95
96 static const struct wl_output_listener output_listener = {
97         display_handle_geometry,
98         display_handle_mode
99 };
100
101 static void
102 screenshot_done(void *data, struct screenshooter *screenshooter)
103 {
104         buffer_copy_done = 1;
105 }
106
107 static const struct screenshooter_listener screenshooter_listener = {
108         screenshot_done
109 };
110
111 static void
112 handle_global(struct wl_display *display, uint32_t id,
113               const char *interface, uint32_t version, void *data)
114 {
115         static struct screenshooter_output *output;
116
117         if (strcmp(interface, "wl_output") == 0) {
118                 output = malloc(sizeof *output);
119                 output->output = wl_display_bind(display, id, &wl_output_interface);
120                 wl_list_insert(&output_list, &output->link);
121                 wl_output_add_listener(output->output, &output_listener, output);
122         } else if (strcmp(interface, "wl_shm") == 0) {
123                 shm = wl_display_bind(display, id, &wl_shm_interface);
124         } else if (strcmp(interface, "screenshooter") == 0) {
125                 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
126         }
127 }
128
129 static struct wl_buffer *
130 create_shm_buffer(int width, int height, void **data_out)
131 {
132         struct wl_shm_pool *pool;
133         struct wl_buffer *buffer;
134         int fd, size, stride;
135         void *data;
136
137         stride = width * 4;
138         size = stride * height;
139
140         fd = os_create_anonymous_file(size);
141         if (fd < 0) {
142                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
143                         size);
144                 return NULL;
145         }
146
147         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
148         if (data == MAP_FAILED) {
149                 fprintf(stderr, "mmap failed: %m\n");
150                 close(fd);
151                 return NULL;
152         }
153
154         pool = wl_shm_create_pool(shm, fd, size);
155         close(fd);
156         buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
157                                            WL_SHM_FORMAT_XRGB8888);
158         wl_shm_pool_destroy(pool);
159
160         *data_out = data;
161
162         return buffer;
163 }
164
165 static void
166 write_png(int width, int height)
167 {
168         int output_stride, buffer_stride, i;
169         cairo_surface_t *surface;
170         void *data, *d, *s;
171         struct screenshooter_output *output, *next;
172
173         buffer_stride = width * 4;
174
175         data = malloc(buffer_stride * height);
176         if (!data)
177                 return;
178
179         wl_list_for_each_safe(output, next, &output_list, link) {
180                 output_stride = output->width * 4;
181                 s = output->data;
182                 d = data + (output->offset_y - min_y) * buffer_stride +
183                            (output->offset_x - min_x) * 4;
184
185                 for (i = 0; i < output->height; i++) {
186                         memcpy(d, s, output_stride);
187                         d += buffer_stride;
188                         s += output_stride;
189                 }
190
191                 free(output);
192         }
193
194         surface = cairo_image_surface_create_for_data(data,
195                                                       CAIRO_FORMAT_ARGB32,
196                                                       width, height, buffer_stride);
197         cairo_surface_write_to_png(surface, "wayland-screenshot.png");
198         cairo_surface_destroy(surface);
199         free(data);
200 }
201
202 static int
203 set_buffer_size(int *width, int *height)
204 {
205         struct screenshooter_output *output;
206         min_x = min_y = INT_MAX;
207         max_x = max_y = INT_MIN;
208         int position = 0;
209
210         wl_list_for_each_reverse(output, &output_list, link) {
211                 output->offset_x = position;
212                 position += output->width;
213         }
214
215         wl_list_for_each(output, &output_list, link) {
216                 min_x = MIN(min_x, output->offset_x);
217                 min_y = MIN(min_y, output->offset_y);
218                 max_x = MAX(max_x, output->offset_x + output->width);
219                 max_y = MAX(max_y, output->offset_y + output->height);
220         }
221
222         if (max_x <= min_x || max_y <= min_y)
223                 return -1;
224
225         *width = max_x - min_x;
226         *height = max_y - min_y;
227
228         return 0;
229 }
230
231 int main(int argc, char *argv[])
232 {
233         struct wl_display *display;
234         struct screenshooter_output *output;
235         int width, height;
236
237         display = wl_display_connect(NULL);
238         if (display == NULL) {
239                 fprintf(stderr, "failed to create display: %m\n");
240                 return -1;
241         }
242
243         wl_list_init(&output_list);
244         wl_display_add_global_listener(display, handle_global, &screenshooter);
245         wl_display_iterate(display, WL_DISPLAY_READABLE);
246         wl_display_roundtrip(display);
247         if (screenshooter == NULL) {
248                 fprintf(stderr, "display doesn't support screenshooter\n");
249                 return -1;
250         }
251
252         screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
253
254         if (set_buffer_size(&width, &height))
255                 return -1;
256
257
258         wl_list_for_each(output, &output_list, link) {
259                 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
260                 screenshooter_shoot(screenshooter, output->output, output->buffer);
261                 buffer_copy_done = 0;
262                 while (!buffer_copy_done)
263                         wl_display_roundtrip(display);
264         }
265
266         write_png(width, height);
267
268         return 0;
269 }