Skip to content

Commit

Permalink
Fix crash if the callback is invalid on macos
Browse files Browse the repository at this point in the history
Signed-off-by: Slendi <slendi@socopon.com>
  • Loading branch information
xslendix committed Jun 24, 2024
1 parent 3a0a7bc commit 4be4bbe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

setup(
name='libvinput',
version='1.2',
version='1.3',
author='Slendi',
description='Python interface for libvinput',
long_description=long_description,
Expand Down
7 changes: 4 additions & 3 deletions src/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,12 @@ void xrecord_callback(XPointer incoming, XRecordInterceptData *data)
{
EventListenerInternal *data_ = (EventListenerInternal *)incoming;
if (data->category == XRecordFromServer) {
if (data->data[0] == KeyPress || data->data[0] == KeyRelease)
if (data_->callback && (data->data[0] == KeyPress || data->data[0] == KeyRelease))
data_->callback(xevent_to_key_event(data_, data));
else if (data->data[0] == ButtonPress || data->data[0] == ButtonRelease)
else if (data_->callback_mouse_button
&& (data->data[0] == ButtonPress || data->data[0] == ButtonRelease))
data_->callback_mouse_button(xevent_to_mouse_button_event(data_, data));
else if (data->data[0] == MotionNotify)
else if (data_->callback_mouse_move && data->data[0] == MotionNotify)
data_->callback_mouse_move(xevent_to_mouse_move_event(data_, data));
}

Expand Down
53 changes: 29 additions & 24 deletions src/macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,32 @@ CGEventRef CGEventCallback(

if (listener->data) {
KeyboardCallback callback = (KeyboardCallback)data->callback;
callback(kevent);
if (callback) callback(kevent);
}
} else if (type == kCGEventLeftMouseDown) {
data->button_callback((MouseButtonEvent) {
.button = MouseButtonLeft,
.kind = MousePressEvent,
});
if (data->button_callback)
data->button_callback((MouseButtonEvent) {
.button = MouseButtonLeft,
.kind = MousePressEvent,
});
} else if (type == kCGEventRightMouseDown) {
data->button_callback((MouseButtonEvent) {
.button = MouseButtonRight,
.kind = MousePressEvent,
});
if (data->button_callback)
data->button_callback((MouseButtonEvent) {
.button = MouseButtonRight,
.kind = MousePressEvent,
});
} else if (type == kCGEventLeftMouseUp) {
data->button_callback((MouseButtonEvent) {
.button = MouseButtonLeft,
.kind = MouseReleaseEvent,
});
if (data->button_callback)
data->button_callback((MouseButtonEvent) {
.button = MouseButtonLeft,
.kind = MouseReleaseEvent,
});
} else if (type == kCGEventRightMouseUp) {
data->button_callback((MouseButtonEvent) {
.button = MouseButtonRight,
.kind = MouseReleaseEvent,
});
if (data->button_callback)
data->button_callback((MouseButtonEvent) {
.button = MouseButtonRight,
.kind = MouseReleaseEvent,
});
} else if (type == kCGEventMouseMoved) {
// FIXME: This is not thread safe!!!
static int last_x = 0, last_y = 0;
Expand All @@ -167,13 +171,14 @@ CGEventRef CGEventCallback(
last_x = pos_x;
last_y = pos_y;

data->move_callback((MouseMoveEvent) {
.x = pos_x,
.y = point.y,
.velocity_x = velocity_x,
.velocity_y = velocity_y,
.velocity = sqrtf(velocity_x * velocity_x + velocity_y * velocity_y),
});
if (data->move_callback)
data->move_callback((MouseMoveEvent) {
.x = pos_x,
.y = point.y,
.velocity_x = velocity_x,
.velocity_y = velocity_y,
.velocity = sqrtf(velocity_x * velocity_x + velocity_y * velocity_y),
});
}

return event;
Expand Down

0 comments on commit 4be4bbe

Please sign in to comment.