From 4be4bbe500140efa06b3165a9e0789d7d63ff48e Mon Sep 17 00:00:00 2001 From: Slendi Date: Mon, 24 Jun 2024 08:42:00 +0300 Subject: [PATCH] Fix crash if the callback is invalid on macos Signed-off-by: Slendi --- setup.py | 2 +- src/linux.c | 7 ++++--- src/macos.c | 53 +++++++++++++++++++++++++++++------------------------ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/setup.py b/setup.py index a3d924e..5f20e5a 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ setup( name='libvinput', - version='1.2', + version='1.3', author='Slendi', description='Python interface for libvinput', long_description=long_description, diff --git a/src/linux.c b/src/linux.c index 5515471..0899b7a 100644 --- a/src/linux.c +++ b/src/linux.c @@ -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)); } diff --git a/src/macos.c b/src/macos.c index cf70b84..d22abae 100644 --- a/src/macos.c +++ b/src/macos.c @@ -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; @@ -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;