Add mouse movement event
This commit is contained in:
parent
3fb874f4f6
commit
dc2e30d5db
5 changed files with 84 additions and 11 deletions
|
@ -38,6 +38,7 @@ 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_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)
|
||||
|
|
33
app/include/zmk/events/mouse_state_changed.h
Normal file
33
app/include/zmk/events/mouse_state_changed.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2020 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <zmk/event_manager.h>
|
||||
|
||||
struct zmk_mouse_state_changed {
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
bool state;
|
||||
int64_t timestamp;
|
||||
};
|
||||
|
||||
ZMK_EVENT_DECLARE(zmk_mouse_state_changed);
|
||||
|
||||
static inline struct zmk_mouse_state_changed_event *
|
||||
zmk_mouse_state_changed_from_encoded(uint32_t encoded, bool pressed,
|
||||
int64_t timestamp) {
|
||||
|
||||
uint32_t x = (binding->param1 & 0xFFFF0000) >> 16;
|
||||
uint32_t y = binding->param1 & 0x0000FFFF;
|
||||
LOG_DBG("x: 0x%02X y: 0x%02X", x, y);
|
||||
zmk_hid_mouse_movement_release(x, y);
|
||||
|
||||
return new_zmk_mouse_state_changed((struct zmk_mouse_state_changed){
|
||||
.x = x, .y = y, .state = pressed, .timestamp = timestamp});
|
||||
}
|
|
@ -13,29 +13,26 @@
|
|||
#include <zmk/event_manager.h>
|
||||
#include <zmk/events/keycode_state_changed.h>
|
||||
#include <zmk/behavior.h>
|
||||
#include <zmk/hid.h>
|
||||
#include <zmk/endpoints.h>
|
||||
#include <zmk/events/mouse_state_changed.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
ZMK_EVENT_DECLARE(zmk_mouse_move);
|
||||
|
||||
static int behavior_mouse_move_init(const struct device *dev) { return 0; };
|
||||
|
||||
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);
|
||||
uint32_t x = (binding->param1 & 0xFFFF0000) >> 16;
|
||||
uint32_t y = binding->param1 & 0x0000FFFF;
|
||||
zmk_hid_mouse_movement_press(x, y);
|
||||
return zmk_endpoints_send_mouse_report();
|
||||
return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, true,
|
||||
event.timestamp);
|
||||
}
|
||||
|
||||
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);
|
||||
uint32_t x = (binding->param1 & 0xFFFF0000) >> 16;
|
||||
uint32_t y = binding->param1 & 0x0000FFFF;
|
||||
zmk_hid_mouse_movement_release(x, y);
|
||||
return zmk_endpoints_send_mouse_report();
|
||||
return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, false,
|
||||
event.timestamp);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_mouse_move_driver_api = {
|
||||
|
|
10
app/src/events/mouse_state_changed.c
Normal file
10
app/src/events/mouse_state_changed.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
/*
|
||||
* Copyright (c) 2020 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <zmk/events/mouse_state_changed.h>
|
||||
|
||||
ZMK_EVENT_IMPL(zmk_mouse_state_changed);
|
|
@ -106,6 +106,28 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed
|
|||
return zmk_endpoints_send_report(ev->usage_page);
|
||||
}
|
||||
|
||||
static int hid_listener_mouse_pressed(const struct zmk_mouse_state_changed *ev) {
|
||||
int err;
|
||||
LOG_DBG("x: 0x%02X, y: 0x%02X", ev->x, ev->y);
|
||||
err = zmk_hid_mouse_movement_press(ev->x, ev->y);
|
||||
if (err) {
|
||||
LOG_ERR("Unable press button");
|
||||
return err;
|
||||
}
|
||||
return zmk_endpoints_send_mouse_report();
|
||||
}
|
||||
|
||||
static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev) {
|
||||
int err;
|
||||
LOG_DBG("x: 0x%02X, y: 0x%02X", ev->x, ev->y);
|
||||
err = zmk_hid_mouse_movement_release(ev->x, ev->y);
|
||||
if (err) {
|
||||
LOG_ERR("Unable to release button");
|
||||
return err;
|
||||
}
|
||||
return zmk_endpoints_send_mouse_report();
|
||||
}
|
||||
|
||||
int hid_listener(const zmk_event_t *eh) {
|
||||
const struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh);
|
||||
if (ev) {
|
||||
|
@ -114,9 +136,19 @@ int hid_listener(const zmk_event_t *eh) {
|
|||
} else {
|
||||
hid_listener_keycode_released(ev);
|
||||
}
|
||||
} else {
|
||||
const struct zmk_mouse_state_changed *ev = as_zmk_mouse_state_changed(eh);
|
||||
if (ev) {
|
||||
if (ev->state) {
|
||||
hid_listener_mouse_pressed(ev);
|
||||
} else {
|
||||
hid_listener_mouse_released(ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ZMK_LISTENER(hid_listener, hid_listener);
|
||||
ZMK_SUBSCRIPTION(hid_listener, zmk_keycode_state_changed);
|
||||
ZMK_SUBSCRIPTION(hid_listener, zmk_mouse_state_changed);
|
||||
|
|
Loading…
Add table
Reference in a new issue