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 21, 2023
1 parent 18c62cd commit fc6f0ad
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext
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,7 +67,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_BEHAVIOR_MOUSE_MOVE app PRIVATE src/behaviors/behavior_mouse_move.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/behaviors/behavior_mouse_scroll.c)
target_sources(app PRIVATE src/combo.c)
target_sources(app PRIVATE src/behaviors/behavior_tap_dance.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
21 changes: 17 additions & 4 deletions app/src/behaviors/behavior_mouse_key_press.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,39 @@
#include <zmk/behavior.h>
#include <zmk/event_manager.h>
#include <zmk/events/mouse_button_state_changed.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
2 changes: 2 additions & 0 deletions app/src/mouse/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
config ZMK_MOUSE
bool "Enable ZMK mouse emulation"
default n
select INPUT
select INPUT_THREAD_PRIORITY_OVERRIDE

config ZMK_MOUSE_TICK_DURATION
int "Mouse tick duration in ms"
Expand Down
68 changes: 68 additions & 0 deletions app/src/mouse/hid_input_listener.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zephyr/kernel.h>
#include <zephyr/input/input.h>
#include <zephyr/dt-bindings/input/input-event-codes.h>

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

void handle_rel_code(struct input_event *evt) {
switch (evt->code) {
case INPUT_REL_X:
zmk_hid_mouse_movement_update(evt->value, 0);
break;
case INPUT_REL_Y:
zmk_hid_mouse_movement_update(0, evt->value);
break;
case INPUT_REL_WHEEL:
zmk_hid_mouse_scroll_update(0, evt->value);
break;
case INPUT_REL_HWHEEL:
zmk_hid_mouse_scroll_update(evt->value, 0);
break;
default:
break;
}
}

void handle_key_code(struct input_event *evt) {

switch (evt->code) {
case INPUT_BTN_0:
case INPUT_BTN_1:
case INPUT_BTN_2:
case INPUT_BTN_3:
case INPUT_BTN_4:
int8_t btn = evt->code - INPUT_BTN_0;
if (evt->value > 0) {
zmk_hid_mouse_button_press(btn);
} else {
zmk_hid_mouse_button_release(btn);
}
break;
default:
break;
}
}

void input_handler(struct input_event *evt) {
switch (evt->type) {
case INPUT_EV_REL:
handle_rel_code(evt);
break;
case INPUT_EV_KEY:
handle_key_code(evt);
}

if (evt->sync) {
zmk_endpoints_send_mouse_report();
}
}

INPUT_CALLBACK_DEFINE(NULL, input_handler);
2 changes: 1 addition & 1 deletion app/src/mouse/key_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void mouse_tick_timer_handler(struct k_work *work) {
LOG_DBG("Raising mouse tick event");
ZMK_EVENT_RAISE(
zmk_mouse_tick(move_speed, scroll_speed, move_config, scroll_config, start_times));
zmk_endpoints_send_mouse_report();
// zmk_endpoints_send_mouse_report();
}

K_WORK_DEFINE(mouse_tick, &mouse_tick_timer_handler);
Expand Down
17 changes: 13 additions & 4 deletions app/src/mouse/tick_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/mouse.h>

#include <zephyr/sys/util.h> // CLAMP
#include <zephyr/input/input.h>
#include <zephyr/dt-bindings/input/input-event-codes.h>

#if !defined(CONFIG_ZMK_SPLIT) || defined(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
#if CONFIG_MINIMAL_LIBC
Expand Down Expand Up @@ -87,13 +89,20 @@ static void mouse_tick_handler(const struct zmk_mouse_tick *tick) {
struct vector2d move =
update_movement(&move_remainder, &(tick->move_config), tick->max_move, tick->timestamp,
tick->start_times.m_x, tick->start_times.m_y);
zmk_hid_mouse_movement_update((int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX),
(int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX));

int ret = 0;
ret = input_report_rel(NULL, INPUT_REL_X, (int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX), false,
K_NO_WAIT);
ret = input_report_rel(NULL, INPUT_REL_Y, (int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX), false,
K_NO_WAIT);

struct vector2d scroll =
update_movement(&scroll_remainder, &(tick->scroll_config), tick->max_scroll,
tick->timestamp, tick->start_times.s_x, tick->start_times.s_y);
zmk_hid_mouse_scroll_update((int8_t)CLAMP(scroll.x, INT8_MIN, INT8_MAX),
(int8_t)CLAMP(scroll.y, INT8_MIN, INT8_MAX));
ret = input_report_rel(NULL, INPUT_REL_WHEEL, (int16_t)CLAMP(scroll.y, INT16_MIN, INT16_MAX),
false, K_NO_WAIT);
ret = input_report_rel(NULL, INPUT_REL_HWHEEL, (int16_t)CLAMP(scroll.x, INT16_MIN, INT16_MAX),
true, K_NO_WAIT);
}

int zmk_mouse_tick_listener(const zmk_event_t *eh) {
Expand Down

0 comments on commit fc6f0ad

Please sign in to comment.