Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mouse): Added mouse emulation #778

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b14b024
Preliminary work for mouse click
krikun98 Apr 27, 2021
54ac765
Fine-tuning report, 16 buttons
krikun98 Apr 28, 2021
016256b
Bluetooth tuning, mouse wheel and movement backend
krikun98 Apr 29, 2021
3fb874f
Mouse-related behaviours
Apr 29, 2021
dc2e30d
Add mouse movement event
Apr 29, 2021
ee51487
Continuous mouse movement prototype
Apr 29, 2021
e2b97f1
Add mouse behaviour documentation
May 1, 2021
a339a9a
Cleaning out prototype traces
krikun98 May 2, 2021
141a525
clang-format
krikun98 May 2, 2021
345ef42
Implemented Rinh's suggestion to remove deadlocks
dtsykunov May 2, 2021
58178a7
Raised BLE mouse report queue size
krikun98 May 2, 2021
571e457
Documentation refactor
krikun98 May 2, 2021
0893c76
Add the doc page to the sidebar
krikun98 May 2, 2021
fddadb9
Added new mouse movement macros
krikun98 May 3, 2021
9957b28
Review edits: macro, event override fix, cosmetics
krikun98 May 3, 2021
6de29af
Mouse movement coordinate signedness consistency
krikun98 May 3, 2021
359f35b
Reverted mouse buttons 9-16
krikun98 May 3, 2021
4d08f97
Report refactor (added macros)
krikun98 May 3, 2021
b4ec49e
Simplify binary arithmetic
okke-formsma May 14, 2021
728f42e
Modified mouse_timer_unref to account for errors
krikun98 May 14, 2021
8fc5962
feat(mouse keys): add events, smoothing and acceleration
okke-formsma May 14, 2021
eb089b5
Added dedicated mouse work queue option
krikun98 Aug 26, 2021
96660dc
Simplified tick rate and made it configurable
krikun98 Aug 27, 2021
0bcd44a
Add messages to BLE queue without a waiting interval
krikun98 Aug 30, 2021
992bbc0
Cleanup and acceleration fixes
krikun98 Sep 2, 2021
8088585
Send mouse messages from dedicated thread
krikun98 Sep 21, 2021
cbce1db
Added documentation for new features
krikun98 Sep 23, 2021
ec85b7a
Moved tick duration
krikun98 Sep 23, 2021
2bf5e44
clang-format
krikun98 Sep 23, 2021
ee855f4
prettier
krikun98 Sep 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/mouse/key_listener.c)
target_sources(app PRIVATE src/mouse/main.c)
target_sources(app PRIVATE src/mouse/tick_listener.c)
target_sources(app PRIVATE src/sensors.c)
target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c)
target_sources(app PRIVATE src/event_manager.c)
Expand All @@ -38,6 +41,10 @@ target_sources(app PRIVATE src/events/keycode_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_state_changed.c)
target_sources(app PRIVATE src/events/endpoint_selection_changed.c)
target_sources(app PRIVATE src/events/sensor_event.c)
target_sources(app PRIVATE src/events/mouse_button_state_changed.c)
target_sources(app PRIVATE src/events/mouse_move_state_changed.c)
target_sources(app PRIVATE src/events/mouse_tick.c)
target_sources(app PRIVATE src/events/mouse_scroll_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/events/wpm_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/ble_active_profile_changed.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/battery_state_changed.c)
Expand All @@ -56,6 +63,9 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
target_sources(app PRIVATE src/behaviors/behavior_transparent.c)
target_sources(app PRIVATE src/behaviors/behavior_none.c)
target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_mouse_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_mouse_move.c)
target_sources(app PRIVATE src/behaviors/behavior_mouse_scroll.c)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c)
target_sources(app PRIVATE src/combo.c)
target_sources(app PRIVATE src/conditional_layer.c)
Expand Down
11 changes: 11 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ config ZMK_BLE_CONSUMER_REPORT_QUEUE_SIZE
int "Max number of consumer HID reports to queue for sending over BLE"
default 5

config ZMK_BLE_MOUSE_REPORT_QUEUE_SIZE
int "Max number of mouse HID reports to queue for sending over BLE"
default 20

config ZMK_BLE_CLEAR_BONDS_ON_START
bool "Configuration that clears all bond information from the keyboard on startup."
default n
Expand Down Expand Up @@ -323,6 +327,13 @@ endif
#Display/LED Options
endmenu

