Skip to content

Commit

Permalink
References are pretty cool
Browse files Browse the repository at this point in the history
  • Loading branch information
serebit committed Nov 4, 2023
1 parent 9111452 commit 6d3c28e
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 165 deletions.
4 changes: 2 additions & 2 deletions src/foreign_toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void ForeignToplevelHandle::set_fullscreen(const bool fullscreen) {
}

void ForeignToplevelHandle::output_enter(const Output& output) {
wlr_foreign_toplevel_handle_v1_output_enter(&handle, output.wlr);
wlr_foreign_toplevel_handle_v1_output_enter(&handle, &output.wlr);
}

void ForeignToplevelHandle::output_leave(const Output& output) {
wlr_foreign_toplevel_handle_v1_output_leave(&handle, output.wlr);
wlr_foreign_toplevel_handle_v1_output_leave(&handle, &output.wlr);
}
16 changes: 8 additions & 8 deletions src/input/constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void constraint_destroy_notify(wl_listener* listener, void* data) {
(void) data;

auto& current_constraint = constraint.seat.current_constraint;
if (current_constraint.has_value() && current_constraint.value().get().wlr == constraint.wlr) {
if (current_constraint.has_value() && &current_constraint.value().get().wlr == &constraint.wlr) {
constraint.seat.cursor.warp_to_constraint(current_constraint.value());
constraint.deactivate();
current_constraint.reset();
Expand All @@ -33,14 +33,14 @@ static void constraint_destroy_notify(wl_listener* listener, void* data) {
delete &constraint;
}

PointerConstraint::PointerConstraint(Seat& seat, wlr_pointer_constraint_v1* constraint) noexcept
: listeners(*this), seat(seat), wlr(constraint) {
PointerConstraint::PointerConstraint(Seat& seat, wlr_pointer_constraint_v1& wlr) noexcept
: listeners(*this), seat(seat), wlr(wlr) {
listeners.set_region.notify = constraint_set_region_notify;
wl_signal_add(&constraint->events.set_region, &listeners.set_region);
wl_signal_add(&wlr.events.set_region, &listeners.set_region);
listeners.surface_commit.notify = constraint_surface_commit_notify;
wl_signal_add(&constraint->surface->events.commit, &listeners.surface_commit);
wl_signal_add(&wlr.surface->events.commit, &listeners.surface_commit);
listeners.destroy.notify = constraint_destroy_notify;
wl_signal_add(&constraint->events.destroy, &listeners.destroy);
wl_signal_add(&wlr.events.destroy, &listeners.destroy);
}

PointerConstraint::~PointerConstraint() noexcept {
Expand All @@ -50,9 +50,9 @@ PointerConstraint::~PointerConstraint() noexcept {
}

void PointerConstraint::activate() const {
wlr_pointer_constraint_v1_send_activated(wlr);
wlr_pointer_constraint_v1_send_activated(&wlr);
}

void PointerConstraint::deactivate() const {
wlr_pointer_constraint_v1_send_deactivated(wlr);
wlr_pointer_constraint_v1_send_deactivated(&wlr);
}
4 changes: 2 additions & 2 deletions src/input/constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class PointerConstraint {

public:
Seat& seat;
wlr_pointer_constraint_v1* wlr;
wlr_pointer_constraint_v1& wlr;

PointerConstraint(Seat& seat, wlr_pointer_constraint_v1* wlr) noexcept;
PointerConstraint(Seat& seat, wlr_pointer_constraint_v1& wlr) noexcept;
~PointerConstraint() noexcept;

void activate() const;
Expand Down
106 changes: 52 additions & 54 deletions src/input/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void Cursor::process_resize(const uint32_t time) {
* commit any movement that was prepared.
*/
View& view = *seat.server.grabbed_view;
double border_x = cursor->x - seat.server.grab_x;
double border_y = cursor->y - seat.server.grab_y;
double border_x = wlr.x - seat.server.grab_x;
double border_y = wlr.y - seat.server.grab_y;
int new_left = seat.server.grab_geobox.x;
int new_right = seat.server.grab_geobox.x + seat.server.grab_geobox.width;
int new_top = seat.server.grab_geobox.y;
Expand Down Expand Up @@ -75,8 +75,8 @@ void Cursor::process_move(const uint32_t time) {

/* Move the grabbed view to the new position. */
View* view = seat.server.grabbed_view;
view->current.x = cursor->x - seat.server.grab_x;
view->current.y = fmax(cursor->y - seat.server.grab_y, 0);
view->current.x = wlr.x - seat.server.grab_x;
view->current.y = fmax(wlr.y - seat.server.grab_y, 0);

set_image("fleur");
wlr_scene_node_set_position(view->scene_node, view->current.x, view->current.y);
Expand All @@ -90,7 +90,7 @@ static void cursor_axis_notify(wl_listener* listener, void* data) {

/* Notify the client with pointer focus of the axis event. */
wlr_seat_pointer_notify_axis(
cursor.seat.seat, event->time_msec, event->orientation, event->delta, event->delta_discrete, event->source);
cursor.seat.wlr, event->time_msec, event->orientation, event->delta, event->delta_discrete, event->source);
}

/* This event is forwarded by the cursor when a pointer emits an frame
Expand All @@ -102,7 +102,7 @@ static void cursor_frame_notify(wl_listener* listener, void* data) {
(void) data;

/* Notify the client with pointer focus of the frame event. */
wlr_seat_pointer_notify_frame(cursor.seat.seat);
wlr_seat_pointer_notify_frame(cursor.seat.wlr);
}

/* This event is forwarded by the cursor when a pointer emits an _absolute_
Expand All @@ -116,20 +116,20 @@ static void cursor_motion_absolute_notify(wl_listener* listener, void* data) {
auto* event = static_cast<wlr_pointer_motion_absolute_event*>(data);

double lx, ly;
wlr_cursor_absolute_to_layout_coords(cursor.cursor, &event->pointer->base, event->x, event->y, &lx, &ly);
wlr_cursor_absolute_to_layout_coords(&cursor.wlr, &event->pointer->base, event->x, event->y, &lx, &ly);

double dx = lx - cursor.cursor->x;
double dy = ly - cursor.cursor->y;
double dx = lx - cursor.wlr.x;
double dy = ly - cursor.wlr.y;
wlr_relative_pointer_manager_v1_send_relative_motion(
cursor.relative_pointer_mgr, cursor.seat.seat, (uint64_t) event->time_msec * 1000, dx, dy, dx, dy);
cursor.relative_pointer_mgr, cursor.seat.wlr, (uint64_t) event->time_msec * 1000, dx, dy, dx, dy);

if (cursor.seat.is_pointer_locked(event->pointer)) {
return;
}

cursor.seat.apply_constraint(event->pointer, &dx, &dy);

wlr_cursor_move(cursor.cursor, &event->pointer->base, dx, dy);
wlr_cursor_move(&cursor.wlr, &event->pointer->base, dx, dy);
cursor.process_motion(event->time_msec);
}

Expand All @@ -141,11 +141,11 @@ static void cursor_button_notify(wl_listener* listener, void* data) {
Server& server = cursor.seat.server;

/* Notify the client with pointer focus that a button press has occurred */
wlr_seat_pointer_notify_button(server.seat->seat, event->time_msec, event->button, event->state);
wlr_seat_pointer_notify_button(server.seat->wlr, event->time_msec, event->button, event->state);
double sx, sy;

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

if (event->state == WLR_BUTTON_RELEASED) {
/* If you released any buttons, we exit interactive move/resize mode. */
Expand All @@ -166,7 +166,7 @@ static void cursor_motion_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, motion);
auto* event = static_cast<wlr_pointer_motion_event*>(data);

wlr_relative_pointer_manager_v1_send_relative_motion(cursor.relative_pointer_mgr, cursor.seat.seat,
wlr_relative_pointer_manager_v1_send_relative_motion(cursor.relative_pointer_mgr, cursor.seat.wlr,
(uint64_t) event->time_msec * 1000, event->delta_x, event->delta_y, event->unaccel_dx, event->unaccel_dy);

if (cursor.seat.is_pointer_locked(event->pointer)) {
Expand All @@ -177,75 +177,73 @@ static void cursor_motion_notify(wl_listener* listener, void* data) {
double dy = event->delta_y;
cursor.seat.apply_constraint(event->pointer, &dx, &dy);

wlr_cursor_move(cursor.cursor, &event->pointer->base, dx, dy);
wlr_cursor_move(&cursor.wlr, &event->pointer->base, dx, dy);
cursor.process_motion(event->time_msec);
}

static void gesture_pinch_begin_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_begin);
auto* event = static_cast<wlr_pointer_pinch_begin_event*>(data);

wlr_pointer_gestures_v1_send_pinch_begin(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->fingers);
wlr_pointer_gestures_v1_send_pinch_begin(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->fingers);
}

static void gesture_pinch_update_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_update);
auto* event = static_cast<wlr_pointer_pinch_update_event*>(data);

wlr_pointer_gestures_v1_send_pinch_update(
cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->dx, event->dy, event->scale, event->rotation);
cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->dx, event->dy, event->scale, event->rotation);
}

static void gesture_pinch_end_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_end);
auto* event = static_cast<wlr_pointer_pinch_end_event*>(data);

wlr_pointer_gestures_v1_send_pinch_end(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->cancelled);
wlr_pointer_gestures_v1_send_pinch_end(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->cancelled);
}

static void gesture_swipe_begin_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_begin);
auto* event = static_cast<wlr_pointer_swipe_begin_event*>(data);

