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_compositor *compositor;
39 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);
228 static const struct wl_callback_listener frame_listener = {
233 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
235 struct display *d = data;
237 d->formats |= (1 << format);
240 struct wl_shm_listener shm_listenter = {
245 display_handle_global(struct wl_display *display, uint32_t id,
246 const char *interface, uint32_t version, void *data)
248 struct display *d = data;
250 if (strcmp(interface, "wl_compositor") == 0) {
252 wl_display_bind(display, id, &wl_compositor_interface);
253 } else if (strcmp(interface, "wl_shell") == 0) {
254 d->shell = wl_display_bind(display, id, &wl_shell_interface);
255 } else if (strcmp(interface, "wl_shm") == 0) {
256 d->shm = wl_display_bind(display, id, &wl_shm_interface);
257 wl_shm_add_listener(d->shm, &shm_listenter, d);
262 event_mask_update(uint32_t mask, void *data)
264 struct display *d = data;
271 static struct display *
274 struct display *display;
276 display = malloc(sizeof *display);
277 display->display = wl_display_connect(NULL);
278 assert(display->display);
280 display->formats = 0;
281 wl_display_add_global_listener(display->display,
282 display_handle_global, display);
283 wl_display_iterate(display->display, WL_DISPLAY_READABLE);
284 wl_display_roundtrip(display->display);
286 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
287 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
291 wl_display_get_fd(display->display, event_mask_update, display);
297 destroy_display(struct display *display)
300 wl_shm_destroy(display->shm);
303 wl_shell_destroy(display->shell);
305 if (display->compositor)
306 wl_compositor_destroy(display->compositor);
308 wl_display_flush(display->display);
309 wl_display_disconnect(display->display);
313 static int running = 1;
316 signal_int(int signum)
322 main(int argc, char **argv)
324 struct sigaction sigint;
325 struct display *display;
326 struct window *window;
328 display = create_display();
329 window = create_window(display, 250, 250);
333 sigint.sa_handler = signal_int;
334 sigemptyset(&sigint.sa_mask);
335 sigint.sa_flags = SA_RESETHAND;
336 sigaction(SIGINT, &sigint, NULL);
338 memset(window->shm_data, 0xff, window->width * window->height * 4);
339 wl_surface_attach(window->surface, window->buffer, 0, 0);
341 redraw(window, NULL, 0);
344 wl_display_iterate(display->display, display->mask);
346 fprintf(stderr, "simple-shm exiting\n");
347 destroy_window(window);
348 destroy_display(display);