Skip to content

Commit

Permalink
Apply masked mods PR zmkfirmware#1114 with urob's tweaks
Browse files Browse the repository at this point in the history
Co-authored by: Kostas Karachalios <vrinek@hey.com>
Co-authored by: urob <978080+urob@users.noreply.github.com>
  • Loading branch information
caksoylar committed Jul 30, 2022
1 parent 33bde54 commit 0bc7f5e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/dts/bindings/behaviors/zmk,behavior-mod-morph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ properties:
mods:
type: int
required: true
masked_mods:
type: int
required: false
3 changes: 2 additions & 1 deletion app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ int zmk_hid_register_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_unregister_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers);
int zmk_hid_implicit_modifiers_release();

int zmk_hid_masked_modifiers_set(zmk_mod_flags_t masked_modifiers);
int zmk_hid_masked_modifiers_clear();
int zmk_hid_keyboard_press(zmk_key_t key);
int zmk_hid_keyboard_release(zmk_key_t key);
void zmk_hid_keyboard_clear();
Expand Down
6 changes: 6 additions & 0 deletions app/src/behaviors/behavior_mod_morph.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct behavior_mod_morph_config {
struct zmk_behavior_binding normal_binding;
struct zmk_behavior_binding morph_binding;
zmk_mod_flags_t mods;
zmk_mod_flags_t masked_mods;
};

struct behavior_mod_morph_data {
Expand All @@ -45,6 +46,8 @@ static int on_mod_morph_binding_pressed(struct zmk_behavior_binding *binding,
}

if (zmk_hid_get_explicit_mods() & cfg->mods) {
zmk_mod_flags_t trigger_mods = zmk_hid_get_explicit_mods() & cfg->mods;
zmk_hid_masked_modifiers_set(cfg->masked_mods);
data->pressed_binding = (struct zmk_behavior_binding *)&cfg->morph_binding;
} else {
data->pressed_binding = (struct zmk_behavior_binding *)&cfg->normal_binding;
Expand All @@ -64,6 +67,7 @@ static int on_mod_morph_binding_released(struct zmk_behavior_binding *binding,

struct zmk_behavior_binding *pressed_binding = data->pressed_binding;
data->pressed_binding = NULL;
zmk_hid_masked_modifiers_clear();
return behavior_keymap_binding_released(pressed_binding, event);
}

Expand All @@ -88,6 +92,8 @@ static int behavior_mod_morph_init(const struct device *dev) { return 0; }
.normal_binding = _TRANSFORM_ENTRY(0, n), \
.morph_binding = _TRANSFORM_ENTRY(1, n), \
.mods = DT_INST_PROP(n, mods), \
.masked_mods = COND_CODE_0(DT_INST_NODE_HAS_PROP(n, masked_mods), (0), \
(DT_INST_PROP(n, masked_mods))), \
}; \
static struct behavior_mod_morph_data behavior_mod_morph_data_##n = {}; \
DEVICE_DT_INST_DEFINE(n, behavior_mod_morph_init, NULL, &behavior_mod_morph_data_##n, \
Expand Down
24 changes: 21 additions & 3 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body =
// Only release the modifier if the count is 0.
static int explicit_modifier_counts[8] = {0, 0, 0, 0, 0, 0, 0, 0};
static zmk_mod_flags_t explicit_modifiers = 0;
static zmk_mod_flags_t implicit_modifiers = 0;
static zmk_mod_flags_t masked_modifiers = 0;

#define SET_MODIFIERS(mods) \
{ \
keyboard_report.body.modifiers = mods; \
keyboard_report.body.modifiers = (mods & ~masked_modifiers) | implicit_modifiers; \
LOG_DBG("Modifiers set to 0x%02X", keyboard_report.body.modifiers); \
}

Expand Down Expand Up @@ -158,13 +160,29 @@ static inline int check_keyboard_usage(zmk_key_t usage) {
} \
}

int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers) {
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t new_implicit_modifiers) {
implicit_modifiers = new_implicit_modifiers;
zmk_mod_flags_t current = GET_MODIFIERS;
SET_MODIFIERS(explicit_modifiers | implicit_modifiers);
SET_MODIFIERS(explicit_modifiers);
return current == GET_MODIFIERS ? 0 : 1;
}

int zmk_hid_implicit_modifiers_release() {
implicit_modifiers = 0;
zmk_mod_flags_t current = GET_MODIFIERS;
SET_MODIFIERS(explicit_modifiers);
return current == GET_MODIFIERS ? 0 : 1;
}

int zmk_hid_masked_modifiers_set(zmk_mod_flags_t new_masked_modifiers) {
masked_modifiers = new_masked_modifiers;
zmk_mod_flags_t current = GET_MODIFIERS;
SET_MODIFIERS(explicit_modifiers);
return current == GET_MODIFIERS ? 0 : 1;
}

int zmk_hid_masked_modifiers_clear() {
masked_modifiers = 0;
zmk_mod_flags_t current = GET_MODIFIERS;
SET_MODIFIERS(explicit_modifiers);
return current == GET_MODIFIERS ? 0 : 1;
Expand Down

0 comments on commit 0bc7f5e

Please sign in to comment.