blob: c70a70ced2be9708f0a47eceb911f64881a3a438 [file] [log] [blame]
Scott Moreau429490d2012-06-17 18:10:59 -06001/*
2 * Copyright © 2012 Scott Moreau
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Kristian Høgsberg583a2362012-06-25 17:13:58 -040025#include <stdlib.h>
26
Scott Moreau429490d2012-06-17 18:10:59 -060027#include "compositor.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040028#include "text-cursor-position-server-protocol.h"
29
Scott Moreau429490d2012-06-17 18:10:59 -060030static void
31weston_zoom_frame_z(struct weston_animation *animation,
32 struct weston_output *output, uint32_t msecs)
33{
34 if (animation->frame_counter <= 1)
35 output->zoom.spring_z.timestamp = msecs;
36
37 weston_spring_update(&output->zoom.spring_z, msecs);
38
39 if (output->zoom.spring_z.current > output->zoom.max_level)
40 output->zoom.spring_z.current = output->zoom.max_level;
41 else if (output->zoom.spring_z.current < 0.0)
42 output->zoom.spring_z.current = 0.0;
43
44 if (weston_spring_done(&output->zoom.spring_z)) {
Ville Syrjäläaa628d02012-11-16 11:48:47 +020045 if (output->zoom.active && output->zoom.level <= 0.0) {
Scott Moreau429490d2012-06-17 18:10:59 -060046 output->zoom.active = 0;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040047 output->disable_planes--;
Giulio Camuffo412b0242013-11-14 23:42:51 +010048 wl_list_remove(&output->zoom.motion_listener.link);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040049 }
Scott Moreau429490d2012-06-17 18:10:59 -060050 output->zoom.spring_z.current = output->zoom.level;
51 wl_list_remove(&animation->link);
52 wl_list_init(&animation->link);
53 }
54
55 output->dirty = 1;
56 weston_output_damage(output);
57}
58
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040059static struct weston_seat *
60weston_zoom_pick_seat(struct weston_compositor *compositor)
61{
62 return container_of(compositor->seat_list.next,
63 struct weston_seat, link);
64}
65
Scott Moreau429490d2012-06-17 18:10:59 -060066static void
67zoom_area_center_from_pointer(struct weston_output *output,
68 wl_fixed_t *x, wl_fixed_t *y)
69{
70 float level = output->zoom.spring_z.current;
71 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
72 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
Scott Moreau1bad5db2012-08-18 01:04:05 -060073 wl_fixed_t w = wl_fixed_from_int(output->width);
74 wl_fixed_t h = wl_fixed_from_int(output->height);
Scott Moreau429490d2012-06-17 18:10:59 -060075
76 *x -= ((((*x - offset_x) / (float) w) - 0.5) * (w * (1.0 - level)));
77 *y -= ((((*y - offset_y) / (float) h) - 0.5) * (h * (1.0 - level)));
78}
79
80static void
81weston_output_update_zoom_transform(struct weston_output *output)
82{
Scott Moreau429490d2012-06-17 18:10:59 -060083 float global_x, global_y;
84 wl_fixed_t x = output->zoom.current.x;
85 wl_fixed_t y = output->zoom.current.y;
86 float trans_min, trans_max;
87 float ratio, level;
88
89 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -060090
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +020091 if (!output->zoom.active || level > output->zoom.max_level ||
92 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -060093 return;
94
Carlos Olmedo Escobar1a873aa2015-01-17 20:31:53 +010095 ratio = 1 / level;
96
Pekka Paalanen13fd4462015-03-10 11:13:14 +020097 zoom_area_center_from_pointer(output, &x, &y);
Scott Moreau429490d2012-06-17 18:10:59 -060098
99 global_x = wl_fixed_to_double(x);
100 global_y = wl_fixed_to_double(y);
101
102 output->zoom.trans_x =
Scott Moreau1bad5db2012-08-18 01:04:05 -0600103 ((((global_x - output->x) / output->width) *
Scott Moreau429490d2012-06-17 18:10:59 -0600104 (level * 2)) - level) * ratio;
105 output->zoom.trans_y =
Scott Moreau1bad5db2012-08-18 01:04:05 -0600106 ((((global_y - output->y) / output->height) *
Scott Moreau429490d2012-06-17 18:10:59 -0600107 (level * 2)) - level) * ratio;
108
109 trans_max = level * 2 - level;
110 trans_min = -trans_max;
111
112 /* Clip zoom area to output */
113 if (output->zoom.trans_x > trans_max)
114 output->zoom.trans_x = trans_max;
115 else if (output->zoom.trans_x < trans_min)
116 output->zoom.trans_x = trans_min;
117 if (output->zoom.trans_y > trans_max)
118 output->zoom.trans_y = trans_max;
119 else if (output->zoom.trans_y < trans_min)
120 output->zoom.trans_y = trans_min;
121}
122
123static void
Pekka Paalanen98087082015-03-10 12:05:44 +0200124weston_zoom_transition(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600125{
Scott Moreau429490d2012-06-17 18:10:59 -0600126 if (output->zoom.level != output->zoom.spring_z.current) {
127 output->zoom.spring_z.target = output->zoom.level;
128 if (wl_list_empty(&output->zoom.animation_z.link)) {
129 output->zoom.animation_z.frame_counter = 0;
130 wl_list_insert(output->animation_list.prev,
131 &output->zoom.animation_z.link);
132 }
133 }
134
135 output->dirty = 1;
136 weston_output_damage(output);
137}
138
139WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500140weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600141{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400142 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600143
Pekka Paalanen13fd4462015-03-10 11:13:14 +0200144 output->zoom.current.x = seat->pointer->x;
145 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600146
Pekka Paalanen98087082015-03-10 12:05:44 +0200147 weston_zoom_transition(output);
Scott Moreau429490d2012-06-17 18:10:59 -0600148 weston_output_update_zoom_transform(output);
149}
150
Giulio Camuffo412b0242013-11-14 23:42:51 +0100151static void
152motion(struct wl_listener *listener, void *data)
153{
154 struct weston_output_zoom *zoom =
155 container_of(listener, struct weston_output_zoom, motion_listener);
156 struct weston_output *output =
157 container_of(zoom, struct weston_output, zoom);
158
159 weston_output_update_zoom(output);
160}
161
162WL_EXPORT void
163weston_output_activate_zoom(struct weston_output *output)
164{
165 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
166
167 if (output->zoom.active)
168 return;
169
170 output->zoom.active = 1;
171 output->disable_planes++;
172 wl_signal_add(&seat->pointer->motion_signal,
173 &output->zoom.motion_listener);
174}
175
Scott Moreau429490d2012-06-17 18:10:59 -0600176WL_EXPORT void
177weston_output_init_zoom(struct weston_output *output)
178{
179 output->zoom.active = 0;
180 output->zoom.increment = 0.07;
181 output->zoom.max_level = 0.95;
182 output->zoom.level = 0.0;
183 output->zoom.trans_x = 0.0;
184 output->zoom.trans_y = 0.0;
Scott Moreau429490d2012-06-17 18:10:59 -0600185 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
186 output->zoom.spring_z.friction = 1000;
187 output->zoom.animation_z.frame = weston_zoom_frame_z;
188 wl_list_init(&output->zoom.animation_z.link);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100189 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600190}