feat(events): add timestamp to keycode_state_changed and sensor_event
These timestamps are necessary to correctly deal with delayed events due to hold-tap shenanigans.
This commit is contained in:
parent
27d036b9d5
commit
bee45f9b3a
7 changed files with 21 additions and 17 deletions
|
@ -23,7 +23,7 @@
|
|||
typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
|
||||
struct zmk_behavior_binding_event event);
|
||||
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
|
||||
struct device *sensor);
|
||||
struct device *sensor, s64_t timestamp);
|
||||
|
||||
__subsystem struct behavior_driver_api {
|
||||
behavior_keymap_binding_callback_t binding_pressed;
|
||||
|
@ -92,11 +92,11 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi
|
|||
* @retval Negative errno code if failure.
|
||||
*/
|
||||
__syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
|
||||
struct device *sensor);
|
||||
struct device *sensor, s64_t timestamp);
|
||||
|
||||
static inline int
|
||||
z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
|
||||
struct device *sensor) {
|
||||
struct device *sensor, s64_t timestamp) {
|
||||
struct device *dev = device_get_binding(binding->behavior_dev);
|
||||
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api;
|
||||
|
||||
|
@ -104,7 +104,7 @@ z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *bin
|
|||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return api->sensor_binding_triggered(binding, sensor);
|
||||
return api->sensor_binding_triggered(binding, sensor, timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,12 +18,13 @@ struct keycode_state_changed {
|
|||
u32_t keycode;
|
||||
u8_t implicit_modifiers;
|
||||
bool state;
|
||||
s64_t timestamp;
|
||||
};
|
||||
|
||||
ZMK_EVENT_DECLARE(keycode_state_changed);
|
||||
|
||||
static inline struct keycode_state_changed *keycode_state_changed_from_encoded(u32_t encoded,
|
||||
bool pressed) {
|
||||
static inline struct keycode_state_changed *
|
||||
keycode_state_changed_from_encoded(u32_t encoded, bool pressed, s64_t timestamp) {
|
||||
u16_t page = HID_USAGE_PAGE(encoded) & 0xFF;
|
||||
u16_t id = HID_USAGE_ID(encoded);
|
||||
zmk_mod_flags implicit_mods = SELECT_MODS(encoded);
|
||||
|
@ -37,5 +38,6 @@ static inline struct keycode_state_changed *keycode_state_changed_from_encoded(u
|
|||
ev->keycode = id;
|
||||
ev->implicit_modifiers = implicit_mods;
|
||||
ev->state = pressed;
|
||||
ev->timestamp = timestamp;
|
||||
return ev;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ struct sensor_event {
|
|||
struct zmk_event_header header;
|
||||
u8_t sensor_number;
|
||||
struct device *sensor;
|
||||
s64_t timestamp;
|
||||
};
|
||||
|
||||
ZMK_EVENT_DECLARE(sensor_event);
|
|
@ -21,15 +21,15 @@ static int behavior_key_press_init(struct device *dev) { return 0; };
|
|||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||
struct zmk_behavior_binding_event event) {
|
||||
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
|
||||
|
||||
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(binding->param1, true));
|
||||
return ZMK_EVENT_RAISE(
|
||||
keycode_state_changed_from_encoded(binding->param1, true, event.timestamp));
|
||||
}
|
||||
|
||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
||||
struct zmk_behavior_binding_event event) {
|
||||
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
|
||||
|
||||
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(binding->param1, false));
|
||||
return ZMK_EVENT_RAISE(
|
||||
keycode_state_changed_from_encoded(binding->param1, false, event.timestamp));
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_key_press_driver_api = {
|
||||
|
|
|
@ -18,8 +18,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|||
|
||||
static int behavior_sensor_rotate_key_press_init(struct device *dev) { return 0; };
|
||||
|
||||
static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
|
||||
struct device *sensor) {
|
||||
static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, struct device *sensor,
|
||||
s64_t timestamp) {
|
||||
struct sensor_value value;
|
||||
int err;
|
||||
u32_t keycode;
|
||||
|
@ -45,12 +45,12 @@ static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
|
|||
|
||||
LOG_DBG("SEND %d", keycode);
|
||||
|
||||
ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(keycode, true));
|
||||
ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(keycode, true, timestamp));
|
||||
|
||||
// TODO: Better way to do this?
|
||||
k_msleep(5);
|
||||
|
||||
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(keycode, false));
|
||||
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(keycode, false, timestamp));
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = {
|
||||
|
|
|
@ -152,7 +152,7 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timest
|
|||
}
|
||||
|
||||
#if ZMK_KEYMAP_HAS_SENSORS
|
||||
int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) {
|
||||
int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor, s64_t timestamp) {
|
||||
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) &&
|
||||
|
@ -171,7 +171,7 @@ int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) {
|
|||
continue;
|
||||
}
|
||||
|
||||
ret = behavior_sensor_keymap_binding_triggered(binding, sensor);
|
||||
ret = behavior_sensor_keymap_binding_triggered(binding, sensor, timestamp);
|
||||
|
||||
if (ret > 0) {
|
||||
LOG_DBG("behavior processing to continue to next layer");
|
||||
|
@ -197,7 +197,7 @@ int keymap_listener(const struct zmk_event_header *eh) {
|
|||
#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);
|
||||
return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor, ev->timestamp);
|
||||
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigge
|
|||
event = new_sensor_event();
|
||||
event->sensor_number = item->sensor_number;
|
||||
event->sensor = dev;
|
||||
event->timestamp = k_uptime_get();
|
||||
|
||||
ZMK_EVENT_RAISE(event);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue