2020-09-10 13:57:44 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
2020-05-04 05:49:33 +10:00
|
|
|
|
2020-06-22 11:43:44 +10:00
|
|
|
#include <sys/util.h>
|
2020-06-03 01:23:22 +10:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
2020-06-22 11:43:44 +10:00
|
|
|
|
|
|
|
#include <zmk/matrix.h>
|
2020-07-22 16:13:52 +10:00
|
|
|
#include <zmk/sensors.h>
|
2020-05-18 23:11:46 +10:00
|
|
|
#include <zmk/keymap.h>
|
2020-06-20 05:32:33 +10:00
|
|
|
#include <drivers/behavior.h>
|
2020-06-21 07:54:52 +10:00
|
|
|
#include <zmk/behavior.h>
|
2020-05-04 05:49:33 +10:00
|
|
|
|
2020-12-23 02:56:00 +11:00
|
|
|
#include <zmk/event_manager.h>
|
|
|
|
#include <zmk/events/position_state_changed.h>
|
|
|
|
#include <zmk/events/layer_state_changed.h>
|
|
|
|
#include <zmk/events/sensor_event.h>
|
2020-06-30 14:31:09 +10:00
|
|
|
|
2020-12-10 01:35:57 +11:00
|
|
|
static zmk_keymap_layers_state _zmk_keymap_layer_state = 0;
|
2020-12-03 03:41:57 +11:00
|
|
|
static uint8_t _zmk_keymap_layer_default = 0;
|
2020-05-11 07:13:24 +10:00
|
|
|
|
2020-08-07 13:28:34 +10:00
|
|
|
#define DT_DRV_COMPAT zmk_keymap
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define LAYER_CHILD_LEN(node) 1 +
|
2020-08-07 13:28:34 +10:00
|
|
|
#define ZMK_KEYMAP_NODE DT_DRV_INST(0)
|
2020-09-14 12:53:24 +10:00
|
|
|
#define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0)
|
2020-06-20 05:32:33 +10:00
|
|
|
|
|
|
|
#define LAYER_NODE(l) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, l)
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define _TRANSFORM_ENTRY(idx, layer) \
|
|
|
|
{ \
|
|
|
|
.behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, bindings, idx)), \
|
|
|
|
.param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param1), (0), \
|
|
|
|
(DT_PHA_BY_IDX(layer, bindings, idx, param1))), \
|
|
|
|
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, bindings, idx, param2), (0), \
|
|
|
|
(DT_PHA_BY_IDX(layer, bindings, idx, param2))), \
|
|
|
|
},
|
2020-06-16 07:03:34 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define TRANSFORMED_LAYER(node) {UTIL_LISTIFY(DT_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, node)},
|
2020-06-16 07:03:34 +10:00
|
|
|
|
2020-07-23 01:08:52 +10:00
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
2020-09-14 12:53:24 +10:00
|
|
|
#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \
|
|
|
|
{ \
|
|
|
|
.behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(layer, sensor_bindings, idx)), \
|
|
|
|
.param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param1), (0), \
|
|
|
|
(DT_PHA_BY_IDX(layer, sensor_bindings, idx, param1))), \
|
|
|
|
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param2), (0), \
|
|
|
|
(DT_PHA_BY_IDX(layer, sensor_bindings, idx, param2))), \
|
|
|
|
},
|
|
|
|
|
|
|
|
#define SENSOR_LAYER(node) \
|
|
|
|
COND_CODE_1( \
|
|
|
|
DT_NODE_HAS_PROP(node, sensor_bindings), \
|
|
|
|
({UTIL_LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, node)}), \
|
|
|
|
({})),
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-08-10 02:13:39 +10:00
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
2020-09-01 23:34:06 +10:00
|
|
|
// When a behavior handles a key position "down" event, we record the layer state
|
2020-08-10 02:13:39 +10:00
|
|
|
// here so that even if that layer is deactivated before the "up", event, we
|
|
|
|
// still send the release event to the behavior in that layer also.
|
2020-12-03 03:41:57 +11:00
|
|
|
static uint32_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN];
|
2020-08-10 02:13:39 +10:00
|
|
|
|
|
|
|
static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
|
2020-09-14 12:53:24 +10:00
|
|
|
DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)};
|
2020-08-10 02:13:39 +10:00
|
|
|
|
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN]
|
|
|
|
[ZMK_KEYMAP_SENSORS_LEN] = {
|
|
|
|
DT_INST_FOREACH_CHILD(0, SENSOR_LAYER)};
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-07-23 01:08:52 +10:00
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
static inline int set_layer_state(uint8_t layer, bool state) {
|
2020-12-10 01:35:57 +11:00
|
|
|
if (layer >= 32) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
WRITE_BIT(_zmk_keymap_layer_state, layer, state);
|
|
|
|
ZMK_EVENT_RAISE(create_layer_state_changed(layer, state));
|
2020-09-14 12:53:24 +10:00
|
|
|
return 0;
|
2020-12-10 01:35:57 +11:00
|
|
|
}
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t zmk_keymap_layer_default() { return _zmk_keymap_layer_default; }
|
2020-12-10 01:35:57 +11:00
|
|
|
|
|
|
|
zmk_keymap_layers_state zmk_keymap_layer_state() { return _zmk_keymap_layer_state; }
|
2020-08-19 04:02:24 +10:00
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
bool zmk_keymap_layer_active(uint8_t layer) {
|
2020-12-10 01:35:57 +11:00
|
|
|
return (_zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer));
|
2020-05-11 07:13:24 +10:00
|
|
|
};
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t zmk_keymap_highest_layer_active() {
|
|
|
|
for (uint8_t layer = 31; layer > 0; layer--) {
|
2020-12-10 01:35:57 +11:00
|
|
|
if (zmk_keymap_layer_active(layer)) {
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zmk_keymap_layer_default();
|
|
|
|
}
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_keymap_layer_activate(uint8_t layer) { return set_layer_state(layer, true); };
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_keymap_layer_deactivate(uint8_t layer) { return set_layer_state(layer, false); };
|
2020-05-11 07:13:24 +10:00
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_keymap_layer_toggle(uint8_t layer) {
|
2020-09-14 12:53:24 +10:00
|
|
|
if (zmk_keymap_layer_active(layer)) {
|
|
|
|
return zmk_keymap_layer_deactivate(layer);
|
|
|
|
}
|
2020-08-19 04:02:25 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
return zmk_keymap_layer_activate(layer);
|
2020-08-19 04:02:25 +10:00
|
|
|
};
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
bool is_active_layer(uint8_t layer, zmk_keymap_layers_state layer_state) {
|
2020-12-10 01:35:57 +11:00
|
|
|
return (layer_state & BIT(layer)) == BIT(layer) || layer == _zmk_keymap_layer_default;
|
2020-08-10 02:13:39 +10:00
|
|
|
}
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) {
|
2020-09-14 12:53:24 +10:00
|
|
|
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *behavior;
|
2020-10-11 08:32:53 +11:00
|
|
|
struct zmk_behavior_binding_event event = {
|
|
|
|
.layer = layer,
|
|
|
|
.position = position,
|
|
|
|
.timestamp = timestamp,
|
|
|
|
};
|
2020-09-01 23:34:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position,
|
|
|
|
log_strdup(binding->behavior_dev));
|
2020-09-01 23:34:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
behavior = device_get_binding(binding->behavior_dev);
|
2020-09-01 23:34:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (!behavior) {
|
|
|
|
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-09-01 23:34:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (pressed) {
|
2020-10-11 08:32:53 +11:00
|
|
|
return behavior_keymap_binding_pressed(binding, event);
|
2020-09-14 12:53:24 +10:00
|
|
|
} else {
|
2020-10-11 08:32:53 +11:00
|
|
|
return behavior_keymap_binding_released(binding, event);
|
2020-09-14 12:53:24 +10:00
|
|
|
}
|
2020-09-01 23:34:06 +10:00
|
|
|
}
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp) {
|
2020-11-12 02:40:13 +11:00
|
|
|
if (pressed) {
|
2020-12-10 01:35:57 +11:00
|
|
|
zmk_keymap_active_behavior_layer[position] = _zmk_keymap_layer_state;
|
2020-11-12 02:40:13 +11:00
|
|
|
}
|
2020-12-10 01:35:57 +11:00
|
|
|
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
|
2020-11-12 02:40:13 +11:00
|
|
|
if (is_active_layer(layer, zmk_keymap_active_behavior_layer[position])) {
|
2020-10-11 08:32:53 +11:00
|
|
|
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
|
2020-09-14 12:53:24 +10:00
|
|
|
if (ret > 0) {
|
|
|
|
LOG_DBG("behavior processing to continue to next layer");
|
|
|
|
continue;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
LOG_DBG("Behavior returned error: %d", ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
2020-05-04 05:49:33 +10:00
|
|
|
}
|
2020-06-30 14:31:09 +10:00
|
|
|
|
2020-07-23 01:08:52 +10:00
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
2020-12-11 06:31:51 +11:00
|
|
|
int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sensor,
|
|
|
|
int64_t timestamp) {
|
2020-12-10 01:35:57 +11:00
|
|
|
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
|
|
|
|
if (((_zmk_keymap_layer_state & BIT(layer)) == BIT(layer) ||
|
|
|
|
layer == _zmk_keymap_layer_default) &&
|
2020-09-14 12:53:24 +10:00
|
|
|
zmk_sensor_keymap[layer] != NULL) {
|
|
|
|
struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number];
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *behavior;
|
2020-09-14 12:53:24 +10:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
LOG_DBG("layer: %d sensor_number: %d, binding name: %s", layer, sensor_number,
|
|
|
|
log_strdup(binding->behavior_dev));
|
|
|
|
|
|
|
|
behavior = device_get_binding(binding->behavior_dev);
|
|
|
|
|
|
|
|
if (!behavior) {
|
|
|
|
LOG_DBG("No behavior assigned to %d on layer %d", sensor_number, layer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-12 08:06:25 +11:00
|
|
|
ret = behavior_sensor_keymap_binding_triggered(binding, sensor, timestamp);
|
2020-09-14 12:53:24 +10:00
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
LOG_DBG("behavior processing to continue to next layer");
|
|
|
|
continue;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
LOG_DBG("Behavior returned error: %d", ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
2020-07-23 00:10:04 +10:00
|
|
|
}
|
|
|
|
|
2020-07-23 01:08:52 +10:00
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int keymap_listener(const struct zmk_event_header *eh) {
|
|
|
|
if (is_position_state_changed(eh)) {
|
|
|
|
const struct position_state_changed *ev = cast_position_state_changed(eh);
|
2020-10-11 08:32:53 +11:00
|
|
|
return zmk_keymap_position_state_changed(ev->position, ev->state, ev->timestamp);
|
2020-07-23 01:08:52 +10:00
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
2020-09-14 12:53:24 +10:00
|
|
|
} else if (is_sensor_event(eh)) {
|
|
|
|
const struct sensor_event *ev = cast_sensor_event(eh);
|
2020-11-12 08:06:25 +11:00
|
|
|
return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor, ev->timestamp);
|
2020-07-23 01:08:52 +10:00
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|
2020-09-14 12:53:24 +10:00
|
|
|
}
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
return -ENOTSUP;
|
2020-06-30 14:31:09 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
ZMK_LISTENER(keymap, keymap_listener);
|
|
|
|
ZMK_SUBSCRIPTION(keymap, position_state_changed);
|
2020-07-23 01:08:52 +10:00
|
|
|
|
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
2020-07-22 16:13:52 +10:00
|
|
|
ZMK_SUBSCRIPTION(keymap, sensor_event);
|
2020-07-23 01:08:52 +10:00
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|