blob: 428befa704ceec645a6f5880ca69fa65604ab2d5 [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øgsberg1e4b86a2008-11-23 23:41:08 -050030#include <glib.h>
Tiago Vignatti4d0d2032011-07-26 11:42:59 +030031#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050032
33#include "wayland-client.h"
34#include "wayland-glib.h"
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040035#include "screenshooter-client-protocol.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050036
37/* 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_output *output;
42static struct wl_shm *shm;
43static struct wl_visual *visual;
44static struct screenshooter *screenshooter;
45static int output_width, output_height;
46
47static void
48display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040049 struct wl_output *wl_output,
50 int x,
51 int y,
52 int physical_width,
53 int physical_height,
54 int subpixel,
55 const char *make,
56 const char *model)
57{
58}
59
60static void
61display_handle_mode(void *data,
62 struct wl_output *wl_output,
63 uint32_t flags,
64 int width,
65 int height,
66 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040067{
68 output_width = width;
69 output_height = height;
70}
71
72static const struct wl_output_listener output_listener = {
73 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040075};
76
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040077static void
78handle_global(struct wl_display *display, uint32_t id,
79 const char *interface, uint32_t version, void *data)
80{
Kristian Høgsberg85449032011-05-02 12:11:07 -040081 static int visual_count;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050082
Kristian Høgsberg85449032011-05-02 12:11:07 -040083 if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040084 output = wl_display_bind(display, id, &wl_output_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040085 wl_output_add_listener(output, &output_listener, NULL);
86 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040087 shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040088 } else if (strcmp(interface, "wl_visual") == 0) {
89 if (visual_count++ == 1)
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040090 visual = wl_display_bind(display, id,
91 &wl_visual_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040092 } else if (strcmp(interface, "screenshooter") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040093 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
Kristian Høgsberg85449032011-05-02 12:11:07 -040094 }
95}
96
Kristian Høgsberg85449032011-05-02 12:11:07 -040097static struct wl_buffer *
98create_shm_buffer(int width, int height, void **data_out)
99{
100 char filename[] = "/tmp/wayland-shm-XXXXXX";
101 struct wl_buffer *buffer;
102 int fd, size, stride;
103 void *data;
104
105 fd = mkstemp(filename);
106 if (fd < 0) {
107 fprintf(stderr, "open %s failed: %m\n", filename);
108 return NULL;
109 }
110 stride = width * 4;
111 size = stride * height;
112 if (ftruncate(fd, size) < 0) {
113 fprintf(stderr, "ftruncate failed: %m\n");
114 close(fd);
115 return NULL;
116 }
117
118 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
119 unlink(filename);
120
121 if (data == MAP_FAILED) {
122 fprintf(stderr, "mmap failed: %m\n");
123 close(fd);
124 return NULL;
125 }
126
127 buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual);
128
129 close(fd);
130
131 *data_out = data;
132
133 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500134}
135
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300136static void
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700137write_png(int width, int height, void *data)
138{
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300139 GdkPixbuf *pixbuf, *normal;
140 GError *error = NULL;
141
142 g_type_init();
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700143 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300144 8, width, height, width * 4, NULL,
145 NULL);
146
147 normal = gdk_pixbuf_flip(pixbuf, FALSE);
148 gdk_pixbuf_save(normal, "wayland-screenshot.png", "png", &error, NULL);
149 g_object_unref(normal);
150 g_object_unref(pixbuf);
151}
152
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500153int main(int argc, char *argv[])
154{
155 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400156 struct wl_buffer *buffer;
Tiago Vignatti4d0d2032011-07-26 11:42:59 +0300157 void *data = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500158
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500159 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500160 if (display == NULL) {
161 fprintf(stderr, "failed to create display: %m\n");
162 return -1;
163 }
164
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400165 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500166 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400167 wl_display_roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400168 if (screenshooter == NULL) {
169 fprintf(stderr, "display doesn't support screenshooter\n");
170 return -1;
171 }
172
Kristian Høgsberg85449032011-05-02 12:11:07 -0400173 buffer = create_shm_buffer(output_width, output_height, &data);
174 screenshooter_shoot(screenshooter, output, buffer);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400175 wl_display_roundtrip(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500176
Kristian Høgsberg8417d432011-07-27 05:58:57 -0700177 write_png(output_width, output_height, data);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500178
179 return 0;
180}