Compare commits

..

3 Commits

Author SHA1 Message Date
Colin 922494beb2 Delete unuse functions. 2024-12-08 16:06:00 +08:00
Hrvoje Cavrak 8aa5bf508b Fix #183 and address the change suggested by #184.
Minor bugfix, properly determine when switching screens is needed.
2024-12-08 16:04:52 +08:00
Hrvoje Cavrak f841035c09 Refactoring some function names (#182) 2024-12-08 16:04:29 +08:00
4 changed files with 52 additions and 33 deletions

View File

@ -28,7 +28,7 @@ void output_toggle_hotkey_handler(device_t *state, hid_keyboard_report_t *report
// return; // return;
state->active_output ^= 1; state->active_output ^= 1;
switch_output(state, state->active_output); set_active_output(state, state->active_output);
}; };
void _get_border_position(device_t *state, border_size_t *border) { void _get_border_position(device_t *state, border_size_t *border) {
@ -354,8 +354,9 @@ void handle_heartbeat_msg(uart_packet_t *packet, device_t *state) {
return; return;
/* It is? Ok, kick off the firmware upgrade */ /* It is? Ok, kick off the firmware upgrade */
state->fw = (fw_upgrade_state_t) { // state->fw = (fw_upgrade_state_t) {
.upgrade_in_progress = true, state->fw = () {
// .upgrade_in_progress = true,
.byte_done = true, .byte_done = true,
.address = 0, .address = 0,
.checksum = 0xffffffff, .checksum = 0xffffffff,
@ -368,7 +369,7 @@ void handle_heartbeat_msg(uart_packet_t *packet, device_t *state) {
* ==================================================== */ * ==================================================== */
/* Update output variable, set LED on/off and notify the other board so they are in sync. */ /* Update output variable, set LED on/off and notify the other board so they are in sync. */
void switch_output(device_t *state, uint8_t new_output) { void set_active_output(device_t *state, uint8_t new_output) {
state->active_output = new_output; state->active_output = new_output;
restore_leds(state); restore_leds(state);
send_value(new_output, OUTPUT_SELECT_MSG); send_value(new_output, OUTPUT_SELECT_MSG);

View File

@ -200,6 +200,7 @@ enum os_type_e {
}; };
enum screen_pos_e { enum screen_pos_e {
NONE = 0,
LEFT = 1, LEFT = 1,
RIGHT = 2, RIGHT = 2,
MIDDLE = 3, MIDDLE = 3,
@ -372,13 +373,13 @@ typedef struct {
typedef enum { IDLE, READING_PACKET, PROCESSING_PACKET } receiver_state_t; typedef enum { IDLE, READING_PACKET, PROCESSING_PACKET } receiver_state_t;
typedef struct { // typedef struct {
uint32_t address; // Address we're sending to the other box // uint32_t address; // Address we're sending to the other box
uint32_t checksum; // uint32_t checksum;
uint16_t version; // uint16_t version;
bool byte_done; // Has the byte been successfully transferred // bool byte_done; // Has the byte been successfully transferred
bool upgrade_in_progress; // True if firmware transfer from the other box is in progress // bool upgrade_in_progress; // True if firmware transfer from the other box is in progress
} fw_upgrade_state_t; // } fw_upgrade_state_t;
typedef struct { typedef struct {
uint8_t kbd_dev_addr; // Address of the keyboard device uint8_t kbd_dev_addr; // Address of the keyboard device
@ -410,7 +411,7 @@ typedef struct {
uint32_t dma_tx_channel; // DMA TX channel we're using to send uint32_t dma_tx_channel; // DMA TX channel we're using to send
/* Firmware */ /* Firmware */
fw_upgrade_state_t fw; // State of the firmware upgrader // fw_upgrade_state_t fw; // State of the firmware upgrader
firmware_metadata_t _running_fw; // RAM copy of running fw metadata firmware_metadata_t _running_fw; // RAM copy of running fw metadata
bool reboot_requested; // If set, stop updating watchdog bool reboot_requested; // If set, stop updating watchdog
uint64_t config_mode_timer; // Counts how long are we to remain in config mode uint64_t config_mode_timer; // Counts how long are we to remain in config mode
@ -569,7 +570,7 @@ void handle_api_read_all_msg(uart_packet_t *, device_t *);
void handle_toggle_gaming_msg(uart_packet_t *, device_t *); void handle_toggle_gaming_msg(uart_packet_t *, device_t *);
// void handle_screensaver_msg(uart_packet_t *, device_t *); // void handle_screensaver_msg(uart_packet_t *, device_t *);
void switch_output(device_t *, uint8_t); void set_active_output(device_t *, uint8_t);
/********* Global variables (don't judge) **********/ /********* Global variables (don't judge) **********/
extern device_t global_state; extern device_t global_state;

View File

@ -47,7 +47,7 @@ int main(void) {
initial_setup(device); initial_setup(device);
// Initial state, A is the default output // Initial state, A is the default output
switch_output(device, OUTPUT_A); set_active_output(device, OUTPUT_A);
while (true) { while (true) {
for (int i = 0; i < NUM_TASKS; i++) for (int i = 0; i < NUM_TASKS; i++)

View File

@ -20,6 +20,17 @@
#define MACOS_SWITCH_MOVE_X 10 #define MACOS_SWITCH_MOVE_X 10
#define MACOS_SWITCH_MOVE_COUNT 5 #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 */ /* Move mouse coordinate 'position' by 'offset', but don't fall off the screen */
int32_t move_and_keep_on_screen(int position, int offset) { int32_t move_and_keep_on_screen(int position, int offset) {
/* Lowest we can go is 0 */ /* Lowest we can go is 0 */
@ -63,7 +74,8 @@ int32_t accelerate(int32_t offset) {
return offset * acceleration[6].factor; 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]; output_t *current = &state->config.output[state->active_output];
uint8_t reduce_speed = 0; 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_x = accelerate(values->move_x) * (current->speed_x >> reduce_speed);
int offset_y = accelerate(values->move_y) * (current->speed_y >> 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 */ /* Update movement */
state->pointer_x = move_and_keep_on_screen(state->pointer_x, offset_x); 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); state->pointer_y = move_and_keep_on_screen(state->pointer_y, offset_y);
/* Update buttons state */ /* Update buttons state */
state->mouse_buttons = values->buttons; state->mouse_buttons = values->buttons;
return switch_direction;
} }
/* If we are active output, queue packet to mouse queue, else send them through UART */ /* If we are active output, queue packet to mouse queue, else send them through UART */
@ -124,7 +141,7 @@ int16_t scale_y_coordinate(int screen_from, int screen_to, device_t *state) {
return ((state->pointer_y - from->border.top) * MAX_SCREEN_COORD) / size_from; return ((state->pointer_y - from->border.top) * MAX_SCREEN_COORD) / size_from;
} }
void switch_screen( void switch_to_another_pc(
device_t *state, output_t *output, int output_to, int direction) { device_t *state, output_t *output, int output_to, int direction) {
uint8_t *mouse_park_pos = &state->config.output[state->active_output].mouse_park_pos; uint8_t *mouse_park_pos = &state->config.output[state->active_output].mouse_park_pos;
@ -135,12 +152,12 @@ void switch_screen(
mouse_report_t hidden_pointer = {.y = mouse_y, .x = MAX_SCREEN_COORD}; mouse_report_t hidden_pointer = {.y = mouse_y, .x = MAX_SCREEN_COORD};
output_mouse_report(&hidden_pointer, state); output_mouse_report(&hidden_pointer, state);
switch_output(state, output_to); set_active_output(state, output_to);
state->pointer_x = (direction == LEFT) ? MAX_SCREEN_COORD : MIN_SCREEN_COORD; state->pointer_x = (direction == LEFT) ? MAX_SCREEN_COORD : MIN_SCREEN_COORD;
state->pointer_y = scale_y_coordinate(output->number, 1 - output->number, state); state->pointer_y = scale_y_coordinate(output->number, 1 - output->number, state);
} }
void switch_desktop_macos(device_t *state, int direction) { void switch_virtual_desktop_macos(device_t *state, int direction) {
/* Fix for MACOS: Send relative mouse movement here, one or two pixels in the /* Fix for MACOS: Send relative mouse movement here, one or two pixels in the
direction of movement, BEFORE absolute report sets X to 0 */ direction of movement, BEFORE absolute report sets X to 0 */
uint16_t move = (direction == LEFT) ? -MACOS_SWITCH_MOVE_X : MACOS_SWITCH_MOVE_X; uint16_t move = (direction == LEFT) ? -MACOS_SWITCH_MOVE_X : MACOS_SWITCH_MOVE_X;
@ -151,10 +168,10 @@ void switch_desktop_macos(device_t *state, int direction) {
output_mouse_report(&move_relative_one, state); output_mouse_report(&move_relative_one, state);
} }
void switch_desktop(device_t *state, output_t *output, int new_index, int direction) { void switch_virtual_desktop(device_t *state, output_t *output, int new_index, int direction) {
switch (output->os) { switch (output->os) {
case MACOS: case MACOS:
switch_desktop_macos(state, direction); switch_virtual_desktop_macos(state, direction);
break; break;
case WINDOWS: case WINDOWS:
@ -182,22 +199,21 @@ void switch_desktop(device_t *state, output_t *output, int new_index, int direct
'---------' '---------' | '---------' '---------' '---------' '---------' '---------' | '---------' '---------' '---------'
)___( )___( | )___( )___( )___( )___( )___( | )___( )___( )___(
*/ */
void check_screen_switch(const mouse_values_t *values, device_t *state) { void do_screen_switch(device_t *state, int direction) {
int new_x = state->pointer_x + values->move_x;
output_t *output = &state->config.output[state->active_output]; output_t *output = &state->config.output[state->active_output];
bool jump_left = new_x < MIN_SCREEN_COORD - state->config.jump_treshold; // bool jump_left = new_x < MIN_SCREEN_COORD - state->config.jump_treshold;
bool jump_right = new_x > MAX_SCREEN_COORD + state->config.jump_treshold; // bool jump_right = new_x > MAX_SCREEN_COORD + state->config.jump_treshold;
int direction = jump_left ? LEFT : RIGHT; // int direction = jump_left ? LEFT : RIGHT;
// /* No switching allowed if explicitly disabled or in gaming mode */ // /* No switching allowed if explicitly disabled or in gaming mode */
// if (state->switch_lock) // if (state->switch_lock)
// return; // return;
/* No jump condition met == nothing to do, return */ /* No jump condition met == nothing to do, return */
if (!jump_left && !jump_right) // if (!jump_left && !jump_right)
return; // return;
/* We want to jump in the direction of the other computer */ /* We want to jump in the direction of the other computer */
if (output->pos != direction) { if (output->pos != direction) {
@ -206,16 +222,16 @@ void check_screen_switch(const mouse_values_t *values, device_t *state) {
if (state->mouse_buttons) if (state->mouse_buttons)
return; return;
switch_screen(state, output, 1 - state->active_output, direction); switch_to_another_pc(state, output, 1 - state->active_output, direction);
} }
/* If here, this output has multiple desktops and we are not on the main one */ /* If here, this output has multiple desktops and we are not on the main one */
else else
switch_desktop(state, output, output->screen_index - 1, direction); switch_virtual_desktop(state, output, output->screen_index - 1, direction);
} }
/* We want to jump away from the other computer, only possible if there is another screen to jump to */ /* We want to jump away from the other computer, only possible if there is another screen to jump to */
else if (output->screen_index < output->screen_count) else if (output->screen_index < output->screen_count)
switch_desktop(state, output, output->screen_index + 1, direction); switch_virtual_desktop(state, output, output->screen_index + 1, direction);
} }
void extract_report_values(uint8_t *raw_report, device_t *state, mouse_values_t *values, hid_interface_t *iface) { void extract_report_values(uint8_t *raw_report, device_t *state, mouse_values_t *values, hid_interface_t *iface) {
@ -270,7 +286,7 @@ void process_mouse_report(uint8_t *raw_report, int len, uint8_t itf, hid_interfa
extract_report_values(raw_report, state, &values, iface); extract_report_values(raw_report, state, &values, iface);
/* Calculate and update mouse pointer movement. */ /* 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 */ /* Create the report for the output PC based on the updated values */
mouse_report_t report = create_mouse_report(state, &values); mouse_report_t report = create_mouse_report(state, &values);
@ -278,8 +294,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 */ /* Move the mouse, depending where the output is supposed to go */
output_mouse_report(&report, state); output_mouse_report(&report, state);
/* We use the mouse to switch outputs, the logic is in check_screen_switch() */ /* We use the mouse to switch outputs, if switch_direction is LEFT or RIGHT */
check_screen_switch(&values, state); if (switch_direction != NONE)
do_screen_switch(state, switch_direction);
} }
/* ==================================================== * /* ==================================================== *