George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Benjamin Franzke |
| 3 | * Copyright © 2010 Intel Corporation |
| 4 | * Copyright © 2014 Collabora Ltd. |
| 5 | * |
| 6 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 7 | * documentation for any purpose is hereby granted without fee, provided that |
| 8 | * the above copyright notice appear in all copies and that both that copyright |
| 9 | * notice and this permission notice appear in supporting documentation, and |
| 10 | * that the name of the copyright holders not be used in advertising or |
| 11 | * publicity pertaining to distribution of the software without specific, |
| 12 | * written prior permission. The copyright holders make no representations |
| 13 | * about the suitability of this software for any purpose. It is provided "as |
| 14 | * is" without express or implied warranty. |
| 15 | * |
| 16 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 | * OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
Bryce Harrington | b4dae9b | 2016-06-15 18:13:07 -0700 | [diff] [blame] | 25 | #include "config.h" |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 26 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 27 | #include <stdint.h> |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <assert.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/mman.h> |
| 35 | #include <signal.h> |
| 36 | #include <fcntl.h> |
| 37 | |
| 38 | #include <xf86drm.h> |
Emmanuel Gil Peyrot | 8ef2957 | 2016-01-11 19:04:37 +0000 | [diff] [blame] | 39 | #include <i915_drm.h> |
| 40 | #include <intel_bufmgr.h> |
| 41 | #include <drm_fourcc.h> |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 42 | |
| 43 | #include <wayland-client.h> |
Bryce Harrington | 0d1a622 | 2016-02-11 16:42:49 -0800 | [diff] [blame] | 44 | #include "shared/zalloc.h" |
Jonas Ådahl | 2a22933 | 2015-11-17 16:00:32 +0800 | [diff] [blame] | 45 | #include "xdg-shell-unstable-v5-client-protocol.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 46 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 47 | #include "linux-dmabuf-unstable-v1-client-protocol.h" |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 48 | |
| 49 | struct display { |
| 50 | struct wl_display *display; |
| 51 | struct wl_registry *registry; |
| 52 | struct wl_compositor *compositor; |
| 53 | struct xdg_shell *shell; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 54 | struct zwp_fullscreen_shell_v1 *fshell; |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 55 | struct zwp_linux_dmabuf_v1 *dmabuf; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 56 | int xrgb8888_format_found; |
| 57 | }; |
| 58 | |
| 59 | struct buffer { |
| 60 | struct wl_buffer *buffer; |
| 61 | int busy; |
| 62 | |
| 63 | int drm_fd; |
| 64 | |
| 65 | drm_intel_bufmgr *bufmgr; |
| 66 | drm_intel_bo *bo; |
| 67 | |
| 68 | uint32_t gem_handle; |
| 69 | int dmabuf_fd; |
| 70 | uint8_t *mmap; |
| 71 | |
| 72 | int width; |
| 73 | int height; |
| 74 | int bpp; |
| 75 | unsigned long stride; |
| 76 | }; |
| 77 | |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 78 | #define NUM_BUFFERS 3 |
| 79 | |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 80 | struct window { |
| 81 | struct display *display; |
| 82 | int width, height; |
| 83 | struct wl_surface *surface; |
| 84 | struct xdg_surface *xdg_surface; |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 85 | struct buffer buffers[NUM_BUFFERS]; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 86 | struct buffer *prev_buffer; |
| 87 | struct wl_callback *callback; |
| 88 | }; |
| 89 | |
| 90 | static int running = 1; |
| 91 | |
| 92 | static void |
| 93 | buffer_release(void *data, struct wl_buffer *buffer) |
| 94 | { |
| 95 | struct buffer *mybuf = data; |
| 96 | |
| 97 | mybuf->busy = 0; |
| 98 | } |
| 99 | |
| 100 | static const struct wl_buffer_listener buffer_listener = { |
| 101 | buffer_release |
| 102 | }; |
| 103 | |
| 104 | static int |
| 105 | drm_connect(struct buffer *my_buf) |
| 106 | { |
| 107 | /* This won't work with card0 as we need to be authenticated; instead, |
| 108 | * boot with drm.rnodes=1 and use that. */ |
| 109 | my_buf->drm_fd = open("/dev/dri/renderD128", O_RDWR); |
| 110 | if (my_buf->drm_fd < 0) |
| 111 | return 0; |
| 112 | |
| 113 | my_buf->bufmgr = drm_intel_bufmgr_gem_init(my_buf->drm_fd, 32); |
| 114 | if (!my_buf->bufmgr) |
| 115 | return 0; |
| 116 | |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | drm_shutdown(struct buffer *my_buf) |
| 122 | { |
| 123 | drm_intel_bufmgr_destroy(my_buf->bufmgr); |
| 124 | close(my_buf->drm_fd); |
| 125 | } |
| 126 | |
| 127 | static int |
| 128 | alloc_bo(struct buffer *my_buf) |
| 129 | { |
| 130 | /* XXX: try different tiling modes for testing FB modifiers. */ |
| 131 | uint32_t tiling = I915_TILING_NONE; |
| 132 | |
| 133 | assert(my_buf->bufmgr); |
| 134 | |
| 135 | my_buf->bo = drm_intel_bo_alloc_tiled(my_buf->bufmgr, "test", |
| 136 | my_buf->width, my_buf->height, |
| 137 | (my_buf->bpp / 8), &tiling, |
| 138 | &my_buf->stride, 0); |
| 139 | |
| 140 | printf("buffer allocated w %d, h %d, stride %lu, size %lu\n", |
| 141 | my_buf->width, my_buf->height, my_buf->stride, my_buf->bo->size); |
| 142 | |
| 143 | if (!my_buf->bo) |
| 144 | return 0; |
| 145 | |
| 146 | if (tiling != I915_TILING_NONE) |
| 147 | return 0; |
| 148 | |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | free_bo(struct buffer *my_buf) |
| 154 | { |
| 155 | drm_intel_bo_unreference(my_buf->bo); |
| 156 | } |
| 157 | |
| 158 | static int |
| 159 | map_bo(struct buffer *my_buf) |
| 160 | { |
| 161 | if (drm_intel_gem_bo_map_gtt(my_buf->bo) != 0) |
| 162 | return 0; |
| 163 | |
| 164 | my_buf->mmap = my_buf->bo->virtual; |
| 165 | |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | fill_content(struct buffer *my_buf) |
| 171 | { |
| 172 | int x = 0, y = 0; |
| 173 | uint32_t *pix; |
| 174 | |
| 175 | assert(my_buf->mmap); |
| 176 | |
| 177 | for (y = 0; y < my_buf->height; y++) { |
| 178 | pix = (uint32_t *)(my_buf->mmap + y * my_buf->stride); |
| 179 | for (x = 0; x < my_buf->width; x++) { |
| 180 | *pix++ = (0xff << 24) | ((x % 256) << 16) | |
| 181 | ((y % 256) << 8) | 0xf0; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | unmap_bo(struct buffer *my_buf) |
| 188 | { |
| 189 | drm_intel_gem_bo_unmap_gtt(my_buf->bo); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | create_succeeded(void *data, |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 194 | struct zwp_linux_buffer_params_v1 *params, |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 195 | struct wl_buffer *new_buffer) |
| 196 | { |
| 197 | struct buffer *buffer = data; |
| 198 | |
| 199 | buffer->buffer = new_buffer; |
| 200 | wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer); |
| 201 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 202 | zwp_linux_buffer_params_v1_destroy(params); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 206 | create_failed(void *data, struct zwp_linux_buffer_params_v1 *params) |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 207 | { |
| 208 | struct buffer *buffer = data; |
| 209 | |
| 210 | buffer->buffer = NULL; |
Emmanuel Gil Peyrot | 8ef2957 | 2016-01-11 19:04:37 +0000 | [diff] [blame] | 211 | running = 0; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 212 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 213 | zwp_linux_buffer_params_v1_destroy(params); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 214 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 215 | fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n"); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 218 | static const struct zwp_linux_buffer_params_v1_listener params_listener = { |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 219 | create_succeeded, |
| 220 | create_failed |
| 221 | }; |
| 222 | |
| 223 | static int |
| 224 | create_dmabuf_buffer(struct display *display, struct buffer *buffer, |
| 225 | int width, int height) |
| 226 | { |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 227 | struct zwp_linux_buffer_params_v1 *params; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 228 | uint64_t modifier; |
| 229 | uint32_t flags; |
| 230 | |
| 231 | if (!drm_connect(buffer)) { |
| 232 | fprintf(stderr, "drm_connect failed\n"); |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | buffer->width = width; |
| 237 | buffer->height = height; |
| 238 | buffer->bpp = 32; /* hardcoded XRGB8888 format */ |
| 239 | |
| 240 | if (!alloc_bo(buffer)) { |
| 241 | fprintf(stderr, "alloc_bo failed\n"); |
| 242 | goto error1; |
| 243 | } |
| 244 | |
| 245 | if (!map_bo(buffer)) { |
| 246 | fprintf(stderr, "map_bo failed\n"); |
| 247 | goto error2; |
| 248 | } |
| 249 | fill_content(buffer); |
| 250 | unmap_bo(buffer); |
| 251 | |
| 252 | if (drm_intel_bo_gem_export_to_prime(buffer->bo, &buffer->dmabuf_fd) != 0) { |
| 253 | fprintf(stderr, "drm_intel_bo_gem_export_to_prime failed\n"); |
| 254 | goto error2; |
| 255 | } |
| 256 | if (buffer->dmabuf_fd < 0) { |
| 257 | fprintf(stderr, "error: dmabuf_fd < 0\n"); |
| 258 | goto error2; |
| 259 | } |
| 260 | |
| 261 | /* We now have a dmabuf! It should contain 2x2 tiles (i.e. each tile |
| 262 | * is 256x256) of misc colours, and be mappable, either as ARGB8888, or |
| 263 | * XRGB8888. */ |
| 264 | modifier = 0; |
| 265 | flags = 0; |
| 266 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 267 | params = zwp_linux_dmabuf_v1_create_params(display->dmabuf); |
| 268 | zwp_linux_buffer_params_v1_add(params, |
| 269 | buffer->dmabuf_fd, |
| 270 | 0, /* plane_idx */ |
| 271 | 0, /* offset */ |
| 272 | buffer->stride, |
| 273 | modifier >> 32, |
| 274 | modifier & 0xffffffff); |
| 275 | zwp_linux_buffer_params_v1_add_listener(params, ¶ms_listener, buffer); |
| 276 | zwp_linux_buffer_params_v1_create(params, |
| 277 | buffer->width, |
| 278 | buffer->height, |
| 279 | DRM_FORMAT_XRGB8888, |
| 280 | flags); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 281 | |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | |
| 284 | error2: |
| 285 | free_bo(buffer); |
| 286 | error1: |
| 287 | drm_shutdown(buffer); |
| 288 | error: |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | handle_configure(void *data, struct xdg_surface *surface, |
| 294 | int32_t width, int32_t height, |
| 295 | struct wl_array *states, uint32_t serial) |
| 296 | { |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | handle_delete(void *data, struct xdg_surface *xdg_surface) |
| 301 | { |
| 302 | running = 0; |
| 303 | } |
| 304 | |
| 305 | static const struct xdg_surface_listener xdg_surface_listener = { |
| 306 | handle_configure, |
| 307 | handle_delete, |
| 308 | }; |
| 309 | |
| 310 | static struct window * |
| 311 | create_window(struct display *display, int width, int height) |
| 312 | { |
| 313 | struct window *window; |
Emmanuel Gil Peyrot | 8ef2957 | 2016-01-11 19:04:37 +0000 | [diff] [blame] | 314 | int i; |
| 315 | int ret; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 316 | |
Bryce Harrington | 0d1a622 | 2016-02-11 16:42:49 -0800 | [diff] [blame] | 317 | window = zalloc(sizeof *window); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 318 | if (!window) |
| 319 | return NULL; |
| 320 | |
| 321 | window->callback = NULL; |
| 322 | window->display = display; |
| 323 | window->width = width; |
| 324 | window->height = height; |
| 325 | window->surface = wl_compositor_create_surface(display->compositor); |
| 326 | |
| 327 | if (display->shell) { |
| 328 | window->xdg_surface = |
| 329 | xdg_shell_get_xdg_surface(display->shell, |
| 330 | window->surface); |
| 331 | |
| 332 | assert(window->xdg_surface); |
| 333 | |
| 334 | xdg_surface_add_listener(window->xdg_surface, |
| 335 | &xdg_surface_listener, window); |
| 336 | |
| 337 | xdg_surface_set_title(window->xdg_surface, "simple-dmabuf"); |
| 338 | } else if (display->fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 339 | zwp_fullscreen_shell_v1_present_surface(display->fshell, |
| 340 | window->surface, |
| 341 | ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT, |
| 342 | NULL); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 343 | } else { |
| 344 | assert(0); |
| 345 | } |
| 346 | |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 347 | for (i = 0; i < NUM_BUFFERS; ++i) { |
Emmanuel Gil Peyrot | 8ef2957 | 2016-01-11 19:04:37 +0000 | [diff] [blame] | 348 | ret = create_dmabuf_buffer(display, &window->buffers[i], |
| 349 | width, height); |
| 350 | |
| 351 | if (ret < 0) |
| 352 | return NULL; |
| 353 | } |
| 354 | |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 355 | return window; |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | destroy_window(struct window *window) |
| 360 | { |
| 361 | int i; |
| 362 | |
| 363 | if (window->callback) |
| 364 | wl_callback_destroy(window->callback); |
| 365 | |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 366 | for (i = 0; i < NUM_BUFFERS; i++) { |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 367 | if (!window->buffers[i].buffer) |
| 368 | continue; |
| 369 | |
| 370 | wl_buffer_destroy(window->buffers[i].buffer); |
| 371 | free_bo(&window->buffers[i]); |
| 372 | close(window->buffers[i].dmabuf_fd); |
| 373 | drm_shutdown(&window->buffers[i]); |
| 374 | } |
| 375 | |
| 376 | if (window->xdg_surface) |
| 377 | xdg_surface_destroy(window->xdg_surface); |
| 378 | wl_surface_destroy(window->surface); |
| 379 | free(window); |
| 380 | } |
| 381 | |
| 382 | static struct buffer * |
| 383 | window_next_buffer(struct window *window) |
| 384 | { |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 385 | int i; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 386 | |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 387 | for (i = 0; i < NUM_BUFFERS; i++) |
| 388 | if (!window->buffers[i].busy) |
| 389 | return &window->buffers[i]; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 390 | |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 391 | return NULL; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static const struct wl_callback_listener frame_listener; |
| 395 | |
| 396 | static void |
| 397 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
| 398 | { |
| 399 | struct window *window = data; |
| 400 | struct buffer *buffer; |
| 401 | |
| 402 | buffer = window_next_buffer(window); |
| 403 | if (!buffer) { |
| 404 | fprintf(stderr, |
| 405 | !callback ? "Failed to create the first buffer.\n" : |
Pekka Paalanen | d56b94a | 2016-06-10 13:13:01 +0300 | [diff] [blame] | 406 | "All buffers busy at redraw(). Server bug?\n"); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 407 | abort(); |
| 408 | } |
| 409 | |
| 410 | /* XXX: would be nice to draw something that changes here... */ |
| 411 | |
| 412 | wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
| 413 | wl_surface_damage(window->surface, 0, 0, window->width, window->height); |
| 414 | |
| 415 | if (callback) |
| 416 | wl_callback_destroy(callback); |
| 417 | |
| 418 | window->callback = wl_surface_frame(window->surface); |
| 419 | wl_callback_add_listener(window->callback, &frame_listener, window); |
| 420 | wl_surface_commit(window->surface); |
| 421 | buffer->busy = 1; |
| 422 | } |
| 423 | |
| 424 | static const struct wl_callback_listener frame_listener = { |
| 425 | redraw |
| 426 | }; |
| 427 | |
| 428 | static void |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 429 | dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, uint32_t format) |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 430 | { |
| 431 | struct display *d = data; |
| 432 | |
| 433 | if (format == DRM_FORMAT_XRGB8888) |
| 434 | d->xrgb8888_format_found = 1; |
| 435 | } |
| 436 | |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 437 | static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = { |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 438 | dmabuf_format |
| 439 | }; |
| 440 | |
| 441 | static void |
| 442 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 443 | { |
| 444 | xdg_shell_pong(shell, serial); |
| 445 | } |
| 446 | |
| 447 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 448 | xdg_shell_ping, |
| 449 | }; |
| 450 | |
| 451 | #define XDG_VERSION 5 /* The version of xdg-shell that we implement */ |
| 452 | #ifdef static_assert |
| 453 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 454 | "Interface version doesn't match implementation version"); |
| 455 | #endif |
| 456 | |
| 457 | static void |
| 458 | registry_handle_global(void *data, struct wl_registry *registry, |
| 459 | uint32_t id, const char *interface, uint32_t version) |
| 460 | { |
| 461 | struct display *d = data; |
| 462 | |
| 463 | if (strcmp(interface, "wl_compositor") == 0) { |
| 464 | d->compositor = |
| 465 | wl_registry_bind(registry, |
| 466 | id, &wl_compositor_interface, 1); |
| 467 | } else if (strcmp(interface, "xdg_shell") == 0) { |
| 468 | d->shell = wl_registry_bind(registry, |
| 469 | id, &xdg_shell_interface, 1); |
| 470 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
| 471 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 472 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 473 | d->fshell = wl_registry_bind(registry, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 474 | id, &zwp_fullscreen_shell_v1_interface, 1); |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 475 | } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) { |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 476 | d->dmabuf = wl_registry_bind(registry, |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 477 | id, &zwp_linux_dmabuf_v1_interface, 1); |
| 478 | zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener, d); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | static void |
| 483 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 484 | uint32_t name) |
| 485 | { |
| 486 | } |
| 487 | |
| 488 | static const struct wl_registry_listener registry_listener = { |
| 489 | registry_handle_global, |
| 490 | registry_handle_global_remove |
| 491 | }; |
| 492 | |
| 493 | static struct display * |
| 494 | create_display(void) |
| 495 | { |
| 496 | struct display *display; |
| 497 | |
| 498 | display = malloc(sizeof *display); |
| 499 | if (display == NULL) { |
| 500 | fprintf(stderr, "out of memory\n"); |
| 501 | exit(1); |
| 502 | } |
| 503 | display->display = wl_display_connect(NULL); |
| 504 | assert(display->display); |
| 505 | |
| 506 | /* XXX: fake, because the compositor does not yet advertise anything */ |
| 507 | display->xrgb8888_format_found = 1; |
| 508 | |
| 509 | display->registry = wl_display_get_registry(display->display); |
| 510 | wl_registry_add_listener(display->registry, |
| 511 | ®istry_listener, display); |
| 512 | wl_display_roundtrip(display->display); |
| 513 | if (display->dmabuf == NULL) { |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 514 | fprintf(stderr, "No zwp_linux_dmabuf global\n"); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 515 | exit(1); |
| 516 | } |
| 517 | |
| 518 | wl_display_roundtrip(display->display); |
| 519 | |
| 520 | if (!display->xrgb8888_format_found) { |
| 521 | fprintf(stderr, "DRM_FORMAT_XRGB8888 not available\n"); |
| 522 | exit(1); |
| 523 | } |
| 524 | |
| 525 | return display; |
| 526 | } |
| 527 | |
| 528 | static void |
| 529 | destroy_display(struct display *display) |
| 530 | { |
| 531 | if (display->dmabuf) |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 532 | zwp_linux_dmabuf_v1_destroy(display->dmabuf); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 533 | |
| 534 | if (display->shell) |
| 535 | xdg_shell_destroy(display->shell); |
| 536 | |
| 537 | if (display->fshell) |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 538 | zwp_fullscreen_shell_v1_release(display->fshell); |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 539 | |
| 540 | if (display->compositor) |
| 541 | wl_compositor_destroy(display->compositor); |
| 542 | |
| 543 | wl_registry_destroy(display->registry); |
| 544 | wl_display_flush(display->display); |
| 545 | wl_display_disconnect(display->display); |
| 546 | free(display); |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | signal_int(int signum) |
| 551 | { |
| 552 | running = 0; |
| 553 | } |
| 554 | |
| 555 | int |
| 556 | main(int argc, char **argv) |
| 557 | { |
| 558 | struct sigaction sigint; |
| 559 | struct display *display; |
| 560 | struct window *window; |
| 561 | int ret = 0; |
| 562 | |
| 563 | display = create_display(); |
| 564 | window = create_window(display, 250, 250); |
| 565 | if (!window) |
| 566 | return 1; |
| 567 | |
| 568 | sigint.sa_handler = signal_int; |
| 569 | sigemptyset(&sigint.sa_mask); |
| 570 | sigint.sa_flags = SA_RESETHAND; |
| 571 | sigaction(SIGINT, &sigint, NULL); |
| 572 | |
Emmanuel Gil Peyrot | 8ef2957 | 2016-01-11 19:04:37 +0000 | [diff] [blame] | 573 | /* Here we retrieve the linux-dmabuf objects, or error */ |
| 574 | wl_display_roundtrip(display->display); |
| 575 | |
| 576 | if (!running) |
| 577 | return 1; |
George Kiagiadakis | 5386898 | 2014-06-12 16:26:49 +0200 | [diff] [blame] | 578 | |
| 579 | redraw(window, NULL, 0); |
| 580 | |
| 581 | while (running && ret != -1) |
| 582 | ret = wl_display_dispatch(display->display); |
| 583 | |
| 584 | fprintf(stderr, "simple-dmabuf exiting\n"); |
| 585 | destroy_window(window); |
| 586 | destroy_display(display); |
| 587 | |
| 588 | return 0; |
| 589 | } |