Skip to content

Commit

Permalink
feat(mouse keys): add events, smoothing and acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
okke-formsma authored and krikun98 committed Jan 28, 2022
1 parent 728f42e commit 8fc5962
Show file tree
Hide file tree
Showing 33 changed files with 653 additions and 268 deletions.
9 changes: 7 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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/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,7 +40,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_state_changed.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 @@ -59,7 +64,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
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_wheel.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
2 changes: 1 addition & 1 deletion app/dts/behaviors.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
#include <behaviors/caps_word.dtsi>
#include <behaviors/mouse_key_press.dtsi>
#include <behaviors/mouse_move.dtsi>
#include <behaviors/mouse_wheel.dtsi>
#include <behaviors/mouse_scroll.dtsi>
3 changes: 3 additions & 0 deletions app/dts/behaviors/mouse_move.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
compatible = "zmk,behavior-mouse-move";
label = "MOUSE_MOVE";
#binding-cells = <1>;
delay-ms = <100>;
time-to-max-speed-ms = <1000>;
acceleration-exponent = <2000>;
};
};
};
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 = <1000>;
acceleration-exponent = <0>;
};
};
};
9 changes: 0 additions & 9 deletions app/dts/behaviors/mouse_wheel.dtsi

This file was deleted.

8 changes: 8 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-move.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ 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
5 changes: 0 additions & 5 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml

This file was deleted.

42 changes: 17 additions & 25 deletions app/include/dt-bindings/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,27 @@
#define MB8 (0x80)

/* Mouse move behavior */

#define MOVE_UP MOVE_VERT(1)

#define MOVE_DOWN MOVE_VERT(-1)

#define MOVE_LEFT MOVE_HOR(-1)

#define MOVE_RIGHT MOVE_HOR(1)

/* -32767 to 32767, barely usable beyond about 50 (probably depends on screen resolution) */
#define MOVE_VERT(vert) ((-(vert)) & 0xFFFF)

#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))

/* Mouse wheel behavior */

#define WHEEL_UP WHEEL_VERT(1)

#define WHEEL_DOWN WHEEL_VERT(-1)

#define WHEEL_LEFT WHEEL_HOR(-1)

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

/* -127 to 127, barely usable beyond about 10 */
#define WHEEL_VERT(vert) ((vert)&0xFF)
/* 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 WHEEL_HOR(hor) (((hor)&0xFF) << 8)
#define SCROLL(hor, vert) (SCROLL_HOR(hor) + SCROLL_VERT(vert))

#define WHEEL(hor, vert) (WHEEL_HOR(hor) + WHEEL_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)
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});
}
30 changes: 0 additions & 30 deletions app/include/zmk/events/mouse_state_changed.h

This file was deleted.

36 changes: 36 additions & 0 deletions app/include/zmk/events/mouse_tick.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

/*
* 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 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) {
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,
.timestamp = k_uptime_get(),
});
}
16 changes: 8 additions & 8 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static const uint8_t zmk_hid_report_desc[] = {
/* REPORT_COUNT (2) */
HID_GI_REPORT_COUNT,
0x02,
/* USAGE (X) */
/* USAGE (X) */ // Vertical scroll
HID_LI_USAGE,
HID_USAGE_GD_X,
/* USAGE (Y) */
Expand All @@ -271,7 +271,7 @@ static const uint8_t zmk_hid_report_desc[] = {
/* Input (Data,Var,Rel) */
HID_MI_INPUT,
0x06,
/* USAGE_PAGE (Consumer) */ // Horizontal wheel
/* USAGE_PAGE (Consumer) */ // Horizontal scroll
HID_GI_USAGE_PAGE,
HID_USAGE_CONSUMER,
/* USAGE (AC Pan) */
Expand Down Expand Up @@ -335,8 +335,8 @@ struct zmk_hid_mouse_report_body {
zmk_mouse_button_flags_t buttons;
int16_t x;
int16_t y;
int8_t wheel_vert;
int8_t wheel_hor;
int8_t scroll_y;
int8_t scroll_x;
} __packed;

struct zmk_hid_mouse_report {
Expand All @@ -363,10 +363,10 @@ 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);
int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons);
int zmk_hid_mouse_movement_press(int16_t x, int16_t y);
int zmk_hid_mouse_movement_release(int16_t x, int16_t y);
int zmk_hid_mouse_wheel_press(int8_t hor, int8_t vert);
int zmk_hid_mouse_wheel_release(int8_t hor, int8_t vert);
void zmk_hid_mouse_movement_set(int16_t x, int16_t y);
void zmk_hid_mouse_scroll_set(int8_t x, int8_t y);
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();

struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report();
Expand Down
14 changes: 14 additions & 0 deletions app/include/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@

typedef uint16_t zmk_mouse_button_flags_t;
typedef uint16_t zmk_mouse_button_t;

struct mouse_config {
int delay_ms;
int time_to_max_speed_ms;
// acceleration exponent 0: uniform speed
// acceleration exponent 1: uniform acceleration
// acceleration exponent 2: uniform jerk
float acceleration_exponent;
};

struct vector2d {
float x;
float y;
};
Loading

0 comments on commit 8fc5962

Please sign in to comment.