Skip to content

Commit

Permalink
fixup! feat(mouse): Add mouse move and scroll support
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Dec 9, 2024
1 parent a072dd7 commit c562284
Show file tree
Hide file tree
Showing 79 changed files with 234 additions and 218 deletions.
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/events/usb_conn_sta
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_SOFT_OFF app PRIVATE src/behaviors/behavior_soft_off.c)
add_subdirectory_ifdef(CONFIG_ZMK_MOUSE src/mouse/)
add_subdirectory_ifdef(CONFIG_ZMK_POINTING src/pointing/)
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
Expand Down
2 changes: 1 addition & 1 deletion app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ endif # ZMK_BACKLIGHT

endmenu # Display/LED Options

rsource "src/mouse/Kconfig"
rsource "src/pointing/Kconfig"

menu "Power Management"

Expand Down
4 changes: 2 additions & 2 deletions app/Kconfig.behaviors
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ config ZMK_BEHAVIOR_KEY_TOGGLE
config ZMK_BEHAVIOR_MOUSE_KEY_PRESS
bool
default y
depends on DT_HAS_ZMK_BEHAVIOR_MOUSE_KEY_PRESS_ENABLED && ZMK_MOUSE
depends on DT_HAS_ZMK_BEHAVIOR_MOUSE_KEY_PRESS_ENABLED && ZMK_POINTING

config ZMK_BEHAVIOR_STICKY_KEY
bool
Expand All @@ -96,7 +96,7 @@ config ZMK_BEHAVIOR_SOFT_OFF
config ZMK_BEHAVIOR_INPUT_TWO_AXIS
bool
default y
depends on DT_HAS_ZMK_BEHAVIOR_INPUT_TWO_AXIS_ENABLED && ZMK_MOUSE
depends on DT_HAS_ZMK_BEHAVIOR_INPUT_TWO_AXIS_ENABLED && ZMK_POINTING

config ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON
bool
Expand Down
2 changes: 1 addition & 1 deletion app/core-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include:
nickname: "display"
- board: nice_nano_v2
shield: kyria_left
cmake-args: "-DCONFIG_ZMK_MOUSE=y"
cmake-args: "-DCONFIG_ZMK_POINTING=y"
nickname: "mouse"
- board: sparkfun_pro_micro_rp2040
shield: reviung41
Expand Down
47 changes: 3 additions & 44 deletions app/include/dt-bindings/zmk/mouse.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@

/*
* Copyright (c) 2023 The ZMK Contributors
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once

#include <zephyr/dt-bindings/dt-util.h>

/* Mouse press behavior */
/* Left click */
#define MB1 BIT(0)
#define LCLK (MB1)

/* Right click */
#define MB2 BIT(1)
#define RCLK (MB2)

/* Middle click */
#define MB3 BIT(2)
#define MCLK (MB3)

#define MB4 BIT(3)
#define MB5 BIT(4)

#ifndef ZMK_MOUSE_DEFAULT_MOVE_VAL
#define ZMK_MOUSE_DEFAULT_MOVE_VAL 600
#endif

#ifndef ZMK_MOUSE_DEFAULT_SCRL_VAL
#define ZMK_MOUSE_DEFAULT_SCRL_VAL 10
#endif

/* Mouse move behavior */
#define MOVE_Y(vert) ((vert) & 0xFFFF)
#define MOVE_Y_DECODE(encoded) (int16_t)((encoded) & 0x0000FFFF)
#define MOVE_X(hor) (((hor) & 0xFFFF) << 16)
#define MOVE_X_DECODE(encoded) (int16_t)(((encoded) & 0xFFFF0000) >> 16)

#define MOVE(hor, vert) (MOVE_X(hor) + MOVE_Y(vert))

#define MOVE_UP MOVE_Y(-ZMK_MOUSE_DEFAULT_MOVE_VAL)
#define MOVE_DOWN MOVE_Y(ZMK_MOUSE_DEFAULT_MOVE_VAL)
#define MOVE_LEFT MOVE_X(-ZMK_MOUSE_DEFAULT_MOVE_VAL)
#define MOVE_RIGHT MOVE_X(ZMK_MOUSE_DEFAULT_MOVE_VAL)

#define SCRL_UP MOVE_Y(ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_DOWN MOVE_Y(-ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_LEFT MOVE_X(-ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_RIGHT MOVE_X(ZMK_MOUSE_DEFAULT_SCRL_VAL)
#include "pointing.h"
50 changes: 50 additions & 0 deletions app/include/dt-bindings/zmk/pointing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once

#include <zephyr/dt-bindings/dt-util.h>

/* Mouse press behavior */
/* Left click */
#define MB1 BIT(0)
#define LCLK (MB1)

