diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 84c131a585e..5f12d3ead39 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -96,8 +96,8 @@ target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/backlight.c) target_sources_ifdef(CONFIG_ZMK_LOW_PRIORITY_WORK_QUEUE app PRIVATE src/workqueue.c) -target_sources_ifdef(CONFIG_ZMK_MOUSE_PS2 app PRIVATE src/mouse/mouse_ps2.c) -target_sources_ifdef(CONFIG_ZMK_MOUSE_PS2 app PRIVATE src/behaviors/behavior_mouse_setting.c) +target_sources_ifdef(CONFIG_ZMK_INPUT_MOUSE_PS2 app PRIVATE src/behaviors/behavior_mouse_setting.c) + target_sources(app PRIVATE src/main.c) add_subdirectory(src/display/) diff --git a/app/dts/arm/nordic/override.dtsi b/app/dts/arm/nordic/override.dtsi deleted file mode 100644 index 0138f4869a9..00000000000 --- a/app/dts/arm/nordic/override.dtsi +++ /dev/null @@ -1 +0,0 @@ -#define NRF_DEFAULT_IRQ_PRIORITY 3 diff --git a/app/dts/behaviors/mouse_setting.dtsi b/app/dts/behaviors/mouse_setting.dtsi index 3bf6e1c0335..3abb14c4fb7 100644 --- a/app/dts/behaviors/mouse_setting.dtsi +++ b/app/dts/behaviors/mouse_setting.dtsi @@ -8,7 +8,6 @@ behaviors { mms: behavior_mouse_setting { compatible = "zmk,behavior-mouse-setting"; - label = "MOUSE_SETTING"; #binding-cells = <1>; }; }; diff --git a/app/dts/bindings/zmk,mouse-ps2.yaml b/app/dts/bindings/zmk,mouse-ps2.yaml deleted file mode 100644 index fe67de43bdd..00000000000 --- a/app/dts/bindings/zmk,mouse-ps2.yaml +++ /dev/null @@ -1,15 +0,0 @@ -description: PS2 mouse configuration - -compatible: "zmk,mouse-ps2" - -properties: - ps2-device: - type: phandle - required: true - description: | - The ps2 device the mouse should use. - - rst-gpios: - type: phandle-array - required: false - description: GPIO to which the RST pin of the device is connected and on which the Power-On-Reset will be performed. diff --git a/app/include/dt-bindings/zmk/mouse_settings.h b/app/include/dt-bindings/zmk/mouse_settings.h index 5a6fc324c55..0d2502376df 100644 --- a/app/include/dt-bindings/zmk/mouse_settings.h +++ b/app/include/dt-bindings/zmk/mouse_settings.h @@ -5,6 +5,9 @@ */ #pragma once +#define MS_LOG 0 +#define MS_RESET 1 + #define MS_TP_SENSITIVITY_INCR 10 #define MS_TP_SENSITIVITY_DECR 11 diff --git a/app/module/drivers/CMakeLists.txt b/app/module/drivers/CMakeLists.txt index 5281c3dcb21..a3a15c65168 100644 --- a/app/module/drivers/CMakeLists.txt +++ b/app/module/drivers/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory_ifdef(CONFIG_GPIO gpio) add_subdirectory_ifdef(CONFIG_KSCAN kscan) add_subdirectory_ifdef(CONFIG_SENSOR sensor) add_subdirectory_ifdef(CONFIG_DISPLAY display) +add_subdirectory_ifdef(CONFIG_INPUT input) diff --git a/app/module/drivers/Kconfig b/app/module/drivers/Kconfig index c57ed3347c9..db3f49a5510 100644 --- a/app/module/drivers/Kconfig +++ b/app/module/drivers/Kconfig @@ -5,3 +5,4 @@ rsource "gpio/Kconfig" rsource "kscan/Kconfig" rsource "sensor/Kconfig" rsource "display/Kconfig" +rsource "input/Kconfig" diff --git a/app/module/drivers/input/CMakeLists.txt b/app/module/drivers/input/CMakeLists.txt new file mode 100644 index 00000000000..c85704d650f --- /dev/null +++ b/app/module/drivers/input/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) 2022 The ZMK Contributors + # SPDX-License-Identifier: MIT + + zephyr_library_amend() + + zephyr_library_sources_ifdef(CONFIG_ZMK_INPUT_MOUSE_PS2 input_mouse_ps2.c) diff --git a/app/module/drivers/input/Kconfig b/app/module/drivers/input/Kconfig new file mode 100644 index 00000000000..c8f7208e3c0 --- /dev/null +++ b/app/module/drivers/input/Kconfig @@ -0,0 +1,19 @@ +# Copyright (c) 2022 The ZMK Contributors +# SPDX-License-Identifier: MIT + +DT_COMPAT_ZMK_INPUT_PS2_MOUSE := zmk,input-mouse-ps2 + +config ZMK_INPUT_MOUSE_PS2 + bool + default $(dt_compat_enabled,$(DT_COMPAT_ZMK_INPUT_PS2_MOUSE)) + depends on (!ZMK_SPLIT || ZMK_SPLIT_ROLE_CENTRAL) + select ZMK_MOUSE + select PS2 + +if ZMK_INPUT_MOUSE_PS2 + +config ZMK_INPUT_MOUSE_PS2_ENABLE_ERROR_MITIGATION + bool "Tries to mitigate transmission errors. Only useful when using a PS2 driver that is prone to miscommunication like the GPIO bitbanging driver." + default n + +endif # ZMK_INPUT_MOUSE_PS2 diff --git a/app/src/mouse/mouse_ps2.c b/app/module/drivers/input/input_mouse_ps2.c similarity index 50% rename from app/src/mouse/mouse_ps2.c rename to app/module/drivers/input/input_mouse_ps2.c index 67936be3c8f..ab9578d6a77 100644 --- a/app/src/mouse/mouse_ps2.c +++ b/app/module/drivers/input/input_mouse_ps2.c @@ -4,33 +4,29 @@ * SPDX-License-Identifier: Apache-2.0 */ -#define DT_DRV_COMPAT zmk_mouse_ps2 +#define DT_DRV_COMPAT zmk_input_mouse_ps2 #include -#include - #include #include #include #include +#include +#include #include #include #include -#include -#include -#include -#include -#include - - LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); /* * Settings */ +// Delay mouse init to give the mouse time to send the init sequence. +#define ZMK_MOUSE_PS2_INIT_THREAD_DELAY_MS 1000 + // How often the driver try to initialize a mouse before we give up. #define MOUSE_PS2_INIT_ATTEMPTS 10 @@ -153,15 +149,27 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define MOUSE_PS2_SETTINGS_SUBTREE "mouse_ps2" -typedef enum -{ +typedef enum { MOUSE_PS2_PACKET_MODE_PS2_DEFAULT, MOUSE_PS2_PACKET_MODE_SCROLL, } zmk_mouse_ps2_packet_mode; struct zmk_mouse_ps2_config { - const struct device *ps2_device; - struct gpio_dt_spec rst_gpio; + const struct device *ps2_device; + struct gpio_dt_spec rst_gpio; + + bool scroll_mode; + bool disable_clicking; + int sampling_rate; + + bool tp_press_to_select; + int tp_press_to_select_threshold; + int tp_sensitivity; + int tp_neg_inertia; + int tp_val6_upper_speed; + bool tp_x_invert; + bool tp_y_invert; + bool tp_xy_swap; }; struct zmk_mouse_ps2_packet { @@ -176,7 +184,8 @@ struct zmk_mouse_ps2_packet { }; struct zmk_mouse_ps2_data { - struct gpio_dt_spec rst_gpio; /* GPIO used for Power-On-Reset line */ + const struct device *dev; + struct gpio_dt_spec rst_gpio; /* GPIO used for Power-On-Reset line */ K_THREAD_STACK_MEMBER(thread_stack, MOUSE_PS2_THREAD_STACK_SIZE); struct k_thread thread; @@ -187,12 +196,6 @@ struct zmk_mouse_ps2_data { struct zmk_mouse_ps2_packet prev_packet; struct k_work_delayable packet_buffer_timeout; - // Stores the x and y coordinates between reporting to the os - struct vector2d move_speed; - struct vector2d scroll_speed; - struct k_timer mouse_timer; - struct k_work mouse_tick; - bool button_l_is_held; bool button_m_is_held; bool button_r_is_held; @@ -207,43 +210,52 @@ struct zmk_mouse_ps2_data { uint8_t tp_pts_threshold; }; - static const struct zmk_mouse_ps2_config zmk_mouse_ps2_config = { .ps2_device = DEVICE_DT_GET(DT_INST_PHANDLE(0, ps2_device)), #if DT_INST_NODE_HAS_PROP(0, rst_gpios) - .rst_gpio = GPIO_DT_SPEC_INST_GET(0, rst_gpios), + .rst_gpio = GPIO_DT_SPEC_INST_GET(0, rst_gpios), #else - .rst_gpio = { - .port = NULL, - .pin = 0, - .dt_flags = 0, - }, + .rst_gpio = + { + .port = NULL, + .pin = 0, + .dt_flags = 0, + }, #endif + .scroll_mode = DT_INST_PROP_OR(0, scroll_mode, false), + .disable_clicking = DT_INST_PROP_OR(0, disable_clicking, false), + .sampling_rate = DT_INST_PROP_OR(0, sampling_rate, MOUSE_PS2_CMD_SET_SAMPLING_RATE_DEFAULT), + .tp_press_to_select = DT_INST_PROP_OR(0, tp_press_to_select, false), + .tp_press_to_select_threshold = DT_INST_PROP_OR(0, tp_press_to_select_threshold, -1), + .tp_sensitivity = DT_INST_PROP_OR(0, tp_sensitivity, -1), + .tp_neg_inertia = DT_INST_PROP_OR(0, tp_neg_inertia, -1), + .tp_val6_upper_speed = DT_INST_PROP_OR(0, tp_val6_upper_speed, -1), + .tp_x_invert = DT_INST_PROP_OR(0, tp_x_invert, false), + .tp_y_invert = DT_INST_PROP_OR(0, tp_y_invert, false), + .tp_xy_swap = DT_INST_PROP_OR(0, tp_xy_swap, false), }; static struct zmk_mouse_ps2_data zmk_mouse_ps2_data = { .packet_mode = MOUSE_PS2_PACKET_MODE_PS2_DEFAULT, .packet_idx = 0, - .prev_packet = { - .button_l = false, - .button_r = false, - .button_m = false, - .overflow_x = 0, - .overflow_y = 0, - .mov_x = 0, - .mov_y = 0, - .scroll = 0, - }, + .prev_packet = + { + .button_l = false, + .button_r = false, + .button_m = false, + .overflow_x = 0, + .overflow_y = 0, + .mov_x = 0, + .mov_y = 0, + .scroll = 0, + }, .button_l_is_held = false, .button_m_is_held = false, .button_r_is_held = false, - .move_speed = {0}, - .scroll_speed = {0}, - // Data reporting is disabled on init .activity_reporting_on = false, .is_trackpoint = false, @@ -257,16 +269,9 @@ static struct zmk_mouse_ps2_data zmk_mouse_ps2_data = { }; static int allowed_sampling_rates[] = { - 10, - 20, - 40, - 60, - 80, - 100, - 200, + 10, 20, 40, 60, 80, 100, 200, }; - /* * Function Definitions */ @@ -277,43 +282,29 @@ int zmk_mouse_ps2_settings_save(); * Helpers */ -#define MOUSE_PS2_GET_BIT(data, bit_pos) ( (data >> bit_pos) & 0x1 ) -#define MOUSE_PS2_SET_BIT(data, bit_val, bit_pos) ( \ - data |= (bit_val) << bit_pos \ -) +#define MOUSE_PS2_GET_BIT(data, bit_pos) ((data >> bit_pos) & 0x1) +#define MOUSE_PS2_SET_BIT(data, bit_val, bit_pos) (data |= (bit_val) << bit_pos) /* * Mouse Activity Packet Reading */ -void zmk_mouse_ps2_activity_process_cmd( - zmk_mouse_ps2_packet_mode packet_mode, - uint8_t packet_state, - uint8_t packet_x, - uint8_t packet_y, - uint8_t packet_extra -); +void zmk_mouse_ps2_activity_process_cmd(zmk_mouse_ps2_packet_mode packet_mode, uint8_t packet_state, + uint8_t packet_x, uint8_t packet_y, uint8_t packet_extra); void zmk_mouse_ps2_activity_abort_cmd(); void zmk_mouse_ps2_activity_move_mouse(int16_t mov_x, int16_t mov_y); void zmk_mouse_ps2_activity_scroll(int8_t scroll_y); -void zmk_mouse_ps2_activity_click_buttons(bool button_l, - bool button_m, - bool button_r); +void zmk_mouse_ps2_activity_click_buttons(bool button_l, bool button_m, bool button_r); void zmk_mouse_ps2_activity_reset_packet_buffer(); -struct zmk_mouse_ps2_packet zmk_mouse_ps2_activity_parse_packet_buffer( - zmk_mouse_ps2_packet_mode packet_mode, - uint8_t packet_state, - uint8_t packet_x, - uint8_t packet_y, - uint8_t packet_extra -); - +struct zmk_mouse_ps2_packet +zmk_mouse_ps2_activity_parse_packet_buffer(zmk_mouse_ps2_packet_mode packet_mode, + uint8_t packet_state, uint8_t packet_x, uint8_t packet_y, + uint8_t packet_extra); +void zmk_mouse_ps2_activity_toggle_layer(); // Called by the PS/2 driver whenver the mouse sends a byte and // reporting is enabled through `zmk_mouse_ps2_activity_reporting_enable`. -void zmk_mouse_ps2_activity_callback(const struct device *ps2_device, - uint8_t byte) -{ +void zmk_mouse_ps2_activity_callback(const struct device *ps2_device, uint8_t byte) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; k_work_cancel_delayable(&data->packet_buffer_timeout); @@ -322,51 +313,41 @@ void zmk_mouse_ps2_activity_callback(const struct device *ps2_device, data->packet_buffer[data->packet_idx] = byte; - if(data->packet_idx == 0) { + if (data->packet_idx == 0) { // Bit 3 of the first command byte should always be 1 // If it is not, then we are definitely out of alignment. // So we ask the device to resend the entire 3-byte command // again. int alignment_bit = MOUSE_PS2_GET_BIT(byte, 3); - if(alignment_bit != 1) { + if (alignment_bit != 1) { - zmk_mouse_ps2_activity_abort_cmd(); + zmk_mouse_ps2_activity_abort_cmd("Bit 3 of packet is 0 instead of 1"); return; } - } else if(data->packet_idx == 1) { + } else if (data->packet_idx == 1) { // Do nothing - } else if( - (data->packet_mode == MOUSE_PS2_PACKET_MODE_PS2_DEFAULT && - data->packet_idx == 2) || - (data->packet_mode == MOUSE_PS2_PACKET_MODE_SCROLL && - data->packet_idx == 3) - ) { - - zmk_mouse_ps2_activity_process_cmd( - data->packet_mode, - data->packet_buffer[0], - data->packet_buffer[1], - data->packet_buffer[2], - data->packet_buffer[3] - ); + } else if ((data->packet_mode == MOUSE_PS2_PACKET_MODE_PS2_DEFAULT && data->packet_idx == 2) || + (data->packet_mode == MOUSE_PS2_PACKET_MODE_SCROLL && data->packet_idx == 3)) { + + zmk_mouse_ps2_activity_process_cmd(data->packet_mode, data->packet_buffer[0], + data->packet_buffer[1], data->packet_buffer[2], + data->packet_buffer[3]); zmk_mouse_ps2_activity_reset_packet_buffer(); return; } data->packet_idx += 1; - k_work_schedule(&data->packet_buffer_timeout, MOUSE_PS2_TIMEOUT_ACTIVITY_PACKET); + k_work_schedule(&data->packet_buffer_timeout, MOUSE_PS2_TIMEOUT_ACTIVITY_PACKET); } -void zmk_mouse_ps2_activity_abort_cmd() { +void zmk_mouse_ps2_activity_abort_cmd(char *reason) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; const struct device *ps2_device = config->ps2_device; - LOG_ERR( - "PS/2 Mouse cmd buffer is out of aligment. Requesting resend." - ); + LOG_ERR("PS/2 Mouse cmd buffer is out of aligment. Requesting resend: %s", reason); data->packet_idx = 0; ps2_write(ps2_device, MOUSE_PS2_CMD_RESEND[0]); @@ -378,21 +359,17 @@ void zmk_mouse_ps2_activity_abort_cmd() { // device to resend the packet. // The device will resend all bytes of the packet. So we need to reset our // buffer. -void zmk_mouse_ps2_activity_resend_callback(const struct device *ps2_device) -{ +void zmk_mouse_ps2_activity_resend_callback(const struct device *ps2_device) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - LOG_WRN( - "Mouse movement cmd had transmission error on idx=%d", data->packet_idx - ); + LOG_WRN("Mouse movement cmd had transmission error on idx=%d", data->packet_idx); zmk_mouse_ps2_activity_reset_packet_buffer(); } // Called if no new byte arrives within // MOUSE_PS2_TIMEOUT_ACTIVITY_PACKET -void zmk_mouse_ps2_activity_packet_timout(struct k_work *item) -{ +void zmk_mouse_ps2_activity_packet_timout(struct k_work *item) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; LOG_DBG("Mouse movement cmd timed out on idx=%d", data->packet_idx); @@ -403,49 +380,36 @@ void zmk_mouse_ps2_activity_packet_timout(struct k_work *item) zmk_mouse_ps2_activity_reset_packet_buffer(); } -void zmk_mouse_ps2_activity_reset_packet_buffer() -{ +void zmk_mouse_ps2_activity_reset_packet_buffer() { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; data->packet_idx = 0; memset(data->packet_buffer, 0x0, sizeof(data->packet_buffer)); } -void zmk_mouse_ps2_activity_process_cmd( - zmk_mouse_ps2_packet_mode packet_mode, - uint8_t packet_state, - uint8_t packet_x, - uint8_t packet_y, - uint8_t packet_extra -) -{ +void zmk_mouse_ps2_activity_process_cmd(zmk_mouse_ps2_packet_mode packet_mode, uint8_t packet_state, + uint8_t packet_x, uint8_t packet_y, uint8_t packet_extra) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; struct zmk_mouse_ps2_packet packet; - packet = zmk_mouse_ps2_activity_parse_packet_buffer( - packet_mode, - packet_state, packet_x, packet_y, packet_extra - ); + packet = zmk_mouse_ps2_activity_parse_packet_buffer(packet_mode, packet_state, packet_x, + packet_y, packet_extra); int x_delta = abs(data->prev_packet.mov_x - packet.mov_x); int y_delta = abs(data->prev_packet.mov_y - packet.mov_y); - LOG_DBG( - "Got mouse activity cmd " - "(mov_x=%d, mov_y=%d, o_x=%d, o_y=%d, scroll=%d, " - "b_l=%d, b_m=%d, b_r=%d) and (" - "x_delta=%d, y_delta=%d)", - packet.mov_x, packet.mov_y, packet.overflow_x, packet.overflow_y, - packet.scroll, packet.button_l, packet.button_m, packet.button_r, - x_delta, y_delta - ); - - if(packet.overflow_x == 1 && packet.overflow_y == 1) { - LOG_WRN( - "Detected overflow in both x and y. " - "Probably mistransmission. Aborting..." - ); - - zmk_mouse_ps2_activity_abort_cmd(); + LOG_DBG("Got mouse activity cmd " + "(mov_x=%d, mov_y=%d, o_x=%d, o_y=%d, scroll=%d, " + "b_l=%d, b_m=%d, b_r=%d) and (" + "x_delta=%d, y_delta=%d)", + packet.mov_x, packet.mov_y, packet.overflow_x, packet.overflow_y, packet.scroll, + packet.button_l, packet.button_m, packet.button_r, x_delta, y_delta); + +#if IS_ENABLED(CONFIG_ZMK_INPUT_MOUSE_PS2_ENABLE_ERROR_MITIGATION) + if (packet.overflow_x == 1 && packet.overflow_y == 1) { + LOG_WRN("Detected overflow in both x and y. " + "Probably mistransmission. Aborting..."); + + zmk_mouse_ps2_activity_abort_cmd("Overflow in both x and y"); return; } @@ -453,39 +417,28 @@ void zmk_mouse_ps2_activity_process_cmd( // a mistransmission or misalignment. // But we only do this check if there was prior movement that wasn't // reset in `zmk_mouse_ps2_activity_packet_timout`. - if((data->move_speed.x != 0 && data->move_speed.y != 0) && - (x_delta > 150 || y_delta > 150)) - { - LOG_WRN( - "Detected malformed packet with " - "(mov_x=%d, mov_y=%d, o_x=%d, o_y=%d, scroll=%d, " - "b_l=%d, b_m=%d, b_r=%d) and (" - "x_delta=%d, y_delta=%d)", - packet.mov_x, packet.mov_y, packet.overflow_x, packet.overflow_y, - packet.scroll, packet.button_l, packet.button_m, packet.button_r, - x_delta, y_delta - ); - zmk_mouse_ps2_activity_abort_cmd(); + if ((packet.mov_x != 0 && packet.mov_y != 0) && (x_delta > 150 || y_delta > 150)) { + LOG_WRN("Detected malformed packet with " + "(mov_x=%d, mov_y=%d, o_x=%d, o_y=%d, scroll=%d, " + "b_l=%d, b_m=%d, b_r=%d) and (" + "x_delta=%d, y_delta=%d)", + packet.mov_x, packet.mov_y, packet.overflow_x, packet.overflow_y, packet.scroll, + packet.button_l, packet.button_m, packet.button_r, x_delta, y_delta); + zmk_mouse_ps2_activity_abort_cmd("Exceeds movement threshold."); return; } +#endif - zmk_mouse_ps2_activity_click_buttons( - packet.button_l, packet.button_m, packet.button_r - ); zmk_mouse_ps2_activity_move_mouse(packet.mov_x, packet.mov_y); - zmk_mouse_ps2_activity_scroll(packet.scroll); + zmk_mouse_ps2_activity_click_buttons(packet.button_l, packet.button_m, packet.button_r); data->prev_packet = packet; } -struct zmk_mouse_ps2_packet zmk_mouse_ps2_activity_parse_packet_buffer( - zmk_mouse_ps2_packet_mode packet_mode, - uint8_t packet_state, - uint8_t packet_x, - uint8_t packet_y, - uint8_t packet_extra -) -{ +struct zmk_mouse_ps2_packet +zmk_mouse_ps2_activity_parse_packet_buffer(zmk_mouse_ps2_packet_mode packet_mode, + uint8_t packet_state, uint8_t packet_x, uint8_t packet_y, + uint8_t packet_extra) { struct zmk_mouse_ps2_packet packet; packet.button_l = MOUSE_PS2_GET_BIT(packet_state, 0); @@ -522,116 +475,40 @@ struct zmk_mouse_ps2_packet zmk_mouse_ps2_activity_parse_packet_buffer( // then the first 4 bit of the extra byte are used for the // scroll wheel. It is a signed number with the rango of // -8 to +7. - if(packet_mode == MOUSE_PS2_PACKET_MODE_SCROLL) { - MOUSE_PS2_SET_BIT( - packet.scroll, - MOUSE_PS2_GET_BIT(packet_extra, 0), - 0 - ); - MOUSE_PS2_SET_BIT( - packet.scroll, - MOUSE_PS2_GET_BIT(packet_extra, 1), - 1 - ); - MOUSE_PS2_SET_BIT( - packet.scroll, - MOUSE_PS2_GET_BIT(packet_extra, 2), - 2 - ); + if (packet_mode == MOUSE_PS2_PACKET_MODE_SCROLL) { + MOUSE_PS2_SET_BIT(packet.scroll, MOUSE_PS2_GET_BIT(packet_extra, 0), 0); + MOUSE_PS2_SET_BIT(packet.scroll, MOUSE_PS2_GET_BIT(packet_extra, 1), 1); + MOUSE_PS2_SET_BIT(packet.scroll, MOUSE_PS2_GET_BIT(packet_extra, 2), 2); packet.scroll = packet_extra - ((packet.scroll << 3) & 0x100); } return packet; } - /* * Mouse Moving and Clicking */ -// We don't send the mouse move over BT every time we get a PS2 packet, -// because it can send too fast. -// Here we just add up the coordinates and then we use a timer to actually -// send the movement once every CONFIG_ZMK_MOUSE_TICK_DURATION ms in -// zmk_mouse_ps2_tick_timer_handler. -void zmk_mouse_ps2_activity_move_mouse(int16_t mov_x, int16_t mov_y) -{ - struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - - data->move_speed.x += mov_x; - data->move_speed.y += mov_y; -} - -void zmk_mouse_ps2_activity_scroll(int8_t scroll_y) -{ - if(scroll_y > 0) { - LOG_INF("Mouse scrolled by: %d", scroll_y); +static bool zmk_mouse_ps2_is_non_zero_1d_movement(int16_t speed) { return speed != 0; } - struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - data->scroll_speed.y += scroll_y; - } -} - -// Called using k_timer data->mouse_timer every x ms as configured with -// CONFIG_ZMK_MOUSE_TICK_DURATION -void zmk_mouse_ps2_tick_timer_cb(struct k_timer *dummy) { +void zmk_mouse_ps2_activity_move_mouse(int16_t mov_x, int16_t mov_y) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; + int ret = 0; - // LOG_DBG("Submitting mouse work to queue"); - // Calls zmk_mouse_ps2_tick_timer_handler on our work queue - k_work_submit_to_queue(zmk_mouse_work_q(), &data->mouse_tick); -} + bool have_x = zmk_mouse_ps2_is_non_zero_1d_movement(mov_x); + bool have_y = zmk_mouse_ps2_is_non_zero_1d_movement(mov_y); -// Here is where we actually ask zmk to send the mouse movement to -// the OS. -static void zmk_mouse_ps2_tick_timer_handler(struct k_work *work) -{ - struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - - // LOG_DBG("Raising mouse tick event"); - - if(data->move_speed.x == 0 && data->move_speed.y == 0 && - data->scroll_speed.x == 0 && data->scroll_speed.y == 0) { - // LOG_DBG("Not raising mouse tick event as the mouse hasn't moved."); - return; + if (have_x) { + ret = input_report_rel(data->dev, INPUT_REL_X, mov_x, !have_y, K_NO_WAIT); + } + if (have_y) { + ret = input_report_rel(data->dev, INPUT_REL_Y, mov_y, true, K_NO_WAIT); } - - struct vector2d mouse_move = data->move_speed; - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_SWAP_XY) - mouse_move.x = data->move_speed.y; - mouse_move.y = data->move_speed.x; - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_SWAP_XY) */ - - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_INVERT_X) - mouse_move.x = -mouse_move.x; - #endif /* IS_ENABLED(ZMK_MOUSE_PS2_INVERT_X) */ - - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_INVERT_Y) - mouse_move.y = -mouse_move.y; - #endif /* IS_ENABLED(ZMK_MOUSE_PS2_INVERT_Y) */ - - zmk_hid_mouse_movement_set(0, 0); - - // ZMK expects up movement to be negative, but PS2 sends it as positive - zmk_hid_mouse_movement_update(mouse_move.x, -mouse_move.y); - - zmk_hid_mouse_scroll_set(0, 0); - zmk_hid_mouse_scroll_update(data->scroll_speed.x, data->scroll_speed.y); - - zmk_endpoints_send_mouse_report(); - - data->move_speed.x = 0; - data->move_speed.y = 0; - - data->scroll_speed.x = 0; - data->scroll_speed.y = 0; } -void zmk_mouse_ps2_activity_click_buttons(bool button_l, - bool button_m, - bool button_r) -{ +void zmk_mouse_ps2_activity_click_buttons(bool button_l, bool button_m, bool button_r) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; + const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; // TODO: Integrate this with the proper button mask instead // of hardcoding the mouse button indeces. @@ -643,12 +520,12 @@ void zmk_mouse_ps2_activity_click_buttons(bool button_l, // First we check which mouse button press states have changed bool button_l_pressed = false; bool button_l_released = false; - if(button_l == true && data->button_l_is_held == false) { + if (button_l == true && data->button_l_is_held == false) { LOG_INF("Pressed button_l"); button_l_pressed = true; buttons_pressed++; - } else if(button_l == false && data->button_l_is_held == true) { + } else if (button_l == false && data->button_l_is_held == true) { LOG_INF("Releasing button_l"); button_l_released = true; @@ -657,12 +534,12 @@ void zmk_mouse_ps2_activity_click_buttons(bool button_l, bool button_m_released = false; bool button_m_pressed = false; - if(button_m == true && data->button_m_is_held == false) { + if (button_m == true && data->button_m_is_held == false) { LOG_INF("Pressing button_m"); button_m_pressed = true; buttons_pressed++; - } else if(button_m == false && data->button_m_is_held == true) { + } else if (button_m == false && data->button_m_is_held == true) { LOG_INF("Releasing button_m"); button_m_released = true; @@ -671,12 +548,12 @@ void zmk_mouse_ps2_activity_click_buttons(bool button_l, bool button_r_released = false; bool button_r_pressed = false; - if(button_r == true && data->button_r_is_held == false) { + if (button_r == true && data->button_r_is_held == false) { LOG_INF("Pressing button_r"); button_r_pressed = true; buttons_pressed++; - } else if(button_r == false && data->button_r_is_held == true) { + } else if (button_r == false && data->button_r_is_held == true) { LOG_INF("Releasing button_r"); button_r_released = true; @@ -684,86 +561,83 @@ void zmk_mouse_ps2_activity_click_buttons(bool button_l, } // Then we check if this is likely a transmission error - if(buttons_pressed > 1 || buttons_released > 1) { - LOG_WRN( - "Ignoring button presses: Received %d button presses " - "and %d button releases in one packet. " - "Probably tranmission error.", - buttons_pressed, buttons_released - ); - - zmk_mouse_ps2_activity_abort_cmd(); + if (buttons_pressed > 1 || buttons_released > 1) { + LOG_WRN("Ignoring button presses: Received %d button presses " + "and %d button releases in one packet. " + "Probably tranmission error.", + buttons_pressed, buttons_released); + + zmk_mouse_ps2_activity_abort_cmd("Multiple button presses"); return; } - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_ENABLE_CLICKING) - + if (config->disable_clicking != true) { // If it wasn't, we actually send the events. - if(buttons_pressed > 0 || buttons_released > 0) { + if (buttons_pressed > 0 || buttons_released > 0) { + + int buttons_need_reporting = buttons_pressed + buttons_released; // Left button - if(button_l_pressed) { + if (button_l_pressed) { - zmk_hid_mouse_button_press(MOUSE_PS2_BUTTON_L_IDX); + input_report_key(data->dev, INPUT_BTN_0, 1, + buttons_need_reporting == 1 ? true : false, K_FOREVER); data->button_l_is_held = true; - } else if(button_l_released) { + } else if (button_l_released) { - zmk_hid_mouse_button_release(MOUSE_PS2_BUTTON_L_IDX); + input_report_key(data->dev, INPUT_BTN_0, 0, + buttons_need_reporting == 1 ? true : false, K_FOREVER); data->button_l_is_held = false; } - // Middle Button - if(button_m_pressed) { - - zmk_hid_mouse_button_press(MOUSE_PS2_BUTTON_M_IDX); - data->button_m_is_held = true; - } else if(button_m_released) { - - zmk_hid_mouse_button_release(MOUSE_PS2_BUTTON_M_IDX); - data->button_m_is_held = false; - } + buttons_need_reporting--; // Right button - if(button_r_pressed) { + if (button_r_pressed) { - zmk_hid_mouse_button_press(MOUSE_PS2_BUTTON_R_IDX); + input_report_key(data->dev, INPUT_BTN_1, 1, + buttons_need_reporting == 1 ? true : false, K_FOREVER); data->button_r_is_held = true; - } else if(button_r_released) { + } else if (button_r_released) { - zmk_hid_mouse_button_release(MOUSE_PS2_BUTTON_R_IDX); + input_report_key(data->dev, INPUT_BTN_1, 0, + buttons_need_reporting == 1 ? true : false, K_FOREVER); data->button_r_is_held = false; } - // Since mouse clicks generate far few events than movement, - // we send them right away instead of using the timer. - zmk_endpoints_send_mouse_report(); + buttons_need_reporting--; + + // Middle Button + if (button_m_pressed) { + + input_report_key(data->dev, INPUT_BTN_2, 1, + buttons_need_reporting == 1 ? true : false, K_FOREVER); + data->button_m_is_held = true; + } else if (button_m_released) { + + input_report_key(data->dev, INPUT_BTN_2, 0, + buttons_need_reporting == 1 ? true : false, K_FOREVER); + data->button_m_is_held = false; + } } - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_ENABLE_CLICKING) */ + } } - /* * PS/2 Command Sending Wrapper */ int zmk_mouse_ps2_activity_reporting_enable(); int zmk_mouse_ps2_activity_reporting_disable(); - struct zmk_mouse_ps2_send_cmd_resp { int err; - char err_msg[50]; + char err_msg[80]; uint8_t resp_buffer[8]; int resp_len; }; -struct zmk_mouse_ps2_send_cmd_resp zmk_mouse_ps2_send_cmd( - char *cmd, - int cmd_len, - uint8_t *arg, - int resp_len, - bool pause_reporting -) -{ +struct zmk_mouse_ps2_send_cmd_resp zmk_mouse_ps2_send_cmd(char *cmd, int cmd_len, uint8_t *arg, + int resp_len, bool pause_reporting) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; const struct device *ps2_device = config->ps2_device; @@ -779,96 +653,79 @@ struct zmk_mouse_ps2_send_cmd_resp zmk_mouse_ps2_send_cmd( // Don't send the string termination NULL byte int cmd_bytes = cmd_len - 1; - if(cmd_bytes < 1) { + if (cmd_bytes < 1) { err = 1; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Cannot send cmd with less than 1 byte length" - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), + "Cannot send cmd with less than 1 byte length"); return resp; } - if(resp_len > sizeof(resp.resp_buffer)) { + if (resp_len > sizeof(resp.resp_buffer)) { err = 2; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Response can't be longer than the resp_buffer (%d)", - sizeof(resp.err_msg) - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), + "Response can't be longer than the resp_buffer (%d)", sizeof(resp.err_msg)); return resp; } - if(pause_reporting == true && data->activity_reporting_on == true) { + if (pause_reporting == true && data->activity_reporting_on == true) { LOG_DBG("Disabling mouse activity reporting..."); err = zmk_mouse_ps2_activity_reporting_disable(); - if(err) { + if (err) { resp.err = err; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Could not disable data reporting (%d)", err - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), "Could not disable data reporting (%d)", + err); } } - if(resp.err == 0) { + if (resp.err == 0) { LOG_DBG("Sending cmd..."); - for(int i = 0; i < cmd_bytes; i++) { + for (int i = 0; i < cmd_bytes; i++) { err = ps2_write(ps2_device, cmd[i]); - if(err) { + if (err) { resp.err = err; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Could not send cmd byte %d/%d (%d)", i+1, cmd_bytes, err - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), "Could not send cmd byte %d/%d (%d)", + i + 1, cmd_bytes, err); break; } } } - if(resp.err == 0 && arg != NULL) { + if (resp.err == 0 && arg != NULL) { LOG_DBG("Sending arg..."); err = ps2_write(ps2_device, *arg); - if(err) { + if (err) { resp.err = err; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Could not send arg (%d)", err - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), "Could not send arg (%d)", err); } } - if(resp.err == 0 && resp_len > 0) { + if (resp.err == 0 && resp_len > 0) { LOG_DBG("Reading response..."); - for(int i = 0; i < resp_len; i++) { + for (int i = 0; i < resp_len; i++) { err = ps2_read(ps2_device, &resp.resp_buffer[i]); - if(err) { + if (err) { resp.err = err; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Could not read response cmd byte %d/%d (%d)", - i+1, resp_len, err - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), + "Could not read response cmd byte %d/%d (%d)", i + 1, resp_len, err); break; } } } - if(pause_reporting == true && prev_activity_reporting_on == true) { + if (pause_reporting == true && prev_activity_reporting_on == true) { LOG_DBG("Enabling mouse activity reporting..."); err = zmk_mouse_ps2_activity_reporting_enable(); - if(err) { + if (err) { // Don' overwrite existing error - if(resp.err == 0) { + if (resp.err == 0) { resp.err = err; - snprintf( - resp.err_msg, sizeof(resp.err_msg), - "Could not re-enable data reporting (%d)", err - ); + snprintf(resp.err_msg, sizeof(resp.err_msg), + "Could not re-enable data reporting (%d)", err); } } } @@ -876,26 +733,24 @@ struct zmk_mouse_ps2_send_cmd_resp zmk_mouse_ps2_send_cmd( return resp; } - -int zmk_mouse_ps2_activity_reporting_enable() -{ +int zmk_mouse_ps2_activity_reporting_enable() { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; const struct device *ps2_device = config->ps2_device; - if(data->activity_reporting_on == true) { + if (data->activity_reporting_on == true) { return 0; } uint8_t cmd = MOUSE_PS2_CMD_ENABLE_REPORTING[0]; int err = ps2_write(ps2_device, cmd); - if(err) { + if (err) { LOG_ERR("Could not enable data reporting: %d", err); return err; } err = ps2_enable_callback(ps2_device); - if(err) { + if (err) { LOG_ERR("Could not enable ps2 callback: %d", err); return err; } @@ -905,25 +760,24 @@ int zmk_mouse_ps2_activity_reporting_enable() return 0; } -int zmk_mouse_ps2_activity_reporting_disable() -{ +int zmk_mouse_ps2_activity_reporting_disable() { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; const struct device *ps2_device = config->ps2_device; - if(data->activity_reporting_on == false) { + if (data->activity_reporting_on == false) { return 0; } uint8_t cmd = MOUSE_PS2_CMD_DISABLE_REPORTING[0]; int err = ps2_write(ps2_device, cmd); - if(err) { + if (err) { LOG_ERR("Could not disable data reporting: %d", err); return err; } err = ps2_disable_callback(ps2_device); - if(err) { + if (err) { LOG_ERR("Could not disable ps2 callback: %d", err); return err; } @@ -933,18 +787,14 @@ int zmk_mouse_ps2_activity_reporting_disable() return 0; } - /* * PS/2 Command Helpers */ -int zmk_mouse_ps2_array_get_elem_index(int elem, - int *array, - size_t array_size) -{ +int zmk_mouse_ps2_array_get_elem_index(int elem, int *array, size_t array_size) { int elem_index = -1; - for(int i = 0; i < array_size; i++) { - if(array[i] == elem) { + for (int i = 0; i < array_size; i++) { + if (array[i] == elem) { elem_index = i; break; } @@ -953,80 +803,55 @@ int zmk_mouse_ps2_array_get_elem_index(int elem, return elem_index; } -int zmk_mouse_ps2_array_get_next_elem(int elem, - int *array, - size_t array_size) -{ - int elem_index = zmk_mouse_ps2_array_get_elem_index( - elem, array, array_size - ); - if(elem_index == -1) { +int zmk_mouse_ps2_array_get_next_elem(int elem, int *array, size_t array_size) { + int elem_index = zmk_mouse_ps2_array_get_elem_index(elem, array, array_size); + if (elem_index == -1) { return -1; } int next_index = elem_index + 1; - if(next_index >= array_size) { + if (next_index >= array_size) { return -1; } return array[next_index]; } -int zmk_mouse_ps2_array_get_prev_elem(int elem, - int *array, - size_t array_size) -{ - int elem_index = zmk_mouse_ps2_array_get_elem_index( - elem, array, array_size - ); - if(elem_index == -1) { +int zmk_mouse_ps2_array_get_prev_elem(int elem, int *array, size_t array_size) { + int elem_index = zmk_mouse_ps2_array_get_elem_index(elem, array, array_size); + if (elem_index == -1) { return -1; } int prev_index = elem_index - 1; - if(prev_index < 0 || prev_index >= array_size) { + if (prev_index < 0 || prev_index >= array_size) { return -1; } return array[prev_index]; } - /* * PS/2 Commands */ int zmk_mouse_ps2_reset(const struct device *ps2_device) { - struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_RESET, - sizeof(MOUSE_PS2_CMD_RESET), - NULL, - MOUSE_PS2_CMD_RESET_RESP_LEN, - false - ); - if(resp.err) { - LOG_ERR( - "Could not send reset cmd" - ); + struct zmk_mouse_ps2_send_cmd_resp resp = + zmk_mouse_ps2_send_cmd(MOUSE_PS2_CMD_RESET, sizeof(MOUSE_PS2_CMD_RESET), NULL, + MOUSE_PS2_CMD_RESET_RESP_LEN, false); + if (resp.err) { + LOG_ERR("Could not send reset cmd"); } return resp.err; } -int zmk_mouse_ps2_get_secondary_id(uint8_t *resp_byte_1, - uint8_t *resp_byte_2) -{ +int zmk_mouse_ps2_get_secondary_id(uint8_t *resp_byte_1, uint8_t *resp_byte_2) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_GET_SECONDARY_ID, - sizeof(MOUSE_PS2_CMD_GET_SECONDARY_ID), - NULL, - MOUSE_PS2_CMD_GET_SECONDARY_ID_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get secondary id" - ); + MOUSE_PS2_CMD_GET_SECONDARY_ID, sizeof(MOUSE_PS2_CMD_GET_SECONDARY_ID), NULL, + MOUSE_PS2_CMD_GET_SECONDARY_ID_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not get secondary id"); return resp.err; } @@ -1036,52 +861,36 @@ int zmk_mouse_ps2_get_secondary_id(uint8_t *resp_byte_1, return 0; } -int zmk_mouse_ps2_set_sampling_rate(uint8_t sampling_rate) -{ +int zmk_mouse_ps2_set_sampling_rate(uint8_t sampling_rate) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - int rate_idx = zmk_mouse_ps2_array_get_elem_index( - sampling_rate, - allowed_sampling_rates, sizeof(allowed_sampling_rates) - ); - if(rate_idx == -1) { + int rate_idx = zmk_mouse_ps2_array_get_elem_index(sampling_rate, allowed_sampling_rates, + sizeof(allowed_sampling_rates)); + if (rate_idx == -1) { LOG_ERR("Requested to set illegal sampling rate: %d", sampling_rate); return -1; } struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_SET_SAMPLING_RATE, - sizeof(MOUSE_PS2_CMD_SET_SAMPLING_RATE), - &sampling_rate, - MOUSE_PS2_CMD_SET_SAMPLING_RATE_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set sample rate to %d", sampling_rate - ); + MOUSE_PS2_CMD_SET_SAMPLING_RATE, sizeof(MOUSE_PS2_CMD_SET_SAMPLING_RATE), &sampling_rate, + MOUSE_PS2_CMD_SET_SAMPLING_RATE_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set sample rate to %d", sampling_rate); return resp.err; } data->sampling_rate = sampling_rate; + LOG_INF("Successfully set sampling rate to %d", sampling_rate); + return resp.err; } -int zmk_mouse_ps2_get_device_id(uint8_t *device_id) -{ - +int zmk_mouse_ps2_get_device_id(uint8_t *device_id) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_GET_DEVICE_ID, - sizeof(MOUSE_PS2_CMD_GET_DEVICE_ID), - NULL, - 1, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get device id" - ); + MOUSE_PS2_CMD_GET_DEVICE_ID, sizeof(MOUSE_PS2_CMD_GET_DEVICE_ID), NULL, 1, true); + if (resp.err) { + LOG_ERR("Could not get device id"); return resp.err; } @@ -1090,11 +899,10 @@ int zmk_mouse_ps2_get_device_id(uint8_t *device_id) return 0; } -int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) -{ +int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - if(mode == MOUSE_PS2_PACKET_MODE_PS2_DEFAULT) { + if (mode == MOUSE_PS2_PACKET_MODE_PS2_DEFAULT) { // Do nothing. Mouse devices enable this by // default. return 0; @@ -1106,7 +914,7 @@ int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) // Setting a mouse mode is a bit like using a cheat code // in a video game. // You have to send a specific sequence of sampling rates. - if(mode == MOUSE_PS2_PACKET_MODE_SCROLL) { + if (mode == MOUSE_PS2_PACKET_MODE_SCROLL) { zmk_mouse_ps2_set_sampling_rate(200); zmk_mouse_ps2_set_sampling_rate(100); @@ -1125,25 +933,22 @@ int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) uint8_t device_id; int err = zmk_mouse_ps2_get_device_id(&device_id); - if(err) { - LOG_ERR( - "Could not enable packet mode %d. Failed to get device id with " - "error %d", mode, err - ); + if (err) { + LOG_ERR("Could not enable packet mode %d. Failed to get device id with " + "error %d", + mode, err); } else { - if(device_id == 0x00) { - LOG_ERR( - "Could not enable packet mode %d. The device does not " - "support it", mode - ); + if (device_id == 0x00) { + LOG_ERR("Could not enable packet mode %d. The device does not " + "support it", + mode); data->packet_mode = MOUSE_PS2_PACKET_MODE_PS2_DEFAULT; err = 1; - } else if(device_id == 0x03 || device_id == 0x04) { - LOG_INF( - "Successfully activated packet mode %d. Mouse returned " - "device id: %d", mode, device_id - ); + } else if (device_id == 0x03 || device_id == 0x04) { + LOG_INF("Successfully activated packet mode %d. Mouse returned " + "device id: %d", + mode, device_id); data->packet_mode = MOUSE_PS2_PACKET_MODE_SCROLL; err = 0; @@ -1158,10 +963,9 @@ int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) // err = 0; // } else { - LOG_ERR( - "Could not enable packet mode %d. Received an invalid " - "device id: %d", mode, device_id - ); + LOG_ERR("Could not enable packet mode %d. Received an invalid " + "device id: %d", + mode, device_id); data->packet_mode = MOUSE_PS2_PACKET_MODE_PS2_DEFAULT; err = 1; @@ -1171,7 +975,7 @@ int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) // Restore sampling rate to prev value zmk_mouse_ps2_set_sampling_rate(data->sampling_rate); - if(prev_activity_reporting_on == true) { + if (prev_activity_reporting_on == true) { zmk_mouse_ps2_activity_reporting_enable(); } @@ -1182,19 +986,16 @@ int zmk_mouse_ps2_set_packet_mode(zmk_mouse_ps2_packet_mode mode) * Trackpoint Commands */ -bool zmk_mouse_ps2_is_device_trackpoint() -{ +bool zmk_mouse_ps2_is_device_trackpoint() { bool ret = false; uint8_t second_id_1, second_id_2; - int err = zmk_mouse_ps2_get_secondary_id( - &second_id_1, &second_id_2 - ); - if(err) { + int err = zmk_mouse_ps2_get_secondary_id(&second_id_1, &second_id_2); + if (err) { // Not all devices implement this command. ret = false; } else { - if(second_id_1 == 0x1) { + if (second_id_1 == 0x1) { ret = true; } } @@ -1204,19 +1005,12 @@ bool zmk_mouse_ps2_is_device_trackpoint() return ret; } -int zmk_mouse_ps2_tp_get_config_byte(uint8_t *config_byte) -{ +int zmk_mouse_ps2_tp_get_config_byte(uint8_t *config_byte) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE, - sizeof(MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE), - NULL, - MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not read trackpoint config byte" - ); + MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE, sizeof(MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE), NULL, + MOUSE_PS2_CMD_TP_GET_CONFIG_BYTE_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not read trackpoint config byte"); return resp.err; } @@ -1225,113 +1019,72 @@ int zmk_mouse_ps2_tp_get_config_byte(uint8_t *config_byte) return 0; } -int zmk_mouse_ps2_tp_set_config_option(int config_bit, - bool enabled, - char *descr) -{ +int zmk_mouse_ps2_tp_set_config_option(int config_bit, bool enabled, char *descr) { uint8_t config_byte; - int err = zmk_mouse_ps2_tp_get_config_byte( - &config_byte - ); - if(err) { + int err = zmk_mouse_ps2_tp_get_config_byte(&config_byte); + if (err) { return err; } - bool is_enabled = MOUSE_PS2_GET_BIT( - config_byte, - config_bit - ); + bool is_enabled = MOUSE_PS2_GET_BIT(config_byte, config_bit); - if(is_enabled == enabled) { - LOG_DBG( - "Trackpoint %s was already %s... not doing anything.", - descr, is_enabled ? "enabled" : "disabled" - ); + if (is_enabled == enabled) { + LOG_DBG("Trackpoint %s was already %s... not doing anything.", descr, + is_enabled ? "enabled" : "disabled"); return 0; } - LOG_DBG( - "Setting trackpoint %s: %s", - descr, enabled ? "enabled" : "disabled" - ); + LOG_DBG("Setting trackpoint %s: %s", descr, enabled ? "enabled" : "disabled"); MOUSE_PS2_SET_BIT(config_byte, enabled, config_bit); struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE, - sizeof(MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE), - &config_byte, - MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set trackpoint %s to %s", - descr, enabled ? "enabled" : "disabled" - ); + MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE, sizeof(MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE), &config_byte, + MOUSE_PS2_CMD_TP_SET_CONFIG_BYTE_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set trackpoint %s to %s", descr, enabled ? "enabled" : "disabled"); return resp.err; } + LOG_INF("Successfully set config option %s to %s", descr, enabled ? "enabled" : "disabled"); + return 0; } -int zmk_mouse_ps2_tp_press_to_select_set(bool enabled) -{ - int err = zmk_mouse_ps2_tp_set_config_option( - MOUSE_PS2_TP_CONFIG_BIT_PRESS_TO_SELECT, - enabled, - "Press To Select" - ); +int zmk_mouse_ps2_tp_press_to_select_set(bool enabled) { + int err = zmk_mouse_ps2_tp_set_config_option(MOUSE_PS2_TP_CONFIG_BIT_PRESS_TO_SELECT, enabled, + "Press To Select"); return err; } -int zmk_mouse_ps2_tp_invert_x_set(bool enabled) -{ - int err = zmk_mouse_ps2_tp_set_config_option( - MOUSE_PS2_TP_CONFIG_BIT_INVERT_X, - enabled, - "Invert X" - ); +int zmk_mouse_ps2_tp_invert_x_set(bool enabled) { + int err = + zmk_mouse_ps2_tp_set_config_option(MOUSE_PS2_TP_CONFIG_BIT_INVERT_X, enabled, "Invert X"); return err; } -int zmk_mouse_ps2_tp_invert_y_set(bool enabled) -{ - int err = zmk_mouse_ps2_tp_set_config_option( - MOUSE_PS2_TP_CONFIG_BIT_INVERT_Y, - enabled, - "Invert Y" - ); +int zmk_mouse_ps2_tp_invert_y_set(bool enabled) { + int err = + zmk_mouse_ps2_tp_set_config_option(MOUSE_PS2_TP_CONFIG_BIT_INVERT_Y, enabled, "Invert Y"); return err; } -int zmk_mouse_ps2_tp_swap_xy_set(bool enabled) -{ - int err = zmk_mouse_ps2_tp_set_config_option( - MOUSE_PS2_TP_CONFIG_BIT_SWAP_XY, - enabled, - "Swap XY" - ); +int zmk_mouse_ps2_tp_swap_xy_set(bool enabled) { + int err = + zmk_mouse_ps2_tp_set_config_option(MOUSE_PS2_TP_CONFIG_BIT_SWAP_XY, enabled, "Swap XY"); return err; } -int zmk_mouse_ps2_tp_sensitivity_get(uint8_t *sensitivity) -{ +int zmk_mouse_ps2_tp_sensitivity_get(uint8_t *sensitivity) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_GET_SENSITIVITY, - sizeof(MOUSE_PS2_CMD_TP_GET_SENSITIVITY), - NULL, - MOUSE_PS2_CMD_TP_GET_SENSITIVITY_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get trackpoint sensitivity" - ); + MOUSE_PS2_CMD_TP_GET_SENSITIVITY, sizeof(MOUSE_PS2_CMD_TP_GET_SENSITIVITY), NULL, + MOUSE_PS2_CMD_TP_GET_SENSITIVITY_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not get trackpoint sensitivity"); return resp.err; } @@ -1345,52 +1098,41 @@ int zmk_mouse_ps2_tp_sensitivity_get(uint8_t *sensitivity) return 0; } -int zmk_mouse_ps2_tp_sensitivity_set(int sensitivity) -{ +int zmk_mouse_ps2_tp_sensitivity_set(int sensitivity) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - if(sensitivity < MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MIN || - sensitivity > MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MAX) - { - LOG_ERR( - "Invalid sensitivity value %d. Min: %d; Max: %d", - sensitivity, - MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MIN, - MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MAX - ); + if (sensitivity < MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MIN || + sensitivity > MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MAX) { + LOG_ERR("Invalid sensitivity value %d. Min: %d; Max: %d", sensitivity, + MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MIN, MOUSE_PS2_CMD_TP_SET_SENSITIVITY_MAX); return 1; } uint8_t arg = sensitivity; struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_SET_SENSITIVITY, - sizeof(MOUSE_PS2_CMD_TP_SET_SENSITIVITY), - &arg, - MOUSE_PS2_CMD_TP_SET_SENSITIVITY_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set sensitivity to %d", sensitivity - ); + MOUSE_PS2_CMD_TP_SET_SENSITIVITY, sizeof(MOUSE_PS2_CMD_TP_SET_SENSITIVITY), &arg, + MOUSE_PS2_CMD_TP_SET_SENSITIVITY_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set sensitivity to %d", sensitivity); return resp.err; } data->tp_sensitivity = sensitivity; + LOG_INF("Successfully set TP sensitivity to %d", sensitivity); + return 0; } -int zmk_mouse_ps2_tp_sensitivity_change(int amount) -{ +int zmk_mouse_ps2_tp_sensitivity_change(int amount) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; int new_val = data->tp_sensitivity + amount; LOG_INF("Setting trackpoint sensitivity to %d", new_val); int err = zmk_mouse_ps2_tp_sensitivity_set(new_val); - if(err == 0) { + if (err == 0) { zmk_mouse_ps2_settings_save(); } @@ -1398,19 +1140,12 @@ int zmk_mouse_ps2_tp_sensitivity_change(int amount) return err; } -int zmk_mouse_ps2_tp_negative_inertia_get(uint8_t *neg_inertia) -{ +int zmk_mouse_ps2_tp_negative_inertia_get(uint8_t *neg_inertia) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_GET_NEG_INERTIA, - sizeof(MOUSE_PS2_CMD_TP_GET_NEG_INERTIA), - NULL, - MOUSE_PS2_CMD_TP_GET_NEG_INERTIA_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get trackpoint negative inertia" - ); + MOUSE_PS2_CMD_TP_GET_NEG_INERTIA, sizeof(MOUSE_PS2_CMD_TP_GET_NEG_INERTIA), NULL, + MOUSE_PS2_CMD_TP_GET_NEG_INERTIA_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not get trackpoint negative inertia"); return resp.err; } @@ -1422,53 +1157,41 @@ int zmk_mouse_ps2_tp_negative_inertia_get(uint8_t *neg_inertia) return 0; } -int zmk_mouse_ps2_tp_neg_inertia_set(int neg_inertia) -{ +int zmk_mouse_ps2_tp_neg_inertia_set(int neg_inertia) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - if(neg_inertia < MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MIN || - neg_inertia > MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MAX) - { - LOG_ERR( - "Invalid negative inertia value %d. Min: %d; Max: %d", - neg_inertia, - MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MIN, - MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MAX - ); + if (neg_inertia < MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MIN || + neg_inertia > MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MAX) { + LOG_ERR("Invalid negative inertia value %d. Min: %d; Max: %d", neg_inertia, + MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MIN, MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_MAX); return 1; } uint8_t arg = neg_inertia; struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_SET_NEG_INERTIA, - sizeof(MOUSE_PS2_CMD_TP_SET_NEG_INERTIA), - &arg, - MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set negative inertia to %d", - neg_inertia - ); + MOUSE_PS2_CMD_TP_SET_NEG_INERTIA, sizeof(MOUSE_PS2_CMD_TP_SET_NEG_INERTIA), &arg, + MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set negative inertia to %d", neg_inertia); return resp.err; } data->tp_neg_inertia = neg_inertia; + LOG_INF("Successfully set TP negative inertia to %d", neg_inertia); + return 0; } -int zmk_mouse_ps2_tp_neg_inertia_change(int amount) -{ +int zmk_mouse_ps2_tp_neg_inertia_change(int amount) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; int new_val = data->tp_neg_inertia + amount; LOG_INF("Setting negative inertia to %d", new_val); int err = zmk_mouse_ps2_tp_neg_inertia_set(new_val); - if(err == 0) { + if (err == 0) { zmk_mouse_ps2_settings_save(); } @@ -1476,19 +1199,13 @@ int zmk_mouse_ps2_tp_neg_inertia_change(int amount) return err; } -int zmk_mouse_ps2_tp_value6_upper_plateau_speed_get(uint8_t *value6) -{ - struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED, - sizeof(MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED), - NULL, - MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get trackpoint value6 upper plateau speed" - ); +int zmk_mouse_ps2_tp_value6_upper_plateau_speed_get(uint8_t *value6) { + struct zmk_mouse_ps2_send_cmd_resp resp = + zmk_mouse_ps2_send_cmd(MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED, + sizeof(MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED), NULL, + MOUSE_PS2_CMD_TP_GET_VALUE6_UPPER_PLATEAU_SPEED_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not get trackpoint value6 upper plateau speed"); return resp.err; } @@ -1500,53 +1217,43 @@ int zmk_mouse_ps2_tp_value6_upper_plateau_speed_get(uint8_t *value6) return 0; } -int zmk_mouse_ps2_tp_value6_upper_plateau_speed_set(int value6) -{ +int zmk_mouse_ps2_tp_value6_upper_plateau_speed_set(int value6) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - if(value6 < MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MIN || - value6 > MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MAX) - { - LOG_ERR( - "Invalid value6 upper plateau speed value %d. Min: %d; Max: %d", - value6, - MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MIN, - MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MAX - ); + if (value6 < MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MIN || + value6 > MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MAX) { + LOG_ERR("Invalid value6 upper plateau speed value %d. Min: %d; Max: %d", value6, + MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MIN, + MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_MAX); return 1; } uint8_t arg = value6; - struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED, - sizeof(MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED), - &arg, - MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set value6 upper plateau speed to %d", - value6 - ); + struct zmk_mouse_ps2_send_cmd_resp resp = + zmk_mouse_ps2_send_cmd(MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED, + sizeof(MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED), &arg, + MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set value6 upper plateau speed to %d", value6); return resp.err; } data->tp_value6 = value6; + LOG_INF("Successfully set TP value6 upper plateau speed to %d", value6); + return 0; } -int zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(int amount) -{ +int zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(int amount) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; int new_val = data->tp_value6 + amount; LOG_INF("Setting value6 upper plateau speed to %d", new_val); int err = zmk_mouse_ps2_tp_value6_upper_plateau_speed_set(new_val); - if(err == 0) { + if (err == 0) { zmk_mouse_ps2_settings_save(); } @@ -1554,79 +1261,58 @@ int zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(int amount) return err; } -int zmk_mouse_ps2_tp_pts_threshold_get(uint8_t *pts_threshold) -{ +int zmk_mouse_ps2_tp_pts_threshold_get(uint8_t *pts_threshold) { struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD, - sizeof(MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD), - NULL, - MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not get trackpoint press-to-select threshold" - ); + MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD, sizeof(MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD), NULL, + MOUSE_PS2_CMD_TP_GET_PTS_THRESHOLD_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not get trackpoint press-to-select threshold"); return resp.err; } uint8_t pts_threshold_int = resp.resp_buffer[0]; *pts_threshold = pts_threshold_int; - LOG_DBG( - "Trackpoint press-to-select threshold is %d", pts_threshold_int - ); + LOG_DBG("Trackpoint press-to-select threshold is %d", pts_threshold_int); return 0; } -int zmk_mouse_ps2_tp_pts_threshold_set(int pts_threshold) -{ +int zmk_mouse_ps2_tp_pts_threshold_set(int pts_threshold) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - if(pts_threshold < MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MIN || - pts_threshold > MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MAX) - { - LOG_ERR( - "Invalid press-to-select threshold value %d. Min: %d; Max: %d", - pts_threshold, - MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MIN, - MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MAX - ); + if (pts_threshold < MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MIN || + pts_threshold > MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MAX) { + LOG_ERR("Invalid press-to-select threshold value %d. Min: %d; Max: %d", pts_threshold, + MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MIN, MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_MAX); return 1; } uint8_t arg = pts_threshold; struct zmk_mouse_ps2_send_cmd_resp resp = zmk_mouse_ps2_send_cmd( - MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD, - sizeof(MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD), - &arg, - MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_RESP_LEN, - true - ); - if(resp.err) { - LOG_ERR( - "Could not set press-to-select threshold to %d", - pts_threshold - ); + MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD, sizeof(MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD), &arg, + MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_RESP_LEN, true); + if (resp.err) { + LOG_ERR("Could not set press-to-select threshold to %d", pts_threshold); return resp.err; } data->tp_pts_threshold = pts_threshold; + LOG_INF("Successfully set TP press-to-select threshold to %d", pts_threshold); + return 0; } -int zmk_mouse_ps2_tp_pts_threshold_change(int amount) -{ +int zmk_mouse_ps2_tp_pts_threshold_change(int amount) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; int new_val = data->tp_pts_threshold + amount; LOG_INF("Setting press-to-select threshold to %d", new_val); int err = zmk_mouse_ps2_tp_pts_threshold_set(new_val); - if(err == 0) { + if (err == 0) { zmk_mouse_ps2_settings_save(); } @@ -1634,7 +1320,6 @@ int zmk_mouse_ps2_tp_pts_threshold_change(int amount) return err; } - /* * State Saving */ @@ -1643,54 +1328,45 @@ int zmk_mouse_ps2_tp_pts_threshold_change(int amount) struct k_work_delayable zmk_mouse_ps2_save_work; -int zmk_mouse_ps2_settings_save_setting(char *setting_name, - const void *value, - size_t val_len) -{ +int zmk_mouse_ps2_settings_save_setting(char *setting_name, const void *value, size_t val_len) { char setting_path[40]; - snprintf( - setting_path, - sizeof(setting_path), - "%s/%s", - MOUSE_PS2_SETTINGS_SUBTREE, - setting_name - ); + snprintf(setting_path, sizeof(setting_path), "%s/%s", MOUSE_PS2_SETTINGS_SUBTREE, setting_name); LOG_DBG("Saving setting to `%s`", setting_path); int err = settings_save_one(setting_path, value, val_len); - if(err) { + if (err) { LOG_ERR("Could not save setting to `%s`: %d", setting_path, err); } return err; } -static void zmk_mouse_ps2_settings_save_work(struct k_work *work) -{ +int zmk_mouse_ps2_settings_reset_setting(char *setting_name) { + char setting_path[40]; + snprintf(setting_path, sizeof(setting_path), "%s/%s", MOUSE_PS2_SETTINGS_SUBTREE, setting_name); + + LOG_DBG("Reseting setting `%s`", setting_path); + int err = settings_delete(setting_path); + if (err) { + LOG_ERR("Could not reset setting `%s`", setting_path); + } + + return err; +} + +static void zmk_mouse_ps2_settings_save_work(struct k_work *work) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; LOG_DBG(""); - zmk_mouse_ps2_settings_save_setting( - MOUSE_PS2_ST_TP_SENSITIVITY, - &data->tp_sensitivity, - sizeof(data->tp_sensitivity) - ); - zmk_mouse_ps2_settings_save_setting( - MOUSE_PS2_ST_TP_NEG_INERTIA, - &data->tp_neg_inertia, - sizeof(data->tp_neg_inertia) - ); - zmk_mouse_ps2_settings_save_setting( - MOUSE_PS2_ST_TP_VALUE6, - &data->tp_value6, - sizeof(data->tp_value6) - ); - zmk_mouse_ps2_settings_save_setting( - MOUSE_PS2_ST_TP_PTS_THRESHOLD, - &data->tp_pts_threshold, - sizeof(data->tp_pts_threshold) - ); + zmk_mouse_ps2_settings_save_setting(MOUSE_PS2_ST_TP_SENSITIVITY, &data->tp_sensitivity, + sizeof(data->tp_sensitivity)); + zmk_mouse_ps2_settings_save_setting(MOUSE_PS2_ST_TP_NEG_INERTIA, &data->tp_neg_inertia, + sizeof(data->tp_neg_inertia)); + zmk_mouse_ps2_settings_save_setting(MOUSE_PS2_ST_TP_VALUE6, &data->tp_value6, + sizeof(data->tp_value6)); + zmk_mouse_ps2_settings_save_setting(MOUSE_PS2_ST_TP_PTS_THRESHOLD, &data->tp_pts_threshold, + sizeof(data->tp_pts_threshold)); } #endif @@ -1698,32 +1374,66 @@ int zmk_mouse_ps2_settings_save() { LOG_DBG(""); #if IS_ENABLED(CONFIG_SETTINGS) - int ret = k_work_reschedule( - &zmk_mouse_ps2_save_work, - K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE) - ); + int ret = + k_work_reschedule(&zmk_mouse_ps2_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE)); return MIN(ret, 0); #else return 0; #endif } +int zmk_mouse_ps2_settings_reset() { + + LOG_INF("Deleting runtime settings..."); + zmk_mouse_ps2_settings_reset_setting(MOUSE_PS2_ST_TP_SENSITIVITY); + zmk_mouse_ps2_settings_reset_setting(MOUSE_PS2_ST_TP_NEG_INERTIA); + zmk_mouse_ps2_settings_reset_setting(MOUSE_PS2_ST_TP_VALUE6); + zmk_mouse_ps2_settings_reset_setting(MOUSE_PS2_ST_TP_PTS_THRESHOLD); + + LOG_INF("Restoring default settings to TP.."); + zmk_mouse_ps2_tp_sensitivity_set(MOUSE_PS2_CMD_TP_SET_SENSITIVITY_DEFAULT); + + zmk_mouse_ps2_tp_neg_inertia_set(MOUSE_PS2_CMD_TP_SET_NEG_INERTIA_DEFAULT); + + zmk_mouse_ps2_tp_value6_upper_plateau_speed_set( + MOUSE_PS2_CMD_TP_SET_VALUE6_UPPER_PLATEAU_SPEED_DEFAULT); + + zmk_mouse_ps2_tp_pts_threshold_set(MOUSE_PS2_CMD_TP_SET_PTS_THRESHOLD_DEFAULT); + + return 0; +} + +int zmk_mouse_ps2_settings_log() { + struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; + + char settings_str[250]; + + snprintf(settings_str, sizeof(settings_str), " \n\ +&mouse_ps2_conf = { \n\ + tp-sensitivity = <%d>; \n\ + tp-neg-inertia = <%d>; \n\ + tp-val6-upper-speed = <%d>; \n\ + tp-tp-press-to-select-threshold = <%d>; \n\ +}", + data->tp_sensitivity, data->tp_neg_inertia, data->tp_value6, data->tp_pts_threshold); + + LOG_INF("Current settings... %s", settings_str); + + return 0; +} + // This function is called when settings are loaded from flash by // `settings_load_subtree`. // It's called once for each PS/2 mouse setting that has been stored. -static int zmk_mouse_ps2_settings_restore(const char *name, - size_t len, - settings_read_cb read_cb, - void *cb_arg) -{ +static int zmk_mouse_ps2_settings_restore(const char *name, size_t len, settings_read_cb read_cb, + void *cb_arg) { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; + const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; + uint8_t setting_val; if (len != sizeof(setting_val)) { - LOG_ERR( - "Could not restore settings %s: Len mismatch", - name - ); + LOG_ERR("Could not restore settings %s: Len mismatch", name); return -EINVAL; } @@ -1734,27 +1444,53 @@ static int zmk_mouse_ps2_settings_restore(const char *name, return -EINVAL; } - if(data->is_trackpoint == false) { - LOG_INF( - "Mouse device is not a trackpoint. Not restoring setting %s.", - name - ); + if (data->is_trackpoint == false) { + LOG_INF("Mouse device is not a trackpoint. Not restoring setting %s.", name); return 0; } LOG_INF("Restoring setting %s with value: %d", name, setting_val); - if(strcmp(name, MOUSE_PS2_ST_TP_SENSITIVITY) == 0) { + if (strcmp(name, MOUSE_PS2_ST_TP_SENSITIVITY) == 0) { + + if (config->tp_sensitivity != -1) { + LOG_WRN("Not restoring runtime settings for %s with value %d, because deviceconfig " + "defines the setting with value %d", + name, setting_val, config->tp_sensitivity); + + return 0; + } return zmk_mouse_ps2_tp_sensitivity_set(setting_val); - } else if(strcmp(name, MOUSE_PS2_ST_TP_NEG_INERTIA) == 0) { + } else if (strcmp(name, MOUSE_PS2_ST_TP_NEG_INERTIA) == 0) { + if (config->tp_neg_inertia != -1) { + LOG_WRN("Not restoring runtime settings for %s with value %d, because deviceconfig " + "defines the setting with value %d", + name, setting_val, config->tp_neg_inertia); + + return 0; + } return zmk_mouse_ps2_tp_neg_inertia_set(setting_val); - } else if(strcmp(name, MOUSE_PS2_ST_TP_VALUE6) == 0) { + } else if (strcmp(name, MOUSE_PS2_ST_TP_VALUE6) == 0) { + if (config->tp_val6_upper_speed != -1) { + LOG_WRN("Not restoring runtime settings for %s with value %d, because deviceconfig " + "defines the setting with value %d", + name, setting_val, config->tp_val6_upper_speed); + + return 0; + } return zmk_mouse_ps2_tp_value6_upper_plateau_speed_set(setting_val); - } else if(strcmp(name, MOUSE_PS2_ST_TP_PTS_THRESHOLD) == 0) { + } else if (strcmp(name, MOUSE_PS2_ST_TP_PTS_THRESHOLD) == 0) { + if (config->tp_press_to_select_threshold != -1) { + LOG_WRN("Not restoring runtime settings for %s with value %d, because deviceconfig " + "defines the setting with value %d", + name, setting_val, config->tp_press_to_select_threshold); + + return 0; + } return zmk_mouse_ps2_tp_pts_threshold_set(setting_val); } @@ -1762,7 +1498,6 @@ static int zmk_mouse_ps2_settings_restore(const char *name, return -EINVAL; } - struct settings_handler zmk_mouse_ps2_settings_conf = { .name = MOUSE_PS2_SETTINGS_SUBTREE, .h_set = zmk_mouse_ps2_settings_restore, @@ -1776,17 +1511,11 @@ int zmk_mouse_ps2_settings_init() { int err = settings_register(&zmk_mouse_ps2_settings_conf); if (err) { - LOG_ERR( - "Failed to register the PS/2 mouse settings handler (err %d)", - err - ); + LOG_ERR("Failed to register the PS/2 mouse settings handler (err %d)", err); return err; } - k_work_init_delayable( - &zmk_mouse_ps2_save_work, - zmk_mouse_ps2_settings_save_work - ); + k_work_init_delayable(&zmk_mouse_ps2_save_work, zmk_mouse_ps2_settings_save_work); // This will load the settings and then call // `zmk_mouse_ps2_settings_restore`, which will set the settings @@ -1804,122 +1533,119 @@ static void zmk_mouse_ps2_init_thread(int dev_ptr, int unused); int zmk_mouse_ps2_init_power_on_reset(); int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev); -static int zmk_mouse_ps2_init(const struct device *dev) -{ - LOG_DBG("Inside zmk_mouse_ps2_init"); - - LOG_DBG("Creating mouse_ps2 init thread."); - k_thread_create( - &zmk_mouse_ps2_data.thread, - zmk_mouse_ps2_data.thread_stack, - MOUSE_PS2_THREAD_STACK_SIZE, - (k_thread_entry_t)zmk_mouse_ps2_init_thread, - (struct device *)dev, 0, NULL, - K_PRIO_COOP(MOUSE_PS2_THREAD_PRIORITY), 0, K_NO_WAIT - ); - - return 0; +static int zmk_mouse_ps2_init(const struct device *dev) { + LOG_DBG("Inside zmk_mouse_ps2_init"); + + LOG_DBG("Creating mouse_ps2 init thread."); + k_thread_create(&zmk_mouse_ps2_data.thread, zmk_mouse_ps2_data.thread_stack, + MOUSE_PS2_THREAD_STACK_SIZE, (k_thread_entry_t)zmk_mouse_ps2_init_thread, + (struct device *)dev, 0, NULL, K_PRIO_COOP(MOUSE_PS2_THREAD_PRIORITY), 0, + K_MSEC(ZMK_MOUSE_PS2_INIT_THREAD_DELAY_MS)); + + return 0; } static void zmk_mouse_ps2_init_thread(int dev_ptr, int unused) { - const struct device *dev = INT_TO_POINTER(dev_ptr); struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - const struct zmk_mouse_ps2_config *config = dev->config; int err; + data->dev = INT_TO_POINTER(dev_ptr); + + const struct zmk_mouse_ps2_config *config = data->dev->config; + zmk_mouse_ps2_init_power_on_reset(); - LOG_INF("Waiting for mouse to connect..."); - err = zmk_mouse_ps2_init_wait_for_mouse(dev); - if(err) { - LOG_ERR( - "Could not init a mouse in %d attempts. Giving up. " - "Power cycle the mouse and reset zmk to try again.", - MOUSE_PS2_INIT_ATTEMPTS - ); + LOG_INF("Waiting for mouse to connect..."); + err = zmk_mouse_ps2_init_wait_for_mouse(data->dev); + if (err) { + LOG_ERR("Could not init a mouse in %d attempts. Giving up. " + "Power cycle the mouse and reset zmk to try again.", + MOUSE_PS2_INIT_ATTEMPTS); return; } - #if CONFIG_ZMK_MOUSE_PS2_SAMPLING_RATE != \ - MOUSE_PS2_CMD_SET_SAMPLING_RATE_DEFAULT - LOG_INF( - "Setting sample rate to %d...", - CONFIG_ZMK_MOUSE_PS2_SAMPLING_RATE - ); - zmk_mouse_ps2_set_sampling_rate(CONFIG_ZMK_MOUSE_PS2_SAMPLING_RATE); - if(err) { - LOG_ERR( - "Could not set sampling rate to %d: %d", - CONFIG_ZMK_MOUSE_PS2_SAMPLING_RATE, - err - ); + if (config->sampling_rate != MOUSE_PS2_CMD_SET_SAMPLING_RATE_DEFAULT) { + + LOG_INF("Setting sample rate to %d...", config->sampling_rate); + zmk_mouse_ps2_set_sampling_rate(config->sampling_rate); + if (err) { + LOG_ERR("Could not set sampling rate to %d: %d", config->sampling_rate, err); return; } - #endif /* CONFIG_ZMK_MOUSE_PS2_SAMPLING_RATE */ + } - if(zmk_mouse_ps2_is_device_trackpoint() == true) { + if (zmk_mouse_ps2_is_device_trackpoint() == true) { LOG_INF("Device is a trackpoint"); data->is_trackpoint = true; - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_TAP_TO_SELECT) - LOG_INF("Enabling trackpoint press to select..."); + if (config->tp_press_to_select) { + LOG_INF("Enabling TP press to select..."); zmk_mouse_ps2_tp_press_to_select_set(true); - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_TAP_TO_SELECT) */ + } + + if (config->tp_press_to_select_threshold != -1) { + LOG_INF("Setting TP press to select thereshold to %d...", + config->tp_press_to_select_threshold); + zmk_mouse_ps2_tp_pts_threshold_set(config->tp_press_to_select_threshold); + } + + if (config->tp_sensitivity != -1) { + LOG_INF("Setting TP sensitivity to %d...", config->tp_sensitivity); + zmk_mouse_ps2_tp_sensitivity_set(config->tp_sensitivity); + } + + if (config->tp_neg_inertia != -1) { + LOG_INF("Setting TP inertia to %d...", config->tp_neg_inertia); + zmk_mouse_ps2_tp_neg_inertia_set(config->tp_neg_inertia); + } - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_INVERT_X) + if (config->tp_val6_upper_speed != -1) { + LOG_INF("Setting TP value 6 upper speed plateau to %d...", config->tp_val6_upper_speed); + zmk_mouse_ps2_tp_value6_upper_plateau_speed_set(config->tp_val6_upper_speed); + } + if (config->tp_x_invert) { LOG_INF("Inverting trackpoint x axis."); zmk_mouse_ps2_tp_invert_x_set(true); - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_INVERT_X) */ + } - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_INVERT_Y) + if (config->tp_y_invert) { LOG_INF("Inverting trackpoint y axis."); zmk_mouse_ps2_tp_invert_y_set(true); - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_INVERT_Y) */ + } - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_SWAP_XY) + if (config->tp_xy_swap) { LOG_INF("Swapping trackpoint x and y axis."); zmk_mouse_ps2_tp_swap_xy_set(true); - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_TP_SWAP_XY) */ - + } } - #if IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_SCROLL) + if (config->scroll_mode) { LOG_INF("Enabling scroll mode."); zmk_mouse_ps2_set_packet_mode(MOUSE_PS2_PACKET_MODE_SCROLL); - #endif /* IS_ENABLED(CONFIG_ZMK_MOUSE_PS2_SCROLL) */ + } zmk_mouse_ps2_settings_init(); // Configure read callback - LOG_DBG("Configuring ps2 callback..."); - err = ps2_config( - config->ps2_device, - &zmk_mouse_ps2_activity_callback, - &zmk_mouse_ps2_activity_resend_callback - ); - if(err) { + LOG_DBG("Configuring ps2 callback..."); + err = ps2_config(config->ps2_device, &zmk_mouse_ps2_activity_callback, + &zmk_mouse_ps2_activity_resend_callback); + if (err) { LOG_ERR("Could not configure ps2 interface: %d", err); - return ; + return; } - LOG_INF("Enabling data reporting and ps2 callback..."); + LOG_INF("Enabling data reporting and ps2 callback..."); err = zmk_mouse_ps2_activity_reporting_enable(); - if(err) { + if (err) { LOG_ERR("Could not activate ps2 callback: %d", err); } else { LOG_DBG("Successfully activated ps2 callback"); } - k_timer_init(&data->mouse_timer, zmk_mouse_ps2_tick_timer_cb, NULL); - k_timer_start( - &data->mouse_timer, K_NO_WAIT, K_MSEC(CONFIG_ZMK_MOUSE_TICK_DURATION) - ); - k_work_init(&data->mouse_tick, zmk_mouse_ps2_tick_timer_handler); - k_work_init_delayable( - &data->packet_buffer_timeout, zmk_mouse_ps2_activity_packet_timout - ); + k_work_init_delayable(&data->packet_buffer_timeout, zmk_mouse_ps2_activity_packet_timout); - return; + return; } // Power-On-Reset for trackpoints (and possibly other devices). @@ -1930,19 +1656,18 @@ static void zmk_mouse_ps2_init_thread(int dev_ptr, int unused) { // the time power is applied to the TrackPoint controller. Activity on the // clock and data lines is ignored prior to the completion of the diagnostic // sequence. (See RESET mode of operation.)" -int zmk_mouse_ps2_init_power_on_reset() -{ +int zmk_mouse_ps2_init_power_on_reset() { struct zmk_mouse_ps2_data *data = &zmk_mouse_ps2_data; - const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; + const struct zmk_mouse_ps2_config *config = &zmk_mouse_ps2_config; // Check if the optional rst-gpios setting was set - if(config->rst_gpio.port == NULL) { + if (config->rst_gpio.port == NULL) { return 0; } LOG_INF("Performing Power-On-Reset..."); - if(data->rst_gpio.port == NULL) { + if (data->rst_gpio.port == NULL) { data->rst_gpio = config->rst_gpio; // Overwrite any user-provided flags from the devicetree @@ -1950,56 +1675,52 @@ int zmk_mouse_ps2_init_power_on_reset() } // Set reset pin low... - int err = gpio_pin_configure_dt( - &data->rst_gpio, - (GPIO_OUTPUT_HIGH) - ); - if (err) { - LOG_ERR( - "Failed Power-On-Reset: Failed to configure RST GPIO pin to " - "output low (err %d)", err - ); + int err = gpio_pin_configure_dt(&data->rst_gpio, (GPIO_OUTPUT_HIGH)); + if (err) { + LOG_ERR("Failed Power-On-Reset: Failed to configure RST GPIO pin to " + "output low (err %d)", + err); return err; - } + } // Wait 600ms k_sleep(MOUSE_PS2_POWER_ON_RESET_TIME); // Set pin high err = gpio_pin_set_dt(&data->rst_gpio, 0); - if (err) { - LOG_ERR( - "Failed Power-On-Reset: Failed to set RST GPIO pin to " - "low (err %d)", err - ); + if (err) { + LOG_ERR("Failed Power-On-Reset: Failed to set RST GPIO pin to " + "low (err %d)", + err); return err; - } + } LOG_DBG("Finished Power-On-Reset successfully..."); return 0; } -int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev) -{ - const struct zmk_mouse_ps2_config *config = dev->config; +int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev) { + const struct zmk_mouse_ps2_config *config = dev->config; int err; uint8_t read_val; - for(int i = 0; i < MOUSE_PS2_INIT_ATTEMPTS; i++) { + for (int i = 0; i < MOUSE_PS2_INIT_ATTEMPTS; i++) { - LOG_INF( - "Trying to initialize mouse device (attempt %d / %d)", - i+1, MOUSE_PS2_INIT_ATTEMPTS - ); + LOG_INF("Trying to initialize mouse device (attempt %d / %d)", i + 1, + MOUSE_PS2_INIT_ATTEMPTS); // PS/2 Devices do a self-test and send the result when they power up. err = ps2_read(config->ps2_device, &read_val); - if(err == 0) { - if(read_val != MOUSE_PS2_RESP_SELF_TEST_PASS) { + if (err == 0) { + if (read_val != MOUSE_PS2_RESP_SELF_TEST_PASS) { LOG_WRN("Got invalid PS/2 self-test result: 0x%x", read_val); + + LOG_INF("Trying to reset PS2 device..."); + zmk_mouse_ps2_reset(config->ps2_device); + continue; } @@ -2008,10 +1729,10 @@ int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev) // Read device id LOG_INF("Reading PS/2 device id..."); err = ps2_read(config->ps2_device, &read_val); - if(err) { + if (err) { LOG_WRN("Could not read PS/2 device id: %d", err); } else { - if(read_val == 0) { + if (read_val == 0) { LOG_INF("Connected PS/2 device is a mouse..."); return 0; } else { @@ -2020,15 +1741,13 @@ int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev) } } } else { - LOG_DBG( - "Could not read PS/2 device self-test result: %d. ", err - ); + LOG_WRN("Could not read PS/2 device self-test result: %d. ", err); } // But when a zmk device is reset, it doesn't cut the power to external // devices. So the device acts as if it was never disconnected. // So we try sending the reset command. - if(i % 2 == 0) { + if (i % 2 == 0) { LOG_INF("Trying to reset PS2 device..."); zmk_mouse_ps2_reset(config->ps2_device); continue; @@ -2040,12 +1759,8 @@ int zmk_mouse_ps2_init_wait_for_mouse(const struct device *dev) return 1; } +// Depends on the UART and PS2 init priorities, which are 55 and 45 by default +#define ZMK_MOUSE_PS2_INIT_PRIORITY 90 -DEVICE_DT_INST_DEFINE( - 0, - &zmk_mouse_ps2_init, - NULL, - &zmk_mouse_ps2_data, &zmk_mouse_ps2_config, - POST_KERNEL, 41, - NULL -); +DEVICE_DT_INST_DEFINE(0, &zmk_mouse_ps2_init, NULL, &zmk_mouse_ps2_data, &zmk_mouse_ps2_config, + POST_KERNEL, ZMK_MOUSE_PS2_INIT_PRIORITY, NULL); diff --git a/app/module/dts/bindings/input/zmk,input-mouse-ps2.yaml b/app/module/dts/bindings/input/zmk,input-mouse-ps2.yaml new file mode 100644 index 00000000000..4f07cd46c83 --- /dev/null +++ b/app/module/dts/bindings/input/zmk,input-mouse-ps2.yaml @@ -0,0 +1,72 @@ +description: PS2 mouse configuration + +compatible: "zmk,input-mouse-ps2" + +properties: + ps2-device: + type: phandle + required: true + description: | + The ps2 device the mouse should use. + + rst-gpios: + type: phandle-array + required: false + description: GPIO to which the RST pin of the device is connected and on which the Power-On-Reset will be performed. + + sampling-rate: + type: int + description: Adjusts the sensitivity setting on trackpoint devices. The allowed values are 10, 20, 40, 60, 80, 100, 200 and the default is 100. + enum: + - 10 + - 20 + - 40 + - 60 + - 80 + - 100 + - 200 + + scroll-mode: + type: boolean + description: Enables scroll wheel on mouse devices supporting the Intellimouse extension. + + disable-clicking: + type: boolean + description: Disables clicking. Useful when using a PS2 driver that is prone to erros in transmissions to reduce accidental clicks. + + tp-press-to-select: + type: boolean + description: Enables mouse clicking by pressing hard on the trackpoint. Not all trackpoints support this feature. + + tp-press-to-select-threshold: + type: int + description: Adjusts how sensitive the press-to-select feature is. The range is 0 - 255 and the default is 8. You can also adjust this setting at runtime without recompiling using the &mms behavior. If this setting is enabled, then the runtime behavior won't be saved into the runtime settings. + + tp-sensitivity: + type: int + description: Adjusts the sensitivity setting on trackpoint devices. The range is 0 - 255 and the default is 128. You can also adjust this setting at runtime without recompiling using the &mms behavior. If this setting is enabled, then the runtime behavior won't be saved into the runtime settings. + + tp-neg-inertia: + type: int + description: Adjusts the negative inertia setting on trackpoint devices. The range is 0 - 255 and the default is 6. You can also adjust this setting at runtime without recompiling using the &mms behavior. If this setting is enabled, then the runtime behavior won't be saved into the runtime settings. + + tp-val6-upper-speed: + type: int + description: Adjusts the negative value0 upper plateau speed setting on trackpoint devices. The range is 0 - 255 and the default is 97. You can also adjust this setting at runtime without recompiling using the &mms behavior. If this setting is enabled, then the runtime behavior won't be saved into the runtime settings. + + tp-x-invert: + type: boolean + description: Enables x axis inversion on the trackpoint. Only works with some trackpoint devices, but is properly considered by the trackpoint's acceleration features (which may or may not have an impact). Alternatively consider achieving the same effect using the `zmk,input-configs` feature. + + tp-y-invert: + type: boolean + description: Enables y axis inversion on the trackpoint. Only works with some trackpoint devices, but is properly considered by the trackpoint's acceleration features (which may or may not have an impact). Alternatively consider achieving the same effect using the `zmk,input-configs` feature. + + tp-xy-swap: + type: boolean + description: Swaps the x and y axis on the trackpoint. Only works with some trackpoint devices, but may properly considered by the trackpoint's acceleration features (which may or may not have an impact). Alternatively consider achieving the same effect using the `zmk,input-configs` feature. + + layer-toggle: + type: int + required: false + description: The layer that should be toggled when the mouse is moved. diff --git a/app/include/zmk/mouse_ps2.h b/app/module/include/zmk/input_mouse_ps2.h similarity index 82% rename from app/include/zmk/mouse_ps2.h rename to app/module/include/zmk/input_mouse_ps2.h index e641f28b1af..36e1a85c7ba 100644 --- a/app/include/zmk/mouse_ps2.h +++ b/app/module/include/zmk/input_mouse_ps2.h @@ -6,6 +6,9 @@ #pragma once +int zmk_mouse_ps2_settings_log(); +int zmk_mouse_ps2_settings_reset(); + int zmk_mouse_ps2_tp_sensitivity_change(int amount); int zmk_mouse_ps2_tp_neg_inertia_change(int amount); int zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(int amount); diff --git a/app/scripts/gen_interrupt_priority_overrides.py b/app/scripts/gen_interrupt_priority_overrides.py new file mode 100755 index 00000000000..6271e77a488 --- /dev/null +++ b/app/scripts/gen_interrupt_priority_overrides.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +""" +Script for generating interrupt adjustments for zephyr dts files. + +Requirements: + - pip install devicetree +""" + +import argparse +from devicetree import dtlib + + +def get_interrupt_nodes(dt): + interrupt_nodes = [] + for node in dt.node_iter(): + if 'interrupts' in node.props.keys(): + interrupt_nodes.append(node) + + return interrupt_nodes + +def get_node_label(node): + label = node.labels[0] + return label + +def gen_interrupt_adjustment_str(node, priority_incr): + label = get_node_label(node) + interrupt_val1, interrupt_priority = node.props['interrupts'].to_nums() + + if label == 'gpiote': + new_priority = 0 + else: + new_priority = interrupt_priority + priority_incr + + adj_str = f''' +&{label} {{ + interrupts = < {interrupt_val1} {new_priority} >; +}}; + ''' + return adj_str + + +def gen_interrupt_adjustment(dts_path, priority_incr): + dt = dtlib.DT(dts_path) + interrupt_nodes = get_interrupt_nodes(dt) + + adjustments = '' + for node in interrupt_nodes: + adj_str = gen_interrupt_adjustment_str(node, priority_incr) + if get_node_label(node) == 'gpiote': + adjustments = adj_str + adjustments + else: + adjustments += adj_str + + return adjustments + + +def main(): + parser = argparse.ArgumentParser(description='Generate interrupt adjustments for zephyr.dts files.') + parser.add_argument('dts_path', metavar='DTS_PATH', type=str, help='Path to the zephyr.dts file') + parser.add_argument('-p', '--priority-incr', type=int, default=2, help='By how much to increase the priority (default: 2)') + args = parser.parse_args() + + adj = gen_interrupt_adjustment(args.dts_path, args.priority_incr) + print(adj) + + +if __name__ == '__main__': + main() diff --git a/app/src/behaviors/behavior_mouse_setting.c b/app/src/behaviors/behavior_mouse_setting.c index 9ec9ddb9190..81b98d772e9 100644 --- a/app/src/behaviors/behavior_mouse_setting.c +++ b/app/src/behaviors/behavior_mouse_setting.c @@ -5,7 +5,7 @@ #include #include -#include +#include #include #define INCREMENT_TP_SENSITIVITY 10 @@ -16,71 +16,48 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event) -{ + struct zmk_behavior_binding_event event) { switch (binding->param1) { - case MS_TP_SENSITIVITY_INCR: - return zmk_mouse_ps2_tp_sensitivity_change( - INCREMENT_TP_SENSITIVITY - ); - case MS_TP_SENSITIVITY_DECR: - return zmk_mouse_ps2_tp_sensitivity_change( - -INCREMENT_TP_SENSITIVITY - ); - - case MS_TP_NEG_INERTIA_INCR: - return zmk_mouse_ps2_tp_neg_inertia_change( - INCREMENT_TP_NEG_INERTIA - ); - case MS_TP_NEG_INERTIA_DECR: - return zmk_mouse_ps2_tp_neg_inertia_change( - -INCREMENT_TP_NEG_INERTIA - ); - - case MS_TP_VALUE6_INCR: - return zmk_mouse_ps2_tp_value6_upper_plateau_speed_change( - INCREMENT_TP_VALUE6 - ); - case MS_TP_VALUE6_DECR: - return zmk_mouse_ps2_tp_value6_upper_plateau_speed_change( - -INCREMENT_TP_VALUE6 - ); - - case MS_TP_PTS_THRESHOLD_INCR: - return zmk_mouse_ps2_tp_pts_threshold_change( - INCREMENT_TP_PTS_THRESHOLD - ); - case MS_TP_PTS_THRESHOLD_DECR: - return zmk_mouse_ps2_tp_pts_threshold_change( - -INCREMENT_TP_PTS_THRESHOLD - ); + case MS_LOG: + return zmk_mouse_ps2_settings_log(); + case MS_RESET: + return zmk_mouse_ps2_settings_reset(); + case MS_TP_SENSITIVITY_INCR: + return zmk_mouse_ps2_tp_sensitivity_change(INCREMENT_TP_SENSITIVITY); + case MS_TP_SENSITIVITY_DECR: + return zmk_mouse_ps2_tp_sensitivity_change(-INCREMENT_TP_SENSITIVITY); + + case MS_TP_NEG_INERTIA_INCR: + return zmk_mouse_ps2_tp_neg_inertia_change(INCREMENT_TP_NEG_INERTIA); + case MS_TP_NEG_INERTIA_DECR: + return zmk_mouse_ps2_tp_neg_inertia_change(-INCREMENT_TP_NEG_INERTIA); + + case MS_TP_VALUE6_INCR: + return zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(INCREMENT_TP_VALUE6); + case MS_TP_VALUE6_DECR: + return zmk_mouse_ps2_tp_value6_upper_plateau_speed_change(-INCREMENT_TP_VALUE6); + + case MS_TP_PTS_THRESHOLD_INCR: + return zmk_mouse_ps2_tp_pts_threshold_change(INCREMENT_TP_PTS_THRESHOLD); + case MS_TP_PTS_THRESHOLD_DECR: + return zmk_mouse_ps2_tp_pts_threshold_change(-INCREMENT_TP_PTS_THRESHOLD); } return -ENOTSUP; } static int on_keymap_binding_released(struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event) -{ + struct zmk_behavior_binding_event event) { return ZMK_BEHAVIOR_OPAQUE; } // Initialization Function -static int zmk_behavior_mouse_setting_init(const struct device *dev) { - return 0; -}; +static int zmk_behavior_mouse_setting_init(const struct device *dev) { return 0; }; -static const struct behavior_driver_api -zmk_behavior_mouse_setting_driver_api = { - .binding_pressed = on_keymap_binding_pressed, - .binding_released = on_keymap_binding_released -}; +static const struct behavior_driver_api zmk_behavior_mouse_setting_driver_api = { + .binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released}; -DEVICE_DT_INST_DEFINE( - 0, - zmk_behavior_mouse_setting_init, NULL, - NULL, NULL, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &zmk_behavior_mouse_setting_driver_api -); +BEHAVIOR_DT_INST_DEFINE(0, zmk_behavior_mouse_setting_init, NULL, NULL, NULL, POST_KERNEL, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &zmk_behavior_mouse_setting_driver_api); diff --git a/app/src/mouse/Kconfig b/app/src/mouse/Kconfig index c35c00d49c6..7f324e28899 100644 --- a/app/src/mouse/Kconfig +++ b/app/src/mouse/Kconfig @@ -1,61 +1,8 @@ # Copyright (c) 2023 The ZMK Contributors # SPDX-License-Identifier: MIT -DT_COMPAT_ZMK_PS2_MOUSE := zmk,mouse-ps2 - config ZMK_MOUSE bool "Enable ZMK mouse emulation" default n select INPUT select INPUT_THREAD_PRIORITY_OVERRIDE - -config ZMK_MOUSE_PS2 - bool - default $(dt_compat_enabled,$(DT_COMPAT_ZMK_PS2_MOUSE)) - depends on (!ZMK_SPLIT || ZMK_SPLIT_ROLE_CENTRAL) - select ZMK_MOUSE - select PS2 - -if ZMK_MOUSE_PS2 - -config ZMK_MOUSE_PS2_SAMPLING_RATE - int "Sets how many mouse activity reports should be sent per second. The default is 100. You can reduce this setting if you see a lot of PS/2 transmission errors. Increasing it will not lead to significant improvements, because mouse reports are accumulated and only sent over bluetooth every `CONFIG_ZMK_MOUSE_TICK_DURATION` ms." - default 100 - -config ZMK_MOUSE_PS2_ENABLE_CLICKING - bool "Enables clicking events." - default y - -config ZMK_MOUSE_PS2_INVERT_X - bool "Invert the mouse movement x axis." - default n - -config ZMK_MOUSE_PS2_INVERT_Y - bool "Invert the mouse movement y axis." - default n - -config ZMK_MOUSE_PS2_SWAP_XY - bool "Swaps the X and Y axis." - default n - -config ZMK_MOUSE_PS2_SCROLL - bool "Enable scroll wheel on mouse devices supporting the Intellimouse extension." - default n - -config ZMK_MOUSE_PS2_TP_TAP_TO_SELECT - bool "Enables the ability to left-click by tapping the trackpoint." - default n - -config ZMK_MOUSE_PS2_TP_INVERT_X - bool "Inverts x on the trackpoint. This is sets the setting directly in the trackpoint firmware and should therefore correctly impact the trackpoint algorithms." - default n - -config ZMK_MOUSE_PS2_TP_INVERT_Y - bool "Inverts y on the trackpoint. This is sets the setting directly in the trackpoint firmware and should therefore correctly impact the trackpoint algorithms." - default n - -config ZMK_MOUSE_PS2_TP_SWAP_XY - bool "Swaps the x and y axis on the trackpoint. This is sets the swap settingin the trackpoint firmware and should therefore correctly impact the trackpoint algorithms. But this setting is not supported by all trackpoints." - default n - -endif # ZMK_MOUSE_PS2