blob: 328d106c514db591924daa96fac51b57a405d6a9 [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;
50 struct wl_list link;
51};
Kristian Høgsberg85449032011-05-02 12:11:07 -040052
53static void
54display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040055 struct wl_output *wl_output,
56 int x,
57 int y,
58 int physical_width,
59 int physical_height,
60 int subpixel,
61 const char *make,
62 const char *model)
63{
Scott Moreau80d27b72012-04-04 11:49:21 -060064 struct screenshooter_output *output;
65
66 output = wl_output_get_user_data(wl_output);
67
68 if (wl_output == output->output) {
69 output->offset_x = x;
70 output->offset_y = y;
71 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040072}
73
74static void
75display_handle_mode(void *data,
76 struct wl_output *wl_output,
77 uint32_t flags,
78 int width,
79 int height,
80 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040081{
Scott Moreau80d27b72012-04-04 11:49:21 -060082 struct screenshooter_output *output;
83
84 output = wl_output_get_user_data(wl_output);
85
Kristian Høgsberg1a361562012-04-04 14:52:35 -040086 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
Scott Moreau80d27b72012-04-04 11:49:21 -060087 output->width = width;
88 output->height = height;
89 }
Kristian Høgsberg85449032011-05-02 12:11:07 -040090}
91
92static const struct wl_output_listener output_listener = {
93 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040094 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040095};
96
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040097static void
Scott Moreau062be7e2012-04-20 13:37:33 -060098screenshot_done(void *data, struct screenshooter *screenshooter)
99{
100 buffer_copy_done = 1;
101}
102
103static const struct screenshooter_listener screenshooter_listener = {
104 screenshot_done
105};
106
107static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400108handle_global(struct wl_display *display, uint32_t id,
109 const char *interface, uint32_t version, void *data)
110{
Scott Moreau80d27b72012-04-04 11:49:21 -0600111 static struct screenshooter_output *output;
112
Kristian Høgsberg85449032011-05-02 12:11:07 -0400113 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600114 output = malloc(sizeof *output);
115 output->output = wl_display_bind(display, id, &wl_output_interface);
116 wl_list_insert(&output_list, &output->link);
117 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400118 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400119 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400120 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400121 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400122 }
123}
124
Kristian Høgsberg85449032011-05-02 12:11:07 -0400125static struct wl_buffer *
126create_shm_buffer(int width, int height, void **data_out)
127{
128 char filename[] = "/tmp/wayland-shm-XXXXXX";
Kristian Høgsberg16626282012-04-03 11:21:27 -0400129 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400130 struct wl_buffer *buffer;
131 int fd, size, stride;
132 void *data;
133
134 fd = mkstemp(filename);
135 if (fd < 0) {
136 fprintf(stderr, "open %s failed: %m\n", filename);
137 return NULL;
138 }
139 stride = width * 4;
140 size = stride * height;
141 if (ftruncate(fd, size) < 0) {
142 fprintf(stderr, "ftruncate failed: %m\n");
143 close(fd);
144 return NULL;
145 }
146
147 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
148 unlink(filename);
149
150 if (data == MAP_FAILED) {
151 fprintf(stderr, "mmap failed: %m\n");
152 close(fd);
153 return NULL;
154 }
155
Kristian Høgsberg16626282012-04-03 11:21:27 -0400156 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400157 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400158 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
159 WL_SHM_FORMAT_XRGB8888);
160 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400161
162 *data_out = data;
163
164 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500165}
166
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300167static void
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700168write_png(int width, int height, void *data)
169{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400170 cairo_surface_t *surface;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300171
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400172 surface = cairo_image_surface_create_for_data(data,
173 CAIRO_FORMAT_ARGB32,
174 width, height, width * 4);
175 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
176 cairo_surface_destroy(surface);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300177}
178
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500179int main(int argc, char *argv[])
180{
181 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400182 struct wl_buffer *buffer;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300183 void *data = NULL;
Scott Moreau80d27b72012-04-04 11:49:21 -0600184 struct screenshooter_output *output, *next;
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400185 int width = 0, height = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500186
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500187 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500188 if (display == NULL) {
189 fprintf(stderr, "failed to create display: %m\n");
190 return -1;
191 }
192
Scott Moreau80d27b72012-04-04 11:49:21 -0600193 wl_list_init(&output_list);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400194 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500195 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400196 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400197 if (screenshooter == NULL) {
198 fprintf(stderr, "display doesn't support screenshooter\n");
199 return -1;
200 }
201
Scott Moreau062be7e2012-04-20 13:37:33 -0600202 screenshooter_add_listener(screenshooter, &screenshooter_listener, screenshooter);
203
204
Scott Moreau80d27b72012-04-04 11:49:21 -0600205 wl_list_for_each(output, &output_list, link) {
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400206 width = MAX(width, output->offset_x + output->width);
207 height = MAX(height, output->offset_y + output->height);
Scott Moreau80d27b72012-04-04 11:49:21 -0600208 }
209
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400210 buffer = create_shm_buffer(width, height, &data);
Scott Moreau80d27b72012-04-04 11:49:21 -0600211
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400212 wl_list_for_each_safe(output, next, &output_list, link) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600213 screenshooter_shoot(screenshooter, output->output, buffer);
Scott Moreau062be7e2012-04-20 13:37:33 -0600214 buffer_copy_done = 0;
215 while (!buffer_copy_done)
216 wl_display_roundtrip(display);
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400217 free(output);
Scott Moreau80d27b72012-04-04 11:49:21 -0600218 }
219
Kristian Høgsberg1a361562012-04-04 14:52:35 -0400220 write_png(width, height, data);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500221
222 return 0;
223}