2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
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.
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
35 #include <wayland-client.h>
36 #include "../shared/os-compatibility.h"
39 struct wl_display *display;
40 struct wl_registry *registry;
41 struct wl_compositor *compositor;
42 struct wl_shell *shell;
48 struct wl_buffer *buffer;
54 struct display *display;
56 struct wl_surface *surface;
57 struct wl_shell_surface *shell_surface;
58 struct buffer buffers[2];
59 struct buffer *prev_buffer;
60 struct wl_callback *callback;
64 buffer_release(void *data, struct wl_buffer *buffer)
66 struct buffer *mybuf = data;
71 static const struct wl_buffer_listener buffer_listener = {
76 create_shm_buffer(struct display *display, struct buffer *buffer,
77 int width, int height, uint32_t format)
79 struct wl_shm_pool *pool;
84 size = stride * height;
86 fd = os_create_anonymous_file(size);
88 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
93 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
94 if (data == MAP_FAILED) {
95 fprintf(stderr, "mmap failed: %m\n");
100 pool = wl_shm_create_pool(display->shm, fd, size);
101 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
104 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
105 wl_shm_pool_destroy(pool);
108 buffer->shm_data = data;
114 handle_ping(void *data, struct wl_shell_surface *shell_surface,
117 wl_shell_surface_pong(shell_surface, serial);
121 handle_configure(void *data, struct wl_shell_surface *shell_surface,
122 uint32_t edges, int32_t width, int32_t height)
127 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
131 static const struct wl_shell_surface_listener shell_surface_listener = {
137 static struct window *
138 create_window(struct display *display, int width, int height)
140 struct window *window;
142 window = calloc(1, sizeof *window);
146 window->callback = NULL;
147 window->display = display;
148 window->width = width;
149 window->height = height;
150 window->surface = wl_compositor_create_surface(display->compositor);
151 window->shell_surface = wl_shell_get_shell_surface(display->shell,
154 if (window->shell_surface)
155 wl_shell_surface_add_listener(window->shell_surface,
156 &shell_surface_listener, window);
158 wl_shell_surface_set_title(window->shell_surface, "simple-shm");
160 wl_shell_surface_set_toplevel(window->shell_surface);
166 destroy_window(struct window *window)
168 if (window->callback)
169 wl_callback_destroy(window->callback);
171 if (window->buffers[0].buffer)
172 wl_buffer_destroy(window->buffers[0].buffer);
173 if (window->buffers[1].buffer)
174 wl_buffer_destroy(window->buffers[1].buffer);
176 wl_shell_surface_destroy(window->shell_surface);
177 wl_surface_destroy(window->surface);
181 static struct buffer *
182 window_next_buffer(struct window *window)
184 struct buffer *buffer;
187 if (!window->buffers[0].busy)
188 buffer = &window->buffers[0];
189 else if (!window->buffers[1].busy)
190 buffer = &window->buffers[1];
194 if (!buffer->buffer) {
195 ret = create_shm_buffer(window->display, buffer,
196 window->width, window->height,
197 WL_SHM_FORMAT_XRGB8888);
202 /* paint the padding */
203 memset(buffer->shm_data, 0xff,
204 window->width * window->height * 4);
211 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
213 const int halfh = padding + (height - padding * 2) / 2;
214 const int halfw = padding + (width - padding * 2) / 2;
216 uint32_t *pixel = image;
219 /* squared radii thresholds */
220 or = (halfw < halfh ? halfw : halfh) - 8;
225 pixel += padding * width;
226 for (y = padding; y < height - padding; y++) {
228 int y2 = (y - halfh) * (y - halfh);
231 for (x = padding; x < width - padding; x++) {
234 /* squared distance from center */
235 int r2 = (x - halfw) * (x - halfw) + y2;
238 v = (r2 / 32 + time / 64) * 0x0080401;
240 v = (y + time / 32) * 0x0080401;
242 v = (x + time / 16) * 0x0080401;
245 /* cross if compositor uses X from XRGB as alpha */
246 if (abs(x - y) > 6 && abs(x + y - height) > 6)
256 static const struct wl_callback_listener frame_listener;
259 redraw(void *data, struct wl_callback *callback, uint32_t time)
261 struct window *window = data;
262 struct buffer *buffer;
264 buffer = window_next_buffer(window);
267 !callback ? "Failed to create the first buffer.\n" :
268 "Both buffers busy at redraw(). Server bug?\n");
272 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
274 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
275 wl_surface_damage(window->surface,
276 20, 20, window->width - 40, window->height - 40);
279 wl_callback_destroy(callback);
281 window->callback = wl_surface_frame(window->surface);
282 wl_callback_add_listener(window->callback, &frame_listener, window);
283 wl_surface_commit(window->surface);
287 static const struct wl_callback_listener frame_listener = {
292 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
294 struct display *d = data;
296 d->formats |= (1 << format);
299 struct wl_shm_listener shm_listener = {
304 registry_handle_global(void *data, struct wl_registry *registry,
305 uint32_t id, const char *interface, uint32_t version)
307 struct display *d = data;
309 if (strcmp(interface, "wl_compositor") == 0) {
311 wl_registry_bind(registry,
312 id, &wl_compositor_interface, 1);
313 } else if (strcmp(interface, "wl_shell") == 0) {
314 d->shell = wl_registry_bind(registry,
315 id, &wl_shell_interface, 1);
316 } else if (strcmp(interface, "wl_shm") == 0) {
317 d->shm = wl_registry_bind(registry,
318 id, &wl_shm_interface, 1);
319 wl_shm_add_listener(d->shm, &shm_listener, d);
324 registry_handle_global_remove(void *data, struct wl_registry *registry,
329 static const struct wl_registry_listener registry_listener = {
330 registry_handle_global,
331 registry_handle_global_remove
334 static struct display *
337 struct display *display;
339 display = malloc(sizeof *display);
340 if (display == NULL) {
341 fprintf(stderr, "out of memory\n");
344 display->display = wl_display_connect(NULL);
345 assert(display->display);
347 display->formats = 0;
348 display->registry = wl_display_get_registry(display->display);
349 wl_registry_add_listener(display->registry,
350 ®istry_listener, display);
351 wl_display_roundtrip(display->display);
352 if (display->shm == NULL) {
353 fprintf(stderr, "No wl_shm global\n");
357 wl_display_roundtrip(display->display);
359 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
360 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
364 wl_display_get_fd(display->display);
370 destroy_display(struct display *display)
373 wl_shm_destroy(display->shm);
376 wl_shell_destroy(display->shell);
378 if (display->compositor)
379 wl_compositor_destroy(display->compositor);
381 wl_registry_destroy(display->registry);
382 wl_display_flush(display->display);
383 wl_display_disconnect(display->display);
387 static int running = 1;
390 signal_int(int signum)
396 main(int argc, char **argv)
398 struct sigaction sigint;
399 struct display *display;
400 struct window *window;
403 display = create_display();
404 window = create_window(display, 250, 250);
408 sigint.sa_handler = signal_int;
409 sigemptyset(&sigint.sa_mask);
410 sigint.sa_flags = SA_RESETHAND;
411 sigaction(SIGINT, &sigint, NULL);
413 /* Initialise damage to full surface, so the padding gets painted */
414 wl_surface_damage(window->surface, 0, 0,
415 window->width, window->height);
417 redraw(window, NULL, 0);
419 while (running && ret != -1)
420 ret = wl_display_dispatch(display->display);
422 fprintf(stderr, "simple-shm exiting\n");
423 destroy_window(window);
424 destroy_display(display);