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
|
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <zmk/behavior_queue.h>
#include <zmk/behavior.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <drivers/behavior.h>
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;
};
K_MSGQ_DEFINE(zmk_behavior_queue_msgq, sizeof(struct q_item), CONFIG_ZMK_BEHAVIORS_QUEUE_SIZE, 4);
static void behavior_queue_process_next(struct k_work *work);
static K_WORK_DELAYABLE_DEFINE(queue_work, behavior_queue_process_next);
static void behavior_queue_process_next(struct k_work *work) {
struct q_item item = {.wait = 0};
while (k_msgq_get(&zmk_behavior_queue_msgq, &item, K_NO_WAIT) == 0) {
LOG_DBG("Invoking %s: 0x%02x 0x%02x", item.binding.behavior_dev, item.binding.param1,
item.binding.param2);
struct zmk_behavior_binding_event event = {.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);
} else {
zmk_behavior_invoke_binding(&item.binding, event, false);
}
LOG_DBG("Processing next queued behavior in %dms", item.wait);
if (item.wait > 0) {
k_work_schedule(&queue_work, K_MSEC(item.wait));
break;
}
}
}
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 = 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) {
return ret;
}
if (!k_work_delayable_is_pending(&queue_work)) {
behavior_queue_process_next(&queue_work.work);
}
return 0;
}
|