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
33 #include <linux/input.h>
34 #include <wayland-client.h>
35 #include <wayland-cursor.h>
40 struct display *display;
41 struct window *window;
42 struct widget *widget;
44 cairo_surface_t *buffer;
59 draw_line(struct clickdot *clickdot, cairo_t *cr,
60 struct rectangle *allocation)
63 cairo_surface_t *tmp_buffer = NULL;
65 if (clickdot->reset) {
66 tmp_buffer = clickdot->buffer;
67 clickdot->buffer = NULL;
68 clickdot->line.x = -1;
69 clickdot->line.y = -1;
70 clickdot->line.old_x = -1;
71 clickdot->line.old_y = -1;
75 if (clickdot->buffer == NULL) {
77 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
80 bcr = cairo_create(clickdot->buffer);
81 cairo_set_source_rgba(bcr, 0, 0, 0, 0);
84 allocation->width, allocation->height);
88 bcr = cairo_create(clickdot->buffer);
91 cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
92 cairo_rectangle(bcr, 0, 0,
93 allocation->width, allocation->height);
97 cairo_surface_destroy(tmp_buffer);
100 if (clickdot->line.x != -1 && clickdot->line.y != -1) {
101 if (clickdot->line.old_x != -1 &&
102 clickdot->line.old_y != -1) {
103 cairo_set_line_width(bcr, 2.0);
104 cairo_set_source_rgb(bcr, 1, 1, 1);
106 -allocation->x, -allocation->y);
109 clickdot->line.old_x,
110 clickdot->line.old_y);
118 clickdot->line.old_x = clickdot->line.x;
119 clickdot->line.old_y = clickdot->line.y;
123 cairo_set_source_surface(cr, clickdot->buffer,
124 allocation->x, allocation->y);
125 cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
127 allocation->x, allocation->y,
128 allocation->width, allocation->height);
134 redraw_handler(struct widget *widget, void *data)
136 static const double r = 10.0;
137 struct clickdot *clickdot = data;
138 cairo_surface_t *surface;
140 struct rectangle allocation;
142 widget_get_allocation(clickdot->widget, &allocation);
144 surface = window_get_surface(clickdot->window);
146 cr = cairo_create(surface);
147 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
153 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
156 draw_line(clickdot, cr, &allocation);
158 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
159 cairo_set_line_width(cr, 1.0);
160 cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
161 cairo_move_to(cr, 0.0, -r);
162 cairo_line_to(cr, 0.0, r);
163 cairo_move_to(cr, -r, 0.0);
164 cairo_line_to(cr, r, 0.0);
165 cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
170 cairo_surface_destroy(surface);
174 keyboard_focus_handler(struct window *window,
175 struct input *device, void *data)
177 struct clickdot *clickdot = data;
179 window_schedule_redraw(clickdot->window);
183 key_handler(struct window *window, struct input *input, uint32_t time,
184 uint32_t key, uint32_t sym, uint32_t state, void *data)
186 struct clickdot *clickdot = data;
193 display_exit(clickdot->display);
199 button_handler(struct widget *widget,
200 struct input *input, uint32_t time,
201 uint32_t button, uint32_t state, void *data)
203 struct clickdot *clickdot = data;
205 if (state && button == BTN_LEFT)
206 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
208 widget_schedule_redraw(widget);
212 motion_handler(struct widget *widget,
213 struct input *input, uint32_t time,
214 float x, float y, void *data)
216 struct clickdot *clickdot = data;
217 clickdot->line.x = x;
218 clickdot->line.y = y;
220 window_schedule_redraw(clickdot->window);
222 return WL_CURSOR_LEFT_PTR;
226 resize_handler(struct widget *widget,
227 int32_t width, int32_t height,
230 struct clickdot *clickdot = data;
236 leave_handler(struct widget *widget,
237 struct input *input, void *data)
239 struct clickdot *clickdot = data;
244 static struct clickdot *
245 clickdot_create(struct display *display)
247 struct clickdot *clickdot;
249 clickdot = malloc(sizeof *clickdot);
250 if (clickdot == NULL)
252 memset(clickdot, 0, sizeof *clickdot);
254 clickdot->window = window_create(display);
255 clickdot->widget = frame_create(clickdot->window, clickdot);
256 window_set_title(clickdot->window, "Wayland ClickDot");
257 clickdot->display = display;
258 clickdot->buffer = NULL;
260 window_set_key_handler(clickdot->window, key_handler);
261 window_set_user_data(clickdot->window, clickdot);
262 window_set_keyboard_focus_handler(clickdot->window,
263 keyboard_focus_handler);
265 widget_set_redraw_handler(clickdot->widget, redraw_handler);
266 widget_set_button_handler(clickdot->widget, button_handler);
267 widget_set_motion_handler(clickdot->widget, motion_handler);
268 widget_set_resize_handler(clickdot->widget, resize_handler);
269 widget_set_leave_handler(clickdot->widget, leave_handler);
271 widget_schedule_resize(clickdot->widget, 500, 400);
272 clickdot->dot.x = 250;
273 clickdot->dot.y = 200;
274 clickdot->line.x = -1;
275 clickdot->line.y = -1;
276 clickdot->line.old_x = -1;
277 clickdot->line.old_y = -1;
284 clickdot_destroy(struct clickdot *clickdot)
286 if (clickdot->buffer)
287 cairo_surface_destroy(clickdot->buffer);
288 widget_destroy(clickdot->widget);
289 window_destroy(clickdot->window);
294 main(int argc, char *argv[])
296 struct display *display;
297 struct clickdot *clickdot;
299 display = display_create(argc, argv);
300 if (display == NULL) {
301 fprintf(stderr, "failed to create display: %m\n");
305 clickdot = clickdot_create(display);
307 display_run(display);
309 clickdot_destroy(clickdot);
310 display_destroy(display);