blob: b1fc5f107bd6ca976032838ed86ab99200350ea0 [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
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070025#include "config.h"
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070026
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030027#include <stdint.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040032#include <assert.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040033#include <unistd.h>
34#include <sys/mman.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020035#include <signal.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040036
37#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070038#include "shared/os-compatibility.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080039#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080040#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080041#include "fullscreen-shell-unstable-v1-client-protocol.h"
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040042
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090043#include <sys/types.h>
44#include "ivi-application-client-protocol.h"
45#define IVI_SURFACE_ID 9000
46
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040047struct display {
48 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040049 struct wl_registry *registry;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040050 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080051 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080052 struct zwp_fullscreen_shell_v1 *fshell;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040053 struct wl_shm *shm;
Murray Calavera1ddb8dd2016-03-15 21:41:14 +000054 bool has_xrgb;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090055 struct ivi_application *ivi_application;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040056};
57
Pekka Paalanen99b705b2012-11-19 15:29:09 +020058struct buffer {
59 struct wl_buffer *buffer;
60 void *shm_data;
61 int busy;
62};
63
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040064struct window {
65 struct display *display;
66 int width, height;
67 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080068 struct xdg_surface *xdg_surface;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090069 struct ivi_surface *ivi_surface;
Pekka Paalanen99b705b2012-11-19 15:29:09 +020070 struct buffer buffers[2];
71 struct buffer *prev_buffer;
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +020072 struct wl_callback *callback;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040073};
74
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -050075static int running = 1;
76
Pekka Paalanen99b705b2012-11-19 15:29:09 +020077static void
78buffer_release(void *data, struct wl_buffer *buffer)
79{
80 struct buffer *mybuf = data;
81
82 mybuf->busy = 0;
83}
84
85static const struct wl_buffer_listener buffer_listener = {
86 buffer_release
87};
88
89static int
90create_shm_buffer(struct display *display, struct buffer *buffer,
91 int width, int height, uint32_t format)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040092{
Kristian Høgsberg16626282012-04-03 11:21:27 -040093 struct wl_shm_pool *pool;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040094 int fd, size, stride;
95 void *data;
96
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040097 stride = width * 4;
98 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030099
100 fd = os_create_anonymous_file(size);
101 if (fd < 0) {
102 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
103 size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200104 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400105 }
106
107 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400108 if (data == MAP_FAILED) {
109 fprintf(stderr, "mmap failed: %m\n");
110 close(fd);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200111 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400112 }
113
Kristian Høgsberg16626282012-04-03 11:21:27 -0400114 pool = wl_shm_create_pool(display->shm, fd, size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200115 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
116 width, height,
117 stride, format);
118 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400119 wl_shm_pool_destroy(pool);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400120 close(fd);
121
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200122 buffer->shm_data = data;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400123
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200124 return 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400125}
126
Scott Moreau7c8b1162012-05-12 11:57:42 -0600127static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800128handle_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700129 int32_t width, int32_t height,
130 struct wl_array *states, uint32_t serial)
Scott Moreau7c8b1162012-05-12 11:57:42 -0600131{
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700132 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800133}
134
135static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500136handle_delete(void *data, struct xdg_surface *xdg_surface)
137{
138 running = 0;
139}
140
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800141static const struct xdg_surface_listener xdg_surface_listener = {
Scott Moreau7c8b1162012-05-12 11:57:42 -0600142 handle_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500143 handle_delete,
Scott Moreau7c8b1162012-05-12 11:57:42 -0600144};
145
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900146static void
147handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
148 int32_t width, int32_t height)
149{
150 /* Simple-shm is resizable */
151}
152
153static const struct ivi_surface_listener ivi_surface_listener = {
154 handle_ivi_surface_configure,
155};
156
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400157static struct window *
158create_window(struct display *display, int width, int height)
159{
160 struct window *window;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300161
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800162 window = zalloc(sizeof *window);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200163 if (!window)
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300164 return NULL;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300165
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200166 window->callback = NULL;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400167 window->display = display;
168 window->width = width;
169 window->height = height;
170 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400171
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500172 if (display->shell) {
173 window->xdg_surface =
174 xdg_shell_get_xdg_surface(display->shell,
175 window->surface);
176
177 assert(window->xdg_surface);
178
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800179 xdg_surface_add_listener(window->xdg_surface,
180 &xdg_surface_listener, window);
Scott Moreau7c8b1162012-05-12 11:57:42 -0600181
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500182 xdg_surface_set_title(window->xdg_surface, "simple-shm");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900183
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500184 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800185 zwp_fullscreen_shell_v1_present_surface(display->fshell,
186 window->surface,
187 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
188 NULL);
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900189 } else if (display->ivi_application ) {
190 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
191 window->ivi_surface =
192 ivi_application_surface_create(display->ivi_application,
193 id_ivisurf, window->surface);
194 if (window->ivi_surface == NULL) {
195 fprintf(stderr, "Failed to create ivi_client_surface\n");
196 abort();
197 }
198
199 ivi_surface_add_listener(window->ivi_surface,
200 &ivi_surface_listener, window);
201
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500202 } else {
203 assert(0);
204 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400205
206 return window;
207}
208
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200209static void
210destroy_window(struct window *window)
211{
212 if (window->callback)
213 wl_callback_destroy(window->callback);
214
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200215 if (window->buffers[0].buffer)
216 wl_buffer_destroy(window->buffers[0].buffer);
217 if (window->buffers[1].buffer)
218 wl_buffer_destroy(window->buffers[1].buffer);
219
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500220 if (window->xdg_surface)
221 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200222 wl_surface_destroy(window->surface);
223 free(window);
224}
225
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200226static struct buffer *
227window_next_buffer(struct window *window)
228{
229 struct buffer *buffer;
230 int ret = 0;
231
232 if (!window->buffers[0].busy)
233 buffer = &window->buffers[0];
234 else if (!window->buffers[1].busy)
235 buffer = &window->buffers[1];
236 else
237 return NULL;
238
239 if (!buffer->buffer) {
240 ret = create_shm_buffer(window->display, buffer,
241 window->width, window->height,
242 WL_SHM_FORMAT_XRGB8888);
243
244 if (ret < 0)
245 return NULL;
246
247 /* paint the padding */
248 memset(buffer->shm_data, 0xff,
249 window->width * window->height * 4);
250 }
251
252 return buffer;
253}
254
Pekka Paalanen313bd842012-04-26 15:14:50 +0300255static void
Rob Bradford371805f2012-10-02 18:03:15 +0100256paint_pixels(void *image, int padding, int width, int height, uint32_t time)
Pekka Paalanen313bd842012-04-26 15:14:50 +0300257{
Rob Bradford371805f2012-10-02 18:03:15 +0100258 const int halfh = padding + (height - padding * 2) / 2;
259 const int halfw = padding + (width - padding * 2) / 2;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300260 int ir, or;
261 uint32_t *pixel = image;
262 int y;
263
264 /* squared radii thresholds */
265 or = (halfw < halfh ? halfw : halfh) - 8;
266 ir = or - 32;
267 or *= or;
268 ir *= ir;
269
Rob Bradford371805f2012-10-02 18:03:15 +0100270 pixel += padding * width;
271 for (y = padding; y < height - padding; y++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300272 int x;
273 int y2 = (y - halfh) * (y - halfh);
274
Rob Bradford371805f2012-10-02 18:03:15 +0100275 pixel += padding;
276 for (x = padding; x < width - padding; x++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300277 uint32_t v;
278
279 /* squared distance from center */
280 int r2 = (x - halfw) * (x - halfw) + y2;
281
282 if (r2 < ir)
283 v = (r2 / 32 + time / 64) * 0x0080401;
284 else if (r2 < or)
285 v = (y + time / 32) * 0x0080401;
286 else
287 v = (x + time / 16) * 0x0080401;
288 v &= 0x00ffffff;
289
290 /* cross if compositor uses X from XRGB as alpha */
291 if (abs(x - y) > 6 && abs(x + y - height) > 6)
292 v |= 0xff000000;
293
294 *pixel++ = v;
295 }
Rob Bradford371805f2012-10-02 18:03:15 +0100296
297 pixel += padding;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300298 }
299}
300
Kristian Høgsberg33418202011-08-16 23:01:28 -0400301static const struct wl_callback_listener frame_listener;
302
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400303static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400304redraw(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400305{
306 struct window *window = data;
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200307 struct buffer *buffer;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400308
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200309 buffer = window_next_buffer(window);
310 if (!buffer) {
311 fprintf(stderr,
312 !callback ? "Failed to create the first buffer.\n" :
313 "Both buffers busy at redraw(). Server bug?\n");
314 abort();
315 }
316
317 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
318
Kristian Høgsberge7144fd2013-03-04 12:11:41 -0500319 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400320 wl_surface_damage(window->surface,
Rob Bradford371805f2012-10-02 18:03:15 +0100321 20, 20, window->width - 40, window->height - 40);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400322
Kristian Høgsberg33418202011-08-16 23:01:28 -0400323 if (callback)
324 wl_callback_destroy(callback);
325
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200326 window->callback = wl_surface_frame(window->surface);
327 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300328 wl_surface_commit(window->surface);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200329 buffer->busy = 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400330}
331
Kristian Høgsberg33418202011-08-16 23:01:28 -0400332static const struct wl_callback_listener frame_listener = {
333 redraw
334};
335
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400336static void
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500337shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
338{
339 struct display *d = data;
340
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000341 if (format == WL_SHM_FORMAT_XRGB8888)
342 d->has_xrgb = true;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500343}
344
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100345struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500346 shm_format
347};
348
349static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800350xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
351{
352 xdg_shell_pong(shell, serial);
353}
354
355static const struct xdg_shell_listener xdg_shell_listener = {
356 xdg_shell_ping,
357};
358
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800359#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800360#ifdef static_assert
361static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
362 "Interface version doesn't match implementation version");
363#endif
364
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800365static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400366registry_handle_global(void *data, struct wl_registry *registry,
367 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400368{
369 struct display *d = data;
370
371 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400372 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400373 wl_registry_bind(registry,
374 id, &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800375 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400376 d->shell = wl_registry_bind(registry,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800377 id, &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800378 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800379 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800380 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500381 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800382 id, &zwp_fullscreen_shell_v1_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400383 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400384 d->shm = wl_registry_bind(registry,
385 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100386 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400387 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900388 else if (strcmp(interface, "ivi_application") == 0) {
389 d->ivi_application =
390 wl_registry_bind(registry, id,
391 &ivi_application_interface, 1);
392 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400393}
394
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200395static void
396registry_handle_global_remove(void *data, struct wl_registry *registry,
397 uint32_t name)
398{
399}
400
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400401static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200402 registry_handle_global,
403 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400404};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400405
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400406static struct display *
407create_display(void)
408{
409 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400410
411 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700412 if (display == NULL) {
413 fprintf(stderr, "out of memory\n");
414 exit(1);
415 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400416 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300417 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400418
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000419 display->has_xrgb = false;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400420 display->registry = wl_display_get_registry(display->display);
421 wl_registry_add_listener(display->registry,
422 &registry_listener, display);
423 wl_display_roundtrip(display->display);
424 if (display->shm == NULL) {
425 fprintf(stderr, "No wl_shm global\n");
426 exit(1);
427 }
428
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500429 wl_display_roundtrip(display->display);
430
Pekka Paalanen8b521182014-11-25 09:56:53 +0200431 /*
432 * Why do we need two roundtrips here?
433 *
434 * wl_display_get_registry() sends a request to the server, to which
435 * the server replies by emitting the wl_registry.global events.
436 * The first wl_display_roundtrip() sends wl_display.sync. The server
437 * first processes the wl_display.get_registry which includes sending
438 * the global events, and then processes the sync. Therefore when the
439 * sync (roundtrip) returns, we are guaranteed to have received and
440 * processed all the global events.
441 *
442 * While we are inside the first wl_display_roundtrip(), incoming
443 * events are dispatched, which causes registry_handle_global() to
444 * be called for each global. One of these globals is wl_shm.
445 * registry_handle_global() sends wl_registry.bind request for the
446 * wl_shm global. However, wl_registry.bind request is sent after
447 * the first wl_display.sync, so the reply to the sync comes before
448 * the initial events of the wl_shm object.
449 *
450 * The initial events that get sent as a reply to binding to wl_shm
451 * include wl_shm.format. These tell us which pixel formats are
452 * supported, and we need them before we can create buffers. They
453 * don't change at runtime, so we receive them as part of init.
454 *
455 * When the reply to the first sync comes, the server may or may not
456 * have sent the initial wl_shm events. Therefore we need the second
457 * wl_display_roundtrip() call here.
458 *
459 * The server processes the wl_registry.bind for wl_shm first, and
460 * the second wl_display.sync next. During our second call to
461 * wl_display_roundtrip() the initial wl_shm events are received and
462 * processed. Finally, when the reply to the second wl_display.sync
463 * arrives, it guarantees we have processed all wl_shm initial events.
464 *
465 * This sequence contains two examples on how wl_display_roundtrip()
466 * can be used to guarantee, that all reply events to a request
467 * have been received and processed. This is a general Wayland
468 * technique.
469 */
470
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000471 if (!display->has_xrgb) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500472 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
473 exit(1);
474 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400475
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400476 return display;
477}
478
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200479static void
480destroy_display(struct display *display)
481{
482 if (display->shm)
483 wl_shm_destroy(display->shm);
484
485 if (display->shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800486 xdg_shell_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200487
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500488 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800489 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500490
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200491 if (display->compositor)
492 wl_compositor_destroy(display->compositor);
493
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200494 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200495 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500496 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200497 free(display);
498}
499
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200500static void
501signal_int(int signum)
502{
503 running = 0;
504}
505
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400506int
507main(int argc, char **argv)
508{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200509 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400510 struct display *display;
511 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300512 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400513
514 display = create_display();
515 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300516 if (!window)
517 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400518
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200519 sigint.sa_handler = signal_int;
520 sigemptyset(&sigint.sa_mask);
521 sigint.sa_flags = SA_RESETHAND;
522 sigaction(SIGINT, &sigint, NULL);
523
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200524 /* Initialise damage to full surface, so the padding gets painted */
525 wl_surface_damage(window->surface, 0, 0,
526 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100527
Kristian Høgsberg33418202011-08-16 23:01:28 -0400528 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400529
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400530 while (running && ret != -1)
531 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400532
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200533 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900534
535 if (window->display->ivi_application) {
536 ivi_surface_destroy(window->ivi_surface);
537 ivi_application_destroy(window->display->ivi_application);
538 }
539
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200540 destroy_window(window);
541 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200542
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400543 return 0;
544}