986e7bd9ba9d5a8f1cfb9fd1ebb811091c238c60
[src/agl-compositor.git] / clients / screenshooter.c
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
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 <getopt.h>
37 #include <cairo.h>
38
39 #include <wayland-client.h>
40 #include "shared/helpers.h"
41 #include "shared/xalloc.h"
42 #include "shared/file-util.h"
43 #include "shared/os-compatibility.h"
44 #include "agl-screenshooter-client-protocol.h"
45 #include "xdg-output-unstable-v1-client-protocol.h"
46
47 struct screenshooter_data;
48
49 struct screenshooter_output {
50         struct wl_output *output;
51         struct wl_buffer *buffer;
52
53         int width, height, offset_x, offset_y, scale;
54         void *data;
55         struct screenshooter_data *sh_data;
56
57         struct wl_list link;    /** screenshooter_data::output_list */
58 };
59
60 struct xdg_output_v1_info {
61         struct zxdg_output_v1 *xdg_output;
62         struct screenshooter_output *output;
63
64         char *name, *description;
65         struct wl_list link;    /** screenshooter_data::xdg_output_list */
66 };
67
68
69 struct buffer_size {
70         int width, height;
71
72         int min_x, min_y;
73         int max_x, max_y;
74 };
75
76 struct screenshooter_data {
77         struct wl_display *display;
78         struct wl_shm *shm;
79         struct wl_list output_list;     /** screenshooter_output::link */
80         struct wl_list xdg_output_list; /** xdg_output_v1_info::link */
81
82         struct zxdg_output_manager_v1 *xdg_output_manager;
83         struct agl_screenshooter *screenshooter;
84         int buffer_copy_done;
85 };
86
87 static int opts = 0x0;
88
89 #define OPT_SCREENSHOT_OUTPUT           1
90 #define OPT_SHOW_ALL_OUTPUTS            2
91 #define OPT_SCREENSHOT_ALL_OUTPUTS      3
92
93 static void
94 display_handle_geometry(void *data,
95                         struct wl_output *wl_output,
96                         int x,
97                         int y,
98                         int physical_width,
99                         int physical_height,
100                         int subpixel,
101                         const char *make,
102                         const char *model,
103                         int transform)
104 {
105         struct screenshooter_output *output;
106
107         output = wl_output_get_user_data(wl_output);
108
109         if (wl_output == output->output) {
110                 output->offset_x = x;
111                 output->offset_y = y;
112         }
113 }
114
115 static void
116 display_handle_mode(void *data,
117                     struct wl_output *wl_output,
118                     uint32_t flags,
119                     int width,
120                     int height,
121                     int refresh)
122 {
123         struct screenshooter_output *output;
124
125         output = wl_output_get_user_data(wl_output);
126
127         if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
128                 output->width = width;
129                 output->height = height;
130         }
131 }
132
133 static void
134 display_handle_done(void *data, struct wl_output *wl_output)
135 {
136
137 }
138
139 static void
140 display_handle_scale(void *data, struct wl_output *wl_output,
141                 int32_t scale)
142 {
143         struct screenshooter_output *output = data;
144         output->scale = scale;
145 }
146
147
148 static const struct wl_output_listener output_listener = {
149         display_handle_geometry,
150         display_handle_mode,
151         display_handle_done,
152         display_handle_scale,
153         NULL,
154         NULL
155 };
156
157 static void
158 handle_xdg_output_v1_logical_position(void *data, struct zxdg_output_v1 *output,
159                                       int32_t x, int32_t y)
160 {
161 }
162
163 static void
164 handle_xdg_output_v1_logical_size(void *data, struct zxdg_output_v1 *output,
165                                       int32_t width, int32_t height)
166 {
167 }
168
169 static void
170 handle_xdg_output_v1_done(void *data, struct zxdg_output_v1 *output)
171 {
172         /* Don't bother waiting for this; there's no good reason a
173          * compositor will wait more than one roundtrip before sending
174          * these initial events. */
175 }
176
177 static void
178 handle_xdg_output_v1_name(void *data, struct zxdg_output_v1 *output,
179                           const char *name)
180 {
181         struct xdg_output_v1_info *xdg_output = data;
182         xdg_output->name = strdup(name);
183 }
184
185 static void
186 handle_xdg_output_v1_description(void *data, struct zxdg_output_v1 *output,
187                           const char *description)
188 {
189         struct xdg_output_v1_info *xdg_output = data;
190         xdg_output->description = strdup(description);
191 }
192
193 static const struct zxdg_output_v1_listener xdg_output_v1_listener = {
194         .logical_position = handle_xdg_output_v1_logical_position,
195         .logical_size = handle_xdg_output_v1_logical_size,
196         .done = handle_xdg_output_v1_done,
197         .name = handle_xdg_output_v1_name,
198         .description = handle_xdg_output_v1_description,
199 };
200
201 static void
202 add_xdg_output_v1_info(struct screenshooter_data *shooter_data,
203                        struct screenshooter_output *output)
204 {
205         struct xdg_output_v1_info *xdg_output = zalloc(sizeof(*xdg_output));
206         if (!xdg_output)
207                 return;
208
209         wl_list_insert(&shooter_data->xdg_output_list, &xdg_output->link);
210
211         xdg_output->xdg_output =
212                 zxdg_output_manager_v1_get_xdg_output(shooter_data->xdg_output_manager,
213                                                       output->output);
214
215         zxdg_output_v1_add_listener(xdg_output->xdg_output,
216                                     &xdg_output_v1_listener, xdg_output);
217         xdg_output->output = output;
218 }
219
220 static void
221 screenshot_done(void *data, struct agl_screenshooter *screenshooter, uint32_t status)
222 {
223         struct screenshooter_data *sh_data = data;
224         sh_data->buffer_copy_done = 1;
225 }
226
227 static const struct agl_screenshooter_listener screenshooter_listener = {
228         screenshot_done
229 };
230
231 static void
232 handle_global(void *data, struct wl_registry *registry,
233               uint32_t name, const char *interface, uint32_t version)
234 {
235         struct screenshooter_output *output;
236         struct screenshooter_data *sh_data = data;
237
238         if (strcmp(interface, "wl_output") == 0) {
239                 output = zalloc(sizeof(*output));
240                 if (!output)
241                         return;
242
243                 output->output = wl_registry_bind(registry, name,
244                                                   &wl_output_interface, 1);
245                 output->sh_data = sh_data;
246                 wl_list_insert(&sh_data->output_list, &output->link);
247                 wl_output_add_listener(output->output, &output_listener, output);
248         } else if (strcmp(interface, "wl_shm") == 0) {
249                 sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
250         } else if (strcmp(interface, "agl_screenshooter") == 0) {
251                 sh_data->screenshooter = wl_registry_bind(registry, name,
252                                                           &agl_screenshooter_interface, 1);
253
254                 agl_screenshooter_add_listener(sh_data->screenshooter,
255                                                &screenshooter_listener, sh_data);
256         } else if (strcmp(interface, "zxdg_output_manager_v1") == 0) {
257                 sh_data->xdg_output_manager = wl_registry_bind(registry, name,
258                                         &zxdg_output_manager_v1_interface, version);
259         }
260 }
261
262 static void
263 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
264 {
265         /* XXX: unimplemented */
266 }
267
268 static const struct wl_registry_listener registry_listener = {
269         handle_global,
270         handle_global_remove
271 };
272
273 static struct wl_buffer *
274 screenshot_create_shm_buffer(int width, int height, void **data_out,
275                              struct wl_shm *shm)
276 {
277         struct wl_shm_pool *pool;
278         struct wl_buffer *buffer;
279         int fd, size, stride;
280         void *data;
281
282         stride = width * 4;
283         size = stride * height;
284
285         fd = os_create_anonymous_file(size);
286         if (fd < 0) {
287                 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
288                         size, strerror(errno));
289                 return NULL;
290         }
291
292         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
293         if (data == MAP_FAILED) {
294                 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
295                 close(fd);
296                 return NULL;
297         }
298
299         pool = wl_shm_create_pool(shm, fd, size);
300         close(fd);
301         buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
302                                            WL_SHM_FORMAT_XRGB8888);
303         wl_shm_pool_destroy(pool);
304
305         *data_out = data;
306
307         return buffer;
308 }
309
310 static void
311 screenshot_write_png_per_output(const struct buffer_size *buff_size,
312                                 struct screenshooter_output *sh_output)
313 {
314         int output_stride, buffer_stride, i;
315         cairo_surface_t *surface;
316         void *data, *d, *s;
317         FILE *fp;
318         char filepath[PATH_MAX];
319
320         buffer_stride = buff_size->width * 4;
321         data = xmalloc(buffer_stride * buff_size->height);
322         if (!data)
323                 return;
324
325         output_stride = sh_output->width * 4;
326         s = sh_output->data;
327         d = data + (sh_output->offset_y - buff_size->min_y) * buffer_stride +
328                    (sh_output->offset_x - buff_size->min_x) * 4;
329
330         for (i = 0; i < sh_output->height; i++) {
331                 memcpy(d, s, output_stride);
332                 d += buffer_stride;
333                 s += output_stride;
334         }
335
336         surface = cairo_image_surface_create_for_data(data,
337                                                       CAIRO_FORMAT_ARGB32,
338                                                       buff_size->width,
339                                                       buff_size->height,
340                                                       buffer_stride);
341
342         fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
343                                ".png", filepath, sizeof(filepath));
344         if (fp) {
345                 fclose(fp);
346                 cairo_surface_write_to_png(surface, filepath);
347         }
348
349         cairo_surface_destroy(surface);
350         free(data);
351 }
352
353 static void
354 screenshot_write_png(const struct buffer_size *buff_size,
355                      struct wl_list *output_list)
356 {
357         int output_stride, buffer_stride, i;
358         cairo_surface_t *surface;
359         void *data, *d, *s;
360         struct screenshooter_output *output, *next;
361         FILE *fp;
362         char filepath[PATH_MAX];
363
364         buffer_stride = buff_size->width * 4;
365
366         data = xmalloc(buffer_stride * buff_size->height);
367         if (!data)
368                 return;
369
370         wl_list_for_each_safe(output, next, output_list, link) {
371                 output_stride = output->width * 4;
372                 s = output->data;
373                 d = data + (output->offset_y - buff_size->min_y) * buffer_stride +
374                            (output->offset_x - buff_size->min_x) * 4;
375
376                 for (i = 0; i < output->height; i++) {
377                         memcpy(d, s, output_stride);
378                         d += buffer_stride;
379                         s += output_stride;
380                 }
381
382                 free(output);
383         }
384
385         surface = cairo_image_surface_create_for_data(data,
386                                                       CAIRO_FORMAT_ARGB32,
387                                                       buff_size->width,
388                                                       buff_size->height,
389                                                       buffer_stride);
390
391         fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
392                                ".png", filepath, sizeof(filepath));
393         if (fp) {
394                 fclose(fp);
395                 cairo_surface_write_to_png(surface, filepath);
396         }
397
398         cairo_surface_destroy(surface);
399         free(data);
400 }
401
402 static void
403 screenshot_set_buffer_size_per_output(struct buffer_size *buff_size,
404                                       struct screenshooter_output *output)
405 {
406         buff_size->min_x = MIN(buff_size->min_x, output->offset_x);
407         buff_size->min_y = MIN(buff_size->min_y, output->offset_y);
408         buff_size->max_x = MAX(buff_size->max_x, output->offset_x + output->width);
409         buff_size->max_y = MAX(buff_size->max_y, output->offset_y + output->height);
410
411         buff_size->width = buff_size->max_x - buff_size->min_x;
412         buff_size->height = buff_size->max_y - buff_size->min_y;
413 }
414
415 static void
416 screenshot_compute_output_offset(int *pos, struct screenshooter_output *sh_output)
417 {
418         sh_output->offset_x = *pos;
419         *pos += sh_output->width;
420 }
421
422 static int
423 screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list)
424 {
425         struct screenshooter_output *output;
426         int pos = 0;
427
428         buff_size->min_x = buff_size->min_y = INT_MAX;
429         buff_size->max_x = buff_size->max_y = INT_MIN;
430
431         wl_list_for_each_reverse(output, output_list, link)
432                 screenshot_compute_output_offset(&pos, output);
433
434         wl_list_for_each(output, output_list, link)
435                 screenshot_set_buffer_size_per_output(buff_size, output);
436
437         if (buff_size->max_x <= buff_size->min_x ||
438             buff_size->max_y <= buff_size->min_y)
439                 return -1;
440
441         buff_size->width = buff_size->max_x - buff_size->min_x;
442         buff_size->height = buff_size->max_y - buff_size->min_y;
443
444         return 0;
445 }
446
447 static struct screenshooter_output *
448 agl_shooter_search_for_output(const char *output_name,
449                               struct screenshooter_data *sh_data)
450 {
451         struct screenshooter_output *found_output = NULL;
452         struct xdg_output_v1_info *output;
453
454         if (!output_name)
455                 return found_output;
456
457         wl_list_for_each(output, &sh_data->xdg_output_list, link) {
458                 if (output->name && strcmp(output->name, output_name) == 0) {
459                         found_output = output->output;
460                         break;
461                 }
462         }
463
464         return found_output;
465 }
466
467 static void
468 agl_shooter_display_all_outputs(struct screenshooter_data *sh_data)
469 {
470         struct xdg_output_v1_info *xdg_output;
471         wl_list_for_each(xdg_output, &sh_data->xdg_output_list, link) {
472                 fprintf(stdout, "Output '%s', desc: '%s'\n", xdg_output->name,
473                                 xdg_output->description);
474         }
475 }
476
477
478 static void
479 agl_shooter_screenshot_all_outputs(struct screenshooter_data *sh_data)
480 {
481         struct screenshooter_output *output;
482         struct buffer_size buff_size = {};
483
484         if (screenshot_set_buffer_size(&buff_size, &sh_data->output_list))
485                 return;
486
487         wl_list_for_each(output, &sh_data->output_list, link) {
488                 output->buffer =
489                         screenshot_create_shm_buffer(output->width,
490                                                      output->height,
491                                                      &output->data,
492                                                      sh_data->shm);
493
494                 agl_screenshooter_take_shot(sh_data->screenshooter,
495                                             output->output,
496                                             output->buffer);
497
498                 sh_data->buffer_copy_done = 0;
499                 while (!sh_data->buffer_copy_done)
500                         wl_display_roundtrip(sh_data->display);
501         }
502
503         screenshot_write_png(&buff_size, &sh_data->output_list);
504 }
505
506 static void
507 agl_shooter_screenshot_output(struct screenshooter_output *sh_output)
508 {
509         int pos = 0;
510         struct buffer_size buff_size = {};
511         struct screenshooter_data *sh_data = sh_output->sh_data;
512
513         screenshot_compute_output_offset(&pos, sh_output);
514         screenshot_set_buffer_size_per_output(&buff_size, sh_output);
515
516         sh_output->buffer =
517                 screenshot_create_shm_buffer(sh_output->width,
518                                              sh_output->height,
519                                              &sh_output->data, sh_data->shm);
520
521         agl_screenshooter_take_shot(sh_data->screenshooter,
522                                     sh_output->output,
523                                     sh_output->buffer);
524
525         sh_data->buffer_copy_done = 0;
526         while (!sh_data->buffer_copy_done)
527                 wl_display_roundtrip(sh_data->display);
528
529         screenshot_write_png_per_output(&buff_size, sh_output);
530 }
531
532 static void
533 agl_shooter_destroy_xdg_output_manager(struct screenshooter_data *sh_data)
534 {
535         struct xdg_output_v1_info *xdg_output;
536
537         wl_list_for_each(xdg_output, &sh_data->xdg_output_list, link) {
538                 free(xdg_output->name);
539                 free(xdg_output->description);
540                 zxdg_output_v1_destroy(xdg_output->xdg_output);
541         }
542
543         zxdg_output_manager_v1_destroy(sh_data->xdg_output_manager);
544 }
545
546 static void
547 print_usage_and_exit(void)
548 {
549         fprintf(stderr, "./agl-screenshooter [-o OUTPUT_NAME] [-l] [-a]\n");
550
551         fprintf(stderr, "\t-o OUTPUT_NAME -- take a screenshot of the output "
552                                 "specified by OUTPUT_NAME\n");
553         fprintf(stderr, "\t-a  -- take a screenshot of all the outputs found\n");
554         fprintf(stderr, "\t-l  -- list all the outputs found\n");
555         exit(EXIT_FAILURE);
556 }
557
558 int main(int argc, char *argv[])
559 {
560         struct wl_display *display;
561         struct wl_registry *registry;
562
563         struct screenshooter_data sh_data = {};
564         struct screenshooter_output *sh_output = NULL;
565         int c, option_index;
566
567         char *output_name = NULL;
568
569         static struct option long_options[] = {
570                 {"output",      required_argument, 0,  'o' },
571                 {"list",        required_argument, 0,  'l' },
572                 {"all",         required_argument, 0,  'a' },
573                 {"help",        no_argument      , 0,  'h' },
574                 {0, 0, 0, 0}
575         };
576
577         while ((c = getopt_long(argc, argv, "o:lah",
578                                 long_options, &option_index)) != -1) {
579                 switch (c) {
580                 case 'o':
581                         output_name = optarg;
582                         opts |= (1 << OPT_SCREENSHOT_OUTPUT);
583                         break;
584                 case 'l':
585                         opts |= (1 << OPT_SHOW_ALL_OUTPUTS);
586                         break;
587                 case 'a':
588                         opts |= (1 << OPT_SCREENSHOT_ALL_OUTPUTS);
589                         break;
590                 default:
591                         print_usage_and_exit();
592                 }
593         }
594
595         display = wl_display_connect(NULL);
596         if (display == NULL) {
597                 fprintf(stderr, "failed to create display: %s\n",
598                         strerror(errno));
599                 return EXIT_FAILURE;
600         }
601
602         wl_list_init(&sh_data.output_list);
603         wl_list_init(&sh_data.xdg_output_list);
604         sh_data.display = display;
605
606         registry = wl_display_get_registry(display);
607         wl_registry_add_listener(registry, &registry_listener, &sh_data);
608
609         wl_display_dispatch(display);
610         wl_display_roundtrip(display);
611
612
613         if (sh_data.screenshooter == NULL) {
614                 fprintf(stderr, "Compositor doesn't support screenshooter\n");
615                 return EXIT_FAILURE;
616         }
617
618         wl_list_for_each(sh_output, &sh_data.output_list, link)
619                 add_xdg_output_v1_info(&sh_data, sh_output);
620
621         /* do another round-trip for xdg_output */
622         wl_display_roundtrip(sh_data.display);
623
624         if (opts & (1 << OPT_SHOW_ALL_OUTPUTS)) {
625                 agl_shooter_display_all_outputs(&sh_data);
626                 agl_shooter_destroy_xdg_output_manager(&sh_data);
627                 return EXIT_SUCCESS;
628         }
629
630         if (opts & (1 << OPT_SCREENSHOT_ALL_OUTPUTS)) {
631                 agl_shooter_screenshot_all_outputs(&sh_data);
632                 agl_shooter_destroy_xdg_output_manager(&sh_data);
633                 return EXIT_SUCCESS;
634         }
635
636         sh_output = NULL;
637         if (output_name)
638                 sh_output = agl_shooter_search_for_output(output_name, &sh_data);
639
640         if (!sh_output && (opts & (1 << OPT_SCREENSHOT_OUTPUT))) {
641                 fprintf(stderr, "Could not find an output matching '%s'\n",
642                                 output_name);
643                 agl_shooter_destroy_xdg_output_manager(&sh_data);
644                 return EXIT_FAILURE;
645         }
646
647         /* if we're still here just pick the first one available
648          * and use that. Still useful in case we are run without
649          * any args whatsoever */
650         if (!sh_output)
651                 sh_output = container_of(sh_data.output_list.next,
652                                          struct screenshooter_output, link);
653
654         /* take a screenshot only of that specific output */
655         agl_shooter_screenshot_output(sh_output);
656         agl_shooter_destroy_xdg_output_manager(&sh_data);
657
658         return 0;
659 }