blob: 56742fac62b30145622c788f129232d226f6a2f2 [file] [log] [blame]
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -04001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdbool.h>
28#include <math.h>
29#include <assert.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <sys/mman.h>
33
34#include <wayland-client.h>
35#include <wayland-egl.h>
36
37struct display {
38 struct wl_display *display;
39 struct wl_visual *xrgb_visual;
40 struct wl_compositor *compositor;
41 struct wl_shell *shell;
42 struct wl_shm *shm;
43 uint32_t mask;
44};
45
46struct window {
47 struct display *display;
48 int width, height;
49 struct wl_surface *surface;
50 struct wl_buffer *buffer;
51 void *data;
52};
53
54static struct wl_buffer *
55create_shm_buffer(struct display *display,
56 int width, int height, struct wl_visual *visual,
57 void **data_out)
58{
59 char filename[] = "/tmp/wayland-shm-XXXXXX";
60 struct wl_buffer *buffer;
61 int fd, size, stride;
62 void *data;
63
64 fd = mkstemp(filename);
65 if (fd < 0) {
66 fprintf(stderr, "open %s failed: %m\n", filename);
67 return NULL;
68 }
69 stride = width * 4;
70 size = stride * height;
71 if (ftruncate(fd, size) < 0) {
72 fprintf(stderr, "ftruncate failed: %m\n");
73 close(fd);
74 return NULL;
75 }
76
77 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
78 unlink(filename);
79
80 if (data == MAP_FAILED) {
81 fprintf(stderr, "mmap failed: %m\n");
82 close(fd);
83 return NULL;
84 }
85
86 buffer = wl_shm_create_buffer(display->shm, fd,
87 width, height, stride, visual);
88
89 close(fd);
90
91 *data_out = data;
92
93 return buffer;
94}
95
96static struct window *
97create_window(struct display *display, int width, int height)
98{
99 struct window *window;
100 struct wl_visual *visual;
101
102 window = malloc(sizeof *window);
103 window->display = display;
104 window->width = width;
105 window->height = height;
106 window->surface = wl_compositor_create_surface(display->compositor);
107 visual = display->xrgb_visual;
108 window->buffer = create_shm_buffer(display,
109 width, height,
110 visual, &window->data);
111
112 wl_shell_set_toplevel(display->shell, window->surface);
113
114 return window;
115}
116
117static void
118redraw(struct wl_surface *surface, void *data, uint32_t time)
119{
120 struct window *window = data;
121 uint32_t *p;
122 int i, end, offset;
123
124 p = window->data;
125 end = window->width * window->height;
126 offset = time >> 4;
127 for (i = 0; i < end; i++)
128 p[i] = (i + offset) * 0x0080401;
129 wl_buffer_damage(window->buffer, 0, 0, window->width, window->height);
130
131 wl_surface_attach(window->surface, window->buffer, 0, 0);
132 wl_surface_damage(window->surface,
133 0, 0, window->width, window->height);
134
135 wl_display_frame_callback(window->display->display,
136 window->surface,
137 redraw, window);
138}
139
140static void
141compositor_handle_visual(void *data,
142 struct wl_compositor *compositor,
143 uint32_t id, uint32_t token)
144{
145 struct display *d = data;
146
147 switch (token) {
148 case WL_COMPOSITOR_VISUAL_XRGB32:
149 d->xrgb_visual = wl_visual_create(d->display, id, 1);
150 break;
151 }
152}
153
154static const struct wl_compositor_listener compositor_listener = {
155 compositor_handle_visual,
156};
157
158static void
159display_handle_global(struct wl_display *display, uint32_t id,
160 const char *interface, uint32_t version, void *data)
161{
162 struct display *d = data;
163
164 if (strcmp(interface, "wl_compositor") == 0) {
165 d->compositor = wl_compositor_create(display, id, 1);
166 wl_compositor_add_listener(d->compositor,
167 &compositor_listener, d);
168 } else if (strcmp(interface, "wl_shell") == 0) {
169 d->shell = wl_shell_create(display, id, 1);
170 } else if (strcmp(interface, "wl_shm") == 0) {
171 d->shm = wl_shm_create(display, id, 1);
172 }
173}
174
175static int
176event_mask_update(uint32_t mask, void *data)
177{
178 struct display *d = data;
179
180 d->mask = mask;
181
182 return 0;
183}
184
185static void
186sync_callback(void *data)
187{
188 int *done = data;
189
190 *done = 1;
191}
192
193static struct display *
194create_display(void)
195{
196 struct display *display;
197 int done;
198
199 display = malloc(sizeof *display);
200 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300201 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400202
203 wl_display_add_global_listener(display->display,
204 display_handle_global, display);
205 wl_display_iterate(display->display, WL_DISPLAY_READABLE);
206
207 wl_display_get_fd(display->display, event_mask_update, display);
208
209 wl_display_sync_callback(display->display, sync_callback, &done);
210 while (!display->xrgb_visual)
211 wl_display_iterate(display->display, display->mask);
212
213 return display;
214}
215
216int
217main(int argc, char **argv)
218{
219 struct display *display;
220 struct window *window;
221
222 display = create_display();
223 window = create_window(display, 250, 250);
224
225 redraw(window->surface, window, 0);
226
227 while (true)
228 wl_display_iterate(display->display, display->mask);
229
230 return 0;
231}