Skip to content

Commit 5b66e87

Browse files
author
Hrvoje Cavrak
committed
Fix #183 and address the change suggested by #184.
Minor bugfix, properly determine when switching screens is needed.
1 parent f3a74c5 commit 5b66e87

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/include/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ enum os_type_e {
200200
};
201201

202202
enum screen_pos_e {
203+
NONE = 0,
203204
LEFT = 1,
204205
RIGHT = 2,
205206
MIDDLE = 3,

src/mouse.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
#define MACOS_SWITCH_MOVE_X 10
2121
#define MACOS_SWITCH_MOVE_COUNT 5
2222

23+
/* Check if our upcoming mouse movement would result in having to switch outputs */
24+
enum screen_pos_e is_screen_switch_needed(int position, int offset) {
25+
if (position + offset < MIN_SCREEN_COORD - global_state.config.jump_treshold)
26+
return LEFT;
27+
28+
if (position + offset > MAX_SCREEN_COORD + global_state.config.jump_treshold)
29+
return RIGHT;
30+
31+
return NONE;
32+
}
33+
2334
/* Move mouse coordinate 'position' by 'offset', but don't fall off the screen */
2435
int32_t move_and_keep_on_screen(int position, int offset) {
2536
/* Lowest we can go is 0 */
@@ -63,7 +74,8 @@ int32_t accelerate(int32_t offset) {
6374
return offset * acceleration[6].factor;
6475
}
6576

66-
void update_mouse_position(device_t *state, mouse_values_t *values) {
77+
/* Returns LEFT if need to jump left, RIGHT if right, NONE otherwise */
78+
enum screen_pos_e update_mouse_position(device_t *state, mouse_values_t *values) {
6779
output_t *current = &state->config.output[state->active_output];
6880
uint8_t reduce_speed = 0;
6981

@@ -75,12 +87,17 @@ void update_mouse_position(device_t *state, mouse_values_t *values) {
7587
int offset_x = accelerate(values->move_x) * (current->speed_x >> reduce_speed);
7688
int offset_y = accelerate(values->move_y) * (current->speed_y >> reduce_speed);
7789

90+
/* Determine if our upcoming movement would stay within the screen */
91+
enum screen_pos_e switch_direction = is_screen_switch_needed(state->pointer_x, offset_x);
92+
7893
/* Update movement */
7994
state->pointer_x = move_and_keep_on_screen(state->pointer_x, offset_x);
8095
state->pointer_y = move_and_keep_on_screen(state->pointer_y, offset_y);
8196

8297
/* Update buttons state */
8398
state->mouse_buttons = values->buttons;
99+
100+
return switch_direction;
84101
}
85102

86103
/* If we are active output, queue packet to mouse queue, else send them through UART */
@@ -182,23 +199,13 @@ void switch_virtual_desktop(device_t *state, output_t *output, int new_index, in
182199
'---------' '---------' | '---------' '---------' '---------'
183200
)___( )___( | )___( )___( )___(
184201
*/
185-
void check_screen_switch(const mouse_values_t *values, device_t *state) {
186-
int new_x = state->pointer_x + values->move_x;
202+
void do_screen_switch(device_t *state, int direction) {
187203
output_t *output = &state->config.output[state->active_output];
188204

189-
bool jump_left = new_x < MIN_SCREEN_COORD - state->config.jump_treshold;
190-
bool jump_right = new_x > MAX_SCREEN_COORD + state->config.jump_treshold;
191-
192-
int direction = jump_left ? LEFT : RIGHT;
193-
194205
/* No switching allowed if explicitly disabled or in gaming mode */
195206
if (state->switch_lock || state->gaming_mode)
196207
return;
197208

198-
/* No jump condition met == nothing to do, return */
199-
if (!jump_left && !jump_right)
200-
return;
201-
202209
/* We want to jump in the direction of the other computer */
203210
if (output->pos != direction) {
204211
if (output->screen_index == 1) { /* We are at the border -> switch outputs */
@@ -270,16 +277,17 @@ void process_mouse_report(uint8_t *raw_report, int len, uint8_t itf, hid_interfa
270277
extract_report_values(raw_report, state, &values, iface);
271278

272279
/* Calculate and update mouse pointer movement. */
273-
update_mouse_position(state, &values);
280+
enum screen_pos_e switch_direction = update_mouse_position(state, &values);
274281

275282
/* Create the report for the output PC based on the updated values */
276283
mouse_report_t report = create_mouse_report(state, &values);
277284

278285
/* Move the mouse, depending where the output is supposed to go */
279286
output_mouse_report(&report, state);
280287

281-
/* We use the mouse to switch outputs, the logic is in check_screen_switch() */
282-
check_screen_switch(&values, state);
288+
/* We use the mouse to switch outputs, if switch_direction is LEFT or RIGHT */
289+
if (switch_direction != NONE)
290+
do_screen_switch(state, switch_direction);
283291
}
284292

285293
/* ==================================================== *

0 commit comments

Comments
 (0)