blob: b5229742fc8982432920ba2376bf4f1ffa3d84a6 [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"
43
Pekka Paalanen69201902012-01-20 11:09:13 +020044struct clickdot {
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020048
Jonas Ådahldf211832012-05-10 23:26:25 +020049 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 Ådahl16fe4dc2014-09-08 19:33:41 +020061
62 struct input *cursor_timeout_input;
63 int cursor_timeout_fd;
64 struct task cursor_timeout_task;
Pekka Paalanen69201902012-01-20 11:09:13 +020065};
66
67static void
Jonas Ådahldf211832012-05-10 23:26:25 +020068draw_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
142static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200143redraw_handler(struct widget *widget, void *data)
144{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200145 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200146 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 Paalanenb13e84f2012-01-20 13:04:56 +0200164
Jonas Ådahldf211832012-05-10 23:26:25 +0200165 draw_line(clickdot, cr, &allocation);
166
167 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200168 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 Paalanen69201902012-01-20 11:09:13 +0200177 cairo_destroy(cr);
178
179 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +0200180}
181
182static void
183keyboard_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
191static void
192key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100193 uint32_t key, uint32_t sym,
194 enum wl_keyboard_key_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200195{
196 struct clickdot *clickdot = data;
197
Daniel Stonec9785ea2012-05-30 16:31:52 +0100198 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen69201902012-01-20 11:09:13 +0200199 return;
200
201 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400202 case XKB_KEY_Escape:
Pekka Paalanen69201902012-01-20 11:09:13 +0200203 display_exit(clickdot->display);
204 break;
205 }
206}
207
208static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200209button_handler(struct widget *widget,
210 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100211 uint32_t button,
212 enum wl_pointer_button_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200213{
214 struct clickdot *clickdot = data;
215
Daniel Stone4dbadb12012-05-30 16:31:51 +0100216 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
Jonas Ådahldf211832012-05-10 23:26:25 +0200217 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200218
219 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200220}
221
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200222static void
223cursor_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 Ådahldf211832012-05-10 23:26:25 +0200235static int
236motion_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 Ådahl16fe4dc2014-09-08 19:33:41 +0200246 cursor_timeout_reset(clickdot);
247 clickdot->cursor_timeout_input = input;
248
249 return CURSOR_BLANK;
Jonas Ådahldf211832012-05-10 23:26:25 +0200250}
251
252static void
253resize_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
262static void
263leave_handler(struct widget *widget,
264 struct input *input, void *data)
265{
266 struct clickdot *clickdot = data;
267
268 clickdot->reset = 1;
269}
270
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200271static void
272cursor_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 Paalanen69201902012-01-20 11:09:13 +0200286static struct clickdot *
287clickdot_create(struct display *display)
288{
289 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200290
Peter Huttererf3d62272013-08-08 11:57:05 +1000291 clickdot = xzalloc(sizeof *clickdot);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500292 clickdot->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500293 clickdot->widget = window_frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200294 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200295 clickdot->display = display;
Jonas Ådahldf211832012-05-10 23:26:25 +0200296 clickdot->buffer = NULL;
Pekka Paalanen69201902012-01-20 11:09:13 +0200297
298 window_set_key_handler(clickdot->window, key_handler);
299 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200300 window_set_keyboard_focus_handler(clickdot->window,
301 keyboard_focus_handler);
302
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200303 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200304 widget_set_button_handler(clickdot->widget, button_handler);
Jonas Ådahldf211832012-05-10 23:26:25 +0200305 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 Paalanen69201902012-01-20 11:09:13 +0200308
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200309 widget_schedule_resize(clickdot->widget, 500, 400);
Jonas Ådahldf211832012-05-10 23:26:25 +0200310 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 Paalanen69201902012-01-20 11:09:13 +0200317
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200318 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 Paalanen69201902012-01-20 11:09:13 +0200325 return clickdot;
326}
327
328static void
329clickdot_destroy(struct clickdot *clickdot)
330{
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200331 display_unwatch_fd(window_get_display(clickdot->window),
332 clickdot->cursor_timeout_fd);
333 close(clickdot->cursor_timeout_fd);
Jonas Ådahldf211832012-05-10 23:26:25 +0200334 if (clickdot->buffer)
335 cairo_surface_destroy(clickdot->buffer);
Pekka Paalanen69201902012-01-20 11:09:13 +0200336 widget_destroy(clickdot->widget);
337 window_destroy(clickdot->window);
338 free(clickdot);
339}
340
341int
342main(int argc, char *argv[])
343{
344 struct display *display;
345 struct clickdot *clickdot;
346
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500347 display = display_create(&argc, argv);
Pekka Paalanen69201902012-01-20 11:09:13 +0200348 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}