Skip to content

Commit

Permalink
Fix #183 and address the change suggested by #184.
Browse files Browse the repository at this point in the history
Minor bugfix, properly determine when switching screens is needed.
  • Loading branch information
Hrvoje Cavrak committed Dec 6, 2024
1 parent f3a74c5 commit 5b66e87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ enum os_type_e {
};

enum screen_pos_e {
NONE = 0,
LEFT = 1,
RIGHT = 2,
MIDDLE = 3,
Expand Down
38 changes: 23 additions & 15 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -182,23 +199,13 @@ 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;

/* We want to jump in the direction of the other computer */
if (output->pos != direction) {
if (output->screen_index == 1) { /* We are at the border -> switch outputs */
Expand Down Expand Up @@ -270,16 +277,17 @@ 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);

/* 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);
}

/* ==================================================== *
Expand Down

0 comments on commit 5b66e87

Please sign in to comment.