Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Benjamin Franzke |
| 3 | * Copyright © 2010 Intel Corporation |
| 4 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 5 | * 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øgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 12 | * 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øgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 25 | #include <config.h> |
| 26 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <stdbool.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 31 | #include <assert.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <sys/mman.h> |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 34 | #include <signal.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 35 | |
| 36 | #include <wayland-client.h> |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 37 | #include "shared/os-compatibility.h" |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 38 | #include "xdg-shell-client-protocol.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 39 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 40 | |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | #include "ivi-application-client-protocol.h" |
| 43 | #define IVI_SURFACE_ID 9000 |
| 44 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 45 | struct display { |
| 46 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 47 | struct wl_registry *registry; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 48 | struct wl_compositor *compositor; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 49 | struct xdg_shell *shell; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 50 | struct zwp_fullscreen_shell_v1 *fshell; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 51 | struct wl_shm *shm; |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 52 | uint32_t formats; |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 53 | struct ivi_application *ivi_application; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 54 | }; |
| 55 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 56 | struct buffer { |
| 57 | struct wl_buffer *buffer; |
| 58 | void *shm_data; |
| 59 | int busy; |
| 60 | }; |
| 61 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 62 | struct window { |
| 63 | struct display *display; |
| 64 | int width, height; |
| 65 | struct wl_surface *surface; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 66 | struct xdg_surface *xdg_surface; |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 67 | struct ivi_surface *ivi_surface; |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 68 | struct buffer buffers[2]; |
| 69 | struct buffer *prev_buffer; |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 70 | struct wl_callback *callback; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 73 | static int running = 1; |
| 74 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 75 | static void |
| 76 | buffer_release(void *data, struct wl_buffer *buffer) |
| 77 | { |
| 78 | struct buffer *mybuf = data; |
| 79 | |
| 80 | mybuf->busy = 0; |
| 81 | } |
| 82 | |
| 83 | static const struct wl_buffer_listener buffer_listener = { |
| 84 | buffer_release |
| 85 | }; |
| 86 | |
| 87 | static int |
| 88 | create_shm_buffer(struct display *display, struct buffer *buffer, |
| 89 | int width, int height, uint32_t format) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 90 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 91 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 92 | int fd, size, stride; |
| 93 | void *data; |
| 94 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 95 | stride = width * 4; |
| 96 | size = stride * height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 97 | |
| 98 | fd = os_create_anonymous_file(size); |
| 99 | if (fd < 0) { |
| 100 | fprintf(stderr, "creating a buffer file for %d B failed: %m\n", |
| 101 | size); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 102 | return -1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 106 | if (data == MAP_FAILED) { |
| 107 | fprintf(stderr, "mmap failed: %m\n"); |
| 108 | close(fd); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 109 | return -1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 112 | pool = wl_shm_create_pool(display->shm, fd, size); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 113 | buffer->buffer = wl_shm_pool_create_buffer(pool, 0, |
| 114 | width, height, |
| 115 | stride, format); |
| 116 | wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 117 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 118 | close(fd); |
| 119 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 120 | buffer->shm_data = data; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 121 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 122 | return 0; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 125 | static void |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 126 | handle_configure(void *data, struct xdg_surface *surface, |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 127 | int32_t width, int32_t height, |
| 128 | struct wl_array *states, uint32_t serial) |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 129 | { |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 130 | xdg_surface_ack_configure(surface, serial); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 134 | handle_delete(void *data, struct xdg_surface *xdg_surface) |
| 135 | { |
| 136 | running = 0; |
| 137 | } |
| 138 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 139 | static const struct xdg_surface_listener xdg_surface_listener = { |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 140 | handle_configure, |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 141 | handle_delete, |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 142 | }; |
| 143 | |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 144 | static void |
| 145 | handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface, |
| 146 | int32_t width, int32_t height) |
| 147 | { |
| 148 | /* Simple-shm is resizable */ |
| 149 | } |
| 150 | |
| 151 | static const struct ivi_surface_listener ivi_surface_listener = { |
| 152 | handle_ivi_surface_configure, |
| 153 | }; |
| 154 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 155 | static struct window * |
| 156 | create_window(struct display *display, int width, int height) |
| 157 | { |
| 158 | struct window *window; |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 159 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 160 | window = calloc(1, sizeof *window); |
| 161 | if (!window) |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 162 | return NULL; |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 163 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 164 | window->callback = NULL; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 165 | window->display = display; |
| 166 | window->width = width; |
| 167 | window->height = height; |
| 168 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 169 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 170 | if (display->shell) { |
| 171 | window->xdg_surface = |
| 172 | xdg_shell_get_xdg_surface(display->shell, |
| 173 | window->surface); |
| 174 | |
| 175 | assert(window->xdg_surface); |
| 176 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 177 | xdg_surface_add_listener(window->xdg_surface, |
| 178 | &xdg_surface_listener, window); |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 179 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 180 | xdg_surface_set_title(window->xdg_surface, "simple-shm"); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 181 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 182 | } else if (display->fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 183 | zwp_fullscreen_shell_v1_present_surface(display->fshell, |
| 184 | window->surface, |
| 185 | ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT, |
| 186 | NULL); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 187 | } else if (display->ivi_application ) { |
| 188 | uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid(); |
| 189 | window->ivi_surface = |
| 190 | ivi_application_surface_create(display->ivi_application, |
| 191 | id_ivisurf, window->surface); |
| 192 | if (window->ivi_surface == NULL) { |
| 193 | fprintf(stderr, "Failed to create ivi_client_surface\n"); |
| 194 | abort(); |
| 195 | } |
| 196 | |
| 197 | ivi_surface_add_listener(window->ivi_surface, |
| 198 | &ivi_surface_listener, window); |
| 199 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 200 | } else { |
| 201 | assert(0); |
| 202 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 203 | |
| 204 | return window; |
| 205 | } |
| 206 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 207 | static void |
| 208 | destroy_window(struct window *window) |
| 209 | { |
| 210 | if (window->callback) |
| 211 | wl_callback_destroy(window->callback); |
| 212 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 213 | if (window->buffers[0].buffer) |
| 214 | wl_buffer_destroy(window->buffers[0].buffer); |
| 215 | if (window->buffers[1].buffer) |
| 216 | wl_buffer_destroy(window->buffers[1].buffer); |
| 217 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 218 | if (window->xdg_surface) |
| 219 | xdg_surface_destroy(window->xdg_surface); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 220 | wl_surface_destroy(window->surface); |
| 221 | free(window); |
| 222 | } |
| 223 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 224 | static struct buffer * |
| 225 | window_next_buffer(struct window *window) |
| 226 | { |
| 227 | struct buffer *buffer; |
| 228 | int ret = 0; |
| 229 | |
| 230 | if (!window->buffers[0].busy) |
| 231 | buffer = &window->buffers[0]; |
| 232 | else if (!window->buffers[1].busy) |
| 233 | buffer = &window->buffers[1]; |
| 234 | else |
| 235 | return NULL; |
| 236 | |
| 237 | if (!buffer->buffer) { |
| 238 | ret = create_shm_buffer(window->display, buffer, |
| 239 | window->width, window->height, |
| 240 | WL_SHM_FORMAT_XRGB8888); |
| 241 | |
| 242 | if (ret < 0) |
| 243 | return NULL; |
| 244 | |
| 245 | /* paint the padding */ |
| 246 | memset(buffer->shm_data, 0xff, |
| 247 | window->width * window->height * 4); |
| 248 | } |
| 249 | |
| 250 | return buffer; |
| 251 | } |
| 252 | |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 253 | static void |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 254 | paint_pixels(void *image, int padding, int width, int height, uint32_t time) |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 255 | { |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 256 | const int halfh = padding + (height - padding * 2) / 2; |
| 257 | const int halfw = padding + (width - padding * 2) / 2; |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 258 | int ir, or; |
| 259 | uint32_t *pixel = image; |
| 260 | int y; |
| 261 | |
| 262 | /* squared radii thresholds */ |
| 263 | or = (halfw < halfh ? halfw : halfh) - 8; |
| 264 | ir = or - 32; |
| 265 | or *= or; |
| 266 | ir *= ir; |
| 267 | |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 268 | pixel += padding * width; |
| 269 | for (y = padding; y < height - padding; y++) { |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 270 | int x; |
| 271 | int y2 = (y - halfh) * (y - halfh); |
| 272 | |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 273 | pixel += padding; |
| 274 | for (x = padding; x < width - padding; x++) { |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 275 | uint32_t v; |
| 276 | |
| 277 | /* squared distance from center */ |
| 278 | int r2 = (x - halfw) * (x - halfw) + y2; |
| 279 | |
| 280 | if (r2 < ir) |
| 281 | v = (r2 / 32 + time / 64) * 0x0080401; |
| 282 | else if (r2 < or) |
| 283 | v = (y + time / 32) * 0x0080401; |
| 284 | else |
| 285 | v = (x + time / 16) * 0x0080401; |
| 286 | v &= 0x00ffffff; |
| 287 | |
| 288 | /* cross if compositor uses X from XRGB as alpha */ |
| 289 | if (abs(x - y) > 6 && abs(x + y - height) > 6) |
| 290 | v |= 0xff000000; |
| 291 | |
| 292 | *pixel++ = v; |
| 293 | } |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 294 | |
| 295 | pixel += padding; |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 299 | static const struct wl_callback_listener frame_listener; |
| 300 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 301 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 302 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 303 | { |
| 304 | struct window *window = data; |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 305 | struct buffer *buffer; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 306 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 307 | buffer = window_next_buffer(window); |
| 308 | if (!buffer) { |
| 309 | fprintf(stderr, |
| 310 | !callback ? "Failed to create the first buffer.\n" : |
| 311 | "Both buffers busy at redraw(). Server bug?\n"); |
| 312 | abort(); |
| 313 | } |
| 314 | |
| 315 | paint_pixels(buffer->shm_data, 20, window->width, window->height, time); |
| 316 | |
Kristian Høgsberg | e7144fd | 2013-03-04 12:11:41 -0500 | [diff] [blame] | 317 | wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 318 | wl_surface_damage(window->surface, |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 319 | 20, 20, window->width - 40, window->height - 40); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 320 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 321 | if (callback) |
| 322 | wl_callback_destroy(callback); |
| 323 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 324 | window->callback = wl_surface_frame(window->surface); |
| 325 | wl_callback_add_listener(window->callback, &frame_listener, window); |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 326 | wl_surface_commit(window->surface); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 327 | buffer->busy = 1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 330 | static const struct wl_callback_listener frame_listener = { |
| 331 | redraw |
| 332 | }; |
| 333 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 334 | static void |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 335 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 336 | { |
| 337 | struct display *d = data; |
| 338 | |
| 339 | d->formats |= (1 << format); |
| 340 | } |
| 341 | |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 342 | struct wl_shm_listener shm_listener = { |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 343 | shm_format |
| 344 | }; |
| 345 | |
| 346 | static void |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 347 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 348 | { |
| 349 | xdg_shell_pong(shell, serial); |
| 350 | } |
| 351 | |
| 352 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 353 | xdg_shell_ping, |
| 354 | }; |
| 355 | |
Jasper St. Pierre | 5ba1e1d | 2015-02-13 14:02:02 +0800 | [diff] [blame] | 356 | #define XDG_VERSION 5 /* The version of xdg-shell that we implement */ |
Kristian Høgsberg | 239902b | 2014-02-11 13:50:08 -0800 | [diff] [blame] | 357 | #ifdef static_assert |
| 358 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 359 | "Interface version doesn't match implementation version"); |
| 360 | #endif |
| 361 | |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 362 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 363 | registry_handle_global(void *data, struct wl_registry *registry, |
| 364 | uint32_t id, const char *interface, uint32_t version) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 365 | { |
| 366 | struct display *d = data; |
| 367 | |
| 368 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 369 | d->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 370 | wl_registry_bind(registry, |
| 371 | id, &wl_compositor_interface, 1); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 372 | } else if (strcmp(interface, "xdg_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 373 | d->shell = wl_registry_bind(registry, |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 374 | id, &xdg_shell_interface, 1); |
Kristian Høgsberg | 239902b | 2014-02-11 13:50:08 -0800 | [diff] [blame] | 375 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 376 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 377 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 378 | d->fshell = wl_registry_bind(registry, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 379 | id, &zwp_fullscreen_shell_v1_interface, 1); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 380 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 381 | d->shm = wl_registry_bind(registry, |
| 382 | id, &wl_shm_interface, 1); |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 383 | wl_shm_add_listener(d->shm, &shm_listener, d); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 384 | } |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 385 | else if (strcmp(interface, "ivi_application") == 0) { |
| 386 | d->ivi_application = |
| 387 | wl_registry_bind(registry, id, |
| 388 | &ivi_application_interface, 1); |
| 389 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 392 | static void |
| 393 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 394 | uint32_t name) |
| 395 | { |
| 396 | } |
| 397 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 398 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 399 | registry_handle_global, |
| 400 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 401 | }; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 402 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 403 | static struct display * |
| 404 | create_display(void) |
| 405 | { |
| 406 | struct display *display; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 407 | |
| 408 | display = malloc(sizeof *display); |
Kristian Høgsberg | 96c619a | 2013-08-15 11:39:52 -0700 | [diff] [blame] | 409 | if (display == NULL) { |
| 410 | fprintf(stderr, "out of memory\n"); |
| 411 | exit(1); |
| 412 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 413 | display->display = wl_display_connect(NULL); |
Tiago Vignatti | 79caa75 | 2011-07-21 16:35:38 +0300 | [diff] [blame] | 414 | assert(display->display); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 415 | |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 416 | display->formats = 0; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 417 | display->registry = wl_display_get_registry(display->display); |
| 418 | wl_registry_add_listener(display->registry, |
| 419 | ®istry_listener, display); |
| 420 | wl_display_roundtrip(display->display); |
| 421 | if (display->shm == NULL) { |
| 422 | fprintf(stderr, "No wl_shm global\n"); |
| 423 | exit(1); |
| 424 | } |
| 425 | |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 426 | wl_display_roundtrip(display->display); |
| 427 | |
Pekka Paalanen | 8b52118 | 2014-11-25 09:56:53 +0200 | [diff] [blame] | 428 | /* |
| 429 | * Why do we need two roundtrips here? |
| 430 | * |
| 431 | * wl_display_get_registry() sends a request to the server, to which |
| 432 | * the server replies by emitting the wl_registry.global events. |
| 433 | * The first wl_display_roundtrip() sends wl_display.sync. The server |
| 434 | * first processes the wl_display.get_registry which includes sending |
| 435 | * the global events, and then processes the sync. Therefore when the |
| 436 | * sync (roundtrip) returns, we are guaranteed to have received and |
| 437 | * processed all the global events. |
| 438 | * |
| 439 | * While we are inside the first wl_display_roundtrip(), incoming |
| 440 | * events are dispatched, which causes registry_handle_global() to |
| 441 | * be called for each global. One of these globals is wl_shm. |
| 442 | * registry_handle_global() sends wl_registry.bind request for the |
| 443 | * wl_shm global. However, wl_registry.bind request is sent after |
| 444 | * the first wl_display.sync, so the reply to the sync comes before |
| 445 | * the initial events of the wl_shm object. |
| 446 | * |
| 447 | * The initial events that get sent as a reply to binding to wl_shm |
| 448 | * include wl_shm.format. These tell us which pixel formats are |
| 449 | * supported, and we need them before we can create buffers. They |
| 450 | * don't change at runtime, so we receive them as part of init. |
| 451 | * |
| 452 | * When the reply to the first sync comes, the server may or may not |
| 453 | * have sent the initial wl_shm events. Therefore we need the second |
| 454 | * wl_display_roundtrip() call here. |
| 455 | * |
| 456 | * The server processes the wl_registry.bind for wl_shm first, and |
| 457 | * the second wl_display.sync next. During our second call to |
| 458 | * wl_display_roundtrip() the initial wl_shm events are received and |
| 459 | * processed. Finally, when the reply to the second wl_display.sync |
| 460 | * arrives, it guarantees we have processed all wl_shm initial events. |
| 461 | * |
| 462 | * This sequence contains two examples on how wl_display_roundtrip() |
| 463 | * can be used to guarantee, that all reply events to a request |
| 464 | * have been received and processed. This is a general Wayland |
| 465 | * technique. |
| 466 | */ |
| 467 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 468 | if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) { |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 469 | fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n"); |
| 470 | exit(1); |
| 471 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 472 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 473 | return display; |
| 474 | } |
| 475 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 476 | static void |
| 477 | destroy_display(struct display *display) |
| 478 | { |
| 479 | if (display->shm) |
| 480 | wl_shm_destroy(display->shm); |
| 481 | |
| 482 | if (display->shell) |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 483 | xdg_shell_destroy(display->shell); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 484 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 485 | if (display->fshell) |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame^] | 486 | zwp_fullscreen_shell_v1_release(display->fshell); |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 487 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 488 | if (display->compositor) |
| 489 | wl_compositor_destroy(display->compositor); |
| 490 | |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 491 | wl_registry_destroy(display->registry); |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 492 | wl_display_flush(display->display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 493 | wl_display_disconnect(display->display); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 494 | free(display); |
| 495 | } |
| 496 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 497 | static void |
| 498 | signal_int(int signum) |
| 499 | { |
| 500 | running = 0; |
| 501 | } |
| 502 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 503 | int |
| 504 | main(int argc, char **argv) |
| 505 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 506 | struct sigaction sigint; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 507 | struct display *display; |
| 508 | struct window *window; |
Ander Conselvan de Oliveira | d0f24cf | 2012-10-17 13:49:08 +0300 | [diff] [blame] | 509 | int ret = 0; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 510 | |
| 511 | display = create_display(); |
| 512 | window = create_window(display, 250, 250); |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 513 | if (!window) |
| 514 | return 1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 515 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 516 | sigint.sa_handler = signal_int; |
| 517 | sigemptyset(&sigint.sa_mask); |
| 518 | sigint.sa_flags = SA_RESETHAND; |
| 519 | sigaction(SIGINT, &sigint, NULL); |
| 520 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 521 | /* Initialise damage to full surface, so the padding gets painted */ |
| 522 | wl_surface_damage(window->surface, 0, 0, |
| 523 | window->width, window->height); |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 524 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 525 | redraw(window, NULL, 0); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 526 | |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 527 | while (running && ret != -1) |
| 528 | ret = wl_display_dispatch(display->display); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 529 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 530 | fprintf(stderr, "simple-shm exiting\n"); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 531 | |
| 532 | if (window->display->ivi_application) { |
| 533 | ivi_surface_destroy(window->ivi_surface); |
| 534 | ivi_application_destroy(window->display->ivi_application); |
| 535 | } |
| 536 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 537 | destroy_window(window); |
| 538 | destroy_display(display); |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 539 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 540 | return 0; |
| 541 | } |