forked from kofal.net/zmk
Initial event manager work, and two first events.
* Add initial event manager implementation, roughly mimicking Nordic's API. * Add `position_state_changed` and `keycode_state_changed` events. * Hook up HID and keymap to new events instead of using behaviour global event crazy.
This commit is contained in:
@@ -13,18 +13,16 @@
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#include <zmk/event-manager.h>
|
||||
#include <zmk/events/keycode-state-changed.h>
|
||||
#include <zmk/hid.h>
|
||||
#include <zmk/endpoints.h>
|
||||
|
||||
struct behavior_hid_config { };
|
||||
struct behavior_hid_data { };
|
||||
|
||||
static int behavior_hid_init(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode)
|
||||
static int behaviour_hid_keycode_pressed(u8_t usage_page, u32_t keycode)
|
||||
{
|
||||
int err;
|
||||
LOG_DBG("keycode %d", keycode);
|
||||
@@ -49,7 +47,7 @@ static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode
|
||||
return zmk_endpoints_send_report(usage_page);
|
||||
}
|
||||
|
||||
static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode)
|
||||
static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode)
|
||||
{
|
||||
int err;
|
||||
LOG_DBG("keycode %d", keycode);
|
||||
@@ -73,6 +71,28 @@ static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycod
|
||||
return zmk_endpoints_send_report(usage_page);
|
||||
}
|
||||
|
||||
|
||||
int behavior_hid_listener(const struct zmk_event_header *eh)
|
||||
{
|
||||
if (is_keycode_state_changed(eh)) {
|
||||
const struct keycode_state_changed *ev = cast_keycode_state_changed(eh);
|
||||
if (ev->state) {
|
||||
behaviour_hid_keycode_pressed(ev->usage_page, ev->keycode);
|
||||
} else {
|
||||
behaviour_hid_keycode_released(ev->usage_page, ev->keycode);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ZMK_LISTENER(behavior_hid, behavior_hid_listener);
|
||||
ZMK_SUBSCRIPTION(behavior_hid, keycode_state_changed);
|
||||
|
||||
static int behavior_hid_init(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers)
|
||||
{
|
||||
LOG_DBG("modifiers %d", modifiers);
|
||||
@@ -90,13 +110,10 @@ static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers)
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_hid_driver_api = {
|
||||
.keycode_pressed = on_keycode_pressed,
|
||||
.keycode_released = on_keycode_released,
|
||||
.modifiers_pressed = on_modifiers_pressed,
|
||||
.modifiers_released = on_modifiers_released
|
||||
};
|
||||
|
||||
|
||||
static const struct behavior_hid_config behavior_hid_config = {};
|
||||
|
||||
static struct behavior_hid_data behavior_hid_data;
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
#include <drivers/behavior.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include <zmk/events.h>
|
||||
#include <zmk/event-manager.h>
|
||||
#include <zmk/events/keycode-state-changed.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
@@ -27,15 +28,27 @@ static int behavior_key_press_init(struct device *dev)
|
||||
static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _)
|
||||
{
|
||||
const struct behavior_key_press_config *cfg = dev->config_info;
|
||||
struct keycode_state_changed *ev;
|
||||
LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode);
|
||||
return zmk_events_keycode_pressed(cfg->usage_page, keycode);
|
||||
|
||||
ev = new_keycode_state_changed();
|
||||
ev->usage_page = cfg->usage_page;
|
||||
ev->keycode = keycode;
|
||||
ev->state = true;
|
||||
return ZMK_EVENT_RAISE(ev);
|
||||
}
|
||||
|
||||
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _)
|
||||
{
|
||||
const struct behavior_key_press_config *cfg = dev->config_info;
|
||||
struct keycode_state_changed *ev;
|
||||
LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode);
|
||||
return zmk_events_keycode_released(cfg->usage_page, keycode);
|
||||
|
||||
ev = new_keycode_state_changed();
|
||||
ev->usage_page = cfg->usage_page;
|
||||
ev->keycode = keycode;
|
||||
ev->state = false;
|
||||
return ZMK_EVENT_RAISE(ev);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_key_press_driver_api = {
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT zmk_behavior_keymap
|
||||
|
||||
#include <device.h>
|
||||
#include <power/reboot.h>
|
||||
#include <drivers/behavior.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#include <zmk/keymap.h>
|
||||
|
||||
struct behavior_keymap_config { };
|
||||
struct behavior_keymap_data { };
|
||||
|
||||
static int behavior_keymap_init(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
};
|
||||
|
||||
static int on_position_pressed(struct device *dev, u32_t position)
|
||||
{
|
||||
return zmk_keymap_position_state_changed(position, true);
|
||||
}
|
||||
|
||||
static int on_position_released(struct device *dev, u32_t position)
|
||||
{
|
||||
return zmk_keymap_position_state_changed(position, false);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_keymap_driver_api = {
|
||||
.position_pressed = on_position_pressed,
|
||||
.position_released = on_position_released,
|
||||
};
|
||||
|
||||
|
||||
static const struct behavior_keymap_config behavior_keymap_config = {};
|
||||
|
||||
static struct behavior_keymap_data behavior_keymap_data;
|
||||
|
||||
DEVICE_AND_API_INIT(behavior_keymap, DT_INST_LABEL(0), behavior_keymap_init,
|
||||
&behavior_keymap_data,
|
||||
&behavior_keymap_config,
|
||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
|
||||
&behavior_keymap_driver_api);
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <drivers/behavior.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include <zmk/event-manager.h>
|
||||
#include <zmk/events/keycode-state-changed.h>
|
||||
#include <zmk/events.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
@@ -19,6 +21,22 @@ struct behavior_mod_tap_data {
|
||||
u16_t pending_press_positions;
|
||||
};
|
||||
|
||||
int behavior_mod_tap_listener(const struct zmk_event_header *eh)
|
||||
{
|
||||
if (is_keycode_state_changed(eh)) {
|
||||
struct device *dev = device_get_binding(DT_INST_LABEL(0));
|
||||
const struct keycode_state_changed *ev = cast_keycode_state_changed(eh);
|
||||
if (ev->state) {
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
data->pending_press_positions = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ZMK_LISTENER(behavior_mod_tap, behavior_mod_tap_listener);
|
||||
ZMK_SUBSCRIPTION(behavior_mod_tap, keycode_state_changed);
|
||||
|
||||
static int behavior_mod_tap_init(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
@@ -40,7 +58,7 @@ static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
LOG_DBG("mods: %d, keycode: %d", mods, keycode);
|
||||
|
||||
zmk_events_modifiers_released(mods);
|
||||
zmk_events_modifiers_released(mods);
|
||||
if (data->pending_press_positions & BIT(position)) {
|
||||
zmk_events_keycode_pressed(USAGE_KEYPAD, keycode);
|
||||
k_msleep(10);
|
||||
@@ -50,30 +68,9 @@ zmk_events_modifiers_released(mods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode)
|
||||
{
|
||||
struct behavior_mod_tap_data *data = dev->driver_data;
|
||||
data->pending_press_positions = 0;
|
||||
LOG_DBG("pressing: %d", keycode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode)
|
||||
{
|
||||
LOG_DBG("releasing: %d", keycode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_mod_tap_driver_api = {
|
||||
// These callbacks are all optional, and define which kinds of events the behavior can handle.
|
||||
// They can reference local functions defined here, or shared event handlers.
|
||||
.binding_pressed = on_keymap_binding_pressed,
|
||||
.binding_released = on_keymap_binding_released,
|
||||
.keycode_pressed = on_keycode_pressed,
|
||||
.keycode_released = on_keycode_released
|
||||
// Other optional callbacks a behavior can implement
|
||||
// .on_mouse_moved
|
||||
// .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior
|
||||
};
|
||||
|
||||
|
||||
@@ -81,7 +78,7 @@ static const struct behavior_mod_tap_config behavior_mod_tap_config = {};
|
||||
|
||||
static struct behavior_mod_tap_data behavior_mod_tap_data;
|
||||
|
||||
DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_mod_tap_init,
|
||||
DEVICE_AND_API_INIT(behavior_mod_tap, DT_INST_LABEL(0), behavior_mod_tap_init,
|
||||
&behavior_mod_tap_data,
|
||||
&behavior_mod_tap_config,
|
||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
|
||||
|
||||
Reference in New Issue
Block a user