client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.git] / clients / simple-shm.c
1 /*
2  * Copyright © 2011 Benjamin Franzke
3  * Copyright © 2010 Intel Corporation
4  *
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.
14  *
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
21  * OF THIS SOFTWARE.
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <signal.h>
34
35 #include <wayland-client.h>
36 #include "../shared/os-compatibility.h"
37 #include "xdg-shell-client-protocol.h"
38
39 struct display {
40         struct wl_display *display;
41         struct wl_registry *registry;
42         struct wl_compositor *compositor;
43         struct xdg_shell *shell;
44         struct wl_shm *shm;
45         uint32_t formats;
46 };
47
48 struct buffer {
49         struct wl_buffer *buffer;
50         void *shm_data;
51         int busy;
52 };
53
54 struct window {
55         struct display *display;
56         int width, height;
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;
62 };
63
64 static void
65 buffer_release(void *data, struct wl_buffer *buffer)
66 {
67         struct buffer *mybuf = data;
68
69         mybuf->busy = 0;
70 }
71
72 static const struct wl_buffer_listener buffer_listener = {
73         buffer_release
74 };
75
76 static int
77 create_shm_buffer(struct display *display, struct buffer *buffer,
78                   int width, int height, uint32_t format)
79 {
80         struct wl_shm_pool *pool;
81         int fd, size, stride;
82         void *data;
83
84         stride = width * 4;
85         size = stride * height;
86
87         fd = os_create_anonymous_file(size);
88         if (fd < 0) {
89                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
90                         size);
91                 return -1;
92         }
93
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");
97                 close(fd);
98                 return -1;
99         }
100
101         pool = wl_shm_create_pool(display->shm, fd, size);
102         buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
103                                                    width, height,
104                                                    stride, format);
105         wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
106         wl_shm_pool_destroy(pool);
107         close(fd);
108
109         buffer->shm_data = data;
110
111         return 0;
112 }
113
114 static void
115 handle_configure(void *data, struct xdg_surface *surface,
116                  int32_t width, int32_t height)
117 {
118 }
119
120 static void
121 handle_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
122 {
123 }
124
125 static void
126 handle_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
127 {
128 }
129
130 static void
131 handle_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
132 {
133 }
134
135 static void
136 handle_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
137 {
138 }
139
140 static void
141 handle_focused_set(void *data, struct xdg_surface *xdg_surface)
142 {
143 }
144
145 static void
146 handle_focused_unset(void *data, struct xdg_surface *xdg_surface)
147 {
148 }
149
150 static const struct xdg_surface_listener xdg_surface_listener = {
151         handle_configure,
152         handle_request_set_maximized,
153         handle_request_unset_maximized,
154         handle_request_set_fullscreen,
155         handle_request_unset_fullscreen,
156         handle_focused_set,
157         handle_focused_unset,
158 };
159
160 static struct window *
161 create_window(struct display *display, int width, int height)
162 {
163         struct window *window;
164
165         window = calloc(1, sizeof *window);
166         if (!window)
167                 return NULL;
168
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,
175                                                         window->surface);
176
177         if (window->xdg_surface)
178                 xdg_surface_add_listener(window->xdg_surface,
179                                          &xdg_surface_listener, window);
180
181         xdg_surface_set_title(window->xdg_surface, "simple-shm");
182
183         return window;
184 }
185
186 static void
187 destroy_window(struct window *window)
188 {
189         if (window->callback)
190                 wl_callback_destroy(window->callback);
191
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);
196
197         xdg_surface_destroy(window->xdg_surface);
198         wl_surface_destroy(window->surface);
199         free(window);
200 }
201
202 static struct buffer *
203 window_next_buffer(struct window *window)
204 {
205         struct buffer *buffer;
206         int ret = 0;
207
208         if (!window->buffers[0].busy)
209                 buffer = &window->buffers[0];
210         else if (!window->buffers[1].busy)
211                 buffer = &window->buffers[1];
212         else
213                 return NULL;
214
215         if (!buffer->buffer) {
216                 ret = create_shm_buffer(window->display, buffer,
217                                         window->width, window->height,
218                                         WL_SHM_FORMAT_XRGB8888);
219
220                 if (ret < 0)
221                         return NULL;
222
223                 /* paint the padding */
224                 memset(buffer->shm_data, 0xff,
225                        window->width * window->height * 4);
226         }
227
228         return buffer;
229 }
230
231 static void
232 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
233 {
234         const int halfh = padding + (height - padding * 2) / 2;
235         const int halfw = padding + (width  - padding * 2) / 2;
236         int ir, or;
237         uint32_t *pixel = image;
238         int y;
239
240         /* squared radii thresholds */
241         or = (halfw < halfh ? halfw : halfh) - 8;
242         ir = or - 32;
243         or *= or;
244         ir *= ir;
245
246         pixel += padding * width;
247         for (y = padding; y < height - padding; y++) {
248                 int x;
249                 int y2 = (y - halfh) * (y - halfh);
250
251                 pixel += padding;
252                 for (x = padding; x < width - padding; x++) {
253                         uint32_t v;
254
255                         /* squared distance from center */
256                         int r2 = (x - halfw) * (x - halfw) + y2;
257
258                         if (r2 < ir)
259                                 v = (r2 / 32 + time / 64) * 0x0080401;
260                         else if (r2 < or)
261                                 v = (y + time / 32) * 0x0080401;
262                         else
263                                 v = (x + time / 16) * 0x0080401;
264                         v &= 0x00ffffff;
265
266                         /* cross if compositor uses X from XRGB as alpha */
267                         if (abs(x - y) > 6 && abs(x + y - height) > 6)
268                                 v |= 0xff000000;
269
270                         *pixel++ = v;
271                 }
272
273                 pixel += padding;
274         }
275 }
276
277 static const struct wl_callback_listener frame_listener;
278
279 static void
280 redraw(void *data, struct wl_callback *callback, uint32_t time)
281 {
282         struct window *window = data;
283         struct buffer *buffer;
284
285         buffer = window_next_buffer(window);
286         if (!buffer) {
287                 fprintf(stderr,
288                         !callback ? "Failed to create the first buffer.\n" :
289                         "Both buffers busy at redraw(). Server bug?\n");
290                 abort();
291         }
292
293         paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
294
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);
298
299         if (callback)
300                 wl_callback_destroy(callback);
301
302         window->callback = wl_surface_frame(window->surface);
303         wl_callback_add_listener(window->callback, &frame_listener, window);
304         wl_surface_commit(window->surface);
305         buffer->busy = 1;
306 }
307
308 static const struct wl_callback_listener frame_listener = {
309         redraw
310 };
311
312 static void
313 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
314 {
315         struct display *d = data;
316
317         d->formats |= (1 << format);
318 }
319
320 struct wl_shm_listener shm_listener = {
321         shm_format
322 };
323
324 static void
325 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
326 {
327         xdg_shell_pong(shell, serial);
328 }
329
330 static const struct xdg_shell_listener xdg_shell_listener = {
331         xdg_shell_ping,
332 };
333
334 #define XDG_VERSION 2 /* The version of xdg-shell that we implement */
335 #ifdef static_assert
336 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
337               "Interface version doesn't match implementation version");
338 #endif
339
340 static void
341 registry_handle_global(void *data, struct wl_registry *registry,
342                        uint32_t id, const char *interface, uint32_t version)
343 {
344         struct display *d = data;
345
346         if (strcmp(interface, "wl_compositor") == 0) {
347                 d->compositor =
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);
359         }
360 }
361
362 static void
363 registry_handle_global_remove(void *data, struct wl_registry *registry,
364                               uint32_t name)
365 {
366 }
367
368 static const struct wl_registry_listener registry_listener = {
369         registry_handle_global,
370         registry_handle_global_remove
371 };
372
373 static struct display *
374 create_display(void)
375 {
376         struct display *display;
377
378         display = malloc(sizeof *display);
379         if (display == NULL) {
380                 fprintf(stderr, "out of memory\n");
381                 exit(1);
382         }
383         display->display = wl_display_connect(NULL);
384         assert(display->display);
385
386         display->formats = 0;
387         display->registry = wl_display_get_registry(display->display);
388         wl_registry_add_listener(display->registry,
389                                  &registry_listener, display);
390         wl_display_roundtrip(display->display);
391         if (display->shm == NULL) {
392                 fprintf(stderr, "No wl_shm global\n");
393                 exit(1);
394         }
395
396         wl_display_roundtrip(display->display);
397
398         if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
399                 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
400                 exit(1);
401         }
402
403         wl_display_get_fd(display->display);
404         
405         return display;
406 }
407
408 static void
409 destroy_display(struct display *display)
410 {
411         if (display->shm)
412                 wl_shm_destroy(display->shm);
413
414         if (display->shell)
415                 xdg_shell_destroy(display->shell);
416
417         if (display->compositor)
418                 wl_compositor_destroy(display->compositor);
419
420         wl_registry_destroy(display->registry);
421         wl_display_flush(display->display);
422         wl_display_disconnect(display->display);
423         free(display);
424 }
425
426 static int running = 1;
427
428 static void
429 signal_int(int signum)
430 {
431         running = 0;
432 }
433
434 int
435 main(int argc, char **argv)
436 {
437         struct sigaction sigint;
438         struct display *display;
439         struct window *window;
440         int ret = 0;
441
442         display = create_display();
443         window = create_window(display, 250, 250);
444         if (!window)
445                 return 1;
446
447         sigint.sa_handler = signal_int;
448         sigemptyset(&sigint.sa_mask);
449         sigint.sa_flags = SA_RESETHAND;
450         sigaction(SIGINT, &sigint, NULL);
451
452         /* Initialise damage to full surface, so the padding gets painted */
453         wl_surface_damage(window->surface, 0, 0,
454                           window->width, window->height);
455
456         redraw(window, NULL, 0);
457
458         while (running && ret != -1)
459                 ret = wl_display_dispatch(display->display);
460
461         fprintf(stderr, "simple-shm exiting\n");
462         destroy_window(window);
463         destroy_display(display);
464
465         return 0;
466 }