Skip to content

Commit

Permalink
Short-circuit if missing data in event listeners
Browse files Browse the repository at this point in the history
Instead of segfaulting, of course
  • Loading branch information
serebit committed Apr 12, 2024
1 parent 1271e07 commit ba4d05f
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 5 deletions.
26 changes: 25 additions & 1 deletion src/foreign_toplevel.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#include "foreign_toplevel.hpp"

#include "output.hpp"

#include "server.hpp"
#include "surface/view.hpp"

#include "wlr-wrap-start.hpp"
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

static void foreign_toplevel_handle_request_maximize_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_foreign_toplevel_handle_v1.events.request_maximize");
return;
}

const ForeignToplevelHandle& handle = magpie_container_of(listener, handle, request_activate);
const auto& event = *static_cast<wlr_foreign_toplevel_handle_v1_maximized_event*>(data);

Expand All @@ -13,6 +22,11 @@ static void foreign_toplevel_handle_request_maximize_notify(wl_listener* listene
}

static void foreign_toplevel_handle_request_fullscreen_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_foreign_toplevel_handle_v1.events.request_fullscreen");
return;
}

const ForeignToplevelHandle& handle = magpie_container_of(listener, handle, request_activate);
const auto& event = *static_cast<wlr_foreign_toplevel_handle_v1_maximized_event*>(data);

Expand All @@ -21,6 +35,11 @@ static void foreign_toplevel_handle_request_fullscreen_notify(wl_listener* liste
}

static void foreign_toplevel_handle_request_minimize_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_foreign_toplevel_handle_v1.events.request_minimize");
return;
}

const ForeignToplevelHandle& handle = magpie_container_of(listener, handle, request_activate);
const auto& event = *static_cast<wlr_foreign_toplevel_handle_v1_minimized_event*>(data);

Expand All @@ -41,6 +60,11 @@ static void foreign_toplevel_handle_request_close_notify(wl_listener* listener,
}

static void foreign_toplevel_handle_set_rectangle_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_foreign_toplevel_handle_v1.events.set_rectangle");
return;
}

const ForeignToplevelHandle& handle = magpie_container_of(listener, handle, set_rectangle);
const auto& event = *static_cast<wlr_foreign_toplevel_handle_v1_set_rectangle_event*>(data);

Expand Down
66 changes: 66 additions & 0 deletions src/input/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/edges.h>
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

