8
8
9
9
#include <zephyr/types.h>
10
10
#include <stddef.h>
11
+ #include <sys/util.h>
12
+ #include <string.h>
11
13
#include <device.h>
12
14
#include <zmk/keys.h>
13
15
#include <zmk/behavior.h>
@@ -26,7 +28,15 @@ typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_bin
26
28
const struct device * sensor ,
27
29
int64_t timestamp );
28
30
31
+ enum behavior_locality {
32
+ BEHAVIOR_LOCALITY_CENTRAL ,
33
+ BEHAVIOR_LOCALITY_EVENT_SOURCE ,
34
+ BEHAVIOR_LOCALITY_GLOBAL
35
+ };
36
+
29
37
__subsystem struct behavior_driver_api {
38
+ enum behavior_locality locality ;
39
+ behavior_keymap_binding_callback_t binding_to_absolute ;
30
40
behavior_keymap_binding_callback_t binding_pressed ;
31
41
behavior_keymap_binding_callback_t binding_released ;
32
42
behavior_sensor_keymap_binding_callback_t sensor_binding_triggered ;
@@ -35,6 +45,53 @@ __subsystem struct behavior_driver_api {
35
45
* @endcond
36
46
*/
37
47
48
+ /**
49
+ * @brief Handle the keymap binding which needs to be converted from relative "toggle" to absolute
50
+ * "turn on"
51
+ * @param binding Pointer to the details so of the binding
52
+ * @param event The event that triggered use of the binding
53
+ *
54
+ * @retval 0 If successful.
55
+ * @retval Negative errno code if failure.
56
+ */
57
+ __syscall int behavior_keymap_binding_to_absolute (struct zmk_behavior_binding * binding ,
58
+ struct zmk_behavior_binding_event event );
59
+
60
+ static inline int
61
+ z_impl_behavior_keymap_binding_to_absolute (struct zmk_behavior_binding * binding ,
62
+ struct zmk_behavior_binding_event event ) {
63
+ const struct device * dev = device_get_binding (binding -> behavior_dev );
64
+ const struct behavior_driver_api * api = (const struct behavior_driver_api * )dev -> api ;
65
+
66
+ if (api -> binding_to_absolute == NULL ) {
67
+ return 0 ;
68
+ }
69
+
70
+ return api -> binding_to_absolute (binding , event );
71
+ }
72
+
73
+ /**
74
+ * @brief Determine where the behavior should be run
75
+ * @param behavior Pointer to the device structure for the driver instance.
76
+ *
77
+ * @retval Zero if successful.
78
+ * @retval Negative errno code if failure.
79
+ */
80
+ __syscall int behavior_get_locality (const struct device * behavior ,
81
+ enum behavior_locality * locality );
82
+
83
+ static inline int z_impl_behavior_get_locality (const struct device * behavior ,
84
+ enum behavior_locality * locality ) {
85
+ if (behavior == NULL ) {
86
+ return - EINVAL ;
87
+ }
88
+
89
+ const struct behavior_driver_api * api = (const struct behavior_driver_api * )behavior -> api ;
90
+ * locality = api -> locality ;
91
+
92
+ return 0 ;
93
+ }
94
+
38
95
/**
39
96
* @brief Handle the keymap binding being pressed
40
97
* @param dev Pointer to the device structure for the driver instance.
@@ -50,6 +107,11 @@ __syscall int behavior_keymap_binding_pressed(struct zmk_behavior_binding *bindi
50
107
static inline int z_impl_behavior_keymap_binding_pressed (struct zmk_behavior_binding * binding ,
51
108
struct zmk_behavior_binding_event event ) {
52
109
const struct device * dev = device_get_binding (binding -> behavior_dev );
110
+
111
+ if (dev == NULL ) {
112
+ return - EINVAL ;
113
+ }
114
+
53
115
const struct behavior_driver_api * api = (const struct behavior_driver_api * )dev -> api ;
54
116
55
117
if (api -> binding_pressed == NULL ) {
@@ -73,6 +135,11 @@ __syscall int behavior_keymap_binding_released(struct zmk_behavior_binding *bind
73
135
static inline int z_impl_behavior_keymap_binding_released (struct zmk_behavior_binding * binding ,
74
136
struct zmk_behavior_binding_event event ) {
75
137
const struct device * dev = device_get_binding (binding -> behavior_dev );
138
+
139
+ if (dev == NULL ) {
140
+ return - EINVAL ;
141
+ }
142
+
76
143
const struct behavior_driver_api * api = (const struct behavior_driver_api * )dev -> api ;
77
144
78
145
if (api -> binding_released == NULL ) {
@@ -100,6 +167,11 @@ static inline int
100
167
z_impl_behavior_sensor_keymap_binding_triggered (struct zmk_behavior_binding * binding ,
101
168
const struct device * sensor , int64_t timestamp ) {
102
169
const struct device * dev = device_get_binding (binding -> behavior_dev );
170
+
171
+ if (dev == NULL ) {
172
+ return - EINVAL ;
173
+ }
174
+
103
175
const struct behavior_driver_api * api = (const struct behavior_driver_api * )dev -> api ;
104
176
105
177
if (api -> sensor_binding_triggered == NULL ) {
0 commit comments