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"
38 #include "fullscreen-shell-client-protocol.h"
41 struct wl_display *display;
42 struct wl_registry *registry;
43 struct wl_compositor *compositor;
44 struct xdg_shell *shell;
45 struct _wl_fullscreen_shell *fshell;
51 struct wl_buffer *buffer;
57 struct display *display;
59 struct wl_surface *surface;
60 struct xdg_surface *xdg_surface;
61 struct buffer buffers[2];
62 struct buffer *prev_buffer;
63 struct wl_callback *callback;
66 static int running = 1;
69 buffer_release(void *data, struct wl_buffer *buffer)
71 struct buffer *mybuf = data;
76 static const struct wl_buffer_listener buffer_listener = {
81 create_shm_buffer(struct display *display, struct buffer *buffer,
82 int width, int height, uint32_t format)
84 struct wl_shm_pool *pool;
89 size = stride * height;
91 fd = os_create_anonymous_file(size);
93 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
98 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
99 if (data == MAP_FAILED) {
100 fprintf(stderr, "mmap failed: %m\n");
105 pool = wl_shm_create_pool(display->shm, fd, size);
106 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
109 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
110 wl_shm_pool_destroy(pool);
113 buffer->shm_data = data;
119 handle_configure(void *data, struct xdg_surface *surface,
120 int32_t width, int32_t height,
121 struct wl_array *states, uint32_t serial)
123 xdg_surface_ack_configure(surface, serial);
127 handle_delete(void *data, struct xdg_surface *xdg_surface)
132 static const struct xdg_surface_listener xdg_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);
152 if (display->shell) {
153 window->xdg_surface =
154 xdg_shell_get_xdg_surface(display->shell,
157 assert(window->xdg_surface);
159 xdg_surface_add_listener(window->xdg_surface,
160 &xdg_surface_listener, window);
162 xdg_surface_set_title(window->xdg_surface, "simple-shm");
163 } else if (display->fshell) {
164 _wl_fullscreen_shell_present_surface(display->fshell,
166 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,
176 destroy_window(struct window *window)
178 if (window->callback)
179 wl_callback_destroy(window->callback);
181 if (window->buffers[0].buffer)
182 wl_buffer_destroy(window->buffers[0].buffer);
183 if (window->buffers[1].buffer)
184 wl_buffer_destroy(window->buffers[1].buffer);
186 if (window->xdg_surface)
187 xdg_surface_destroy(window->xdg_surface);
188 wl_surface_destroy(window->surface);
192 static struct buffer *
193 window_next_buffer(struct window *window)
195 struct buffer *buffer;
198 if (!window->buffers[0].busy)
199 buffer = &window->buffers[0];
200 else if (!window->buffers[1].busy)
201 buffer = &window->buffers[1];
205 if (!buffer->buffer) {
206 ret = create_shm_buffer(window->display, buffer,
207 window->width, window->height,
208 WL_SHM_FORMAT_XRGB8888);
213 /* paint the padding */
214 memset(buffer->shm_data, 0xff,
215 window->width * window->height * 4);
222 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
224 const int halfh = padding + (height - padding * 2) / 2;
225 const int halfw = padding + (width - padding * 2) / 2;
227 uint32_t *pixel = image;
230 /* squared radii thresholds */
231 or = (halfw < halfh ? halfw : halfh) - 8;
236 pixel += padding * width;
237 for (y = padding; y < height - padding; y++) {
239 int y2 = (y - halfh) * (y - halfh);
242 for (x = padding; x < width - padding; x++) {
245 /* squared distance from center */
246 int r2 = (x - halfw) * (x - halfw) + y2;
249 v = (r2 / 32 + time / 64) * 0x0080401;
251 v = (y + time / 32) * 0x0080401;
253 v = (x + time / 16) * 0x0080401;
256 /* cross if compositor uses X from XRGB as alpha */
257 if (abs(x - y) > 6 && abs(x + y - height) > 6)
267 static const struct wl_callback_listener frame_listener;
270 redraw(void *data, struct wl_callback *callback, uint32_t time)
272 struct window *window = data;
273 struct buffer *buffer;
275 buffer = window_next_buffer(window);
278 !callback ? "Failed to create the first buffer.\n" :
279 "Both buffers busy at redraw(). Server bug?\n");
283 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
285 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
286 wl_surface_damage(window->surface,
287 20, 20, window->width - 40, window->height - 40);
290 wl_callback_destroy(callback);
292 window->callback = wl_surface_frame(window->surface);
293 wl_callback_add_listener(window->callback, &frame_listener, window);
294 wl_surface_commit(window->surface);
298 static const struct wl_callback_listener frame_listener = {
303 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
305 struct display *d = data;
307 d->formats |= (1 << format);
310 struct wl_shm_listener shm_listener = {
315 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
317 xdg_shell_pong(shell, serial);
320 static const struct xdg_shell_listener xdg_shell_listener = {
324 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
326 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
327 "Interface version doesn't match implementation version");
331 registry_handle_global(void *data, struct wl_registry *registry,
332 uint32_t id, const char *interface, uint32_t version)
334 struct display *d = data;
336 if (strcmp(interface, "wl_compositor") == 0) {
338 wl_registry_bind(registry,
339 id, &wl_compositor_interface, 1);
340 } else if (strcmp(interface, "xdg_shell") == 0) {
341 d->shell = wl_registry_bind(registry,
342 id, &xdg_shell_interface, 1);
343 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
344 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
345 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
346 d->fshell = wl_registry_bind(registry,
347 id, &_wl_fullscreen_shell_interface, 1);
348 } else if (strcmp(interface, "wl_shm") == 0) {
349 d->shm = wl_registry_bind(registry,
350 id, &wl_shm_interface, 1);
351 wl_shm_add_listener(d->shm, &shm_listener, d);
356 registry_handle_global_remove(void *data, struct wl_registry *registry,
361 static const struct wl_registry_listener registry_listener = {
362 registry_handle_global,
363 registry_handle_global_remove
366 static struct display *
369 struct display *display;
371 display = malloc(sizeof *display);
372 if (display == NULL) {
373 fprintf(stderr, "out of memory\n");
376 display->display = wl_display_connect(NULL);
377 assert(display->display);
379 display->formats = 0;
380 display->registry = wl_display_get_registry(display->display);
381 wl_registry_add_listener(display->registry,
382 ®istry_listener, display);
383 wl_display_roundtrip(display->display);
384 if (display->shm == NULL) {
385 fprintf(stderr, "No wl_shm global\n");
389 wl_display_roundtrip(display->display);
391 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
392 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
396 wl_display_get_fd(display->display);
402 destroy_display(struct display *display)
405 wl_shm_destroy(display->shm);
408 xdg_shell_destroy(display->shell);
411 _wl_fullscreen_shell_release(display->fshell);
413 if (display->compositor)
414 wl_compositor_destroy(display->compositor);
416 wl_registry_destroy(display->registry);
417 wl_display_flush(display->display);
418 wl_display_disconnect(display->display);
423 signal_int(int signum)
429 main(int argc, char **argv)
431 struct sigaction sigint;
432 struct display *display;
433 struct window *window;
436 display = create_display();
437 window = create_window(display, 250, 250);
441 sigint.sa_handler = signal_int;
442 sigemptyset(&sigint.sa_mask);
443 sigint.sa_flags = SA_RESETHAND;
444 sigaction(SIGINT, &sigint, NULL);
446 /* Initialise damage to full surface, so the padding gets painted */
447 wl_surface_damage(window->surface, 0, 0,
448 window->width, window->height);
450 redraw(window, NULL, 0);
452 while (running && ret != -1)
453 ret = wl_display_dispatch(display->display);
455 fprintf(stderr, "simple-shm exiting\n");
456 destroy_window(window);
457 destroy_display(display);