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
// We want to make a copy of this, since it may be converted from
165
184
// relative to absolute before being invoked
166
185
struct zmk_behavior_binding binding = zmk_keymap [layer ][position ];
@@ -177,7 +196,7 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
177
196
behavior = device_get_binding (binding .behavior_dev );
178
197
179
198
if (!behavior ) {
180
- LOG_DBG ("No behavior assigned to %d on layer %d" , position , layer );
199
+ LOG_WRN ("No behavior assigned to %d on layer %d" , position , layer );
181
200
return 1 ;
182
201
}
183
202
@@ -187,20 +206,44 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
187
206
return err ;
188
207
}
189
208
190
- if (pressed ) {
191
- return behavior_keymap_binding_pressed (& binding , event );
192
- } else {
193
- return behavior_keymap_binding_released (& binding , event );
209
+ enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL ;
210
+ err = behavior_get_locality (behavior , & locality );
211
+ if (err ) {
212
+ LOG_ERR ("Failed to get behavior locality %d" , err );
213
+ return err ;
214
+ }
215
+
216
+ switch (locality ) {
217
+ case BEHAVIOR_LOCALITY_CENTRAL :
218
+ return invoke_locally (& binding , event , pressed );
219
+ case BEHAVIOR_LOCALITY_EVENT_SOURCE :
220
+ #if IS_BLE_CENTRAL
221
+ if (!bt_addr_le_cmp (source , BT_ADDR_LE_NONE )) {
222
+ return invoke_locally (& binding , event , pressed );
223
+ } else {
224
+ return zmk_split_bt_invoke_behavior (source , & binding , event , pressed );
225
+ }
226
+ #else
227
+ return invoke_locally (& binding , event , pressed );
228
+ #endif
229
+ case BEHAVIOR_LOCALITY_GLOBAL :
230
+ #if IS_BLE_CENTRAL
231
+ zmk_split_bt_invoke_behavior (BT_ADDR_LE_ANY , & binding , event , pressed );
232
+ #endif
233
+ return invoke_locally (& binding , event , pressed );
194
234
}
235
+
236
+ return - ENOTSUP ;
195
237
}
196
238
197
- int zmk_keymap_position_state_changed (uint32_t position , bool pressed , int64_t timestamp ) {
239
+ int zmk_keymap_position_state_changed (zmk_position_state_changed_source_t source , uint32_t position ,
240
+ bool pressed , int64_t timestamp ) {
198
241
if (pressed ) {
199
242
zmk_keymap_active_behavior_layer [position ] = _zmk_keymap_layer_state ;
200
243
}
201
244
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1 ; layer >= _zmk_keymap_layer_default ; layer -- ) {
202
245
if (zmk_keymap_layer_active_with_state (layer , zmk_keymap_active_behavior_layer [position ])) {
203
- int ret = zmk_keymap_apply_position_state (layer , position , pressed , timestamp );
246
+ int ret = zmk_keymap_apply_position_state (source , layer , position , pressed , timestamp );
204
247
if (ret > 0 ) {
205
248
LOG_DBG ("behavior processing to continue to next layer" );
206
249
continue ;
@@ -257,7 +300,7 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
257
300
int keymap_listener (const zmk_event_t * eh ) {
258
301
const struct zmk_position_state_changed * pos_ev ;
259
302
if ((pos_ev = as_zmk_position_state_changed (eh )) != NULL ) {
260
- return zmk_keymap_position_state_changed (pos_ev -> position , pos_ev -> state ,
303
+ return zmk_keymap_position_state_changed (pos_ev -> source , pos_ev -> position , pos_ev -> state ,
261
304
pos_ev -> timestamp );
262
305
}
263
306
0 commit comments