From 8aa5bf508be994ba3d6e99c790d3383555355e90 Mon Sep 17 00:00:00 2001 From: Hrvoje Cavrak Date: Sat, 7 Dec 2024 00:17:02 +0100 Subject: [PATCH] Fix #183 and address the change suggested by #184. Minor bugfix, properly determine when switching screens is needed. --- src/include/main.h | 1 + src/mouse.c | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/include/main.h b/src/include/main.h index 0c8699f..55aaf20 100644 --- a/src/include/main.h +++ b/src/include/main.h @@ -200,6 +200,7 @@ enum os_type_e { }; enum screen_pos_e { + NONE = 0, LEFT = 1, RIGHT = 2, MIDDLE = 3, diff --git a/src/mouse.c b/src/mouse.c index c2474a5..32d022e 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -20,6 +20,17 @@ #define MACOS_SWITCH_MOVE_X 10 #define MACOS_SWITCH_MOVE_COUNT 5 +/* Check if our upcoming mouse movement would result in having to switch outputs */ +enum screen_pos_e is_screen_switch_needed(int position, int offset) { + if (position + offset < MIN_SCREEN_COORD - global_state.config.jump_treshold) + return LEFT; + + if (position + offset > MAX_SCREEN_COORD + global_state.config.jump_treshold) + return RIGHT; + + return NONE; +} + /* Move mouse coordinate 'position' by 'offset', but don't fall off the screen */ int32_t move_and_keep_on_screen(int position, int offset) { /* Lowest we can go is 0 */ @@ -63,7 +74,8 @@ int32_t accelerate(int32_t offset) { return offset * acceleration[6].factor; } -void update_mouse_position(device_t *state, mouse_values_t *values) { +/* Returns LEFT if need to jump left, RIGHT if right, NONE otherwise */ +enum screen_pos_e update_mouse_position(device_t *state, mouse_values_t *values) { output_t *current = &state->config.output[state->active_output]; uint8_t reduce_speed = 0; @@ -75,12 +87,17 @@ void update_mouse_position(device_t *state, mouse_values_t *values) { int offset_x = accelerate(values->move_x) * (current->speed_x >> reduce_speed); int offset_y = accelerate(values->move_y) * (current->speed_y >> reduce_speed); + /* Determine if our upcoming movement would stay within the screen */ + enum screen_pos_e switch_direction = is_screen_switch_needed(state->pointer_x, offset_x); + /* Update movement */ state->pointer_x = move_and_keep_on_screen(state->pointer_x, offset_x); state->pointer_y = move_and_keep_on_screen(state->pointer_y, offset_y); /* Update buttons state */ state->mouse_buttons = values->buttons; + + return switch_direction; } /* If we are active output, queue packet to mouse queue, else send them through UART */ @@ -182,22 +199,12 @@ void switch_virtual_desktop(device_t *state, output_t *output, int new_index, in '---------' '---------' | '---------' '---------' '---------' )___( )___( | )___( )___( )___( */ -void check_screen_switch(const mouse_values_t *values, device_t *state) { - int new_x = state->pointer_x + values->move_x; +void do_screen_switch(device_t *state, int direction) { output_t *output = &state->config.output[state->active_output]; - bool jump_left = new_x < MIN_SCREEN_COORD - state->config.jump_treshold; - bool jump_right = new_x > MAX_SCREEN_COORD + state->config.jump_treshold; - - int direction = jump_left ? LEFT : RIGHT; - - /* No switching allowed if explicitly disabled or in gaming mode */ - if (state->switch_lock || state->gaming_mode) - return; - - /* No jump condition met == nothing to do, return */ - if (!jump_left && !jump_right) - return; + // /* No switching allowed if explicitly disabled or in gaming mode */ + // if (state->switch_lock || state->gaming_mode) + // return; /* We want to jump in the direction of the other computer */ if (output->pos != direction) { @@ -270,7 +277,7 @@ void process_mouse_report(uint8_t *raw_report, int len, uint8_t itf, hid_interfa extract_report_values(raw_report, state, &values, iface); /* Calculate and update mouse pointer movement. */ - update_mouse_position(state, &values); + enum screen_pos_e switch_direction = update_mouse_position(state, &values); /* Create the report for the output PC based on the updated values */ mouse_report_t report = create_mouse_report(state, &values); @@ -278,8 +285,9 @@ void process_mouse_report(uint8_t *raw_report, int len, uint8_t itf, hid_interfa /* Move the mouse, depending where the output is supposed to go */ output_mouse_report(&report, state); - /* We use the mouse to switch outputs, the logic is in check_screen_switch() */ - check_screen_switch(&values, state); + /* We use the mouse to switch outputs, if switch_direction is LEFT or RIGHT */ + if (switch_direction != NONE) + do_screen_switch(state, switch_direction); } /* ==================================================== *