Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Kristian Høgsberg | bdd8377 | 2013-08-12 21:45:19 -0700 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 25 | #include <stdint.h> |
Kristian Høgsberg | bdd8377 | 2013-08-12 21:45:19 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 31 | #include <unistd.h> |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 32 | #include <limits.h> |
| 33 | #include <sys/param.h> |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 34 | #include <sys/mman.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 35 | #include <cairo.h> |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 36 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 37 | #include <wayland-client.h> |
Kristian Høgsberg | 3dd66d6 | 2010-09-14 16:23:24 -0400 | [diff] [blame] | 38 | #include "screenshooter-client-protocol.h" |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 39 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 40 | |
| 41 | /* The screenshooter is a good example of a custom object exposed by |
| 42 | * the compositor and serves as a test bed for implementing client |
| 43 | * side marshalling outside libwayland.so */ |
| 44 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 45 | static struct wl_shm *shm; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 46 | static struct screenshooter *screenshooter; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 47 | static struct wl_list output_list; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 48 | int min_x, min_y, max_x, max_y; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 49 | int buffer_copy_done; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 50 | |
| 51 | struct screenshooter_output { |
| 52 | struct wl_output *output; |
| 53 | struct wl_buffer *buffer; |
| 54 | int width, height, offset_x, offset_y; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 55 | void *data; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 56 | struct wl_list link; |
| 57 | }; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 58 | |
| 59 | static void |
| 60 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 61 | struct wl_output *wl_output, |
| 62 | int x, |
| 63 | int y, |
| 64 | int physical_width, |
| 65 | int physical_height, |
| 66 | int subpixel, |
| 67 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 68 | const char *model, |
| 69 | int transform) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 70 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 71 | struct screenshooter_output *output; |
| 72 | |
| 73 | output = wl_output_get_user_data(wl_output); |
| 74 | |
| 75 | if (wl_output == output->output) { |
| 76 | output->offset_x = x; |
| 77 | output->offset_y = y; |
| 78 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Kristian Høgsberg | bdd8377 | 2013-08-12 21:45:19 -0700 | [diff] [blame] | 81 | static void * |
| 82 | xmalloc(size_t size) |
| 83 | { |
| 84 | void *p; |
| 85 | |
| 86 | p = malloc(size); |
| 87 | if (p == NULL) { |
| 88 | fprintf(stderr, "%s: out of memory\n", |
| 89 | program_invocation_short_name); |
| 90 | exit(EXIT_FAILURE); |
| 91 | } |
| 92 | |
| 93 | return p; |
| 94 | } |
| 95 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 96 | static void |
| 97 | display_handle_mode(void *data, |
| 98 | struct wl_output *wl_output, |
| 99 | uint32_t flags, |
| 100 | int width, |
| 101 | int height, |
| 102 | int refresh) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 103 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 104 | struct screenshooter_output *output; |
| 105 | |
| 106 | output = wl_output_get_user_data(wl_output); |
| 107 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame] | 108 | if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 109 | output->width = width; |
| 110 | output->height = height; |
| 111 | } |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static const struct wl_output_listener output_listener = { |
| 115 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 116 | display_handle_mode |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 119 | static void |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 120 | screenshot_done(void *data, struct screenshooter *screenshooter) |
| 121 | { |
| 122 | buffer_copy_done = 1; |
| 123 | } |
| 124 | |
| 125 | static const struct screenshooter_listener screenshooter_listener = { |
| 126 | screenshot_done |
| 127 | }; |
| 128 | |
| 129 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 130 | handle_global(void *data, struct wl_registry *registry, |
| 131 | uint32_t name, const char *interface, uint32_t version) |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 132 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 133 | static struct screenshooter_output *output; |
| 134 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 135 | if (strcmp(interface, "wl_output") == 0) { |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 136 | output = xmalloc(sizeof *output); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 137 | output->output = wl_registry_bind(registry, name, |
| 138 | &wl_output_interface, 1); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 139 | wl_list_insert(&output_list, &output->link); |
| 140 | wl_output_add_listener(output->output, &output_listener, output); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 141 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 142 | shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 143 | } else if (strcmp(interface, "screenshooter") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 144 | screenshooter = wl_registry_bind(registry, name, |
| 145 | &screenshooter_interface, 1); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 149 | static void |
| 150 | handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) |
| 151 | { |
| 152 | /* XXX: unimplemented */ |
| 153 | } |
| 154 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 155 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 156 | handle_global, |
| 157 | handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 158 | }; |
| 159 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 160 | static struct wl_buffer * |
| 161 | create_shm_buffer(int width, int height, void **data_out) |
| 162 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 163 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 164 | struct wl_buffer *buffer; |
| 165 | int fd, size, stride; |
| 166 | void *data; |
| 167 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 168 | stride = width * 4; |
| 169 | size = stride * height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 170 | |
| 171 | fd = os_create_anonymous_file(size); |
| 172 | if (fd < 0) { |
| 173 | fprintf(stderr, "creating a buffer file for %d B failed: %m\n", |
| 174 | size); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 179 | if (data == MAP_FAILED) { |
| 180 | fprintf(stderr, "mmap failed: %m\n"); |
| 181 | close(fd); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 185 | pool = wl_shm_create_pool(shm, fd, size); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 186 | close(fd); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 187 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 188 | WL_SHM_FORMAT_XRGB8888); |
| 189 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 190 | |
| 191 | *data_out = data; |
| 192 | |
| 193 | return buffer; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 196 | static void |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 197 | write_png(int width, int height) |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame] | 198 | { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 199 | int output_stride, buffer_stride, i; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 200 | cairo_surface_t *surface; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 201 | void *data, *d, *s; |
| 202 | struct screenshooter_output *output, *next; |
| 203 | |
| 204 | buffer_stride = width * 4; |
| 205 | |
Kristian Høgsberg | bdd8377 | 2013-08-12 21:45:19 -0700 | [diff] [blame] | 206 | data = xmalloc(buffer_stride * height); |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 207 | if (!data) |
| 208 | return; |
| 209 | |
| 210 | wl_list_for_each_safe(output, next, &output_list, link) { |
| 211 | output_stride = output->width * 4; |
| 212 | s = output->data; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 213 | d = data + (output->offset_y - min_y) * buffer_stride + |
| 214 | (output->offset_x - min_x) * 4; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 215 | |
| 216 | for (i = 0; i < output->height; i++) { |
| 217 | memcpy(d, s, output_stride); |
| 218 | d += buffer_stride; |
| 219 | s += output_stride; |
| 220 | } |
| 221 | |
| 222 | free(output); |
| 223 | } |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 224 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 225 | surface = cairo_image_surface_create_for_data(data, |
| 226 | CAIRO_FORMAT_ARGB32, |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 227 | width, height, buffer_stride); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 228 | cairo_surface_write_to_png(surface, "wayland-screenshot.png"); |
| 229 | cairo_surface_destroy(surface); |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 230 | free(data); |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 231 | } |
| 232 | |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 233 | static int |
| 234 | set_buffer_size(int *width, int *height) |
| 235 | { |
| 236 | struct screenshooter_output *output; |
| 237 | min_x = min_y = INT_MAX; |
| 238 | max_x = max_y = INT_MIN; |
Scott Moreau | bb58983 | 2012-08-18 19:52:42 -0600 | [diff] [blame] | 239 | int position = 0; |
| 240 | |
| 241 | wl_list_for_each_reverse(output, &output_list, link) { |
| 242 | output->offset_x = position; |
| 243 | position += output->width; |
| 244 | } |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 245 | |
| 246 | wl_list_for_each(output, &output_list, link) { |
| 247 | min_x = MIN(min_x, output->offset_x); |
| 248 | min_y = MIN(min_y, output->offset_y); |
| 249 | max_x = MAX(max_x, output->offset_x + output->width); |
| 250 | max_y = MAX(max_y, output->offset_y + output->height); |
| 251 | } |
| 252 | |
| 253 | if (max_x <= min_x || max_y <= min_y) |
| 254 | return -1; |
| 255 | |
| 256 | *width = max_x - min_x; |
| 257 | *height = max_y - min_y; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 262 | int main(int argc, char *argv[]) |
| 263 | { |
| 264 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 265 | struct wl_registry *registry; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 266 | struct screenshooter_output *output; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 267 | int width, height; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 268 | |
Kristian Høgsberg | c4a42ca | 2013-02-13 13:40:58 -0500 | [diff] [blame] | 269 | if (getenv("WAYLAND_SOCKET") == NULL) { |
Bryce W. Harrington | 3d2046e | 2013-07-23 21:53:26 +0000 | [diff] [blame] | 270 | fprintf(stderr, "%s must be launched by weston.\n" |
Kristian Høgsberg | 473f248 | 2013-08-12 22:15:38 -0700 | [diff] [blame^] | 271 | "Use the MOD+S shortcut to take a screenshot.\n", |
| 272 | program_invocation_short_name); |
Kristian Høgsberg | c4a42ca | 2013-02-13 13:40:58 -0500 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 276 | display = wl_display_connect(NULL); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 277 | if (display == NULL) { |
| 278 | fprintf(stderr, "failed to create display: %m\n"); |
| 279 | return -1; |
| 280 | } |
| 281 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 282 | wl_list_init(&output_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 283 | registry = wl_display_get_registry(display); |
| 284 | wl_registry_add_listener(registry, ®istry_listener, NULL); |
| 285 | wl_display_dispatch(display); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 286 | wl_display_roundtrip(display); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 287 | if (screenshooter == NULL) { |
| 288 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 292 | screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter); |
| 293 | |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 294 | if (set_buffer_size(&width, &height)) |
| 295 | return -1; |
| 296 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 297 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 298 | wl_list_for_each(output, &output_list, link) { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 299 | output->buffer = create_shm_buffer(output->width, output->height, &output->data); |
| 300 | screenshooter_shoot(screenshooter, output->output, output->buffer); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 301 | buffer_copy_done = 0; |
| 302 | while (!buffer_copy_done) |
| 303 | wl_display_roundtrip(display); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 304 | } |
| 305 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 306 | write_png(width, height); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |