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 | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 30 | #include <glib.h> |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 31 | #include <gdk-pixbuf/gdk-pixbuf.h> |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 32 | |
| 33 | #include "wayland-client.h" |
| 34 | #include "wayland-glib.h" |
Kristian Høgsberg | 3dd66d6 | 2010-09-14 16:23:24 -0400 | [diff] [blame] | 35 | #include "screenshooter-client-protocol.h" |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 36 | |
| 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_output *output; |
| 42 | static struct wl_shm *shm; |
| 43 | static struct wl_visual *visual; |
| 44 | static struct screenshooter *screenshooter; |
| 45 | static int output_width, output_height; |
| 46 | |
| 47 | static void |
| 48 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 49 | struct wl_output *wl_output, |
| 50 | int x, |
| 51 | int y, |
| 52 | int physical_width, |
| 53 | int physical_height, |
| 54 | int subpixel, |
| 55 | const char *make, |
| 56 | const char *model) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | display_handle_mode(void *data, |
| 62 | struct wl_output *wl_output, |
| 63 | uint32_t flags, |
| 64 | int width, |
| 65 | int height, |
| 66 | int refresh) |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 67 | { |
| 68 | output_width = width; |
| 69 | output_height = height; |
| 70 | } |
| 71 | |
| 72 | static const struct wl_output_listener output_listener = { |
| 73 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 74 | display_handle_mode |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 77 | static void |
| 78 | handle_global(struct wl_display *display, uint32_t id, |
| 79 | const char *interface, uint32_t version, void *data) |
| 80 | { |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 81 | static int visual_count; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 82 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 83 | if (strcmp(interface, "wl_output") == 0) { |
| 84 | output = wl_output_create(display, id, 1); |
| 85 | wl_output_add_listener(output, &output_listener, NULL); |
| 86 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 87 | shm = wl_shm_create(display, id, 1); |
| 88 | } else if (strcmp(interface, "wl_visual") == 0) { |
| 89 | if (visual_count++ == 1) |
| 90 | visual = wl_visual_create(display, id, 1); |
| 91 | } else if (strcmp(interface, "screenshooter") == 0) { |
| 92 | screenshooter = screenshooter_create(display, id, 1); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | sync_callback(void *data) |
| 98 | { |
| 99 | int *done = data; |
| 100 | |
| 101 | *done = 1; |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | roundtrip(struct wl_display *display) |
| 106 | { |
| 107 | int done; |
| 108 | |
| 109 | done = 0; |
| 110 | wl_display_sync_callback(display, sync_callback, &done); |
| 111 | wl_display_iterate(display, WL_DISPLAY_WRITABLE); |
| 112 | while (!done) |
| 113 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
| 114 | } |
| 115 | |
| 116 | static struct wl_buffer * |
| 117 | create_shm_buffer(int width, int height, void **data_out) |
| 118 | { |
| 119 | char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 120 | struct wl_buffer *buffer; |
| 121 | int fd, size, stride; |
| 122 | void *data; |
| 123 | |
| 124 | fd = mkstemp(filename); |
| 125 | if (fd < 0) { |
| 126 | fprintf(stderr, "open %s failed: %m\n", filename); |
| 127 | return NULL; |
| 128 | } |
| 129 | stride = width * 4; |
| 130 | size = stride * height; |
| 131 | if (ftruncate(fd, size) < 0) { |
| 132 | fprintf(stderr, "ftruncate failed: %m\n"); |
| 133 | close(fd); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 138 | unlink(filename); |
| 139 | |
| 140 | if (data == MAP_FAILED) { |
| 141 | fprintf(stderr, "mmap failed: %m\n"); |
| 142 | close(fd); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual); |
| 147 | |
| 148 | close(fd); |
| 149 | |
| 150 | *data_out = data; |
| 151 | |
| 152 | return buffer; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 155 | static void |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame^] | 156 | write_png(int width, int height, void *data) |
| 157 | { |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 158 | GdkPixbuf *pixbuf, *normal; |
| 159 | GError *error = NULL; |
| 160 | |
| 161 | g_type_init(); |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame^] | 162 | pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 163 | 8, width, height, width * 4, NULL, |
| 164 | NULL); |
| 165 | |
| 166 | normal = gdk_pixbuf_flip(pixbuf, FALSE); |
| 167 | gdk_pixbuf_save(normal, "wayland-screenshot.png", "png", &error, NULL); |
| 168 | g_object_unref(normal); |
| 169 | g_object_unref(pixbuf); |
| 170 | } |
| 171 | |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 172 | int main(int argc, char *argv[]) |
| 173 | { |
| 174 | struct wl_display *display; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 175 | struct wl_buffer *buffer; |
Tiago Vignatti | 4d0d203 | 2011-07-26 11:42:59 +0300 | [diff] [blame] | 176 | void *data = NULL; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 177 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 178 | display = wl_display_connect(NULL); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 179 | if (display == NULL) { |
| 180 | fprintf(stderr, "failed to create display: %m\n"); |
| 181 | return -1; |
| 182 | } |
| 183 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 184 | wl_display_add_global_listener(display, handle_global, &screenshooter); |
Kristian Høgsberg | 03fd86b | 2009-02-10 14:15:44 -0500 | [diff] [blame] | 185 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 186 | roundtrip(display); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 187 | if (screenshooter == NULL) { |
| 188 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame] | 192 | buffer = create_shm_buffer(output_width, output_height, &data); |
| 193 | screenshooter_shoot(screenshooter, output, buffer); |
| 194 | roundtrip(display); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 195 | |
Kristian Høgsberg | 8417d43 | 2011-07-27 05:58:57 -0700 | [diff] [blame^] | 196 | write_png(output_width, output_height, data); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |