Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Benjamin Franzke |
| 3 | * Copyright © 2011 Intel Corporation |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 4 | * Copyright © 2021 Collabora, Ltd. |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 5 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 12 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 24 | */ |
| 25 | |
Bryce Harrington | b4dae9b | 2016-06-15 18:13:07 -0700 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 28 | #include <stdint.h> |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <stdbool.h> |
| 33 | #include <assert.h> |
| 34 | #include <unistd.h> |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 35 | #include <errno.h> |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 36 | #include <sys/mman.h> |
| 37 | |
| 38 | #include <wayland-client.h> |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 39 | #include "shared/helpers.h" |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 40 | #include "shared/xalloc.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 41 | #include "shared/os-compatibility.h" |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 42 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 43 | #include "xdg-shell-client-protocol.h" |
| 44 | |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 45 | struct seat { |
| 46 | struct touch *touch; |
| 47 | struct wl_seat *seat; |
| 48 | struct wl_touch *wl_touch; |
| 49 | }; |
| 50 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 51 | struct buffer { |
| 52 | struct wl_buffer *buffer; |
| 53 | void *data; |
| 54 | }; |
| 55 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 56 | struct touch { |
| 57 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 58 | struct wl_registry *registry; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 59 | struct wl_compositor *compositor; |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 60 | struct xdg_wm_base *wm_base; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 61 | struct wl_shm *shm; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 62 | struct wl_surface *surface; |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 63 | struct xdg_surface *xdg_surface; |
| 64 | struct xdg_toplevel *xdg_toplevel; |
| 65 | struct buffer *buffer; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 66 | int width, height; |
Emmanuel Gil Peyrot | 152405b | 2021-10-14 11:43:46 +0200 | [diff] [blame] | 67 | bool running; |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 68 | bool wait_for_configure; |
Emmanuel Gil Peyrot | 152405b | 2021-10-14 11:43:46 +0200 | [diff] [blame] | 69 | bool has_argb; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 70 | }; |
| 71 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 72 | static struct buffer * |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 73 | create_shm_buffer(struct touch *touch) |
| 74 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 75 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 76 | int fd, size, stride; |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 77 | void *data; |
| 78 | struct buffer *buffer = NULL; |
| 79 | |
| 80 | buffer = zalloc(sizeof(*buffer)); |
| 81 | if (!buffer) |
| 82 | return NULL; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 83 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 84 | stride = touch->width * 4; |
| 85 | size = stride * touch->height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 86 | |
| 87 | fd = os_create_anonymous_file(size); |
| 88 | if (fd < 0) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 89 | fprintf(stderr, "creating a buffer file for %d B failed: %s\n", |
| 90 | size, strerror(errno)); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 91 | exit(1); |
| 92 | } |
| 93 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 94 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 95 | if (data == MAP_FAILED) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 96 | fprintf(stderr, "mmap failed: %s\n", strerror(errno)); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 97 | close(fd); |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 98 | return NULL; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 101 | pool = wl_shm_create_pool(touch->shm, fd, size); |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 102 | buffer->buffer = |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 103 | wl_shm_pool_create_buffer(pool, 0, |
| 104 | touch->width, touch->height, stride, |
| 105 | WL_SHM_FORMAT_ARGB8888); |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 106 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 107 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 108 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 109 | buffer->data = data; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 110 | close(fd); |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 111 | |
| 112 | return buffer; |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | initial_redraw(void *data) |
| 117 | { |
| 118 | struct touch *touch = data; |
| 119 | struct buffer *buffer = NULL; |
| 120 | |
| 121 | buffer = create_shm_buffer(touch); |
| 122 | assert(buffer); |
| 123 | |
| 124 | touch->buffer = buffer; |
| 125 | |
| 126 | /* paint the "work-area" */ |
| 127 | memset(buffer->data, 64, touch->width * touch->height * 4); |
| 128 | |
| 129 | wl_surface_attach(touch->surface, buffer->buffer, 0, 0); |
| 130 | wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height); |
| 131 | wl_surface_commit(touch->surface); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void |
| 135 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 136 | { |
| 137 | struct touch *touch = data; |
| 138 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 139 | if (format == WL_SHM_FORMAT_ARGB8888) |
Emmanuel Gil Peyrot | b94219a | 2021-10-14 11:41:28 +0200 | [diff] [blame] | 140 | touch->has_argb = true; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 143 | struct wl_shm_listener shm_listener = { |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 144 | shm_format |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 149 | touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id) |
| 150 | { |
| 151 | uint32_t *p, c; |
| 152 | static const uint32_t colors[] = { |
| 153 | 0xffff0000, |
| 154 | 0xffffff00, |
| 155 | 0xff0000ff, |
| 156 | 0xffff00ff, |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame] | 157 | 0xff00ff00, |
| 158 | 0xff00ffff, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 159 | }; |
| 160 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 161 | if (id < (int32_t) ARRAY_LENGTH(colors)) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 162 | c = colors[id]; |
| 163 | else |
| 164 | c = 0xffffffff; |
| 165 | |
Pekka Paalanen | 0768419 | 2012-07-31 13:21:12 +0300 | [diff] [blame] | 166 | if (x < 2 || x >= touch->width - 2 || |
| 167 | y < 2 || y >= touch->height - 2) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 168 | return; |
| 169 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 170 | p = ((uint32_t *) touch->buffer->data) + (x - 2) + (y - 2) * touch->width; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame] | 171 | p[2] = c; |
| 172 | p += touch->width; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 173 | p[1] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame] | 174 | p[2] = c; |
| 175 | p[3] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 176 | p += touch->width; |
| 177 | p[0] = c; |
| 178 | p[1] = c; |
| 179 | p[2] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame] | 180 | p[3] = c; |
| 181 | p[4] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 182 | p += touch->width; |
| 183 | p[1] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame] | 184 | p[2] = c; |
| 185 | p[3] = c; |
| 186 | p += touch->width; |
| 187 | p[2] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 188 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 189 | wl_surface_attach(touch->surface, touch->buffer->buffer, 0, 0); |
Pekka Paalanen | 76fc57e | 2012-07-31 13:21:13 +0300 | [diff] [blame] | 190 | wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5); |
Pekka Paalanen | 8e15918 | 2012-10-10 12:49:25 +0300 | [diff] [blame] | 191 | /* todo: We could queue up more damage before committing, if there |
| 192 | * are more input events to handle. |
| 193 | */ |
| 194 | wl_surface_commit(touch->surface); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 198 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 199 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 200 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 201 | { |
| 202 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 203 | float x = wl_fixed_to_double(x_w); |
| 204 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 205 | |
| 206 | touch_paint(touch, x, y, id); |
| 207 | } |
| 208 | |
| 209 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 210 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 211 | uint32_t serial, uint32_t time, int32_t id) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 212 | { |
| 213 | } |
| 214 | |
| 215 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 216 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 217 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 218 | { |
| 219 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 220 | float x = wl_fixed_to_double(x_w); |
| 221 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 222 | |
| 223 | touch_paint(touch, x, y, id); |
| 224 | } |
| 225 | |
| 226 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 227 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 228 | { |
| 229 | } |
| 230 | |
| 231 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 232 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 233 | { |
| 234 | } |
| 235 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 236 | static const struct wl_touch_listener touch_listener = { |
| 237 | touch_handle_down, |
| 238 | touch_handle_up, |
| 239 | touch_handle_motion, |
| 240 | touch_handle_frame, |
| 241 | touch_handle_cancel, |
| 242 | }; |
| 243 | |
| 244 | static void |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 245 | seat_handle_capabilities(void *data, struct wl_seat *wl_seat, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 246 | enum wl_seat_capability caps) |
| 247 | { |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 248 | struct seat *seat = data; |
| 249 | struct touch *touch = seat->touch; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 250 | |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 251 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !seat->wl_touch) { |
| 252 | seat->wl_touch = wl_seat_get_touch(wl_seat); |
| 253 | wl_touch_set_user_data(seat->wl_touch, touch); |
| 254 | wl_touch_add_listener(seat->wl_touch, &touch_listener, touch); |
| 255 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->wl_touch) { |
| 256 | wl_touch_destroy(seat->wl_touch); |
| 257 | seat->wl_touch = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | static const struct wl_seat_listener seat_listener = { |
| 262 | seat_handle_capabilities, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | static void |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 266 | add_seat(struct touch *touch, uint32_t name, uint32_t version) |
| 267 | { |
| 268 | struct seat *seat; |
| 269 | |
| 270 | seat = malloc(sizeof *seat); |
| 271 | assert(seat); |
| 272 | |
| 273 | seat->touch = touch; |
| 274 | seat->wl_touch = NULL; |
| 275 | seat->seat = wl_registry_bind(touch->registry, name, |
| 276 | &wl_seat_interface, 1); |
| 277 | wl_seat_add_listener(seat->seat, &seat_listener, seat); |
| 278 | } |
| 279 | |
| 280 | static void |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 281 | handle_xdg_surface_configure(void *data, struct xdg_surface *surface, |
| 282 | uint32_t serial) |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 283 | { |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 284 | struct touch *touch = data; |
| 285 | |
| 286 | xdg_surface_ack_configure(surface, serial); |
| 287 | |
| 288 | if (touch->wait_for_configure) { |
| 289 | initial_redraw(touch); |
| 290 | touch->wait_for_configure = false; |
| 291 | } |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 292 | } |
| 293 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 294 | static const struct xdg_surface_listener xdg_surface_listener = { |
| 295 | handle_xdg_surface_configure, |
| 296 | }; |
| 297 | |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 298 | static void |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 299 | xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial) |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 300 | { |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 301 | xdg_wm_base_pong(shell, serial); |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 302 | } |
| 303 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 304 | static const struct xdg_wm_base_listener wm_base_listener = { |
| 305 | xdg_wm_base_ping, |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 309 | handle_global(void *data, struct wl_registry *registry, |
| 310 | uint32_t name, const char *interface, uint32_t version) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 311 | { |
| 312 | struct touch *touch = data; |
| 313 | |
| 314 | if (strcmp(interface, "wl_compositor") == 0) { |
| 315 | touch->compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 316 | wl_registry_bind(registry, name, |
| 317 | &wl_compositor_interface, 1); |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 318 | } else if (strcmp(interface, "xdg_wm_base") == 0) { |
| 319 | touch->wm_base = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 320 | wl_registry_bind(registry, name, |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 321 | &xdg_wm_base_interface, 1); |
| 322 | xdg_wm_base_add_listener(touch->wm_base, |
| 323 | &wm_base_listener, touch); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 324 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 325 | touch->shm = wl_registry_bind(registry, name, |
| 326 | &wl_shm_interface, 1); |
Stefan Schmidt | 85c40f2 | 2013-08-05 13:50:50 +0100 | [diff] [blame] | 327 | wl_shm_add_listener(touch->shm, &shm_listener, touch); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 328 | } else if (strcmp(interface, "wl_seat") == 0) { |
Ander Conselvan de Oliveira | c3f03f5 | 2014-05-07 11:57:27 +0300 | [diff] [blame] | 329 | add_seat(touch, name, version); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 333 | static void |
| 334 | handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) |
| 335 | { |
| 336 | } |
| 337 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 338 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 339 | handle_global, |
| 340 | handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 341 | }; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 342 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 343 | static void |
| 344 | handle_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel, |
| 345 | int32_t width, int32_t height, |
| 346 | struct wl_array *state) |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | static void |
| 351 | handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) |
| 352 | { |
Emmanuel Gil Peyrot | 152405b | 2021-10-14 11:43:46 +0200 | [diff] [blame] | 353 | struct touch *touch = data; |
| 354 | touch->running = false; |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static const struct xdg_toplevel_listener xdg_toplevel_listener = { |
| 358 | handle_toplevel_configure, |
| 359 | handle_toplevel_close, |
| 360 | }; |
| 361 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 362 | static struct touch * |
| 363 | touch_create(int width, int height) |
| 364 | { |
| 365 | struct touch *touch; |
| 366 | |
| 367 | touch = malloc(sizeof *touch); |
Kristian Høgsberg | c85a917 | 2013-08-15 11:40:30 -0700 | [diff] [blame] | 368 | if (touch == NULL) { |
| 369 | fprintf(stderr, "out of memory\n"); |
| 370 | exit(1); |
| 371 | } |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 372 | touch->display = wl_display_connect(NULL); |
| 373 | assert(touch->display); |
| 374 | |
Emmanuel Gil Peyrot | b94219a | 2021-10-14 11:41:28 +0200 | [diff] [blame] | 375 | touch->has_argb = false; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 376 | touch->registry = wl_display_get_registry(touch->display); |
| 377 | wl_registry_add_listener(touch->registry, ®istry_listener, touch); |
| 378 | wl_display_dispatch(touch->display); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 379 | wl_display_roundtrip(touch->display); |
| 380 | |
| 381 | if (!touch->has_argb) { |
| 382 | fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n"); |
| 383 | exit(1); |
| 384 | } |
| 385 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 386 | if (!touch->wm_base) { |
| 387 | fprintf(stderr, "xdg-shell required!\n"); |
| 388 | exit(1); |
| 389 | } |
| 390 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 391 | touch->width = width; |
| 392 | touch->height = height; |
| 393 | touch->surface = wl_compositor_create_surface(touch->compositor); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 394 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 395 | touch->xdg_surface = |
| 396 | xdg_wm_base_get_xdg_surface(touch->wm_base, touch->surface); |
| 397 | assert(touch->xdg_surface); |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 398 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 399 | xdg_surface_add_listener(touch->xdg_surface, &xdg_surface_listener, touch); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 400 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 401 | touch->xdg_toplevel = xdg_surface_get_toplevel(touch->xdg_surface); |
| 402 | assert(touch->xdg_toplevel); |
| 403 | xdg_toplevel_add_listener(touch->xdg_toplevel, |
| 404 | &xdg_toplevel_listener, touch); |
| 405 | xdg_toplevel_set_title(touch->xdg_toplevel, "simple-touch"); |
| 406 | xdg_toplevel_set_app_id(touch->xdg_toplevel, "simple-touch"); |
| 407 | touch->wait_for_configure = true; |
Pekka Paalanen | c9e00c0 | 2012-10-10 12:49:24 +0300 | [diff] [blame] | 408 | wl_surface_commit(touch->surface); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 409 | |
Emmanuel Gil Peyrot | 152405b | 2021-10-14 11:43:46 +0200 | [diff] [blame] | 410 | touch->running = true; |
| 411 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 412 | return touch; |
| 413 | } |
| 414 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 415 | static void |
| 416 | destroy_touch(struct touch *touch) |
| 417 | { |
| 418 | if (touch->buffer->buffer) |
| 419 | wl_buffer_destroy(touch->buffer->buffer); |
| 420 | |
| 421 | if (touch->xdg_toplevel) |
| 422 | xdg_toplevel_destroy(touch->xdg_toplevel); |
| 423 | if (touch->xdg_surface) |
| 424 | xdg_surface_destroy(touch->xdg_surface); |
| 425 | if (touch->wm_base) |
| 426 | xdg_wm_base_destroy(touch->wm_base); |
| 427 | if (touch->shm) |
| 428 | wl_shm_destroy(touch->shm); |
| 429 | if (touch->compositor) |
| 430 | wl_compositor_destroy(touch->compositor); |
| 431 | |
| 432 | |
| 433 | wl_surface_destroy(touch->surface); |
| 434 | free(touch->buffer); |
| 435 | free(touch); |
| 436 | } |
| 437 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 438 | int |
| 439 | main(int argc, char **argv) |
| 440 | { |
| 441 | struct touch *touch; |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 442 | int ret = 0; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 443 | |
| 444 | touch = touch_create(600, 500); |
| 445 | |
Emmanuel Gil Peyrot | 152405b | 2021-10-14 11:43:46 +0200 | [diff] [blame] | 446 | while (ret != -1 && touch->running) |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 447 | ret = wl_display_dispatch(touch->display); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 448 | |
Marius Vlad | 539c5a6 | 2021-08-13 18:18:34 +0300 | [diff] [blame] | 449 | destroy_touch(touch); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 450 | return 0; |
| 451 | } |