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