blob: 880df68cd5bd926ecb80213e6e1ae308ba2c3f97 [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"
32
Scott Moreau429490d2012-06-17 18:10:59 -060033static void
34weston_zoom_frame_z(struct weston_animation *animation,
35 struct weston_output *output, uint32_t msecs)
36{
37 if (animation->frame_counter <= 1)
38 output->zoom.spring_z.timestamp = msecs;
39
40 weston_spring_update(&output->zoom.spring_z, msecs);
41
42 if (output->zoom.spring_z.current > output->zoom.max_level)
43 output->zoom.spring_z.current = output->zoom.max_level;
44 else if (output->zoom.spring_z.current < 0.0)
45 output->zoom.spring_z.current = 0.0;
46
47 if (weston_spring_done(&output->zoom.spring_z)) {
Ville Syrjäläaa628d02012-11-16 11:48:47 +020048 if (output->zoom.active && output->zoom.level <= 0.0) {
Scott Moreau429490d2012-06-17 18:10:59 -060049 output->zoom.active = 0;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040050 output->disable_planes--;
Giulio Camuffo412b0242013-11-14 23:42:51 +010051 wl_list_remove(&output->zoom.motion_listener.link);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040052 }
Scott Moreau429490d2012-06-17 18:10:59 -060053 output->zoom.spring_z.current = output->zoom.level;
54 wl_list_remove(&animation->link);
55 wl_list_init(&animation->link);
56 }
57
58 output->dirty = 1;
59 weston_output_damage(output);
60}
61
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040062static struct weston_seat *
63weston_zoom_pick_seat(struct weston_compositor *compositor)
64{
65 return container_of(compositor->seat_list.next,
66 struct weston_seat, link);
67}
68
Scott Moreau429490d2012-06-17 18:10:59 -060069static void
70zoom_area_center_from_pointer(struct weston_output *output,
71 wl_fixed_t *x, wl_fixed_t *y)
72{
73 float level = output->zoom.spring_z.current;
74 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
75 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
Scott Moreau1bad5db2012-08-18 01:04:05 -060076 wl_fixed_t w = wl_fixed_from_int(output->width);
77 wl_fixed_t h = wl_fixed_from_int(output->height);
Scott Moreau429490d2012-06-17 18:10:59 -060078
Jason Ekstrand87535e22014-05-20 22:45:02 -050079 *x = (*x - offset_x) * level + w / 2;
80 *y = (*y - offset_y) * level + h / 2;
Scott Moreau429490d2012-06-17 18:10:59 -060081}
82
83static void
84weston_output_update_zoom_transform(struct weston_output *output)
85{
Scott Moreau429490d2012-06-17 18:10:59 -060086 float global_x, global_y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050087 wl_fixed_t x = output->zoom.current.x; /* global pointer coords */
Scott Moreau429490d2012-06-17 18:10:59 -060088 wl_fixed_t y = output->zoom.current.y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050089 float level;
Scott Moreau429490d2012-06-17 18:10:59 -060090
91 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -060092
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +020093 if (!output->zoom.active || level > output->zoom.max_level ||
94 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -060095 return;
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
Derek Foreman7cb916e2015-03-24 11:36:15 -0500102 output->zoom.trans_x = global_x - output->width / 2;
103 output->zoom.trans_y = global_y - output->height / 2;
Scott Moreau429490d2012-06-17 18:10:59 -0600104
Jason Ekstrand87535e22014-05-20 22:45:02 -0500105 if (output->zoom.trans_x < 0)
106 output->zoom.trans_x = 0;
107 if (output->zoom.trans_y < 0)
108 output->zoom.trans_y = 0;
109 if (output->zoom.trans_x > level * output->width)
110 output->zoom.trans_x = level * output->width;
111 if (output->zoom.trans_y > level * output->height)
112 output->zoom.trans_y = level * output->height;
Scott Moreau429490d2012-06-17 18:10:59 -0600113}
114
115static void
Pekka Paalanen98087082015-03-10 12:05:44 +0200116weston_zoom_transition(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600117{
Scott Moreau429490d2012-06-17 18:10:59 -0600118 if (output->zoom.level != output->zoom.spring_z.current) {
119 output->zoom.spring_z.target = output->zoom.level;
120 if (wl_list_empty(&output->zoom.animation_z.link)) {
121 output->zoom.animation_z.frame_counter = 0;
122 wl_list_insert(output->animation_list.prev,
123 &output->zoom.animation_z.link);
124 }
125 }
126
127 output->dirty = 1;
128 weston_output_damage(output);
129}
130
131WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500132weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600133{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400134 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600135
Pekka Paalanen13fd4462015-03-10 11:13:14 +0200136 output->zoom.current.x = seat->pointer->x;
137 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600138
Pekka Paalanen98087082015-03-10 12:05:44 +0200139 weston_zoom_transition(output);
Scott Moreau429490d2012-06-17 18:10:59 -0600140 weston_output_update_zoom_transform(output);
141}
142
Giulio Camuffo412b0242013-11-14 23:42:51 +0100143static void
144motion(struct wl_listener *listener, void *data)
145{
146 struct weston_output_zoom *zoom =
147 container_of(listener, struct weston_output_zoom, motion_listener);
148 struct weston_output *output =
149 container_of(zoom, struct weston_output, zoom);
150
151 weston_output_update_zoom(output);
152}
153
154WL_EXPORT void
155weston_output_activate_zoom(struct weston_output *output)
156{
157 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
158
159 if (output->zoom.active)
160 return;
161
162 output->zoom.active = 1;
163 output->disable_planes++;
164 wl_signal_add(&seat->pointer->motion_signal,
165 &output->zoom.motion_listener);
166}
167
Scott Moreau429490d2012-06-17 18:10:59 -0600168WL_EXPORT void
169weston_output_init_zoom(struct weston_output *output)
170{
171 output->zoom.active = 0;
172 output->zoom.increment = 0.07;
173 output->zoom.max_level = 0.95;
174 output->zoom.level = 0.0;
175 output->zoom.trans_x = 0.0;
176 output->zoom.trans_y = 0.0;
Scott Moreau429490d2012-06-17 18:10:59 -0600177 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
178 output->zoom.spring_z.friction = 1000;
179 output->zoom.animation_z.frame = weston_zoom_frame_z;
180 wl_list_init(&output->zoom.animation_z.link);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100181 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600182}