Skip to content

Commit

Permalink
refactor: Condition source props on CONFIG_ZMK_SPLIT
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar authored and petejohanson committed Sep 23, 2024
1 parent b249135 commit fb18a4d
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 34 deletions.
2 changes: 2 additions & 0 deletions app/include/zmk/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ struct zmk_behavior_binding_event {
int layer;
uint32_t position;
int64_t timestamp;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
};

/**
Expand Down
2 changes: 1 addition & 1 deletion app/include/zmk/behavior_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#include <stdint.h>
#include <zmk/behavior.h>

int zmk_behavior_queue_add(uint32_t position, uint8_t source,
int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event,
const struct zmk_behavior_binding behavior, bool press, uint32_t wait);
2 changes: 1 addition & 1 deletion app/src/behavior.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding,
case BEHAVIOR_LOCALITY_CENTRAL:
return invoke_locally(&binding, event, pressed);
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
#if ZMK_BLE_IS_CENTRAL
#if ZMK_BLE_IS_CENTRAL // source is a member of event because CONFIG_ZMK_SPLIT is enabled
if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) {
return invoke_locally(&binding, event, pressed);
} else {
Expand Down
20 changes: 17 additions & 3 deletions app/src/behavior_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

struct q_item {
uint32_t position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
struct zmk_behavior_binding binding;
bool press : 1;
uint32_t wait : 31;
Expand All @@ -34,7 +36,12 @@ static void behavior_queue_process_next(struct k_work *work) {
item.binding.param2);

struct zmk_behavior_binding_event event = {
.position = item.position, .timestamp = k_uptime_get(), .source = item.source};
.position = item.position,
.timestamp = k_uptime_get(),
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = item.source
#endif
};

if (item.press) {
zmk_behavior_invoke_binding(&item.binding, event, true);
Expand All @@ -51,10 +58,17 @@ static void behavior_queue_process_next(struct k_work *work) {
}
}

int zmk_behavior_queue_add(uint32_t position, uint8_t source,
int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event,
const struct zmk_behavior_binding binding, bool press, uint32_t wait) {
struct q_item item = {
.press = press, .binding = binding, .wait = wait, .position = position, .source = source};
.press = press,
.binding = binding,
.wait = wait,
.position = event->position,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = event->source,
#endif
};

const int ret = k_msgq_put(&zmk_behavior_queue_msgq, &item, K_NO_WAIT);
if (ret < 0) {
Expand Down
26 changes: 19 additions & 7 deletions app/src/behaviors/behavior_hold_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ struct behavior_hold_tap_data {
// this data is specific for each hold-tap
struct active_hold_tap {
int32_t position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
uint32_t param_hold;
uint32_t param_tap;
int64_t timestamp;
Expand Down Expand Up @@ -250,21 +252,22 @@ static struct active_hold_tap *find_hold_tap(uint32_t position) {
return NULL;
}

static struct active_hold_tap *store_hold_tap(uint32_t position, uint8_t source,
static struct active_hold_tap *store_hold_tap(struct zmk_behavior_binding_event *event,
uint32_t param_hold, uint32_t param_tap,
int64_t timestamp,
const struct behavior_hold_tap_config *config) {
for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) {
if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) {
continue;
}
active_hold_taps[i].position = position;
active_hold_taps[i].source = source;
active_hold_taps[i].position = event->position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
active_hold_taps[i].source = event->source;
#endif
active_hold_taps[i].status = STATUS_UNDECIDED;
active_hold_taps[i].config = config;
active_hold_taps[i].param_hold = param_hold;
active_hold_taps[i].param_tap = param_tap;
active_hold_taps[i].timestamp = timestamp;
active_hold_taps[i].timestamp = event->timestamp;
active_hold_taps[i].position_of_first_other_key_pressed = -1;
return &active_hold_taps[i];
}
Expand Down Expand Up @@ -402,7 +405,9 @@ static int press_hold_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = hold_tap->source,
#endif
};

struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev,
Expand All @@ -414,7 +419,9 @@ static int press_tap_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = hold_tap->source,
#endif
};

struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev,
Expand All @@ -427,7 +434,9 @@ static int release_hold_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = hold_tap->source,
#endif
};

struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev,
Expand All @@ -439,7 +448,9 @@ static int release_tap_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = hold_tap->source,
#endif
};

struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev,
Expand Down Expand Up @@ -603,8 +614,9 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
return ZMK_BEHAVIOR_OPAQUE;
}

struct active_hold_tap *hold_tap = store_hold_tap(event.position, event.source, binding->param1,
binding->param2, event.timestamp, cfg);
struct active_hold_tap *hold_tap =
store_hold_tap(&event, binding->param1, binding->param2, cfg);

if (hold_tap == NULL) {
LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?",
ZMK_BHV_HOLD_TAP_MAX_HELD);
Expand Down
14 changes: 7 additions & 7 deletions app/src/behaviors/behavior_macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static void replace_params(struct behavior_macro_trigger_state *state,
state->param2_source = PARAM_SOURCE_BINDING;
}

static void queue_macro(uint32_t position, uint8_t source,
static void queue_macro(struct zmk_behavior_binding_event *event,
const struct zmk_behavior_binding bindings[],
struct behavior_macro_trigger_state state,
const struct zmk_behavior_binding *macro_binding) {
Expand All @@ -170,14 +170,14 @@ static void queue_macro(uint32_t position, uint8_t source,

switch (state.mode) {
case MACRO_MODE_TAP:
zmk_behavior_queue_add(position, source, binding, true, state.tap_ms);
zmk_behavior_queue_add(position, source, binding, false, state.wait_ms);
zmk_behavior_queue_add(event, binding, true, state.tap_ms);
zmk_behavior_queue_add(event, binding, false, state.wait_ms);
break;
case MACRO_MODE_PRESS:
zmk_behavior_queue_add(position, source, binding, true, state.wait_ms);
zmk_behavior_queue_add(event, binding, true, state.wait_ms);
break;
case MACRO_MODE_RELEASE:
zmk_behavior_queue_add(position, source, binding, false, state.wait_ms);
zmk_behavior_queue_add(event, binding, false, state.wait_ms);
break;
default:
LOG_ERR("Unknown macro mode: %d", state.mode);
Expand All @@ -198,7 +198,7 @@ static int on_macro_binding_pressed(struct zmk_behavior_binding *binding,
.start_index = 0,
.count = state->press_bindings_count};

queue_macro(event.position, event.source, cfg->bindings, trigger_state, binding);
queue_macro(&event, cfg->bindings, trigger_state, binding);

return ZMK_BEHAVIOR_OPAQUE;
}
Expand All @@ -209,7 +209,7 @@ static int on_macro_binding_released(struct zmk_behavior_binding *binding,
const struct behavior_macro_config *cfg = dev->config;
struct behavior_macro_state *state = dev->data;

queue_macro(event.position, event.source, cfg->bindings, state->release_state, binding);
queue_macro(&event, cfg->bindings, state->release_state, binding);

return ZMK_BEHAVIOR_OPAQUE;
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/behaviors/behavior_sensor_rotate_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi

LOG_DBG("Sensor binding: %s", binding->behavior_dev);

#if IS_ENABLED(CONFIG_ZMK_SPLIT)
// set this value so that it always triggers on central, can be handled more properly later
event.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL;
#endif

for (int i = 0; i < triggers; i++) {
zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
triggered_binding, true, cfg->tap_ms);
zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
triggered_binding, false, 0);
zmk_behavior_queue_add(&event, triggered_binding, true, cfg->tap_ms);
zmk_behavior_queue_add(&event, triggered_binding, false, 0);
}

return ZMK_BEHAVIOR_OPAQUE;
Expand Down
17 changes: 12 additions & 5 deletions app/src/behaviors/behavior_sticky_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ struct behavior_sticky_key_config {

struct active_sticky_key {
uint32_t position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
uint32_t param1;
uint32_t param2;
const struct behavior_sticky_key_config *config;
Expand All @@ -56,7 +58,7 @@ struct active_sticky_key {

struct active_sticky_key active_sticky_keys[ZMK_BHV_STICKY_KEY_MAX_HELD] = {};

static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t source,
static struct active_sticky_key *store_sticky_key(struct zmk_behavior_binding_event *event,
uint32_t param1, uint32_t param2,
const struct behavior_sticky_key_config *config) {
for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) {
Expand All @@ -65,8 +67,10 @@ static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t sou
sticky_key->timer_cancelled) {
continue;
}
sticky_key->position = position;
sticky_key->source = source;
sticky_key->position = event->position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
sticky_key->source = event->source;
#endif
sticky_key->param1 = param1;
sticky_key->param2 = param2;
sticky_key->config = config;
Expand Down Expand Up @@ -103,7 +107,9 @@ static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key
struct zmk_behavior_binding_event event = {
.position = sticky_key->position,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = sticky_key->source,
#endif
};
return zmk_behavior_invoke_binding(&binding, event, true);
}
Expand All @@ -118,7 +124,9 @@ static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_k
struct zmk_behavior_binding_event event = {
.position = sticky_key->position,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = sticky_key->source,
#endif
};

clear_sticky_key(sticky_key);
Expand Down Expand Up @@ -153,8 +161,7 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding,
stop_timer(sticky_key);
release_sticky_key_behavior(sticky_key, event.timestamp);
}
sticky_key =
store_sticky_key(event.position, event.source, binding->param1, binding->param2, cfg);
sticky_key = store_sticky_key(&event, binding->param1, binding->param2, cfg);
if (sticky_key == NULL) {
LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?",
ZMK_BHV_STICKY_KEY_MAX_HELD);
Expand Down
16 changes: 12 additions & 4 deletions app/src/behaviors/behavior_tap_dance.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ struct active_tap_dance {
// Tap Dance Data
int counter;
uint32_t position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
uint32_t param1;
uint32_t param2;
bool is_pressed;
Expand All @@ -60,15 +62,17 @@ static struct active_tap_dance *find_tap_dance(uint32_t position) {
return NULL;
}

static int new_tap_dance(uint32_t position, uint8_t source,
static int new_tap_dance(struct zmk_behavior_binding_event *event,
const struct behavior_tap_dance_config *config,
struct active_tap_dance **tap_dance) {
for (int i = 0; i < ZMK_BHV_TAP_DANCE_MAX_HELD; i++) {
struct active_tap_dance *const ref_dance = &active_tap_dances[i];
if (ref_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) {
ref_dance->counter = 0;
ref_dance->position = position;
ref_dance->source = source;
ref_dance->position = event->position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
ref_dance->source = event->source;
#endif
ref_dance->config = config;
ref_dance->release_at = 0;
ref_dance->is_pressed = true;
Expand Down Expand Up @@ -111,7 +115,9 @@ static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance, i
struct zmk_behavior_binding_event event = {
.position = tap_dance->position,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = tap_dance->source,
#endif
};
return zmk_behavior_invoke_binding(&binding, event, true);
}
Expand All @@ -122,7 +128,9 @@ static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance,
struct zmk_behavior_binding_event event = {
.position = tap_dance->position,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = tap_dance->source,
#endif
};
clear_tap_dance(tap_dance);
return zmk_behavior_invoke_binding(&binding, event, false);
Expand All @@ -135,7 +143,7 @@ static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
struct active_tap_dance *tap_dance;
tap_dance = find_tap_dance(event.position);
if (tap_dance == NULL) {
if (new_tap_dance(event.position, event.source, cfg, &tap_dance) == -ENOMEM) {
if (new_tap_dance(&event, cfg, &tap_dance) == -ENOMEM) {
LOG_ERR("Unable to create new tap dance. Insufficient space in active_tap_dances[].");
return ZMK_BEHAVIOR_OPAQUE;
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ static int release_pressed_keys() {
static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestamp) {
struct zmk_behavior_binding_event event = {
.position = combo->virtual_key_position,
.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
#endif
};

last_combo_timestamp = timestamp;
Expand All @@ -303,8 +305,10 @@ static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestam
static inline int release_combo_behavior(struct combo_cfg *combo, int32_t timestamp) {
struct zmk_behavior_binding_event event = {
.position = combo->virtual_key_position,
.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
#endif
};

return zmk_behavior_invoke_binding(&combo->behavior, event, false);
Expand Down
2 changes: 2 additions & 0 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ int zmk_keymap_apply_position_state(uint8_t source, zmk_keymap_layer_id_t layer_
.layer = layer_id,
.position = position,
.timestamp = timestamp,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = source,
#endif
};

LOG_DBG("layer_id: %d position: %d, binding name: %s", layer_id, position,
Expand Down

0 comments on commit fb18a4d

Please sign in to comment.