blob: 574f525cff5c1338013326adf6bb03ca9752ebce [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>
31
32#include "wayland-client.h"
33#include "wayland-glib.h"
Kristian Høgsberg3dd66d62010-09-14 16:23:24 -040034#include "screenshooter-client-protocol.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050035
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 */
39
Kristian Høgsberg85449032011-05-02 12:11:07 -040040static struct wl_output *output;
41static struct wl_shm *shm;
42static struct wl_visual *visual;
43static struct screenshooter *screenshooter;
44static int output_width, output_height;
45
46static void
47display_handle_geometry(void *data,
48 struct wl_output *output,
49 int32_t x, int32_t y, int32_t width, int32_t height)
50{
51 output_width = width;
52 output_height = height;
53}
54
55static const struct wl_output_listener output_listener = {
56 display_handle_geometry,
57};
58
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040059static void
60handle_global(struct wl_display *display, uint32_t id,
61 const char *interface, uint32_t version, void *data)
62{
Kristian Høgsberg85449032011-05-02 12:11:07 -040063 static int visual_count;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050064
Kristian Høgsberg85449032011-05-02 12:11:07 -040065 if (strcmp(interface, "wl_output") == 0) {
66 output = wl_output_create(display, id, 1);
67 wl_output_add_listener(output, &output_listener, NULL);
68 } else if (strcmp(interface, "wl_shm") == 0) {
69 shm = wl_shm_create(display, id, 1);
70 } else if (strcmp(interface, "wl_visual") == 0) {
71 if (visual_count++ == 1)
72 visual = wl_visual_create(display, id, 1);
73 } else if (strcmp(interface, "screenshooter") == 0) {
74 screenshooter = screenshooter_create(display, id, 1);
75 }
76}
77
78static void
79sync_callback(void *data)
80{
81 int *done = data;
82
83 *done = 1;
84}
85
86static void
87roundtrip(struct wl_display *display)
88{
89 int done;
90
91 done = 0;
92 wl_display_sync_callback(display, sync_callback, &done);
93 wl_display_iterate(display, WL_DISPLAY_WRITABLE);
94 while (!done)
95 wl_display_iterate(display, WL_DISPLAY_READABLE);
96}
97
98static struct wl_buffer *
99create_shm_buffer(int width, int height, void **data_out)
100{
101 char filename[] = "/tmp/wayland-shm-XXXXXX";
102 struct wl_buffer *buffer;
103 int fd, size, stride;
104 void *data;
105
106 fd = mkstemp(filename);
107 if (fd < 0) {
108 fprintf(stderr, "open %s failed: %m\n", filename);
109 return NULL;
110 }
111 stride = width * 4;
112 size = stride * height;
113 if (ftruncate(fd, size) < 0) {
114 fprintf(stderr, "ftruncate failed: %m\n");
115 close(fd);
116 return NULL;
117 }
118
119 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
120 unlink(filename);
121
122 if (data == MAP_FAILED) {
123 fprintf(stderr, "mmap failed: %m\n");
124 close(fd);
125 return NULL;
126 }
127
128 buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual);
129
130 close(fd);
131
132 *data_out = data;
133
134 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500135}
136
137int main(int argc, char *argv[])
138{
139 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400140 struct wl_buffer *buffer;
141 void *data;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500142
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500143 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500144 if (display == NULL) {
145 fprintf(stderr, "failed to create display: %m\n");
146 return -1;
147 }
148
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400149 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500150 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400151 roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400152 if (screenshooter == NULL) {
153 fprintf(stderr, "display doesn't support screenshooter\n");
154 return -1;
155 }
156
Kristian Høgsberg85449032011-05-02 12:11:07 -0400157 buffer = create_shm_buffer(output_width, output_height, &data);
158 screenshooter_shoot(screenshooter, output, buffer);
159 roundtrip(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500160
Kristian Høgsberg85449032011-05-02 12:11:07 -0400161 /* FIXME: write png */
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500162
163 return 0;
164}