Skip to content

Commit

Permalink
Use shared pointers instead of raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
serebit committed Apr 13, 2024
1 parent 02fe2cb commit f0fcf8e
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/foreign_toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void foreign_toplevel_handle_request_activate_notify(wl_listener* listene
const ForeignToplevelHandle& handle = magpie_container_of(listener, handle, request_activate);

handle.view.set_minimized(false);
handle.view.get_server().focus_view(&handle.view);
handle.view.get_server().focus_view(std::dynamic_pointer_cast<View>(handle.view.shared_from_this()));
}

static void foreign_toplevel_handle_request_close_notify(wl_listener* listener, void*) {
Expand Down
6 changes: 2 additions & 4 deletions src/input/constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ static void constraint_destroy_notify(wl_listener* listener, void*) {
PointerConstraint& constraint = magpie_container_of(listener, constraint, destroy);

auto& current_constraint = constraint.seat.current_constraint;
if (current_constraint.has_value() && &current_constraint.value().get().wlr == &constraint.wlr) {
constraint.seat.cursor.warp_to_constraint(current_constraint.value());
if (current_constraint != nullptr && &current_constraint->wlr == &constraint.wlr) {
constraint.seat.cursor.warp_to_constraint(*current_constraint);
constraint.deactivate();
current_constraint.reset();
}

delete &constraint;
}

PointerConstraint::PointerConstraint(Seat& seat, wlr_pointer_constraint_v1& wlr) noexcept
Expand Down
52 changes: 33 additions & 19 deletions src/input/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ void Cursor::process_resize(const uint32_t time) const {
* you'd wait for the client to prepare a buffer at the new size, then
* commit any movement that was prepared.
*/
View& view = *seat.server.grabbed_view;
const wlr_box min_size = view.get_min_size();
const wlr_box max_size = view.get_max_size();
std::shared_ptr<View> view = seat.server.grabbed_view.lock();

if (view == nullptr) {
wlr_log(WLR_ERROR, "Attempted to process_resize without a grabbed view");
return;
}

const wlr_box min_size = view->get_min_size();
const wlr_box max_size = view->get_max_size();
const double border_x = wlr.x - seat.server.grab_x;
const double border_y = wlr.y - seat.server.grab_y;
int32_t new_left = seat.server.grab_geobox.x;
Expand Down Expand Up @@ -67,14 +73,14 @@ void Cursor::process_resize(const uint32_t time) const {
}
}

const wlr_box geo_box = view.get_geometry();
const wlr_box geo_box = view->get_geometry();
const int32_t new_width = std::clamp(new_right - new_left, min_size.width, max_size.width);
const int32_t new_height = std::clamp(new_bottom - new_top, min_size.height, max_size.height);
const int32_t new_x = new_width == view.current.width ? view.current.x : new_left - geo_box.x;
const int32_t new_y = new_height == view.current.height ? view.current.y : new_top - geo_box.y;
view.set_geometry(new_x, new_y, new_width, new_height);
const int32_t new_x = new_width == view->current.width ? view->current.x : new_left - geo_box.x;
const int32_t new_y = new_height == view->current.height ? view->current.y : new_top - geo_box.y;
view->set_geometry(new_x, new_y, new_width, new_height);

view.update_outputs();
view->update_outputs();
}

void Cursor::process_move(const uint32_t time) {
Expand All @@ -83,12 +89,18 @@ void Cursor::process_move(const uint32_t time) {
set_image("fleur");

/* Move the grabbed view to the new position. */
View& view = *seat.server.grabbed_view;
std::shared_ptr<View> view = seat.server.grabbed_view.lock();

if (view == nullptr) {
wlr_log(WLR_ERROR, "Attempted to process_move without a grabbed view");
return;
}

const auto new_x = static_cast<int32_t>(std::round(wlr.x - seat.server.grab_x));
const auto new_y = static_cast<int32_t>(std::round(std::fmax(wlr.y - seat.server.grab_y, 0)));
view.set_position(new_x, new_y);
view->set_position(new_x, new_y);

view.update_outputs();
view->update_outputs();
}

/* This event is forwarded by the cursor when a pointer emits an axis event,
Expand Down Expand Up @@ -168,7 +180,7 @@ static void cursor_button_notify(wl_listener* listener, void* data) {
double sx, sy;

wlr_surface* surface = nullptr;
Surface* magpie_surface = server.surface_at(cursor.wlr.x, cursor.wlr.y, &surface, &sx, &sy);
auto magpie_surface = server.surface_at(cursor.wlr.x, cursor.wlr.y, &surface, &sx, &sy).lock();

if (event->state == WLR_BUTTON_RELEASED) {
/* If you released any buttons, we exit interactive move/resize mode. */
Expand All @@ -177,7 +189,7 @@ static void cursor_button_notify(wl_listener* listener, void* data) {
}
} else if (magpie_surface != nullptr && magpie_surface->is_view()) {
/* Focus that client if the button was _pressed_ */
server.focus_view(dynamic_cast<View*>(magpie_surface), surface);
server.focus_view(std::dynamic_pointer_cast<View>(magpie_surface), surface);
} else {
server.focus_view(nullptr);
}
Expand Down Expand Up @@ -402,7 +414,7 @@ void Cursor::process_motion(const uint32_t time) {
/* Otherwise, find the view under the pointer and send the event along. */
double sx, sy;
wlr_surface* surface = nullptr;
const Surface* magpie_surface = seat.server.surface_at(wlr.x, wlr.y, &surface, &sx, &sy);
auto magpie_surface = seat.server.surface_at(wlr.x, wlr.y, &surface, &sx, &sy).lock();
if (magpie_surface == nullptr) {
/* If there's no view under the cursor, set the cursor image to a
* default. This is what makes the cursor image appear when you move it
Expand All @@ -419,7 +431,7 @@ void Cursor::process_motion(const uint32_t time) {
* a window.
*
* Note that wlroots will avoid sending duplicate enter/motion events if
* the surface has already has pointer focus or if the client is already
* the surface has already had pointer focus or if the client is already
* aware of the coordinates passed.
*/
current_image = "";
Expand All @@ -437,24 +449,26 @@ void Cursor::reset_mode() {
set_image("left_ptr");
}
mode = MAGPIE_CURSOR_PASSTHROUGH;
seat.server.grabbed_view = nullptr;
seat.server.grabbed_view.reset();
}

void Cursor::warp_to_constraint(const PointerConstraint& constraint) const {
if (seat.server.focused_view == nullptr) {
auto focused_view = seat.server.focused_view.lock();

if (focused_view == nullptr) {
// only warp to constraints tied to views...
return;
}

if (seat.server.focused_view->get_wlr_surface() != constraint.wlr.surface) {
if (focused_view->get_wlr_surface() != constraint.wlr.surface) {
return;
}

if (constraint.wlr.current.committed & WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT) {
const double x = constraint.wlr.current.cursor_hint.x;
const double y = constraint.wlr.current.cursor_hint.y;

wlr_cursor_warp(&wlr, nullptr, seat.server.focused_view->current.x + x, seat.server.focused_view->current.y + y);
wlr_cursor_warp(&wlr, nullptr, focused_view->current.x + x, focused_view->current.y + y);
wlr_seat_pointer_warp(seat.wlr, x, y);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
static void keyboard_handle_destroy(wl_listener* listener, void*) {
Keyboard& keyboard = magpie_container_of(listener, keyboard, destroy);

std::vector<Keyboard*>& keyboards = keyboard.seat.keyboards;
(void) std::ranges::remove(keyboards, &keyboard).begin();

delete &keyboard;
auto& keyboards = keyboard.seat.keyboards;
(void) std::ranges::remove(keyboards, keyboard.shared_from_this());
}

static bool handle_compositor_keybinding(const Keyboard& keyboard, const uint32_t modifiers, const xkb_keysym_t sym) {
Expand Down
29 changes: 16 additions & 13 deletions src/input/seat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Seat::Seat(Server& server) noexcept : listeners(*this), server(server), cursor(*
void Seat::new_input_device(wlr_input_device* device) {
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
keyboards.push_back(new Keyboard(*this, *wlr_keyboard_from_input_device(device)));
keyboards.push_back(std::make_shared<Keyboard>(*this, *wlr_keyboard_from_input_device(device)));
break;
case WLR_INPUT_DEVICE_POINTER:
case WLR_INPUT_DEVICE_TABLET_TOOL:
Expand All @@ -157,40 +157,43 @@ void Seat::new_input_device(wlr_input_device* device) {
}

void Seat::set_constraint(wlr_pointer_constraint_v1* wlr_constraint) {
if (current_constraint.has_value()) {
if (&current_constraint.value().get().wlr == wlr_constraint) {
if (current_constraint != nullptr) {
if (&current_constraint->wlr == wlr_constraint) {
// we already have this constraint marked as the current constraint
return;
}

cursor.warp_to_constraint(current_constraint.value());
cursor.warp_to_constraint(*current_constraint);
current_constraint.reset();
}

if (wlr_constraint != nullptr) {
current_constraint = *new PointerConstraint(*this, *wlr_constraint);
current_constraint.value().get().activate();
current_constraint = std::make_shared<PointerConstraint>(*this, *wlr_constraint);
current_constraint->activate();
}
}

void Seat::apply_constraint(const wlr_pointer* pointer, double* dx, double* dy) const {
if (!current_constraint.has_value() || pointer->base.type != WLR_INPUT_DEVICE_POINTER) {
if (current_constraint == nullptr || pointer->base.type != WLR_INPUT_DEVICE_POINTER) {
return;
}

if (server.focused_view == nullptr) {
auto focused_view = server.focused_view.lock();

if (focused_view == nullptr) {
wlr_log(WLR_DEBUG, "Attempted to apply a pointer constraint without a focused view");
return;
}

double x = cursor.wlr.x;
double y = cursor.wlr.y;

x -= server.focused_view->current.x;
y -= server.focused_view->current.y;
x -= focused_view->current.x;
y -= focused_view->current.y;

double confined_x = 0;
double confined_y = 0;
if (!wlr_region_confine(&current_constraint->get().wlr.region, x, y, x + *dx, y + *dy, &confined_x, &confined_y)) {
if (!wlr_region_confine(&current_constraint->wlr.region, x, y, x + *dx, y + *dy, &confined_x, &confined_y)) {
wlr_log(WLR_ERROR, "Couldn't confine\n");
return;
}
Expand All @@ -200,6 +203,6 @@ void Seat::apply_constraint(const wlr_pointer* pointer, double* dx, double* dy)
}

bool Seat::is_pointer_locked(const wlr_pointer* pointer) const {
return current_constraint.has_value() && pointer->base.type == WLR_INPUT_DEVICE_POINTER &&
current_constraint->get().wlr.type == WLR_POINTER_CONSTRAINT_V1_LOCKED;
return current_constraint != nullptr && pointer->base.type == WLR_INPUT_DEVICE_POINTER &&
current_constraint->wlr.type == WLR_POINTER_CONSTRAINT_V1_LOCKED;
}
4 changes: 2 additions & 2 deletions src/input/seat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Seat final : public std::enable_shared_from_this<Seat> {
Server& server;
wlr_seat* wlr;
Cursor cursor;
std::vector<Keyboard*> keyboards;
std::vector<std::shared_ptr<Keyboard>> keyboards;
wlr_virtual_pointer_manager_v1* virtual_pointer_mgr;
wlr_virtual_keyboard_manager_v1* virtual_keyboard_mgr;
wlr_pointer_constraints_v1* pointer_constraints;
std::optional<std::reference_wrapper<PointerConstraint>> current_constraint = {};
std::shared_ptr<PointerConstraint> current_constraint;

explicit Seat(Server& server) noexcept;

Expand Down
8 changes: 3 additions & 5 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ static void output_frame_notify(wl_listener* listener, void*) {
static void output_destroy_notify(wl_listener* listener, void*) {
Output& output = magpie_container_of(listener, output, destroy);

output.server.outputs.erase(&output);
for (const auto* layer : std::as_const(output.layers)) {
for (const auto& layer : output.layers) {
wlr_layer_surface_v1_destroy(&layer->wlr);
}

delete &output;
output.server.outputs.erase(output.shared_from_this());
}

Output::Output(Server& server, wlr_output& wlr) noexcept : listeners(*this), server(server), wlr(wlr) {
Expand Down Expand Up @@ -105,7 +103,7 @@ void Output::update_layout() {

usable_area = full_area;

for (const auto* layer : std::as_const(layers)) {
for (const auto& layer : layers) {
wlr_scene_layer_surface_v1_configure(layer->scene_surface, &full_area, &usable_area);
}
}
2 changes: 1 addition & 1 deletion src/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Output final : public std::enable_shared_from_this<Output> {
wlr_output& wlr;
wlr_box full_area = {};
wlr_box usable_area = {};
std::set<Layer*> layers;
std::set<std::shared_ptr<Layer>> layers;
bool is_leased = false;

Output(Server& server, wlr_output& wlr) noexcept;
Expand Down
28 changes: 14 additions & 14 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

void Server::focus_view(View* view, wlr_surface* surface) {
void Server::focus_view(std::shared_ptr<View>&& view, wlr_surface* surface) {
const wlr_surface* prev_surface = seat->wlr->keyboard_state.focused_surface;
if (prev_surface == surface && surface != nullptr) {
/* Don't re-focus an already focused surface. */
Expand Down Expand Up @@ -63,7 +63,7 @@ void Server::focus_view(View* view, wlr_surface* surface) {
/* Move the view to the front */
wlr_scene_node_raise_to_top(view->scene_node);
std::ranges::remove(views, view);
for (auto* it : std::as_const(views)) {
for (const auto& it : views) {
it->set_activated(false);
}

Expand All @@ -88,18 +88,18 @@ void Server::focus_view(View* view, wlr_surface* surface) {
seat->set_constraint(constraint);
}

Surface* Server::surface_at(const double lx, const double ly, wlr_surface** wlr, double* sx, double* sy) const {
std::weak_ptr<Surface> Server::surface_at(const double lx, const double ly, wlr_surface** wlr, double* sx, double* sy) const {
/* This returns the topmost node in the scene at the given layout coords.
* we only care about surface nodes as we are specifically looking for a
* surface in the surface tree of a magpie_view. */
wlr_scene_node* node = wlr_scene_node_at(&scene->tree.node, lx, ly, sx, sy);
if (node == nullptr || node->type != WLR_SCENE_NODE_BUFFER) {
return nullptr;
return {};
}
wlr_scene_buffer* scene_buffer = wlr_scene_buffer_from_node(node);
const wlr_scene_surface* scene_surface = wlr_scene_surface_try_from_buffer(scene_buffer);
if (!scene_surface) {
return nullptr;
return {};
}

*wlr = scene_surface->surface;
Expand All @@ -111,10 +111,10 @@ Surface* Server::surface_at(const double lx, const double ly, wlr_surface** wlr,
}

if (tree != nullptr) {
return static_cast<Surface*>(tree->node.data);
return static_cast<Surface*>(tree->node.data)->weak_from_this();
}

return nullptr;
return {};
}

/* This event is raised by the backend when a new output (aka a display or
Expand Down Expand Up @@ -151,7 +151,7 @@ static void new_output_notify(wl_listener* listener, void* data) {
}

/* Allocates and configures our state for this output */
auto* output = new Output(server, *new_output);
auto output = std::make_shared<Output>(server, *new_output);
server.outputs.emplace(output);

/* Adds this to the output layout. The add_auto function arranges outputs
Expand Down Expand Up @@ -200,10 +200,10 @@ static void new_xdg_surface_notify(wl_listener* listener, void* data) {
const auto& xdg_surface = *static_cast<wlr_xdg_surface*>(data);

if (xdg_surface.role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
server.views.emplace_back(new XdgView(server, *xdg_surface.toplevel));
server.views.emplace_back(std::make_shared<XdgView>(server, *xdg_surface.toplevel));
} else if (xdg_surface.role == WLR_XDG_SURFACE_ROLE_POPUP) {
auto* surface = static_cast<Surface*>(xdg_surface.popup->parent->data);
surface->popups.emplace(new Popup(*surface, *xdg_surface.popup));
surface->popups.emplace(std::make_shared<Popup>(*surface, *xdg_surface.popup));
}
}

Expand All @@ -225,7 +225,7 @@ static void new_layer_surface_notify(wl_listener* listener, void* data) {
output = static_cast<Output*>(layer_surface.output->data);
}

output->layers.emplace(new Layer(*output, layer_surface));
output->layers.emplace(std::make_shared<Layer>(*output, layer_surface));
}

static void request_activation_notify(wl_listener* listener, void* data) {
Expand All @@ -244,7 +244,7 @@ static void request_activation_notify(wl_listener* listener, void* data) {

auto* view = dynamic_cast<View*>(static_cast<Surface*>(xdg_surface->surface->data));
if (view != nullptr && xdg_surface->surface->mapped) {
server.focus_view(view, xdg_surface->surface);
server.focus_view(std::dynamic_pointer_cast<View>(view->shared_from_this()), xdg_surface->surface);
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ void output_layout_change_notify(wl_listener* listener, void*) {

wlr_output_configuration_v1* config = wlr_output_configuration_v1_create();

for (const auto* output : std::as_const(server.outputs)) {
for (const auto& output : server.outputs) {
wlr_output_configuration_head_v1* head = wlr_output_configuration_head_v1_create(config, &output->wlr);

wlr_box box = {};
Expand Down Expand Up @@ -357,7 +357,7 @@ void output_manager_apply_notify(wl_listener* listener, void* data) {
wlr_output_configuration_v1_send_succeeded(&config);
wlr_output_configuration_v1_destroy(&config);

for (auto* output : server.outputs) {
for (const auto& output : server.outputs) {
wlr_xcursor_manager_load(server.seat->cursor.cursor_mgr, output->wlr.scale);
}

Expand Down
Loading

0 comments on commit f0fcf8e

Please sign in to comment.