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 | |
Bryce Harrington | b4dae9b | 2016-06-15 18:13:07 -0700 | [diff] [blame] | 25 | #include "config.h" |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 26 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 27 | #include <stdint.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdbool.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 32 | #include <assert.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <sys/mman.h> |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 35 | #include <signal.h> |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 36 | |
| 37 | #include <wayland-client.h> |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 38 | #include "shared/os-compatibility.h" |
Bryce Harrington | 0d1a622 | 2016-02-11 16:42:49 -0800 | [diff] [blame] | 39 | #include "shared/zalloc.h" |
Jonas Ådahl | 2a22933 | 2015-11-17 16:00:32 +0800 | [diff] [blame] | 40 | #include "xdg-shell-unstable-v5-client-protocol.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 41 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 42 | |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include "ivi-application-client-protocol.h" |
| 45 | #define IVI_SURFACE_ID 9000 |
| 46 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 47 | struct display { |
| 48 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 49 | struct wl_registry *registry; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 50 | struct wl_compositor *compositor; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 51 | struct xdg_shell *shell; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 52 | struct zwp_fullscreen_shell_v1 *fshell; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 53 | struct wl_shm *shm; |
Murray Calavera | 1ddb8dd | 2016-03-15 21:41:14 +0000 | [diff] [blame] | 54 | bool has_xrgb; |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 55 | struct ivi_application *ivi_application; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 56 | }; |
| 57 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 58 | struct buffer { |
| 59 | struct wl_buffer *buffer; |
| 60 | void *shm_data; |
| 61 | int busy; |
| 62 | }; |
| 63 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 64 | struct window { |
| 65 | struct display *display; |
| 66 | int width, height; |
| 67 | struct wl_surface *surface; |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 68 | struct xdg_surface *xdg_surface; |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 69 | struct ivi_surface *ivi_surface; |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 70 | struct buffer buffers[2]; |
| 71 | struct buffer *prev_buffer; |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 72 | struct wl_callback *callback; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 73 | }; |
| 74 | |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 75 | static int running = 1; |
| 76 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 77 | static void |
| 78 | buffer_release(void *data, struct wl_buffer *buffer) |
| 79 | { |
| 80 | struct buffer *mybuf = data; |
| 81 | |
| 82 | mybuf->busy = 0; |
| 83 | } |
| 84 | |
| 85 | static const struct wl_buffer_listener buffer_listener = { |
| 86 | buffer_release |
| 87 | }; |
| 88 | |
| 89 | static int |
| 90 | create_shm_buffer(struct display *display, struct buffer *buffer, |
| 91 | int width, int height, uint32_t format) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 92 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 93 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 94 | int fd, size, stride; |
| 95 | void *data; |
| 96 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 97 | stride = width * 4; |
| 98 | size = stride * height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 99 | |
| 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 Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 104 | return -1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | 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] | 108 | if (data == MAP_FAILED) { |
| 109 | fprintf(stderr, "mmap failed: %m\n"); |
| 110 | close(fd); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 111 | return -1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 114 | pool = wl_shm_create_pool(display->shm, fd, size); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 115 | 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øgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 119 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 120 | close(fd); |
| 121 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 122 | buffer->shm_data = data; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 123 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 124 | return 0; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 127 | static void |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 128 | handle_configure(void *data, struct xdg_surface *surface, |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 129 | int32_t width, int32_t height, |
| 130 | struct wl_array *states, uint32_t serial) |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 131 | { |
Jasper St. Pierre | ab2c108 | 2014-04-10 10:41:46 -0700 | [diff] [blame] | 132 | xdg_surface_ack_configure(surface, serial); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 136 | handle_delete(void *data, struct xdg_surface *xdg_surface) |
| 137 | { |
| 138 | running = 0; |
| 139 | } |
| 140 | |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 141 | static const struct xdg_surface_listener xdg_surface_listener = { |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 142 | handle_configure, |
Jasper St. Pierre | a0d8a30 | 2014-02-08 18:31:10 -0500 | [diff] [blame] | 143 | handle_delete, |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 144 | }; |
| 145 | |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 146 | static void |
| 147 | handle_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 | |
| 153 | static const struct ivi_surface_listener ivi_surface_listener = { |
| 154 | handle_ivi_surface_configure, |
| 155 | }; |
| 156 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 157 | static struct window * |
| 158 | create_window(struct display *display, int width, int height) |
| 159 | { |
| 160 | struct window *window; |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 161 | |
Bryce Harrington | 0d1a622 | 2016-02-11 16:42:49 -0800 | [diff] [blame] | 162 | window = zalloc(sizeof *window); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 163 | if (!window) |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 164 | return NULL; |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 165 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 166 | window->callback = NULL; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 167 | window->display = display; |
| 168 | window->width = width; |
| 169 | window->height = height; |
| 170 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 171 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 172 | 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øgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 179 | xdg_surface_add_listener(window->xdg_surface, |
| 180 | &xdg_surface_listener, window); |
Scott Moreau | 7c8b116 | 2012-05-12 11:57:42 -0600 | [diff] [blame] | 181 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 182 | xdg_surface_set_title(window->xdg_surface, "simple-shm"); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 183 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 184 | } else if (display->fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 185 | zwp_fullscreen_shell_v1_present_surface(display->fshell, |
| 186 | window->surface, |
| 187 | ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT, |
| 188 | NULL); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 189 | } 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 Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 202 | } else { |
| 203 | assert(0); |
| 204 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 205 | |
| 206 | return window; |
| 207 | } |
| 208 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 209 | static void |
| 210 | destroy_window(struct window *window) |
| 211 | { |
| 212 | if (window->callback) |
| 213 | wl_callback_destroy(window->callback); |
| 214 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 215 | 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 Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 220 | if (window->xdg_surface) |
| 221 | xdg_surface_destroy(window->xdg_surface); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 222 | wl_surface_destroy(window->surface); |
| 223 | free(window); |
| 224 | } |
| 225 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 226 | static struct buffer * |
| 227 | window_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 Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 255 | static void |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 256 | 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] | 257 | { |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 258 | const int halfh = padding + (height - padding * 2) / 2; |
| 259 | const int halfw = padding + (width - padding * 2) / 2; |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 260 | 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 Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 270 | pixel += padding * width; |
| 271 | for (y = padding; y < height - padding; y++) { |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 272 | int x; |
| 273 | int y2 = (y - halfh) * (y - halfh); |
| 274 | |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 275 | pixel += padding; |
| 276 | for (x = padding; x < width - padding; x++) { |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 277 | 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 Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 296 | |
| 297 | pixel += padding; |
Pekka Paalanen | 313bd84 | 2012-04-26 15:14:50 +0300 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 301 | static const struct wl_callback_listener frame_listener; |
| 302 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 303 | static void |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 304 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 305 | { |
| 306 | struct window *window = data; |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 307 | struct buffer *buffer; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 308 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 309 | 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øgsberg | e7144fd | 2013-03-04 12:11:41 -0500 | [diff] [blame] | 319 | wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 320 | wl_surface_damage(window->surface, |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 321 | 20, 20, window->width - 40, window->height - 40); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 322 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 323 | if (callback) |
| 324 | wl_callback_destroy(callback); |
| 325 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 326 | window->callback = wl_surface_frame(window->surface); |
| 327 | wl_callback_add_listener(window->callback, &frame_listener, window); |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 328 | wl_surface_commit(window->surface); |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 329 | buffer->busy = 1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 332 | static const struct wl_callback_listener frame_listener = { |
| 333 | redraw |
| 334 | }; |
| 335 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 336 | static void |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 337 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 338 | { |
| 339 | struct display *d = data; |
| 340 | |
Murray Calavera | 1ddb8dd | 2016-03-15 21:41:14 +0000 | [diff] [blame] | 341 | if (format == WL_SHM_FORMAT_XRGB8888) |
| 342 | d->has_xrgb = true; |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 343 | } |
| 344 | |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 345 | struct wl_shm_listener shm_listener = { |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 346 | shm_format |
| 347 | }; |
| 348 | |
| 349 | static void |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 350 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 351 | { |
| 352 | xdg_shell_pong(shell, serial); |
| 353 | } |
| 354 | |
| 355 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 356 | xdg_shell_ping, |
| 357 | }; |
| 358 | |
Jasper St. Pierre | 5ba1e1d | 2015-02-13 14:02:02 +0800 | [diff] [blame] | 359 | #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] | 360 | #ifdef static_assert |
| 361 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 362 | "Interface version doesn't match implementation version"); |
| 363 | #endif |
| 364 | |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 365 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 366 | registry_handle_global(void *data, struct wl_registry *registry, |
| 367 | uint32_t id, const char *interface, uint32_t version) |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 368 | { |
| 369 | struct display *d = data; |
| 370 | |
| 371 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 372 | d->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 373 | wl_registry_bind(registry, |
| 374 | id, &wl_compositor_interface, 1); |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 375 | } else if (strcmp(interface, "xdg_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 376 | d->shell = wl_registry_bind(registry, |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 377 | id, &xdg_shell_interface, 1); |
Kristian Høgsberg | 239902b | 2014-02-11 13:50:08 -0800 | [diff] [blame] | 378 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
Kristian Høgsberg | 2bff94e | 2014-02-11 12:22:51 -0800 | [diff] [blame] | 379 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 380 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 381 | d->fshell = wl_registry_bind(registry, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 382 | id, &zwp_fullscreen_shell_v1_interface, 1); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 383 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 384 | d->shm = wl_registry_bind(registry, |
| 385 | id, &wl_shm_interface, 1); |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 386 | wl_shm_add_listener(d->shm, &shm_listener, d); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 387 | } |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 388 | 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øgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 395 | static void |
| 396 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 397 | uint32_t name) |
| 398 | { |
| 399 | } |
| 400 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 401 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 402 | registry_handle_global, |
| 403 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 404 | }; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 405 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 406 | static struct display * |
| 407 | create_display(void) |
| 408 | { |
| 409 | struct display *display; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 410 | |
| 411 | display = malloc(sizeof *display); |
Kristian Høgsberg | 96c619a | 2013-08-15 11:39:52 -0700 | [diff] [blame] | 412 | if (display == NULL) { |
| 413 | fprintf(stderr, "out of memory\n"); |
| 414 | exit(1); |
| 415 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 416 | display->display = wl_display_connect(NULL); |
Tiago Vignatti | 79caa75 | 2011-07-21 16:35:38 +0300 | [diff] [blame] | 417 | assert(display->display); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 418 | |
Murray Calavera | 1ddb8dd | 2016-03-15 21:41:14 +0000 | [diff] [blame] | 419 | display->has_xrgb = false; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 420 | display->registry = wl_display_get_registry(display->display); |
| 421 | wl_registry_add_listener(display->registry, |
| 422 | ®istry_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øgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 429 | wl_display_roundtrip(display->display); |
| 430 | |
Pekka Paalanen | 8b52118 | 2014-11-25 09:56:53 +0200 | [diff] [blame] | 431 | /* |
| 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 Calavera | 1ddb8dd | 2016-03-15 21:41:14 +0000 | [diff] [blame] | 471 | if (!display->has_xrgb) { |
Kristian Høgsberg | a3cdf59 | 2011-11-17 10:27:17 -0500 | [diff] [blame] | 472 | fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n"); |
| 473 | exit(1); |
| 474 | } |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 475 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 476 | return display; |
| 477 | } |
| 478 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 479 | static void |
| 480 | destroy_display(struct display *display) |
| 481 | { |
| 482 | if (display->shm) |
| 483 | wl_shm_destroy(display->shm); |
| 484 | |
| 485 | if (display->shell) |
Kristian Høgsberg | dfaf65b | 2014-02-07 17:01:57 -0800 | [diff] [blame] | 486 | xdg_shell_destroy(display->shell); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 487 | |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 488 | if (display->fshell) |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 489 | zwp_fullscreen_shell_v1_release(display->fshell); |
Jason Ekstrand | 428c24e | 2014-04-02 19:53:48 -0500 | [diff] [blame] | 490 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 491 | if (display->compositor) |
| 492 | wl_compositor_destroy(display->compositor); |
| 493 | |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 494 | wl_registry_destroy(display->registry); |
Pekka Paalanen | fb850c4 | 2011-12-15 10:07:52 +0200 | [diff] [blame] | 495 | wl_display_flush(display->display); |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 496 | wl_display_disconnect(display->display); |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 497 | free(display); |
| 498 | } |
| 499 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 500 | static void |
| 501 | signal_int(int signum) |
| 502 | { |
| 503 | running = 0; |
| 504 | } |
| 505 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 506 | int |
| 507 | main(int argc, char **argv) |
| 508 | { |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 509 | struct sigaction sigint; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 510 | struct display *display; |
| 511 | struct window *window; |
Ander Conselvan de Oliveira | d0f24cf | 2012-10-17 13:49:08 +0300 | [diff] [blame] | 512 | int ret = 0; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 513 | |
| 514 | display = create_display(); |
| 515 | window = create_window(display, 250, 250); |
Pekka Paalanen | 3baf946 | 2012-04-18 13:23:09 +0300 | [diff] [blame] | 516 | if (!window) |
| 517 | return 1; |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 518 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 519 | sigint.sa_handler = signal_int; |
| 520 | sigemptyset(&sigint.sa_mask); |
| 521 | sigint.sa_flags = SA_RESETHAND; |
| 522 | sigaction(SIGINT, &sigint, NULL); |
| 523 | |
Pekka Paalanen | 99b705b | 2012-11-19 15:29:09 +0200 | [diff] [blame] | 524 | /* Initialise damage to full surface, so the padding gets painted */ |
| 525 | wl_surface_damage(window->surface, 0, 0, |
| 526 | window->width, window->height); |
Rob Bradford | 371805f | 2012-10-02 18:03:15 +0100 | [diff] [blame] | 527 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 528 | redraw(window, NULL, 0); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 529 | |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 530 | while (running && ret != -1) |
| 531 | ret = wl_display_dispatch(display->display); |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 532 | |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 533 | fprintf(stderr, "simple-shm exiting\n"); |
Nobuhiko Tanibata | fba4ea3 | 2014-11-27 13:24:29 +0900 | [diff] [blame] | 534 | |
| 535 | if (window->display->ivi_application) { |
| 536 | ivi_surface_destroy(window->ivi_surface); |
| 537 | ivi_application_destroy(window->display->ivi_application); |
| 538 | } |
| 539 | |
Pekka Paalanen | c4cd62a | 2011-12-13 13:48:24 +0200 | [diff] [blame] | 540 | destroy_window(window); |
| 541 | destroy_display(display); |
Pekka Paalanen | 88e60fc | 2011-12-13 12:09:09 +0200 | [diff] [blame] | 542 | |
Kristian Høgsberg | 97ba2e6 | 2011-07-06 11:58:45 -0400 | [diff] [blame] | 543 | return 0; |
| 544 | } |