blob: f340d33fcabbf517af25b092a2bb0b97376a1709 [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;
44
45struct 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øgsberg85449032011-05-02 12:11:07 -040051
52static void
53display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040054 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 Moreau80d27b72012-04-04 11:49:21 -060063 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øgsberg8f0ce052011-06-21 11:16:58 -040071}
72
73static void
74display_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øgsberg85449032011-05-02 12:11:07 -040080{
Scott Moreau80d27b72012-04-04 11:49:21 -060081 struct screenshooter_output *output;
82
83 output = wl_output_get_user_data(wl_output);
84
85 if (wl_output == output->output &&
86 (flags & WL_OUTPUT_MODE_CURRENT)) {
87 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
98handle_global(struct wl_display *display, uint32_t id,
99 const char *interface, uint32_t version, void *data)
100{
Scott Moreau80d27b72012-04-04 11:49:21 -0600101 static struct screenshooter_output *output;
102
Kristian Høgsberg85449032011-05-02 12:11:07 -0400103 if (strcmp(interface, "wl_output") == 0) {
Scott Moreau80d27b72012-04-04 11:49:21 -0600104 output = malloc(sizeof *output);
105 output->output = wl_display_bind(display, id, &wl_output_interface);
106 wl_list_insert(&output_list, &output->link);
107 wl_output_add_listener(output->output, &output_listener, output);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400108 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400109 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400110 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400111 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400112 }
113}
114
Kristian Høgsberg85449032011-05-02 12:11:07 -0400115static struct wl_buffer *
116create_shm_buffer(int width, int height, void **data_out)
117{
118 char filename[] = "/tmp/wayland-shm-XXXXXX";
Kristian Høgsberg16626282012-04-03 11:21:27 -0400119 struct wl_shm_pool *pool;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400120 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
Kristian Høgsberg16626282012-04-03 11:21:27 -0400146 pool = wl_shm_create_pool(shm, fd, size);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400147 close(fd);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400148 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
149 WL_SHM_FORMAT_XRGB8888);
150 wl_shm_pool_destroy(pool);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400151
152 *data_out = data;
153
154 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500155}
156
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300157static void
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700158write_png(int width, int height, void *data)
159{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400160 cairo_surface_t *surface;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300161
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400162 surface = cairo_image_surface_create_for_data(data,
163 CAIRO_FORMAT_ARGB32,
164 width, height, width * 4);
165 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
166 cairo_surface_destroy(surface);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300167}
168
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500169int main(int argc, char *argv[])
170{
171 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400172 struct wl_buffer *buffer;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300173 void *data = NULL;
Scott Moreau80d27b72012-04-04 11:49:21 -0600174 struct screenshooter_output *output, *next;
175 int ss_area_width = 0, ss_area_height = 0, num_outputs = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500176
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500177 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500178 if (display == NULL) {
179 fprintf(stderr, "failed to create display: %m\n");
180 return -1;
181 }
182
Scott Moreau80d27b72012-04-04 11:49:21 -0600183 wl_list_init(&output_list);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400184 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500185 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400186 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400187 if (screenshooter == NULL) {
188 fprintf(stderr, "display doesn't support screenshooter\n");
189 return -1;
190 }
191
Scott Moreau80d27b72012-04-04 11:49:21 -0600192 wl_list_for_each(output, &output_list, link) {
193
194 if (!num_outputs) {
195 ss_area_width = output->width + output->offset_x;
196 ss_area_height = output->height + output->offset_y;
197 }
198 else {
199 ss_area_width = MAX(ss_area_width, output->offset_x + output->width);
200 ss_area_height = MAX(ss_area_height, output->offset_y + output->height);
201 }
202 num_outputs++;
203 }
204
205 buffer = create_shm_buffer(ss_area_width, ss_area_height, &data);
206
207 wl_list_for_each(output, &output_list, link) {
208 screenshooter_shoot(screenshooter, output->output, buffer);
209 }
210
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400211 wl_display_roundtrip(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500212
Scott Moreau80d27b72012-04-04 11:49:21 -0600213 write_png(ss_area_width, ss_area_height, data);
214
215 wl_list_for_each_safe(output, next, &output_list, link) {
216 free(output);
217 }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500218
219 return 0;
220}