From 13ab1cefdc8906b665679530c66c813db2299fa4 Mon Sep 17 00:00:00 2001
From: Peter Johanson <peter@peterjohanson.com>
Date: Wed, 10 Apr 2024 02:13:51 +0000
Subject: [PATCH] refactor(mouse): Tweak behavior inclusion, listener code

* Always import mouse keys behavior and their associated listeners.
* Tweak listener code to only add listener nodes when
  listener and the associated input device are enabled.
---
 app/dts/behaviors.dtsi                 |  1 +
 app/dts/behaviors/mouse_key_press.dtsi |  2 +-
 app/dts/behaviors/mouse_move.dtsi      |  2 +-
 app/dts/behaviors/mouse_scroll.dtsi    |  3 ++-
 app/src/mouse/input_listener.c         | 36 +++++++++++++++++---------
 5 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi
index 672eb3c2..72209e74 100644
--- a/app/dts/behaviors.dtsi
+++ b/app/dts/behaviors.dtsi
@@ -30,3 +30,4 @@
 #include <behaviors/mouse_move.dtsi>
 #include <behaviors/mouse_scroll.dtsi>
 #include <behaviors/macros.dtsi>
+#include <behaviors/mouse_keys.dtsi>
diff --git a/app/dts/behaviors/mouse_key_press.dtsi b/app/dts/behaviors/mouse_key_press.dtsi
index 9e6f9337..fcb76664 100644
--- a/app/dts/behaviors/mouse_key_press.dtsi
+++ b/app/dts/behaviors/mouse_key_press.dtsi
@@ -17,7 +17,7 @@
         };
     };
 
-    mkp_input_listener {
+    mkp_input_listener: mkp_input_listener {
         compatible = "zmk,input-listener";
         device = <&mkp>;
     };
diff --git a/app/dts/behaviors/mouse_move.dtsi b/app/dts/behaviors/mouse_move.dtsi
index 47dc6cec..09b93520 100644
--- a/app/dts/behaviors/mouse_move.dtsi
+++ b/app/dts/behaviors/mouse_move.dtsi
@@ -18,7 +18,7 @@
         };
     };
 
-    mmv_input_listener {
+    mmv_input_listener: mmv_input_listener {
         compatible = "zmk,input-listener";
         device = <&mmv>;
     };
diff --git a/app/dts/behaviors/mouse_scroll.dtsi b/app/dts/behaviors/mouse_scroll.dtsi
index e9d141f8..b482efde 100644
--- a/app/dts/behaviors/mouse_scroll.dtsi
+++ b/app/dts/behaviors/mouse_scroll.dtsi
@@ -1,3 +1,4 @@
+
 /*
  * Copyright (c) 2024 The ZMK Contributors
  *
@@ -18,7 +19,7 @@
         };
     };
 
-    msc_input_listener {
+    msc_input_listener: msc_input_listener {
         compatible = "zmk,input-listener";
         device = <&msc>;
     };
diff --git a/app/src/mouse/input_listener.c b/app/src/mouse/input_listener.c
index 0acc4349..6863e00e 100644
--- a/app/src/mouse/input_listener.c
+++ b/app/src/mouse/input_listener.c
@@ -15,6 +15,13 @@
 #include <zmk/endpoints.h>
 #include <zmk/hid.h>
 
+#define ONE_IF_DEV_OK(n)                                                                           \
+    COND_CODE_1(DT_NODE_HAS_STATUS(DT_INST_PHANDLE(n, device), okay), (1 +), (0 +))
+
+#define VALID_LISTENER_COUNT (DT_INST_FOREACH_STATUS_OKAY(ONE_IF_DEV_OK) 0)
+
+#if VALID_LISTENER_COUNT > 0
+
 enum input_listener_xy_data_mode {
     INPUT_LISTENER_XY_DATA_MODE_NONE,
     INPUT_LISTENER_XY_DATA_MODE_REL,
@@ -171,18 +178,23 @@ static void input_handler(const struct input_listener_config *config,
     }
 }
 
+#endif // VALID_LISTENER_COUNT > 0
+
 #define IL_INST(n)                                                                                 \
-    static const struct input_listener_config config_##n = {                                       \
-        .xy_swap = DT_INST_PROP(n, xy_swap),                                                       \
-        .x_invert = DT_INST_PROP(n, x_invert),                                                     \
-        .y_invert = DT_INST_PROP(n, y_invert),                                                     \
-        .scale_multiplier = DT_INST_PROP(n, scale_multiplier),                                     \
-        .scale_divisor = DT_INST_PROP(n, scale_divisor),                                           \
-    };                                                                                             \
-    static struct input_listener_data data_##n = {};                                               \
-    void input_handler_##n(struct input_event *evt) {                                              \
-        input_handler(&config_##n, &data_##n, evt);                                                \
-    }                                                                                              \
-    INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_INST_PHANDLE(n, device)), input_handler_##n);
+    COND_CODE_1(                                                                                   \
+        DT_NODE_HAS_STATUS(DT_INST_PHANDLE(n, device), okay),                                      \
+        (static const struct input_listener_config config_##n =                                    \
+             {                                                                                     \
+                 .xy_swap = DT_INST_PROP(n, xy_swap),                                              \
+                 .x_invert = DT_INST_PROP(n, x_invert),                                            \
+                 .y_invert = DT_INST_PROP(n, y_invert),                                            \
+                 .scale_multiplier = DT_INST_PROP(n, scale_multiplier),                            \
+                 .scale_divisor = DT_INST_PROP(n, scale_divisor),                                  \
+             };                                                                                    \
+         static struct input_listener_data data_##n = {};                                          \
+         void input_handler_##n(struct input_event *evt) {                                         \
+             input_handler(&config_##n, &data_##n, evt);                                           \
+         } INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_INST_PHANDLE(n, device)), input_handler_##n);),  \
+        ())
 
 DT_INST_FOREACH_STATUS_OKAY(IL_INST)