forked from kofal.net/zmk
Refactor global bindings, implement mod-tap.
* Use extra comptible = "zmk,behavior-global" to add behaviors to global bindings for event notification. * Implement mod-tap, as a keymap binding and global one to skip tap if other keycode pressed while held.
This commit is contained in:
@@ -42,9 +42,27 @@ static int on_keycode_released(struct device *dev, u32_t keycode)
|
||||
return zmk_endpoints_send_report(changes);
|
||||
}
|
||||
|
||||
static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers)
|
||||
{
|
||||
LOG_DBG("modifiers %d", modifiers);
|
||||
|
||||
zmk_hid_register_mods(modifiers);
|
||||
return zmk_endpoints_send_report(Keypad);
|
||||
}
|
||||
|
||||
static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers)
|
||||
{
|
||||
LOG_DBG("modifiers %d", modifiers);
|
||||
|
||||
zmk_hid_unregister_mods(modifiers);
|
||||
return zmk_endpoints_send_report(Keypad);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_hid_driver_api = {
|
||||
.keycode_pressed = on_keycode_pressed,
|
||||
.keycode_released = on_keycode_released
|
||||
.keycode_released = on_keycode_released,
|
||||
.modifiers_pressed = on_modifiers_pressed,
|
||||
.modifiers_released = on_modifiers_released
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -29,25 +29,25 @@ static int behavior_key_press_init(struct device *dev)
|
||||
// * > 0 - indicate successful processing, and halt further handling,
|
||||
// * 0 - Indicate successful processing, and continue propagation.
|
||||
// * < 0 - Indicate error processing, report and halt further propagation.
|
||||
static int on_position_pressed(struct device *dev, u32_t keycode, u32_t _)
|
||||
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _)
|
||||
{
|
||||
LOG_DBG("pressing: %d", keycode);
|
||||
LOG_DBG("position %d keycode %d", position, keycode);
|
||||
return zmk_events_keycode_pressed(keycode);
|
||||
}
|
||||
|
||||
|
||||
// They keycode is passed by the "keymap" based on the parameter created as part of the assignment.
|
||||
static int on_position_released(struct device *dev, u32_t keycode, u32_t _)
|
||||
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _)
|
||||
{
|
||||
LOG_DBG("releasing: %d", keycode);
|
||||
LOG_DBG("position %d keycode %d", position, keycode);
|
||||
return zmk_events_keycode_released(keycode);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_key_press_driver_api = {
|
||||
// These callbacks are all optional, and define which kinds of events the behavior can handle.
|
||||
// They can reference local functions defined here, or shared event handlers.
|
||||
.position_pressed = on_position_pressed,
|
||||
.position_released = on_position_released
|
||||
.binding_pressed = on_keymap_binding_pressed,
|
||||
.binding_released = on_keymap_binding_released
|
||||
// Other optional callbacks a behavior can implement
|
||||
// .on_mouse_moved
|
||||
// .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior
|
||||
|
||||
@@ -23,19 +23,19 @@ static int behavior_keymap_init(struct device *dev)
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int on_position_pressed(struct device *dev, u32_t position, u32_t _)
|
||||
static int on_position_pressed(struct device *dev, u32_t position)
|
||||
{
|
||||
return zmk_keymap_position_state_changed(position, true);
|
||||
}
|
||||
|
||||
static int on_position_released(struct device *dev, u32_t position, u32_t _)
|
||||
static int on_position_released(struct device *dev, u32_t position)
|
||||
{
|
||||
return zmk_keymap_position_state_changed(position, false);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_keymap_driver_api = {
|
||||
.position_pressed = on_position_pressed,
|
||||
.position_released = on_position_released
|
||||
.position_released = on_position_released,
|
||||
};
|
||||
|
||||
|
||||
@@ -47,4 +47,4 @@ DEVICE_AND_API_INIT(behavior_keymap, DT_INST_LABEL(0), behavior_keymap_init,
|
||||
&behavior_keymap_data,
|
||||
&behavior_keymap_config,
|
||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
|
||||
&behavior_keymap_driver_api);
|
||||
&behavior_keymap_driver_api);
|
||||
|
||||
89
app/src/behaviors/behavior_mod_tap.c
Normal file
89
app/src/behaviors/behavior_mod_tap.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT zmk_behavior_mod_tap
|
||||
|
||||
#include <device.h>
|
||||
#include <drivers/behavior.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include <zmk/events.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
struct behavior_mod_tap_config { };
|
||||
struct behavior_mod_tap_data {
|
||||
u16_t pending_press_positions;
|
||||
};
|
||||
|
||||
static int behavior_mod_tap_init(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
||||
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t mods, u32_t keycode)
|
||||
{
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
LOG_DBG("mods: %d, keycode: %d", mods, keycode);
|
||||
WRITE_BIT(data->pending_press_positions, position, true);
|
||||
return zmk_events_modifiers_pressed(mods);
|
||||
}
|
||||
|
||||
|
||||
// They keycode is passed by the "keymap" based on the parameter created as part of the assignment.
|
||||
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t mods, u32_t keycode)
|
||||
{
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
LOG_DBG("mods: %d, keycode: %d", mods, keycode);
|
||||
|
||||
zmk_events_modifiers_released(mods);
|
||||
if (data->pending_press_positions & BIT(position)) {
|
||||
zmk_events_keycode_pressed(keycode);
|
||||
k_msleep(10);
|
||||
zmk_events_keycode_released(keycode);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_keycode_pressed(struct device *dev, u32_t keycode)
|
||||
{
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
data->pending_press_positions = 0;
|
||||
LOG_DBG("pressing: %d", keycode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int on_keycode_released(struct device *dev, u32_t keycode)
|
||||
{
|
||||
LOG_DBG("releasing: %d", keycode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_mod_tap_driver_api = {
|
||||
// These callbacks are all optional, and define which kinds of events the behavior can handle.
|
||||
// They can reference local functions defined here, or shared event handlers.
|
||||
.binding_pressed = on_keymap_binding_pressed,
|
||||
.binding_released = on_keymap_binding_released,
|
||||
.keycode_pressed = on_keycode_pressed,
|
||||
.keycode_released = on_keycode_released
|
||||
// Other optional callbacks a behavior can implement
|
||||
// .on_mouse_moved
|
||||
// .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior
|
||||
};
|
||||
|
||||
|
||||
static const struct behavior_mod_tap_config behavior_mod_tap_config = {};
|
||||
|
||||
static struct behavior_mod_tap_data behavior_mod_tap_data;
|
||||
|
||||
DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_mod_tap_init,
|
||||
&behavior_mod_tap_data,
|
||||
&behavior_mod_tap_config,
|
||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
|
||||
&behavior_mod_tap_driver_api);
|
||||
@@ -21,7 +21,7 @@ static int behavior_reset_init(struct device *dev)
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int on_position_pressed(struct device *dev, u32_t _param1, u32_t _param2)
|
||||
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t _param1, u32_t _param2)
|
||||
{
|
||||
// TODO: Correct magic code for going into DFU?
|
||||
// See https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107
|
||||
@@ -29,14 +29,8 @@ static int on_position_pressed(struct device *dev, u32_t _param1, u32_t _param2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_position_released(struct device *dev, u32_t _param1, u32_t _param2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_reset_driver_api = {
|
||||
.position_pressed = on_position_pressed,
|
||||
.position_released = on_position_released
|
||||
.binding_pressed = on_keymap_binding_pressed,
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -5,63 +5,74 @@
|
||||
#include <zmk/events.h>
|
||||
#include <sys/util.h>
|
||||
|
||||
#define BINDINGS_NODE DT_CHOSEN(zmk_global_bindings)
|
||||
#define BINDING_COUNT DT_PROP_LEN(BINDINGS_NODE, bindings)
|
||||
#define DT_DRV_COMPAT zmk_behavior_global
|
||||
#define GLOBAL_BEHAVIOR_LEN DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT)
|
||||
|
||||
#define BINDING_GEN(idx,_) \
|
||||
{ .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(BINDINGS_NODE, bindings, idx)), \
|
||||
.param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(BINDINGS_NODE, bindings, idx, param1), (0), (DT_PHA_BY_IDX(BINDINGS_NODE, bindings, idx, param1))), \
|
||||
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(BINDINGS_NODE, bindings, idx, param2), (0), (DT_PHA_BY_IDX(BINDINGS_NODE, bindings, idx, param2))), \
|
||||
},
|
||||
|
||||
static const struct zmk_behavior_binding bindings[] =
|
||||
{ UTIL_LISTIFY(BINDING_COUNT, BINDING_GEN, 0) };
|
||||
#define LABEL_ENTRY(i) DT_INST_LABEL(i),
|
||||
static const char *global_behaviors[] = {
|
||||
DT_INST_FOREACH_STATUS_OKAY(LABEL_ENTRY)
|
||||
};
|
||||
|
||||
int zmk_events_position_pressed(u32_t position)
|
||||
{
|
||||
for (int i = 0; i < BINDING_COUNT; i++) {
|
||||
const struct zmk_behavior_binding *b = &bindings[i];
|
||||
struct device *dev = device_get_binding(b->behavior_dev);
|
||||
behavior_position_pressed(dev, position, 0);
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_position_pressed(dev, position);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
int zmk_events_position_released(u32_t position)
|
||||
{
|
||||
for (int i = 0; i < BINDING_COUNT; i++) {
|
||||
const struct zmk_behavior_binding *b = &bindings[i];
|
||||
struct device *dev = device_get_binding(b->behavior_dev);
|
||||
behavior_position_released(dev, position, 0);
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_position_released(dev, position);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
int zmk_events_keycode_pressed(u32_t keycode)
|
||||
{
|
||||
for (int i = 0; i < BINDING_COUNT; i++) {
|
||||
const struct zmk_behavior_binding *b = &bindings[i];
|
||||
struct device *dev = device_get_binding(b->behavior_dev);
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_keycode_pressed(dev, keycode);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
int zmk_events_keycode_released(u32_t keycode)
|
||||
{
|
||||
for (int i = 0; i < BINDING_COUNT; i++) {
|
||||
const struct zmk_behavior_binding *b = &bindings[i];
|
||||
struct device *dev = device_get_binding(b->behavior_dev);
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_keycode_released(dev, keycode);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
int zmk_events_mod_pressed(u32_t modifier)
|
||||
|
||||
int zmk_events_modifiers_pressed(zmk_mod_flags modifiers)
|
||||
{
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_modifiers_pressed(dev, modifiers);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
int zmk_events_mod_released(u32_t modifier)
|
||||
|
||||
int zmk_events_modifiers_released(zmk_mod_flags modifiers)
|
||||
{
|
||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) {
|
||||
const char* label = global_behaviors[i];
|
||||
struct device *dev = device_get_binding(label);
|
||||
behavior_modifiers_released(dev, modifiers);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
int zmk_events_consumer_key_pressed(u32_t usage)
|
||||
{
|
||||
return 0;
|
||||
@@ -69,4 +80,4 @@ int zmk_events_consumer_key_pressed(u32_t usage)
|
||||
int zmk_events_consumer_key_released(u32_t usage)
|
||||
{
|
||||
return 0;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
|
||||
#include <sys/util.h>
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#include <zmk/matrix.h>
|
||||
#include <zmk/keymap.h>
|
||||
#include <dt-bindings/zmk/matrix-transform.h>
|
||||
#include <drivers/behavior.h>
|
||||
#include <zmk/behavior.h>
|
||||
#include <sys/util.h>
|
||||
|
||||
static u32_t zmk_keymap_layer_state = 0;
|
||||
static u8_t zmk_keymap_layer_default = 0;
|
||||
@@ -88,10 +90,15 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed)
|
||||
LOG_DBG("position: %d, binding name: %s", position, binding->behavior_dev);
|
||||
|
||||
behavior = device_get_binding(binding->behavior_dev);
|
||||
|
||||
if (!behavior) {
|
||||
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
|
||||
continue;
|
||||
}
|
||||
if (pressed) {
|
||||
ret = behavior_position_pressed(behavior, binding->param1, binding->param2);
|
||||
ret = behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2);
|
||||
} else {
|
||||
ret = behavior_position_released(behavior, binding->param1, binding->param2);
|
||||
ret = behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user