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