1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
#include "wayfire/geometry.hpp"
#include "wayfire/plugins/common/input-grab.hpp"
#include "wayfire/scene-input.hpp"
#include "wayfire/txn/transaction-manager.hpp"
#include <wayfire/toplevel.hpp>
#include <cmath>
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/view.hpp>
#include <wayfire/core.hpp>
#include <wayfire/workspace-set.hpp>
#include <linux/input.h>
#include <wayfire/signal-definitions.hpp>
#include <wayfire/plugins/wobbly/wobbly-signal.hpp>
#include <wayfire/nonstd/wlroots-full.hpp>
#include <wlr/util/edges.h>
class wayfire_resize : public wf::per_output_plugin_instance_t, public wf::pointer_interaction_t,
public wf::touch_interaction_t
{
wf::signal::connection_t<wf::view_resize_request_signal> on_resize_request =
[=] (wf::view_resize_request_signal *request)
{
if (!request->view)
{
return;
}
auto touch = wf::get_core().get_touch_position(0);
if (!std::isnan(touch.x) && !std::isnan(touch.y))
{
is_using_touch = true;
} else
{
is_using_touch = false;
}
was_client_request = true;
preserve_aspect = false;
initiate(request->view, request->edges);
};
wf::signal::connection_t<wf::view_disappeared_signal> on_view_disappeared =
[=] (wf::view_disappeared_signal *ev)
{
if (ev->view == view)
{
view = nullptr;
input_pressed(WLR_BUTTON_RELEASED);
}
};
wf::button_callback activate_binding;
wf::button_callback activate_binding_preserve_aspect;
wayfire_toplevel_view view;
bool was_client_request, is_using_touch;
bool preserve_aspect = false;
wf::point_t grab_start;
wf::geometry_t grabbed_geometry;
uint32_t edges;
wf::option_wrapper_t<int> user_min_width{"resize/min_width"};
wf::option_wrapper_t<int> user_min_height{"resize/min_height"};
wf::option_wrapper_t<wf::buttonbinding_t> button{"resize/activate"};
wf::option_wrapper_t<wf::buttonbinding_t> button_preserve_aspect{
"resize/activate_preserve_aspect"};
std::unique_ptr<wf::input_grab_t> input_grab;
wf::plugin_activation_data_t grab_interface = {
.name = "resize",
.capabilities = wf::CAPABILITY_GRAB_INPUT | wf::CAPABILITY_MANAGE_DESKTOP,
};
public:
void init() override
{
input_grab = std::make_unique<wf::input_grab_t>("resize", output, nullptr, this, this);
activate_binding = [=] (auto)
{
return activate(false);
};
activate_binding_preserve_aspect = [=] (auto)
{
return activate(true);
};
output->add_button(button, &activate_binding);
output->add_button(button_preserve_aspect, &activate_binding_preserve_aspect);
grab_interface.cancel = [=] ()
{
input_pressed(WLR_BUTTON_RELEASED);
};
output->connect(&on_resize_request);
output->connect(&on_view_disappeared);
}
bool activate(bool should_preserve_aspect)
{
auto view = toplevel_cast(wf::get_core().get_cursor_focus_view());
if (view)
{
is_using_touch = false;
was_client_request = false;
preserve_aspect = should_preserve_aspect;
initiate(view);
}
return false;
}
void handle_pointer_button(const wlr_pointer_button_event& event) override
{
if ((event.state == WL_POINTER_BUTTON_STATE_RELEASED) && was_client_request &&
(event.button == BTN_LEFT))
{
return input_pressed(event.state);
}
if ((event.button != wf::buttonbinding_t(button).get_button()) &&
(event.button != wf::buttonbinding_t(button_preserve_aspect).get_button()))
{
return;
}
input_pressed(event.state);
}
void handle_pointer_motion(wf::pointf_t pointer_position, uint32_t time_ms) override
{
input_motion();
}
void handle_touch_up(uint32_t time_ms, int finger_id, wf::pointf_t lift_off_position) override
{
if (finger_id == 0)
{
input_pressed(WLR_BUTTON_RELEASED);
}
}
void handle_touch_motion(uint32_t time_ms, int finger_id, wf::pointf_t position) override
{
if (finger_id == 0)
{
input_motion();
}
}
/* Returns the currently used input coordinates in global compositor space */
wf::point_t get_global_input_coords()
{
wf::pointf_t input;
if (is_using_touch)
{
input = wf::get_core().get_touch_position(0);
} else
{
input = wf::get_core().get_cursor_position();
}
return {(int)input.x, (int)input.y};
}
/* Returns the currently used input coordinates in output-local space */
wf::point_t get_input_coords()
{
auto og = output->get_layout_geometry();
return get_global_input_coords() - wf::point_t{og.x, og.y};
}
/* Calculate resize edges, grab starts at (sx, sy), view's geometry is vg */
uint32_t calculate_edges(wf::geometry_t vg, int sx, int sy)
{
int view_x = sx - vg.x;
int view_y = sy - vg.y;
uint32_t edges = 0;
if (view_x < vg.width / 2)
{
edges |= WLR_EDGE_LEFT;
} else
{
edges |= WLR_EDGE_RIGHT;
}
if (view_y < vg.height / 2)
{
edges |= WLR_EDGE_TOP;
} else
{
edges |= WLR_EDGE_BOTTOM;
}
return edges;
}
bool initiate(wayfire_toplevel_view view, uint32_t forced_edges = 0)
{
if (!view || (view->role == wf::VIEW_ROLE_DESKTOP_ENVIRONMENT) ||
!view->is_mapped() || view->pending_fullscreen())
{
return false;
}
this->edges = forced_edges ?: calculate_edges(view->get_bounding_box(),
get_input_coords().x, get_input_coords().y);
if ((edges == 0) || !(view->get_allowed_actions() & wf::VIEW_ALLOW_RESIZE))
{
return false;
}
if (!output->activate_plugin(&grab_interface))
{
return false;
}
input_grab->set_wants_raw_input(true);
input_grab->grab_input(wf::scene::layer::OVERLAY);
grab_start = get_input_coords();
grabbed_geometry = view->get_geometry();
if (view->pending_tiled_edges())
{
view->toplevel()->pending().tiled_edges = 0;
}
this->view = view;
auto og = view->get_bounding_box();
int anchor_x = og.x;
int anchor_y = og.y;
if (edges & WLR_EDGE_LEFT)
{
anchor_x += og.width;
}
if (edges & WLR_EDGE_TOP)
{
anchor_y += og.height;
}
start_wobbly(view, anchor_x, anchor_y);
wf::get_core().set_cursor(wlr_xcursor_get_resize_name((wlr_edges)edges));
return true;
}
void input_pressed(uint32_t state)
{
if (state != WLR_BUTTON_RELEASED)
{
return;
}
input_grab->ungrab_input();
output->deactivate_plugin(&grab_interface);
if (view)
{
end_wobbly(view);
wf::view_change_workspace_signal workspace_may_changed;
workspace_may_changed.view = this->view;
workspace_may_changed.to = output->wset()->get_current_workspace();
workspace_may_changed.old_workspace_valid = false;
output->emit(&workspace_may_changed);
}
}
// Convert resize edges to gravity
uint32_t calculate_gravity()
{
uint32_t gravity = 0;
if (edges & WLR_EDGE_LEFT)
{
gravity |= WLR_EDGE_RIGHT;
}
if (edges & WLR_EDGE_RIGHT)
{
gravity |= WLR_EDGE_LEFT;
}
if (edges & WLR_EDGE_TOP)
{
gravity |= WLR_EDGE_BOTTOM;
}
if (edges & WLR_EDGE_BOTTOM)
{
gravity |= WLR_EDGE_TOP;
}
return gravity;
}
wf::dimensions_t calculate_min_size()
{
// Min size, if not set to something larger, is 1x1 + decoration size
wf::dimensions_t min_size = view->toplevel()->get_min_size();
min_size.width = std::max(1, min_size.width);
min_size.height = std::max(1, min_size.height);
min_size = wf::expand_dimensions_by_margins(min_size,
view->toplevel()->pending().margins);
min_size.width = std::max(min_size.width, static_cast<int>(user_min_width));
min_size.height = std::max(min_size.height, static_cast<int>(user_min_height));
return min_size;
}
wf::dimensions_t calculate_max_size(wf::dimensions_t min)
{
// Max size is whatever is set by the client, if not set, then it is MAX_INT
wf::dimensions_t max_size = view->toplevel()->get_max_size();
int64_t width, height;
if (max_size.width > 0)
{
width = static_cast<int64_t>(max_size.width) +
view->toplevel()->pending().margins.left +
view->toplevel()->pending().margins.right;
} else
{
width = std::numeric_limits<decltype(max_size.width)>::max();
}
if (max_size.height > 0)
{
height = static_cast<int64_t>(max_size.height) +
view->toplevel()->pending().margins.top +
view->toplevel()->pending().margins.bottom;
} else
{
height = std::numeric_limits<decltype(max_size.height)>::max();
}
// Sanitize values in case desired.width/height gets negative for example.
max_size.width = static_cast<decltype(max_size.width)>(std::clamp(width,
static_cast<int64_t>(min.width),
static_cast<int64_t>(std::numeric_limits<decltype(max_size.width)>::max())));
max_size.height = static_cast<decltype(max_size.height)>(std::clamp(height,
static_cast<int64_t>(min.height),
static_cast<int64_t>(std::numeric_limits<decltype(max_size.height)>::max())));
return max_size;
}
bool does_shrink(int dx, int dy)
{
if (std::abs(dx) > std::abs(dy))
{
return dx < 0;
} else
{
return dy < 0;
}
}
void input_motion()
{
auto input = get_input_coords();
int dx = input.x - grab_start.x;
int dy = input.y - grab_start.y;
wf::geometry_t desired = grabbed_geometry;
double ratio = 1.0;
if (preserve_aspect)
{
ratio = (double)desired.width / desired.height;
}
if (edges & WLR_EDGE_LEFT)
{
desired.x += dx;
desired.width -= dx;
} else if (edges & WLR_EDGE_RIGHT)
{
desired.width += dx;
}
if (edges & WLR_EDGE_TOP)
{
desired.y += dy;
desired.height -= dy;
} else if (edges & WLR_EDGE_BOTTOM)
{
desired.height += dy;
}
auto min_size = calculate_min_size();
auto max_size = calculate_max_size(min_size);
auto desired_unconstrained = desired;
if (preserve_aspect)
{
float new_ratio = 1.0 * desired.width / desired.height;
if ((new_ratio < ratio) ^ does_shrink(desired.width - grabbed_geometry.width,
desired.height - grabbed_geometry.height))
{
// If we do not shrink: the window is taller than it should be, so expand width first.
// If we do shrink: the window is wider than it should be, so shrink width first.
desired.width = std::clamp(int(desired.height * ratio),
min_size.width, max_size.width);
desired.height = std::clamp(int(desired.width / ratio),
min_size.height, max_size.height);
// Clamp once more to ensure we fit the min/max sizes.
desired.width = std::clamp(int(desired.height * ratio),
min_size.width, max_size.width);
} else
{
// The window is wider than it should be, so expand height first.
desired.height = std::clamp(int(desired.width / ratio),
min_size.height, max_size.height);
desired.width = std::clamp(int(desired.height * ratio),
min_size.width, max_size.width);
// Clamp once more to ensure we fit the min/max sizes.
desired.height = std::clamp(int(desired.width / ratio),
min_size.height, max_size.height);
}
} else
{
desired.width = std::clamp(desired.width, min_size.width, max_size.width);
desired.height = std::clamp(desired.height, min_size.height, max_size.height);
}
// If we had to change the size due to ratio/min/max constraints, make sure to keep the gravity
// correct.
if (edges & WLR_EDGE_LEFT)
{
desired.x += desired_unconstrained.width - desired.width;
}
if (edges & WLR_EDGE_TOP)
{
desired.y += desired_unconstrained.height - desired.height;
}
if (wf::dimensions(view->toplevel()->pending().geometry) != wf::dimensions(desired))
{
view->toplevel()->pending().gravity = calculate_gravity();
view->toplevel()->pending().geometry = desired;
wf::get_core().tx_manager->schedule_object(view->toplevel());
}
}
void fini() override
{
if (input_grab->is_grabbed())
{
input_pressed(WLR_BUTTON_RELEASED);
}
output->rem_binding(&activate_binding);
output->rem_binding(&activate_binding_preserve_aspect);
}
};
DECLARE_WAYFIRE_PLUGIN(wf::per_output_plugin_t<wayfire_resize>);
|