2 * Copyright 2024 Collabora, Ltd.
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:
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
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.
35 #include <sys/param.h>
42 #include <wayland-client.h>
43 #include "weston-output-capture-client-protocol.h"
44 #include "shared/os-compatibility.h"
45 #include "shared/xalloc.h"
46 #include "shared/file-util.h"
47 #include "shared/pixel-formats.h"
48 #include "shared/helpers.h"
49 #include "shared/string-helpers.h"
51 static int opts = 0x0;
53 #define OPT_SCREENSHOT_OUTPUT 1
54 #define OPT_SHOW_ALL_OUTPUTS 2
55 #define OPT_SCREENSHOT_ALL_OUTPUTS 3
57 struct screenshooter_app {
58 struct wl_display *display;
59 struct wl_registry *registry;
61 struct weston_capture_v1 *capture_factory;
63 struct wl_list output_list; /* struct screenshooter_output::link */
70 struct screenshooter_buffer {
73 struct wl_buffer *wl_buffer;
74 pixman_image_t *image;
77 struct screenshooter_output {
78 struct screenshooter_app *app;
79 struct wl_list link; /* struct screenshooter_app::output_list */
81 struct wl_output *wl_output;
82 int offset_x, offset_y;
85 struct weston_capture_source_v1 *source;
89 const struct pixel_format_info *fmt;
90 struct screenshooter_buffer *buffer;
100 static struct screenshooter_buffer *
101 screenshot_create_shm_buffer(struct screenshooter_app *app,
102 size_t width, size_t height,
103 const struct pixel_format_info *fmt)
105 struct screenshooter_buffer *buffer;
106 struct wl_shm_pool *pool;
113 assert(fmt && fmt->bpp > 0);
114 assert(fmt->pixman_format);
116 buffer = xzalloc(sizeof *buffer);
118 bytes_pp = fmt->bpp / 8;
119 stride = width * bytes_pp;
120 buffer->len = stride * height;
122 assert(width == stride / bytes_pp);
123 assert(height == buffer->len / stride);
125 fd = os_create_anonymous_file(buffer->len);
127 fprintf(stderr, "creating a buffer file for %zd B failed: %s\n",
128 buffer->len, strerror(errno));
133 buffer->data = mmap(NULL, buffer->len, PROT_READ | PROT_WRITE,
135 if (buffer->data == MAP_FAILED) {
136 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
142 pool = wl_shm_create_pool(app->shm, fd, buffer->len);
145 wl_shm_pool_create_buffer(pool, 0, width, height, stride,
146 pixel_format_get_shm_format(fmt));
147 wl_shm_pool_destroy(pool);
149 buffer->image = pixman_image_create_bits(fmt->pixman_format,
151 buffer->data, stride);
152 if (!buffer->image) {
153 fprintf(stderr, "Failed to create buffer image!\n");
163 screenshooter_buffer_destroy(struct screenshooter_buffer *buffer)
168 pixman_image_unref(buffer->image);
169 munmap(buffer->data, buffer->len);
170 wl_buffer_destroy(buffer->wl_buffer);
175 capture_source_handle_format(void *data,
176 struct weston_capture_source_v1 *proxy,
179 struct screenshooter_output *output = data;
181 assert(output->source == proxy);
183 output->fmt = pixel_format_get_info(drm_format);
187 capture_source_handle_size(void *data,
188 struct weston_capture_source_v1 *proxy,
189 int32_t width, int32_t height)
191 struct screenshooter_output *output = data;
196 output->buffer_width = width;
197 output->buffer_height = height;
201 capture_source_handle_complete(void *data,
202 struct weston_capture_source_v1 *proxy)
204 struct screenshooter_output *output = data;
206 output->app->waitcount--;
210 capture_source_handle_retry(void *data,
211 struct weston_capture_source_v1 *proxy)
213 struct screenshooter_output *output = data;
215 output->app->waitcount--;
216 output->app->retry = true;
220 capture_source_handle_failed(void *data,
221 struct weston_capture_source_v1 *proxy,
224 struct screenshooter_output *output = data;
226 output->app->waitcount--;
227 output->app->failed = true;
230 fprintf(stderr, "Output capture error: %s\n", msg);
233 static const struct weston_capture_source_v1_listener capture_source_handlers = {
234 .format = capture_source_handle_format,
235 .size = capture_source_handle_size,
236 .complete = capture_source_handle_complete,
237 .retry = capture_source_handle_retry,
238 .failed = capture_source_handle_failed,
242 display_handle_geometry(void *data,
243 struct wl_output *wl_output,
255 display_handle_mode(void *data,
256 struct wl_output *wl_output,
265 display_handle_scale(void *data,
266 struct wl_output *wl_output,
272 display_handle_name(void *data, struct wl_output *wl_output, const char *name)
274 struct screenshooter_output *output = data;
275 output->name = strdup(name);
279 display_handle_description(void *data, struct wl_output *wl_output, const char *desc)
284 display_handle_done(void *data,
285 struct wl_output *wl_output)
290 static const struct wl_output_listener output_listener = {
291 display_handle_geometry,
294 display_handle_scale,
296 display_handle_description,
300 create_output(struct screenshooter_app *app, uint32_t output_name, uint32_t version)
302 struct screenshooter_output *output;
304 version = MIN(version, 4);
305 output = xzalloc(sizeof *output);
307 output->wl_output = wl_registry_bind(app->registry, output_name,
308 &wl_output_interface, version);
309 if (!output->wl_output) {
310 fprintf(stderr, "Failed to get bind output!\n");
314 wl_output_add_listener(output->wl_output, &output_listener, output);
316 output->source = weston_capture_v1_create(app->capture_factory,
318 WESTON_CAPTURE_V1_SOURCE_FRAMEBUFFER);
319 if (!output->source) {
320 fprintf(stderr, "Failed to get a capture source!\n");
323 weston_capture_source_v1_add_listener(output->source,
324 &capture_source_handlers, output);
326 wl_list_insert(&app->output_list, &output->link);
330 destroy_output(struct screenshooter_output *output)
332 weston_capture_source_v1_destroy(output->source);
334 if (wl_output_get_version(output->wl_output) >= WL_OUTPUT_RELEASE_SINCE_VERSION)
335 wl_output_release(output->wl_output);
337 wl_output_destroy(output->wl_output);
339 screenshooter_buffer_destroy(output->buffer);
340 wl_list_remove(&output->link);
346 handle_global(void *data, struct wl_registry *registry,
347 uint32_t name, const char *interface, uint32_t version)
349 struct screenshooter_app *app = data;
351 if (strcmp(interface, wl_output_interface.name) == 0) {
352 create_output(app, name, version);
353 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
354 app->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
356 * Not listening for format advertisements,
357 * weston_capture_source_v1.format event tells us what to use.
359 } else if (strcmp(interface, weston_capture_v1_interface.name) == 0) {
360 app->capture_factory = wl_registry_bind(registry, name,
361 &weston_capture_v1_interface,
367 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
369 /* Dynamic output removals will just fail the respective shot. */
372 static const struct wl_registry_listener registry_listener = {
378 screenshooter_output_capture(struct screenshooter_output *output)
380 screenshooter_buffer_destroy(output->buffer);
381 output->buffer = screenshot_create_shm_buffer(output->app,
382 output->buffer_width,
383 output->buffer_height,
385 if (!output->buffer) {
386 fprintf(stderr, "Failed to create output buffer\n");
390 weston_capture_source_v1_capture(output->source,
391 output->buffer->wl_buffer);
392 output->app->waitcount++;
396 screenshot_write_png_per_output(const struct buffer_size *buff_size,
397 struct screenshooter_output *output,
400 pixman_image_t *shot;
401 cairo_surface_t *surface;
403 char filepath[PATH_MAX];
404 char *filename_to_write;
406 shot = pixman_image_create_bits(PIXMAN_a8r8g8b8,
407 buff_size->width, buff_size->height,
410 fprintf(stderr, "Failed to create shot\n");
415 pixman_image_composite32(PIXMAN_OP_SRC,
416 output->buffer->image, /* src */
421 output->offset_x, output->offset_y, /* dst x,y */
422 output->buffer_width, output->buffer_height);
424 surface = cairo_image_surface_create_for_data((void *)pixman_image_get_data(shot),
426 pixman_image_get_width(shot),
427 pixman_image_get_height(shot),
428 pixman_image_get_stride(shot));
430 str_printf(&filename_to_write, "agl-screenshot-%s-", fn);
432 str_printf(&filename_to_write, "agl-screenshot-");
434 fp = file_create_dated(getenv("XDG_PICTURES_DIR"), filename_to_write,
435 ".png", filepath, sizeof(filepath));
438 cairo_surface_write_to_png(surface, filepath);
440 cairo_surface_destroy(surface);
441 pixman_image_unref(shot);
445 screenshot_set_buffer_size_per_output(struct buffer_size *buff_size,
446 struct screenshooter_output *output)
448 buff_size->min_x = MIN(buff_size->min_x, output->offset_x);
449 buff_size->min_y = MIN(buff_size->min_y, output->offset_y);
450 buff_size->max_x = MAX(buff_size->max_x, output->offset_x + output->buffer_width);
451 buff_size->max_y = MAX(buff_size->max_y, output->offset_y + output->buffer_height);
453 buff_size->width = buff_size->max_x - buff_size->min_x;
454 buff_size->height = buff_size->max_y - buff_size->min_y;
458 screenshot_compute_output_offset(int *pos, struct screenshooter_output *sh_output)
460 sh_output->offset_x = *pos;
461 *pos += sh_output->buffer_width;
464 static struct screenshooter_output *
465 agl_shooter_search_for_output(const char *output_name,
466 struct screenshooter_app *app)
468 struct screenshooter_output *found_output = NULL;
469 struct screenshooter_output *output;
474 wl_list_for_each(output, &app->output_list, link) {
475 if (output->name && strcmp(output->name, output_name) == 0) {
476 found_output = output;
485 agl_shooter_search_get_output_name(struct screenshooter_output *sh_output)
487 struct screenshooter_app *app;
488 struct screenshooter_output *output;
493 app = sh_output->app;
495 wl_list_for_each(output, &app->output_list, link) {
496 if (output == sh_output) {
505 agl_shooter_display_all_outputs(struct screenshooter_app *app)
507 struct screenshooter_output *output;
509 wl_list_for_each(output, &app->output_list, link) {
510 fprintf(stdout, "Output '%s'\n", output->name);
515 agl_shooter_screenshot_output(struct screenshooter_output *output, int *pos)
517 struct buffer_size buff_size = {};
518 struct screenshooter_app *app = output->app;
523 screenshooter_output_capture(output);
525 while (app->waitcount > 0 && !app->failed) {
526 if (wl_display_dispatch(app->display) < 0)
528 assert(app->waitcount >= 0);
530 } while (app->retry && !app->failed);
533 screenshot_compute_output_offset(pos, output);
534 screenshot_set_buffer_size_per_output(&buff_size, output);
536 output_name = agl_shooter_search_get_output_name(output);
538 screenshot_write_png_per_output(&buff_size, output, output_name);
540 fprintf(stderr, "Error: screenshot or protocol failure\n");
546 agl_shooter_screenshot_all_outputs(struct screenshooter_app *app)
548 struct screenshooter_output *output;
551 wl_list_for_each(output, &app->output_list, link)
552 agl_shooter_screenshot_output(output, &pos);
556 print_usage_and_exit(void)
558 fprintf(stderr, "./agl-screenshooter [-o OUTPUT_NAME] [-l] [-a]\n");
560 fprintf(stderr, "\t-o OUTPUT_NAME -- take a screenshot of the output "
561 "specified by OUTPUT_NAME\n");
562 fprintf(stderr, "\t-a -- take a screenshot of all the outputs found\n");
563 fprintf(stderr, "\t-l -- list all the outputs found\n");
568 main(int argc, char *argv[])
570 struct wl_display *display;
571 struct screenshooter_output *output;
572 struct screenshooter_output *sh_output = NULL;
573 struct screenshooter_output *tmp_output;
574 struct screenshooter_app app = {};
577 char *output_name = NULL;
580 wl_list_init(&app.output_list);
582 static struct option long_options[] = {
583 {"output", required_argument, 0, 'o' },
584 {"list", required_argument, 0, 'l' },
585 {"all", required_argument, 0, 'a' },
586 {"help", no_argument , 0, 'h' },
590 while ((c = getopt_long(argc, argv, "o:lah",
591 long_options, &option_index)) != -1) {
594 output_name = optarg;
595 opts |= (1 << OPT_SCREENSHOT_OUTPUT);
598 opts |= (1 << OPT_SHOW_ALL_OUTPUTS);
601 opts |= (1 << OPT_SCREENSHOT_ALL_OUTPUTS);
604 print_usage_and_exit();
608 display = wl_display_connect(NULL);
609 if (display == NULL) {
610 fprintf(stderr, "failed to create display: %s\n",
615 app.display = display;
616 app.registry = wl_display_get_registry(display);
617 wl_registry_add_listener(app.registry, ®istry_listener, &app);
619 /* Process wl_registry advertisements */
620 wl_display_roundtrip(display);
623 fprintf(stderr, "Error: display does not support wl_shm\n");
626 if (!app.capture_factory) {
627 fprintf(stderr, "Error: display does not support weston_capture_v1\n");
631 /* Process initial events for wl_output and weston_capture_source_v1 */
632 wl_display_roundtrip(display);
634 if (opts & (1 << OPT_SHOW_ALL_OUTPUTS)) {
635 agl_shooter_display_all_outputs(&app);
639 if (opts & (1 << OPT_SCREENSHOT_ALL_OUTPUTS)) {
640 agl_shooter_screenshot_all_outputs(&app);
646 sh_output = agl_shooter_search_for_output(output_name, &app);
648 if (!sh_output && (opts & (1 << OPT_SCREENSHOT_OUTPUT))) {
649 fprintf(stderr, "Could not find an output matching '%s'\n",
654 /* if we're still here just pick the first one available
655 * and use that. Still useful in case we are run without
656 * any args whatsoever */
658 sh_output = container_of(app.output_list.next,
659 struct screenshooter_output, link);
661 /* take a screenshot only of that specific output */
662 agl_shooter_screenshot_output(sh_output, &pos);
664 wl_list_for_each_safe(output, tmp_output, &app.output_list, link)
665 destroy_output(output);
667 weston_capture_v1_destroy(app.capture_factory);
668 wl_shm_destroy(app.shm);
669 wl_registry_destroy(app.registry);
670 wl_display_disconnect(display);