| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright © 2008 Kristian Høgsberg | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a | 
					
						
							|  |  |  |  * copy of this software and associated documentation files (the "Software"), | 
					
						
							|  |  |  |  * to deal in the Software without restriction, including without limitation | 
					
						
							|  |  |  |  * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
					
						
							|  |  |  |  * and/or sell copies of the Software, and to permit persons to whom the | 
					
						
							|  |  |  |  * Software is furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice (including the next | 
					
						
							|  |  |  |  * paragraph) shall be included in all copies or substantial portions of the | 
					
						
							|  |  |  |  * Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL | 
					
						
							|  |  |  |  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
					
						
							|  |  |  |  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
					
						
							|  |  |  |  * DEALINGS IN THE SOFTWARE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _XOPEN_SOURCE 700
 | 
					
						
							|  |  |  | #define _POSIX_C_SOURCE 199309L
 | 
					
						
							| 
									
										
										
										
											2018-02-15 07:17:02 +01:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/mman.h>
 | 
					
						
							| 
									
										
										
										
											2018-02-15 07:17:02 +01:00
										 |  |  | #include <sys/param.h>
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | #include <sys/wait.h>
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <wayland-client.h>
 | 
					
						
							| 
									
										
										
										
											2018-01-15 22:51:00 -05:00
										 |  |  | #include <wlr/util/log.h>
 | 
					
						
							| 
									
										
										
										
											2018-02-15 07:17:02 +01:00
										 |  |  | #include "screenshooter-client-protocol.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | static struct wl_shm *shm = NULL; | 
					
						
							|  |  |  | static struct orbital_screenshooter *screenshooter = NULL; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | static struct wl_list output_list; | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | static bool buffer_copy_done; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct screenshooter_output { | 
					
						
							|  |  |  | 	struct wl_output *output; | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	int width, height; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	struct wl_list link; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | static void output_handle_geometry(void *data, struct wl_output *wl_output, | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 		int x, int y, int physical_width, int physical_height, int subpixel, | 
					
						
							|  |  |  | 		const char *make, const char *model, int transform) { | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	// No-op
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | static void output_handle_mode(void *data, struct wl_output *wl_output, | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 		uint32_t flags, int width, int height, int refresh) { | 
					
						
							|  |  |  | 	struct screenshooter_output *output = wl_output_get_user_data(wl_output); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { | 
					
						
							|  |  |  | 		output->width = width; | 
					
						
							|  |  |  | 		output->height = height; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | static void output_handle_done(void *data, struct wl_output *wl_output) { | 
					
						
							| 
									
										
										
										
											2018-01-23 22:06:54 +01:00
										 |  |  | 	// No-op
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | static const struct wl_output_listener output_listener = { | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | 	.geometry = output_handle_geometry, | 
					
						
							|  |  |  | 	.mode = output_handle_mode, | 
					
						
							|  |  |  | 	.done = output_handle_done, | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | static void screenshot_done(void *data, struct orbital_screenshot *screenshot) { | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	buffer_copy_done = true; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct orbital_screenshot_listener screenshot_listener = { | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | 	.done = screenshot_done, | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | static void handle_global(void *data, struct wl_registry *registry, | 
					
						
							|  |  |  | 		uint32_t name, const char *interface, uint32_t version) { | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	static struct screenshooter_output *output; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (strcmp(interface, "wl_output") == 0) { | 
					
						
							| 
									
										
										
										
											2017-12-28 16:28:19 +01:00
										 |  |  | 		output = calloc(1, sizeof(*output)); | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 		output->output = wl_registry_bind(registry, name, &wl_output_interface, | 
					
						
							|  |  |  | 			1); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 		wl_list_insert(&output_list, &output->link); | 
					
						
							|  |  |  | 		wl_output_add_listener(output->output, &output_listener, output); | 
					
						
							|  |  |  | 	} else if (strcmp(interface, "wl_shm") == 0) { | 
					
						
							|  |  |  | 		shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); | 
					
						
							|  |  |  | 	} else if (strcmp(interface, "orbital_screenshooter") == 0) { | 
					
						
							|  |  |  | 		screenshooter = wl_registry_bind(registry, name, | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 			&orbital_screenshooter_interface, 1); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | static void handle_global_remove(void *data, struct wl_registry *registry, | 
					
						
							|  |  |  | 		uint32_t name) { | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	// Who cares?
 | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct wl_registry_listener registry_listener = { | 
					
						
							| 
									
										
										
										
											2017-10-07 19:30:00 +02:00
										 |  |  | 	.global = handle_global, | 
					
						
							|  |  |  | 	.global_remove = handle_global_remove, | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 12:04:32 +01:00
										 |  |  | static int backingfile(off_t size) { | 
					
						
							| 
									
										
										
										
											2018-02-24 23:55:48 +01:00
										 |  |  | 	char template[] = "/tmp/wlroots-shared-XXXXXX"; | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	int fd = mkstemp(template); | 
					
						
							| 
									
										
										
										
											2018-02-22 12:04:32 +01:00
										 |  |  | 	if (fd < 0) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	int ret; | 
					
						
							|  |  |  | 	while ((ret = ftruncate(fd, size)) == EINTR) { | 
					
						
							|  |  |  | 		// No-op
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-22 12:04:32 +01:00
										 |  |  | 	if (ret < 0) { | 
					
						
							|  |  |  | 		close(fd); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	unlink(template); | 
					
						
							|  |  |  | 	return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | static struct wl_buffer *create_shm_buffer(int width, int height, | 
					
						
							|  |  |  | 		void **data_out) { | 
					
						
							|  |  |  | 	int stride = width * 4; | 
					
						
							|  |  |  | 	int size = stride * height; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 12:04:32 +01:00
										 |  |  | 	int fd = backingfile(size); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	if (fd < 0) { | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 		fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	if (data == MAP_FAILED) { | 
					
						
							|  |  |  | 		fprintf(stderr, "mmap failed: %m\n"); | 
					
						
							|  |  |  | 		close(fd); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	close(fd); | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height, | 
					
						
							|  |  |  | 		stride, WL_SHM_FORMAT_XRGB8888); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	wl_shm_pool_destroy(pool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*data_out = data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return buffer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | static void write_image(const char *filename, int width, int height, | 
					
						
							|  |  |  | 		void *data) { | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 	char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
 | 
					
						
							|  |  |  | 	sprintf(size, "%dx%d+0", width, height); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int fd[2]; | 
					
						
							| 
									
										
										
										
											2017-11-11 16:47:54 +01:00
										 |  |  | 	if (pipe(fd) != 0) { | 
					
						
							|  |  |  | 		fprintf(stderr, "cannot create pipe: %s\n", strerror(errno)); | 
					
						
							|  |  |  | 		exit(EXIT_FAILURE); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pid_t child = fork(); | 
					
						
							|  |  |  | 	if (child < 0) { | 
					
						
							|  |  |  | 		fprintf(stderr, "fork() failed\n"); | 
					
						
							|  |  |  | 		exit(EXIT_FAILURE); | 
					
						
							|  |  |  | 	} else if (child != 0) { | 
					
						
							|  |  |  | 		close(fd[0]); | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 		if (write(fd[1], data, 4 * width * height) < 0) { | 
					
						
							| 
									
										
										
										
											2017-11-11 16:47:54 +01:00
										 |  |  | 			fprintf(stderr, "write() failed: %s\n", strerror(errno)); | 
					
						
							|  |  |  | 			exit(EXIT_FAILURE); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 		close(fd[1]); | 
					
						
							|  |  |  | 		waitpid(child, NULL, 0); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		close(fd[1]); | 
					
						
							|  |  |  | 		if (dup2(fd[0], 0) != 0) { | 
					
						
							|  |  |  | 			fprintf(stderr, "cannot dup the pipe\n"); | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 			exit(EXIT_FAILURE); | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		close(fd[0]); | 
					
						
							| 
									
										
										
										
											2018-01-23 22:06:54 +01:00
										 |  |  | 		// We requested WL_SHM_FORMAT_XRGB8888 in little endian, so that's BGRA
 | 
					
						
							|  |  |  | 		// in big endian.
 | 
					
						
							|  |  |  | 		execlp("convert", "convert", "-depth", "8", "-size", size, "bgra:-", | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 			"-alpha", "opaque", filename, NULL); | 
					
						
							|  |  |  | 		fprintf(stderr, "cannot execute convert\n"); | 
					
						
							|  |  |  | 		exit(EXIT_FAILURE); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | int main(int argc, char *argv[]) { | 
					
						
							| 
									
										
										
										
											2018-07-09 22:49:54 +01:00
										 |  |  | 	wlr_log_init(WLR_DEBUG, NULL); | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	struct wl_display * display = wl_display_connect(NULL); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	if (display == NULL) { | 
					
						
							|  |  |  | 		fprintf(stderr, "failed to create display: %m\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wl_list_init(&output_list); | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	struct wl_registry *registry = wl_display_get_registry(display); | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	wl_registry_add_listener(registry, ®istry_listener, NULL); | 
					
						
							|  |  |  | 	wl_display_dispatch(display); | 
					
						
							|  |  |  | 	wl_display_roundtrip(display); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (screenshooter == NULL) { | 
					
						
							|  |  |  | 		fprintf(stderr, "display doesn't support screenshooter\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 	int i = 0; | 
					
						
							| 
									
										
										
										
											2017-10-07 19:22:55 +02:00
										 |  |  | 	struct screenshooter_output *output; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	wl_list_for_each(output, &output_list, link) { | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 		void *data = NULL; | 
					
						
							|  |  |  | 		struct wl_buffer *buffer = | 
					
						
							|  |  |  | 			create_shm_buffer(output->width, output->height, &data); | 
					
						
							|  |  |  | 		if (buffer == NULL) { | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		struct orbital_screenshot *screenshot = orbital_screenshooter_shoot( | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 			screenshooter, output->output, buffer); | 
					
						
							|  |  |  | 		orbital_screenshot_add_listener(screenshot, &screenshot_listener, | 
					
						
							|  |  |  | 			screenshot); | 
					
						
							|  |  |  | 		buffer_copy_done = false; | 
					
						
							| 
									
										
										
										
											2018-01-21 16:28:21 +01:00
										 |  |  | 		while (!buffer_copy_done) { | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 			wl_display_roundtrip(display); | 
					
						
							| 
									
										
										
										
											2018-01-21 16:28:21 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-14 19:21:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		char filename[24 + 10]; // int32_t are max 10 digits
 | 
					
						
							|  |  |  | 		snprintf(filename, sizeof(filename), "wayland-screenshot-%d.png", i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		write_image(filename, output->width, output->height, data); | 
					
						
							|  |  |  | 		wl_buffer_destroy(buffer); | 
					
						
							|  |  |  | 		++i; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 19:12:47 +02:00
										 |  |  | 	return EXIT_SUCCESS; | 
					
						
							| 
									
										
										
										
											2017-09-08 18:09:09 +02:00
										 |  |  | } |