menu "Mouse Options"

rsource "src/mouse/Kconfig"

#Mouse Options
endmenu

menu "Power Management"

config ZMK_IDLE_TIMEOUT
Expand Down
3 changes: 3 additions & 0 deletions app/dts/behaviors.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
#include <behaviors/ext_power.dtsi>
#include <behaviors/outputs.dtsi>
#include <behaviors/caps_word.dtsi>
#include <behaviors/mouse_key_press.dtsi>
#include <behaviors/mouse_move.dtsi>
#include <behaviors/mouse_scroll.dtsi>
9 changes: 9 additions & 0 deletions app/dts/behaviors/mouse_key_press.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/ {
behaviors {
/omit-if-no-ref/ mkp: behavior_mouse_key_press {
compatible = "zmk,behavior-mouse-key-press";
label = "MOUSE_KEY_PRESS";
#binding-cells = <1>;
};
};
};
12 changes: 12 additions & 0 deletions app/dts/behaviors/mouse_move.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/ {
behaviors {
/omit-if-no-ref/ mmv: behavior_mouse_move {
compatible = "zmk,behavior-mouse-move";
label = "MOUSE_MOVE";
#binding-cells = <1>;
delay-ms = <0>;
time-to-max-speed-ms = <300>;
acceleration-exponent = <1>;
};
};
};
12 changes: 12 additions & 0 deletions app/dts/behaviors/mouse_scroll.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/ {
behaviors {
/omit-if-no-ref/ mwh: msc: behavior_mouse_scroll {
compatible = "zmk,behavior-mouse-scroll";
label = "MOUSE_SCROLL";
#binding-cells = <1>;
delay-ms = <0>;
time-to-max-speed-ms = <300>;
acceleration-exponent = <0>;
};
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description: Mouse key press/release behavior

compatible: "zmk,behavior-mouse-key-press"

include: one_param.yaml
13 changes: 13 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-move.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: Mouse move

compatible: "zmk,behavior-mouse-move"

include: one_param.yaml

properties:
delay-ms:
type: int
time-to-max-speed-ms:
type: int
acceleration-exponent:
type: int
13 changes: 13 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-scroll.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: Mouse scroll

compatible: "zmk,behavior-mouse-scroll"

include: one_param.yaml

properties:
delay-ms:
type: int
time-to-max-speed-ms:
type: int
acceleration-exponent:
type: int
1 change: 1 addition & 0 deletions app/include/dt-bindings/zmk/hid_usage_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define HID_USAGE_GDV (0x06) // Generic Device Controls
#define HID_USAGE_KEY (0x07) // Keyboard/Keypad
#define HID_USAGE_LED (0x08) // LED
#define HID_USAGE_BUTTON (0x09) // Button
#define HID_USAGE_TELEPHONY (0x0B) // Telephony Device
#define HID_USAGE_CONSUMER (0x0C) // Consumer
#define HID_USAGE_DIGITIZERS (0x0D) // Digitizers
Expand Down
55 changes: 55 additions & 0 deletions app/include/dt-bindings/zmk/mouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once

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

/* Right click */
#define MB2 (0x02)
#define RCLK (MB2)

/* Middle click */
#define MB3 (0x04)
#define MCLK (MB3)

#define MB4 (0x08)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MPREV / MNEXT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As aliases? Sure, I'll add these in.


#define MB5 (0x10)

#define MB6 (0x20)

#define MB7 (0x40)

#define MB8 (0x80)

/* Mouse move behavior */
#define MOVE_VERT(vert) ((vert)&0xFFFF)
#define MOVE_VERT_DECODE(encoded) (int16_t)((encoded)&0x0000FFFF)
#define MOVE_HOR(hor) (((hor)&0xFFFF) << 16)
#define MOVE_HOR_DECODE(encoded) (int16_t)(((encoded)&0xFFFF0000) >> 16)

#define MOVE(hor, vert) (MOVE_HOR(hor) + MOVE_VERT(vert))

#define MOVE_UP MOVE_VERT(-600)
#define MOVE_DOWN MOVE_VERT(600)
#define MOVE_LEFT MOVE_HOR(-600)
#define MOVE_RIGHT MOVE_HOR(600)

/* Mouse scroll behavior */
#define SCROLL_VERT(vert) ((vert)&0xFFFF)
#define SCROLL_VERT_DECODE(encoded) (int16_t)((encoded)&0x0000FFFF)
#define SCROLL_HOR(hor) (((hor)&0xFFFF) << 16)
#define SCROLL_HOR_DECODE(encoded) (int16_t)(((encoded)&0xFFFF0000) >> 16)

#define SCROLL(hor, vert) (SCROLL_HOR(hor) + SCROLL_VERT(vert))

#define SCROLL_UP SCROLL_VERT(10)
#define SCROLL_DOWN SCROLL_VERT(-10)
#define SCROLL_LEFT SCROLL_HOR(-10)
#define SCROLL_RIGHT SCROLL_HOR(10)
1 change: 1 addition & 0 deletions app/include/zmk/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ int zmk_endpoints_toggle();
enum zmk_endpoint zmk_endpoints_selected();

int zmk_endpoints_send_report(uint16_t usage_page);
int zmk_endpoints_send_mouse_report();
27 changes: 27 additions & 0 deletions app/include/zmk/events/mouse_button_state_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

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

struct zmk_mouse_button_state_changed {
zmk_mouse_button_t buttons;
bool state;
int64_t timestamp;
};

ZMK_EVENT_DECLARE(zmk_mouse_button_state_changed);

static inline struct zmk_mouse_button_state_changed_event *
zmk_mouse_button_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t timestamp) {
return new_zmk_mouse_button_state_changed((struct zmk_mouse_button_state_changed){
.buttons = HID_USAGE_ID(encoded), .state = pressed, .timestamp = timestamp});
}
33 changes: 33 additions & 0 deletions app/include/zmk/events/mouse_move_state_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr.h>
#include <zmk/event_manager.h>
#include <zmk/mouse.h>

struct zmk_mouse_move_state_changed {
struct vector2d max_speed;
struct mouse_config config;
bool state;
int64_t timestamp;
};

ZMK_EVENT_DECLARE(zmk_mouse_move_state_changed);

static inline struct zmk_mouse_move_state_changed_event *
zmk_mouse_move_state_changed_from_encoded(uint32_t encoded, struct mouse_config config,
bool pressed, int64_t timestamp) {
struct vector2d max_speed = (struct vector2d){
.x = MOVE_HOR_DECODE(encoded),
.y = MOVE_VERT_DECODE(encoded),
};

return new_zmk_mouse_move_state_changed((struct zmk_mouse_move_state_changed){
.max_speed = max_speed, .config = config, .state = pressed, .timestamp = timestamp});
}
34 changes: 34 additions & 0 deletions app/include/zmk/events/mouse_scroll_state_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

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

struct zmk_mouse_scroll_state_changed {
struct vector2d max_speed;
struct mouse_config config;
bool state;
int64_t timestamp;
};

ZMK_EVENT_DECLARE(zmk_mouse_scroll_state_changed);

static inline struct zmk_mouse_scroll_state_changed_event *
zmk_mouse_scroll_state_changed_from_encoded(uint32_t encoded, struct mouse_config config,
bool pressed, int64_t timestamp) {
struct vector2d max_speed = (struct vector2d){
.x = SCROLL_HOR_DECODE(encoded),
.y = SCROLL_VERT_DECODE(encoded),
};

return new_zmk_mouse_scroll_state_changed((struct zmk_mouse_scroll_state_changed){
.max_speed = max_speed, .config = config, .state = pressed, .timestamp = timestamp});
}
39 changes: 39 additions & 0 deletions app/include/zmk/events/mouse_tick.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

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

struct zmk_mouse_tick {
struct vector2d max_move;
struct vector2d max_scroll;
struct mouse_config move_config;
struct mouse_config scroll_config;
int64_t *start_time;
int64_t timestamp;
};

ZMK_EVENT_DECLARE(zmk_mouse_tick);

static inline struct zmk_mouse_tick_event *zmk_mouse_tick(struct vector2d max_move,
struct vector2d max_scroll,
struct mouse_config move_config,
struct mouse_config scroll_config,
int64_t *movement_start) {
return new_zmk_mouse_tick((struct zmk_mouse_tick){
.max_move = max_move,
.max_scroll = max_scroll,
.move_config = move_config,
.scroll_config = scroll_config,
.start_time = movement_start,
.timestamp = k_uptime_get(),
});
}
Loading