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
33 #include <wayland-client.h>
34 #include "../shared/os-compatibility.h"
37 struct wl_display *display;
38 struct wl_registry *registry;
39 struct wl_compositor *compositor;
40 struct wl_shell *shell;
46 struct display *display;
48 struct wl_surface *surface;
49 struct wl_shell_surface *shell_surface;
50 struct wl_buffer *buffer;
52 struct wl_callback *callback;
55 static struct wl_buffer *
56 create_shm_buffer(struct display *display,
57 int width, int height, uint32_t format, void **data_out)
59 struct wl_shm_pool *pool;
60 struct wl_buffer *buffer;
65 size = stride * height;
67 fd = os_create_anonymous_file(size);
69 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
74 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
75 if (data == MAP_FAILED) {
76 fprintf(stderr, "mmap failed: %m\n");
81 pool = wl_shm_create_pool(display->shm, fd, size);
82 buffer = wl_shm_pool_create_buffer(pool, 0,
83 width, height, stride, format);
84 wl_shm_pool_destroy(pool);
93 handle_ping(void *data, struct wl_shell_surface *shell_surface,
96 wl_shell_surface_pong(shell_surface, serial);
100 handle_configure(void *data, struct wl_shell_surface *shell_surface,
101 uint32_t edges, int32_t width, int32_t height)
106 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
110 static const struct wl_shell_surface_listener shell_surface_listener = {
116 static struct window *
117 create_window(struct display *display, int width, int height)
119 struct window *window;
121 window = malloc(sizeof *window);
123 window->buffer = create_shm_buffer(display,
125 WL_SHM_FORMAT_XRGB8888,
128 if (!window->buffer) {
133 window->callback = NULL;
134 window->display = display;
135 window->width = width;
136 window->height = height;
137 window->surface = wl_compositor_create_surface(display->compositor);
138 window->shell_surface = wl_shell_get_shell_surface(display->shell,
141 if (window->shell_surface)
142 wl_shell_surface_add_listener(window->shell_surface,
143 &shell_surface_listener, window);
145 wl_shell_surface_set_title(window->shell_surface, "simple-shm");
147 wl_shell_surface_set_toplevel(window->shell_surface);
153 destroy_window(struct window *window)
155 if (window->callback)
156 wl_callback_destroy(window->callback);
158 wl_buffer_destroy(window->buffer);
159 wl_shell_surface_destroy(window->shell_surface);
160 wl_surface_destroy(window->surface);
165 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
167 const int halfh = padding + (height - padding * 2) / 2;
168 const int halfw = padding + (width - padding * 2) / 2;
170 uint32_t *pixel = image;
173 /* squared radii thresholds */
174 or = (halfw < halfh ? halfw : halfh) - 8;
179 pixel += padding * width;
180 for (y = padding; y < height - padding; y++) {
182 int y2 = (y - halfh) * (y - halfh);
185 for (x = padding; x < width - padding; x++) {
188 /* squared distance from center */
189 int r2 = (x - halfw) * (x - halfw) + y2;
192 v = (r2 / 32 + time / 64) * 0x0080401;
194 v = (y + time / 32) * 0x0080401;
196 v = (x + time / 16) * 0x0080401;
199 /* cross if compositor uses X from XRGB as alpha */
200 if (abs(x - y) > 6 && abs(x + y - height) > 6)
210 static const struct wl_callback_listener frame_listener;
213 redraw(void *data, struct wl_callback *callback, uint32_t time)
215 struct window *window = data;
217 paint_pixels(window->shm_data, 20, window->width, window->height, time);
218 wl_surface_damage(window->surface,
219 20, 20, window->width - 40, window->height - 40);
222 wl_callback_destroy(callback);
224 window->callback = wl_surface_frame(window->surface);
225 wl_callback_add_listener(window->callback, &frame_listener, window);
226 wl_surface_commit(window->surface);
229 static const struct wl_callback_listener frame_listener = {
234 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
236 struct display *d = data;
238 d->formats |= (1 << format);
241 struct wl_shm_listener shm_listenter = {
246 registry_handle_global(void *data, struct wl_registry *registry,
247 uint32_t id, const char *interface, uint32_t version)
249 struct display *d = data;
251 if (strcmp(interface, "wl_compositor") == 0) {
253 wl_registry_bind(registry,
254 id, &wl_compositor_interface, 1);
255 } else if (strcmp(interface, "wl_shell") == 0) {
256 d->shell = wl_registry_bind(registry,
257 id, &wl_shell_interface, 1);
258 } else if (strcmp(interface, "wl_shm") == 0) {
259 d->shm = wl_registry_bind(registry,
260 id, &wl_shm_interface, 1);
261 wl_shm_add_listener(d->shm, &shm_listenter, d);
265 static const struct wl_registry_listener registry_listener = {
266 registry_handle_global
269 static struct display *
272 struct display *display;
274 display = malloc(sizeof *display);
275 display->display = wl_display_connect(NULL);
276 assert(display->display);
278 display->formats = 0;
279 display->registry = wl_display_get_registry(display->display);
280 wl_registry_add_listener(display->registry,
281 ®istry_listener, display);
282 wl_display_roundtrip(display->display);
283 if (display->shm == NULL) {
284 fprintf(stderr, "No wl_shm global\n");
288 wl_display_roundtrip(display->display);
290 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
291 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
295 wl_display_get_fd(display->display);
301 destroy_display(struct display *display)
304 wl_shm_destroy(display->shm);
307 wl_shell_destroy(display->shell);
309 if (display->compositor)
310 wl_compositor_destroy(display->compositor);
312 wl_display_flush(display->display);
313 wl_display_disconnect(display->display);
317 static int running = 1;
320 signal_int(int signum)
326 main(int argc, char **argv)
328 struct sigaction sigint;
329 struct display *display;
330 struct window *window;
333 display = create_display();
334 window = create_window(display, 250, 250);
338 sigint.sa_handler = signal_int;
339 sigemptyset(&sigint.sa_mask);
340 sigint.sa_flags = SA_RESETHAND;
341 sigaction(SIGINT, &sigint, NULL);
343 memset(window->shm_data, 0xff, window->width * window->height * 4);
344 wl_surface_attach(window->surface, window->buffer, 0, 0);
346 redraw(window, NULL, 0);
348 while (running && ret != -1)
349 ret = wl_display_dispatch(display->display);
351 fprintf(stderr, "simple-shm exiting\n");
352 destroy_window(window);
353 destroy_display(display);