blob: 1aa26cef36a45a5e50a3b7d39c0be10476af7c51 [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;
Murray Calavera1ddb8dd2016-03-15 21:41:14 +000053 bool has_xrgb;
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
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000340 if (format == WL_SHM_FORMAT_XRGB8888)
341 d->has_xrgb = true;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500342}
343
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100344struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500345 shm_format
346};
347
348static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800349xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
350{
351 xdg_shell_pong(shell, serial);
352}
353
354static const struct xdg_shell_listener xdg_shell_listener = {
355 xdg_shell_ping,
356};
357
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800358#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800359#ifdef static_assert
360static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
361 "Interface version doesn't match implementation version");
362#endif
363
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800364static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400365registry_handle_global(void *data, struct wl_registry *registry,
366 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400367{
368 struct display *d = data;
369
370 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400371 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400372 wl_registry_bind(registry,
373 id, &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800374 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400375 d->shell = wl_registry_bind(registry,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800376 id, &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800377 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800378 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800379 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500380 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800381 id, &zwp_fullscreen_shell_v1_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400382 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400383 d->shm = wl_registry_bind(registry,
384 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100385 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400386 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900387 else if (strcmp(interface, "ivi_application") == 0) {
388 d->ivi_application =
389 wl_registry_bind(registry, id,
390 &ivi_application_interface, 1);
391 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400392}
393
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200394static void
395registry_handle_global_remove(void *data, struct wl_registry *registry,
396 uint32_t name)
397{
398}
399
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400400static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200401 registry_handle_global,
402 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400403};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400404
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400405static struct display *
406create_display(void)
407{
408 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400409
410 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700411 if (display == NULL) {
412 fprintf(stderr, "out of memory\n");
413 exit(1);
414 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400415 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300416 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400417
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000418 display->has_xrgb = false;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400419 display->registry = wl_display_get_registry(display->display);
420 wl_registry_add_listener(display->registry,
421 &registry_listener, display);
422 wl_display_roundtrip(display->display);
423 if (display->shm == NULL) {
424 fprintf(stderr, "No wl_shm global\n");
425 exit(1);
426 }
427
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500428 wl_display_roundtrip(display->display);
429
Pekka Paalanen8b521182014-11-25 09:56:53 +0200430 /*
431 * Why do we need two roundtrips here?
432 *
433 * wl_display_get_registry() sends a request to the server, to which
434 * the server replies by emitting the wl_registry.global events.
435 * The first wl_display_roundtrip() sends wl_display.sync. The server
436 * first processes the wl_display.get_registry which includes sending
437 * the global events, and then processes the sync. Therefore when the
438 * sync (roundtrip) returns, we are guaranteed to have received and
439 * processed all the global events.
440 *
441 * While we are inside the first wl_display_roundtrip(), incoming
442 * events are dispatched, which causes registry_handle_global() to
443 * be called for each global. One of these globals is wl_shm.
444 * registry_handle_global() sends wl_registry.bind request for the
445 * wl_shm global. However, wl_registry.bind request is sent after
446 * the first wl_display.sync, so the reply to the sync comes before
447 * the initial events of the wl_shm object.
448 *
449 * The initial events that get sent as a reply to binding to wl_shm
450 * include wl_shm.format. These tell us which pixel formats are
451 * supported, and we need them before we can create buffers. They
452 * don't change at runtime, so we receive them as part of init.
453 *
454 * When the reply to the first sync comes, the server may or may not
455 * have sent the initial wl_shm events. Therefore we need the second
456 * wl_display_roundtrip() call here.
457 *
458 * The server processes the wl_registry.bind for wl_shm first, and
459 * the second wl_display.sync next. During our second call to
460 * wl_display_roundtrip() the initial wl_shm events are received and
461 * processed. Finally, when the reply to the second wl_display.sync
462 * arrives, it guarantees we have processed all wl_shm initial events.
463 *
464 * This sequence contains two examples on how wl_display_roundtrip()
465 * can be used to guarantee, that all reply events to a request
466 * have been received and processed. This is a general Wayland
467 * technique.
468 */
469
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000470 if (!display->has_xrgb) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500471 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
472 exit(1);
473 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400474
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400475 return display;
476}
477
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200478static void
479destroy_display(struct display *display)
480{
481 if (display->shm)
482 wl_shm_destroy(display->shm);
483
484 if (display->shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800485 xdg_shell_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200486
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500487 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800488 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500489
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200490 if (display->compositor)
491 wl_compositor_destroy(display->compositor);
492
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200493 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200494 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500495 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200496 free(display);
497}
498
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200499static void
500signal_int(int signum)
501{
502 running = 0;
503}
504
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400505int
506main(int argc, char **argv)
507{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200508 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400509 struct display *display;
510 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300511 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400512
513 display = create_display();
514 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300515 if (!window)
516 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400517
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200518 sigint.sa_handler = signal_int;
519 sigemptyset(&sigint.sa_mask);
520 sigint.sa_flags = SA_RESETHAND;
521 sigaction(SIGINT, &sigint, NULL);
522
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200523 /* Initialise damage to full surface, so the padding gets painted */
524 wl_surface_damage(window->surface, 0, 0,
525 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100526
Kristian Høgsberg33418202011-08-16 23:01:28 -0400527 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400528
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400529 while (running && ret != -1)
530 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400531
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200532 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900533
534 if (window->display->ivi_application) {
535 ivi_surface_destroy(window->ivi_surface);
536 ivi_application_destroy(window->display->ivi_application);
537 }
538
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200539 destroy_window(window);
540 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200541
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400542 return 0;
543}