blob: 6d8f608408c5b9033f0376e77d555b4317afc093 [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"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080038#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080039#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080040#include "fullscreen-shell-unstable-v1-client-protocol.h"
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040041
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090042#include <sys/types.h>
43#include "ivi-application-client-protocol.h"
44#define IVI_SURFACE_ID 9000
45
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040046struct display {
47 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040048 struct wl_registry *registry;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040049 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080050 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080051 struct zwp_fullscreen_shell_v1 *fshell;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040052 struct wl_shm *shm;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -050053 uint32_t formats;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090054 struct ivi_application *ivi_application;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040055};
56
Pekka Paalanen99b705b2012-11-19 15:29:09 +020057struct buffer {
58 struct wl_buffer *buffer;
59 void *shm_data;
60 int busy;
61};
62
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040063struct window {
64 struct display *display;
65 int width, height;
66 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080067 struct xdg_surface *xdg_surface;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090068 struct ivi_surface *ivi_surface;
Pekka Paalanen99b705b2012-11-19 15:29:09 +020069 struct buffer buffers[2];
70 struct buffer *prev_buffer;
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +020071 struct wl_callback *callback;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040072};
73
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -050074static int running = 1;
75
Pekka Paalanen99b705b2012-11-19 15:29:09 +020076static void
77buffer_release(void *data, struct wl_buffer *buffer)
78{
79 struct buffer *mybuf = data;
80
81 mybuf->busy = 0;
82}
83
84static const struct wl_buffer_listener buffer_listener = {
85 buffer_release
86};
87
88static int
89create_shm_buffer(struct display *display, struct buffer *buffer,
90 int width, int height, uint32_t format)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040091{
Kristian Høgsberg16626282012-04-03 11:21:27 -040092 struct wl_shm_pool *pool;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040093 int fd, size, stride;
94 void *data;
95
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040096 stride = width * 4;
97 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030098
99 fd = os_create_anonymous_file(size);
100 if (fd < 0) {
101 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
102 size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200103 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400104 }
105
106 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400107 if (data == MAP_FAILED) {
108 fprintf(stderr, "mmap failed: %m\n");
109 close(fd);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200110 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400111 }
112
Kristian Høgsberg16626282012-04-03 11:21:27 -0400113 pool = wl_shm_create_pool(display->shm, fd, size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200114 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
115 width, height,
116 stride, format);
117 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400118 wl_shm_pool_destroy(pool);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400119 close(fd);
120
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200121 buffer->shm_data = data;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400122
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200123 return 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400124}
125
Scott Moreau7c8b1162012-05-12 11:57:42 -0600126static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800127handle_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700128 int32_t width, int32_t height,
129 struct wl_array *states, uint32_t serial)
Scott Moreau7c8b1162012-05-12 11:57:42 -0600130{
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700131 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800132}
133
134static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500135handle_delete(void *data, struct xdg_surface *xdg_surface)
136{
137 running = 0;
138}
139
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800140static const struct xdg_surface_listener xdg_surface_listener = {
Scott Moreau7c8b1162012-05-12 11:57:42 -0600141 handle_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500142 handle_delete,
Scott Moreau7c8b1162012-05-12 11:57:42 -0600143};
144
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900145static void
146handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
147 int32_t width, int32_t height)
148{
149 /* Simple-shm is resizable */
150}
151
152static const struct ivi_surface_listener ivi_surface_listener = {
153 handle_ivi_surface_configure,
154};
155
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400156static struct window *
157create_window(struct display *display, int width, int height)
158{
159 struct window *window;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300160
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800161 window = zalloc(sizeof *window);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200162 if (!window)
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300163 return NULL;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300164
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200165 window->callback = NULL;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400166 window->display = display;
167 window->width = width;
168 window->height = height;
169 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400170
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500171 if (display->shell) {
172 window->xdg_surface =
173 xdg_shell_get_xdg_surface(display->shell,
174 window->surface);
175
176 assert(window->xdg_surface);
177
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800178 xdg_surface_add_listener(window->xdg_surface,
179 &xdg_surface_listener, window);
Scott Moreau7c8b1162012-05-12 11:57:42 -0600180
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500181 xdg_surface_set_title(window->xdg_surface, "simple-shm");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900182
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500183 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800184 zwp_fullscreen_shell_v1_present_surface(display->fshell,
185 window->surface,
186 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
187 NULL);
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900188 } else if (display->ivi_application ) {
189 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
190 window->ivi_surface =
191 ivi_application_surface_create(display->ivi_application,
192 id_ivisurf, window->surface);
193 if (window->ivi_surface == NULL) {
194 fprintf(stderr, "Failed to create ivi_client_surface\n");
195 abort();
196 }
197
198 ivi_surface_add_listener(window->ivi_surface,
199 &ivi_surface_listener, window);
200
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500201 } else {
202 assert(0);
203 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400204
205 return window;
206}
207
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200208static void
209destroy_window(struct window *window)
210{
211 if (window->callback)
212 wl_callback_destroy(window->callback);
213
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200214 if (window->buffers[0].buffer)
215 wl_buffer_destroy(window->buffers[0].buffer);
216 if (window->buffers[1].buffer)
217 wl_buffer_destroy(window->buffers[1].buffer);
218
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500219 if (window->xdg_surface)
220 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200221 wl_surface_destroy(window->surface);
222 free(window);
223}
224
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200225static struct buffer *
226window_next_buffer(struct window *window)
227{
228 struct buffer *buffer;
229 int ret = 0;
230
231 if (!window->buffers[0].busy)
232 buffer = &window->buffers[0];
233 else if (!window->buffers[1].busy)
234 buffer = &window->buffers[1];
235 else
236 return NULL;
237
238 if (!buffer->buffer) {
239 ret = create_shm_buffer(window->display, buffer,
240 window->width, window->height,
241 WL_SHM_FORMAT_XRGB8888);
242
243 if (ret < 0)
244 return NULL;
245
246 /* paint the padding */
247 memset(buffer->shm_data, 0xff,
248 window->width * window->height * 4);
249 }
250
251 return buffer;
252}
253
Pekka Paalanen313bd842012-04-26 15:14:50 +0300254static void
Rob Bradford371805f2012-10-02 18:03:15 +0100255paint_pixels(void *image, int padding, int width, int height, uint32_t time)
Pekka Paalanen313bd842012-04-26 15:14:50 +0300256{
Rob Bradford371805f2012-10-02 18:03:15 +0100257 const int halfh = padding + (height - padding * 2) / 2;
258 const int halfw = padding + (width - padding * 2) / 2;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300259 int ir, or;
260 uint32_t *pixel = image;
261 int y;
262
263 /* squared radii thresholds */
264 or = (halfw < halfh ? halfw : halfh) - 8;
265 ir = or - 32;
266 or *= or;
267 ir *= ir;
268
Rob Bradford371805f2012-10-02 18:03:15 +0100269 pixel += padding * width;
270 for (y = padding; y < height - padding; y++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300271 int x;
272 int y2 = (y - halfh) * (y - halfh);
273
Rob Bradford371805f2012-10-02 18:03:15 +0100274 pixel += padding;
275 for (x = padding; x < width - padding; x++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300276 uint32_t v;
277
278 /* squared distance from center */
279 int r2 = (x - halfw) * (x - halfw) + y2;
280
281 if (r2 < ir)
282 v = (r2 / 32 + time / 64) * 0x0080401;
283 else if (r2 < or)
284 v = (y + time / 32) * 0x0080401;
285 else
286 v = (x + time / 16) * 0x0080401;
287 v &= 0x00ffffff;
288
289 /* cross if compositor uses X from XRGB as alpha */
290 if (abs(x - y) > 6 && abs(x + y - height) > 6)
291 v |= 0xff000000;
292
293 *pixel++ = v;
294 }
Rob Bradford371805f2012-10-02 18:03:15 +0100295
296 pixel += padding;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300297 }
298}
299
Kristian Høgsberg33418202011-08-16 23:01:28 -0400300static const struct wl_callback_listener frame_listener;
301
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400302static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400303redraw(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400304{
305 struct window *window = data;
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200306 struct buffer *buffer;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400307
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200308 buffer = window_next_buffer(window);
309 if (!buffer) {
310 fprintf(stderr,
311 !callback ? "Failed to create the first buffer.\n" :
312 "Both buffers busy at redraw(). Server bug?\n");
313 abort();
314 }
315
316 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
317
Kristian Høgsberge7144fd2013-03-04 12:11:41 -0500318 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400319 wl_surface_damage(window->surface,
Rob Bradford371805f2012-10-02 18:03:15 +0100320 20, 20, window->width - 40, window->height - 40);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400321
Kristian Høgsberg33418202011-08-16 23:01:28 -0400322 if (callback)
323 wl_callback_destroy(callback);
324
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200325 window->callback = wl_surface_frame(window->surface);
326 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300327 wl_surface_commit(window->surface);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200328 buffer->busy = 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400329}
330
Kristian Høgsberg33418202011-08-16 23:01:28 -0400331static const struct wl_callback_listener frame_listener = {
332 redraw
333};
334
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400335static void
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500336shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
337{
338 struct display *d = data;
339
340 d->formats |= (1 << format);
341}
342
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100343struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500344 shm_format
345};
346
347static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800348xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
349{
350 xdg_shell_pong(shell, serial);
351}
352
353static const struct xdg_shell_listener xdg_shell_listener = {
354 xdg_shell_ping,
355};
356
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800357#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800358#ifdef static_assert
359static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
360 "Interface version doesn't match implementation version");
361#endif
362
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800363static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400364registry_handle_global(void *data, struct wl_registry *registry,
365 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400366{
367 struct display *d = data;
368
369 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400370 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400371 wl_registry_bind(registry,
372 id, &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800373 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400374 d->shell = wl_registry_bind(registry,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800375 id, &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800376 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800377 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800378 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500379 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800380 id, &zwp_fullscreen_shell_v1_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400381 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400382 d->shm = wl_registry_bind(registry,
383 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100384 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400385 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900386 else if (strcmp(interface, "ivi_application") == 0) {
387 d->ivi_application =
388 wl_registry_bind(registry, id,
389 &ivi_application_interface, 1);
390 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400391}
392
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200393static void
394registry_handle_global_remove(void *data, struct wl_registry *registry,
395 uint32_t name)
396{
397}
398
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400399static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200400 registry_handle_global,
401 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400402};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400403
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400404static struct display *
405create_display(void)
406{
407 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400408
409 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700410 if (display == NULL) {
411 fprintf(stderr, "out of memory\n");
412 exit(1);
413 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400414 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300415 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400416
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500417 display->formats = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400418 display->registry = wl_display_get_registry(display->display);
419 wl_registry_add_listener(display->registry,
420 &registry_listener, display);
421 wl_display_roundtrip(display->display);
422 if (display->shm == NULL) {
423 fprintf(stderr, "No wl_shm global\n");
424 exit(1);
425 }
426
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500427 wl_display_roundtrip(display->display);
428
Pekka Paalanen8b521182014-11-25 09:56:53 +0200429 /*
430 * Why do we need two roundtrips here?
431 *
432 * wl_display_get_registry() sends a request to the server, to which
433 * the server replies by emitting the wl_registry.global events.
434 * The first wl_display_roundtrip() sends wl_display.sync. The server
435 * first processes the wl_display.get_registry which includes sending
436 * the global events, and then processes the sync. Therefore when the
437 * sync (roundtrip) returns, we are guaranteed to have received and
438 * processed all the global events.
439 *
440 * While we are inside the first wl_display_roundtrip(), incoming
441 * events are dispatched, which causes registry_handle_global() to
442 * be called for each global. One of these globals is wl_shm.
443 * registry_handle_global() sends wl_registry.bind request for the
444 * wl_shm global. However, wl_registry.bind request is sent after
445 * the first wl_display.sync, so the reply to the sync comes before
446 * the initial events of the wl_shm object.
447 *
448 * The initial events that get sent as a reply to binding to wl_shm
449 * include wl_shm.format. These tell us which pixel formats are
450 * supported, and we need them before we can create buffers. They
451 * don't change at runtime, so we receive them as part of init.
452 *
453 * When the reply to the first sync comes, the server may or may not
454 * have sent the initial wl_shm events. Therefore we need the second
455 * wl_display_roundtrip() call here.
456 *
457 * The server processes the wl_registry.bind for wl_shm first, and
458 * the second wl_display.sync next. During our second call to
459 * wl_display_roundtrip() the initial wl_shm events are received and
460 * processed. Finally, when the reply to the second wl_display.sync
461 * arrives, it guarantees we have processed all wl_shm initial events.
462 *
463 * This sequence contains two examples on how wl_display_roundtrip()
464 * can be used to guarantee, that all reply events to a request
465 * have been received and processed. This is a general Wayland
466 * technique.
467 */
468
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500469 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500470 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
471 exit(1);
472 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400473
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400474 return display;
475}
476
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200477static void
478destroy_display(struct display *display)
479{
480 if (display->shm)
481 wl_shm_destroy(display->shm);
482
483 if (display->shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800484 xdg_shell_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200485
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500486 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800487 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500488
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200489 if (display->compositor)
490 wl_compositor_destroy(display->compositor);
491
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200492 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200493 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500494 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200495 free(display);
496}
497
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200498static void
499signal_int(int signum)
500{
501 running = 0;
502}
503
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400504int
505main(int argc, char **argv)
506{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200507 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400508 struct display *display;
509 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300510 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400511
512 display = create_display();
513 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300514 if (!window)
515 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400516
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200517 sigint.sa_handler = signal_int;
518 sigemptyset(&sigint.sa_mask);
519 sigint.sa_flags = SA_RESETHAND;
520 sigaction(SIGINT, &sigint, NULL);
521
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200522 /* Initialise damage to full surface, so the padding gets painted */
523 wl_surface_damage(window->surface, 0, 0,
524 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100525
Kristian Høgsberg33418202011-08-16 23:01:28 -0400526 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400527
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400528 while (running && ret != -1)
529 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400530
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200531 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900532
533 if (window->display->ivi_application) {
534 ivi_surface_destroy(window->ivi_surface);
535 ivi_application_destroy(window->display->ivi_application);
536 }
537
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200538 destroy_window(window);
539 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200540
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400541 return 0;
542}