blob: 5137ba661222819913e11adc8a92b1949c384c3f [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 *
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 Wedgbury9cd661e2014-04-07 12:40:35 +010025#include "config.h"
26
Pekka Paalanen69201902012-01-20 11:09:13 +020027#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 Ådahl16fe4dc2014-09-08 19:33:41 +020034#include <sys/timerfd.h>
35#include <sys/epoll.h>
36#include <unistd.h>
Pekka Paalanen69201902012-01-20 11:09:13 +020037
38#include <linux/input.h>
39#include <wayland-client.h>
40
41#include "window.h"
42
Pekka Paalanen69201902012-01-20 11:09:13 +020043struct clickdot {
44 struct display *display;
45 struct window *window;
46 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020047
Jonas Ådahldf211832012-05-10 23:26:25 +020048 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 Ådahl16fe4dc2014-09-08 19:33:41 +020060
61 struct input *cursor_timeout_input;
62 int cursor_timeout_fd;
63 struct task cursor_timeout_task;
Pekka Paalanen69201902012-01-20 11:09:13 +020064};
65
66static void
Jonas Ådahldf211832012-05-10 23:26:25 +020067draw_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
141static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200142redraw_handler(struct widget *widget, void *data)
143{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200144 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200145 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 Paalanenb13e84f2012-01-20 13:04:56 +0200163
Jonas Ådahldf211832012-05-10 23:26:25 +0200164 draw_line(clickdot, cr, &allocation);
165
166 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200167 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 Paalanen69201902012-01-20 11:09:13 +0200176 cairo_destroy(cr);
177
178 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +0200179}
180
181static void
182keyboard_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
190static void
191key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100192 uint32_t key, uint32_t sym,
193 enum wl_keyboard_key_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200194{
195 struct clickdot *clickdot = data;
196
Daniel Stonec9785ea2012-05-30 16:31:52 +0100197 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen69201902012-01-20 11:09:13 +0200198 return;
199
200 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400201 case XKB_KEY_Escape:
Pekka Paalanen69201902012-01-20 11:09:13 +0200202 display_exit(clickdot->display);
203 break;
204 }
205}
206
207static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200208button_handler(struct widget *widget,
209 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100210 uint32_t button,
211 enum wl_pointer_button_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200212{
213 struct clickdot *clickdot = data;
214
Daniel Stone4dbadb12012-05-30 16:31:51 +0100215 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
Jonas Ådahldf211832012-05-10 23:26:25 +0200216 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200217
218 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200219}
220
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200221static void
222cursor_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 Ådahldf211832012-05-10 23:26:25 +0200234static int
235motion_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 Ådahl16fe4dc2014-09-08 19:33:41 +0200245 cursor_timeout_reset(clickdot);
246 clickdot->cursor_timeout_input = input;
247
248 return CURSOR_BLANK;
Jonas Ådahldf211832012-05-10 23:26:25 +0200249}
250
251static void
252resize_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
261static void
262leave_handler(struct widget *widget,
263 struct input *input, void *data)
264{
265 struct clickdot *clickdot = data;
266
267 clickdot->reset = 1;
268}
269
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200270static void
271cursor_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 Paalanen69201902012-01-20 11:09:13 +0200285static struct clickdot *
286clickdot_create(struct display *display)
287{
288 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200289
Peter Huttererf3d62272013-08-08 11:57:05 +1000290 clickdot = xzalloc(sizeof *clickdot);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500291 clickdot->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500292 clickdot->widget = window_frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200293 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200294 clickdot->display = display;
Jonas Ådahldf211832012-05-10 23:26:25 +0200295 clickdot->buffer = NULL;
Pekka Paalanen69201902012-01-20 11:09:13 +0200296
297 window_set_key_handler(clickdot->window, key_handler);
298 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200299 window_set_keyboard_focus_handler(clickdot->window,
300 keyboard_focus_handler);
301
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200302 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200303 widget_set_button_handler(clickdot->widget, button_handler);
Jonas Ådahldf211832012-05-10 23:26:25 +0200304 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 Paalanen69201902012-01-20 11:09:13 +0200307
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200308 widget_schedule_resize(clickdot->widget, 500, 400);
Jonas Ådahldf211832012-05-10 23:26:25 +0200309 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 Paalanen69201902012-01-20 11:09:13 +0200316
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200317 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 Paalanen69201902012-01-20 11:09:13 +0200324 return clickdot;
325}
326
327static void
328clickdot_destroy(struct clickdot *clickdot)
329{
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200330 display_unwatch_fd(window_get_display(clickdot->window),
331 clickdot->cursor_timeout_fd);
332 close(clickdot->cursor_timeout_fd);
Jonas Ådahldf211832012-05-10 23:26:25 +0200333 if (clickdot->buffer)
334 cairo_surface_destroy(clickdot->buffer);
Pekka Paalanen69201902012-01-20 11:09:13 +0200335 widget_destroy(clickdot->widget);
336 window_destroy(clickdot->window);
337 free(clickdot);
338}
339
340int
341main(int argc, char *argv[])
342{
343 struct display *display;
344 struct clickdot *clickdot;
345
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500346 display = display_create(&argc, argv);
Pekka Paalanen69201902012-01-20 11:09:13 +0200347 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}