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> |
| 35 | |
| 36 | struct touch { |
| 37 | struct wl_display *display; |
| 38 | struct wl_compositor *compositor; |
| 39 | struct wl_shell *shell; |
| 40 | struct wl_shm *shm; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 41 | struct wl_seat *seat; |
| 42 | struct wl_touch *wl_touch; |
| 43 | struct wl_pointer *pointer; |
| 44 | struct wl_keyboard *keyboard; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 45 | struct wl_surface *surface; |
| 46 | struct wl_shell_surface *shell_surface; |
| 47 | struct wl_buffer *buffer; |
| 48 | int has_argb; |
| 49 | uint32_t mask; |
| 50 | int width, height; |
| 51 | void *data; |
| 52 | }; |
| 53 | |
| 54 | static void |
| 55 | create_shm_buffer(struct touch *touch) |
| 56 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 57 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 58 | char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 59 | int fd, size, stride; |
| 60 | |
| 61 | fd = mkstemp(filename); |
| 62 | if (fd < 0) { |
| 63 | fprintf(stderr, "open %s failed: %m\n", filename); |
| 64 | exit(1); |
| 65 | } |
| 66 | stride = touch->width * 4; |
| 67 | size = stride * touch->height; |
| 68 | if (ftruncate(fd, size) < 0) { |
| 69 | fprintf(stderr, "ftruncate failed: %m\n"); |
| 70 | close(fd); |
| 71 | exit(1); |
| 72 | } |
| 73 | |
| 74 | touch->data = |
| 75 | mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 76 | unlink(filename); |
| 77 | |
| 78 | if (touch->data == MAP_FAILED) { |
| 79 | fprintf(stderr, "mmap failed: %m\n"); |
| 80 | close(fd); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 84 | pool = wl_shm_create_pool(touch->shm, fd, size); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 85 | touch->buffer = |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 86 | wl_shm_pool_create_buffer(pool, 0, |
| 87 | touch->width, touch->height, stride, |
| 88 | WL_SHM_FORMAT_ARGB8888); |
| 89 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 90 | |
| 91 | close(fd); |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 96 | { |
| 97 | struct touch *touch = data; |
| 98 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 99 | if (format == WL_SHM_FORMAT_ARGB8888) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 100 | touch->has_argb = 1; |
| 101 | } |
| 102 | |
| 103 | struct wl_shm_listener shm_listenter = { |
| 104 | shm_format |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 109 | touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id) |
| 110 | { |
| 111 | uint32_t *p, c; |
| 112 | static const uint32_t colors[] = { |
| 113 | 0xffff0000, |
| 114 | 0xffffff00, |
| 115 | 0xff0000ff, |
| 116 | 0xffff00ff, |
| 117 | }; |
| 118 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 119 | if (id < (int32_t) ARRAY_LENGTH(colors)) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 120 | c = colors[id]; |
| 121 | else |
| 122 | c = 0xffffffff; |
| 123 | |
| 124 | if (x < 1 || touch->width - 1 < x || |
| 125 | y < 1 || touch->height - 1 < y) |
| 126 | return; |
| 127 | |
| 128 | p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width; |
| 129 | p[1] = c; |
| 130 | p += touch->width; |
| 131 | p[0] = c; |
| 132 | p[1] = c; |
| 133 | p[2] = c; |
| 134 | p += touch->width; |
| 135 | p[1] = c; |
| 136 | |
Kristian Høgsberg | 9629fe3 | 2012-03-26 15:56:39 -0400 | [diff] [blame] | 137 | wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 141 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 142 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 143 | 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] | 144 | { |
| 145 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 146 | float x = wl_fixed_to_double(x_w); |
| 147 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 148 | |
| 149 | touch_paint(touch, x, y, id); |
| 150 | } |
| 151 | |
| 152 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 153 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 154 | uint32_t serial, uint32_t time, int32_t id) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
| 158 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 159 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 160 | 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] | 161 | { |
| 162 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 163 | float x = wl_fixed_to_double(x_w); |
| 164 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 165 | |
| 166 | touch_paint(touch, x, y, id); |
| 167 | } |
| 168 | |
| 169 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 170 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 171 | { |
| 172 | } |
| 173 | |
| 174 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 175 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 176 | { |
| 177 | } |
| 178 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 179 | static const struct wl_touch_listener touch_listener = { |
| 180 | touch_handle_down, |
| 181 | touch_handle_up, |
| 182 | touch_handle_motion, |
| 183 | touch_handle_frame, |
| 184 | touch_handle_cancel, |
| 185 | }; |
| 186 | |
| 187 | static void |
| 188 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 189 | enum wl_seat_capability caps) |
| 190 | { |
| 191 | struct touch *touch = data; |
| 192 | |
| 193 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) { |
| 194 | touch->wl_touch = wl_seat_get_touch(seat); |
| 195 | wl_touch_set_user_data(touch->wl_touch, touch); |
| 196 | wl_touch_add_listener(touch->wl_touch, &touch_listener, touch); |
| 197 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) { |
| 198 | wl_touch_destroy(touch->wl_touch); |
| 199 | touch->wl_touch = NULL; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static const struct wl_seat_listener seat_listener = { |
| 204 | seat_handle_capabilities, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static void |
| 208 | handle_global(struct wl_display *display, uint32_t id, |
| 209 | const char *interface, uint32_t version, void *data) |
| 210 | { |
| 211 | struct touch *touch = data; |
| 212 | |
| 213 | if (strcmp(interface, "wl_compositor") == 0) { |
| 214 | touch->compositor = |
| 215 | wl_display_bind(display, id, &wl_compositor_interface); |
| 216 | } else if (strcmp(interface, "wl_shell") == 0) { |
| 217 | touch->shell = |
| 218 | wl_display_bind(display, id, &wl_shell_interface); |
| 219 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 220 | touch->shm = wl_display_bind(display, id, &wl_shm_interface); |
| 221 | wl_shm_add_listener(touch->shm, &shm_listenter, touch); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame^] | 222 | } else if (strcmp(interface, "wl_seat") == 0) { |
| 223 | touch->seat = wl_display_bind(display, id, &wl_seat_interface); |
| 224 | wl_seat_add_listener(touch->seat, &seat_listener, touch); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | event_mask_update(uint32_t mask, void *data) |
| 230 | { |
| 231 | struct touch *touch = data; |
| 232 | |
| 233 | touch->mask = mask; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static struct touch * |
| 239 | touch_create(int width, int height) |
| 240 | { |
| 241 | struct touch *touch; |
| 242 | |
| 243 | touch = malloc(sizeof *touch); |
| 244 | touch->display = wl_display_connect(NULL); |
| 245 | assert(touch->display); |
| 246 | |
| 247 | touch->has_argb = 0; |
| 248 | wl_display_add_global_listener(touch->display, handle_global, touch); |
| 249 | wl_display_iterate(touch->display, WL_DISPLAY_READABLE); |
| 250 | wl_display_roundtrip(touch->display); |
| 251 | |
| 252 | if (!touch->has_argb) { |
| 253 | fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n"); |
| 254 | exit(1); |
| 255 | } |
| 256 | |
| 257 | wl_display_get_fd(touch->display, event_mask_update, touch); |
| 258 | |
| 259 | touch->width = width; |
| 260 | touch->height = height; |
| 261 | touch->surface = wl_compositor_create_surface(touch->compositor); |
| 262 | touch->shell_surface = wl_shell_get_shell_surface(touch->shell, |
| 263 | touch->surface); |
| 264 | create_shm_buffer(touch); |
| 265 | |
| 266 | wl_shell_surface_set_toplevel(touch->shell_surface); |
| 267 | wl_surface_set_user_data(touch->surface, touch); |
| 268 | |
| 269 | memset(touch->data, 64, width * height * 4); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 270 | wl_surface_attach(touch->surface, touch->buffer, 0, 0); |
| 271 | wl_surface_damage(touch->surface, 0, 0, width, height); |
| 272 | |
| 273 | return touch; |
| 274 | } |
| 275 | |
| 276 | int |
| 277 | main(int argc, char **argv) |
| 278 | { |
| 279 | struct touch *touch; |
| 280 | |
| 281 | touch = touch_create(600, 500); |
| 282 | |
| 283 | while (true) |
| 284 | wl_display_iterate(touch->display, touch->mask); |
| 285 | |
| 286 | return 0; |
| 287 | } |