blob: f810bbee97a109f7ff6c5ca00db2662371349e9a [file] [log] [blame]
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -04001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070024#include <config.h>
25
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040030#include <assert.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040031#include <unistd.h>
32#include <sys/mman.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020033#include <signal.h>
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040034
35#include <wayland-client.h>
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030036#include "../shared/os-compatibility.h"
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080037#include "xdg-shell-client-protocol.h"
Jason Ekstrand428c24e2014-04-02 19:53:48 -050038#include "fullscreen-shell-client-protocol.h"
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040039
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090040#include <sys/types.h>
41#include "ivi-application-client-protocol.h"
42#define IVI_SURFACE_ID 9000
43
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040044struct display {
45 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040046 struct wl_registry *registry;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040047 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080048 struct xdg_shell *shell;
Jason Ekstrand428c24e2014-04-02 19:53:48 -050049 struct _wl_fullscreen_shell *fshell;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040050 struct wl_shm *shm;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -050051 uint32_t formats;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090052 struct ivi_application *ivi_application;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040053};
54
Pekka Paalanen99b705b2012-11-19 15:29:09 +020055struct buffer {
56 struct wl_buffer *buffer;
57 void *shm_data;
58 int busy;
59};
60
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040061struct window {
62 struct display *display;
63 int width, height;
64 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080065 struct xdg_surface *xdg_surface;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090066 struct ivi_surface *ivi_surface;
Pekka Paalanen99b705b2012-11-19 15:29:09 +020067 struct buffer buffers[2];
68 struct buffer *prev_buffer;
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +020069 struct wl_callback *callback;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040070};
71
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -050072static int running = 1;
73
Pekka Paalanen99b705b2012-11-19 15:29:09 +020074static void
75buffer_release(void *data, struct wl_buffer *buffer)
76{
77 struct buffer *mybuf = data;
78
79 mybuf->busy = 0;
80}
81
82static const struct wl_buffer_listener buffer_listener = {
83 buffer_release
84};
85
86static int
87create_shm_buffer(struct display *display, struct buffer *buffer,
88 int width, int height, uint32_t format)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040089{
Kristian Høgsberg16626282012-04-03 11:21:27 -040090 struct wl_shm_pool *pool;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040091 int fd, size, stride;
92 void *data;
93
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040094 stride = width * 4;
95 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +030096
97 fd = os_create_anonymous_file(size);
98 if (fd < 0) {
99 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
100 size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200101 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400102 }
103
104 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400105 if (data == MAP_FAILED) {
106 fprintf(stderr, "mmap failed: %m\n");
107 close(fd);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200108 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400109 }
110
Kristian Høgsberg16626282012-04-03 11:21:27 -0400111 pool = wl_shm_create_pool(display->shm, fd, size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200112 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
113 width, height,
114 stride, format);
115 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400116 wl_shm_pool_destroy(pool);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400117 close(fd);
118
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200119 buffer->shm_data = data;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400120
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200121 return 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400122}
123
Scott Moreau7c8b1162012-05-12 11:57:42 -0600124static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800125handle_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700126 int32_t width, int32_t height,
127 struct wl_array *states, uint32_t serial)
Scott Moreau7c8b1162012-05-12 11:57:42 -0600128{
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700129 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800130}
131
132static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500133handle_delete(void *data, struct xdg_surface *xdg_surface)
134{
135 running = 0;
136}
137
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800138static const struct xdg_surface_listener xdg_surface_listener = {
Scott Moreau7c8b1162012-05-12 11:57:42 -0600139 handle_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500140 handle_delete,
Scott Moreau7c8b1162012-05-12 11:57:42 -0600141};
142
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900143static void
144handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
145 int32_t width, int32_t height)
146{
147 /* Simple-shm is resizable */
148}
149
150static const struct ivi_surface_listener ivi_surface_listener = {
151 handle_ivi_surface_configure,
152};
153
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400154static struct window *
155create_window(struct display *display, int width, int height)
156{
157 struct window *window;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300158
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200159 window = calloc(1, sizeof *window);
160 if (!window)
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300161 return NULL;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300162
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200163 window->callback = NULL;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400164 window->display = display;
165 window->width = width;
166 window->height = height;
167 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400168
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500169 if (display->shell) {
170 window->xdg_surface =
171 xdg_shell_get_xdg_surface(display->shell,
172 window->surface);
173
174 assert(window->xdg_surface);
175
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800176 xdg_surface_add_listener(window->xdg_surface,
177 &xdg_surface_listener, window);
Scott Moreau7c8b1162012-05-12 11:57:42 -0600178
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500179 xdg_surface_set_title(window->xdg_surface, "simple-shm");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900180
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500181 } else if (display->fshell) {
182 _wl_fullscreen_shell_present_surface(display->fshell,
183 window->surface,
184 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,
185 NULL);
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900186 } else if (display->ivi_application ) {
187 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
188 window->ivi_surface =
189 ivi_application_surface_create(display->ivi_application,
190 id_ivisurf, window->surface);
191 if (window->ivi_surface == NULL) {
192 fprintf(stderr, "Failed to create ivi_client_surface\n");
193 abort();
194 }
195
196 ivi_surface_add_listener(window->ivi_surface,
197 &ivi_surface_listener, window);
198
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500199 } else {
200 assert(0);
201 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400202
203 return window;
204}
205
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200206static void
207destroy_window(struct window *window)
208{
209 if (window->callback)
210 wl_callback_destroy(window->callback);
211
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200212 if (window->buffers[0].buffer)
213 wl_buffer_destroy(window->buffers[0].buffer);
214 if (window->buffers[1].buffer)
215 wl_buffer_destroy(window->buffers[1].buffer);
216
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500217 if (window->xdg_surface)
218 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200219 wl_surface_destroy(window->surface);
220 free(window);
221}
222
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200223static struct buffer *
224window_next_buffer(struct window *window)
225{
226 struct buffer *buffer;
227 int ret = 0;
228
229 if (!window->buffers[0].busy)
230 buffer = &window->buffers[0];
231 else if (!window->buffers[1].busy)
232 buffer = &window->buffers[1];
233 else
234 return NULL;
235
236 if (!buffer->buffer) {
237 ret = create_shm_buffer(window->display, buffer,
238 window->width, window->height,
239 WL_SHM_FORMAT_XRGB8888);
240
241 if (ret < 0)
242 return NULL;
243
244 /* paint the padding */
245 memset(buffer->shm_data, 0xff,
246 window->width * window->height * 4);
247 }
248
249 return buffer;
250}
251
Pekka Paalanen313bd842012-04-26 15:14:50 +0300252static void
Rob Bradford371805f2012-10-02 18:03:15 +0100253paint_pixels(void *image, int padding, int width, int height, uint32_t time)
Pekka Paalanen313bd842012-04-26 15:14:50 +0300254{
Rob Bradford371805f2012-10-02 18:03:15 +0100255 const int halfh = padding + (height - padding * 2) / 2;
256 const int halfw = padding + (width - padding * 2) / 2;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300257 int ir, or;
258 uint32_t *pixel = image;
259 int y;
260
261 /* squared radii thresholds */
262 or = (halfw < halfh ? halfw : halfh) - 8;
263 ir = or - 32;
264 or *= or;
265 ir *= ir;
266
Rob Bradford371805f2012-10-02 18:03:15 +0100267 pixel += padding * width;
268 for (y = padding; y < height - padding; y++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300269 int x;
270 int y2 = (y - halfh) * (y - halfh);
271
Rob Bradford371805f2012-10-02 18:03:15 +0100272 pixel += padding;
273 for (x = padding; x < width - padding; x++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300274 uint32_t v;
275
276 /* squared distance from center */
277 int r2 = (x - halfw) * (x - halfw) + y2;
278
279 if (r2 < ir)
280 v = (r2 / 32 + time / 64) * 0x0080401;
281 else if (r2 < or)
282 v = (y + time / 32) * 0x0080401;
283 else
284 v = (x + time / 16) * 0x0080401;
285 v &= 0x00ffffff;
286
287 /* cross if compositor uses X from XRGB as alpha */
288 if (abs(x - y) > 6 && abs(x + y - height) > 6)
289 v |= 0xff000000;
290
291 *pixel++ = v;
292 }
Rob Bradford371805f2012-10-02 18:03:15 +0100293
294 pixel += padding;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300295 }
296}
297
Kristian Høgsberg33418202011-08-16 23:01:28 -0400298static const struct wl_callback_listener frame_listener;
299
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400300static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400301redraw(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400302{
303 struct window *window = data;
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200304 struct buffer *buffer;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400305
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200306 buffer = window_next_buffer(window);
307 if (!buffer) {
308 fprintf(stderr,
309 !callback ? "Failed to create the first buffer.\n" :
310 "Both buffers busy at redraw(). Server bug?\n");
311 abort();
312 }
313
314 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
315
Kristian Høgsberge7144fd2013-03-04 12:11:41 -0500316 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400317 wl_surface_damage(window->surface,
Rob Bradford371805f2012-10-02 18:03:15 +0100318 20, 20, window->width - 40, window->height - 40);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400319
Kristian Høgsberg33418202011-08-16 23:01:28 -0400320 if (callback)
321 wl_callback_destroy(callback);
322
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200323 window->callback = wl_surface_frame(window->surface);
324 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300325 wl_surface_commit(window->surface);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200326 buffer->busy = 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400327}
328
Kristian Høgsberg33418202011-08-16 23:01:28 -0400329static const struct wl_callback_listener frame_listener = {
330 redraw
331};
332
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400333static void
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500334shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
335{
336 struct display *d = data;
337
338 d->formats |= (1 << format);
339}
340
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100341struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500342 shm_format
343};
344
345static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800346xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
347{
348 xdg_shell_pong(shell, serial);
349}
350
351static const struct xdg_shell_listener xdg_shell_listener = {
352 xdg_shell_ping,
353};
354
Pekka Paalanen71182ae2014-08-21 17:47:20 +0300355#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800356#ifdef static_assert
357static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
358 "Interface version doesn't match implementation version");
359#endif
360
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800361static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400362registry_handle_global(void *data, struct wl_registry *registry,
363 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400364{
365 struct display *d = data;
366
367 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400368 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400369 wl_registry_bind(registry,
370 id, &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800371 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400372 d->shell = wl_registry_bind(registry,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800373 id, &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800374 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800375 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500376 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
377 d->fshell = wl_registry_bind(registry,
378 id, &_wl_fullscreen_shell_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400379 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400380 d->shm = wl_registry_bind(registry,
381 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100382 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400383 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900384 else if (strcmp(interface, "ivi_application") == 0) {
385 d->ivi_application =
386 wl_registry_bind(registry, id,
387 &ivi_application_interface, 1);
388 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400389}
390
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200391static void
392registry_handle_global_remove(void *data, struct wl_registry *registry,
393 uint32_t name)
394{
395}
396
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400397static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200398 registry_handle_global,
399 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400400};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400401
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400402static struct display *
403create_display(void)
404{
405 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400406
407 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700408 if (display == NULL) {
409 fprintf(stderr, "out of memory\n");
410 exit(1);
411 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400412 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300413 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400414
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500415 display->formats = 0;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400416 display->registry = wl_display_get_registry(display->display);
417 wl_registry_add_listener(display->registry,
418 &registry_listener, display);
419 wl_display_roundtrip(display->display);
420 if (display->shm == NULL) {
421 fprintf(stderr, "No wl_shm global\n");
422 exit(1);
423 }
424
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500425 wl_display_roundtrip(display->display);
426
Pekka Paalanen8b521182014-11-25 09:56:53 +0200427 /*
428 * Why do we need two roundtrips here?
429 *
430 * wl_display_get_registry() sends a request to the server, to which
431 * the server replies by emitting the wl_registry.global events.
432 * The first wl_display_roundtrip() sends wl_display.sync. The server
433 * first processes the wl_display.get_registry which includes sending
434 * the global events, and then processes the sync. Therefore when the
435 * sync (roundtrip) returns, we are guaranteed to have received and
436 * processed all the global events.
437 *
438 * While we are inside the first wl_display_roundtrip(), incoming
439 * events are dispatched, which causes registry_handle_global() to
440 * be called for each global. One of these globals is wl_shm.
441 * registry_handle_global() sends wl_registry.bind request for the
442 * wl_shm global. However, wl_registry.bind request is sent after
443 * the first wl_display.sync, so the reply to the sync comes before
444 * the initial events of the wl_shm object.
445 *
446 * The initial events that get sent as a reply to binding to wl_shm
447 * include wl_shm.format. These tell us which pixel formats are
448 * supported, and we need them before we can create buffers. They
449 * don't change at runtime, so we receive them as part of init.
450 *
451 * When the reply to the first sync comes, the server may or may not
452 * have sent the initial wl_shm events. Therefore we need the second
453 * wl_display_roundtrip() call here.
454 *
455 * The server processes the wl_registry.bind for wl_shm first, and
456 * the second wl_display.sync next. During our second call to
457 * wl_display_roundtrip() the initial wl_shm events are received and
458 * processed. Finally, when the reply to the second wl_display.sync
459 * arrives, it guarantees we have processed all wl_shm initial events.
460 *
461 * This sequence contains two examples on how wl_display_roundtrip()
462 * can be used to guarantee, that all reply events to a request
463 * have been received and processed. This is a general Wayland
464 * technique.
465 */
466
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500467 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500468 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
469 exit(1);
470 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400471
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400472 return display;
473}
474
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200475static void
476destroy_display(struct display *display)
477{
478 if (display->shm)
479 wl_shm_destroy(display->shm);
480
481 if (display->shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800482 xdg_shell_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200483
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500484 if (display->fshell)
485 _wl_fullscreen_shell_release(display->fshell);
486
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200487 if (display->compositor)
488 wl_compositor_destroy(display->compositor);
489
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200490 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200491 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500492 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200493 free(display);
494}
495
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200496static void
497signal_int(int signum)
498{
499 running = 0;
500}
501
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400502int
503main(int argc, char **argv)
504{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200505 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400506 struct display *display;
507 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300508 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400509
510 display = create_display();
511 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300512 if (!window)
513 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400514
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200515 sigint.sa_handler = signal_int;
516 sigemptyset(&sigint.sa_mask);
517 sigint.sa_flags = SA_RESETHAND;
518 sigaction(SIGINT, &sigint, NULL);
519
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200520 /* Initialise damage to full surface, so the padding gets painted */
521 wl_surface_damage(window->surface, 0, 0,
522 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100523
Kristian Høgsberg33418202011-08-16 23:01:28 -0400524 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400525
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400526 while (running && ret != -1)
527 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400528
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200529 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900530
531 if (window->display->ivi_application) {
532 ivi_surface_destroy(window->ivi_surface);
533 ivi_application_destroy(window->display->ivi_application);
534 }
535
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200536 destroy_window(window);
537 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200538
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400539 return 0;
540}