From 223edf05ad08938b066f9187668ebfae43c5e91a Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 21 Jun 2020 21:43:44 -0400 Subject: [PATCH] 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. --- app/CMakeLists.txt | 1 + app/boards/native_posix.overlay | 9 +- app/boards/shields/kyria/kyria.overlay | 6 - .../keymap/keymap.overlay | 5 - app/dts/behaviors/hid.dtsi | 2 +- app/dts/behaviors/keymap.dtsi | 2 +- app/dts/behaviors/mod_tap.dtsi | 9 ++ .../behaviors/zmk,behavior-mod-tap.yaml | 8 ++ app/dts/bindings/zmk,global-bindings.yaml | 9 -- app/include/drivers/behavior.h | 119 ++++++++++++++++-- app/include/zmk/events.h | 6 +- app/include/zmk/keymap.h | 8 -- app/include/zmk/matrix.h | 2 + app/src/behaviors/behavior_hid.c | 20 ++- app/src/behaviors/behavior_key_press.c | 12 +- app/src/behaviors/behavior_keymap.c | 8 +- app/src/behaviors/behavior_mod_tap.c | 89 +++++++++++++ app/src/behaviors/behavior_reset.c | 10 +- app/src/events.c | 69 +++++----- app/src/keymap.c | 13 +- 20 files changed, 307 insertions(+), 100 deletions(-) create mode 100644 app/dts/behaviors/mod_tap.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml delete mode 100644 app/dts/bindings/zmk,global-bindings.yaml create mode 100644 app/src/behaviors/behavior_mod_tap.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index ff534fe2..e91bd58a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources(app PRIVATE src/behaviors/behavior_keymap.c) target_sources(app PRIVATE src/behaviors/behavior_hid.c) target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c) +target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c) diff --git a/app/boards/native_posix.overlay b/app/boards/native_posix.overlay index 3f2eead1..80047054 100644 --- a/app/boards/native_posix.overlay +++ b/app/boards/native_posix.overlay @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,12 +10,6 @@ chosen { zmk,kscan = &kscan0; zmk,keymap = &keymap0; - zmk,global_bindings = &bindings; - }; - - bindings: global_bindings { - compatible = "zmk,global-bindings"; - bindings = <&keymap_behavior &hid_behavior>; }; kscan0: kscan_0 { @@ -70,7 +65,7 @@ >; bindings = < - &kp 5 &kp 0 &kp 10 &kp 11 + &kp 5 &mt MOD_LSFT KC_B &kp 10 &kp 11 &kp 1 &kp 2 &kp 4 &kp 89>; }; diff --git a/app/boards/shields/kyria/kyria.overlay b/app/boards/shields/kyria/kyria.overlay index 4140fcfc..d560ed69 100644 --- a/app/boards/shields/kyria/kyria.overlay +++ b/app/boards/shields/kyria/kyria.overlay @@ -12,12 +12,6 @@ chosen { zmk,kscan = &kscan0; zmk,matrix_transform = &default_transform; - zmk,global_bindings = &bindings; - }; - - bindings: global_bindings { - compatible = "zmk,global-bindings"; - bindings = <&keymap_behavior &hid_behavior>; }; kscan0: kscan_comp { diff --git a/app/boards/shields/petejohanson_handwire/keymap/keymap.overlay b/app/boards/shields/petejohanson_handwire/keymap/keymap.overlay index 5ae78cf8..1a70ff4c 100644 --- a/app/boards/shields/petejohanson_handwire/keymap/keymap.overlay +++ b/app/boards/shields/petejohanson_handwire/keymap/keymap.overlay @@ -7,13 +7,8 @@ / { chosen { zmk,keymap = &keymap0; - zmk,global_bindings = &bindings; }; - bindings: global_bindings { - compatible = "zmk,global-bindings"; - bindings = <&reset>; - }; keymap0: keymap { compatible = "zmk,keymap"; label ="Default keymap"; diff --git a/app/dts/behaviors/hid.dtsi b/app/dts/behaviors/hid.dtsi index d859c378..30ebb090 100644 --- a/app/dts/behaviors/hid.dtsi +++ b/app/dts/behaviors/hid.dtsi @@ -1,7 +1,7 @@ / { behaviors { hid_behavior: behavior_hid { - compatible = "zmk,behavior-hid"; + compatible = "zmk,behavior-hid", "zmk,behavior-global"; label = "HID"; #binding-cells = <0>; }; diff --git a/app/dts/behaviors/keymap.dtsi b/app/dts/behaviors/keymap.dtsi index 87b2f41c..ec434d5f 100644 --- a/app/dts/behaviors/keymap.dtsi +++ b/app/dts/behaviors/keymap.dtsi @@ -1,7 +1,7 @@ / { behaviors { keymap_behavior: behavior_keymap { - compatible = "zmk,behavior-keymap"; + compatible = "zmk,behavior-keymap", "zmk,behavior-global"; label = "KEYMAP"; #binding-cells = <0>; }; diff --git a/app/dts/behaviors/mod_tap.dtsi b/app/dts/behaviors/mod_tap.dtsi new file mode 100644 index 00000000..1be7db6f --- /dev/null +++ b/app/dts/behaviors/mod_tap.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + mt: behavior_mod_tap { + compatible = "zmk,behavior-mod-tap", "zmk,behavior-global"; + label = "MOD_TAP"; + #binding-cells = <2>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml b/app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml new file mode 100644 index 00000000..7911082f --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-mod-tap.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2020, Pete Johanson +# SPDX-License-Identifier: MIT + +description: Mod-Tap Beavhior + +compatible: "zmk,behavior-mod-tap" + +include: two_param.yaml diff --git a/app/dts/bindings/zmk,global-bindings.yaml b/app/dts/bindings/zmk,global-bindings.yaml deleted file mode 100644 index 69e79cda..00000000 --- a/app/dts/bindings/zmk,global-bindings.yaml +++ /dev/null @@ -1,9 +0,0 @@ -description: | - Specify the the global behaviors bound to state changes - -compatible: "zmk,global-bindings" - -properties: - bindings: - type: phandles - required: true diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index f5f5f537..b3c6ad2c 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -22,30 +23,36 @@ extern "C" { * (Internal use only.) */ -typedef int (*behavior_position_callback_t)(struct device *dev, u32_t param1, u32_t param2); +typedef int (*behavior_position_callback_t)(struct device *dev, u32_t position); +typedef int (*behavior_keymap_binding_callback_t)(struct device *dev, u32_t position, u32_t param1, u32_t param2); typedef int (*behavior_keycode_callback_t)(struct device *dev, u32_t keycode); +typedef int (*behavior_modifiers_callback_t)(struct device *dev, zmk_mod_flags modifiers); __subsystem struct behavior_driver_api { behavior_position_callback_t position_pressed; behavior_position_callback_t position_released; + behavior_keymap_binding_callback_t binding_pressed; + behavior_keymap_binding_callback_t binding_released; behavior_keycode_callback_t keycode_pressed; behavior_keycode_callback_t keycode_released; + behavior_modifiers_callback_t modifiers_pressed; + behavior_modifiers_callback_t modifiers_released; }; /** * @endcond */ /** - * @brief Handle the assigned position being pressed + * @brief Handle the key position being pressed * @param dev Pointer to the device structure for the driver instance. - * @param param1 User parameter specified at time of behavior assignment. + * @param position They key position that was pressed * * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_position_pressed(struct device *dev, u32_t param1, u32_t param2); +__syscall int behavior_position_pressed(struct device *dev, u32_t position); -static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t param1, u32_t param2) +static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t position) { const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; @@ -54,7 +61,52 @@ static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t par return -ENOTSUP; } - return api->position_pressed(dev, param1, param2); + return api->position_pressed(dev, position); +} + +/** + * @brief Handle the key position being released + * @param dev Pointer to the device structure for the driver instance. + * @param position They key position that was released + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_position_released(struct device *dev, u32_t position); + +static inline int z_impl_behavior_position_released(struct device *dev, u32_t position) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->position_released == NULL) { + return -ENOTSUP; + } + + return api->position_released(dev, position); +} + +/** + * @brief Handle the keymap binding being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param param1 User parameter specified at time of behavior binding. + * @param param2 User parameter specified at time of behavior binding. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_keymap_binding_pressed(struct device *dev, u32_t position, u32_t param1, u32_t param2); + +static inline int z_impl_behavior_keymap_binding_pressed(struct device *dev, u32_t position, u32_t param1, u32_t param2) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->binding_pressed == NULL) { + return -ENOTSUP; + } + + return api->binding_pressed(dev, position, param1, param2); } /** @@ -65,20 +117,21 @@ static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t par * @retval 0 If successful. * @retval Negative errno code if failure. */ -__syscall int behavior_position_released(struct device *dev, u32_t param1, u32_t param2); +__syscall int behavior_keymap_binding_released(struct device *dev, u32_t position, u32_t param1, u32_t param2); -static inline int z_impl_behavior_position_released(struct device *dev, u32_t param1, u32_t param2) +static inline int z_impl_behavior_keymap_binding_released(struct device *dev, u32_t position, u32_t param1, u32_t param2) { const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; - if (api->position_released == NULL) { + if (api->binding_released == NULL) { return -ENOTSUP; } - return api->position_released(dev, param1, param2); + return api->binding_released(dev, position, param1, param2); } + /** * @brief Handle the keycode being pressed * @param dev Pointer to the device structure for the driver instance. @@ -124,6 +177,52 @@ static inline int z_impl_behavior_keycode_released(struct device *dev, u32_t key return api->keycode_released(dev, keycode); } + +/** + * @brief Handle the keycode being pressed + * @param dev Pointer to the device structure for the driver instance. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers); + +static inline int z_impl_behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->modifiers_pressed == NULL) { + return -ENOTSUP; + } + + return api->modifiers_pressed(dev, modifiers); +} + + +/** + * @brief Handle the keycode being released + * @param dev Pointer to the device structure for the driver instance. + * @param keycode The keycode that is being pressed. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +__syscall int behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers); + +static inline int z_impl_behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers) +{ + const struct behavior_driver_api *api = + (const struct behavior_driver_api *)dev->driver_api; + + if (api->modifiers_released == NULL) { + return -ENOTSUP; + } + + return api->modifiers_released(dev, modifiers); +} + #ifdef __cplusplus } #endif diff --git a/app/include/zmk/events.h b/app/include/zmk/events.h index ac4815a9..8d1ae6af 100644 --- a/app/include/zmk/events.h +++ b/app/include/zmk/events.h @@ -1,11 +1,13 @@ #pragma once +#include + int zmk_events_position_pressed(u32_t position); int zmk_events_position_released(u32_t position); int zmk_events_keycode_pressed(u32_t keycode); int zmk_events_keycode_released(u32_t keycode); -int zmk_events_mod_pressed(u32_t modifier); -int zmk_events_mod_released(u32_t modifier); +int zmk_events_modifiers_pressed(zmk_mod_flags modifiers); +int zmk_events_modifiers_released(zmk_mod_flags modifiers); int zmk_events_consumer_key_pressed(u32_t usage); int zmk_events_consumer_key_released(u32_t usage); diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index cf38904a..38c1249d 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -1,13 +1,5 @@ #pragma once -#include -#include -#include -#include - -#include -// #include - bool zmk_keymap_layer_activate(u8_t layer); bool zmk_keymap_layer_deactivate(u8_t layer); diff --git a/app/include/zmk/matrix.h b/app/include/zmk/matrix.h index 783c98e2..9cabb7e4 100644 --- a/app/include/zmk/matrix.h +++ b/app/include/zmk/matrix.h @@ -1,5 +1,7 @@ #pragma once +#include + #define ZMK_MATRIX_NODE_ID DT_CHOSEN(zmk_kscan) #if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,row_gpios) diff --git a/app/src/behaviors/behavior_hid.c b/app/src/behaviors/behavior_hid.c index 2779568e..0aeade51 100644 --- a/app/src/behaviors/behavior_hid.c +++ b/app/src/behaviors/behavior_hid.c @@ -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 }; diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 3c57c513..1b3e670f 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -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 diff --git a/app/src/behaviors/behavior_keymap.c b/app/src/behaviors/behavior_keymap.c index 9369b404..7a06b2fd 100644 --- a/app/src/behaviors/behavior_keymap.c +++ b/app/src/behaviors/behavior_keymap.c @@ -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); \ No newline at end of file + &behavior_keymap_driver_api); diff --git a/app/src/behaviors/behavior_mod_tap.c b/app/src/behaviors/behavior_mod_tap.c new file mode 100644 index 00000000..7684c298 --- /dev/null +++ b/app/src/behaviors/behavior_mod_tap.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_mod_tap + +#include +#include +#include + +#include + +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); diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 1609fad6..44cbc219 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -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, }; diff --git a/app/src/events.c b/app/src/events.c index 73c0e291..376f370a 100644 --- a/app/src/events.c +++ b/app/src/events.c @@ -5,63 +5,74 @@ #include #include -#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; -}; \ No newline at end of file +}; diff --git a/app/src/keymap.c b/app/src/keymap.c index 6ee61ec3..e14626a3 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -1,11 +1,13 @@ +#include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include #include #include #include #include -#include 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); }