wlr_pointer_gestures_v1_send_swipe_begin(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->fingers);
wlr_pointer_gestures_v1_send_swipe_begin(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->fingers);
}

static void gesture_swipe_update_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_update);
auto* event = static_cast<wlr_pointer_swipe_update_event*>(data);

wlr_pointer_gestures_v1_send_swipe_update(
cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->dx, event->dy);
wlr_pointer_gestures_v1_send_swipe_update(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->dx, event->dy);
}

static void gesture_swipe_end_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_end);
auto* event = static_cast<wlr_pointer_swipe_end_event*>(data);

wlr_pointer_gestures_v1_send_swipe_end(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->cancelled);
wlr_pointer_gestures_v1_send_swipe_end(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->cancelled);
}

static void gesture_hold_begin_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_hold_begin);
auto* event = static_cast<wlr_pointer_hold_begin_event*>(data);

wlr_pointer_gestures_v1_send_hold_begin(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->fingers);
wlr_pointer_gestures_v1_send_hold_begin(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->fingers);
}

static void gesture_hold_end_notify(wl_listener* listener, void* data) {
Cursor& cursor = magpie_container_of(listener, cursor, gesture_hold_end);
auto* event = static_cast<wlr_pointer_hold_end_event*>(data);

wlr_pointer_gestures_v1_send_hold_end(cursor.pointer_gestures, cursor.seat.seat, event->time_msec, event->cancelled);
wlr_pointer_gestures_v1_send_hold_end(cursor.pointer_gestures, cursor.seat.wlr, event->time_msec, event->cancelled);
}

