blob: 878ecc2fc7185d6ae4bab7c929790b60c9a8cfb4 [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>
Derek Foremana36eb502015-07-23 14:55:12 -050029#include <stdbool.h>
Kristian Høgsberg583a2362012-06-25 17:13:58 -040030
Scott Moreau429490d2012-06-17 18:10:59 -060031#include "compositor.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040032#include "text-cursor-position-server-protocol.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070033#include "shared/helpers.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040034
Scott Moreau429490d2012-06-17 18:10:59 -060035static void
36weston_zoom_frame_z(struct weston_animation *animation,
37 struct weston_output *output, uint32_t msecs)
38{
39 if (animation->frame_counter <= 1)
40 output->zoom.spring_z.timestamp = msecs;
41
42 weston_spring_update(&output->zoom.spring_z, msecs);
43
44 if (output->zoom.spring_z.current > output->zoom.max_level)
45 output->zoom.spring_z.current = output->zoom.max_level;
46 else if (output->zoom.spring_z.current < 0.0)
47 output->zoom.spring_z.current = 0.0;
48
49 if (weston_spring_done(&output->zoom.spring_z)) {
Ville Syrjäläaa628d02012-11-16 11:48:47 +020050 if (output->zoom.active && output->zoom.level <= 0.0) {
Derek Foremana36eb502015-07-23 14:55:12 -050051 output->zoom.active = false;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040052 output->disable_planes--;
Giulio Camuffo412b0242013-11-14 23:42:51 +010053 wl_list_remove(&output->zoom.motion_listener.link);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040054 }
Scott Moreau429490d2012-06-17 18:10:59 -060055 output->zoom.spring_z.current = output->zoom.level;
56 wl_list_remove(&animation->link);
57 wl_list_init(&animation->link);
58 }
59
60 output->dirty = 1;
61 weston_output_damage(output);
62}
63
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040064static struct weston_seat *
65weston_zoom_pick_seat(struct weston_compositor *compositor)
66{
67 return container_of(compositor->seat_list.next,
68 struct weston_seat, link);
69}
70
Scott Moreau429490d2012-06-17 18:10:59 -060071static void
72zoom_area_center_from_pointer(struct weston_output *output,
73 wl_fixed_t *x, wl_fixed_t *y)
74{
75 float level = output->zoom.spring_z.current;
76 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
77 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
Scott Moreau1bad5db2012-08-18 01:04:05 -060078 wl_fixed_t w = wl_fixed_from_int(output->width);
79 wl_fixed_t h = wl_fixed_from_int(output->height);
Scott Moreau429490d2012-06-17 18:10:59 -060080
Jason Ekstrand87535e22014-05-20 22:45:02 -050081 *x = (*x - offset_x) * level + w / 2;
82 *y = (*y - offset_y) * level + h / 2;
Scott Moreau429490d2012-06-17 18:10:59 -060083}
84
85static void
86weston_output_update_zoom_transform(struct weston_output *output)
87{
Scott Moreau429490d2012-06-17 18:10:59 -060088 float global_x, global_y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050089 wl_fixed_t x = output->zoom.current.x; /* global pointer coords */
Scott Moreau429490d2012-06-17 18:10:59 -060090 wl_fixed_t y = output->zoom.current.y;
Jason Ekstrand87535e22014-05-20 22:45:02 -050091 float level;
Scott Moreau429490d2012-06-17 18:10:59 -060092
93 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -060094
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +020095 if (!output->zoom.active || level > output->zoom.max_level ||
96 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -060097 return;
98
Pekka Paalanen13fd4462015-03-10 11:13:14 +020099 zoom_area_center_from_pointer(output, &x, &y);
Scott Moreau429490d2012-06-17 18:10:59 -0600100
101 global_x = wl_fixed_to_double(x);
102 global_y = wl_fixed_to_double(y);
103
Derek Foreman7cb916e2015-03-24 11:36:15 -0500104 output->zoom.trans_x = global_x - output->width / 2;
105 output->zoom.trans_y = global_y - output->height / 2;
Scott Moreau429490d2012-06-17 18:10:59 -0600106
Jason Ekstrand87535e22014-05-20 22:45:02 -0500107 if (output->zoom.trans_x < 0)
108 output->zoom.trans_x = 0;
109 if (output->zoom.trans_y < 0)
110 output->zoom.trans_y = 0;
111 if (output->zoom.trans_x > level * output->width)
112 output->zoom.trans_x = level * output->width;
113 if (output->zoom.trans_y > level * output->height)
114 output->zoom.trans_y = level * output->height;
Scott Moreau429490d2012-06-17 18:10:59 -0600115}
116
117static void
Pekka Paalanen98087082015-03-10 12:05:44 +0200118weston_zoom_transition(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600119{
Scott Moreau429490d2012-06-17 18:10:59 -0600120 if (output->zoom.level != output->zoom.spring_z.current) {
121 output->zoom.spring_z.target = output->zoom.level;
122 if (wl_list_empty(&output->zoom.animation_z.link)) {
123 output->zoom.animation_z.frame_counter = 0;
124 wl_list_insert(output->animation_list.prev,
125 &output->zoom.animation_z.link);
126 }
127 }
128
129 output->dirty = 1;
130 weston_output_damage(output);
131}
132
133WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500134weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600135{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400136 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600137
Pekka Paalanen13fd4462015-03-10 11:13:14 +0200138 output->zoom.current.x = seat->pointer->x;
139 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600140
Pekka Paalanen98087082015-03-10 12:05:44 +0200141 weston_zoom_transition(output);
Scott Moreau429490d2012-06-17 18:10:59 -0600142 weston_output_update_zoom_transform(output);
143}
144
Giulio Camuffo412b0242013-11-14 23:42:51 +0100145static void
146motion(struct wl_listener *listener, void *data)
147{
148 struct weston_output_zoom *zoom =
149 container_of(listener, struct weston_output_zoom, motion_listener);
150 struct weston_output *output =
151 container_of(zoom, struct weston_output, zoom);
152
153 weston_output_update_zoom(output);
154}
155
156WL_EXPORT void
157weston_output_activate_zoom(struct weston_output *output)
158{
159 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
160
161 if (output->zoom.active)
162 return;
163
Derek Foremana36eb502015-07-23 14:55:12 -0500164 output->zoom.active = true;
Giulio Camuffo412b0242013-11-14 23:42:51 +0100165 output->disable_planes++;
166 wl_signal_add(&seat->pointer->motion_signal,
167 &output->zoom.motion_listener);
168}
169
Scott Moreau429490d2012-06-17 18:10:59 -0600170WL_EXPORT void
171weston_output_init_zoom(struct weston_output *output)
172{
Derek Foremana36eb502015-07-23 14:55:12 -0500173 output->zoom.active = false;
Scott Moreau429490d2012-06-17 18:10:59 -0600174 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;
Scott Moreau429490d2012-06-17 18:10:59 -0600179 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);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100183 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600184}