blob: 776f8da84e06fa8ab3d3b184e05ce49ab9db776b [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"
Pekka Paalanen69201902012-01-20 11:09:13 +020044
Pekka Paalanen69201902012-01-20 11:09:13 +020045struct clickdot {
46 struct display *display;
47 struct window *window;
48 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020049
Jonas Ådahldf211832012-05-10 23:26:25 +020050 cairo_surface_t *buffer;
51
52 struct {
53 int32_t x, y;
54 } dot;
55
56 struct {
57 int32_t x, y;
58 int32_t old_x, old_y;
59 } line;
60
61 int reset;
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +020062
63 struct input *cursor_timeout_input;
64 int cursor_timeout_fd;
65 struct task cursor_timeout_task;
Pekka Paalanen69201902012-01-20 11:09:13 +020066};
67
68static void
Jonas Ådahldf211832012-05-10 23:26:25 +020069draw_line(struct clickdot *clickdot, cairo_t *cr,
70 struct rectangle *allocation)
71{
72 cairo_t *bcr;
73 cairo_surface_t *tmp_buffer = NULL;
74
75 if (clickdot->reset) {
76 tmp_buffer = clickdot->buffer;
77 clickdot->buffer = NULL;
78 clickdot->line.x = -1;
79 clickdot->line.y = -1;
80 clickdot->line.old_x = -1;
81 clickdot->line.old_y = -1;
82 clickdot->reset = 0;
83 }
84
85 if (clickdot->buffer == NULL) {
86 clickdot->buffer =
87 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
88 allocation->width,
89 allocation->height);
90 bcr = cairo_create(clickdot->buffer);
91 cairo_set_source_rgba(bcr, 0, 0, 0, 0);
92 cairo_rectangle(bcr,
93 0, 0,
94 allocation->width, allocation->height);
95 cairo_fill(bcr);
96 }
97 else
98 bcr = cairo_create(clickdot->buffer);
99
100 if (tmp_buffer) {
101 cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
102 cairo_rectangle(bcr, 0, 0,
103 allocation->width, allocation->height);
104 cairo_clip(bcr);
105 cairo_paint(bcr);
106
107 cairo_surface_destroy(tmp_buffer);
108 }
109
110 if (clickdot->line.x != -1 && clickdot->line.y != -1) {
111 if (clickdot->line.old_x != -1 &&
112 clickdot->line.old_y != -1) {
113 cairo_set_line_width(bcr, 2.0);
114 cairo_set_source_rgb(bcr, 1, 1, 1);
115 cairo_translate(bcr,
116 -allocation->x, -allocation->y);
117
118 cairo_move_to(bcr,
119 clickdot->line.old_x,
120 clickdot->line.old_y);
121 cairo_line_to(bcr,
122 clickdot->line.x,
123 clickdot->line.y);
124
125 cairo_stroke(bcr);
126 }
127
128 clickdot->line.old_x = clickdot->line.x;
129 clickdot->line.old_y = clickdot->line.y;
130 }
131 cairo_destroy(bcr);
132
133 cairo_set_source_surface(cr, clickdot->buffer,
134 allocation->x, allocation->y);
135 cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
136 cairo_rectangle(cr,
137 allocation->x, allocation->y,
138 allocation->width, allocation->height);
139 cairo_clip(cr);
140 cairo_paint(cr);
141}
142
143static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200144redraw_handler(struct widget *widget, void *data)
145{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200146 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200147 struct clickdot *clickdot = data;
148 cairo_surface_t *surface;
149 cairo_t *cr;
150 struct rectangle allocation;
151
152 widget_get_allocation(clickdot->widget, &allocation);
153
154 surface = window_get_surface(clickdot->window);
155
156 cr = cairo_create(surface);
157 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
158 cairo_rectangle(cr,
159 allocation.x,
160 allocation.y,
161 allocation.width,
162 allocation.height);
163 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
164 cairo_fill(cr);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200165
Jonas Ådahldf211832012-05-10 23:26:25 +0200166 draw_line(clickdot, cr, &allocation);
167
168 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200169 cairo_set_line_width(cr, 1.0);
170 cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
171 cairo_move_to(cr, 0.0, -r);
172 cairo_line_to(cr, 0.0, r);
173 cairo_move_to(cr, -r, 0.0);
174 cairo_line_to(cr, r, 0.0);
175 cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
176 cairo_stroke(cr);
177
Pekka Paalanen69201902012-01-20 11:09:13 +0200178 cairo_destroy(cr);
179
180 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +0200181}
182
183static void
184keyboard_focus_handler(struct window *window,
185 struct input *device, void *data)
186{
187 struct clickdot *clickdot = data;
188
189 window_schedule_redraw(clickdot->window);
190}
191
192static void
193key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100194 uint32_t key, uint32_t sym,
195 enum wl_keyboard_key_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200196{
197 struct clickdot *clickdot = data;
198
Daniel Stonec9785ea2012-05-30 16:31:52 +0100199 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Pekka Paalanen69201902012-01-20 11:09:13 +0200200 return;
201
202 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400203 case XKB_KEY_Escape:
Pekka Paalanen69201902012-01-20 11:09:13 +0200204 display_exit(clickdot->display);
205 break;
206 }
207}
208
209static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200210button_handler(struct widget *widget,
211 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100212 uint32_t button,
213 enum wl_pointer_button_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200214{
215 struct clickdot *clickdot = data;
216
Daniel Stone4dbadb12012-05-30 16:31:51 +0100217 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
Jonas Ådahldf211832012-05-10 23:26:25 +0200218 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200219
220 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200221}
222
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200223static void
224cursor_timeout_reset(struct clickdot *clickdot)
225{
226 const long cursor_timeout = 500;
227 struct itimerspec its;
228
229 its.it_interval.tv_sec = 0;
230 its.it_interval.tv_nsec = 0;
231 its.it_value.tv_sec = cursor_timeout / 1000;
232 its.it_value.tv_nsec = (cursor_timeout % 1000) * 1000 * 1000;
233 timerfd_settime(clickdot->cursor_timeout_fd, 0, &its, NULL);
234}
235
Jonas Ådahldf211832012-05-10 23:26:25 +0200236static int
237motion_handler(struct widget *widget,
238 struct input *input, uint32_t time,
239 float x, float y, void *data)
240{
241 struct clickdot *clickdot = data;
242 clickdot->line.x = x;
243 clickdot->line.y = y;
244
245 window_schedule_redraw(clickdot->window);
246
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200247 cursor_timeout_reset(clickdot);
248 clickdot->cursor_timeout_input = input;
249
250 return CURSOR_BLANK;
Jonas Ådahldf211832012-05-10 23:26:25 +0200251}
252
253static void
254resize_handler(struct widget *widget,
255 int32_t width, int32_t height,
256 void *data)
257{
258 struct clickdot *clickdot = data;
259
260 clickdot->reset = 1;
261}
262
263static void
264leave_handler(struct widget *widget,
265 struct input *input, void *data)
266{
267 struct clickdot *clickdot = data;
268
269 clickdot->reset = 1;
270}
271
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200272static void
273cursor_timeout_func(struct task *task, uint32_t events)
274{
275 struct clickdot *clickdot =
276 container_of(task, struct clickdot, cursor_timeout_task);
277 uint64_t exp;
278
279 if (read(clickdot->cursor_timeout_fd, &exp, sizeof (uint64_t)) !=
280 sizeof(uint64_t))
281 abort();
282
283 input_set_pointer_image(clickdot->cursor_timeout_input,
284 CURSOR_LEFT_PTR);
285}
286
Pekka Paalanen69201902012-01-20 11:09:13 +0200287static struct clickdot *
288clickdot_create(struct display *display)
289{
290 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200291
Peter Huttererf3d62272013-08-08 11:57:05 +1000292 clickdot = xzalloc(sizeof *clickdot);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500293 clickdot->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500294 clickdot->widget = window_frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200295 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200296 clickdot->display = display;
Jonas Ådahldf211832012-05-10 23:26:25 +0200297 clickdot->buffer = NULL;
Pekka Paalanen69201902012-01-20 11:09:13 +0200298
299 window_set_key_handler(clickdot->window, key_handler);
300 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200301 window_set_keyboard_focus_handler(clickdot->window,
302 keyboard_focus_handler);
303
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200304 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200305 widget_set_button_handler(clickdot->widget, button_handler);
Jonas Ådahldf211832012-05-10 23:26:25 +0200306 widget_set_motion_handler(clickdot->widget, motion_handler);
307 widget_set_resize_handler(clickdot->widget, resize_handler);
308 widget_set_leave_handler(clickdot->widget, leave_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200309
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200310 widget_schedule_resize(clickdot->widget, 500, 400);
Jonas Ådahldf211832012-05-10 23:26:25 +0200311 clickdot->dot.x = 250;
312 clickdot->dot.y = 200;
313 clickdot->line.x = -1;
314 clickdot->line.y = -1;
315 clickdot->line.old_x = -1;
316 clickdot->line.old_y = -1;
317 clickdot->reset = 0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200318
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200319 clickdot->cursor_timeout_fd =
320 timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
321 clickdot->cursor_timeout_task.run = cursor_timeout_func;
322 display_watch_fd(window_get_display(clickdot->window),
323 clickdot->cursor_timeout_fd,
324 EPOLLIN, &clickdot->cursor_timeout_task);
325
Pekka Paalanen69201902012-01-20 11:09:13 +0200326 return clickdot;
327}
328
329static void
330clickdot_destroy(struct clickdot *clickdot)
331{
Jonas Ådahl16fe4dc2014-09-08 19:33:41 +0200332 display_unwatch_fd(window_get_display(clickdot->window),
333 clickdot->cursor_timeout_fd);
334 close(clickdot->cursor_timeout_fd);
Jonas Ådahldf211832012-05-10 23:26:25 +0200335 if (clickdot->buffer)
336 cairo_surface_destroy(clickdot->buffer);
Pekka Paalanen69201902012-01-20 11:09:13 +0200337 widget_destroy(clickdot->widget);
338 window_destroy(clickdot->window);
339 free(clickdot);
340}
341
342int
343main(int argc, char *argv[])
344{
345 struct display *display;
346 struct clickdot *clickdot;
347
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500348 display = display_create(&argc, argv);
Pekka Paalanen69201902012-01-20 11:09:13 +0200349 if (display == NULL) {
350 fprintf(stderr, "failed to create display: %m\n");
351 return -1;
352 }
353
354 clickdot = clickdot_create(display);
355
356 display_run(display);
357
358 clickdot_destroy(clickdot);
359 display_destroy(display);
360
361 return 0;
362}