blob: f52fbf04880c5e734fe27103b9dac1753d60d89d [file] [log] [blame]
Pekka Paalanen69201902012-01-20 11:09:13 +02001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanenb13e84f2012-01-20 13:04:56 +02003 * Copyright © 2012 Collabora, Ltd.
Jonas Ådahldf211832012-05-10 23:26:25 +02004 * Copyright © 2012 Jonas Ådahl
Pekka Paalanen69201902012-01-20 11:09:13 +02005 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07006 * 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 Paalanen69201902012-01-20 11:09:13 +020012 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070013 * 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 Paalanen69201902012-01-20 11:09:13 +020024 */
25
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010026#include "config.h"
27
Pekka Paalanen69201902012-01-20 11:09:13 +020028#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 Ådahl16fe4dc2014-09-08 19:33:41 +020035#include <sys/timerfd.h>
36#include <sys/epoll.h>
37#include <unistd.h>
Pekka Paalanen69201902012-01-20 11:09:13 +020038
39#include <linux/input.h>
40#include <wayland-client.h>
41
42#include "window.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070043#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070044#include "shared/xalloc.h"
Pekka Paalanen69201902012-01-20 11:09:13 +020045
Pekka Paalanen69201902012-01-20 11:09:13 +020046struct clickdot {
47 struct display *display;
48 struct window *window;
49 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020050
Jonas Ådahldf211832012-05-10 23:26:25 +020051 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 Ådahl16fe4dc2014-09-08 19:33:41 +020063
64 struct input *cursor_timeout_input;
65 int cursor_timeout_fd;
66 struct task cursor_timeout_task;
Pekka Paalanen69201902012-01-20 11:09:13 +020067};
68
69static void
Jonas Ådahldf211832012-05-10 23:26:25 +020070draw_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
144static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200145redraw_handler(struct widget *widget, void *data)
146{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200147 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200148 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 Paalanenb13e84f2012-01-20 13:04:56 +0200166
Jonas Ådahldf211832012-05-10 23:26:25 +0200167 draw_line(clickdot, cr, &allocation);
168
169 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200170 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 Paalanen69201902012-01-20 11:09:13 +0200179 cairo_destroy(cr);
180
181 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +0200182}
183
184static void
185keyboard_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
193static void
194key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100195 uint32_t key, uint32_t sym,
196 enum wl_keyboard_key_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200197{
198 struct clickdot *clickdot = data;
199
Daniel Stonec9785ea2012-05-30 16:31:52 +0100200 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen69201902012-01-20 11:09:13 +0200201 return;
202
203 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400204 case XKB_KEY_Escape:
Pekka Paalanen69201902012-01-20 11:09:13 +0200205 display_exit(clickdot->display);
206 break;
207 }
208}
209
210static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200211button_handler(struct widget *widget,
212 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100213 uint32_t button,
214 enum wl_pointer_button_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200215{
216 struct clickdot *clickdot = data;
217
Daniel Stone4dbadb12012-05-30 16:31:51 +0100218 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
Jonas Ådahldf211832012-05-10 23:26:25 +0200219 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200220
221 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200222}
223
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200224static void
225cursor_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 Ådahldf211832012-05-10 23:26:25 +0200237static int
238motion_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 Ådahl16fe4dc2014-09-08 19:33:41 +0200248 cursor_timeout_reset(clickdot);
249 clickdot->cursor_timeout_input = input;
250
251 return CURSOR_BLANK;
Jonas Ådahldf211832012-05-10 23:26:25 +0200252}
253
254static void
255resize_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
264static void
265leave_handler(struct widget *widget,
266 struct input *input, void *data)
267{
268 struct clickdot *clickdot = data;
269
270 clickdot->reset = 1;
271}
272
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200273static void
274cursor_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 Paalanen69201902012-01-20 11:09:13 +0200288static struct clickdot *
289clickdot_create(struct display *display)
290{
291 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200292
Peter Huttererf3d62272013-08-08 11:57:05 +1000293 clickdot = xzalloc(sizeof *clickdot);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500294 clickdot->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500295 clickdot->widget = window_frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200296 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200297 clickdot->display = display;
Jonas Ådahldf211832012-05-10 23:26:25 +0200298 clickdot->buffer = NULL;
Pekka Paalanen69201902012-01-20 11:09:13 +0200299
300 window_set_key_handler(clickdot->window, key_handler);
301 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200302 window_set_keyboard_focus_handler(clickdot->window,
303 keyboard_focus_handler);
304
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200305 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200306 widget_set_button_handler(clickdot->widget, button_handler);
Jonas Ådahldf211832012-05-10 23:26:25 +0200307 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 Paalanen69201902012-01-20 11:09:13 +0200310
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200311 widget_schedule_resize(clickdot->widget, 500, 400);
Jonas Ådahldf211832012-05-10 23:26:25 +0200312 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 Paalanen69201902012-01-20 11:09:13 +0200319
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200320 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 Paalanen69201902012-01-20 11:09:13 +0200327 return clickdot;
328}
329
330static void
331clickdot_destroy(struct clickdot *clickdot)
332{
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200333 display_unwatch_fd(window_get_display(clickdot->window),
334 clickdot->cursor_timeout_fd);
335 close(clickdot->cursor_timeout_fd);
Jonas Ådahldf211832012-05-10 23:26:25 +0200336 if (clickdot->buffer)
337 cairo_surface_destroy(clickdot->buffer);
Pekka Paalanen69201902012-01-20 11:09:13 +0200338 widget_destroy(clickdot->widget);
339 window_destroy(clickdot->window);
340 free(clickdot);
341}
342
343int
344main(int argc, char *argv[])
345{
346 struct display *display;
347 struct clickdot *clickdot;
348
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500349 display = display_create(&argc, argv);
Pekka Paalanen69201902012-01-20 11:09:13 +0200350 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}