void Cursor::process_resize(const uint32_t time) const {
Expand Down Expand Up @@ -93,6 +94,11 @@ void Cursor::process_move(const uint32_t time) {
/* This event is forwarded by the cursor when a pointer emits an axis event,
* for example when you move the scroll wheel. */
static void cursor_axis_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.cursor_axis");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, axis);
const auto* event = static_cast<wlr_pointer_axis_event*>(data);

Expand All @@ -119,6 +125,11 @@ static void cursor_frame_notify(wl_listener* listener, void*) {
* so we have to warp the mouse there. There is also some hardware which
* emits these events. */
static void cursor_motion_absolute_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.motion_absolute");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, motion_absolute);
const auto* event = static_cast<wlr_pointer_motion_absolute_event*>(data);

Expand All @@ -142,6 +153,11 @@ static void cursor_motion_absolute_notify(wl_listener* listener, void* data) {

/* This event is forwarded by the cursor when a pointer emits a button event. */
static void cursor_button_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.button");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, button);
const auto* event = static_cast<wlr_pointer_button_event*>(data);

Expand Down Expand Up @@ -170,6 +186,11 @@ static void cursor_button_notify(wl_listener* listener, void* data) {
/* This event is forwarded by the cursor when a pointer emits a _relative_
* pointer motion event (i.e. a delta) */
static void cursor_motion_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.motion");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, motion);
const auto* event = static_cast<wlr_pointer_motion_event*>(data);

Expand All @@ -189,13 +210,23 @@ static void cursor_motion_notify(wl_listener* listener, void* data) {
}

static void gesture_pinch_begin_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.pinch_begin");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_begin);
const auto* event = static_cast<wlr_pointer_pinch_begin_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.pinch_update");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_update);
const auto* event = static_cast<wlr_pointer_pinch_update_event*>(data);

Expand All @@ -204,48 +235,83 @@ static void gesture_pinch_update_notify(wl_listener* listener, void* data) {
}

static void gesture_pinch_end_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.pinch_end");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_pinch_end);
const auto* event = static_cast<wlr_pointer_pinch_end_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.swipe_begin");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_begin);
const auto* event = static_cast<wlr_pointer_swipe_begin_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.swipe_update");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_update);
const auto* event = static_cast<wlr_pointer_swipe_update_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.swipe_end");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_swipe_end);
const auto* event = static_cast<wlr_pointer_swipe_end_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.hold_begin");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_hold_begin);
const auto* event = static_cast<wlr_pointer_hold_begin_event*>(data);

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) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.hold_end");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, gesture_hold_end);
const auto* event = static_cast<wlr_pointer_hold_end_event*>(data);

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

static void request_set_shape_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_cursor.events.set_shape");
return;
}

Cursor& cursor = magpie_container_of(listener, cursor, request_set_shape);
const auto* event = static_cast<wlr_cursor_shape_manager_v1_request_set_shape_event*>(data);

Expand Down
9 changes: 7 additions & 2 deletions src/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <wlr/backend/session.h>
#include <wlr/types/wlr_idle_notify_v1.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

/* This event is raised by the keyboard base wlr_input_device to signal
Expand Down Expand Up @@ -61,11 +62,15 @@ static bool handle_compositor_keybinding(const Keyboard& keyboard, const uint32_

/* This event is raised when a key is pressed or released. */
static void keyboard_handle_key(wl_listener* listener, void* data) {
const Keyboard& keyboard = magpie_container_of(listener, keyboard, key);
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_keyboard.events.key");
return;
}

const Keyboard& keyboard = magpie_container_of(listener, keyboard, key);
const auto* event = static_cast<wlr_keyboard_key_event*>(data);
wlr_seat* seat = keyboard.seat.wlr;

wlr_seat* seat = keyboard.seat.wlr;
wlr_idle_notifier_v1_notify_activity(keyboard.seat.server.idle_notifier, seat);

/* Translate libinput keycode -> xkbcommon */
Expand Down
30 changes: 30 additions & 0 deletions src/input/seat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,47 @@
#include "wlr-wrap-end.hpp"

static void new_input_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.new_input");
return;
}

Seat& seat = magpie_container_of(listener, seat, new_input);
auto* device = static_cast<wlr_input_device*>(data);

seat.new_input_device(device);
}

static void new_virtual_pointer_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.new_virtual_pointer");
return;
}

Seat& seat = magpie_container_of(listener, seat, new_virtual_pointer);
const auto* event = static_cast<wlr_virtual_pointer_v1_new_pointer_event*>(data);

seat.new_input_device(&event->new_pointer->pointer.base);
}

static void new_virtual_keyboard_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.new_virtual_keyboard");
return;
}

Seat& seat = magpie_container_of(listener, seat, new_virtual_keyboard);
auto* keyboard = static_cast<wlr_virtual_keyboard_v1*>(data);

seat.new_input_device(&keyboard->keyboard.base);
}

static void new_pointer_constraint_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.new_pointer_constraint");
return;
}

Seat& seat = magpie_container_of(listener, seat, new_pointer_constraint);
auto* wlr_constraint = static_cast<wlr_pointer_constraint_v1*>(data);

Expand All @@ -51,6 +71,11 @@ static void new_pointer_constraint_notify(wl_listener* listener, void* data) {
}

static void request_cursor_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.request_cursor");
return;
}

const Seat& seat = magpie_container_of(listener, seat, request_cursor);
const auto* event = static_cast<wlr_seat_pointer_request_set_cursor_event*>(data);

Expand All @@ -70,6 +95,11 @@ static void request_cursor_notify(wl_listener* listener, void* data) {
* ignore such requests if they so choose, but in magpie we always honor
*/
static void request_set_selection_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_seat.events.request_set_selection");
return;
}

const Seat& seat = magpie_container_of(listener, seat, request_set_selection);
const auto* event = static_cast<wlr_seat_request_set_selection_event*>(data);

Expand Down
6 changes: 6 additions & 0 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
#include <wlr-wrap-start.hpp>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/util/log.h>
#include <wlr-wrap-end.hpp>

/* This function is called every time an output is ready to display a frame,
* generally at the output's refresh rate (e.g. 60Hz). */
static void output_request_state_notify(wl_listener* listener, void* data) {
if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_output.events.request_state");
return;
}

Output& output = magpie_container_of(listener, output, request_state);
const auto* event = static_cast<wlr_output_event_request_state*>(data);

Expand Down
Loading

0 comments on commit ba4d05f

Please sign in to comment.