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 | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <fcntl.h> |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 28 | #include <unistd.h> |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 29 | #include <limits.h> |
| 30 | #include <sys/param.h> |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 31 | #include <sys/mman.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 32 | #include <cairo.h> |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 33 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 34 | #include <wayland-client.h> |
Kristian Høgsberg | 3dd66d6 | 2010-09-14 16:23:24 -0400 | [diff] [blame] | 35 | #include "screenshooter-client-protocol.h" |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 36 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 37 | |
| 38 | /* The screenshooter is a good example of a custom object exposed by |
| 39 | * the compositor and serves as a test bed for implementing client |
| 40 | * side marshalling outside libwayland.so */ |
| 41 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 42 | static struct wl_shm *shm; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 43 | static struct screenshooter *screenshooter; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 44 | static struct wl_list output_list; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 45 | int min_x, min_y, max_x, max_y; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 46 | int buffer_copy_done; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 47 | |
| 48 | struct screenshooter_output { |
| 49 | struct wl_output *output; |
| 50 | struct wl_buffer *buffer; |
| 51 | int width, height, offset_x, offset_y; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 52 | void *data; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 53 | struct wl_list link; |
| 54 | }; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 55 | |
| 56 | static void |
| 57 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 58 | struct wl_output *wl_output, |
| 59 | int x, |
| 60 | int y, |
| 61 | int physical_width, |
| 62 | int physical_height, |
| 63 | int subpixel, |
| 64 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 65 | const char *model, |
| 66 | int transform) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 67 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 68 | struct screenshooter_output *output; |
| 69 | |
| 70 | output = wl_output_get_user_data(wl_output); |
| 71 | |
| 72 | if (wl_output == output->output) { |
| 73 | output->offset_x = x; |
| 74 | output->offset_y = y; |
| 75 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void |
| 79 | display_handle_mode(void *data, |
| 80 | struct wl_output *wl_output, |
| 81 | uint32_t flags, |
| 82 | int width, |
| 83 | int height, |
| 84 | int refresh) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 85 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 86 | struct screenshooter_output *output; |
| 87 | |
| 88 | output = wl_output_get_user_data(wl_output); |
| 89 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame] | 90 | if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 91 | output->width = width; |
| 92 | output->height = height; |
| 93 | } |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static const struct wl_output_listener output_listener = { |
| 97 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 98 | display_handle_mode |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 101 | static void |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 102 | screenshot_done(void *data, struct screenshooter *screenshooter) |
| 103 | { |
| 104 | buffer_copy_done = 1; |
| 105 | } |
| 106 | |
| 107 | static const struct screenshooter_listener screenshooter_listener = { |
| 108 | screenshot_done |
| 109 | }; |
| 110 | |
| 111 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 112 | handle_global(void *data, struct wl_registry *registry, |
| 113 | uint32_t name, const char *interface, uint32_t version) |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 114 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 115 | static struct screenshooter_output *output; |
| 116 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 117 | if (strcmp(interface, "wl_output") == 0) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 118 | output = malloc(sizeof *output); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 119 | output->output = wl_registry_bind(registry, name, |
| 120 | &wl_output_interface, 1); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 121 | wl_list_insert(&output_list, &output->link); |
| 122 | wl_output_add_listener(output->output, &output_listener, output); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 123 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 124 | shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 125 | } else if (strcmp(interface, "screenshooter") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 126 | screenshooter = wl_registry_bind(registry, name, |
| 127 | &screenshooter_interface, 1); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame^] | 131 | static void |
| 132 | handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) |
| 133 | { |
| 134 | /* XXX: unimplemented */ |
| 135 | } |
| 136 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 137 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame^] | 138 | handle_global, |
| 139 | handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 142 | static struct wl_buffer * |
| 143 | create_shm_buffer(int width, int height, void **data_out) |
| 144 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 145 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 146 | struct wl_buffer *buffer; |
| 147 | int fd, size, stride; |
| 148 | void *data; |
| 149 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 150 | stride = width * 4; |
| 151 | size = stride * height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 152 | |
| 153 | fd = os_create_anonymous_file(size); |
| 154 | if (fd < 0) { |
| 155 | fprintf(stderr, "creating a buffer file for %d B failed: %m\n", |
| 156 | size); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | 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] | 161 | if (data == MAP_FAILED) { |
| 162 | fprintf(stderr, "mmap failed: %m\n"); |
| 163 | close(fd); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 167 | pool = wl_shm_create_pool(shm, fd, size); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 168 | close(fd); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 169 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 170 | WL_SHM_FORMAT_XRGB8888); |
| 171 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 172 | |
| 173 | *data_out = data; |
| 174 | |
| 175 | return buffer; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 178 | static void |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 179 | write_png(int width, int height) |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame] | 180 | { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 181 | int output_stride, buffer_stride, i; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 182 | cairo_surface_t *surface; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 183 | void *data, *d, *s; |
| 184 | struct screenshooter_output *output, *next; |
| 185 | |
| 186 | buffer_stride = width * 4; |
| 187 | |
| 188 | data = malloc(buffer_stride * height); |
| 189 | if (!data) |
| 190 | return; |
| 191 | |
| 192 | wl_list_for_each_safe(output, next, &output_list, link) { |
| 193 | output_stride = output->width * 4; |
| 194 | s = output->data; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 195 | d = data + (output->offset_y - min_y) * buffer_stride + |
| 196 | (output->offset_x - min_x) * 4; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < output->height; i++) { |
| 199 | memcpy(d, s, output_stride); |
| 200 | d += buffer_stride; |
| 201 | s += output_stride; |
| 202 | } |
| 203 | |
| 204 | free(output); |
| 205 | } |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 206 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 207 | surface = cairo_image_surface_create_for_data(data, |
| 208 | CAIRO_FORMAT_ARGB32, |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 209 | width, height, buffer_stride); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 210 | cairo_surface_write_to_png(surface, "wayland-screenshot.png"); |
| 211 | cairo_surface_destroy(surface); |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 212 | free(data); |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 213 | } |
| 214 | |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 215 | static int |
| 216 | set_buffer_size(int *width, int *height) |
| 217 | { |
| 218 | struct screenshooter_output *output; |
| 219 | min_x = min_y = INT_MAX; |
| 220 | max_x = max_y = INT_MIN; |
Scott Moreau | bb58983 | 2012-08-18 19:52:42 -0600 | [diff] [blame] | 221 | int position = 0; |
| 222 | |
| 223 | wl_list_for_each_reverse(output, &output_list, link) { |
| 224 | output->offset_x = position; |
| 225 | position += output->width; |
| 226 | } |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 227 | |
| 228 | wl_list_for_each(output, &output_list, link) { |
| 229 | min_x = MIN(min_x, output->offset_x); |
| 230 | min_y = MIN(min_y, output->offset_y); |
| 231 | max_x = MAX(max_x, output->offset_x + output->width); |
| 232 | max_y = MAX(max_y, output->offset_y + output->height); |
| 233 | } |
| 234 | |
| 235 | if (max_x <= min_x || max_y <= min_y) |
| 236 | return -1; |
| 237 | |
| 238 | *width = max_x - min_x; |
| 239 | *height = max_y - min_y; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 244 | int main(int argc, char *argv[]) |
| 245 | { |
| 246 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 247 | struct wl_registry *registry; |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 248 | struct screenshooter_output *output; |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 249 | int width, height; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 250 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 251 | display = wl_display_connect(NULL); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 252 | if (display == NULL) { |
| 253 | fprintf(stderr, "failed to create display: %m\n"); |
| 254 | return -1; |
| 255 | } |
| 256 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 257 | wl_list_init(&output_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 258 | registry = wl_display_get_registry(display); |
| 259 | wl_registry_add_listener(registry, ®istry_listener, NULL); |
| 260 | wl_display_dispatch(display); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 261 | wl_display_roundtrip(display); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 262 | if (screenshooter == NULL) { |
| 263 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 267 | screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter); |
| 268 | |
Scott Moreau | 2074f1d | 2012-04-20 13:37:35 -0600 | [diff] [blame] | 269 | if (set_buffer_size(&width, &height)) |
| 270 | return -1; |
| 271 | |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 272 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 273 | wl_list_for_each(output, &output_list, link) { |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 274 | output->buffer = create_shm_buffer(output->width, output->height, &output->data); |
| 275 | screenshooter_shoot(screenshooter, output->output, output->buffer); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 276 | buffer_copy_done = 0; |
| 277 | while (!buffer_copy_done) |
| 278 | wl_display_roundtrip(display); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 279 | } |
| 280 | |
Scott Moreau | 72c2372 | 2012-04-20 13:37:34 -0600 | [diff] [blame] | 281 | write_png(width, height); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |