4
4
* SPDX-License-Identifier: MIT
5
5
*/
6
6
7
+ #define IS_BLE_CENTRAL \
8
+ (IS_ENABLED(CONFIG_ZMK_SPLIT) && IS_ENABLED(CONFIG_ZMK_BLE) && \
9
+ IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL))
10
+
7
11
#include <sys/util.h>
12
+ #include <bluetooth/bluetooth.h>
8
13
#include <logging/log.h>
9
14
LOG_MODULE_DECLARE (zmk , CONFIG_ZMK_LOG_LEVEL );
10
15
@@ -14,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
14
19
#include <drivers/behavior.h>
15
20
#include <zmk/behavior.h>
16
21
22
+ #if IS_BLE_CENTRAL
23
+ #include <zmk/split/bluetooth/central.h>
24
+ #endif
25
+
17
26
#include <zmk/event_manager.h>
18
27
#include <zmk/events/position_state_changed.h>
19
28
#include <zmk/events/layer_state_changed.h>
@@ -160,7 +169,17 @@ const char *zmk_keymap_layer_label(uint8_t layer) {
160
169
return zmk_keymap_layer_names [layer ];
161
170
}
162
171
163
- int zmk_keymap_apply_position_state (int layer , uint32_t position , bool pressed , int64_t timestamp ) {
172
+ int invoke_locally (struct zmk_behavior_binding * binding , struct zmk_behavior_binding_event event ,
173
+ bool pressed ) {
174
+ if (pressed ) {
175
+ return behavior_keymap_binding_pressed (binding , event );
176
+ } else {
177
+ return behavior_keymap_binding_released (binding , event );
178
+ }
179
+ }
180
+
181
+ int zmk_keymap_apply_position_state (zmk_position_state_changed_source_t source , int layer ,
182
+ uint32_t position , bool pressed , int64_t timestamp ) {
164
183
struct zmk_behavior_binding * binding = & zmk_keymap [layer ][position ];
165
184
const struct device * behavior ;
166
185
struct zmk_behavior_binding_event event = {
@@ -175,24 +194,48 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
175
194
behavior = device_get_binding (binding -> behavior_dev );
176
195
177
196
if (!behavior ) {
178
- LOG_DBG ("No behavior assigned to %d on layer %d" , position , layer );
197
+ LOG_WRN ("No behavior assigned to %d on layer %d" , position , layer );
179
198
return 1 ;
180
199
}
181
200
182
- if (pressed ) {
183
- return behavior_keymap_binding_pressed (binding , event );
184
- } else {
185
- return behavior_keymap_binding_released (binding , event );
201
+ enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL ;
202
+ int err = behavior_get_locality (behavior , & locality );
203
+ if (err ) {
204
+ LOG_ERR ("Failed to get behavior locality %d" , err );
205
+ return err ;
186
206
}
207
+
208
+ switch (locality ) {
209
+ case BEHAVIOR_LOCALITY_CENTRAL :
210
+ return invoke_locally (binding , event , pressed );
211
+ case BEHAVIOR_LOCALITY_EVENT_SOURCE :
212
+ #if IS_BLE_CENTRAL
213
+ if (!bt_addr_le_cmp (source , BT_ADDR_LE_NONE )) {
214
+ return invoke_locally (binding , event , pressed );
215
+ } else {
216
+ return zmk_split_bt_invoke_behavior (source , binding , event , pressed );
217
+ }
218
+ #else
219
+ return invoke_locally (binding , event , pressed );
220
+ #endif
221
+ case BEHAVIOR_LOCALITY_GLOBAL :
222
+ #if IS_BLE_CENTRAL
223
+ zmk_split_bt_invoke_behavior (BT_ADDR_LE_ANY , binding , event , pressed );
224
+ #endif
225
+ return invoke_locally (binding , event , pressed );
226
+ }
227
+
228
+ return - ENOTSUP ;
187
229
}
188
230
189
- int zmk_keymap_position_state_changed (uint32_t position , bool pressed , int64_t timestamp ) {
231
+ int zmk_keymap_position_state_changed (zmk_position_state_changed_source_t source , uint32_t position ,
232
+ bool pressed , int64_t timestamp ) {
190
233
if (pressed ) {
191
234
zmk_keymap_active_behavior_layer [position ] = _zmk_keymap_layer_state ;
192
235
}
193
236
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1 ; layer >= _zmk_keymap_layer_default ; layer -- ) {
194
237
if (zmk_keymap_layer_active_with_state (layer , zmk_keymap_active_behavior_layer [position ])) {
195
- int ret = zmk_keymap_apply_position_state (layer , position , pressed , timestamp );
238
+ int ret = zmk_keymap_apply_position_state (source , layer , position , pressed , timestamp );
196
239
if (ret > 0 ) {
197
240
LOG_DBG ("behavior processing to continue to next layer" );
198
241
continue ;
@@ -249,7 +292,8 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
249
292
int keymap_listener (const struct zmk_event_header * eh ) {
250
293
if (is_position_state_changed (eh )) {
251
294
const struct position_state_changed * ev = cast_position_state_changed (eh );
252
- return zmk_keymap_position_state_changed (ev -> position , ev -> state , ev -> timestamp );
295
+ return zmk_keymap_position_state_changed (ev -> source , ev -> position , ev -> state ,
296
+ ev -> timestamp );
253
297
#if ZMK_KEYMAP_HAS_SENSORS
254
298
} else if (is_sensor_event (eh )) {
255
299
const struct sensor_event * ev = cast_sensor_event (eh );
0 commit comments