Skip to content

Commit

Permalink
Mouse-related behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Tsykunov authored and krikun98 committed Jan 28, 2022
1 parent 016256b commit 3fb874f
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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_wheel.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
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_wheel.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>;
};
};
};
9 changes: 9 additions & 0 deletions app/dts/behaviors/mouse_move.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/ {
behaviors {
/omit-if-no-ref/ mmv: behavior_mouse_move {
compatible = "zmk,behavior-mouse-move";
label = "MOUSE_MOVE";
#binding-cells = <1>;
};
};
};
9 changes: 9 additions & 0 deletions app/dts/behaviors/mouse_wheel.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/ {
behaviors {
/omit-if-no-ref/ mwh: behavior_mouse_wheel {
compatible = "zmk,behavior-mouse-wheel";
label = "MOUSE_WHEEL";
#binding-cells = <1>;
};
};
};
5 changes: 5 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-mouse-key-press.yaml
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
5 changes: 5 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,5 @@
description: Mouse move

compatible: "zmk,behavior-mouse-move"

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

compatible: "zmk,behavior-mouse-wheel"

include: one_param.yaml
39 changes: 37 additions & 2 deletions app/include/dt-bindings/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,40 @@
*/
#pragma once

#include <dt-bindings/zmk/hid_usage.h>
#include <dt-bindings/zmk/hid_usage_pages.h>
/* 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)

#define MB5 (0x10)

#define MB6 (0x20)

#define MB7 (0x40)

#define MB8 (0x80)

#define MOVE_UP (0x0000FFFF)

#define MOVE_DOWN (0x00000001)

#define MOVE_LEFT (0xFFFF0000)

#define MOVE_RIGHT (0x00010000)

#define WHEEL_UP (0x0001)

#define WHEEL_DOWN (0x00FF)

#define WHEEL_LEFT (0xFF00)

#define WHEEL_RIGHT (0x0100)
45 changes: 45 additions & 0 deletions app/src/behaviors/behavior_mouse_key_press.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_mouse_key_press

#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>

#include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/behavior.h>
#include <zmk/hid.h>
#include <zmk/endpoints.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

static int behavior_mouse_key_press_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);
zmk_hid_mouse_buttons_press(HID_USAGE_ID(binding->param1));
return zmk_endpoints_send_mouse_report();
}

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);
zmk_hid_mouse_buttons_release(HID_USAGE_ID(binding->param1));
return zmk_endpoints_send_mouse_report();
}

static const struct behavior_driver_api behavior_mouse_key_press_driver_api = {
.binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released};

#define KP_INST(n) \
DEVICE_AND_API_INIT(behavior_mouse_key_press_##n, DT_INST_LABEL(n), behavior_mouse_key_press_init, NULL, \
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_mouse_key_press_driver_api);

DT_INST_FOREACH_STATUS_OKAY(KP_INST)
49 changes: 49 additions & 0 deletions app/src/behaviors/behavior_mouse_move.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2021 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_mouse_move

#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>

#include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/behavior.h>
#include <zmk/hid.h>
#include <zmk/endpoints.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

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();
}

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();
}

static const struct behavior_driver_api behavior_mouse_move_driver_api = {
.binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released};

#define KP_INST(n) \
DEVICE_AND_API_INIT(behavior_mouse_move_##n, DT_INST_LABEL(n), behavior_mouse_move_init, NULL, \
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_mouse_move_driver_api);

DT_INST_FOREACH_STATUS_OKAY(KP_INST)
50 changes: 50 additions & 0 deletions app/src/behaviors/behavior_mouse_wheel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2021 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_mouse_wheel

#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>

#include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/behavior.h>
#include <zmk/hid.h>
#include <zmk/endpoints.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

static int behavior_mouse_wheel_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);
int32_t x = (binding->param1 & 0xFF00) >> 8;
int32_t y = binding->param1 & 0x00FF;
zmk_hid_mouse_wheel_press(x, y);
return zmk_endpoints_send_mouse_report();
}

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);
int32_t x = (binding->param1 & 0xFF00) >> 8;
int32_t y = binding->param1 & 0x00FF;
zmk_hid_mouse_wheel_release(x, y);
return zmk_endpoints_send_mouse_report();
}

static const struct behavior_driver_api behavior_mouse_wheel_driver_api = {
.binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released};


#define KP_INST(n) \
DEVICE_AND_API_INIT(behavior_mouse_wheel_##n, DT_INST_LABEL(n), behavior_mouse_wheel_init, NULL, \
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_mouse_wheel_driver_api);

DT_INST_FOREACH_STATUS_OKAY(KP_INST)

0 comments on commit 3fb874f

Please sign in to comment.