blob: bee038b1d8a6c32ed66a9af28cf5ba03b7319a34 [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
Jason Ekstrand87535e22014-05-20 22:45:02 -050076 *x = (*x - offset_x) * level + w / 2;
77 *y = (*y - offset_y) * level + h / 2;
Scott Moreau429490d2012-06-17 18:10:59 -060078}
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;
Jason Ekstrand87535e22014-05-20 22:45:02 -050084 wl_fixed_t x = output->zoom.current.x; /* global pointer coords */
Scott Moreau429490d2012-06-17 18:10:59 -060085 wl_fixed_t y = output->zoom.current.y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050086 float level;
Scott Moreau429490d2012-06-17 18:10:59 -060087
88 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -060089
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +020090 if (!output->zoom.active || level > output->zoom.max_level ||
91 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -060092 return;
93
Pekka Paalanen13fd4462015-03-10 11:13:14 +020094 zoom_area_center_from_pointer(output, &x, &y);
Scott Moreau429490d2012-06-17 18:10:59 -060095
96 global_x = wl_fixed_to_double(x);
97 global_y = wl_fixed_to_double(y);
98
Derek Foreman7cb916e2015-03-24 11:36:15 -050099 output->zoom.trans_x = global_x - output->width / 2;
100 output->zoom.trans_y = global_y - output->height / 2;
Scott Moreau429490d2012-06-17 18:10:59 -0600101
Jason Ekstrand87535e22014-05-20 22:45:02 -0500102 if (output->zoom.trans_x < 0)
103 output->zoom.trans_x = 0;
104 if (output->zoom.trans_y < 0)
105 output->zoom.trans_y = 0;
106 if (output->zoom.trans_x > level * output->width)
107 output->zoom.trans_x = level * output->width;
108 if (output->zoom.trans_y > level * output->height)
109 output->zoom.trans_y = level * output->height;
Scott Moreau429490d2012-06-17 18:10:59 -0600110}
111
112static void
Pekka Paalanen98087082015-03-10 12:05:44 +0200113weston_zoom_transition(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600114{
Scott Moreau429490d2012-06-17 18:10:59 -0600115 if (output->zoom.level != output->zoom.spring_z.current) {
116 output->zoom.spring_z.target = output->zoom.level;
117 if (wl_list_empty(&output->zoom.animation_z.link)) {
118 output->zoom.animation_z.frame_counter = 0;
119 wl_list_insert(output->animation_list.prev,
120 &output->zoom.animation_z.link);
121 }
122 }
123
124 output->dirty = 1;
125 weston_output_damage(output);
126}
127
128WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500129weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600130{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400131 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600132
Pekka Paalanen13fd4462015-03-10 11:13:14 +0200133 output->zoom.current.x = seat->pointer->x;
134 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600135
Pekka Paalanen98087082015-03-10 12:05:44 +0200136 weston_zoom_transition(output);
Scott Moreau429490d2012-06-17 18:10:59 -0600137 weston_output_update_zoom_transform(output);
138}
139
Giulio Camuffo412b0242013-11-14 23:42:51 +0100140static void
141motion(struct wl_listener *listener, void *data)
142{
143 struct weston_output_zoom *zoom =
144 container_of(listener, struct weston_output_zoom, motion_listener);
145 struct weston_output *output =
146 container_of(zoom, struct weston_output, zoom);
147
148 weston_output_update_zoom(output);
149}
150
151WL_EXPORT void
152weston_output_activate_zoom(struct weston_output *output)
153{
154 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
155
156 if (output->zoom.active)
157 return;
158
159 output->zoom.active = 1;
160 output->disable_planes++;
161 wl_signal_add(&seat->pointer->motion_signal,
162 &output->zoom.motion_listener);
163}
164
Scott Moreau429490d2012-06-17 18:10:59 -0600165WL_EXPORT void
166weston_output_init_zoom(struct weston_output *output)
167{
168 output->zoom.active = 0;
169 output->zoom.increment = 0.07;
170 output->zoom.max_level = 0.95;
171 output->zoom.level = 0.0;
172 output->zoom.trans_x = 0.0;
173 output->zoom.trans_y = 0.0;
Scott Moreau429490d2012-06-17 18:10:59 -0600174 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
175 output->zoom.spring_z.friction = 1000;
176 output->zoom.animation_z.frame = weston_zoom_frame_z;
177 wl_list_init(&output->zoom.animation_z.link);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100178 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600179}