blob: a23ef13b6aa0e23c32a9d10c812a91881b876f74 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
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øgsberg1e4b86a2008-11-23 23:41:08 -050023#include <stdint.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <fcntl.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040028#include <unistd.h>
29#include <sys/mman.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040030#include <cairo.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050031
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-client.h>
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040033#include "screenshooter-client-protocol.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050034
Scott Moreau80d27b72012-04-04 11:49:21 -060035#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
36
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050037/* 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øgsberg85449032011-05-02 12:11:07 -040041static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040042static struct screenshooter *screenshooter;
Scott Moreau80d27b72012-04-04 11:49:21 -060043static struct wl_list output_list;
Scott Moreau062be7e2012-04-20 13:37:33 -060044int buffer_copy_done;
Scott Moreau80d27b72012-04-04 11:49:21 -060045
46struct screenshooter_output {
47 struct wl_output *output;
48 struct wl_buffer *buffer;
49 int width, height, offset_x, offset_y;
Scott Moreau72c23722012-04-20 13:37:34 -060050 void *data;
Scott Moreau80d27b72012-04-04 11:49:21 -060051 struct wl_list link;
52};
Kristian Høgsberg85449032011-05-02 12:11:07 -040053
54static void
55display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040056 struct wl_output *wl_output,
57 int x,
58 int y,
59 int physical_width,
60 int physical_height,
61 int subpixel,
62 const char *make,
63 const char *model)
64{
Scott Moreau80d27b72012-04-04 11:49:21 -060065 struct screenshooter_output *output;
66
67 output = wl_output_get_user_data(wl_output);
68
69 if (wl_output == output->output) {
70 output->offset_x = x;
71 output->offset_y = y;
72 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040073}
74
75static void
76display_handle_mode(void *data,
77 struct wl_output *wl_output,
78 uint32_t flags,
79 int width,
80 int height,
81 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040082{
Scott Moreau80d27b72012-04-04 11:49:21 -060083 struct screenshooter_output *output;
84
85 output = wl_output_get_user_data(wl_output);
86
Kristian Høgsberg1a361562012-04-04 14:52:35 -040087 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060088 output->width = width;
89 output->height = height;
90 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040091}
92
93static const struct wl_output_listener output_listener = {
94 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040095 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040096};
97
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040098static void
Scott Moreau062be7e2012-04-20 13:37:33 -060099screenshot_done(void *data, struct screenshooter *screenshooter)
100{
101 buffer_copy_done = 1;
102}
103
104static const struct screenshooter_listener screenshooter_listener = {
105 screenshot_done
106};
107
108static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400109handle_global(struct wl_display *display, uint32_t id,
110 const char *interface, uint32_t version, void *data)
111{
Scott Moreau80d27b72012-04-04 11:49:21 -0600112 static struct screenshooter_output *output;
113
Kristian Høgsberg85449032011-05-02 12:11:07 -0400114 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600115 output = malloc(sizeof *output);
116 output->output = wl_display_bind(display, id, &wl_output_interface);
117 wl_list_insert(&output_list, &output->link);
118 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400119 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400120 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400121 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400122 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400123 }
124}
125
Kristian Høgsberg85449032011-05-02 12:11:07 -0400126static struct wl_buffer *
127create_shm_buffer(int width, int height, void **data_out)
128{
129 char filename[] = "/tmp/wayland-shm-XXXXXX";
Kristian Høgsberg16626282012-04-03 11:21:27 -0400130 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400131 struct wl_buffer *buffer;
132 int fd, size, stride;
133 void *data;
134
135 fd = mkstemp(filename);
136 if (fd < 0) {
137 fprintf(stderr, "open %s failed: %m\n", filename);
138 return NULL;
139 }
140 stride = width * 4;
141 size = stride * height;
142 if (ftruncate(fd, size) < 0) {
143 fprintf(stderr, "ftruncate failed: %m\n");
144 close(fd);
145 return NULL;
146 }
147
148 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
149 unlink(filename);
150
151 if (data == MAP_FAILED) {
152 fprintf(stderr, "mmap failed: %m\n");
153 close(fd);
154 return NULL;
155 }
156
Kristian Høgsberg16626282012-04-03 11:21:27 -0400157 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400158 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400159 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
160 WL_SHM_FORMAT_XRGB8888);
161 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400162
163 *data_out = data;
164
165 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500166}
167
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300168static void
Scott Moreau72c23722012-04-20 13:37:34 -0600169write_png(int width, int height)
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700170{
Scott Moreau72c23722012-04-20 13:37:34 -0600171 int output_stride, buffer_stride, i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400172 cairo_surface_t *surface;
Scott Moreau72c23722012-04-20 13:37:34 -0600173 void *data, *d, *s;
174 struct screenshooter_output *output, *next;
175
176 buffer_stride = width * 4;
177
178 data = malloc(buffer_stride * height);
179 if (!data)
180 return;
181
182 wl_list_for_each_safe(output, next, &output_list, link) {
183 output_stride = output->width * 4;
184 s = output->data;
185 d = data + output->offset_y * buffer_stride + output->offset_x * 4;
186
187 for (i = 0; i < output->height; i++) {
188 memcpy(d, s, output_stride);
189 d += buffer_stride;
190 s += output_stride;
191 }
192
193 free(output);
194 }
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300195
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400196 surface = cairo_image_surface_create_for_data(data,
197 CAIRO_FORMAT_ARGB32,
Scott Moreau72c23722012-04-20 13:37:34 -0600198 width, height, buffer_stride);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400199 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
200 cairo_surface_destroy(surface);
Scott Moreau72c23722012-04-20 13:37:34 -0600201 free(data);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300202}
203
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500204int main(int argc, char *argv[])
205{
206 struct wl_display *display;
Scott Moreau72c23722012-04-20 13:37:34 -0600207 struct screenshooter_output *output;
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400208 int width = 0, height = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500209
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500210 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500211 if (display == NULL) {
212 fprintf(stderr, "failed to create display: %m\n");
213 return -1;
214 }
215
Scott Moreau80d27b72012-04-04 11:49:21 -0600216 wl_list_init(&output_list);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400217 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500218 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400219 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400220 if (screenshooter == NULL) {
221 fprintf(stderr, "display doesn't support screenshooter\n");
222 return -1;
223 }
224
Scott Moreau062be7e2012-04-20 13:37:33 -0600225 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
226
227
Scott Moreau80d27b72012-04-04 11:49:21 -0600228 wl_list_for_each(output, &output_list, link) {
Scott Moreau72c23722012-04-20 13:37:34 -0600229 output->buffer = create_shm_buffer(output->width, output->height, &output->data);
230 screenshooter_shoot(screenshooter, output->output, output->buffer);
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400231 width = MAX(width, output->offset_x + output->width);
232 height = MAX(height, output->offset_y + output->height);
Scott Moreau062be7e2012-04-20 13:37:33 -0600233 buffer_copy_done = 0;
234 while (!buffer_copy_done)
235 wl_display_roundtrip(display);
Scott Moreau80d27b72012-04-04 11:49:21 -0600236 }
237
Scott Moreau72c23722012-04-20 13:37:34 -0600238 write_png(width, height);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500239
240 return 0;
241}