summaryrefslogtreecommitdiff
path: root/app/src/keymap.c
blob: ee6e370d2b42b6a108613b4e630fbb9577101055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

#include <sys/util.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <zmk/matrix.h>
#include <zmk/sensors.h>
#include <zmk/keymap.h>
#include <dt-bindings/zmk/matrix-transform.h>
#include <drivers/behavior.h>
#include <zmk/behavior.h>

#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
#include <zmk/events/sensor-event.h>

static u32_t zmk_keymap_layer_state = 0;
static u8_t zmk_keymap_layer_default = 0;

#define DT_DRV_COMPAT zmk_keymap

#define LAYER_CHILD_LEN(node) 1+
#define ZMK_KEYMAP_NODE DT_DRV_INST(0)
#define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0) 

#define LAYER_NODE(l) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, l)

#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))), \
	},

#define TRANSFORMED_LAYER(node) \
  { UTIL_LISTIFY(DT_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, node) },


#if ZMK_KEYMAP_HAS_SENSORS
#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) }), \
		({})),

#endif /* ZMK_KEYMAP_HAS_SENSORS */

// State

// When a behavior handles a key position "down" event, we record that layer
// 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.
static u8_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN];

static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
	DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)
};

#if ZMK_KEYMAP_HAS_SENSORS

static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_SENSORS_LEN] = {
	DT_INST_FOREACH_CHILD(0, SENSOR_LAYER)
};

#endif /* ZMK_KEYMAP_HAS_SENSORS */

#define SET_LAYER_STATE(layer, state)                \
	if (layer >= 32)                                 \
	{                                                \
		return -EINVAL;                              \
	}                                                \
	WRITE_BIT(zmk_keymap_layer_state, layer, state); \
	return 0;

bool zmk_keymap_layer_active(u8_t layer)
{
	return (zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer));
};

int zmk_keymap_layer_activate(u8_t layer)
{
	SET_LAYER_STATE(layer, true);
};

int zmk_keymap_layer_deactivate(u8_t layer)
{
	SET_LAYER_STATE(layer, false);
};

int zmk_keymap_layer_toggle(u8_t layer)
{
	if (zmk_keymap_layer_active(layer))
	{
		return zmk_keymap_layer_deactivate(layer);
	}

  	return zmk_keymap_layer_activate(layer);
};

bool is_active_position(u32_t position, u8_t layer)
{
	return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer)
		|| layer == zmk_keymap_layer_default
		|| zmk_keymap_active_behavior_layer[position] == layer;
}

int zmk_keymap_position_state_changed(u32_t position, bool pressed)
{
	for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--)
	{
		if (is_active_position(position, layer))
		{
			struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
			struct device *behavior;
			int ret;

			LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, log_strdup(binding->behavior_dev));

			behavior = device_get_binding(binding->behavior_dev);

			if (!behavior) {
				LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
				continue;
			}
			if (pressed) {
				ret = behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2);
			} else {
				ret = behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2);
			}
			

			if (ret > 0) {
				LOG_DBG("behavior processing to continue to next layer");
				continue;
			} else if (ret < 0) {
				LOG_DBG("Behavior returned error: %d", ret);
				zmk_keymap_active_behavior_layer[position] = 0;
				return ret; 
			} else {
				zmk_keymap_active_behavior_layer[position] = pressed ? layer : 0;
				return ret;
			}
		}
	}

	return -ENOTSUP;
}

#if ZMK_KEYMAP_HAS_SENSORS
int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor)
{
	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) && zmk_sensor_keymap[layer] != NULL)
		{
			struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number];
			struct device *behavior;
			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;
			}
			
			ret = behavior_sensor_keymap_binding_triggered(behavior, sensor, binding->param1, binding->param2);

			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;
}

#endif /* ZMK_KEYMAP_HAS_SENSORS */

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);
		return zmk_keymap_position_state_changed(ev->position, ev->state);
#if ZMK_KEYMAP_HAS_SENSORS
	} else if (is_sensor_event(eh)) {
		const struct sensor_event *ev = cast_sensor_event(eh);
		return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor);
#endif /* ZMK_KEYMAP_HAS_SENSORS */
	}

	return -ENOTSUP;
}

ZMK_LISTENER(keymap, keymap_listener);
ZMK_SUBSCRIPTION(keymap, position_state_changed);

#if ZMK_KEYMAP_HAS_SENSORS
ZMK_SUBSCRIPTION(keymap, sensor_event);
#endif /* ZMK_KEYMAP_HAS_SENSORS */