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"
37 #include "xdg-shell-client-protocol.h"
40 struct wl_display *display;
41 struct wl_registry *registry;
42 struct wl_compositor *compositor;
43 struct xdg_shell *shell;
49 struct wl_buffer *buffer;
55 struct display *display;
57 struct wl_surface *surface;
58 struct xdg_surface *xdg_surface;
59 struct buffer buffers[2];
60 struct buffer *prev_buffer;
61 struct wl_callback *callback;
65 buffer_release(void *data, struct wl_buffer *buffer)
67 struct buffer *mybuf = data;
72 static const struct wl_buffer_listener buffer_listener = {
77 create_shm_buffer(struct display *display, struct buffer *buffer,
78 int width, int height, uint32_t format)
80 struct wl_shm_pool *pool;
85 size = stride * height;
87 fd = os_create_anonymous_file(size);
89 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
94 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
95 if (data == MAP_FAILED) {
96 fprintf(stderr, "mmap failed: %m\n");
101 pool = wl_shm_create_pool(display->shm, fd, size);
102 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
105 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
106 wl_shm_pool_destroy(pool);
109 buffer->shm_data = data;
115 handle_configure(void *data, struct xdg_surface *surface,
116 int32_t width, int32_t height)
121 handle_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
126 handle_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
131 handle_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
136 handle_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
141 handle_focused_set(void *data, struct xdg_surface *xdg_surface)
146 handle_focused_unset(void *data, struct xdg_surface *xdg_surface)
150 static const struct xdg_surface_listener xdg_surface_listener = {
152 handle_request_set_maximized,
153 handle_request_unset_maximized,
154 handle_request_set_fullscreen,
155 handle_request_unset_fullscreen,
157 handle_focused_unset,
160 static struct window *
161 create_window(struct display *display, int width, int height)
163 struct window *window;
165 window = calloc(1, sizeof *window);
169 window->callback = NULL;
170 window->display = display;
171 window->width = width;
172 window->height = height;
173 window->surface = wl_compositor_create_surface(display->compositor);
174 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
177 if (window->xdg_surface)
178 xdg_surface_add_listener(window->xdg_surface,
179 &xdg_surface_listener, window);
181 xdg_surface_set_title(window->xdg_surface, "simple-shm");
187 destroy_window(struct window *window)
189 if (window->callback)
190 wl_callback_destroy(window->callback);
192 if (window->buffers[0].buffer)
193 wl_buffer_destroy(window->buffers[0].buffer);
194 if (window->buffers[1].buffer)
195 wl_buffer_destroy(window->buffers[1].buffer);
197 xdg_surface_destroy(window->xdg_surface);
198 wl_surface_destroy(window->surface);
202 static struct buffer *
203 window_next_buffer(struct window *window)
205 struct buffer *buffer;
208 if (!window->buffers[0].busy)
209 buffer = &window->buffers[0];
210 else if (!window->buffers[1].busy)
211 buffer = &window->buffers[1];
215 if (!buffer->buffer) {
216 ret = create_shm_buffer(window->display, buffer,
217 window->width, window->height,
218 WL_SHM_FORMAT_XRGB8888);
223 /* paint the padding */
224 memset(buffer->shm_data, 0xff,
225 window->width * window->height * 4);
232 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
234 const int halfh = padding + (height - padding * 2) / 2;
235 const int halfw = padding + (width - padding * 2) / 2;
237 uint32_t *pixel = image;
240 /* squared radii thresholds */
241 or = (halfw < halfh ? halfw : halfh) - 8;
246 pixel += padding * width;
247 for (y = padding; y < height - padding; y++) {
249 int y2 = (y - halfh) * (y - halfh);
252 for (x = padding; x < width - padding; x++) {
255 /* squared distance from center */
256 int r2 = (x - halfw) * (x - halfw) + y2;
259 v = (r2 / 32 + time / 64) * 0x0080401;
261 v = (y + time / 32) * 0x0080401;
263 v = (x + time / 16) * 0x0080401;
266 /* cross if compositor uses X from XRGB as alpha */
267 if (abs(x - y) > 6 && abs(x + y - height) > 6)
277 static const struct wl_callback_listener frame_listener;
280 redraw(void *data, struct wl_callback *callback, uint32_t time)
282 struct window *window = data;
283 struct buffer *buffer;
285 buffer = window_next_buffer(window);
288 !callback ? "Failed to create the first buffer.\n" :
289 "Both buffers busy at redraw(). Server bug?\n");
293 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
295 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
296 wl_surface_damage(window->surface,
297 20, 20, window->width - 40, window->height - 40);
300 wl_callback_destroy(callback);
302 window->callback = wl_surface_frame(window->surface);
303 wl_callback_add_listener(window->callback, &frame_listener, window);
304 wl_surface_commit(window->surface);
308 static const struct wl_callback_listener frame_listener = {
313 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
315 struct display *d = data;
317 d->formats |= (1 << format);
320 struct wl_shm_listener shm_listener = {
325 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
327 xdg_shell_pong(shell, serial);
330 static const struct xdg_shell_listener xdg_shell_listener = {
334 #define XDG_VERSION 2 /* The version of xdg-shell that we implement */
336 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
337 "Interface version doesn't match implementation version");
341 registry_handle_global(void *data, struct wl_registry *registry,
342 uint32_t id, const char *interface, uint32_t version)
344 struct display *d = data;
346 if (strcmp(interface, "wl_compositor") == 0) {
348 wl_registry_bind(registry,
349 id, &wl_compositor_interface, 1);
350 } else if (strcmp(interface, "xdg_shell") == 0) {
351 d->shell = wl_registry_bind(registry,
352 id, &xdg_shell_interface, 1);
353 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
354 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
355 } else if (strcmp(interface, "wl_shm") == 0) {
356 d->shm = wl_registry_bind(registry,
357 id, &wl_shm_interface, 1);
358 wl_shm_add_listener(d->shm, &shm_listener, d);
363 registry_handle_global_remove(void *data, struct wl_registry *registry,
368 static const struct wl_registry_listener registry_listener = {
369 registry_handle_global,
370 registry_handle_global_remove
373 static struct display *
376 struct display *display;
378 display = malloc(sizeof *display);
379 if (display == NULL) {
380 fprintf(stderr, "out of memory\n");
383 display->display = wl_display_connect(NULL);
384 assert(display->display);
386 display->formats = 0;
387 display->registry = wl_display_get_registry(display->display);
388 wl_registry_add_listener(display->registry,
389 ®istry_listener, display);
390 wl_display_roundtrip(display->display);
391 if (display->shm == NULL) {
392 fprintf(stderr, "No wl_shm global\n");
396 wl_display_roundtrip(display->display);
398 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
399 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
403 wl_display_get_fd(display->display);
409 destroy_display(struct display *display)
412 wl_shm_destroy(display->shm);
415 xdg_shell_destroy(display->shell);
417 if (display->compositor)
418 wl_compositor_destroy(display->compositor);
420 wl_registry_destroy(display->registry);
421 wl_display_flush(display->display);
422 wl_display_disconnect(display->display);
426 static int running = 1;
429 signal_int(int signum)
435 main(int argc, char **argv)
437 struct sigaction sigint;
438 struct display *display;
439 struct window *window;
442 display = create_display();
443 window = create_window(display, 250, 250);
447 sigint.sa_handler = signal_int;
448 sigemptyset(&sigint.sa_mask);
449 sigint.sa_flags = SA_RESETHAND;
450 sigaction(SIGINT, &sigint, NULL);
452 /* Initialise damage to full surface, so the padding gets painted */
453 wl_surface_damage(window->surface, 0, 0,
454 window->width, window->height);
456 redraw(window, NULL, 0);
458 while (running && ret != -1)
459 ret = wl_display_dispatch(display->display);
461 fprintf(stderr, "simple-shm exiting\n");
462 destroy_window(window);
463 destroy_display(display);