From c9824094c6c8a91961889c913360938cae72efbb Mon Sep 17 00:00:00 2001 From: zvecr Date: Thu, 5 Jun 2025 05:11:18 +0100 Subject: [PATCH 1/2] Partially implement connection update logic --- quantum/keyboard.c | 4 ++++ tmk_core/protocol/host.c | 31 +++++++++++++++++++++++++++++-- tmk_core/protocol/host.h | 3 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index c1a6d444a5a..4605e7434cd 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -29,6 +29,7 @@ along with this program. If not, see . #include "debug.h" #include "command.h" #include "util.h" +#include "host.h" #include "sendchar.h" #include "eeconfig.h" #include "action_layer.h" @@ -471,6 +472,7 @@ void keyboard_init(void) { #ifdef CONNECTION_ENABLE connection_init(); #endif + host_init(); led_init_ports(); #ifdef BACKLIGHT_ENABLE backlight_init_ports(); @@ -715,6 +717,8 @@ void quantum_task(void) { #ifdef LAYER_LOCK_ENABLE layer_lock_task(); #endif + + host_task(); } /** \brief Main task that is repeatedly called as fast as possible. */ diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index 4874d7c1d36..166bcbabea6 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -18,6 +18,7 @@ along with this program. If not, see . #include #include "keyboard.h" #include "keycode.h" +#include "action.h" #include "host.h" #include "util.h" #include "debug.h" @@ -78,9 +79,35 @@ host_driver_t *host_get_driver(void) { return driver; } +#ifdef CONNECTION_ENABLE +static connection_host_t active_host = CONNECTION_HOST_NONE; + +static void host_update_active_driver(connection_host_t current, connection_host_t next) { + if (current != CONNECTION_HOST_NONE) { + // TODO: Additionally have host_driver_t handle swap + clear_keyboard(); + } +} +#endif + +void host_init(void) { + // currently do nothing +} + +void host_task(void) { +#ifdef CONNECTION_ENABLE + connection_host_t next_host = connection_get_host(); + if (next_host != active_host) { + host_update_active_driver(active_host, next_host); + + active_host = next_host; + } +#endif +} + static host_driver_t *host_get_active_driver(void) { #ifdef CONNECTION_ENABLE - switch (connection_get_host()) { + switch (active_host) { # ifdef BLUETOOTH_ENABLE case CONNECTION_HOST_BLUETOOTH: return &bt_driver; @@ -96,7 +123,7 @@ static host_driver_t *host_get_active_driver(void) { bool host_can_send_nkro(void) { #ifdef CONNECTION_ENABLE - switch (connection_get_host()) { + switch (active_host) { # ifdef BLUETOOTH_ENABLE case CONNECTION_HOST_BLUETOOTH: return bluetooth_can_send_nkro(); diff --git a/tmk_core/protocol/host.h b/tmk_core/protocol/host.h index a0b1e73dccc..7ff6d0df08d 100644 --- a/tmk_core/protocol/host.h +++ b/tmk_core/protocol/host.h @@ -27,6 +27,9 @@ along with this program. If not, see . extern "C" { #endif +void host_init(void); +void host_task(void); + /* host driver */ void host_set_driver(host_driver_t *driver); host_driver_t *host_get_driver(void); From 6dd03f42882b8de1483db79e30bf937bff781c49 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 6 Jun 2025 02:42:57 +0100 Subject: [PATCH 2/2] Add active driver callbacks --- tmk_core/protocol/host.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index 166bcbabea6..f4fa9064e03 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -82,12 +82,25 @@ host_driver_t *host_get_driver(void) { #ifdef CONNECTION_ENABLE static connection_host_t active_host = CONNECTION_HOST_NONE; +__attribute__((weak)) void host_disconnect_active_driver_user(connection_host_t host) {} +__attribute__((weak)) void host_disconnect_active_driver_kb(connection_host_t host) {} + +__attribute__((weak)) void host_connect_active_driver_user(connection_host_t host) {} +__attribute__((weak)) void host_connect_active_driver_kb(connection_host_t host) {} + +// TODO: Additionally have host_driver_t handle swap static void host_update_active_driver(connection_host_t current, connection_host_t next) { + host_disconnect_active_driver_user(current); + host_disconnect_active_driver_kb(current); + if (current != CONNECTION_HOST_NONE) { - // TODO: Additionally have host_driver_t handle swap clear_keyboard(); } + + host_connect_active_driver_user(next); + host_connect_active_driver_kb(next); } + #endif void host_init(void) {