blob: 9fa2e214c7746cb5f14a67955433f579e37e1f97 [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 Ådahle023cbc2016-08-11 23:29:38 +080040#include "xdg-shell-unstable-v6-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;
Jonas Ådahle023cbc2016-08-11 23:29:38 +080051 struct zxdg_shell_v6 *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;
Jonas Ådahle023cbc2016-08-11 23:29:38 +080068 struct zxdg_surface_v6 *xdg_surface;
69 struct zxdg_toplevel_v6 *xdg_toplevel;
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +090070 struct ivi_surface *ivi_surface;
Pekka Paalanen99b705b2012-11-19 15:29:09 +020071 struct buffer buffers[2];
72 struct buffer *prev_buffer;
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +020073 struct wl_callback *callback;
Jonas Ådahle023cbc2016-08-11 23:29:38 +080074 bool wait_for_configure;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040075};
76
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -050077static int running = 1;
78
Pekka Paalanen99b705b2012-11-19 15:29:09 +020079static void
Jonas Ådahle023cbc2016-08-11 23:29:38 +080080redraw(void *data, struct wl_callback *callback, uint32_t time);
81
82static void
Pekka Paalanen99b705b2012-11-19 15:29:09 +020083buffer_release(void *data, struct wl_buffer *buffer)
84{
85 struct buffer *mybuf = data;
86
87 mybuf->busy = 0;
88}
89
90static const struct wl_buffer_listener buffer_listener = {
91 buffer_release
92};
93
94static int
95create_shm_buffer(struct display *display, struct buffer *buffer,
96 int width, int height, uint32_t format)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040097{
Kristian Høgsberg16626282012-04-03 11:21:27 -040098 struct wl_shm_pool *pool;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -040099 int fd, size, stride;
100 void *data;
101
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400102 stride = width * 4;
103 size = stride * height;
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300104
105 fd = os_create_anonymous_file(size);
106 if (fd < 0) {
107 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
108 size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200109 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400110 }
111
112 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400113 if (data == MAP_FAILED) {
114 fprintf(stderr, "mmap failed: %m\n");
115 close(fd);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200116 return -1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400117 }
118
Kristian Høgsberg16626282012-04-03 11:21:27 -0400119 pool = wl_shm_create_pool(display->shm, fd, size);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200120 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
121 width, height,
122 stride, format);
123 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400124 wl_shm_pool_destroy(pool);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400125 close(fd);
126
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200127 buffer->shm_data = data;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400128
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200129 return 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400130}
131
Scott Moreau7c8b1162012-05-12 11:57:42 -0600132static void
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800133handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
134 uint32_t serial)
Scott Moreau7c8b1162012-05-12 11:57:42 -0600135{
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800136 struct window *window = data;
137
138 zxdg_surface_v6_ack_configure(surface, serial);
139
140 if (window->wait_for_configure) {
141 redraw(window, NULL, 0);
142 window->wait_for_configure = false;
143 }
144}
145
146static const struct zxdg_surface_v6_listener xdg_surface_listener = {
147 handle_xdg_surface_configure,
148};
149
150static void
151handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
152 int32_t width, int32_t height,
153 struct wl_array *state)
154{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800155}
156
157static void
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800158handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500159{
160 running = 0;
161}
162
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800163static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
164 handle_xdg_toplevel_configure,
165 handle_xdg_toplevel_close,
Scott Moreau7c8b1162012-05-12 11:57:42 -0600166};
167
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900168static void
169handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
170 int32_t width, int32_t height)
171{
172 /* Simple-shm is resizable */
173}
174
175static const struct ivi_surface_listener ivi_surface_listener = {
176 handle_ivi_surface_configure,
177};
178
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400179static struct window *
180create_window(struct display *display, int width, int height)
181{
182 struct window *window;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300183
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800184 window = zalloc(sizeof *window);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200185 if (!window)
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300186 return NULL;
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300187
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200188 window->callback = NULL;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400189 window->display = display;
190 window->width = width;
191 window->height = height;
192 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400193
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500194 if (display->shell) {
195 window->xdg_surface =
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800196 zxdg_shell_v6_get_xdg_surface(display->shell,
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500197 window->surface);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500198 assert(window->xdg_surface);
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800199 zxdg_surface_v6_add_listener(window->xdg_surface,
200 &xdg_surface_listener, window);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500201
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800202 window->xdg_toplevel =
203 zxdg_surface_v6_get_toplevel(window->xdg_surface);
204 assert(window->xdg_toplevel);
205 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
206 &xdg_toplevel_listener, window);
Scott Moreau7c8b1162012-05-12 11:57:42 -0600207
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800208 zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-shm");
209 wl_surface_commit(window->surface);
210 window->wait_for_configure = true;
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500211 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800212 zwp_fullscreen_shell_v1_present_surface(display->fshell,
213 window->surface,
214 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
215 NULL);
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900216 } else if (display->ivi_application ) {
217 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
218 window->ivi_surface =
219 ivi_application_surface_create(display->ivi_application,
220 id_ivisurf, window->surface);
221 if (window->ivi_surface == NULL) {
222 fprintf(stderr, "Failed to create ivi_client_surface\n");
223 abort();
224 }
225
226 ivi_surface_add_listener(window->ivi_surface,
227 &ivi_surface_listener, window);
228
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500229 } else {
230 assert(0);
231 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400232
233 return window;
234}
235
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200236static void
237destroy_window(struct window *window)
238{
239 if (window->callback)
240 wl_callback_destroy(window->callback);
241
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200242 if (window->buffers[0].buffer)
243 wl_buffer_destroy(window->buffers[0].buffer);
244 if (window->buffers[1].buffer)
245 wl_buffer_destroy(window->buffers[1].buffer);
246
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800247 if (window->xdg_toplevel)
248 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500249 if (window->xdg_surface)
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800250 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200251 wl_surface_destroy(window->surface);
252 free(window);
253}
254
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200255static struct buffer *
256window_next_buffer(struct window *window)
257{
258 struct buffer *buffer;
259 int ret = 0;
260
261 if (!window->buffers[0].busy)
262 buffer = &window->buffers[0];
263 else if (!window->buffers[1].busy)
264 buffer = &window->buffers[1];
265 else
266 return NULL;
267
268 if (!buffer->buffer) {
269 ret = create_shm_buffer(window->display, buffer,
270 window->width, window->height,
271 WL_SHM_FORMAT_XRGB8888);
272
273 if (ret < 0)
274 return NULL;
275
276 /* paint the padding */
277 memset(buffer->shm_data, 0xff,
278 window->width * window->height * 4);
279 }
280
281 return buffer;
282}
283
Pekka Paalanen313bd842012-04-26 15:14:50 +0300284static void
Rob Bradford371805f2012-10-02 18:03:15 +0100285paint_pixels(void *image, int padding, int width, int height, uint32_t time)
Pekka Paalanen313bd842012-04-26 15:14:50 +0300286{
Rob Bradford371805f2012-10-02 18:03:15 +0100287 const int halfh = padding + (height - padding * 2) / 2;
288 const int halfw = padding + (width - padding * 2) / 2;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300289 int ir, or;
290 uint32_t *pixel = image;
291 int y;
292
293 /* squared radii thresholds */
294 or = (halfw < halfh ? halfw : halfh) - 8;
295 ir = or - 32;
296 or *= or;
297 ir *= ir;
298
Rob Bradford371805f2012-10-02 18:03:15 +0100299 pixel += padding * width;
300 for (y = padding; y < height - padding; y++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300301 int x;
302 int y2 = (y - halfh) * (y - halfh);
303
Rob Bradford371805f2012-10-02 18:03:15 +0100304 pixel += padding;
305 for (x = padding; x < width - padding; x++) {
Pekka Paalanen313bd842012-04-26 15:14:50 +0300306 uint32_t v;
307
308 /* squared distance from center */
309 int r2 = (x - halfw) * (x - halfw) + y2;
310
311 if (r2 < ir)
312 v = (r2 / 32 + time / 64) * 0x0080401;
313 else if (r2 < or)
314 v = (y + time / 32) * 0x0080401;
315 else
316 v = (x + time / 16) * 0x0080401;
317 v &= 0x00ffffff;
318
319 /* cross if compositor uses X from XRGB as alpha */
320 if (abs(x - y) > 6 && abs(x + y - height) > 6)
321 v |= 0xff000000;
322
323 *pixel++ = v;
324 }
Rob Bradford371805f2012-10-02 18:03:15 +0100325
326 pixel += padding;
Pekka Paalanen313bd842012-04-26 15:14:50 +0300327 }
328}
329
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330static const struct wl_callback_listener frame_listener;
331
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400332static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400333redraw(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400334{
335 struct window *window = data;
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200336 struct buffer *buffer;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400337
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200338 buffer = window_next_buffer(window);
339 if (!buffer) {
340 fprintf(stderr,
341 !callback ? "Failed to create the first buffer.\n" :
342 "Both buffers busy at redraw(). Server bug?\n");
343 abort();
344 }
345
346 paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
347
Kristian Høgsberge7144fd2013-03-04 12:11:41 -0500348 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400349 wl_surface_damage(window->surface,
Rob Bradford371805f2012-10-02 18:03:15 +0100350 20, 20, window->width - 40, window->height - 40);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400351
Kristian Høgsberg33418202011-08-16 23:01:28 -0400352 if (callback)
353 wl_callback_destroy(callback);
354
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200355 window->callback = wl_surface_frame(window->surface);
356 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300357 wl_surface_commit(window->surface);
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200358 buffer->busy = 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400359}
360
Kristian Høgsberg33418202011-08-16 23:01:28 -0400361static const struct wl_callback_listener frame_listener = {
362 redraw
363};
364
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400365static void
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500366shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
367{
368 struct display *d = data;
369
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000370 if (format == WL_SHM_FORMAT_XRGB8888)
371 d->has_xrgb = true;
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500372}
373
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100374struct wl_shm_listener shm_listener = {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500375 shm_format
376};
377
378static void
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800379xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800380{
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800381 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800382}
383
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800384static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800385 xdg_shell_ping,
386};
387
388static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400389registry_handle_global(void *data, struct wl_registry *registry,
390 uint32_t id, const char *interface, uint32_t version)
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400391{
392 struct display *d = data;
393
394 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400395 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400396 wl_registry_bind(registry,
397 id, &wl_compositor_interface, 1);
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800398 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400399 d->shell = wl_registry_bind(registry,
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800400 id, &zxdg_shell_v6_interface, 1);
401 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800402 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500403 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800404 id, &zwp_fullscreen_shell_v1_interface, 1);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400405 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400406 d->shm = wl_registry_bind(registry,
407 id, &wl_shm_interface, 1);
Stefan Schmidt85c40f22013-08-05 13:50:50 +0100408 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400409 }
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900410 else if (strcmp(interface, "ivi_application") == 0) {
411 d->ivi_application =
412 wl_registry_bind(registry, id,
413 &ivi_application_interface, 1);
414 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400415}
416
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200417static void
418registry_handle_global_remove(void *data, struct wl_registry *registry,
419 uint32_t name)
420{
421}
422
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400423static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200424 registry_handle_global,
425 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400426};
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400427
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400428static struct display *
429create_display(void)
430{
431 struct display *display;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400432
433 display = malloc(sizeof *display);
Kristian Høgsberg96c619a2013-08-15 11:39:52 -0700434 if (display == NULL) {
435 fprintf(stderr, "out of memory\n");
436 exit(1);
437 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400438 display->display = wl_display_connect(NULL);
Tiago Vignatti79caa752011-07-21 16:35:38 +0300439 assert(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400440
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000441 display->has_xrgb = false;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400442 display->registry = wl_display_get_registry(display->display);
443 wl_registry_add_listener(display->registry,
444 &registry_listener, display);
445 wl_display_roundtrip(display->display);
446 if (display->shm == NULL) {
447 fprintf(stderr, "No wl_shm global\n");
448 exit(1);
449 }
450
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500451 wl_display_roundtrip(display->display);
452
Pekka Paalanen8b521182014-11-25 09:56:53 +0200453 /*
454 * Why do we need two roundtrips here?
455 *
456 * wl_display_get_registry() sends a request to the server, to which
457 * the server replies by emitting the wl_registry.global events.
458 * The first wl_display_roundtrip() sends wl_display.sync. The server
459 * first processes the wl_display.get_registry which includes sending
460 * the global events, and then processes the sync. Therefore when the
461 * sync (roundtrip) returns, we are guaranteed to have received and
462 * processed all the global events.
463 *
464 * While we are inside the first wl_display_roundtrip(), incoming
465 * events are dispatched, which causes registry_handle_global() to
466 * be called for each global. One of these globals is wl_shm.
467 * registry_handle_global() sends wl_registry.bind request for the
468 * wl_shm global. However, wl_registry.bind request is sent after
469 * the first wl_display.sync, so the reply to the sync comes before
470 * the initial events of the wl_shm object.
471 *
472 * The initial events that get sent as a reply to binding to wl_shm
473 * include wl_shm.format. These tell us which pixel formats are
474 * supported, and we need them before we can create buffers. They
475 * don't change at runtime, so we receive them as part of init.
476 *
477 * When the reply to the first sync comes, the server may or may not
478 * have sent the initial wl_shm events. Therefore we need the second
479 * wl_display_roundtrip() call here.
480 *
481 * The server processes the wl_registry.bind for wl_shm first, and
482 * the second wl_display.sync next. During our second call to
483 * wl_display_roundtrip() the initial wl_shm events are received and
484 * processed. Finally, when the reply to the second wl_display.sync
485 * arrives, it guarantees we have processed all wl_shm initial events.
486 *
487 * This sequence contains two examples on how wl_display_roundtrip()
488 * can be used to guarantee, that all reply events to a request
489 * have been received and processed. This is a general Wayland
490 * technique.
491 */
492
Murray Calavera1ddb8dd2016-03-15 21:41:14 +0000493 if (!display->has_xrgb) {
Kristian Høgsberga3cdf592011-11-17 10:27:17 -0500494 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
495 exit(1);
496 }
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400497
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400498 return display;
499}
500
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200501static void
502destroy_display(struct display *display)
503{
504 if (display->shm)
505 wl_shm_destroy(display->shm);
506
507 if (display->shell)
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800508 zxdg_shell_v6_destroy(display->shell);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200509
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500510 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800511 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand428c24e2014-04-02 19:53:48 -0500512
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200513 if (display->compositor)
514 wl_compositor_destroy(display->compositor);
515
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200516 wl_registry_destroy(display->registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200517 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500518 wl_display_disconnect(display->display);
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200519 free(display);
520}
521
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200522static void
523signal_int(int signum)
524{
525 running = 0;
526}
527
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400528int
529main(int argc, char **argv)
530{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200531 struct sigaction sigint;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400532 struct display *display;
533 struct window *window;
Ander Conselvan de Oliveirad0f24cf2012-10-17 13:49:08 +0300534 int ret = 0;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400535
536 display = create_display();
537 window = create_window(display, 250, 250);
Pekka Paalanen3baf9462012-04-18 13:23:09 +0300538 if (!window)
539 return 1;
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400540
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200541 sigint.sa_handler = signal_int;
542 sigemptyset(&sigint.sa_mask);
543 sigint.sa_flags = SA_RESETHAND;
544 sigaction(SIGINT, &sigint, NULL);
545
Pekka Paalanen99b705b2012-11-19 15:29:09 +0200546 /* Initialise damage to full surface, so the padding gets painted */
547 wl_surface_damage(window->surface, 0, 0,
548 window->width, window->height);
Rob Bradford371805f2012-10-02 18:03:15 +0100549
Jonas Ådahle023cbc2016-08-11 23:29:38 +0800550 if (!window->wait_for_configure)
551 redraw(window, NULL, 0);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400552
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400553 while (running && ret != -1)
554 ret = wl_display_dispatch(display->display);
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400555
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200556 fprintf(stderr, "simple-shm exiting\n");
Nobuhiko Tanibatafba4ea32014-11-27 13:24:29 +0900557
558 if (window->display->ivi_application) {
559 ivi_surface_destroy(window->ivi_surface);
560 ivi_application_destroy(window->display->ivi_application);
561 }
562
Pekka Paalanenc4cd62a2011-12-13 13:48:24 +0200563 destroy_window(window);
564 destroy_display(display);
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200565
Kristian Høgsberg97ba2e62011-07-06 11:58:45 -0400566 return 0;
567}