blob: b6ca48e6ba0fcfb9e2aa955eab822b050efd0ddd [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,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040048 struct wl_output *wl_output,
49 int x,
50 int y,
51 int physical_width,
52 int physical_height,
53 int subpixel,
54 const char *make,
55 const char *model)
56{
57}
58
59static void
60display_handle_mode(void *data,
61 struct wl_output *wl_output,
62 uint32_t flags,
63 int width,
64 int height,
65 int refresh)
Kristian Høgsberg85449032011-05-02 12:11:07 -040066{
67 output_width = width;
68 output_height = height;
69}
70
71static const struct wl_output_listener output_listener = {
72 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040073 display_handle_mode
Kristian Høgsberg85449032011-05-02 12:11:07 -040074};
75
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -040076static void
77handle_global(struct wl_display *display, uint32_t id,
78 const char *interface, uint32_t version, void *data)
79{
Kristian Høgsberg85449032011-05-02 12:11:07 -040080 static int visual_count;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050081
Kristian Høgsberg85449032011-05-02 12:11:07 -040082 if (strcmp(interface, "wl_output") == 0) {
83 output = wl_output_create(display, id, 1);
84 wl_output_add_listener(output, &output_listener, NULL);
85 } else if (strcmp(interface, "wl_shm") == 0) {
86 shm = wl_shm_create(display, id, 1);
87 } else if (strcmp(interface, "wl_visual") == 0) {
88 if (visual_count++ == 1)
89 visual = wl_visual_create(display, id, 1);
90 } else if (strcmp(interface, "screenshooter") == 0) {
91 screenshooter = screenshooter_create(display, id, 1);
92 }
93}
94
95static void
96sync_callback(void *data)
97{
98 int *done = data;
99
100 *done = 1;
101}
102
103static void
104roundtrip(struct wl_display *display)
105{
106 int done;
107
108 done = 0;
109 wl_display_sync_callback(display, sync_callback, &done);
110 wl_display_iterate(display, WL_DISPLAY_WRITABLE);
111 while (!done)
112 wl_display_iterate(display, WL_DISPLAY_READABLE);
113}
114
115static struct wl_buffer *
116create_shm_buffer(int width, int height, void **data_out)
117{
118 char filename[] = "/tmp/wayland-shm-XXXXXX";
119 struct wl_buffer *buffer;
120 int fd, size, stride;
121 void *data;
122
123 fd = mkstemp(filename);
124 if (fd < 0) {
125 fprintf(stderr, "open %s failed: %m\n", filename);
126 return NULL;
127 }
128 stride = width * 4;
129 size = stride * height;
130 if (ftruncate(fd, size) < 0) {
131 fprintf(stderr, "ftruncate failed: %m\n");
132 close(fd);
133 return NULL;
134 }
135
136 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
137 unlink(filename);
138
139 if (data == MAP_FAILED) {
140 fprintf(stderr, "mmap failed: %m\n");
141 close(fd);
142 return NULL;
143 }
144
145 buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual);
146
147 close(fd);
148
149 *data_out = data;
150
151 return buffer;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152}
153
154int main(int argc, char *argv[])
155{
156 struct wl_display *display;
Kristian Høgsberg85449032011-05-02 12:11:07 -0400157 struct wl_buffer *buffer;
158 void *data;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500159
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500160 display = wl_display_connect(NULL);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500161 if (display == NULL) {
162 fprintf(stderr, "failed to create display: %m\n");
163 return -1;
164 }
165
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400166 wl_display_add_global_listener(display, handle_global, &screenshooter);
Kristian Høgsberg03fd86b2009-02-10 14:15:44 -0500167 wl_display_iterate(display, WL_DISPLAY_READABLE);
Kristian Høgsberg85449032011-05-02 12:11:07 -0400168 roundtrip(display);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400169 if (screenshooter == NULL) {
170 fprintf(stderr, "display doesn't support screenshooter\n");
171 return -1;
172 }
173
Kristian Høgsberg85449032011-05-02 12:11:07 -0400174 buffer = create_shm_buffer(output_width, output_height, &data);
175 screenshooter_shoot(screenshooter, output, buffer);
176 roundtrip(display);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500177
Kristian Høgsberg85449032011-05-02 12:11:07 -0400178 /* FIXME: write png */
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500179
180 return 0;
181}