Pete Johanson
4 years ago
committed by
GitHub
27 changed files with 411 additions and 339 deletions
@ -1,9 +0,0 @@ |
|||||||
/ { |
|
||||||
behaviors { |
|
||||||
hid_behavior: behavior_hid { |
|
||||||
compatible = "zmk,behavior-hid", "zmk,behavior-global"; |
|
||||||
label = "HID"; |
|
||||||
#binding-cells = <0>; |
|
||||||
}; |
|
||||||
}; |
|
||||||
}; |
|
@ -1,9 +0,0 @@ |
|||||||
/ { |
|
||||||
behaviors { |
|
||||||
keymap_behavior: behavior_keymap { |
|
||||||
compatible = "zmk,behavior-keymap", "zmk,behavior-global"; |
|
||||||
label = "KEYMAP"; |
|
||||||
#binding-cells = <0>; |
|
||||||
}; |
|
||||||
}; |
|
||||||
}; |
|
@ -1,8 +0,0 @@ |
|||||||
# Copyright (c) 2020, Pete Johanson |
|
||||||
# SPDX-License-Identifier: MIT |
|
||||||
|
|
||||||
description: HID Report Behavior |
|
||||||
|
|
||||||
compatible: "zmk,behavior-hid" |
|
||||||
|
|
||||||
include: zero_param.yaml |
|
@ -1,8 +0,0 @@ |
|||||||
# Copyright (c) 2020, Pete Johanson |
|
||||||
# SPDX-License-Identifier: MIT |
|
||||||
|
|
||||||
description: Keymap Behavior |
|
||||||
|
|
||||||
compatible: "zmk,behavior-keymap" |
|
||||||
|
|
||||||
include: zero_param.yaml |
|
@ -0,0 +1,16 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <linker/linker-defs.h> |
||||||
|
|
||||||
|
__event_type_start = .; \ |
||||||
|
KEEP(*(".event_type")); \ |
||||||
|
__event_type_end = .; \ |
||||||
|
|
||||||
|
__event_subscriptions_start = .; \ |
||||||
|
KEEP(*(".event_subscription")); \ |
||||||
|
__event_subscriptions_end = .; \ |
||||||
|
|
@ -0,0 +1,71 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <stddef.h> |
||||||
|
#include <kernel.h> |
||||||
|
#include <zephyr/types.h> |
||||||
|
|
||||||
|
struct zmk_event_type |
||||||
|
{ |
||||||
|
const char *name; |
||||||
|
}; |
||||||
|
|
||||||
|
struct zmk_event_header { |
||||||
|
const struct zmk_event_type* event; |
||||||
|
}; |
||||||
|
|
||||||
|
typedef int (*zmk_listener_callback_t)(const struct zmk_event_header *eh); |
||||||
|
struct zmk_listener |
||||||
|
{ |
||||||
|
zmk_listener_callback_t callback; |
||||||
|
}; |
||||||
|
|
||||||
|
struct zmk_event_subscription { |
||||||
|
const struct zmk_event_type *event_type; |
||||||
|
const struct zmk_listener *listener; |
||||||
|
}; |
||||||
|
|
||||||
|
#define ZMK_EVENT_DECLARE(event_type) \ |
||||||
|
struct event_type* new_##event_type(); \
|
||||||
|
bool is_##event_type(const struct zmk_event_header *eh); \
|
||||||
|
const struct event_type* cast_##event_type(const struct zmk_event_header *eh); \
|
||||||
|
extern const struct zmk_event_type zmk_event_##event_type; |
||||||
|
|
||||||
|
#define ZMK_EVENT_IMPL(event_type) \ |
||||||
|
const struct zmk_event_type zmk_event_##event_type = { \
|
||||||
|
.name = STRINGIFY(event_type) \
|
||||||
|
}; \
|
||||||
|
const struct zmk_event_type* zmk_event_ref_##event_type __used __attribute__((__section__(".event_type"))) = &zmk_event_##event_type; \
|
||||||
|
struct event_type* new_##event_type() { \
|
||||||
|
struct event_type* ev = (struct event_type *) k_malloc(sizeof(struct event_type)); \
|
||||||
|
ev->header.event = &zmk_event_##event_type; \
|
||||||
|
return ev; \
|
||||||
|
}; \
|
||||||
|
bool is_##event_type(const struct zmk_event_header *eh) { \
|
||||||
|
return eh->event == &zmk_event_##event_type; \
|
||||||
|
}; \
|
||||||
|
const struct event_type* cast_##event_type(const struct zmk_event_header *eh) {\
|
||||||
|
return (const struct event_type*)eh; \
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
#define ZMK_LISTENER(mod, cb) \ |
||||||
|
const struct zmk_listener zmk_listener_##mod = { \
|
||||||
|
.callback = cb \
|
||||||
|
}; |
||||||
|
|
||||||
|
#define ZMK_SUBSCRIPTION(mod, ev_type) \ |
||||||
|
const Z_DECL_ALIGN(struct zmk_event_subscription) _CONCAT(_CONCAT(zmk_event_sub_,mod),ev_type) __used __attribute__((__section__(".event_subscription"))) = { \
|
||||||
|
.event_type = &zmk_event_##ev_type, \
|
||||||
|
.listener = &zmk_listener_##mod, \
|
||||||
|
}; |
||||||
|
|
||||||
|
#define ZMK_EVENT_RAISE(ev) \ |
||||||
|
zmk_event_manager_raise((struct zmk_event_header *)ev); |
||||||
|
|
||||||
|
int zmk_event_manager_raise(struct zmk_event_header *event); |
@ -1,15 +0,0 @@ |
|||||||
#pragma once |
|
||||||
|
|
||||||
#include <zmk/keys.h> |
|
||||||
|
|
||||||
int zmk_events_position_pressed(u32_t position); |
|
||||||
int zmk_events_position_released(u32_t position); |
|
||||||
int zmk_events_keycode_pressed(u8_t usage_page, u32_t keycode); |
|
||||||
int zmk_events_keycode_released(u8_t usage_page, u32_t keycode); |
|
||||||
int zmk_events_modifiers_pressed(zmk_mod_flags modifiers); |
|
||||||
int zmk_events_modifiers_released(zmk_mod_flags modifiers); |
|
||||||
int zmk_events_consumer_key_pressed(u32_t usage); |
|
||||||
int zmk_events_consumer_key_released(u32_t usage); |
|
||||||
|
|
||||||
// TODO: Encoders?
|
|
||||||
// TODO: Sensors?
|
|
@ -0,0 +1,29 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <zmk/event-manager.h> |
||||||
|
|
||||||
|
struct keycode_state_changed { |
||||||
|
struct zmk_event_header header; |
||||||
|
u8_t usage_page; |
||||||
|
u32_t keycode; |
||||||
|
bool state; |
||||||
|
}; |
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(keycode_state_changed); |
||||||
|
|
||||||
|
inline struct keycode_state_changed* create_keycode_state_changed(u8_t usage_page, u32_t keycode, bool state) |
||||||
|
{ |
||||||
|
struct keycode_state_changed* ev = new_keycode_state_changed(); |
||||||
|
ev->usage_page = usage_page; |
||||||
|
ev->keycode = keycode; |
||||||
|
ev->state = state; |
||||||
|
|
||||||
|
return ev; |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <zmk/keys.h> |
||||||
|
#include <zmk/event-manager.h> |
||||||
|
|
||||||
|
struct modifiers_state_changed { |
||||||
|
struct zmk_event_header header; |
||||||
|
zmk_mod_flags modifiers; |
||||||
|
bool state; |
||||||
|
}; |
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(modifiers_state_changed); |
||||||
|
|
||||||
|
inline struct modifiers_state_changed* create_modifiers_state_changed(zmk_mod_flags modifiers, bool state) |
||||||
|
{ |
||||||
|
struct modifiers_state_changed* ev = new_modifiers_state_changed(); |
||||||
|
ev->modifiers = modifiers; |
||||||
|
ev->state = state; |
||||||
|
|
||||||
|
return ev; |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <zmk/event-manager.h> |
||||||
|
|
||||||
|
struct position_state_changed { |
||||||
|
struct zmk_event_header header; |
||||||
|
u32_t position; |
||||||
|
bool state; |
||||||
|
}; |
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(position_state_changed); |
@ -1,108 +0,0 @@ |
|||||||
/*
|
|
||||||
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
|
||||||
* |
|
||||||
* SPDX-License-Identifier: MIT |
|
||||||
*/ |
|
||||||
|
|
||||||
#define DT_DRV_COMPAT zmk_behavior_hid |
|
||||||
|
|
||||||
#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/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) |
|
||||||
{ |
|
||||||
int err; |
|
||||||
LOG_DBG("keycode %d", keycode); |
|
||||||
|
|
||||||
switch (usage_page) { |
|
||||||
case USAGE_KEYPAD: |
|
||||||
err = zmk_hid_keypad_press(keycode); |
|
||||||
if (err) { |
|
||||||
LOG_ERR("Unable to press keycode"); |
|
||||||
return err; |
|
||||||
} |
|
||||||
break; |
|
||||||
case USAGE_CONSUMER: |
|
||||||
err = zmk_hid_consumer_press(keycode); |
|
||||||
if (err) { |
|
||||||
LOG_ERR("Unable to press keycode"); |
|
||||||
return err; |
|
||||||
} |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
return zmk_endpoints_send_report(usage_page); |
|
||||||
} |
|
||||||
|
|
||||||
static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) |
|
||||||
{ |
|
||||||
int err; |
|
||||||
LOG_DBG("keycode %d", keycode); |
|
||||||
|
|
||||||
switch (usage_page) { |
|
||||||
case USAGE_KEYPAD: |
|
||||||
err = zmk_hid_keypad_release(keycode); |
|
||||||
if (err) { |
|
||||||
LOG_ERR("Unable to press keycode"); |
|
||||||
return err; |
|
||||||
} |
|
||||||
break; |
|
||||||
case USAGE_CONSUMER: |
|
||||||
err = zmk_hid_consumer_release(keycode); |
|
||||||
if (err) { |
|
||||||
LOG_ERR("Unable to press keycode"); |
|
||||||
return err; |
|
||||||
} |
|
||||||
break; |
|
||||||
} |
|
||||||
return zmk_endpoints_send_report(usage_page); |
|
||||||
} |
|
||||||
|
|
||||||
static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) |
|
||||||
{ |
|
||||||
LOG_DBG("modifiers %d", modifiers); |
|
||||||
|
|
||||||
zmk_hid_register_mods(modifiers); |
|
||||||
return zmk_endpoints_send_report(USAGE_KEYPAD); |
|
||||||
} |
|
||||||
|
|
||||||
static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers) |
|
||||||
{ |
|
||||||
LOG_DBG("modifiers %d", modifiers); |
|
||||||
|
|
||||||
zmk_hid_unregister_mods(modifiers); |
|
||||||
return zmk_endpoints_send_report(USAGE_KEYPAD); |
|
||||||
} |
|
||||||
|
|
||||||
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; |
|
||||||
|
|
||||||
DEVICE_AND_API_INIT(behavior_hid, DT_INST_LABEL(0), behavior_hid_init, |
|
||||||
&behavior_hid_data, |
|
||||||
&behavior_hid_config, |
|
||||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, |
|
||||||
&behavior_hid_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); |
|
@ -0,0 +1,37 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <logging/log.h> |
||||||
|
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/event-manager.h> |
||||||
|
|
||||||
|
extern struct zmk_event_type* __event_type_start[]; |
||||||
|
extern struct zmk_event_type* __event_type_end[]; |
||||||
|
|
||||||
|
extern struct zmk_event_subscription __event_subscriptions_start[]; |
||||||
|
extern struct zmk_event_subscription __event_subscriptions_end[]; |
||||||
|
|
||||||
|
int zmk_event_manager_raise(struct zmk_event_header *event) |
||||||
|
{ |
||||||
|
int ret; |
||||||
|
struct zmk_event_subscription *ev_sub; |
||||||
|
for (ev_sub = __event_subscriptions_start; ev_sub != __event_subscriptions_end; ev_sub++) { |
||||||
|
if (ev_sub->event_type == event->event) { |
||||||
|
ret = ev_sub->listener->callback(event); |
||||||
|
if (ret) { |
||||||
|
LOG_DBG("Listener returned an error: %d", ret); |
||||||
|
goto release; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
release: |
||||||
|
k_free(event); |
||||||
|
return ret; |
||||||
|
} |
@ -1,83 +0,0 @@ |
|||||||
|
|
||||||
#include <zephyr.h> |
|
||||||
#include <drivers/behavior.h> |
|
||||||
#include <zmk/behavior.h> |
|
||||||
#include <zmk/events.h> |
|
||||||
#include <sys/util.h> |
|
||||||
|
|
||||||
#define DT_DRV_COMPAT zmk_behavior_global |
|
||||||
#define GLOBAL_BEHAVIOR_LEN DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) |
|
||||||
|
|
||||||
#define LABEL_ENTRY(i) DT_INST_LABEL(i), |
|
||||||
static const char *global_behaviors[] = { |
|
||||||
DT_INST_FOREACH_STATUS_OKAY(LABEL_ENTRY) |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_position_pressed(u32_t position) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_position_pressed(dev, position); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_position_released(u32_t position) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_position_released(dev, position); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_keycode_pressed(u8_t usage_page, u32_t keycode) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_keycode_pressed(dev, usage_page, keycode); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_keycode_released(u8_t usage_page, u32_t keycode) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_keycode_released(dev, usage_page, keycode); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_modifiers_pressed(zmk_mod_flags modifiers) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_modifiers_pressed(dev, modifiers); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_modifiers_released(zmk_mod_flags modifiers) |
|
||||||
{ |
|
||||||
for (int i = 0; i < GLOBAL_BEHAVIOR_LEN; i++) { |
|
||||||
const char* label = global_behaviors[i]; |
|
||||||
struct device *dev = device_get_binding(label); |
|
||||||
behavior_modifiers_released(dev, modifiers); |
|
||||||
} |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
|
|
||||||
int zmk_events_consumer_key_pressed(u32_t usage) |
|
||||||
{ |
|
||||||
return 0; |
|
||||||
}; |
|
||||||
int zmk_events_consumer_key_released(u32_t usage) |
|
||||||
{ |
|
||||||
return 0; |
|
||||||
}; |
|
@ -0,0 +1,10 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <zmk/events/keycode-state-changed.h> |
||||||
|
|
||||||
|
ZMK_EVENT_IMPL(keycode_state_changed); |
@ -0,0 +1,10 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <zmk/events/modifiers-state-changed.h> |
||||||
|
|
||||||
|
ZMK_EVENT_IMPL(modifiers_state_changed); |
@ -0,0 +1,10 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <zmk/events/position-state-changed.h> |
||||||
|
|
||||||
|
ZMK_EVENT_IMPL(position_state_changed); |
@ -0,0 +1,106 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com> |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <drivers/behavior.h> |
||||||
|
#include <logging/log.h> |
||||||
|
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/event-manager.h> |
||||||
|
#include <zmk/events/keycode-state-changed.h> |
||||||
|
#include <zmk/events/modifiers-state-changed.h> |
||||||
|
#include <zmk/hid.h> |
||||||
|
#include <zmk/endpoints.h> |
||||||
|
|
||||||
|
|
||||||
|
static int hid_listener_keycode_pressed(u8_t usage_page, u32_t keycode) |
||||||
|
{ |
||||||
|
int err; |
||||||
|
LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); |
||||||
|
|
||||||
|
switch (usage_page) { |
||||||
|
case USAGE_KEYPAD: |
||||||
|
err = zmk_hid_keypad_press(keycode); |
||||||
|
if (err) { |
||||||
|
LOG_ERR("Unable to press keycode"); |
||||||
|
return err; |
||||||
|
} |
||||||
|
break; |
||||||
|
case USAGE_CONSUMER: |
||||||
|
err = zmk_hid_consumer_press(keycode); |
||||||
|
if (err) { |
||||||
|
LOG_ERR("Unable to press keycode"); |
||||||
|
return err; |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
return zmk_endpoints_send_report(usage_page); |
||||||
|
} |
||||||
|
|
||||||
|
static int hid_listener_keycode_released(u8_t usage_page, u32_t keycode) |
||||||
|
{ |
||||||
|
int err; |
||||||
|
LOG_DBG("usage_page 0x%02X keycode 0x%02X", usage_page, keycode); |
||||||
|
|
||||||
|
switch (usage_page) { |
||||||
|
case USAGE_KEYPAD: |
||||||
|
err = zmk_hid_keypad_release(keycode); |
||||||
|
if (err) { |
||||||
|
LOG_ERR("Unable to release keycode"); |
||||||
|
return err; |
||||||
|
} |
||||||
|
break; |
||||||
|
case USAGE_CONSUMER: |
||||||
|
err = zmk_hid_consumer_release(keycode); |
||||||
|
if (err) { |
||||||
|
LOG_ERR("Unable to release keycode"); |
||||||
|
return err; |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
return zmk_endpoints_send_report(usage_page); |
||||||
|
} |
||||||
|
|
||||||
|
static int hid_listener_modifiers_pressed(zmk_mod_flags modifiers) |
||||||
|
{ |
||||||
|
LOG_DBG("modifiers %d", modifiers); |
||||||
|
|
||||||
|
zmk_hid_register_mods(modifiers); |
||||||
|
return zmk_endpoints_send_report(USAGE_KEYPAD); |
||||||
|
} |
||||||
|
|
||||||
|
static int hid_listener_modifiers_released(zmk_mod_flags modifiers) |
||||||
|
{ |
||||||
|
LOG_DBG("modifiers %d", modifiers); |
||||||
|
|
||||||
|
zmk_hid_unregister_mods(modifiers); |
||||||
|
return zmk_endpoints_send_report(USAGE_KEYPAD); |
||||||
|
} |
||||||
|
|
||||||
|
int 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) { |
||||||
|
hid_listener_keycode_pressed(ev->usage_page, ev->keycode); |
||||||
|
} else { |
||||||
|
hid_listener_keycode_released(ev->usage_page, ev->keycode); |
||||||
|
} |
||||||
|
} else if (is_modifiers_state_changed(eh)) { |
||||||
|
const struct modifiers_state_changed *ev = cast_modifiers_state_changed(eh); |
||||||
|
if (ev->state) { |
||||||
|
hid_listener_modifiers_pressed(ev->modifiers); |
||||||
|
} else { |
||||||
|
hid_listener_modifiers_released(ev->modifiers); |
||||||
|
} |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
ZMK_LISTENER(hid_listener, hid_listener); |
||||||
|
ZMK_SUBSCRIPTION(hid_listener, keycode_state_changed); |
||||||
|
ZMK_SUBSCRIPTION(hid_listener, modifiers_state_changed); |
Loading…
Reference in new issue