2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
4 * Copyright © 2012 Jonas Ådahl
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.
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
34 #include <sys/timerfd.h>
35 #include <sys/epoll.h>
38 #include <linux/input.h>
39 #include <wayland-client.h>
44 struct display *display;
45 struct window *window;
46 struct widget *widget;
48 cairo_surface_t *buffer;
61 struct input *cursor_timeout_input;
62 int cursor_timeout_fd;
63 struct task cursor_timeout_task;
67 draw_line(struct clickdot *clickdot, cairo_t *cr,
68 struct rectangle *allocation)
71 cairo_surface_t *tmp_buffer = NULL;
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;
83 if (clickdot->buffer == NULL) {
85 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
88 bcr = cairo_create(clickdot->buffer);
89 cairo_set_source_rgba(bcr, 0, 0, 0, 0);
92 allocation->width, allocation->height);
96 bcr = cairo_create(clickdot->buffer);
99 cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
100 cairo_rectangle(bcr, 0, 0,
101 allocation->width, allocation->height);
105 cairo_surface_destroy(tmp_buffer);
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);
114 -allocation->x, -allocation->y);
117 clickdot->line.old_x,
118 clickdot->line.old_y);
126 clickdot->line.old_x = clickdot->line.x;
127 clickdot->line.old_y = clickdot->line.y;
131 cairo_set_source_surface(cr, clickdot->buffer,
132 allocation->x, allocation->y);
133 cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
135 allocation->x, allocation->y,
136 allocation->width, allocation->height);
142 redraw_handler(struct widget *widget, void *data)
144 static const double r = 10.0;
145 struct clickdot *clickdot = data;
146 cairo_surface_t *surface;
148 struct rectangle allocation;
150 widget_get_allocation(clickdot->widget, &allocation);
152 surface = window_get_surface(clickdot->window);
154 cr = cairo_create(surface);
155 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
161 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
164 draw_line(clickdot, cr, &allocation);
166 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
167 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);
178 cairo_surface_destroy(surface);
182 keyboard_focus_handler(struct window *window,
183 struct input *device, void *data)
185 struct clickdot *clickdot = data;
187 window_schedule_redraw(clickdot->window);
191 key_handler(struct window *window, struct input *input, uint32_t time,
192 uint32_t key, uint32_t sym,
193 enum wl_keyboard_key_state state, void *data)
195 struct clickdot *clickdot = data;
197 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
202 display_exit(clickdot->display);
208 button_handler(struct widget *widget,
209 struct input *input, uint32_t time,
211 enum wl_pointer_button_state state, void *data)
213 struct clickdot *clickdot = data;
215 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
216 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
218 widget_schedule_redraw(widget);
222 cursor_timeout_reset(struct clickdot *clickdot)
224 const long cursor_timeout = 500;
225 struct itimerspec its;
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);
235 motion_handler(struct widget *widget,
236 struct input *input, uint32_t time,
237 float x, float y, void *data)
239 struct clickdot *clickdot = data;
240 clickdot->line.x = x;
241 clickdot->line.y = y;
243 window_schedule_redraw(clickdot->window);
245 cursor_timeout_reset(clickdot);
246 clickdot->cursor_timeout_input = input;
252 resize_handler(struct widget *widget,
253 int32_t width, int32_t height,
256 struct clickdot *clickdot = data;
262 leave_handler(struct widget *widget,
263 struct input *input, void *data)
265 struct clickdot *clickdot = data;
271 cursor_timeout_func(struct task *task, uint32_t events)
273 struct clickdot *clickdot =
274 container_of(task, struct clickdot, cursor_timeout_task);
277 if (read(clickdot->cursor_timeout_fd, &exp, sizeof (uint64_t)) !=
281 input_set_pointer_image(clickdot->cursor_timeout_input,
285 static struct clickdot *
286 clickdot_create(struct display *display)
288 struct clickdot *clickdot;
290 clickdot = xzalloc(sizeof *clickdot);
291 clickdot->window = window_create(display);
292 clickdot->widget = window_frame_create(clickdot->window, clickdot);
293 window_set_title(clickdot->window, "Wayland ClickDot");
294 clickdot->display = display;
295 clickdot->buffer = NULL;
297 window_set_key_handler(clickdot->window, key_handler);
298 window_set_user_data(clickdot->window, clickdot);
299 window_set_keyboard_focus_handler(clickdot->window,
300 keyboard_focus_handler);
302 widget_set_redraw_handler(clickdot->widget, redraw_handler);
303 widget_set_button_handler(clickdot->widget, button_handler);
304 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);
308 widget_schedule_resize(clickdot->widget, 500, 400);
309 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;
317 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);
328 clickdot_destroy(struct clickdot *clickdot)
330 display_unwatch_fd(window_get_display(clickdot->window),
331 clickdot->cursor_timeout_fd);
332 close(clickdot->cursor_timeout_fd);
333 if (clickdot->buffer)
334 cairo_surface_destroy(clickdot->buffer);
335 widget_destroy(clickdot->widget);
336 window_destroy(clickdot->window);
341 main(int argc, char *argv[])
343 struct display *display;
344 struct clickdot *clickdot;
346 display = display_create(&argc, argv);
347 if (display == NULL) {
348 fprintf(stderr, "failed to create display: %m\n");
352 clickdot = clickdot_create(display);
354 display_run(display);
356 clickdot_destroy(clickdot);
357 display_destroy(display);