1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* 👀
*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2012 Collabora Ltd.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cairo.h>
#include <glib.h>
#include <linux/input.h>
#include <wayland-client.h>
#include "window.h"
#include "pointer-tracking-client-protocol.h"
struct eyes {
struct display *display;
struct window *window;
struct widget *widget;
int width, height;
int32_t pointer_x, pointer_y;
struct pointer_tracking *pointer_tracking;
double iris_r, iris_g, iris_b;
};
static void
pick_color(struct eyes *eyes)
{
eyes->iris_r = (random() % 50) / 100.0;
eyes->iris_g = (random() % 50) / 100.0;
eyes->iris_b = (random() % 50) / 100.0;
}
static void
draw_eyeball(struct eyes *eyes, cairo_t *cr,
int x, int y, int width, int height)
{
cairo_matrix_t transform;
double transformed_x, transformed_y;
double line_width = 9 * (2. / MAX(width, height));
double radius = 0.5;
/* Transform coordinate system so the eyeball appears to be a unit
* circle centred at (0, 0)
*/
cairo_save(cr);
cairo_translate(cr, x + width / 2., y + height / 2.);
cairo_scale(cr, (width - 10) / 2., (height - 10) / 2.);
cairo_get_matrix(cr, &transform);
/* Eyeball */
cairo_arc(cr, 0., 0., 1., 0., 2 * M_PI);
cairo_set_source_rgba(cr, 1., 1., 1., 0.9);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0., 0., 0., 1.0);
cairo_set_line_width(cr, line_width);
cairo_stroke(cr);
/* Pupil */
transformed_x = eyes->pointer_x;
transformed_y = eyes->pointer_y;
cairo_matrix_invert(&transform);
cairo_matrix_transform_point (&transform, &transformed_x, &transformed_y);
double r_squared = pow(transformed_x, 2) + pow(transformed_y, 2);
if (r_squared > pow(radius, 2)) {
double ratio = radius / sqrt(r_squared);
transformed_x *= ratio;
transformed_y *= ratio;
}
cairo_arc(cr, transformed_x, transformed_y, line_width * 1.5, 0., 2 * M_PI);
cairo_set_source_rgba(cr, 0., 0., 0., 1.0);
cairo_fill_preserve(cr);
/* Iris */
cairo_set_source_rgb(cr, eyes->iris_r, eyes->iris_g, eyes->iris_b);
cairo_stroke(cr);
cairo_restore(cr);
}
static void
draw_stuff(struct eyes *eyes, cairo_surface_t *surface, int width, int height)
{
cairo_t *cr;
cr = cairo_create(surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
draw_eyeball(eyes, cr, 0, 0, width / 2, height);
draw_eyeball(eyes, cr, width / 2, 0, width / 2, height);
cairo_destroy(cr);
}
static void
resize_handler(struct widget *widget,
int32_t width, int32_t height, void *data)
{
struct eyes *eyes = data;
/* Dont resize me */
widget_set_size(eyes->widget, eyes->width, eyes->height);
}
static void
redraw_handler(struct widget *widget, void *data)
{
struct eyes *eyes = data;
cairo_surface_t *surface;
surface = window_get_surface(eyes->window);
if (surface == NULL ||
cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "failed to create cairo egl surface\n");
return;
}
draw_stuff(eyes, surface, eyes->width, eyes->height);
cairo_surface_destroy(surface);
}
static int
motion_handler(struct widget *widget, struct input *input,
uint32_t time, int32_t x, int32_t y, void *data)
{
struct eyes *eyes = data;
eyes->pointer_x = x;
eyes->pointer_y = y;
widget_schedule_redraw(widget);
return POINTER_IBEAM;
}
static void
button_handler(struct widget *widget,
struct input *input, uint32_t time,
int button, int state, void *data)
{
struct eyes *eyes = data;
switch (button) {
case BTN_LEFT:
if (state)
window_move(eyes->window, input, time);
break;
case BTN_RIGHT:
if (state)
window_show_frame_menu(eyes->window, input, time);
break;
}
}
static void
eyes_pointer_moved(void *data,
struct pointer_tracking *pointer_tracking,
int32_t x, int32_t y)
{
struct eyes *eyes = data;
motion_handler(eyes->widget, NULL, 0, x, y, eyes);
}
static struct pointer_tracking_listener eyes_pointer_tracking_listener = {
eyes_pointer_moved
};
static void
eyes_display_handle_global(struct wl_display *display, uint32_t id,
const char *interface, uint32_t version, void *data)
{
struct eyes *eyes = data;
if (strcmp(interface, "pointer_tracking") == 0) {
eyes->pointer_tracking =
wl_display_bind(display, id,
&pointer_tracking_interface);
pointer_tracking_track_relative_to(eyes->pointer_tracking,
window_get_wl_surface(eyes->window));
pointer_tracking_add_listener(eyes->pointer_tracking,
&eyes_pointer_tracking_listener,
eyes);
widget_set_motion_handler(eyes->widget, NULL);
}
}
int main(int argc, char *argv[])
{
struct eyes eyes;
struct display *d;
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec);
d = display_create(argc, argv);
if (d == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
eyes.width = 150;
eyes.height = 100;
eyes.display = d;
eyes.window = window_create(d);
eyes.widget = window_add_widget(eyes.window, &eyes);
pick_color(&eyes);
widget_set_resize_handler(eyes.widget, resize_handler);
widget_set_redraw_handler(eyes.widget, redraw_handler);
/* Just in case the server doesn't have the pointer-tracking extension. */
widget_set_motion_handler(eyes.widget, motion_handler);
widget_set_button_handler(eyes.widget, button_handler);
wl_display_add_global_listener(display_get_display(d),
eyes_display_handle_global, &eyes);
window_schedule_resize(eyes.window, eyes.width, eyes.height);
display_run(d);
return 0;
}
|