1 /*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
4 * Copyright © 2012 Jonas Ådahl
5 *
6 * 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:
12 *
13 * 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.
24 */
25
26 #include "config.h"
27
28 #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>
35 #include <unistd.h>
36 #include <time.h>
37 #include <errno.h>
38
39 #include <linux/input.h>
40 #include <wayland-client.h>
41
42 #include "window.h"
43 #include "shared/helpers.h"
44 #include "shared/xalloc.h"
45
46 struct clickdot {
47 struct display *display;
48 struct window *window;
49 struct widget *widget;
50
51 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;
63
64 struct input *cursor_timeout_input;
65 struct toytimer cursor_timeout;
66 };
67
68 static void
draw_line(struct clickdot * clickdot,cairo_t * cr,struct rectangle * allocation)69 draw_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
143 static void
redraw_handler(struct widget * widget,void * data)144 redraw_handler(struct widget *widget, void *data)
145 {
146 static const double r = 10.0;
147 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);
165
166 draw_line(clickdot, cr, &allocation);
167
168 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
169 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
178 cairo_destroy(cr);
179
180 cairo_surface_destroy(surface);
181 }
182
183 static void
keyboard_focus_handler(struct window * window,struct input * device,void * data)184 keyboard_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
192 static void
key_handler(struct window * window,struct input * input,uint32_t time,uint32_t key,uint32_t sym,enum wl_keyboard_key_state state,void * data)193 key_handler(struct window *window, struct input *input, uint32_t time,
194 uint32_t key, uint32_t sym,
195 enum wl_keyboard_key_state state, void *data)
196 {
197 struct clickdot *clickdot = data;
198
199 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
200 return;
201
202 switch (sym) {
203 case XKB_KEY_Escape:
204 display_exit(clickdot->display);
205 break;
206 }
207 }
208
209 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)210 button_handler(struct widget *widget,
211 struct input *input, uint32_t time,
212 uint32_t button,
213 enum wl_pointer_button_state state, void *data)
214 {
215 struct clickdot *clickdot = data;
216
217 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
218 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
219
220 widget_schedule_redraw(widget);
221 }
222
223 static void
cursor_timeout_reset(struct clickdot * clickdot)224 cursor_timeout_reset(struct clickdot *clickdot)
225 {
226 toytimer_arm_once_usec(&clickdot->cursor_timeout, 500 * 1000);
227 }
228
229 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)230 motion_handler(struct widget *widget,
231 struct input *input, uint32_t time,
232 float x, float y, void *data)
233 {
234 struct clickdot *clickdot = data;
235 clickdot->line.x = x;
236 clickdot->line.y = y;
237
238 window_schedule_redraw(clickdot->window);
239
240 cursor_timeout_reset(clickdot);
241 clickdot->cursor_timeout_input = input;
242
243 return CURSOR_BLANK;
244 }
245
246 static void
resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)247 resize_handler(struct widget *widget,
248 int32_t width, int32_t height,
249 void *data)
250 {
251 struct clickdot *clickdot = data;
252
253 clickdot->reset = 1;
254 }
255
256 static void
leave_handler(struct widget * widget,struct input * input,void * data)257 leave_handler(struct widget *widget,
258 struct input *input, void *data)
259 {
260 struct clickdot *clickdot = data;
261
262 clickdot->reset = 1;
263 }
264
265 static void
cursor_timeout_func(struct toytimer * tt)266 cursor_timeout_func(struct toytimer *tt)
267 {
268 struct clickdot *clickdot =
269 container_of(tt, struct clickdot, cursor_timeout);
270
271 input_set_pointer_image(clickdot->cursor_timeout_input,
272 CURSOR_LEFT_PTR);
273 }
274
275 static struct clickdot *
clickdot_create(struct display * display)276 clickdot_create(struct display *display)
277 {
278 struct clickdot *clickdot;
279
280 clickdot = xzalloc(sizeof *clickdot);
281 clickdot->window = window_create(display);
282 clickdot->widget = window_frame_create(clickdot->window, clickdot);
283 window_set_title(clickdot->window, "Wayland ClickDot");
284 clickdot->display = display;
285 clickdot->buffer = NULL;
286
287 window_set_key_handler(clickdot->window, key_handler);
288 window_set_user_data(clickdot->window, clickdot);
289 window_set_keyboard_focus_handler(clickdot->window,
290 keyboard_focus_handler);
291
292 widget_set_redraw_handler(clickdot->widget, redraw_handler);
293 widget_set_button_handler(clickdot->widget, button_handler);
294 widget_set_motion_handler(clickdot->widget, motion_handler);
295 widget_set_resize_handler(clickdot->widget, resize_handler);
296 widget_set_leave_handler(clickdot->widget, leave_handler);
297
298 widget_schedule_resize(clickdot->widget, 500, 400);
299 clickdot->dot.x = 250;
300 clickdot->dot.y = 200;
301 clickdot->line.x = -1;
302 clickdot->line.y = -1;
303 clickdot->line.old_x = -1;
304 clickdot->line.old_y = -1;
305 clickdot->reset = 0;
306
307 toytimer_init(&clickdot->cursor_timeout, CLOCK_MONOTONIC,
308 display, cursor_timeout_func);
309
310 return clickdot;
311 }
312
313 static void
clickdot_destroy(struct clickdot * clickdot)314 clickdot_destroy(struct clickdot *clickdot)
315 {
316 toytimer_fini(&clickdot->cursor_timeout);
317 if (clickdot->buffer)
318 cairo_surface_destroy(clickdot->buffer);
319 widget_destroy(clickdot->widget);
320 window_destroy(clickdot->window);
321 free(clickdot);
322 }
323
324 int
main(int argc,char * argv[])325 main(int argc, char *argv[])
326 {
327 struct display *display;
328 struct clickdot *clickdot;
329
330 display = display_create(&argc, argv);
331 if (display == NULL) {
332 fprintf(stderr, "failed to create display: %s\n",
333 strerror(errno));
334 return -1;
335 }
336
337 clickdot = clickdot_create(display);
338
339 display_run(display);
340
341 clickdot_destroy(clickdot);
342 display_destroy(display);
343
344 return 0;
345 }
346