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>
39 struct display *display;
40 struct window *window;
41 struct widget *widget;
43 cairo_surface_t *buffer;
58 draw_line(struct clickdot *clickdot, cairo_t *cr,
59 struct rectangle *allocation)
62 cairo_surface_t *tmp_buffer = NULL;
64 if (clickdot->reset) {
65 tmp_buffer = clickdot->buffer;
66 clickdot->buffer = NULL;
67 clickdot->line.x = -1;
68 clickdot->line.y = -1;
69 clickdot->line.old_x = -1;
70 clickdot->line.old_y = -1;
74 if (clickdot->buffer == NULL) {
76 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
79 bcr = cairo_create(clickdot->buffer);
80 cairo_set_source_rgba(bcr, 0, 0, 0, 0);
83 allocation->width, allocation->height);
87 bcr = cairo_create(clickdot->buffer);
90 cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
91 cairo_rectangle(bcr, 0, 0,
92 allocation->width, allocation->height);
96 cairo_surface_destroy(tmp_buffer);
99 if (clickdot->line.x != -1 && clickdot->line.y != -1) {
100 if (clickdot->line.old_x != -1 &&
101 clickdot->line.old_y != -1) {
102 cairo_set_line_width(bcr, 2.0);
103 cairo_set_source_rgb(bcr, 1, 1, 1);
105 -allocation->x, -allocation->y);
108 clickdot->line.old_x,
109 clickdot->line.old_y);
117 clickdot->line.old_x = clickdot->line.x;
118 clickdot->line.old_y = clickdot->line.y;
122 cairo_set_source_surface(cr, clickdot->buffer,
123 allocation->x, allocation->y);
124 cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
126 allocation->x, allocation->y,
127 allocation->width, allocation->height);
133 redraw_handler(struct widget *widget, void *data)
135 static const double r = 10.0;
136 struct clickdot *clickdot = data;
137 cairo_surface_t *surface;
139 struct rectangle allocation;
141 widget_get_allocation(clickdot->widget, &allocation);
143 surface = window_get_surface(clickdot->window);
145 cr = cairo_create(surface);
146 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
152 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
155 draw_line(clickdot, cr, &allocation);
157 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
158 cairo_set_line_width(cr, 1.0);
159 cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
160 cairo_move_to(cr, 0.0, -r);
161 cairo_line_to(cr, 0.0, r);
162 cairo_move_to(cr, -r, 0.0);
163 cairo_line_to(cr, r, 0.0);
164 cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
169 cairo_surface_destroy(surface);
173 keyboard_focus_handler(struct window *window,
174 struct input *device, void *data)
176 struct clickdot *clickdot = data;
178 window_schedule_redraw(clickdot->window);
182 key_handler(struct window *window, struct input *input, uint32_t time,
183 uint32_t key, uint32_t sym,
184 enum wl_keyboard_key_state state, void *data)
186 struct clickdot *clickdot = data;
188 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
193 display_exit(clickdot->display);
199 button_handler(struct widget *widget,
200 struct input *input, uint32_t time,
202 enum wl_pointer_button_state state, void *data)
204 struct clickdot *clickdot = data;
206 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
207 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
209 widget_schedule_redraw(widget);
213 motion_handler(struct widget *widget,
214 struct input *input, uint32_t time,
215 float x, float y, void *data)
217 struct clickdot *clickdot = data;
218 clickdot->line.x = x;
219 clickdot->line.y = y;
221 window_schedule_redraw(clickdot->window);
223 return CURSOR_LEFT_PTR;
227 resize_handler(struct widget *widget,
228 int32_t width, int32_t height,
231 struct clickdot *clickdot = data;
237 leave_handler(struct widget *widget,
238 struct input *input, void *data)
240 struct clickdot *clickdot = data;
245 static struct clickdot *
246 clickdot_create(struct display *display)
248 struct clickdot *clickdot;
250 clickdot = xzalloc(sizeof *clickdot);
251 clickdot->window = window_create(display);
252 clickdot->widget = window_frame_create(clickdot->window, clickdot);
253 window_set_title(clickdot->window, "Wayland ClickDot");
254 clickdot->display = display;
255 clickdot->buffer = NULL;
257 window_set_key_handler(clickdot->window, key_handler);
258 window_set_user_data(clickdot->window, clickdot);
259 window_set_keyboard_focus_handler(clickdot->window,
260 keyboard_focus_handler);
262 widget_set_redraw_handler(clickdot->widget, redraw_handler);
263 widget_set_button_handler(clickdot->widget, button_handler);
264 widget_set_motion_handler(clickdot->widget, motion_handler);
265 widget_set_resize_handler(clickdot->widget, resize_handler);
266 widget_set_leave_handler(clickdot->widget, leave_handler);
268 widget_schedule_resize(clickdot->widget, 500, 400);
269 clickdot->dot.x = 250;
270 clickdot->dot.y = 200;
271 clickdot->line.x = -1;
272 clickdot->line.y = -1;
273 clickdot->line.old_x = -1;
274 clickdot->line.old_y = -1;
281 clickdot_destroy(struct clickdot *clickdot)
283 if (clickdot->buffer)
284 cairo_surface_destroy(clickdot->buffer);
285 widget_destroy(clickdot->widget);
286 window_destroy(clickdot->window);
291 main(int argc, char *argv[])
293 struct display *display;
294 struct clickdot *clickdot;
296 display = display_create(&argc, argv);
297 if (display == NULL) {
298 fprintf(stderr, "failed to create display: %m\n");
302 clickdot = clickdot_create(display);
304 display_run(display);
306 clickdot_destroy(clickdot);
307 display_destroy(display);