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