/* Right click */
#define MB2 BIT(1)
#define RCLK (MB2)

/* Middle click */
#define MB3 BIT(2)
#define MCLK (MB3)

#define MB4 BIT(3)
#define MB5 BIT(4)

#ifndef ZMK_POINTING_DEFAULT_MOVE_VAL
#define ZMK_POINTING_DEFAULT_MOVE_VAL 600
#endif

#ifndef ZMK_POINTING_DEFAULT_SCRL_VAL
#define ZMK_POINTING_DEFAULT_SCRL_VAL 10
#endif

/* Mouse move behavior */
#define MOVE_Y(vert) ((vert) & 0xFFFF)
#define MOVE_Y_DECODE(encoded) (int16_t)((encoded) & 0x0000FFFF)
#define MOVE_X(hor) (((hor) & 0xFFFF) << 16)
#define MOVE_X_DECODE(encoded) (int16_t)(((encoded) & 0xFFFF0000) >> 16)

#define MOVE(hor, vert) (MOVE_X(hor) + MOVE_Y(vert))

#define MOVE_UP MOVE_Y(-ZMK_POINTING_DEFAULT_MOVE_VAL)
#define MOVE_DOWN MOVE_Y(ZMK_POINTING_DEFAULT_MOVE_VAL)
#define MOVE_LEFT MOVE_X(-ZMK_POINTING_DEFAULT_MOVE_VAL)
#define MOVE_RIGHT MOVE_X(ZMK_POINTING_DEFAULT_MOVE_VAL)

#define SCRL_UP MOVE_Y(ZMK_POINTING_DEFAULT_SCRL_VAL)
#define SCRL_DOWN MOVE_Y(-ZMK_POINTING_DEFAULT_SCRL_VAL)
#define SCRL_LEFT MOVE_X(-ZMK_POINTING_DEFAULT_SCRL_VAL)
#define SCRL_RIGHT MOVE_X(ZMK_POINTING_DEFAULT_SCRL_VAL)
4 changes: 2 additions & 2 deletions app/include/zmk/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ struct zmk_endpoint_instance zmk_endpoints_selected(void);

int zmk_endpoints_send_report(uint16_t usage_page);

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
int zmk_endpoints_send_mouse_report();
#endif // IS_ENABLE(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)

void zmk_endpoints_clear_current(void);
2 changes: 1 addition & 1 deletion app/include/zmk/events/mouse_button_state_changed.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <zmk/hid.h>
#include <zmk/event_manager.h>
#include <zmk/mouse.h>
#include <zmk/pointing.h>

struct zmk_mouse_button_state_changed {
zmk_mouse_button_t buttons;
Expand Down
34 changes: 17 additions & 17 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <zephyr/usb/class/usb_hid.h>

#include <zmk/keys.h>
#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#include <zmk/mouse.h>
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
#include <zmk/pointing.h>
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)

#include <dt-bindings/zmk/hid_usage.h>
#include <dt-bindings/zmk/hid_usage_pages.h>
Expand Down Expand Up @@ -185,7 +185,7 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_ARRAY | ZMK_HID_MAIN_VAL_ABS),
HID_END_COLLECTION,

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
HID_USAGE_PAGE(HID_USAGE_GD),
HID_USAGE(HID_USAGE_GD_MOUSE),
HID_COLLECTION(HID_COLLECTION_APPLICATION),
Expand Down Expand Up @@ -214,7 +214,7 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_REPORT_COUNT(0x02),
HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_REL),
HID_COLLECTION(HID_COLLECTION_LOGICAL),
#if IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#if IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)
HID_USAGE(HID_USAGE_GD_RESOLUTION_MULTIPLIER),
HID_LOGICAL_MIN8(0x00),
HID_LOGICAL_MAX8(0x0F),
Expand All @@ -224,7 +224,7 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_REPORT_COUNT(0x01),
HID_PUSH,
HID_FEATURE(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS),
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)
HID_USAGE(HID_USAGE_GD_WHEEL),
HID_LOGICAL_MIN16(0xFF, -0x7F),
HID_LOGICAL_MAX16(0xFF, 0x7F),
Expand All @@ -235,11 +235,11 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_REL),
HID_END_COLLECTION,
HID_COLLECTION(HID_COLLECTION_LOGICAL),
#if IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#if IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)
HID_USAGE(HID_USAGE_GD_RESOLUTION_MULTIPLIER),
HID_POP,
HID_FEATURE(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS),
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)
HID_USAGE_PAGE(HID_USAGE_CONSUMER),
HID_USAGE16_SINGLE(HID_USAGE_CONSUMER_AC_PAN),
HID_LOGICAL_MIN16(0xFF, -0x7F),
Expand All @@ -252,7 +252,7 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_END_COLLECTION,
HID_END_COLLECTION,
HID_END_COLLECTION,
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)
};