Cursor::Cursor(Seat& seat) noexcept : listeners(*this), seat(seat) {
Cursor::Cursor(Seat& seat) noexcept : listeners(*this), seat(seat), wlr(*wlr_cursor_create()) {
/*
* Creates a cursor, which is a wlroots utility for tracking the cursor
* image shown on screen.
*/
cursor = wlr_cursor_create();
wlr_cursor_attach_output_layout(cursor, seat.server.output_layout);
wlr_cursor_attach_output_layout(&wlr, seat.server.output_layout);

/* Creates an xcursor manager, another wlroots utility which loads up
* Xcursor themes to source cursor images from and makes sure that cursor
Expand All @@ -271,40 +269,40 @@ Cursor::Cursor(Seat& seat) noexcept : listeners(*this), seat(seat) {
*/
mode = MAGPIE_CURSOR_PASSTHROUGH;
listeners.motion.notify = cursor_motion_notify;
wl_signal_add(&cursor->events.motion, &listeners.motion);
wl_signal_add(&wlr.events.motion, &listeners.motion);
listeners.motion_absolute.notify = cursor_motion_absolute_notify;
wl_signal_add(&cursor->events.motion_absolute, &listeners.motion_absolute);
wl_signal_add(&wlr.events.motion_absolute, &listeners.motion_absolute);
listeners.button.notify = cursor_button_notify;
wl_signal_add(&cursor->events.button, &listeners.button);
wl_signal_add(&wlr.events.button, &listeners.button);
listeners.axis.notify = cursor_axis_notify;
wl_signal_add(&cursor->events.axis, &listeners.axis);
wl_signal_add(&wlr.events.axis, &listeners.axis);
listeners.frame.notify = cursor_frame_notify;
wl_signal_add(&cursor->events.frame, &listeners.frame);
wl_signal_add(&wlr.events.frame, &listeners.frame);

listeners.gesture_pinch_begin.notify = gesture_pinch_begin_notify;
wl_signal_add(&cursor->events.pinch_begin, &listeners.gesture_pinch_begin);
wl_signal_add(&wlr.events.pinch_begin, &listeners.gesture_pinch_begin);
listeners.gesture_pinch_update.notify = gesture_pinch_update_notify;
wl_signal_add(&cursor->events.pinch_update, &listeners.gesture_pinch_update);
wl_signal_add(&wlr.events.pinch_update, &listeners.gesture_pinch_update);
listeners.gesture_pinch_end.notify = gesture_pinch_end_notify;
wl_signal_add(&cursor->events.pinch_end, &listeners.gesture_pinch_end);
wl_signal_add(&wlr.events.pinch_end, &listeners.gesture_pinch_end);
listeners.gesture_swipe_begin.notify = gesture_swipe_begin_notify;
wl_signal_add(&cursor->events.swipe_begin, &listeners.gesture_swipe_begin);
wl_signal_add(&wlr.events.swipe_begin, &listeners.gesture_swipe_begin);
listeners.gesture_swipe_update.notify = gesture_swipe_update_notify;
wl_signal_add(&cursor->events.swipe_update, &listeners.gesture_swipe_update);
wl_signal_add(&wlr.events.swipe_update, &listeners.gesture_swipe_update);
listeners.gesture_swipe_end.notify = gesture_swipe_end_notify;
wl_signal_add(&cursor->events.swipe_end, &listeners.gesture_swipe_end);
wl_signal_add(&wlr.events.swipe_end, &listeners.gesture_swipe_end);
listeners.gesture_hold_begin.notify = gesture_hold_begin_notify;
wl_signal_add(&cursor->events.hold_begin, &listeners.gesture_swipe_update);
wl_signal_add(&wlr.events.hold_begin, &listeners.gesture_swipe_update);
listeners.gesture_hold_end.notify = gesture_hold_end_notify;
wl_signal_add(&cursor->events.hold_end, &listeners.gesture_swipe_end);
wl_signal_add(&wlr.events.hold_end, &listeners.gesture_swipe_end);
}

void Cursor::attach_input_device(wlr_input_device* device) {
wlr_cursor_attach_input_device(cursor, device);
wlr_cursor_attach_input_device(&wlr, device);
}

void Cursor::process_motion(const uint32_t time) {
wlr_idle_notifier_v1_notify_activity(seat.server.idle_notifier, seat.seat);
wlr_idle_notifier_v1_notify_activity(seat.server.idle_notifier, seat.wlr);

/* If the mode is non-passthrough, delegate to those functions. */
if (mode == MAGPIE_CURSOR_MOVE) {
Expand All @@ -318,7 +316,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;
Surface* magpie_surface = seat.server.surface_at(cursor->x, cursor->y, &surface, &sx, &sy);
Surface* magpie_surface = seat.server.surface_at(wlr.x, wlr.y, &surface, &sx, &sy);
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 @@ -339,12 +337,12 @@ void Cursor::process_motion(const uint32_t time) {
* aware of the coordinates passed.
*/
current_image = "";
wlr_seat_pointer_notify_enter(seat.seat, surface, sx, sy);
wlr_seat_pointer_notify_motion(seat.seat, time, sx, sy);
wlr_seat_pointer_notify_enter(seat.wlr, surface, sx, sy);
wlr_seat_pointer_notify_motion(seat.wlr, time, sx, sy);
} else {
/* Clear pointer focus so future button events and such are not sent to
* the last client to have the cursor over it. */
wlr_seat_pointer_clear_focus(seat.seat);
wlr_seat_pointer_clear_focus(seat.wlr);
}
}

Expand All @@ -357,7 +355,7 @@ void Cursor::reset_mode() {
}

void Cursor::warp_to_constraint(PointerConstraint& constraint) {
if (seat.server.focused_view->get_wlr_surface() != constraint.wlr->surface) {
if (seat.server.focused_view->get_wlr_surface() != constraint.wlr.surface) {
return;
}

Expand All @@ -366,22 +364,22 @@ void Cursor::warp_to_constraint(PointerConstraint& constraint) {
return;
}

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

wlr_cursor_warp(cursor, nullptr, seat.server.focused_view->current.x + x, seat.server.focused_view->current.y + y);
wlr_seat_pointer_warp(seat.seat, x, y);
wlr_cursor_warp(&wlr, nullptr, seat.server.focused_view->current.x + x, seat.server.focused_view->current.y + y);
wlr_seat_pointer_warp(seat.wlr, x, y);
}
}

void Cursor::set_image(const std::string name) {
if (current_image != name) {
wlr_xcursor_manager_set_cursor_image(cursor_mgr, name.c_str(), cursor);
wlr_xcursor_manager_set_cursor_image(cursor_mgr, name.c_str(), &wlr);
current_image = name;
}
}

void Cursor::reload_image() {
wlr_xcursor_manager_set_cursor_image(cursor_mgr, current_image.c_str(), cursor);
wlr_xcursor_manager_set_cursor_image(cursor_mgr, current_image.c_str(), &wlr);
}
3 changes: 2 additions & 1 deletion src/input/cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class Cursor {

public:
const Seat& seat;
wlr_cursor& wlr;

CursorMode mode;
wlr_cursor* cursor;
wlr_xcursor_manager* cursor_mgr;
wlr_relative_pointer_manager_v1* relative_pointer_mgr;
wlr_pointer_gestures_v1* pointer_gestures;
Expand Down
Loading

0 comments on commit 6d3c28e

Please sign in to comment.