From 3c6d8c6a824530608cbe46876325d7d9fce2dabe Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Fri, 2 Feb 2018 12:46:47 +0100 Subject: [PATCH] Refactor in dispatching gesture events 1. Error messages are more descriptive. 2. Refactor of transform_coordinates and correct_coordinates functions Change-Id: I7a2e4e3d23070581bcbcaea6a223c1f318cb41ee --- src/e_dispatch_gesture_event.c | 51 +++++++++++++++++++++++++---------------- src/e_dispatch_rotation_event.c | 4 ++-- src/e_mod_utils.c | 12 ++++++---- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/e_dispatch_gesture_event.c b/src/e_dispatch_gesture_event.c index a2cf7db..8df31f5 100644 --- a/src/e_dispatch_gesture_event.c +++ b/src/e_dispatch_gesture_event.c @@ -3,6 +3,7 @@ #include "e_dispatch_gesture_event.h" #include "e_mod_utils.h" +#define ERROR_BUFFER_SIZE 1024 #define STEP_DURATION 0.01 int device_number_shift; @@ -150,29 +151,39 @@ static void _dispatch_mouse_up_event(int x, int y, int device) } static Eldbus_Message * -_correct_coordinates(Point *start_point, Point *end_point, int steps, const Eldbus_Message *msg) +_check_and_correct_gesture_data(DragInfo *drag_info, const Eldbus_Message *msg) { if (during_gesture) - return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Previous gesture is not finished yet"); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Previous gesture is not finished yet."); E_Zone *zone = e_zone_current_get(); if (!zone) - return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Fail to find zone"); - - if (start_point->x < 0 || start_point->y < 0 || start_point->x > zone->w || start_point->y > zone->h || steps < 1) - { - ERROR("Invalid value: start.x: %d, start.y: %d, steps: %d", start_point->x, start_point->y, steps); - return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Invalid starting coordinates or steps number"); - } - - _transform_coordinates(&start_point->x, &start_point->y); - _transform_coordinates(&end_point->x, &end_point->y); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Fail to find zone."); + + if (drag_info->start.x < 0 || drag_info->start.y < 0 || drag_info->start.x > zone->w || drag_info->start.y > zone->h) + { + char message[ERROR_BUFFER_SIZE] = {0}; + snprintf(message, ERROR_BUFFER_SIZE, "Invalid value of starting point: x: %d, y: %d. Point is outside of the viewport.", drag_info->start.x, drag_info->start.y); + ERROR("%s", message); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", message); + } + + if (drag_info->steps < 1) + { + char message[ERROR_BUFFER_SIZE] = {0}; + snprintf(message, ERROR_BUFFER_SIZE, "Invalid steps value: %d. Expected higher than 0.", drag_info->steps); + ERROR("%s", message); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", message); + } + + _transform_coordinates(&drag_info->start.x, &drag_info->start.y); + _transform_coordinates(&drag_info->end.x, &drag_info->end.y); // Starting gesture from edge causes additional gesture on the screen - if (start_point->x == 0) start_point->x += 1; - if (start_point->x == zone->w) start_point->x -= 1; - if (start_point->y == 0) start_point->y += 1; - if (start_point->y == zone->h) start_point->y -= 1; + if (drag_info->start.x == 0) drag_info->start.x += 1; + if (drag_info->start.x == zone->w) drag_info->start.x -= 1; + if (drag_info->start.y == 0) drag_info->start.y += 1; + if (drag_info->start.y == zone->h) drag_info->start.y -= 1; return NULL; } @@ -188,13 +199,13 @@ _swap_points(Point *a, Point *b) Eldbus_Message * _e_dispatch_drag_event(DragInfo drag_info, Eldbus_Connection *conn, const Eldbus_Message *msg) { - Eldbus_Message *error_message = _correct_coordinates(&drag_info.start, &drag_info.end, drag_info.steps, msg); + Eldbus_Message *error_message = _check_and_correct_gesture_data(&drag_info, msg); if (error_message) return error_message; GestureCommand *cmd = (GestureCommand *)malloc(sizeof(GestureCommand)); if (!cmd) - return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Malloc error"); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Malloc error."); cmd->drag_info[0] = drag_info; cmd->drag_info_size = 1; @@ -226,14 +237,14 @@ _e_dispatch_pinch_event(PinchInfo pinch_info, Eldbus_Connection *conn, const Eld if (pinch_info.radius_change < 0) _swap_points(&temporary_drag_info[i].start, &temporary_drag_info[i].end); - Eldbus_Message *error_message = _correct_coordinates(&temporary_drag_info[i].start, &temporary_drag_info[i].end, temporary_drag_info[i].steps, msg); + Eldbus_Message *error_message = _check_and_correct_gesture_data(&temporary_drag_info[i], msg); if (error_message) return error_message; } GestureCommand *cmd = (GestureCommand *)malloc(sizeof(GestureCommand)); if (!cmd) - return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Malloc error"); + return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Malloc error."); cmd->drag_info[0] = temporary_drag_info[0]; cmd->drag_info[1] = temporary_drag_info[1]; diff --git a/src/e_dispatch_rotation_event.c b/src/e_dispatch_rotation_event.c index a52354d..bb37f80 100644 --- a/src/e_dispatch_rotation_event.c +++ b/src/e_dispatch_rotation_event.c @@ -14,7 +14,7 @@ void _e_dispatch_rotation_event(int angle) { if (angle % 90 != 0 || angle < 0 || angle > 270) { - ERROR("Wrong angle"); + ERROR("Wrong angle, expected: 0, 90 or 270, get: %d", angle); return; } @@ -36,4 +36,4 @@ void _e_dispatch_rotation_event(int angle) e_object_ref(E_OBJECT(ev->zone)); ecore_event_add(E_EVENT_ZONE_ROTATION_CHANGE_BEGIN, ev, _e_zone_event_rotation_change_begin_free, NULL); } -} \ No newline at end of file +} diff --git a/src/e_mod_utils.c b/src/e_mod_utils.c index d8199b5..265664e 100644 --- a/src/e_mod_utils.c +++ b/src/e_mod_utils.c @@ -21,14 +21,16 @@ void _transform_coordinates(int *ax, int *ay) { E_Zone *zone = e_zone_current_get(); - int win_angle = _get_window_angle(); - if (win_angle < 0) + if (!zone) { - ERROR("Invalid angle"); - return; + ERROR("Fail to find zone"); + return; } + int win_angle = _get_window_angle(); switch (win_angle) { + case 0: + break; case 90: { int tmp = *ax; @@ -43,5 +45,7 @@ void _transform_coordinates(int *ax, int *ay) *ay = tmp; break; } + default: + ERROR("Invalid angle"); } } -- 2.7.4