#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
Expand Down Expand Up @@ -315,7 +315,7 @@ struct zmk_hid_consumer_report {
struct zmk_hid_consumer_report_body body;
} __packed;

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
struct zmk_hid_mouse_report_body {
zmk_mouse_button_flags_t buttons;
int16_t d_x;
Expand All @@ -329,7 +329,7 @@ struct zmk_hid_mouse_report {
struct zmk_hid_mouse_report_body body;
} __packed;

#if IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#if IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)

struct zmk_hid_mouse_resolution_feature_report_body {
uint8_t wheel_res : 4;
Expand All @@ -341,9 +341,9 @@ struct zmk_hid_mouse_resolution_feature_report {
struct zmk_hid_mouse_resolution_feature_report_body body;
} __packed;

#endif // IS_ENABLED(CONFIG_ZMK_MOUSE_SMOOTH_SCROLLING)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)

#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)

zmk_mod_flags_t zmk_hid_get_explicit_mods(void);
int zmk_hid_register_mod(zmk_mod_t modifier);
Expand Down Expand Up @@ -371,7 +371,7 @@ int zmk_hid_press(uint32_t usage);
int zmk_hid_release(uint32_t usage);
bool zmk_hid_is_pressed(uint32_t usage);

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
int zmk_hid_mouse_button_press(zmk_mouse_button_t button);
int zmk_hid_mouse_button_release(zmk_mouse_button_t button);
int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons);
Expand All @@ -382,7 +382,7 @@ void zmk_hid_mouse_movement_update(int16_t x, int16_t y);
void zmk_hid_mouse_scroll_update(int8_t x, int8_t y);
void zmk_hid_mouse_clear(void);

#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)

struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report(void);
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(void);
Expand All @@ -391,6 +391,6 @@ struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(void);
zmk_hid_boot_report_t *zmk_hid_get_boot_report();
#endif

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
struct zmk_hid_mouse_report *zmk_hid_get_mouse_report();
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)
4 changes: 2 additions & 2 deletions app/include/zmk/hog.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
int zmk_hog_send_keyboard_report(struct zmk_hid_keyboard_report_body *body);
int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *body);

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *body);
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)
25 changes: 0 additions & 25 deletions app/include/zmk/mouse/resolution_multipliers.h

This file was deleted.

2 changes: 1 addition & 1 deletion app/include/zmk/mouse.h → app/include/zmk/pointing.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include <dt-bindings/zmk/mouse.h>
#include <dt-bindings/zmk/pointing.h>

typedef uint8_t zmk_mouse_button_flags_t;
typedef uint16_t zmk_mouse_button_t;
26 changes: 26 additions & 0 deletions app/include/zmk/pointing/resolution_multipliers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/hid.h>
#include <zmk/endpoints.h>

struct zmk_pointing_resolution_multipliers {
uint8_t wheel;
uint8_t hor_wheel;
};

struct zmk_pointing_resolution_multipliers
zmk_pointing_resolution_multipliers_get_current_profile(void);
struct zmk_pointing_resolution_multipliers
zmk_pointing_resolution_multipliers_get_profile(struct zmk_endpoint_instance endpoint);
void zmk_pointing_resolution_multipliers_set_profile(
struct zmk_pointing_resolution_multipliers multipliers, struct zmk_endpoint_instance endpoint);

void zmk_pointing_resolution_multipliers_process_report(
struct zmk_hid_mouse_resolution_feature_report_body *report,
struct zmk_endpoint_instance endpoint);
4 changes: 2 additions & 2 deletions app/include/zmk/usb_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

int zmk_usb_hid_send_keyboard_report(void);
int zmk_usb_hid_send_consumer_report(void);
#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
int zmk_usb_hid_send_mouse_report(void);
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)
void zmk_usb_hid_set_protocol(uint8_t protocol);
4 changes: 2 additions & 2 deletions app/src/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/usb.h>
#endif

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)
#include <zephyr/input/input.h>
#endif

Expand Down Expand Up @@ -110,7 +110,7 @@ ZMK_LISTENER(activity, activity_event_listener);
ZMK_SUBSCRIPTION(activity, zmk_position_state_changed);
ZMK_SUBSCRIPTION(activity, zmk_sensor_event);

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_POINTING)

static void note_activity_work_cb(struct k_work *_work) { note_activity(); }

Expand Down
Loading

0 comments on commit c562284

Please sign in to comment.