Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Collabora, Ltd. |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 4 | * Copyright © 2012 Jonas Ådahl |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [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: |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [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. |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <cairo.h> |
| 33 | #include <math.h> |
| 34 | #include <assert.h> |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 35 | #include <sys/timerfd.h> |
| 36 | #include <sys/epoll.h> |
| 37 | #include <unistd.h> |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 38 | |
| 39 | #include <linux/input.h> |
| 40 | #include <wayland-client.h> |
| 41 | |
| 42 | #include "window.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 43 | #include "shared/helpers.h" |
Bryce Harrington | e99e4bf | 2016-03-16 14:15:18 -0700 | [diff] [blame] | 44 | #include "shared/xalloc.h" |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 45 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 46 | struct clickdot { |
| 47 | struct display *display; |
| 48 | struct window *window; |
| 49 | struct widget *widget; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 50 | |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 51 | cairo_surface_t *buffer; |
| 52 | |
| 53 | struct { |
| 54 | int32_t x, y; |
| 55 | } dot; |
| 56 | |
| 57 | struct { |
| 58 | int32_t x, y; |
| 59 | int32_t old_x, old_y; |
| 60 | } line; |
| 61 | |
| 62 | int reset; |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 63 | |
| 64 | struct input *cursor_timeout_input; |
| 65 | int cursor_timeout_fd; |
| 66 | struct task cursor_timeout_task; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static void |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 70 | draw_line(struct clickdot *clickdot, cairo_t *cr, |
| 71 | struct rectangle *allocation) |
| 72 | { |
| 73 | cairo_t *bcr; |
| 74 | cairo_surface_t *tmp_buffer = NULL; |
| 75 | |
| 76 | if (clickdot->reset) { |
| 77 | tmp_buffer = clickdot->buffer; |
| 78 | clickdot->buffer = NULL; |
| 79 | clickdot->line.x = -1; |
| 80 | clickdot->line.y = -1; |
| 81 | clickdot->line.old_x = -1; |
| 82 | clickdot->line.old_y = -1; |
| 83 | clickdot->reset = 0; |
| 84 | } |
| 85 | |
| 86 | if (clickdot->buffer == NULL) { |
| 87 | clickdot->buffer = |
| 88 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 89 | allocation->width, |
| 90 | allocation->height); |
| 91 | bcr = cairo_create(clickdot->buffer); |
| 92 | cairo_set_source_rgba(bcr, 0, 0, 0, 0); |
| 93 | cairo_rectangle(bcr, |
| 94 | 0, 0, |
| 95 | allocation->width, allocation->height); |
| 96 | cairo_fill(bcr); |
| 97 | } |
| 98 | else |
| 99 | bcr = cairo_create(clickdot->buffer); |
| 100 | |
| 101 | if (tmp_buffer) { |
| 102 | cairo_set_source_surface(bcr, tmp_buffer, 0, 0); |
| 103 | cairo_rectangle(bcr, 0, 0, |
| 104 | allocation->width, allocation->height); |
| 105 | cairo_clip(bcr); |
| 106 | cairo_paint(bcr); |
| 107 | |
| 108 | cairo_surface_destroy(tmp_buffer); |
| 109 | } |
| 110 | |
| 111 | if (clickdot->line.x != -1 && clickdot->line.y != -1) { |
| 112 | if (clickdot->line.old_x != -1 && |
| 113 | clickdot->line.old_y != -1) { |
| 114 | cairo_set_line_width(bcr, 2.0); |
| 115 | cairo_set_source_rgb(bcr, 1, 1, 1); |
| 116 | cairo_translate(bcr, |
| 117 | -allocation->x, -allocation->y); |
| 118 | |
| 119 | cairo_move_to(bcr, |
| 120 | clickdot->line.old_x, |
| 121 | clickdot->line.old_y); |
| 122 | cairo_line_to(bcr, |
| 123 | clickdot->line.x, |
| 124 | clickdot->line.y); |
| 125 | |
| 126 | cairo_stroke(bcr); |
| 127 | } |
| 128 | |
| 129 | clickdot->line.old_x = clickdot->line.x; |
| 130 | clickdot->line.old_y = clickdot->line.y; |
| 131 | } |
| 132 | cairo_destroy(bcr); |
| 133 | |
| 134 | cairo_set_source_surface(cr, clickdot->buffer, |
| 135 | allocation->x, allocation->y); |
| 136 | cairo_set_operator(cr, CAIRO_OPERATOR_ADD); |
| 137 | cairo_rectangle(cr, |
| 138 | allocation->x, allocation->y, |
| 139 | allocation->width, allocation->height); |
| 140 | cairo_clip(cr); |
| 141 | cairo_paint(cr); |
| 142 | } |
| 143 | |
| 144 | static void |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 145 | redraw_handler(struct widget *widget, void *data) |
| 146 | { |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 147 | static const double r = 10.0; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 148 | struct clickdot *clickdot = data; |
| 149 | cairo_surface_t *surface; |
| 150 | cairo_t *cr; |
| 151 | struct rectangle allocation; |
| 152 | |
| 153 | widget_get_allocation(clickdot->widget, &allocation); |
| 154 | |
| 155 | surface = window_get_surface(clickdot->window); |
| 156 | |
| 157 | cr = cairo_create(surface); |
| 158 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 159 | cairo_rectangle(cr, |
| 160 | allocation.x, |
| 161 | allocation.y, |
| 162 | allocation.width, |
| 163 | allocation.height); |
| 164 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 165 | cairo_fill(cr); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 166 | |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 167 | draw_line(clickdot, cr, &allocation); |
| 168 | |
| 169 | cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 170 | cairo_set_line_width(cr, 1.0); |
| 171 | cairo_set_source_rgb(cr, 0.1, 0.9, 0.9); |
| 172 | cairo_move_to(cr, 0.0, -r); |
| 173 | cairo_line_to(cr, 0.0, r); |
| 174 | cairo_move_to(cr, -r, 0.0); |
| 175 | cairo_line_to(cr, r, 0.0); |
| 176 | cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI); |
| 177 | cairo_stroke(cr); |
| 178 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 179 | cairo_destroy(cr); |
| 180 | |
| 181 | cairo_surface_destroy(surface); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void |
| 185 | keyboard_focus_handler(struct window *window, |
| 186 | struct input *device, void *data) |
| 187 | { |
| 188 | struct clickdot *clickdot = data; |
| 189 | |
| 190 | window_schedule_redraw(clickdot->window); |
| 191 | } |
| 192 | |
| 193 | static void |
| 194 | key_handler(struct window *window, struct input *input, uint32_t time, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 195 | uint32_t key, uint32_t sym, |
| 196 | enum wl_keyboard_key_state state, void *data) |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 197 | { |
| 198 | struct clickdot *clickdot = data; |
| 199 | |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 200 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 201 | return; |
| 202 | |
| 203 | switch (sym) { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 204 | case XKB_KEY_Escape: |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 205 | display_exit(clickdot->display); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 211 | button_handler(struct widget *widget, |
| 212 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 213 | uint32_t button, |
| 214 | enum wl_pointer_button_state state, void *data) |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 215 | { |
| 216 | struct clickdot *clickdot = data; |
| 217 | |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 218 | if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT) |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 219 | input_get_position(input, &clickdot->dot.x, &clickdot->dot.y); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 220 | |
| 221 | widget_schedule_redraw(widget); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 224 | static void |
| 225 | cursor_timeout_reset(struct clickdot *clickdot) |
| 226 | { |
| 227 | const long cursor_timeout = 500; |
| 228 | struct itimerspec its; |
| 229 | |
| 230 | its.it_interval.tv_sec = 0; |
| 231 | its.it_interval.tv_nsec = 0; |
| 232 | its.it_value.tv_sec = cursor_timeout / 1000; |
| 233 | its.it_value.tv_nsec = (cursor_timeout % 1000) * 1000 * 1000; |
| 234 | timerfd_settime(clickdot->cursor_timeout_fd, 0, &its, NULL); |
| 235 | } |
| 236 | |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 237 | static int |
| 238 | motion_handler(struct widget *widget, |
| 239 | struct input *input, uint32_t time, |
| 240 | float x, float y, void *data) |
| 241 | { |
| 242 | struct clickdot *clickdot = data; |
| 243 | clickdot->line.x = x; |
| 244 | clickdot->line.y = y; |
| 245 | |
| 246 | window_schedule_redraw(clickdot->window); |
| 247 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 248 | cursor_timeout_reset(clickdot); |
| 249 | clickdot->cursor_timeout_input = input; |
| 250 | |
| 251 | return CURSOR_BLANK; |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void |
| 255 | resize_handler(struct widget *widget, |
| 256 | int32_t width, int32_t height, |
| 257 | void *data) |
| 258 | { |
| 259 | struct clickdot *clickdot = data; |
| 260 | |
| 261 | clickdot->reset = 1; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | leave_handler(struct widget *widget, |
| 266 | struct input *input, void *data) |
| 267 | { |
| 268 | struct clickdot *clickdot = data; |
| 269 | |
| 270 | clickdot->reset = 1; |
| 271 | } |
| 272 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 273 | static void |
| 274 | cursor_timeout_func(struct task *task, uint32_t events) |
| 275 | { |
| 276 | struct clickdot *clickdot = |
| 277 | container_of(task, struct clickdot, cursor_timeout_task); |
| 278 | uint64_t exp; |
| 279 | |
| 280 | if (read(clickdot->cursor_timeout_fd, &exp, sizeof (uint64_t)) != |
| 281 | sizeof(uint64_t)) |
| 282 | abort(); |
| 283 | |
| 284 | input_set_pointer_image(clickdot->cursor_timeout_input, |
| 285 | CURSOR_LEFT_PTR); |
| 286 | } |
| 287 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 288 | static struct clickdot * |
| 289 | clickdot_create(struct display *display) |
| 290 | { |
| 291 | struct clickdot *clickdot; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 292 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 293 | clickdot = xzalloc(sizeof *clickdot); |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 294 | clickdot->window = window_create(display); |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 295 | clickdot->widget = window_frame_create(clickdot->window, clickdot); |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 296 | window_set_title(clickdot->window, "Wayland ClickDot"); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 297 | clickdot->display = display; |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 298 | clickdot->buffer = NULL; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 299 | |
| 300 | window_set_key_handler(clickdot->window, key_handler); |
| 301 | window_set_user_data(clickdot->window, clickdot); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 302 | window_set_keyboard_focus_handler(clickdot->window, |
| 303 | keyboard_focus_handler); |
| 304 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 305 | widget_set_redraw_handler(clickdot->widget, redraw_handler); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 306 | widget_set_button_handler(clickdot->widget, button_handler); |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 307 | widget_set_motion_handler(clickdot->widget, motion_handler); |
| 308 | widget_set_resize_handler(clickdot->widget, resize_handler); |
| 309 | widget_set_leave_handler(clickdot->widget, leave_handler); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 310 | |
Pekka Paalanen | b13e84f | 2012-01-20 13:04:56 +0200 | [diff] [blame] | 311 | widget_schedule_resize(clickdot->widget, 500, 400); |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 312 | clickdot->dot.x = 250; |
| 313 | clickdot->dot.y = 200; |
| 314 | clickdot->line.x = -1; |
| 315 | clickdot->line.y = -1; |
| 316 | clickdot->line.old_x = -1; |
| 317 | clickdot->line.old_y = -1; |
| 318 | clickdot->reset = 0; |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 319 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 320 | clickdot->cursor_timeout_fd = |
| 321 | timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); |
| 322 | clickdot->cursor_timeout_task.run = cursor_timeout_func; |
| 323 | display_watch_fd(window_get_display(clickdot->window), |
| 324 | clickdot->cursor_timeout_fd, |
| 325 | EPOLLIN, &clickdot->cursor_timeout_task); |
| 326 | |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 327 | return clickdot; |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | clickdot_destroy(struct clickdot *clickdot) |
| 332 | { |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 333 | display_unwatch_fd(window_get_display(clickdot->window), |
| 334 | clickdot->cursor_timeout_fd); |
| 335 | close(clickdot->cursor_timeout_fd); |
Jonas Ådahl | df21183 | 2012-05-10 23:26:25 +0200 | [diff] [blame] | 336 | if (clickdot->buffer) |
| 337 | cairo_surface_destroy(clickdot->buffer); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 338 | widget_destroy(clickdot->widget); |
| 339 | window_destroy(clickdot->window); |
| 340 | free(clickdot); |
| 341 | } |
| 342 | |
| 343 | int |
| 344 | main(int argc, char *argv[]) |
| 345 | { |
| 346 | struct display *display; |
| 347 | struct clickdot *clickdot; |
| 348 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 349 | display = display_create(&argc, argv); |
Pekka Paalanen | 6920190 | 2012-01-20 11:09:13 +0200 | [diff] [blame] | 350 | if (display == NULL) { |
| 351 | fprintf(stderr, "failed to create display: %m\n"); |
| 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | clickdot = clickdot_create(display); |
| 356 | |
| 357 | display_run(display); |
| 358 | |
| 359 | clickdot_destroy(clickdot); |
| 360 | display_destroy(display); |
| 361 | |
| 362 | return 0; |
| 363 | } |