blob: 8b1edf6e204a27534849170e7891650d5bc0bae8 [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
35/* The screenshooter is a good example of a custom object exposed by
36 * the compositor and serves as a test bed for implementing client
37 * side marshalling outside libwayland.so */
38
Kristian Høgsberg85449032011-05-02 12:11:07 -040039static struct wl_output *output;
40static struct wl_shm *shm;
Kristian Høgsberg85449032011-05-02 12:11:07 -040041static struct screenshooter *screenshooter;
42static int output_width, output_height;
43
44static void
45display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040046 struct wl_output *wl_output,
47 int x,
48 int y,
49 int physical_width,
50 int physical_height,
51 int subpixel,
52 const char *make,
53 const char *model)
54{
55}
56
57static void
58display_handle_mode(void *data,
59 struct wl_output *wl_output,
60 uint32_t flags,
61 int width,
62 int height,
63 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040064{
65 output_width = width;
66 output_height = height;
67}
68
69static const struct wl_output_listener output_listener = {
70 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040071 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040072};
73
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040074static void
75handle_global(struct wl_display *display, uint32_t id,
76 const char *interface, uint32_t version, void *data)
77{
Kristian Høgsberg85449032011-05-02 12:11:07 -040078 if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040079 output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040080 wl_output_add_listener(output, &output_listener, NULL);
81 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040082 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040083 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040084 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040085 }
86}
87
Kristian Høgsberg85449032011-05-02 12:11:07 -040088static struct wl_buffer *
89create_shm_buffer(int width, int height, void **data_out)
90{
91 char filename[] = "/tmp/wayland-shm-XXXXXX";
92 struct wl_buffer *buffer;
93 int fd, size, stride;
94 void *data;
95
96 fd = mkstemp(filename);
97 if (fd < 0) {
98 fprintf(stderr, "open %s failed: %m\n", filename);
99 return NULL;
100 }
101 stride = width * 4;
102 size = stride * height;
103 if (ftruncate(fd, size) < 0) {
104 fprintf(stderr, "ftruncate failed: %m\n");
105 close(fd);
106 return NULL;
107 }
108
109 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
110 unlink(filename);
111
112 if (data == MAP_FAILED) {
113 fprintf(stderr, "mmap failed: %m\n");
114 close(fd);
115 return NULL;
116 }
117
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400118 buffer = wl_shm_create_buffer(shm, fd, width, height, stride,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500119 WL_SHM_FORMAT_XRGB8888);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400120
121 close(fd);
122
123 *data_out = data;
124
125 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500126}
127
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300128static void
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700129write_png(int width, int height, void *data)
130{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400131 cairo_surface_t *surface;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300132
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400133 surface = cairo_image_surface_create_for_data(data,
134 CAIRO_FORMAT_ARGB32,
135 width, height, width * 4);
136 cairo_surface_write_to_png(surface, "wayland-screenshot.png");
137 cairo_surface_destroy(surface);
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300138}
139
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500140int main(int argc, char *argv[])
141{
142 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400143 struct wl_buffer *buffer;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300144 void *data = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500145
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500146 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500147 if (display == NULL) {
148 fprintf(stderr, "failed to create display: %m\n");
149 return -1;
150 }
151
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400152 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500153 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400154 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400155 if (screenshooter == NULL) {
156 fprintf(stderr, "display doesn't support screenshooter\n");
157 return -1;
158 }
159
Kristian Høgsberg85449032011-05-02 12:11:07 -0400160 buffer = create_shm_buffer(output_width, output_height, &data);
161 screenshooter_shoot(screenshooter, output, buffer);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400162 wl_display_roundtrip(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500163
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700164 write_png(output_width, output_height, data);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500165
166 return 0;
167}