blob: be4ee181e21eecede4140ef066110ef079c94f8d [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 *
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.
15 *
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
22 * OF THIS SOFTWARE.
23 */
24
25#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <cairo.h>
30#include <math.h>
31#include <assert.h>
32
33#include <linux/input.h>
34#include <wayland-client.h>
35
36#include "window.h"
37
Pekka Paalanen69201902012-01-20 11:09:13 +020038struct clickdot {
39 struct display *display;
40 struct window *window;
41 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020042
Jonas Ådahldf211832012-05-10 23:26:25 +020043 cairo_surface_t *buffer;
44
45 struct {
46 int32_t x, y;
47 } dot;
48
49 struct {
50 int32_t x, y;
51 int32_t old_x, old_y;
52 } line;
53
54 int reset;
Pekka Paalanen69201902012-01-20 11:09:13 +020055};
56
57static void
Jonas Ådahldf211832012-05-10 23:26:25 +020058draw_line(struct clickdot *clickdot, cairo_t *cr,
59 struct rectangle *allocation)
60{
61 cairo_t *bcr;
62 cairo_surface_t *tmp_buffer = NULL;
63
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;
71 clickdot->reset = 0;
72 }
73
74 if (clickdot->buffer == NULL) {
75 clickdot->buffer =
76 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
77 allocation->width,
78 allocation->height);
79 bcr = cairo_create(clickdot->buffer);
80 cairo_set_source_rgba(bcr, 0, 0, 0, 0);
81 cairo_rectangle(bcr,
82 0, 0,
83 allocation->width, allocation->height);
84 cairo_fill(bcr);
85 }
86 else
87 bcr = cairo_create(clickdot->buffer);
88
89 if (tmp_buffer) {
90 cairo_set_source_surface(bcr, tmp_buffer, 0, 0);
91 cairo_rectangle(bcr, 0, 0,
92 allocation->width, allocation->height);
93 cairo_clip(bcr);
94 cairo_paint(bcr);
95
96 cairo_surface_destroy(tmp_buffer);
97 }
98
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);
104 cairo_translate(bcr,
105 -allocation->x, -allocation->y);
106
107 cairo_move_to(bcr,
108 clickdot->line.old_x,
109 clickdot->line.old_y);
110 cairo_line_to(bcr,
111 clickdot->line.x,
112 clickdot->line.y);
113
114 cairo_stroke(bcr);
115 }
116
117 clickdot->line.old_x = clickdot->line.x;
118 clickdot->line.old_y = clickdot->line.y;
119 }
120 cairo_destroy(bcr);
121
122 cairo_set_source_surface(cr, clickdot->buffer,
123 allocation->x, allocation->y);
124 cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
125 cairo_rectangle(cr,
126 allocation->x, allocation->y,
127 allocation->width, allocation->height);
128 cairo_clip(cr);
129 cairo_paint(cr);
130}
131
132static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200133redraw_handler(struct widget *widget, void *data)
134{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200135 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200136 struct clickdot *clickdot = data;
137 cairo_surface_t *surface;
138 cairo_t *cr;
139 struct rectangle allocation;
140
141 widget_get_allocation(clickdot->widget, &allocation);
142
143 surface = window_get_surface(clickdot->window);
144
145 cr = cairo_create(surface);
146 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
147 cairo_rectangle(cr,
148 allocation.x,
149 allocation.y,
150 allocation.width,
151 allocation.height);
152 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
153 cairo_fill(cr);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200154
Jonas Ådahldf211832012-05-10 23:26:25 +0200155 draw_line(clickdot, cr, &allocation);
156
157 cairo_translate(cr, clickdot->dot.x + 0.5, clickdot->dot.y + 0.5);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200158 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);
165 cairo_stroke(cr);
166
Pekka Paalanen69201902012-01-20 11:09:13 +0200167 cairo_destroy(cr);
168
169 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +0200170}
171
172static void
173keyboard_focus_handler(struct window *window,
174 struct input *device, void *data)
175{
176 struct clickdot *clickdot = data;
177
178 window_schedule_redraw(clickdot->window);
179}
180
181static void
182key_handler(struct window *window, struct input *input, uint32_t time,
183 uint32_t key, uint32_t sym, uint32_t state, void *data)
184{
185 struct clickdot *clickdot = data;
186
187 if (state == 0)
188 return;
189
190 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400191 case XKB_KEY_Escape:
Pekka Paalanen69201902012-01-20 11:09:13 +0200192 display_exit(clickdot->display);
193 break;
194 }
195}
196
197static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200198button_handler(struct widget *widget,
199 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100200 uint32_t button,
201 enum wl_pointer_button_state state, void *data)
Pekka Paalanen69201902012-01-20 11:09:13 +0200202{
203 struct clickdot *clickdot = data;
204
Daniel Stone4dbadb12012-05-30 16:31:51 +0100205 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT)
Jonas Ådahldf211832012-05-10 23:26:25 +0200206 input_get_position(input, &clickdot->dot.x, &clickdot->dot.y);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200207
208 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200209}
210
Jonas Ådahldf211832012-05-10 23:26:25 +0200211static int
212motion_handler(struct widget *widget,
213 struct input *input, uint32_t time,
214 float x, float y, void *data)
215{
216 struct clickdot *clickdot = data;
217 clickdot->line.x = x;
218 clickdot->line.y = y;
219
220 window_schedule_redraw(clickdot->window);
221
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300222 return CURSOR_LEFT_PTR;
Jonas Ådahldf211832012-05-10 23:26:25 +0200223}
224
225static void
226resize_handler(struct widget *widget,
227 int32_t width, int32_t height,
228 void *data)
229{
230 struct clickdot *clickdot = data;
231
232 clickdot->reset = 1;
233}
234
235static void
236leave_handler(struct widget *widget,
237 struct input *input, void *data)
238{
239 struct clickdot *clickdot = data;
240
241 clickdot->reset = 1;
242}
243
Pekka Paalanen69201902012-01-20 11:09:13 +0200244static struct clickdot *
245clickdot_create(struct display *display)
246{
247 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200248
249 clickdot = malloc(sizeof *clickdot);
250 if (clickdot == NULL)
251 return clickdot;
252 memset(clickdot, 0, sizeof *clickdot);
253
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500254 clickdot->window = window_create(display);
Pekka Paalanen69201902012-01-20 11:09:13 +0200255 clickdot->widget = frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200256 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200257 clickdot->display = display;
Jonas Ådahldf211832012-05-10 23:26:25 +0200258 clickdot->buffer = NULL;
Pekka Paalanen69201902012-01-20 11:09:13 +0200259
260 window_set_key_handler(clickdot->window, key_handler);
261 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200262 window_set_keyboard_focus_handler(clickdot->window,
263 keyboard_focus_handler);
264
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200265 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200266 widget_set_button_handler(clickdot->widget, button_handler);
Jonas Ådahldf211832012-05-10 23:26:25 +0200267 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);
Pekka Paalanen69201902012-01-20 11:09:13 +0200270
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200271 widget_schedule_resize(clickdot->widget, 500, 400);
Jonas Ådahldf211832012-05-10 23:26:25 +0200272 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;
278 clickdot->reset = 0;
Pekka Paalanen69201902012-01-20 11:09:13 +0200279
280 return clickdot;
281}
282
283static void
284clickdot_destroy(struct clickdot *clickdot)
285{
Jonas Ådahldf211832012-05-10 23:26:25 +0200286 if (clickdot->buffer)
287 cairo_surface_destroy(clickdot->buffer);
Pekka Paalanen69201902012-01-20 11:09:13 +0200288 widget_destroy(clickdot->widget);
289 window_destroy(clickdot->window);
290 free(clickdot);
291}
292
293int
294main(int argc, char *argv[])
295{
296 struct display *display;
297 struct clickdot *clickdot;
298
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400299 display = display_create(argc, argv);
Pekka Paalanen69201902012-01-20 11:09:13 +0200300 if (display == NULL) {
301 fprintf(stderr, "failed to create display: %m\n");
302 return -1;
303 }
304
305 clickdot = clickdot_create(display);
306
307 display_run(display);
308
309 clickdot_destroy(clickdot);
310 display_destroy(display);
311
312 return 0;
313}