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> |
| 31 | |
| 32 | #include "wayland-client.h" |
| 33 | #include "wayland-glib.h" |
Kristian Høgsberg | 3dd66d6 | 2010-09-14 16:23:24 -0400 | [diff] [blame] | 34 | #include "screenshooter-client-protocol.h" |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 35 | |
| 36 | /* The screenshooter is a good example of a custom object exposed by |
| 37 | * the compositor and serves as a test bed for implementing client |
| 38 | * side marshalling outside libwayland.so */ |
| 39 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 40 | static struct wl_output *output; |
| 41 | static struct wl_shm *shm; |
| 42 | static struct wl_visual *visual; |
| 43 | static struct screenshooter *screenshooter; |
| 44 | static int output_width, output_height; |
| 45 | |
| 46 | static void |
| 47 | display_handle_geometry(void *data, |
| 48 | struct wl_output *output, |
| 49 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 50 | { |
| 51 | output_width = width; |
| 52 | output_height = height; |
| 53 | } |
| 54 | |
| 55 | static const struct wl_output_listener output_listener = { |
| 56 | display_handle_geometry, |
| 57 | }; |
| 58 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 59 | static void |
| 60 | handle_global(struct wl_display *display, uint32_t id, |
| 61 | const char *interface, uint32_t version, void *data) |
| 62 | { |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 63 | static int visual_count; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 64 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 65 | if (strcmp(interface, "wl_output") == 0) { |
| 66 | output = wl_output_create(display, id, 1); |
| 67 | wl_output_add_listener(output, &output_listener, NULL); |
| 68 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 69 | shm = wl_shm_create(display, id, 1); |
| 70 | } else if (strcmp(interface, "wl_visual") == 0) { |
| 71 | if (visual_count++ == 1) |
| 72 | visual = wl_visual_create(display, id, 1); |
| 73 | } else if (strcmp(interface, "screenshooter") == 0) { |
| 74 | screenshooter = screenshooter_create(display, id, 1); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | sync_callback(void *data) |
| 80 | { |
| 81 | int *done = data; |
| 82 | |
| 83 | *done = 1; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | roundtrip(struct wl_display *display) |
| 88 | { |
| 89 | int done; |
| 90 | |
| 91 | done = 0; |
| 92 | wl_display_sync_callback(display, sync_callback, &done); |
| 93 | wl_display_iterate(display, WL_DISPLAY_WRITABLE); |
| 94 | while (!done) |
| 95 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
| 96 | } |
| 97 | |
| 98 | static struct wl_buffer * |
| 99 | create_shm_buffer(int width, int height, void **data_out) |
| 100 | { |
| 101 | char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 102 | struct wl_buffer *buffer; |
| 103 | int fd, size, stride; |
| 104 | void *data; |
| 105 | |
| 106 | fd = mkstemp(filename); |
| 107 | if (fd < 0) { |
| 108 | fprintf(stderr, "open %s failed: %m\n", filename); |
| 109 | return NULL; |
| 110 | } |
| 111 | stride = width * 4; |
| 112 | size = stride * height; |
| 113 | if (ftruncate(fd, size) < 0) { |
| 114 | fprintf(stderr, "ftruncate failed: %m\n"); |
| 115 | close(fd); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 120 | unlink(filename); |
| 121 | |
| 122 | if (data == MAP_FAILED) { |
| 123 | fprintf(stderr, "mmap failed: %m\n"); |
| 124 | close(fd); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual); |
| 129 | |
| 130 | close(fd); |
| 131 | |
| 132 | *data_out = data; |
| 133 | |
| 134 | return buffer; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int main(int argc, char *argv[]) |
| 138 | { |
| 139 | struct wl_display *display; |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 140 | struct wl_buffer *buffer; |
| 141 | void *data; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 143 | display = wl_display_connect(NULL); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 144 | if (display == NULL) { |
| 145 | fprintf(stderr, "failed to create display: %m\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 149 | wl_display_add_global_listener(display, handle_global, &screenshooter); |
Kristian Høgsberg | 03fd86b | 2009-02-10 14:15:44 -0500 | [diff] [blame] | 150 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 151 | roundtrip(display); |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 152 | if (screenshooter == NULL) { |
| 153 | fprintf(stderr, "display doesn't support screenshooter\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 157 | buffer = create_shm_buffer(output_width, output_height, &data); |
| 158 | screenshooter_shoot(screenshooter, output, buffer); |
| 159 | roundtrip(display); |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 160 | |
Kristian Høgsberg | 8544903 | 2011-05-02 12:11:07 -0400 | [diff] [blame^] | 161 | /* FIXME: write png */ |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |