Skip to content

Commit

Permalink
refactor: Use input system for pointer events.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Nov 23, 2023
1 parent 18c62cd commit 5cf5a4f
Show file tree
Hide file tree
Showing 35 changed files with 499 additions and 637 deletions.
10 changes: 2 additions & 8 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,22 @@ target_sources(app PRIVATE src/stdlib.c)
target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/key_listener.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/main.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE 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)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c)
target_sources(app PRIVATE src/events/activity_state_changed.c)
target_sources(app PRIVATE src/events/position_state_changed.c)
target_sources(app PRIVATE src/events/sensor_event.c)
target_sources(app PRIVATE src/events/mouse_button_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/events/mouse_move_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/events/mouse_tick.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE 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_USB_DEVICE_STACK app PRIVATE src/events/usb_conn_state_changed.c)
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c)
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE src/hid.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/main.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/hid_input_listener.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY_TOGGLE app PRIVATE src/behaviors/behavior_key_toggle.c)
target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c)
Expand All @@ -66,8 +61,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_SENSOR_ROTATE_VAR app PRIVATE src/behaviors/behavior_sensor_rotate_var.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON app PRIVATE src/behaviors/behavior_sensor_rotate_common.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_MOUSE_KEY_PRESS app PRIVATE src/behaviors/behavior_mouse_key_press.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/behaviors/behavior_mouse_move.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/behaviors/behavior_mouse_scroll.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_MOUSE_MOVE app PRIVATE src/behaviors/behavior_mouse_move.c)
target_sources(app PRIVATE src/combo.c)
target_sources(app PRIVATE src/behaviors/behavior_tap_dance.c)
target_sources(app PRIVATE src/behavior_queue.c)
Expand Down
6 changes: 6 additions & 0 deletions app/Kconfig.behaviors
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ config ZMK_BEHAVIOR_MOUSE_KEY_PRESS
depends on DT_HAS_ZMK_BEHAVIOR_MOUSE_KEY_PRESS_ENABLED
imply ZMK_MOUSE

config ZMK_BEHAVIOR_MOUSE_MOVE
bool
default y
depends on DT_HAS_ZMK_BEHAVIOR_MOUSE_MOVE_ENABLED
imply ZMK_MOUSE

config ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON
bool
default n
Expand Down
5 changes: 4 additions & 1 deletion app/dts/behaviors/mouse_move.dtsi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <zephyr/dt-bindings/input/input-event-codes.h>

/ {
behaviors {
/omit-if-no-ref/ mmv: behavior_mouse_move {
compatible = "zmk,behavior-mouse-move";
label = "MOUSE_MOVE";
#binding-cells = <1>;
delay-ms = <0>;
x-code = <INPUT_REL_X>;
y-code = <INPUT_REL_Y>;
time-to-max-speed-ms = <300>;
acceleration-exponent = <1>;
};
Expand Down
7 changes: 5 additions & 2 deletions app/dts/behaviors/mouse_scroll.dtsi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <zephyr/dt-bindings/input/input-event-codes.h>

/ {
behaviors {
/omit-if-no-ref/ msc: behavior_mouse_scroll {
compatible = "zmk,behavior-mouse-scroll";
compatible = "zmk,behavior-mouse-move";
label = "MOUSE_SCROLL";
#binding-cells = <1>;
delay-ms = <0>;
x-code = <INPUT_REL_HWHEEL>;
y-code = <INPUT_REL_WHEEL>;
time-to-max-speed-ms = <300>;
acceleration-exponent = <0>;
};
Expand Down
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 @@ -5,9 +5,17 @@ compatible: "zmk,behavior-mouse-move"
include: one_param.yaml

properties:
x-code:
type: int
required: true
y-code:
type: int
required: true
delay-ms:
type: int
time-to-max-speed-ms:
type: int
required: true
acceleration-exponent:
type: int
default: 1
13 changes: 0 additions & 13 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-scroll.yaml

This file was deleted.

16 changes: 4 additions & 12 deletions app/include/dt-bindings/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@
#define MOVE_LEFT MOVE_X(-ZMK_MOUSE_DEFAULT_MOVE_VAL)
#define MOVE_RIGHT MOVE_X(ZMK_MOUSE_DEFAULT_MOVE_VAL)

/* Mouse scroll behavior */
#define SCRL_Y(vert) ((vert)&0xFFFF)
#define SCRL_Y_DECODE(encoded) (int16_t)((encoded)&0x0000FFFF)
#define SCRL_X(hor) (((hor)&0xFFFF) << 16)
#define SCRL_X_DECODE(encoded) (int16_t)(((encoded)&0xFFFF0000) >> 16)

#define SCROLL(hor, vert) (SCRL_X(hor) + SCRL_Y(vert))

#define SCRL_UP SCRL_Y(ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_DOWN SCRL_Y(-ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_LEFT SCRL_X(-ZMK_MOUSE_DEFAULT_SCRL_VAL)
#define SCRL_RIGHT SCRL_X(ZMK_MOUSE_DEFAULT_SCRL_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)
26 changes: 0 additions & 26 deletions app/include/zmk/events/mouse_button_state_changed.h

This file was deleted.

33 changes: 0 additions & 33 deletions app/include/zmk/events/mouse_move_state_changed.h

This file was deleted.

34 changes: 0 additions & 34 deletions app/include/zmk/events/mouse_scroll_state_changed.h

This file was deleted.

39 changes: 0 additions & 39 deletions app/include/zmk/events/mouse_tick.h

This file was deleted.

24 changes: 0 additions & 24 deletions app/include/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,3 @@

typedef uint8_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
int acceleration_exponent;
};

struct vector2d {
float x;
float y;
};

struct mouse_times {
uint64_t m_x;
uint64_t m_y;
uint64_t s_x;
uint64_t s_y;
};

struct k_work_q *zmk_mouse_work_q();
int zmk_mouse_init();
25 changes: 19 additions & 6 deletions app/src/behaviors/behavior_mouse_key_press.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,41 @@
#include <zephyr/logging/log.h>

#include <zmk/behavior.h>
#include <zmk/event_manager.h>
#include <zmk/events/mouse_button_state_changed.h>
#include <zmk/hid.h>
#include <zephyr/input/input.h>
#include <zephyr/dt-bindings/input/input-event-codes.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

static int behavior_mouse_key_press_init(const struct device *dev) { return 0; };

static void process_key_state(const struct device *dev, int32_t val, bool pressed) {
for (int i = 0; i < ZMK_HID_MOUSE_NUM_BUTTONS; i++) {
if (val & BIT(i)) {
WRITE_BIT(val, i, 0);
input_report_key(dev, INPUT_BTN_0 + i, pressed ? 1 : 0, val == 0, K_FOREVER);
}
}
}

static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);

return ZMK_EVENT_RAISE(
zmk_mouse_button_state_changed_from_encoded(binding->param1, true, event.timestamp));
process_key_state(device_get_binding(binding->behavior_dev), binding->param1, true);

return 0;
}

static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(
zmk_mouse_button_state_changed_from_encoded(binding->param1, false, event.timestamp));

process_key_state(device_get_binding(binding->behavior_dev), binding->param1, false);

return 0;
}

static const struct behavior_driver_api behavior_mouse_key_press_driver_api = {
Expand Down
Loading

0 comments on commit 5cf5a4f

Please sign in to comment.