blob: cf35faaac4bd1384b13a05cfd65a411409b56f6b [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.
Pekka Paalanen69201902012-01-20 11:09:13 +02004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <cairo.h>
29#include <math.h>
30#include <assert.h>
31
32#include <linux/input.h>
33#include <wayland-client.h>
34
35#include "window.h"
36
37#include <X11/keysym.h>
38
39struct clickdot {
40 struct display *display;
41 struct window *window;
42 struct widget *widget;
Pekka Paalanen69201902012-01-20 11:09:13 +020043
Pekka Paalanenb13e84f2012-01-20 13:04:56 +020044 int32_t x, y;
Pekka Paalanen69201902012-01-20 11:09:13 +020045};
46
47static void
48redraw_handler(struct widget *widget, void *data)
49{
Pekka Paalanenb13e84f2012-01-20 13:04:56 +020050 static const double r = 10.0;
Pekka Paalanen69201902012-01-20 11:09:13 +020051 struct clickdot *clickdot = data;
52 cairo_surface_t *surface;
53 cairo_t *cr;
54 struct rectangle allocation;
55
56 widget_get_allocation(clickdot->widget, &allocation);
57
58 surface = window_get_surface(clickdot->window);
59
60 cr = cairo_create(surface);
61 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
62 cairo_rectangle(cr,
63 allocation.x,
64 allocation.y,
65 allocation.width,
66 allocation.height);
67 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
68 cairo_fill(cr);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +020069
70 cairo_translate(cr, clickdot->x + 0.5, clickdot->y + 0.5);
71 cairo_set_line_width(cr, 1.0);
72 cairo_set_source_rgb(cr, 0.1, 0.9, 0.9);
73 cairo_move_to(cr, 0.0, -r);
74 cairo_line_to(cr, 0.0, r);
75 cairo_move_to(cr, -r, 0.0);
76 cairo_line_to(cr, r, 0.0);
77 cairo_arc(cr, 0.0, 0.0, r, 0.0, 2.0 * M_PI);
78 cairo_stroke(cr);
79
Pekka Paalanen69201902012-01-20 11:09:13 +020080 cairo_destroy(cr);
81
82 cairo_surface_destroy(surface);
Pekka Paalanen69201902012-01-20 11:09:13 +020083}
84
85static void
86keyboard_focus_handler(struct window *window,
87 struct input *device, void *data)
88{
89 struct clickdot *clickdot = data;
90
91 window_schedule_redraw(clickdot->window);
92}
93
94static void
95key_handler(struct window *window, struct input *input, uint32_t time,
96 uint32_t key, uint32_t sym, uint32_t state, void *data)
97{
98 struct clickdot *clickdot = data;
99
100 if (state == 0)
101 return;
102
103 switch (sym) {
Pekka Paalanen69201902012-01-20 11:09:13 +0200104 case XK_Escape:
105 display_exit(clickdot->display);
106 break;
107 }
108}
109
110static void
Pekka Paalanen69201902012-01-20 11:09:13 +0200111button_handler(struct widget *widget,
112 struct input *input, uint32_t time,
113 int button, int state, void *data)
114{
115 struct clickdot *clickdot = data;
116
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200117 if (state && button == BTN_LEFT)
118 input_get_position(input, &clickdot->x, &clickdot->y);
119
120 widget_schedule_redraw(widget);
Pekka Paalanen69201902012-01-20 11:09:13 +0200121}
122
123static struct clickdot *
124clickdot_create(struct display *display)
125{
126 struct clickdot *clickdot;
Pekka Paalanen69201902012-01-20 11:09:13 +0200127
128 clickdot = malloc(sizeof *clickdot);
129 if (clickdot == NULL)
130 return clickdot;
131 memset(clickdot, 0, sizeof *clickdot);
132
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500133 clickdot->window = window_create(display);
Pekka Paalanen69201902012-01-20 11:09:13 +0200134 clickdot->widget = frame_create(clickdot->window, clickdot);
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200135 window_set_title(clickdot->window, "Wayland ClickDot");
Pekka Paalanen69201902012-01-20 11:09:13 +0200136 clickdot->display = display;
137
138 window_set_key_handler(clickdot->window, key_handler);
139 window_set_user_data(clickdot->window, clickdot);
Pekka Paalanen69201902012-01-20 11:09:13 +0200140 window_set_keyboard_focus_handler(clickdot->window,
141 keyboard_focus_handler);
142
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200143 widget_set_redraw_handler(clickdot->widget, redraw_handler);
Pekka Paalanen69201902012-01-20 11:09:13 +0200144 widget_set_button_handler(clickdot->widget, button_handler);
145
Pekka Paalanenb13e84f2012-01-20 13:04:56 +0200146 widget_schedule_resize(clickdot->widget, 500, 400);
147 clickdot->x = 250;
148 clickdot->y = 200;
Pekka Paalanen69201902012-01-20 11:09:13 +0200149
150 return clickdot;
151}
152
153static void
154clickdot_destroy(struct clickdot *clickdot)
155{
Pekka Paalanen69201902012-01-20 11:09:13 +0200156 widget_destroy(clickdot->widget);
157 window_destroy(clickdot->window);
158 free(clickdot);
159}
160
161int
162main(int argc, char *argv[])
163{
164 struct display *display;
165 struct clickdot *clickdot;
166
167 display = display_create(&argc, &argv, NULL);
168 if (display == NULL) {
169 fprintf(stderr, "failed to create display: %m\n");
170 return -1;
171 }
172
173 clickdot = clickdot_create(display);
174
175 display_run(display);
176
177 clickdot_destroy(clickdot);
178 display_destroy(display);
179
180 return 0;
181}