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:
Okke Formsma 2020-11-11 22:06:25 +01:00 committed by Pete Johanson
parent 27d036b9d5
commit bee45f9b3a
7 changed files with 21 additions and 17 deletions

View file

@ -23,7 +23,7 @@
typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding, typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event); struct zmk_behavior_binding_event event);
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding, 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 { __subsystem struct behavior_driver_api {
behavior_keymap_binding_callback_t binding_pressed; 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. * @retval Negative errno code if failure.
*/ */
__syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, __syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
struct device *sensor); struct device *sensor, s64_t timestamp);
static inline int static inline int
z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding, 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); struct device *dev = device_get_binding(binding->behavior_dev);
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; 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 -ENOTSUP;
} }
return api->sensor_binding_triggered(binding, sensor); return api->sensor_binding_triggered(binding, sensor, timestamp);
} }
/** /**

View file

@ -18,12 +18,13 @@ struct keycode_state_changed {
u32_t keycode; u32_t keycode;
u8_t implicit_modifiers; u8_t implicit_modifiers;
bool state; bool state;
s64_t timestamp;
}; };
ZMK_EVENT_DECLARE(keycode_state_changed); ZMK_EVENT_DECLARE(keycode_state_changed);
static inline struct keycode_state_changed *keycode_state_changed_from_encoded(u32_t encoded, static inline struct keycode_state_changed *
bool pressed) { keycode_state_changed_from_encoded(u32_t encoded, bool pressed, s64_t timestamp) {
u16_t page = HID_USAGE_PAGE(encoded) & 0xFF; u16_t page = HID_USAGE_PAGE(encoded) & 0xFF;
u16_t id = HID_USAGE_ID(encoded); u16_t id = HID_USAGE_ID(encoded);
zmk_mod_flags implicit_mods = SELECT_MODS(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->keycode = id;
ev->implicit_modifiers = implicit_mods; ev->implicit_modifiers = implicit_mods;
ev->state = pressed; ev->state = pressed;
ev->timestamp = timestamp;
return ev; return ev;
} }

View file

@ -14,6 +14,7 @@ struct sensor_event {
struct zmk_event_header header; struct zmk_event_header header;
u8_t sensor_number; u8_t sensor_number;
struct device *sensor; struct device *sensor;
s64_t timestamp;
}; };
ZMK_EVENT_DECLARE(sensor_event); ZMK_EVENT_DECLARE(sensor_event);

View file

@ -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, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(binding->param1, true)); keycode_state_changed_from_encoded(binding->param1, true, event.timestamp));
} }
static int on_keymap_binding_released(struct zmk_behavior_binding *binding, static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(
return ZMK_EVENT_RAISE(keycode_state_changed_from_encoded(binding->param1, false)); keycode_state_changed_from_encoded(binding->param1, false, event.timestamp));
} }
static const struct behavior_driver_api behavior_key_press_driver_api = { static const struct behavior_driver_api behavior_key_press_driver_api = {

View file

@ -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 behavior_sensor_rotate_key_press_init(struct device *dev) { return 0; };
static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, struct device *sensor,
struct device *sensor) { s64_t timestamp) {
struct sensor_value value; struct sensor_value value;
int err; int err;
u32_t keycode; u32_t keycode;
@ -45,12 +45,12 @@ static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
LOG_DBG("SEND %d", keycode); 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? // TODO: Better way to do this?
k_msleep(5); 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 = { static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = {

View file

@ -152,7 +152,7 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timest
} }
#if ZMK_KEYMAP_HAS_SENSORS #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--) { for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--) {
if (((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || if (((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) ||
layer == zmk_keymap_layer_default) && layer == zmk_keymap_layer_default) &&
@ -171,7 +171,7 @@ int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) {
continue; continue;
} }
ret = behavior_sensor_keymap_binding_triggered(binding, sensor); ret = behavior_sensor_keymap_binding_triggered(binding, sensor, timestamp);
if (ret > 0) { if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer"); 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 #if ZMK_KEYMAP_HAS_SENSORS
} else if (is_sensor_event(eh)) { } else if (is_sensor_event(eh)) {
const struct sensor_event *ev = cast_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 */ #endif /* ZMK_KEYMAP_HAS_SENSORS */
} }

View file

@ -48,6 +48,7 @@ static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigge
event = new_sensor_event(); event = new_sensor_event();
event->sensor_number = item->sensor_number; event->sensor_number = item->sensor_number;
event->sensor = dev; event->sensor = dev;
event->timestamp = k_uptime_get();
ZMK_EVENT_RAISE(event); ZMK_EVENT_RAISE(event);
} }