blob: 8a3c5394d7797d8665574d4a2ca4893e5c203a87 [file] [log] [blame]
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -04001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040023 */
24
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070025#include <config.h>
26
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040031#include <assert.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040032#include <unistd.h>
33#include <sys/mman.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020034#include <signal.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040035
36#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070037#include "shared/os-compatibility.h"
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080038#include "xdg-shell-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080039#include "fullscreen-shell-unstable-v1-client-protocol.h"
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040040
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090041#include <sys/types.h>
42#include "ivi-application-client-protocol.h"
43#define IVI_SURFACE_ID 9000
44
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040045struct display {
46 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040047 struct wl_registry *registry;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040048 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080049 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080050 struct zwp_fullscreen_shell_v1 *fshell;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040051 struct wl_shm *shm;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -050052 uint32_t formats;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090053 struct ivi_application *ivi_application;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040054};
55
Pekka Paalanen99b705b2012-11-19 15:29:09 +020056struct buffer {
57 struct wl_buffer *buffer;
58 void *shm_data;
59 int busy;
60};
61
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040062struct window {
63 struct display *display;
64 int width, height;
65 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080066 struct xdg_surface *xdg_surface;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090067 struct ivi_surface *ivi_surface;
Pekka Paalanen99b705b2012-11-19 15:29:09 +020068 struct buffer buffers[2];
69 struct buffer *prev_buffer;
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +020070 struct wl_callback *callback;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040071};
72
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -050073static int running = 1;
74
Pekka Paalanen99b705b2012-11-19 15:29:09 +020075static void
76buffer_release(void *data, struct wl_buffer *buffer)
77{
78 struct buffer *mybuf = data;
79
80 mybuf->busy = 0;
81}
82
83static const struct wl_buffer_listener buffer_listener = {
84 buffer_release
85};
86
87static int
88create_shm_buffer(struct display *display, struct buffer *buffer,
89 int width, int height, uint32_t format)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040090{
Kristian Høgsberg16626282012-04-03 11:21:27 -040091 struct wl_shm_pool *pool;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040092 int fd, size, stride;
93 void *data;
94
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040095 stride = width * 4;
96 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030097
98 fd = os_create_anonymous_file(size);
99 if (fd < 0) {
100 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
101 size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200102 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400103 }
104
105 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400106 if (data == MAP_FAILED) {
107 fprintf(stderr, "mmap failed: %m\n");
108 close(fd);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200109 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400110 }
111
Kristian Høgsberg16626282012-04-03 11:21:27 -0400112 pool = wl_shm_create_pool(display->shm, fd, size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200113 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
114 width, height,
115 stride, format);
116 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400117 wl_shm_pool_destroy(pool);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400118 close(fd);
119
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200120 buffer->shm_data = data;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400121
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200122 return 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400123}
124
Scott Moreau7c8b1162012-05-12 11:57:42 -0600125static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800126handle_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700127 int32_t width, int32_t height,
128 struct wl_array *states, uint32_t serial)
Scott Moreau7c8b1162012-05-12 11:57:42 -0600129{
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700130 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800131}
132
133static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500134handle_delete(void *data, struct xdg_surface *xdg_surface)
135{
136 running = 0;
137}
138
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800139static const struct xdg_surface_listener xdg_surface_listener = {
Scott Moreau7c8b1162012-05-12 11:57:42 -0600140 handle_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500141 handle_delete,
Scott Moreau7c8b1162012-05-12 11:57:42 -0600142};
143
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900144static void
145handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
146 int32_t width, int32_t height)
147{
148 /* Simple-shm is resizable */
149}
150
151static const struct ivi_surface_listener ivi_surface_listener = {
152 handle_ivi_surface_configure,
153};
154
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400155static struct window *
156create_window(struct display *display, int width, int height)
157{
158 struct window *window;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300159
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200160 window = calloc(1, sizeof *window);
161 if (!window)
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300162 return NULL;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300163
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200164 window->callback = NULL;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400165 window->display = display;
166 window->width = width;
167 window->height = height;
168 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400169
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500170 if (display->shell) {
171 window->xdg_surface =
172 xdg_shell_get_xdg_surface(display->shell,
173 window->surface);
174
175 assert(window->xdg_surface);
176
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800177 xdg_surface_add_listener(window->xdg_surface,
178 &xdg_surface_listener, window);
Scott Moreau7c8b1162012-05-12 11:57:42 -0600179
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500180 xdg_surface_set_title(window->xdg_surface, "simple-shm");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900181
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500182 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800183 zwp_fullscreen_shell_v1_present_surface(display->fshell,
184 window->surface,
185 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
186 NULL);
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900187 } else if (display->ivi_application ) {
188 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
189 window->ivi_surface =
190 ivi_application_surface_create(display->ivi_application,
191 id_ivisurf, window->surface);
192 if (window->ivi_surface == NULL) {
193 fprintf(stderr, "Failed to create ivi_client_surface\n");
194 abort();
195 }
196
197 ivi_surface_add_listener(window->ivi_surface,
198 &ivi_surface_listener, window);
199
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500200 } else {
201 assert(0);
202 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400203
204 return window;
205}
206
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200207static void
208destroy_window(struct window *window)
209{
210 if (window->callback)
211 wl_callback_destroy(window->callback);
212
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200213 if (window->buffers[0].buffer)
214 wl_buffer_destroy(window->buffers[0].buffer);
215 if (window->buffers[1].buffer)
216 wl_buffer_destroy(window->buffers[1].buffer);
217
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500218 if (window->xdg_surface)
219 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200220 wl_surface_destroy(window->surface);
221 free(window);
222}
223
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200224static struct buffer *
225window_next_buffer(struct window *window)
226{
227 struct buffer *buffer;
228 int ret = 0;
229
230 if (!window->buffers[0].busy)
231 buffer = &window->buffers[0];
232 else if (!window->buffers[1].busy)
233 buffer = &window->buffers[1];
234 else
235 return NULL;
236
237 if (!buffer->buffer) {
238 ret = create_shm_buffer(window->display, buffer,
239 window->width, window->height,
240 WL_SHM_FORMAT_XRGB8888);
241
242 if (ret < 0)
243 return NULL;
244
245 /* paint the padding */
246 memset(buffer->shm_data, 0xff,
247 window->width * window->height * 4);
248 }
249
250 return buffer;
251}
252
Pekka Paalanen313bd842012-04-26 15:14:50 +0300253static void
Rob Bradford371805f2012-10-02 18:03:15 +0100254paint_pixels(void *image, int padding, int width, int height, uint32_t time)
Pekka Paalanen313bd842012-04-26 15:14:50 +0300255{
Rob Bradford371805f2012-10-02 18:03:15 +0100256 const int halfh = padding + (height - padding * 2) / 2;
257 const int halfw = padding + (width - padding * 2) / 2;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300258 int ir, or;
259 uint32_t *pixel = image;
260 int y;
261
262 /* squared radii thresholds */
263 or = (halfw < halfh ? halfw : halfh) - 8;
264 ir = or - 32;
265 or *= or;
266 ir *= ir;
267
Rob Bradford371805f2012-10-02 18:03:15 +0100268 pixel += padding * width;
269 for (y = padding; y < height - padding; y++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300270 int x;
271 int y2 = (y - halfh) * (y - halfh);
272
Rob Bradford371805f2012-10-02 18:03:15 +0100273 pixel += padding;
274 for (x = padding; x < width - padding; x++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300275 uint32_t v;
276
277 /* squared distance from center */
278 int r2 = (x - halfw) * (x - halfw) + y2;
279
280 if (r2 < ir)
281 v = (r2 / 32 + time / 64) * 0x0080401;
282 else if (r2 < or)
283 v = (y + time / 32) * 0x0080401;
284 else
285 v = (x + time / 16) * 0x0080401;
286 v &= 0x00ffffff;
287
288 /* cross if compositor uses X from XRGB as alpha */
289 if (abs(x - y) > 6 && abs(x + y - height) > 6)
290 v |= 0xff000000;
291
292 *pixel++ = v;
293 }
Rob Bradford371805f2012-10-02 18:03:15 +0100294
295 pixel += padding;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300296 }
297}
298
Kristian Høgsberg33418202011-08-16 23:01:28 -0400299static const struct wl_callback_listener frame_listener;
300
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400301static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400302redraw(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400303{
304 struct window *window = data;
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200305 struct buffer *buffer;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400306
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200307 buffer = window_next_buffer(window);
308 if (!buffer) {
309 fprintf(stderr,
310 !callback ? "Failed to create the first buffer.\n" :
311 "Both buffers busy at redraw(). Server bug?\n");
312 abort();
313 }
314
315 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
316
Kristian Høgsberge7144fd2013-03-04 12:11:41 -0500317 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400318 wl_surface_damage(window->surface,
Rob Bradford371805f2012-10-02 18:03:15 +0100319 20, 20, window->width - 40, window->height - 40);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400320
Kristian Høgsberg33418202011-08-16 23:01:28 -0400321 if (callback)
322 wl_callback_destroy(callback);
323
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200324 window->callback = wl_surface_frame(window->surface);
325 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300326 wl_surface_commit(window->surface);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200327 buffer->busy = 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400328}
329
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330static const struct wl_callback_listener frame_listener = {
331 redraw
332};
333
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400334static void
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500335shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
336{
337 struct display *d = data;
338
339 d->formats |= (1 << format);
340}
341
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100342struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500343 shm_format
344};
345
346static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800347xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
348{
349 xdg_shell_pong(shell, serial);
350}
351
352static const struct xdg_shell_listener xdg_shell_listener = {
353 xdg_shell_ping,
354};
355
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800356#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800357#ifdef static_assert
358static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
359 "Interface version doesn't match implementation version");
360#endif
361
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800362static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400363registry_handle_global(void *data, struct wl_registry *registry,
364 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400365{
366 struct display *d = data;
367
368 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400369 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400370 wl_registry_bind(registry,
371 id, &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800372 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400373 d->shell = wl_registry_bind(registry,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800374 id, &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800375 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800376 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800377 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500378 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800379 id, &zwp_fullscreen_shell_v1_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400380 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400381 d->shm = wl_registry_bind(registry,
382 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100383 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400384 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900385 else if (strcmp(interface, "ivi_application") == 0) {
386 d->ivi_application =
387 wl_registry_bind(registry, id,
388 &ivi_application_interface, 1);
389 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400390}
391
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200392static void
393registry_handle_global_remove(void *data, struct wl_registry *registry,
394 uint32_t name)
395{
396}
397
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400398static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200399 registry_handle_global,
400 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400401};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400402
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400403static struct display *
404create_display(void)
405{
406 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400407
408 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700409 if (display == NULL) {
410 fprintf(stderr, "out of memory\n");
411 exit(1);
412 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400413 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300414 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400415
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500416 display->formats = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400417 display->registry = wl_display_get_registry(display->display);
418 wl_registry_add_listener(display->registry,
419 &registry_listener, display);
420 wl_display_roundtrip(display->display);
421 if (display->shm == NULL) {
422 fprintf(stderr, "No wl_shm global\n");
423 exit(1);
424 }
425
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500426 wl_display_roundtrip(display->display);
427
Pekka Paalanen8b521182014-11-25 09:56:53 +0200428 /*
429 * Why do we need two roundtrips here?
430 *
431 * wl_display_get_registry() sends a request to the server, to which
432 * the server replies by emitting the wl_registry.global events.
433 * The first wl_display_roundtrip() sends wl_display.sync. The server
434 * first processes the wl_display.get_registry which includes sending
435 * the global events, and then processes the sync. Therefore when the
436 * sync (roundtrip) returns, we are guaranteed to have received and
437 * processed all the global events.
438 *
439 * While we are inside the first wl_display_roundtrip(), incoming
440 * events are dispatched, which causes registry_handle_global() to
441 * be called for each global. One of these globals is wl_shm.
442 * registry_handle_global() sends wl_registry.bind request for the
443 * wl_shm global. However, wl_registry.bind request is sent after
444 * the first wl_display.sync, so the reply to the sync comes before
445 * the initial events of the wl_shm object.
446 *
447 * The initial events that get sent as a reply to binding to wl_shm
448 * include wl_shm.format. These tell us which pixel formats are
449 * supported, and we need them before we can create buffers. They
450 * don't change at runtime, so we receive them as part of init.
451 *
452 * When the reply to the first sync comes, the server may or may not
453 * have sent the initial wl_shm events. Therefore we need the second
454 * wl_display_roundtrip() call here.
455 *
456 * The server processes the wl_registry.bind for wl_shm first, and
457 * the second wl_display.sync next. During our second call to
458 * wl_display_roundtrip() the initial wl_shm events are received and
459 * processed. Finally, when the reply to the second wl_display.sync
460 * arrives, it guarantees we have processed all wl_shm initial events.
461 *
462 * This sequence contains two examples on how wl_display_roundtrip()
463 * can be used to guarantee, that all reply events to a request
464 * have been received and processed. This is a general Wayland
465 * technique.
466 */
467
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500468 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500469 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
470 exit(1);
471 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400472
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400473 return display;
474}
475
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200476static void
477destroy_display(struct display *display)
478{
479 if (display->shm)
480 wl_shm_destroy(display->shm);
481
482 if (display->shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800483 xdg_shell_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200484
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500485 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800486 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500487
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200488 if (display->compositor)
489 wl_compositor_destroy(display->compositor);
490
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200491 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200492 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500493 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200494 free(display);
495}
496
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200497static void
498signal_int(int signum)
499{
500 running = 0;
501}
502
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400503int
504main(int argc, char **argv)
505{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200506 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400507 struct display *display;
508 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300509 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400510
511 display = create_display();
512 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300513 if (!window)
514 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400515
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200516 sigint.sa_handler = signal_int;
517 sigemptyset(&sigint.sa_mask);
518 sigint.sa_flags = SA_RESETHAND;
519 sigaction(SIGINT, &sigint, NULL);
520
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200521 /* Initialise damage to full surface, so the padding gets painted */
522 wl_surface_damage(window->surface, 0, 0,
523 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100524
Kristian Høgsberg33418202011-08-16 23:01:28 -0400525 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400526
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400527 while (running && ret != -1)
528 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400529
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200530 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900531
532 if (window->display->ivi_application) {
533 ivi_surface_destroy(window->ivi_surface);
534 ivi_application_destroy(window->display->ivi_application);
535 }
536
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200537 destroy_window(window);
538 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200539
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400540 return 0;
541}