blob: 4216607e0c818da087ab0172403e95289aff8c03 [file] [log] [blame]
Scott Moreau429490d2012-06-17 18:10:59 -06001/*
2 * Copyright © 2012 Scott Moreau
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * 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:
Scott Moreau429490d2012-06-17 18:10:59 -060011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * 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.
Scott Moreau429490d2012-06-17 18:10:59 -060024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg583a2362012-06-25 17:13:58 -040028#include <stdlib.h>
29
Scott Moreau429490d2012-06-17 18:10:59 -060030#include "compositor.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040031#include "text-cursor-position-server-protocol.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070032#include "shared/helpers.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040033
Scott Moreau429490d2012-06-17 18:10:59 -060034static void
35weston_zoom_frame_z(struct weston_animation *animation,
36 struct weston_output *output, uint32_t msecs)
37{
38 if (animation->frame_counter <= 1)
39 output->zoom.spring_z.timestamp = msecs;
40
41 weston_spring_update(&output->zoom.spring_z, msecs);
42
43 if (output->zoom.spring_z.current > output->zoom.max_level)
44 output->zoom.spring_z.current = output->zoom.max_level;
45 else if (output->zoom.spring_z.current < 0.0)
46 output->zoom.spring_z.current = 0.0;
47
48 if (weston_spring_done(&output->zoom.spring_z)) {
Ville Syrjäläaa628d02012-11-16 11:48:47 +020049 if (output->zoom.active && output->zoom.level <= 0.0) {
Scott Moreau429490d2012-06-17 18:10:59 -060050 output->zoom.active = 0;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040051 output->disable_planes--;
Giulio Camuffo412b0242013-11-14 23:42:51 +010052 wl_list_remove(&output->zoom.motion_listener.link);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040053 }
Scott Moreau429490d2012-06-17 18:10:59 -060054 output->zoom.spring_z.current = output->zoom.level;
55 wl_list_remove(&animation->link);
56 wl_list_init(&animation->link);
57 }
58
59 output->dirty = 1;
60 weston_output_damage(output);
61}
62
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040063static struct weston_seat *
64weston_zoom_pick_seat(struct weston_compositor *compositor)
65{
66 return container_of(compositor->seat_list.next,
67 struct weston_seat, link);
68}
69
Scott Moreau429490d2012-06-17 18:10:59 -060070static void
71zoom_area_center_from_pointer(struct weston_output *output,
72 wl_fixed_t *x, wl_fixed_t *y)
73{
74 float level = output->zoom.spring_z.current;
75 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
76 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
Scott Moreau1bad5db2012-08-18 01:04:05 -060077 wl_fixed_t w = wl_fixed_from_int(output->width);
78 wl_fixed_t h = wl_fixed_from_int(output->height);
Scott Moreau429490d2012-06-17 18:10:59 -060079
Jason Ekstrand87535e22014-05-20 22:45:02 -050080 *x = (*x - offset_x) * level + w / 2;
81 *y = (*y - offset_y) * level + h / 2;
Scott Moreau429490d2012-06-17 18:10:59 -060082}
83
84static void
85weston_output_update_zoom_transform(struct weston_output *output)
86{
Scott Moreau429490d2012-06-17 18:10:59 -060087 float global_x, global_y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050088 wl_fixed_t x = output->zoom.current.x; /* global pointer coords */
Scott Moreau429490d2012-06-17 18:10:59 -060089 wl_fixed_t y = output->zoom.current.y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050090 float level;
Scott Moreau429490d2012-06-17 18:10:59 -060091
92 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -060093
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +020094 if (!output->zoom.active || level > output->zoom.max_level ||
95 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -060096 return;
97
Pekka Paalanen13fd4462015-03-10 11:13:14 +020098 zoom_area_center_from_pointer(output, &x, &y);
Scott Moreau429490d2012-06-17 18:10:59 -060099
100 global_x = wl_fixed_to_double(x);
101 global_y = wl_fixed_to_double(y);
102
Derek Foreman7cb916e2015-03-24 11:36:15 -0500103 output->zoom.trans_x = global_x - output->width / 2;
104 output->zoom.trans_y = global_y - output->height / 2;
Scott Moreau429490d2012-06-17 18:10:59 -0600105
Jason Ekstrand87535e22014-05-20 22:45:02 -0500106 if (output->zoom.trans_x < 0)
107 output->zoom.trans_x = 0;
108 if (output->zoom.trans_y < 0)
109 output->zoom.trans_y = 0;
110 if (output->zoom.trans_x > level * output->width)
111 output->zoom.trans_x = level * output->width;
112 if (output->zoom.trans_y > level * output->height)
113 output->zoom.trans_y = level * output->height;
Scott Moreau429490d2012-06-17 18:10:59 -0600114}
115
116static void
Pekka Paalanen98087082015-03-10 12:05:44 +0200117weston_zoom_transition(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600118{
Scott Moreau429490d2012-06-17 18:10:59 -0600119 if (output->zoom.level != output->zoom.spring_z.current) {
120 output->zoom.spring_z.target = output->zoom.level;
121 if (wl_list_empty(&output->zoom.animation_z.link)) {
122 output->zoom.animation_z.frame_counter = 0;
123 wl_list_insert(output->animation_list.prev,
124 &output->zoom.animation_z.link);
125 }
126 }
127
128 output->dirty = 1;
129 weston_output_damage(output);
130}
131
132WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500133weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600134{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400135 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600136
Pekka Paalanen13fd4462015-03-10 11:13:14 +0200137 output->zoom.current.x = seat->pointer->x;
138 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600139
Pekka Paalanen98087082015-03-10 12:05:44 +0200140 weston_zoom_transition(output);
Scott Moreau429490d2012-06-17 18:10:59 -0600141 weston_output_update_zoom_transform(output);
142}
143
Giulio Camuffo412b0242013-11-14 23:42:51 +0100144static void
145motion(struct wl_listener *listener, void *data)
146{
147 struct weston_output_zoom *zoom =
148 container_of(listener, struct weston_output_zoom, motion_listener);
149 struct weston_output *output =
150 container_of(zoom, struct weston_output, zoom);
151
152 weston_output_update_zoom(output);
153}
154
155WL_EXPORT void
156weston_output_activate_zoom(struct weston_output *output)
157{
158 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
159
160 if (output->zoom.active)
161 return;
162
163 output->zoom.active = 1;
164 output->disable_planes++;
165 wl_signal_add(&seat->pointer->motion_signal,
166 &output->zoom.motion_listener);
167}
168
Scott Moreau429490d2012-06-17 18:10:59 -0600169WL_EXPORT void
170weston_output_init_zoom(struct weston_output *output)
171{
172 output->zoom.active = 0;
173 output->zoom.increment = 0.07;
174 output->zoom.max_level = 0.95;
175 output->zoom.level = 0.0;
176 output->zoom.trans_x = 0.0;
177 output->zoom.trans_y = 0.0;
Scott Moreau429490d2012-06-17 18:10:59 -0600178 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
179 output->zoom.spring_z.friction = 1000;
180 output->zoom.animation_z.frame = weston_zoom_frame_z;
181 wl_list_init(&output->zoom.animation_z.link);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100182 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600183}