@ -4,7 +4,12 @@
* SPDX - License - Identifier : MIT
* SPDX - License - Identifier : MIT
*/
*/
# define IS_BLE_CENTRAL \
( IS_ENABLED ( CONFIG_ZMK_SPLIT ) & & IS_ENABLED ( CONFIG_ZMK_BLE ) & & \
IS_ENABLED ( CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL ) )
# include <sys/util.h>
# include <sys/util.h>
# include <bluetooth/bluetooth.h>
# include <logging/log.h>
# include <logging/log.h>
LOG_MODULE_DECLARE ( zmk , CONFIG_ZMK_LOG_LEVEL ) ;
LOG_MODULE_DECLARE ( zmk , CONFIG_ZMK_LOG_LEVEL ) ;
@ -14,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
# include <drivers/behavior.h>
# include <drivers/behavior.h>
# include <zmk/behavior.h>
# include <zmk/behavior.h>
# if IS_BLE_CENTRAL
# include <zmk/split/bluetooth/central.h>
# endif
# include <zmk/event_manager.h>
# include <zmk/event_manager.h>
# include <zmk/events/position_state_changed.h>
# include <zmk/events/position_state_changed.h>
# include <zmk/events/layer_state_changed.h>
# include <zmk/events/layer_state_changed.h>
@ -152,7 +161,17 @@ const char *zmk_keymap_layer_label(uint8_t layer) {
return zmk_keymap_layer_names [ layer ] ;
return zmk_keymap_layer_names [ layer ] ;
}
}
int zmk_keymap_apply_position_state ( int layer , uint32_t position , bool pressed , int64_t timestamp ) {
int invoke_locally ( struct zmk_behavior_binding * binding , struct zmk_behavior_binding_event event ,
bool pressed ) {
if ( pressed ) {
return behavior_keymap_binding_pressed ( binding , event ) ;
} else {
return behavior_keymap_binding_released ( binding , event ) ;
}
}
int zmk_keymap_apply_position_state ( zmk_position_state_changed_source_t source , int layer ,
uint32_t position , bool pressed , int64_t timestamp ) {
// We want to make a copy of this, since it may be converted from
// We want to make a copy of this, since it may be converted from
// relative to absolute before being invoked
// relative to absolute before being invoked
struct zmk_behavior_binding binding = zmk_keymap [ layer ] [ position ] ;
struct zmk_behavior_binding binding = zmk_keymap [ layer ] [ position ] ;
@ -169,7 +188,7 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
behavior = device_get_binding ( binding . behavior_dev ) ;
behavior = device_get_binding ( binding . behavior_dev ) ;
if ( ! behavior ) {
if ( ! behavior ) {
LOG_DBG ( " No behavior assigned to %d on layer %d " , position , layer ) ;
LOG_WRN ( " No behavior assigned to %d on layer %d " , position , layer ) ;
return 1 ;
return 1 ;
}
}
@ -179,20 +198,44 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
return err ;
return err ;
}
}
if ( pressed ) {
enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL ;
return behavior_keymap_binding_pressed ( & binding , event ) ;
err = behavior_get_locality ( behavior , & locality ) ;
if ( err ) {
LOG_ERR ( " Failed to get behavior locality %d " , err ) ;
return err ;
}
switch ( locality ) {
case BEHAVIOR_LOCALITY_CENTRAL :
return invoke_locally ( & binding , event , pressed ) ;
case BEHAVIOR_LOCALITY_EVENT_SOURCE :
# if IS_BLE_CENTRAL
if ( ! bt_addr_le_cmp ( source , BT_ADDR_LE_NONE ) ) {
return invoke_locally ( & binding , event , pressed ) ;
} else {
} else {
return behavior_keymap_binding_released ( & binding , event ) ;
return zmk_split_bt_invoke_behavior ( source , & binding , event , pressed ) ;
}
}
# else
return invoke_locally ( & binding , event , pressed ) ;
# endif
case BEHAVIOR_LOCALITY_GLOBAL :
# if IS_BLE_CENTRAL
zmk_split_bt_invoke_behavior ( BT_ADDR_LE_ANY , & binding , event , pressed ) ;
# endif
return invoke_locally ( & binding , event , pressed ) ;
}
return - ENOTSUP ;
}
}
int zmk_keymap_position_state_changed ( uint32_t position , bool pressed , int64_t timestamp ) {
int zmk_keymap_position_state_changed ( zmk_position_state_changed_source_t source , uint32_t position ,
bool pressed , int64_t timestamp ) {
if ( pressed ) {
if ( pressed ) {
zmk_keymap_active_behavior_layer [ position ] = _zmk_keymap_layer_state ;
zmk_keymap_active_behavior_layer [ position ] = _zmk_keymap_layer_state ;
}
}
for ( int layer = ZMK_KEYMAP_LAYERS_LEN - 1 ; layer > = _zmk_keymap_layer_default ; layer - - ) {
for ( int layer = ZMK_KEYMAP_LAYERS_LEN - 1 ; layer > = _zmk_keymap_layer_default ; layer - - ) {
if ( zmk_keymap_layer_active_with_state ( layer , zmk_keymap_active_behavior_layer [ position ] ) ) {
if ( zmk_keymap_layer_active_with_state ( layer , zmk_keymap_active_behavior_layer [ position ] ) ) {
int ret = zmk_keymap_apply_position_state ( layer , position , pressed , timestamp ) ;
int ret = zmk_keymap_apply_position_state ( source , layer , position , pressed , timestamp ) ;
if ( ret > 0 ) {
if ( ret > 0 ) {
LOG_DBG ( " behavior processing to continue to next layer " ) ;
LOG_DBG ( " behavior processing to continue to next layer " ) ;
continue ;
continue ;
@ -249,7 +292,7 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
int keymap_listener ( const zmk_event_t * eh ) {
int keymap_listener ( const zmk_event_t * eh ) {
const struct zmk_position_state_changed * pos_ev ;
const struct zmk_position_state_changed * pos_ev ;
if ( ( pos_ev = as_zmk_position_state_changed ( eh ) ) ! = NULL ) {
if ( ( pos_ev = as_zmk_position_state_changed ( eh ) ) ! = NULL ) {
return zmk_keymap_position_state_changed ( pos_ev - > position , pos_ev - > state ,
return zmk_keymap_position_state_changed ( pos_ev - > source , pos_ev - > position , pos_ev - > state ,
pos_ev - > timestamp ) ;
pos_ev - > timestamp ) ;
}
}