2 * Copyright © 2020 Collabora, Ltd.
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:
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.
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
34 #include <sys/param.h>
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"
47 struct screenshooter_data;
49 struct screenshooter_output {
50 struct wl_output *output;
51 struct wl_buffer *buffer;
53 int width, height, offset_x, offset_y, scale;
55 struct screenshooter_data *sh_data;
57 struct wl_list link; /** screenshooter_data::output_list */
60 struct xdg_output_v1_info {
61 struct zxdg_output_v1 *xdg_output;
62 struct screenshooter_output *output;
64 char *name, *description;
65 struct wl_list link; /** screenshooter_data::xdg_output_list */
76 struct screenshooter_data {
77 struct wl_display *display;
79 struct wl_list output_list; /** screenshooter_output::link */
80 struct wl_list xdg_output_list; /** xdg_output_v1_info::link */
82 struct zxdg_output_manager_v1 *xdg_output_manager;
83 struct agl_screenshooter *screenshooter;
87 static int opts = 0x0;
89 #define OPT_SCREENSHOT_OUTPUT 1
90 #define OPT_SHOW_ALL_OUTPUTS 2
91 #define OPT_SCREENSHOT_ALL_OUTPUTS 3
94 display_handle_geometry(void *data,
95 struct wl_output *wl_output,
105 struct screenshooter_output *output;
107 output = wl_output_get_user_data(wl_output);
109 if (wl_output == output->output) {
110 output->offset_x = x;
111 output->offset_y = y;
116 display_handle_mode(void *data,
117 struct wl_output *wl_output,
123 struct screenshooter_output *output;
125 output = wl_output_get_user_data(wl_output);
127 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
128 output->width = width;
129 output->height = height;
134 display_handle_done(void *data, struct wl_output *wl_output)
140 display_handle_scale(void *data, struct wl_output *wl_output,
143 struct screenshooter_output *output = data;
144 output->scale = scale;
148 static const struct wl_output_listener output_listener = {
149 display_handle_geometry,
152 display_handle_scale,
158 handle_xdg_output_v1_logical_position(void *data, struct zxdg_output_v1 *output,
159 int32_t x, int32_t y)
164 handle_xdg_output_v1_logical_size(void *data, struct zxdg_output_v1 *output,
165 int32_t width, int32_t height)
170 handle_xdg_output_v1_done(void *data, struct zxdg_output_v1 *output)
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. */
178 handle_xdg_output_v1_name(void *data, struct zxdg_output_v1 *output,
181 struct xdg_output_v1_info *xdg_output = data;
182 xdg_output->name = strdup(name);
186 handle_xdg_output_v1_description(void *data, struct zxdg_output_v1 *output,
187 const char *description)
189 struct xdg_output_v1_info *xdg_output = data;
190 xdg_output->description = strdup(description);
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,
202 add_xdg_output_v1_info(struct screenshooter_data *shooter_data,
203 struct screenshooter_output *output)
205 struct xdg_output_v1_info *xdg_output = zalloc(sizeof(*xdg_output));
209 wl_list_insert(&shooter_data->xdg_output_list, &xdg_output->link);
211 xdg_output->xdg_output =
212 zxdg_output_manager_v1_get_xdg_output(shooter_data->xdg_output_manager,
215 zxdg_output_v1_add_listener(xdg_output->xdg_output,
216 &xdg_output_v1_listener, xdg_output);
217 xdg_output->output = output;
221 screenshot_done(void *data, struct agl_screenshooter *screenshooter, uint32_t status)
223 struct screenshooter_data *sh_data = data;
224 sh_data->buffer_copy_done = 1;
227 static const struct agl_screenshooter_listener screenshooter_listener = {
232 handle_global(void *data, struct wl_registry *registry,
233 uint32_t name, const char *interface, uint32_t version)
235 struct screenshooter_output *output;
236 struct screenshooter_data *sh_data = data;
238 if (strcmp(interface, "wl_output") == 0) {
239 output = zalloc(sizeof(*output));
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);
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);
263 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
265 /* XXX: unimplemented */
268 static const struct wl_registry_listener registry_listener = {
273 static struct wl_buffer *
274 screenshot_create_shm_buffer(int width, int height, void **data_out,
277 struct wl_shm_pool *pool;
278 struct wl_buffer *buffer;
279 int fd, size, stride;
283 size = stride * height;
285 fd = os_create_anonymous_file(size);
287 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
288 size, strerror(errno));
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));
299 pool = wl_shm_create_pool(shm, fd, size);
301 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
302 WL_SHM_FORMAT_XRGB8888);
303 wl_shm_pool_destroy(pool);
311 screenshot_write_png_per_output(const struct buffer_size *buff_size,
312 struct screenshooter_output *sh_output)
314 int output_stride, buffer_stride, i;
315 cairo_surface_t *surface;
318 char filepath[PATH_MAX];
320 buffer_stride = buff_size->width * 4;
321 data = xmalloc(buffer_stride * buff_size->height);
325 output_stride = sh_output->width * 4;
327 d = data + (sh_output->offset_y - buff_size->min_y) * buffer_stride +
328 (sh_output->offset_x - buff_size->min_x) * 4;
330 for (i = 0; i < sh_output->height; i++) {
331 memcpy(d, s, output_stride);
336 surface = cairo_image_surface_create_for_data(data,
342 fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
343 ".png", filepath, sizeof(filepath));
346 cairo_surface_write_to_png(surface, filepath);
349 cairo_surface_destroy(surface);
354 screenshot_write_png(const struct buffer_size *buff_size,
355 struct wl_list *output_list)
357 int output_stride, buffer_stride, i;
358 cairo_surface_t *surface;
360 struct screenshooter_output *output, *next;
362 char filepath[PATH_MAX];
364 buffer_stride = buff_size->width * 4;
366 data = xmalloc(buffer_stride * buff_size->height);
370 wl_list_for_each_safe(output, next, output_list, link) {
371 output_stride = output->width * 4;
373 d = data + (output->offset_y - buff_size->min_y) * buffer_stride +
374 (output->offset_x - buff_size->min_x) * 4;
376 for (i = 0; i < output->height; i++) {
377 memcpy(d, s, output_stride);
385 surface = cairo_image_surface_create_for_data(data,
391 fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
392 ".png", filepath, sizeof(filepath));
395 cairo_surface_write_to_png(surface, filepath);
398 cairo_surface_destroy(surface);
403 screenshot_set_buffer_size_per_output(struct buffer_size *buff_size,
404 struct screenshooter_output *output)
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);
411 buff_size->width = buff_size->max_x - buff_size->min_x;
412 buff_size->height = buff_size->max_y - buff_size->min_y;
416 screenshot_compute_output_offset(int *pos, struct screenshooter_output *sh_output)
418 sh_output->offset_x = *pos;
419 *pos += sh_output->width;
423 screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list)
425 struct screenshooter_output *output;
428 buff_size->min_x = buff_size->min_y = INT_MAX;
429 buff_size->max_x = buff_size->max_y = INT_MIN;
431 wl_list_for_each_reverse(output, output_list, link)
432 screenshot_compute_output_offset(&pos, output);
434 wl_list_for_each(output, output_list, link)
435 screenshot_set_buffer_size_per_output(buff_size, output);
437 if (buff_size->max_x <= buff_size->min_x ||
438 buff_size->max_y <= buff_size->min_y)
441 buff_size->width = buff_size->max_x - buff_size->min_x;
442 buff_size->height = buff_size->max_y - buff_size->min_y;
447 static struct screenshooter_output *
448 agl_shooter_search_for_output(const char *output_name,
449 struct screenshooter_data *sh_data)
451 struct screenshooter_output *found_output = NULL;
452 struct xdg_output_v1_info *output;
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;
468 agl_shooter_display_all_outputs(struct screenshooter_data *sh_data)
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);
479 agl_shooter_screenshot_all_outputs(struct screenshooter_data *sh_data)
481 struct screenshooter_output *output;
482 struct buffer_size buff_size = {};
484 if (screenshot_set_buffer_size(&buff_size, &sh_data->output_list))
487 wl_list_for_each(output, &sh_data->output_list, link) {
489 screenshot_create_shm_buffer(output->width,
494 agl_screenshooter_take_shot(sh_data->screenshooter,
498 sh_data->buffer_copy_done = 0;
499 while (!sh_data->buffer_copy_done)
500 wl_display_roundtrip(sh_data->display);
503 screenshot_write_png(&buff_size, &sh_data->output_list);
507 agl_shooter_screenshot_output(struct screenshooter_output *sh_output)
510 struct buffer_size buff_size = {};
511 struct screenshooter_data *sh_data = sh_output->sh_data;
513 screenshot_compute_output_offset(&pos, sh_output);
514 screenshot_set_buffer_size_per_output(&buff_size, sh_output);
517 screenshot_create_shm_buffer(sh_output->width,
519 &sh_output->data, sh_data->shm);
521 agl_screenshooter_take_shot(sh_data->screenshooter,
525 sh_data->buffer_copy_done = 0;
526 while (!sh_data->buffer_copy_done)
527 wl_display_roundtrip(sh_data->display);
529 screenshot_write_png_per_output(&buff_size, sh_output);
533 agl_shooter_destroy_xdg_output_manager(struct screenshooter_data *sh_data)
535 struct xdg_output_v1_info *xdg_output;
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);
543 zxdg_output_manager_v1_destroy(sh_data->xdg_output_manager);
547 print_usage_and_exit(void)
549 fprintf(stderr, "./agl-screenshooter [-o OUTPUT_NAME] [-l] [-a]\n");
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");
558 int main(int argc, char *argv[])
560 struct wl_display *display;
561 struct wl_registry *registry;
563 struct screenshooter_data sh_data = {};
564 struct screenshooter_output *sh_output = NULL;
567 char *output_name = NULL;
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' },
577 while ((c = getopt_long(argc, argv, "o:lah",
578 long_options, &option_index)) != -1) {
581 output_name = optarg;
582 opts |= (1 << OPT_SCREENSHOT_OUTPUT);
585 opts |= (1 << OPT_SHOW_ALL_OUTPUTS);
588 opts |= (1 << OPT_SCREENSHOT_ALL_OUTPUTS);
591 print_usage_and_exit();
595 display = wl_display_connect(NULL);
596 if (display == NULL) {
597 fprintf(stderr, "failed to create display: %s\n",
602 wl_list_init(&sh_data.output_list);
603 wl_list_init(&sh_data.xdg_output_list);
604 sh_data.display = display;
606 registry = wl_display_get_registry(display);
607 wl_registry_add_listener(registry, ®istry_listener, &sh_data);
609 wl_display_dispatch(display);
610 wl_display_roundtrip(display);
613 if (sh_data.screenshooter == NULL) {
614 fprintf(stderr, "Compositor doesn't support screenshooter\n");
618 wl_list_for_each(sh_output, &sh_data.output_list, link)
619 add_xdg_output_v1_info(&sh_data, sh_output);
621 /* do another round-trip for xdg_output */
622 wl_display_roundtrip(sh_data.display);
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);
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);
638 sh_output = agl_shooter_search_for_output(output_name, &sh_data);
640 if (!sh_output && (opts & (1 << OPT_SCREENSHOT_OUTPUT))) {
641 fprintf(stderr, "Could not find an output matching '%s'\n",
643 agl_shooter_destroy_xdg_output_manager(&sh_data);
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 */
651 sh_output = container_of(sh_data.output_list.next,
652 struct screenshooter_output, link);
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);