1 /*
2 * Copyright © 2012 Scott Moreau
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdbool.h>
32
33 #include <libweston/libweston.h>
34 #include "backend.h"
35 #include "libweston-internal.h"
36 // #include "text-cursor-position-server-protocol.h" // useless
37 #include "shared/helpers.h"
38
39 static void
weston_zoom_frame_z(struct weston_animation * animation,struct weston_output * output,const struct timespec * time)40 weston_zoom_frame_z(struct weston_animation *animation,
41 struct weston_output *output,
42 const struct timespec *time)
43 {
44 if (animation->frame_counter <= 1)
45 output->zoom.spring_z.timestamp = *time;
46
47 weston_spring_update(&output->zoom.spring_z, time);
48
49 if (output->zoom.spring_z.current > output->zoom.max_level)
50 output->zoom.spring_z.current = output->zoom.max_level;
51 else if (output->zoom.spring_z.current < 0.0)
52 output->zoom.spring_z.current = 0.0;
53
54 if (weston_spring_done(&output->zoom.spring_z)) {
55 if (output->zoom.active && output->zoom.level <= 0.0) {
56 output->zoom.active = false;
57 output->zoom.seat = NULL;
58 weston_output_disable_planes_decr(output);
59 wl_list_remove(&output->zoom.motion_listener.link);
60 }
61 output->zoom.spring_z.current = output->zoom.level;
62 wl_list_remove(&animation->link);
63 wl_list_init(&animation->link);
64 }
65
66 output->dirty = 1;
67 weston_output_damage(output);
68 }
69
70 static void
zoom_area_center_from_point(struct weston_output * output,double * x,double * y)71 zoom_area_center_from_point(struct weston_output *output,
72 double *x, double *y)
73 {
74 float level = output->zoom.spring_z.current;
75
76 *x = (*x - output->x) * level + output->width / 2.;
77 *y = (*y - output->y) * level + output->height / 2.;
78 }
79
80 static void
weston_output_update_zoom_transform(struct weston_output * output)81 weston_output_update_zoom_transform(struct weston_output *output)
82 {
83 double x = output->zoom.current.x; /* global pointer coords */
84 double y = output->zoom.current.y;
85 float level;
86
87 level = output->zoom.spring_z.current;
88
89 if (!output->zoom.active || level > output->zoom.max_level ||
90 level == 0.0f)
91 return;
92
93 zoom_area_center_from_point(output, &x, &y);
94
95 output->zoom.trans_x = x - output->width / 2;
96 output->zoom.trans_y = y - output->height / 2;
97
98 if (output->zoom.trans_x < 0)
99 output->zoom.trans_x = 0;
100 if (output->zoom.trans_y < 0)
101 output->zoom.trans_y = 0;
102 if (output->zoom.trans_x > level * output->width)
103 output->zoom.trans_x = level * output->width;
104 if (output->zoom.trans_y > level * output->height)
105 output->zoom.trans_y = level * output->height;
106 }
107
108 static void
weston_zoom_transition(struct weston_output * output)109 weston_zoom_transition(struct weston_output *output)
110 {
111 if (output->zoom.level != output->zoom.spring_z.current) {
112 output->zoom.spring_z.target = output->zoom.level;
113 if (wl_list_empty(&output->zoom.animation_z.link)) {
114 output->zoom.animation_z.frame_counter = 0;
115 wl_list_insert(output->animation_list.prev,
116 &output->zoom.animation_z.link);
117 }
118 }
119
120 output->dirty = 1;
121 weston_output_damage(output);
122 }
123
124 WL_EXPORT void
weston_output_update_zoom(struct weston_output * output)125 weston_output_update_zoom(struct weston_output *output)
126 {
127 struct weston_seat *seat = output->zoom.seat;
128 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
129
130 if (!pointer)
131 return;
132
133 assert(output->zoom.active);
134
135 output->zoom.current.x = wl_fixed_to_double(pointer->x);
136 output->zoom.current.y = wl_fixed_to_double(pointer->y);
137
138 weston_zoom_transition(output);
139 weston_output_update_zoom_transform(output);
140 }
141
142 static void
motion(struct wl_listener * listener,void * data)143 motion(struct wl_listener *listener, void *data)
144 {
145 struct weston_output_zoom *zoom =
146 container_of(listener, struct weston_output_zoom, motion_listener);
147 struct weston_output *output =
148 container_of(zoom, struct weston_output, zoom);
149
150 weston_output_update_zoom(output);
151 }
152
153 WL_EXPORT void
weston_output_activate_zoom(struct weston_output * output,struct weston_seat * seat)154 weston_output_activate_zoom(struct weston_output *output,
155 struct weston_seat *seat)
156 {
157 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
158
159 if (!pointer || output->zoom.active)
160 return;
161
162 output->zoom.active = true;
163 output->zoom.seat = seat;
164 weston_output_disable_planes_incr(output);
165 wl_signal_add(&pointer->motion_signal,
166 &output->zoom.motion_listener);
167 }
168
169 WL_EXPORT void
weston_output_init_zoom(struct weston_output * output)170 weston_output_init_zoom(struct weston_output *output)
171 {
172 output->zoom.active = false;
173 output->zoom.seat = NULL;
174 output->zoom.increment = 0.07;
175 output->zoom.max_level = 0.95;
176 output->zoom.level = 0.0;
177 output->zoom.trans_x = 0.0;
178 output->zoom.trans_y = 0.0;
179 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
180 output->zoom.spring_z.friction = 1000;
181 output->zoom.animation_z.frame = weston_zoom_frame_z;
182 wl_list_init(&output->zoom.animation_z.link);
183 output->zoom.motion_listener.notify = motion;
184 }
185