2 * Copyright © 2008 Kristian Høgsberg
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.
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
31 #include <gdk-pixbuf/gdk-pixbuf.h>
33 #include "wayland-client.h"
34 #include "screenshooter-client-protocol.h"
36 /* The screenshooter is a good example of a custom object exposed by
37 * the compositor and serves as a test bed for implementing client
38 * side marshalling outside libwayland.so */
40 static struct wl_output *output;
41 static struct wl_shm *shm;
42 static struct screenshooter *screenshooter;
43 static int output_width, output_height;
46 display_handle_geometry(void *data,
47 struct wl_output *wl_output,
59 display_handle_mode(void *data,
60 struct wl_output *wl_output,
67 output_height = height;
70 static const struct wl_output_listener output_listener = {
71 display_handle_geometry,
76 handle_global(struct wl_display *display, uint32_t id,
77 const char *interface, uint32_t version, void *data)
79 if (strcmp(interface, "wl_output") == 0) {
80 output = wl_display_bind(display, id, &wl_output_interface);
81 wl_output_add_listener(output, &output_listener, NULL);
82 } else if (strcmp(interface, "wl_shm") == 0) {
83 shm = wl_display_bind(display, id, &wl_shm_interface);
84 } else if (strcmp(interface, "screenshooter") == 0) {
85 screenshooter = wl_display_bind(display, id, &screenshooter_interface);
89 static struct wl_buffer *
90 create_shm_buffer(int width, int height, void **data_out)
92 char filename[] = "/tmp/wayland-shm-XXXXXX";
93 struct wl_buffer *buffer;
97 fd = mkstemp(filename);
99 fprintf(stderr, "open %s failed: %m\n", filename);
103 size = stride * height;
104 if (ftruncate(fd, size) < 0) {
105 fprintf(stderr, "ftruncate failed: %m\n");
110 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
113 if (data == MAP_FAILED) {
114 fprintf(stderr, "mmap failed: %m\n");
119 buffer = wl_shm_create_buffer(shm, fd, width, height, stride,
120 WL_SHM_FORMAT_XRGB32);
130 write_png(int width, int height, void *data)
132 GdkPixbuf *pixbuf, *normal;
133 GError *error = NULL;
136 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
137 8, width, height, width * 4, NULL,
140 normal = gdk_pixbuf_flip(pixbuf, FALSE);
141 gdk_pixbuf_save(normal, "wayland-screenshot.png", "png", &error, NULL);
142 g_object_unref(normal);
143 g_object_unref(pixbuf);
146 int main(int argc, char *argv[])
148 struct wl_display *display;
149 struct wl_buffer *buffer;
152 display = wl_display_connect(NULL);
153 if (display == NULL) {
154 fprintf(stderr, "failed to create display: %m\n");
158 wl_display_add_global_listener(display, handle_global, &screenshooter);
159 wl_display_iterate(display, WL_DISPLAY_READABLE);
160 wl_display_roundtrip(display);
161 if (screenshooter == NULL) {
162 fprintf(stderr, "display doesn't support screenshooter\n");
166 buffer = create_shm_buffer(output_width, output_height, &data);
167 screenshooter_shoot(screenshooter, output, buffer);
168 wl_display_roundtrip(display);
170 write_png(output_width, output_height, data);