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> |
| 29 | #include <sys/mman.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 30 | #include <cairo.h> |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 31 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 32 | #include <wayland-client.h> |
Kristian Høgsberg | 3dd66d6 | 2010-09-14 16:23:24 -0400 | [diff] [blame] | 33 | #include "screenshooter-client-protocol.h" |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 34 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 35 | #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
| 36 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 37 | /* The screenshooter is a good example of a custom object exposed by |
| 38 | * the compositor and serves as a test bed for implementing client |
| 39 | * side marshalling outside libwayland.so */ |
| 40 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 41 | static struct wl_shm *shm; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 42 | static struct screenshooter *screenshooter; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 43 | static struct wl_list output_list; |
| 44 | |
| 45 | struct screenshooter_output { |
| 46 | struct wl_output *output; |
| 47 | struct wl_buffer *buffer; |
| 48 | int width, height, offset_x, offset_y; |
| 49 | struct wl_list link; |
| 50 | }; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 51 | |
| 52 | static void |
| 53 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 54 | struct wl_output *wl_output, |
| 55 | int x, |
| 56 | int y, |
| 57 | int physical_width, |
| 58 | int physical_height, |
| 59 | int subpixel, |
| 60 | const char *make, |
| 61 | const char *model) |
| 62 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 63 | struct screenshooter_output *output; |
| 64 | |
| 65 | output = wl_output_get_user_data(wl_output); |
| 66 | |
| 67 | if (wl_output == output->output) { |
| 68 | output->offset_x = x; |
| 69 | output->offset_y = y; |
| 70 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void |
| 74 | display_handle_mode(void *data, |
| 75 | struct wl_output *wl_output, |
| 76 | uint32_t flags, |
| 77 | int width, |
| 78 | int height, |
| 79 | int refresh) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 80 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 81 | struct screenshooter_output *output; |
| 82 | |
| 83 | output = wl_output_get_user_data(wl_output); |
| 84 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 85 | if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 86 | output->width = width; |
| 87 | output->height = height; |
| 88 | } |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static const struct wl_output_listener output_listener = { |
| 92 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 93 | display_handle_mode |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 96 | static void |
| 97 | handle_global(struct wl_display *display, uint32_t id, |
| 98 | const char *interface, uint32_t version, void *data) |
| 99 | { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 100 | static struct screenshooter_output *output; |
| 101 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 102 | if (strcmp(interface, "wl_output") == 0) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 103 | output = malloc(sizeof *output); |
| 104 | output->output = wl_display_bind(display, id, &wl_output_interface); |
| 105 | wl_list_insert(&output_list, &output->link); |
| 106 | wl_output_add_listener(output->output, &output_listener, output); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 107 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 108 | shm = wl_display_bind(display, id, &wl_shm_interface); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 109 | } else if (strcmp(interface, "screenshooter") == 0) { |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 110 | screenshooter = wl_display_bind(display, id, &screenshooter_interface); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 114 | static struct wl_buffer * |
| 115 | create_shm_buffer(int width, int height, void **data_out) |
| 116 | { |
| 117 | char filename[] = "/tmp/wayland-shm-XXXXXX"; |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 118 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 119 | struct wl_buffer *buffer; |
| 120 | int fd, size, stride; |
| 121 | void *data; |
| 122 | |
| 123 | fd = mkstemp(filename); |
| 124 | if (fd < 0) { |
| 125 | fprintf(stderr, "open %s failed: %m\n", filename); |
| 126 | return NULL; |
| 127 | } |
| 128 | stride = width * 4; |
| 129 | size = stride * height; |
| 130 | if (ftruncate(fd, size) < 0) { |
| 131 | fprintf(stderr, "ftruncate failed: %m\n"); |
| 132 | close(fd); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 137 | unlink(filename); |
| 138 | |
| 139 | if (data == MAP_FAILED) { |
| 140 | fprintf(stderr, "mmap failed: %m\n"); |
| 141 | close(fd); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 145 | pool = wl_shm_create_pool(shm, fd, size); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 146 | close(fd); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 147 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 148 | WL_SHM_FORMAT_XRGB8888); |
| 149 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 150 | |
| 151 | *data_out = data; |
| 152 | |
| 153 | return buffer; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 156 | static void |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame] | 157 | write_png(int width, int height, void *data) |
| 158 | { |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 159 | cairo_surface_t *surface; |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 160 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 161 | surface = cairo_image_surface_create_for_data(data, |
| 162 | CAIRO_FORMAT_ARGB32, |
| 163 | width, height, width * 4); |
| 164 | cairo_surface_write_to_png(surface, "wayland-screenshot.png"); |
| 165 | cairo_surface_destroy(surface); |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 166 | } |
| 167 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 168 | int main(int argc, char *argv[]) |
| 169 | { |
| 170 | struct wl_display *display; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 171 | struct wl_buffer *buffer; |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 172 | void *data = NULL; |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 173 | struct screenshooter_output *output, *next; |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 174 | int width = 0, height = 0; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 175 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 176 | display = wl_display_connect(NULL); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 177 | if (display == NULL) { |
| 178 | fprintf(stderr, "failed to create display: %m\n"); |
| 179 | return -1; |
| 180 | } |
| 181 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 182 | wl_list_init(&output_list); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 183 | wl_display_add_global_listener(display, handle_global, &screenshooter); |
Kristian Høgsberg | 03fd86b | 2009-02-10 14:15:44 -0500 | [diff] [blame] | 184 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 185 | wl_display_roundtrip(display); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 186 | if (screenshooter == NULL) { |
| 187 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 191 | wl_list_for_each(output, &output_list, link) { |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 192 | width = MAX(width, output->offset_x + output->width); |
| 193 | height = MAX(height, output->offset_y + output->height); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 196 | buffer = create_shm_buffer(width, height, &data); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 197 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 198 | wl_list_for_each_safe(output, next, &output_list, link) { |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 199 | screenshooter_shoot(screenshooter, output->output, buffer); |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 200 | free(output); |
Scott Moreau | 80d27b7 | 2012-04-04 11:49:21 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 203 | wl_display_roundtrip(display); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 204 | |
Kristian Høgsberg | 1a36156 | 2012-04-04 14:52:35 -0400 | [diff] [blame^] | 205 | write_png(width, height, data); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |