1 /*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * 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:
10 *
11 * 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.
22 */
23
24 #include "config.h"
25
26 #include <stdint.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <cairo.h>
37
38 #include <wayland-client.h>
39 #include "weston-screenshooter-client-protocol.h"
40 #include "shared/os-compatibility.h"
41 #include "shared/xalloc.h"
42 #include "shared/file-util.h"
43
44 /* The screenshooter is a good example of a custom object exposed by
45 * the compositor and serves as a test bed for implementing client
46 * side marshalling outside libwayland.so */
47
48
49 struct screenshooter_output {
50 struct wl_output *output;
51 struct wl_buffer *buffer;
52 int width, height, offset_x, offset_y;
53 void *data;
54 struct wl_list link;
55 };
56
57 struct buffer_size {
58 int width, height;
59
60 int min_x, min_y;
61 int max_x, max_y;
62 };
63
64 struct screenshooter_data {
65 struct wl_shm *shm;
66 struct wl_list output_list;
67
68 struct weston_screenshooter *screenshooter;
69 int buffer_copy_done;
70 };
71
72
73 static void
display_handle_geometry(void * data,struct wl_output * wl_output,int x,int y,int physical_width,int physical_height,int subpixel,const char * make,const char * model,int transform)74 display_handle_geometry(void *data,
75 struct wl_output *wl_output,
76 int x,
77 int y,
78 int physical_width,
79 int physical_height,
80 int subpixel,
81 const char *make,
82 const char *model,
83 int transform)
84 {
85 struct screenshooter_output *output;
86
87 output = wl_output_get_user_data(wl_output);
88
89 if (wl_output == output->output) {
90 output->offset_x = x;
91 output->offset_y = y;
92 }
93 }
94
95 static void
display_handle_mode(void * data,struct wl_output * wl_output,uint32_t flags,int width,int height,int refresh)96 display_handle_mode(void *data,
97 struct wl_output *wl_output,
98 uint32_t flags,
99 int width,
100 int height,
101 int refresh)
102 {
103 struct screenshooter_output *output;
104
105 output = wl_output_get_user_data(wl_output);
106
107 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
108 output->width = width;
109 output->height = height;
110 }
111 }
112
113 static const struct wl_output_listener output_listener = {
114 display_handle_geometry,
115 display_handle_mode
116 };
117
118 static void
screenshot_done(void * data,struct weston_screenshooter * screenshooter)119 screenshot_done(void *data, struct weston_screenshooter *screenshooter)
120 {
121 struct screenshooter_data *sh_data = data;
122 sh_data->buffer_copy_done = 1;
123 }
124
125 static const struct weston_screenshooter_listener screenshooter_listener = {
126 screenshot_done
127 };
128
129 static void
handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)130 handle_global(void *data, struct wl_registry *registry,
131 uint32_t name, const char *interface, uint32_t version)
132 {
133 static struct screenshooter_output *output;
134 struct screenshooter_data *sh_data = data;
135
136 if (strcmp(interface, "wl_output") == 0) {
137 output = xmalloc(sizeof *output);
138 output->output = wl_registry_bind(registry, name,
139 &wl_output_interface, 1);
140 wl_list_insert(&sh_data->output_list, &output->link);
141 wl_output_add_listener(output->output, &output_listener, output);
142 } else if (strcmp(interface, "wl_shm") == 0) {
143 sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
144 } else if (strcmp(interface, "weston_screenshooter") == 0) {
145 sh_data->screenshooter = wl_registry_bind(registry, name,
146 &weston_screenshooter_interface,
147 1);
148 }
149 }
150
151 static void
handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)152 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
153 {
154 /* XXX: unimplemented */
155 }
156
157 static const struct wl_registry_listener registry_listener = {
158 handle_global,
159 handle_global_remove
160 };
161
162 static struct wl_buffer *
screenshot_create_shm_buffer(int width,int height,void ** data_out,struct wl_shm * shm)163 screenshot_create_shm_buffer(int width, int height, void **data_out,
164 struct wl_shm *shm)
165 {
166 struct wl_shm_pool *pool;
167 struct wl_buffer *buffer;
168 int fd, size, stride;
169 void *data;
170
171 stride = width * 4;
172 size = stride * height;
173
174 fd = os_create_anonymous_file(size);
175 if (fd < 0) {
176 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
177 size, strerror(errno));
178 return NULL;
179 }
180
181 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
182 if (data == MAP_FAILED) {
183 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
184 close(fd);
185 return NULL;
186 }
187
188 pool = wl_shm_create_pool(shm, fd, size);
189 close(fd);
190 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
191 WL_SHM_FORMAT_XRGB8888);
192 wl_shm_pool_destroy(pool);
193
194 *data_out = data;
195
196 return buffer;
197 }
198
199 static void
screenshot_write_png(const struct buffer_size * buff_size,struct wl_list * output_list)200 screenshot_write_png(const struct buffer_size *buff_size,
201 struct wl_list *output_list)
202 {
203 int output_stride, buffer_stride, i;
204 cairo_surface_t *surface;
205 void *data, *d, *s;
206 struct screenshooter_output *output, *next;
207 FILE *fp;
208 char filepath[PATH_MAX];
209
210 buffer_stride = buff_size->width * 4;
211
212 data = xmalloc(buffer_stride * buff_size->height);
213 if (!data)
214 return;
215
216 wl_list_for_each_safe(output, next, output_list, link) {
217 output_stride = output->width * 4;
218 s = output->data;
219 d = data + (output->offset_y - buff_size->min_y) * buffer_stride +
220 (output->offset_x - buff_size->min_x) * 4;
221
222 for (i = 0; i < output->height; i++) {
223 memcpy(d, s, output_stride);
224 d += buffer_stride;
225 s += output_stride;
226 }
227
228 free(output);
229 }
230
231 surface = cairo_image_surface_create_for_data(data,
232 CAIRO_FORMAT_ARGB32,
233 buff_size->width,
234 buff_size->height,
235 buffer_stride);
236
237 fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "wayland-screenshot-",
238 ".png", filepath, sizeof(filepath));
239 if (fp) {
240 fclose (fp);
241 cairo_surface_write_to_png(surface, filepath);
242 }
243 cairo_surface_destroy(surface);
244 free(data);
245 }
246
247 static int
screenshot_set_buffer_size(struct buffer_size * buff_size,struct wl_list * output_list)248 screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list)
249 {
250 struct screenshooter_output *output;
251 buff_size->min_x = buff_size->min_y = INT_MAX;
252 buff_size->max_x = buff_size->max_y = INT_MIN;
253 int position = 0;
254
255 wl_list_for_each_reverse(output, output_list, link) {
256 output->offset_x = position;
257 position += output->width;
258 }
259
260 wl_list_for_each(output, output_list, link) {
261 buff_size->min_x = MIN(buff_size->min_x, output->offset_x);
262 buff_size->min_y = MIN(buff_size->min_y, output->offset_y);
263 buff_size->max_x =
264 MAX(buff_size->max_x, output->offset_x + output->width);
265 buff_size->max_y =
266 MAX(buff_size->max_y, output->offset_y + output->height);
267 }
268
269 if (buff_size->max_x <= buff_size->min_x ||
270 buff_size->max_y <= buff_size->min_y)
271 return -1;
272
273 buff_size->width = buff_size->max_x - buff_size->min_x;
274 buff_size->height = buff_size->max_y - buff_size->min_y;
275
276 return 0;
277 }
278
main(int argc,char * argv[])279 int main(int argc, char *argv[])
280 {
281 struct wl_display *display;
282 struct wl_registry *registry;
283 struct screenshooter_output *output;
284 struct buffer_size buff_size = {};
285 struct screenshooter_data sh_data = {};
286
287 display = wl_display_connect(NULL);
288 if (display == NULL) {
289 fprintf(stderr, "failed to create display: %s\n",
290 strerror(errno));
291 return -1;
292 }
293
294 wl_list_init(&sh_data.output_list);
295 registry = wl_display_get_registry(display);
296 wl_registry_add_listener(registry, ®istry_listener, &sh_data);
297 wl_display_dispatch(display);
298 wl_display_roundtrip(display);
299 if (sh_data.screenshooter == NULL) {
300 fprintf(stderr, "display doesn't support screenshooter\n");
301 return -1;
302 }
303
304 weston_screenshooter_add_listener(sh_data.screenshooter,
305 &screenshooter_listener,
306 &sh_data);
307
308 if (screenshot_set_buffer_size(&buff_size, &sh_data.output_list))
309 return -1;
310
311
312 wl_list_for_each(output, &sh_data.output_list, link) {
313 output->buffer =
314 screenshot_create_shm_buffer(output->width,
315 output->height,
316 &output->data,
317 sh_data.shm);
318 weston_screenshooter_shoot(sh_data.screenshooter,
319 output->output,
320 output->buffer);
321 sh_data.buffer_copy_done = 0;
322 while (!sh_data.buffer_copy_done)
323 wl_display_roundtrip(display);
324 }
325
326 screenshot_write_png(&buff_size, &sh_data.output_list);
327
328 return 0;
329 }
330