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; |
| 41 | struct wl_input_device *input_device; |
| 42 | struct wl_surface *surface; |
| 43 | struct wl_shell_surface *shell_surface; |
| 44 | struct wl_buffer *buffer; |
| 45 | int has_argb; |
| 46 | uint32_t mask; |
| 47 | int width, height; |
| 48 | void *data; |
| 49 | }; |
| 50 | |
| 51 | static void |
| 52 | create_shm_buffer(struct touch *touch) |
| 53 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 54 | struct wl_shm_pool *pool; |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 55 | char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 56 | int fd, size, stride; |
| 57 | |
| 58 | fd = mkstemp(filename); |
| 59 | if (fd < 0) { |
| 60 | fprintf(stderr, "open %s failed: %m\n", filename); |
| 61 | exit(1); |
| 62 | } |
| 63 | stride = touch->width * 4; |
| 64 | size = stride * touch->height; |
| 65 | if (ftruncate(fd, size) < 0) { |
| 66 | fprintf(stderr, "ftruncate failed: %m\n"); |
| 67 | close(fd); |
| 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | touch->data = |
| 72 | mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 73 | unlink(filename); |
| 74 | |
| 75 | if (touch->data == MAP_FAILED) { |
| 76 | fprintf(stderr, "mmap failed: %m\n"); |
| 77 | close(fd); |
| 78 | exit(1); |
| 79 | } |
| 80 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 81 | pool = wl_shm_create_pool(touch->shm, fd, size); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 82 | touch->buffer = |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 83 | wl_shm_pool_create_buffer(pool, 0, |
| 84 | touch->width, touch->height, stride, |
| 85 | WL_SHM_FORMAT_ARGB8888); |
| 86 | wl_shm_pool_destroy(pool); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 87 | |
| 88 | close(fd); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 93 | { |
| 94 | struct touch *touch = data; |
| 95 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 96 | if (format == WL_SHM_FORMAT_ARGB8888) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 97 | touch->has_argb = 1; |
| 98 | } |
| 99 | |
| 100 | struct wl_shm_listener shm_listenter = { |
| 101 | shm_format |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | static void |
| 106 | input_device_handle_motion(void *data, struct wl_input_device *input_device, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 107 | uint32_t time, |
| 108 | wl_fixed_t sx_w, |
| 109 | wl_fixed_t sy_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 110 | { |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | input_device_handle_button(void *data, |
| 115 | struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 116 | uint32_t serial, uint32_t time, |
| 117 | uint32_t button, uint32_t state) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 118 | { |
| 119 | } |
| 120 | |
| 121 | static void |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 122 | input_device_handle_axis(void *data, struct wl_input_device *input_device, |
| 123 | uint32_t time, uint32_t axis, int32_t value) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 128 | input_device_handle_key(void *data, struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 129 | uint32_t serial, uint32_t time, |
| 130 | uint32_t key, uint32_t state) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 131 | { |
| 132 | } |
| 133 | |
| 134 | static void |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 135 | input_device_handle_pointer_enter(void *data, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 136 | struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 137 | uint32_t serial, struct wl_surface *surface, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 138 | wl_fixed_t sx_w, wl_fixed_t sy_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 139 | { |
| 140 | } |
| 141 | |
| 142 | static void |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 143 | input_device_handle_pointer_leave(void *data, |
| 144 | struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 145 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 146 | { |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | input_device_handle_keyboard_enter(void *data, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 151 | struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 152 | uint32_t serial, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 153 | struct wl_surface *surface, |
| 154 | struct wl_array *keys) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | static void |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 159 | input_device_handle_keyboard_leave(void *data, |
| 160 | struct wl_input_device *input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 161 | uint32_t serial, |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 162 | struct wl_surface *surface) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | static void |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 167 | touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id) |
| 168 | { |
| 169 | uint32_t *p, c; |
| 170 | static const uint32_t colors[] = { |
| 171 | 0xffff0000, |
| 172 | 0xffffff00, |
| 173 | 0xff0000ff, |
| 174 | 0xffff00ff, |
| 175 | }; |
| 176 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 177 | if (id < (int32_t) ARRAY_LENGTH(colors)) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 178 | c = colors[id]; |
| 179 | else |
| 180 | c = 0xffffffff; |
| 181 | |
| 182 | if (x < 1 || touch->width - 1 < x || |
| 183 | y < 1 || touch->height - 1 < y) |
| 184 | return; |
| 185 | |
| 186 | p = (uint32_t *) touch->data + (x - 1) + (y -1 ) * touch->width; |
| 187 | p[1] = c; |
| 188 | p += touch->width; |
| 189 | p[0] = c; |
| 190 | p[1] = c; |
| 191 | p[2] = c; |
| 192 | p += touch->width; |
| 193 | p[1] = c; |
| 194 | |
Kristian Høgsberg | 9629fe3 | 2012-03-26 15:56:39 -0400 | [diff] [blame] | 195 | wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | input_device_handle_touch_down(void *data, |
| 200 | struct wl_input_device *wl_input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 201 | uint32_t serial, uint32_t time, |
| 202 | struct wl_surface *surface, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 203 | int32_t id, |
| 204 | wl_fixed_t x_w, |
| 205 | wl_fixed_t y_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 206 | { |
| 207 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame^] | 208 | float x = wl_fixed_to_double(x_w); |
| 209 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 210 | |
| 211 | touch_paint(touch, x, y, id); |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | input_device_handle_touch_up(void *data, |
| 216 | struct wl_input_device *wl_input_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 217 | uint32_t serial, uint32_t time, int32_t id) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 218 | { |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | input_device_handle_touch_motion(void *data, |
| 223 | struct wl_input_device *wl_input_device, |
| 224 | uint32_t time, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 225 | int32_t id, |
| 226 | wl_fixed_t x_w, |
| 227 | wl_fixed_t y_w) |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 228 | { |
| 229 | struct touch *touch = data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame^] | 230 | float x = wl_fixed_to_double(x_w); |
| 231 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 232 | |
| 233 | touch_paint(touch, x, y, id); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | input_device_handle_touch_frame(void *data, |
| 238 | struct wl_input_device *wl_input_device) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | input_device_handle_touch_cancel(void *data, |
| 244 | struct wl_input_device *wl_input_device) |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | static const struct wl_input_device_listener input_device_listener = { |
| 249 | input_device_handle_motion, |
| 250 | input_device_handle_button, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 251 | input_device_handle_axis, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 252 | input_device_handle_key, |
Kristian Høgsberg | dd631c1 | 2012-02-23 16:20:38 -0500 | [diff] [blame] | 253 | input_device_handle_pointer_enter, |
| 254 | input_device_handle_pointer_leave, |
| 255 | input_device_handle_keyboard_enter, |
| 256 | input_device_handle_keyboard_leave, |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 257 | input_device_handle_touch_down, |
| 258 | input_device_handle_touch_up, |
| 259 | input_device_handle_touch_motion, |
| 260 | input_device_handle_touch_frame, |
| 261 | input_device_handle_touch_cancel, |
| 262 | }; |
| 263 | |
| 264 | static void |
| 265 | handle_global(struct wl_display *display, uint32_t id, |
| 266 | const char *interface, uint32_t version, void *data) |
| 267 | { |
| 268 | struct touch *touch = data; |
| 269 | |
| 270 | if (strcmp(interface, "wl_compositor") == 0) { |
| 271 | touch->compositor = |
| 272 | wl_display_bind(display, id, &wl_compositor_interface); |
| 273 | } else if (strcmp(interface, "wl_shell") == 0) { |
| 274 | touch->shell = |
| 275 | wl_display_bind(display, id, &wl_shell_interface); |
| 276 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 277 | touch->shm = wl_display_bind(display, id, &wl_shm_interface); |
| 278 | wl_shm_add_listener(touch->shm, &shm_listenter, touch); |
| 279 | } else if (strcmp(interface, "wl_input_device") == 0) { |
| 280 | touch->input_device = |
| 281 | wl_display_bind(display, id, |
| 282 | &wl_input_device_interface); |
| 283 | wl_input_device_add_listener(touch->input_device, |
| 284 | &input_device_listener, touch); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | event_mask_update(uint32_t mask, void *data) |
| 290 | { |
| 291 | struct touch *touch = data; |
| 292 | |
| 293 | touch->mask = mask; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static struct touch * |
| 299 | touch_create(int width, int height) |
| 300 | { |
| 301 | struct touch *touch; |
| 302 | |
| 303 | touch = malloc(sizeof *touch); |
| 304 | touch->display = wl_display_connect(NULL); |
| 305 | assert(touch->display); |
| 306 | |
| 307 | touch->has_argb = 0; |
| 308 | wl_display_add_global_listener(touch->display, handle_global, touch); |
| 309 | wl_display_iterate(touch->display, WL_DISPLAY_READABLE); |
| 310 | wl_display_roundtrip(touch->display); |
| 311 | |
| 312 | if (!touch->has_argb) { |
| 313 | fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n"); |
| 314 | exit(1); |
| 315 | } |
| 316 | |
| 317 | wl_display_get_fd(touch->display, event_mask_update, touch); |
| 318 | |
| 319 | touch->width = width; |
| 320 | touch->height = height; |
| 321 | touch->surface = wl_compositor_create_surface(touch->compositor); |
| 322 | touch->shell_surface = wl_shell_get_shell_surface(touch->shell, |
| 323 | touch->surface); |
| 324 | create_shm_buffer(touch); |
| 325 | |
| 326 | wl_shell_surface_set_toplevel(touch->shell_surface); |
| 327 | wl_surface_set_user_data(touch->surface, touch); |
| 328 | |
| 329 | memset(touch->data, 64, width * height * 4); |
Kristian Høgsberg | 558949b | 2011-12-21 22:54:49 -0500 | [diff] [blame] | 330 | wl_surface_attach(touch->surface, touch->buffer, 0, 0); |
| 331 | wl_surface_damage(touch->surface, 0, 0, width, height); |
| 332 | |
| 333 | return touch; |
| 334 | } |
| 335 | |
| 336 | int |
| 337 | main(int argc, char **argv) |
| 338 | { |
| 339 | struct touch *touch; |
| 340 | |
| 341 | touch = touch_create(600, 500); |
| 342 | |
| 343 | while (true) |
| 344 | wl_display_iterate(touch->display, touch->mask); |
| 345 | |
| 346 | return 0; |
| 347 | } |