Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Benjamin Franzke |
| 3 | * Copyright © 2011 Intel Corporation |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <assert.h> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/mman.h> |
| 31 | |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 32 | #include <GLES2/gl2.h> |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 33 | #include <wayland-client.h> |
| 34 | #include <wayland-egl.h> |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 35 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 36 | |
| 37 | struct touch { |
| 38 | struct wl_display *display; |
| 39 | struct wl_compositor *compositor; |
| 40 | struct wl_shell *shell; |
| 41 | struct wl_shm *shm; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 42 | struct wl_seat *seat; |
| 43 | struct wl_touch *wl_touch; |
| 44 | struct wl_pointer *pointer; |
| 45 | struct wl_keyboard *keyboard; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 46 | struct wl_surface *surface; |
| 47 | struct wl_shell_surface *shell_surface; |
| 48 | struct wl_buffer *buffer; |
| 49 | int has_argb; |
| 50 | uint32_t mask; |
| 51 | int width, height; |
| 52 | void *data; |
| 53 | }; |
| 54 | |
| 55 | static void |
| 56 | create_shm_buffer(struct touch *touch) |
| 57 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 58 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 59 | int fd, size, stride; |
| 60 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 61 | stride = touch->width * 4; |
| 62 | size = stride * touch->height; |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 63 | |
| 64 | fd = os_create_anonymous_file(size); |
| 65 | if (fd < 0) { |
| 66 | fprintf(stderr, "creating a buffer file for %d B failed: %m\n", |
| 67 | size); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | touch->data = |
| 72 | mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 73 | if (touch->data == MAP_FAILED) { |
| 74 | fprintf(stderr, "mmap failed: %m\n"); |
| 75 | close(fd); |
| 76 | exit(1); |
| 77 | } |
| 78 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 79 | pool = wl_shm_create_pool(touch->shm, fd, size); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 80 | touch->buffer = |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 81 | wl_shm_pool_create_buffer(pool, 0, |
| 82 | touch->width, touch->height, stride, |
| 83 | WL_SHM_FORMAT_ARGB8888); |
| 84 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 85 | |
| 86 | close(fd); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 91 | { |
| 92 | struct touch *touch = data; |
| 93 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 94 | if (format == WL_SHM_FORMAT_ARGB8888) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 95 | touch->has_argb = 1; |
| 96 | } |
| 97 | |
| 98 | struct wl_shm_listener shm_listenter = { |
| 99 | shm_format |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 104 | touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id) |
| 105 | { |
| 106 | uint32_t *p, c; |
| 107 | static const uint32_t colors[] = { |
| 108 | 0xffff0000, |
| 109 | 0xffffff00, |
| 110 | 0xff0000ff, |
| 111 | 0xffff00ff, |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 112 | 0xff00ff00, |
| 113 | 0xff00ffff, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 114 | }; |
| 115 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 116 | if (id < (int32_t) ARRAY_LENGTH(colors)) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 117 | c = colors[id]; |
| 118 | else |
| 119 | c = 0xffffffff; |
| 120 | |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 121 | if (x < 2 || touch->width - 2 < x || |
| 122 | y < 2 || touch->height - 2 < y) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 123 | return; |
| 124 | |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 125 | p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width; |
| 126 | p[2] = c; |
| 127 | p += touch->width; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 128 | p[1] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 129 | p[2] = c; |
| 130 | p[3] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 131 | p += touch->width; |
| 132 | p[0] = c; |
| 133 | p[1] = c; |
| 134 | p[2] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 135 | p[3] = c; |
| 136 | p[4] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 137 | p += touch->width; |
| 138 | p[1] = c; |
Pekka Paalanen | 55b7cb2 | 2012-07-31 13:21:11 +0300 | [diff] [blame^] | 139 | p[2] = c; |
| 140 | p[3] = c; |
| 141 | p += touch->width; |
| 142 | p[2] = c; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 143 | |
Kristian Høgsberg | 9629fe3 | 2012-03-26 15:56:39 -0400 | [diff] [blame] | 144 | wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 148 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 149 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 150 | 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] | 151 | { |
| 152 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 153 | float x = wl_fixed_to_double(x_w); |
| 154 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 155 | |
| 156 | touch_paint(touch, x, y, id); |
| 157 | } |
| 158 | |
| 159 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 160 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 161 | uint32_t serial, uint32_t time, int32_t id) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 162 | { |
| 163 | } |
| 164 | |
| 165 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 166 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 167 | 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] | 168 | { |
| 169 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 170 | float x = wl_fixed_to_double(x_w); |
| 171 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 172 | |
| 173 | touch_paint(touch, x, y, id); |
| 174 | } |
| 175 | |
| 176 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 177 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 178 | { |
| 179 | } |
| 180 | |
| 181 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 182 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 183 | { |
| 184 | } |
| 185 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 186 | static const struct wl_touch_listener touch_listener = { |
| 187 | touch_handle_down, |
| 188 | touch_handle_up, |
| 189 | touch_handle_motion, |
| 190 | touch_handle_frame, |
| 191 | touch_handle_cancel, |
| 192 | }; |
| 193 | |
| 194 | static void |
| 195 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 196 | enum wl_seat_capability caps) |
| 197 | { |
| 198 | struct touch *touch = data; |
| 199 | |
| 200 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) { |
| 201 | touch->wl_touch = wl_seat_get_touch(seat); |
| 202 | wl_touch_set_user_data(touch->wl_touch, touch); |
| 203 | wl_touch_add_listener(touch->wl_touch, &touch_listener, touch); |
| 204 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) { |
| 205 | wl_touch_destroy(touch->wl_touch); |
| 206 | touch->wl_touch = NULL; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static const struct wl_seat_listener seat_listener = { |
| 211 | seat_handle_capabilities, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static void |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 215 | handle_ping(void *data, struct wl_shell_surface *shell_surface, |
| 216 | uint32_t serial) |
| 217 | { |
| 218 | wl_shell_surface_pong(shell_surface, serial); |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | handle_configure(void *data, struct wl_shell_surface *shell_surface, |
| 223 | uint32_t edges, int32_t width, int32_t height) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | handle_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 233 | handle_ping, |
| 234 | handle_configure, |
| 235 | handle_popup_done |
| 236 | }; |
| 237 | |
| 238 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 239 | handle_global(struct wl_display *display, uint32_t id, |
| 240 | const char *interface, uint32_t version, void *data) |
| 241 | { |
| 242 | struct touch *touch = data; |
| 243 | |
| 244 | if (strcmp(interface, "wl_compositor") == 0) { |
| 245 | touch->compositor = |
| 246 | wl_display_bind(display, id, &wl_compositor_interface); |
| 247 | } else if (strcmp(interface, "wl_shell") == 0) { |
| 248 | touch->shell = |
| 249 | wl_display_bind(display, id, &wl_shell_interface); |
| 250 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 251 | touch->shm = wl_display_bind(display, id, &wl_shm_interface); |
| 252 | wl_shm_add_listener(touch->shm, &shm_listenter, touch); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 253 | } else if (strcmp(interface, "wl_seat") == 0) { |
| 254 | touch->seat = wl_display_bind(display, id, &wl_seat_interface); |
| 255 | wl_seat_add_listener(touch->seat, &seat_listener, touch); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | static int |
| 260 | event_mask_update(uint32_t mask, void *data) |
| 261 | { |
| 262 | struct touch *touch = data; |
| 263 | |
| 264 | touch->mask = mask; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static struct touch * |
| 270 | touch_create(int width, int height) |
| 271 | { |
| 272 | struct touch *touch; |
| 273 | |
| 274 | touch = malloc(sizeof *touch); |
| 275 | touch->display = wl_display_connect(NULL); |
| 276 | assert(touch->display); |
| 277 | |
| 278 | touch->has_argb = 0; |
| 279 | wl_display_add_global_listener(touch->display, handle_global, touch); |
| 280 | wl_display_iterate(touch->display, WL_DISPLAY_READABLE); |
| 281 | wl_display_roundtrip(touch->display); |
| 282 | |
| 283 | if (!touch->has_argb) { |
| 284 | fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n"); |
| 285 | exit(1); |
| 286 | } |
| 287 | |
| 288 | wl_display_get_fd(touch->display, event_mask_update, touch); |
| 289 | |
| 290 | touch->width = width; |
| 291 | touch->height = height; |
| 292 | touch->surface = wl_compositor_create_surface(touch->compositor); |
| 293 | touch->shell_surface = wl_shell_get_shell_surface(touch->shell, |
| 294 | touch->surface); |
| 295 | create_shm_buffer(touch); |
| 296 | |
Pekka Paalanen | 7e94a98 | 2012-07-31 13:21:10 +0300 | [diff] [blame] | 297 | if (touch->shell_surface) { |
| 298 | wl_shell_surface_add_listener(touch->shell_surface, |
| 299 | &shell_surface_listener, touch); |
| 300 | wl_shell_surface_set_toplevel(touch->shell_surface); |
| 301 | } |
| 302 | |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 303 | wl_surface_set_user_data(touch->surface, touch); |
| 304 | |
| 305 | memset(touch->data, 64, width * height * 4); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 306 | wl_surface_attach(touch->surface, touch->buffer, 0, 0); |
| 307 | wl_surface_damage(touch->surface, 0, 0, width, height); |
| 308 | |
| 309 | return touch; |
| 310 | } |
| 311 | |
| 312 | int |
| 313 | main(int argc, char **argv) |
| 314 | { |
| 315 | struct touch *touch; |
| 316 | |
| 317 | touch = touch_create(600, 500); |
| 318 | |
| 319 | while (true) |
| 320 | wl_display_iterate(touch->display, touch->mask); |
| 321 | |
| 322 | return 0; |
| 323 | } |