summary refs log tree commit diff
diff options
context:
space:
mode:
authorAriane Emory <97994360+ariane-emory@users.noreply.github.com>2023-06-02 17:46:04 -0400
committerGitHub <noreply@github.com>2023-06-02 14:46:04 -0700
commitc754f644dcccc44e09a3e6f9986ad70ec2fc3b54 (patch)
treead67cd9ef8a9ea3c6df15613f40abfc870e18cce
parent27120f2fb6cc32a1a544c2d26decce8facac9740 (diff)
[Core] Move dynamic macro "stop recording" logic to a function (#21108)
-rw-r--r--quantum/process_keycode/process_dynamic_macro.c117
-rw-r--r--quantum/process_keycode/process_dynamic_macro.h1
2 files changed, 63 insertions, 55 deletions
diff --git a/quantum/process_keycode/process_dynamic_macro.c b/quantum/process_keycode/process_dynamic_macro.c
index bf6af566e2..a022949d3d 100644
--- a/quantum/process_keycode/process_dynamic_macro.c
+++ b/quantum/process_keycode/process_dynamic_macro.c
@@ -151,6 +151,67 @@ void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_poin
     *macro_end = macro_pointer;
 }
 
+/* Both macros use the same buffer but read/write on different
+ * ends of it.
+ *
+ * Macro1 is written left-to-right starting from the beginning of
+ * the buffer.
+ *
+ * Macro2 is written right-to-left starting from the end of the
+ * buffer.
+ *
+ * &macro_buffer   macro_end
+ *  v                   v
+ * +------------------------------------------------------------+
+ * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
+ * +------------------------------------------------------------+
+ *                           ^                                 ^
+ *                         r_macro_end                  r_macro_buffer
+ *
+ * During the recording when one macro encounters the end of the
+ * other macro, the recording is stopped. Apart from this, there
+ * are no arbitrary limits for the macros' length in relation to
+ * each other: for example one can either have two medium sized
+ * macros or one long macro and one short macro. Or even one empty
+ * and one using the whole buffer.
+ */
+static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
+
+/* Pointer to the first buffer element after the first macro.
+ * Initially points to the very beginning of the buffer since the
+ * macro is empty. */
+static keyrecord_t *macro_end = macro_buffer;
+
+/* The other end of the macro buffer. Serves as the beginning of
+ * the second macro. */
+static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
+
+/* Like macro_end but for the second macro. */
+static keyrecord_t *r_macro_end = r_macro_buffer;
+
+/* A persistent pointer to the current macro position (iterator)
+ * used during the recording. */
+static keyrecord_t *macro_pointer = NULL;
+
+/* 0   - no macro is being recorded right now
+ * 1,2 - either macro 1 or 2 is being recorded */
+static uint8_t macro_id = 0;
+
+/**
+ * If a dynamic macro is currently being recorded, stop recording.
+ */
+void dynamic_macro_stop_recording(void) {
+    switch (macro_id) {
+        case 1:
+            dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
+            break;
+        case 2:
+            dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
+            break;
+    }
+    macro_id = 0;
+}
+
 /* Handle the key events related to the dynamic macros. Should be
  * called from process_record_user() like this:
  *
@@ -162,52 +223,6 @@ void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_poin
  *   }
  */
 bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
-    /* Both macros use the same buffer but read/write on different
-     * ends of it.
-     *
-     * Macro1 is written left-to-right starting from the beginning of
-     * the buffer.
-     *
-     * Macro2 is written right-to-left starting from the end of the
-     * buffer.
-     *
-     * &macro_buffer   macro_end
-     *  v                   v
-     * +------------------------------------------------------------+
-     * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
-     * +------------------------------------------------------------+
-     *                           ^                                 ^
-     *                         r_macro_end                  r_macro_buffer
-     *
-     * During the recording when one macro encounters the end of the
-     * other macro, the recording is stopped. Apart from this, there
-     * are no arbitrary limits for the macros' length in relation to
-     * each other: for example one can either have two medium sized
-     * macros or one long macro and one short macro. Or even one empty
-     * and one using the whole buffer.
-     */
-    static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
-
-    /* Pointer to the first buffer element after the first macro.
-     * Initially points to the very beginning of the buffer since the
-     * macro is empty. */
-    static keyrecord_t *macro_end = macro_buffer;
-
-    /* The other end of the macro buffer. Serves as the beginning of
-     * the second macro. */
-    static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
-
-    /* Like macro_end but for the second macro. */
-    static keyrecord_t *r_macro_end = r_macro_buffer;
-
-    /* A persistent pointer to the current macro position (iterator)
-     * used during the recording. */
-    static keyrecord_t *macro_pointer = NULL;
-
-    /* 0   - no macro is being recorded right now
-     * 1,2 - either macro 1 or 2 is being recorded */
-    static uint8_t macro_id = 0;
-
     if (macro_id == 0) {
         /* No macro recording in progress. */
         if (!record->event.pressed) {
@@ -238,15 +253,7 @@ bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
                 if (record->event.pressed ^ (keycode != QK_DYNAMIC_MACRO_RECORD_STOP)) { /* Ignore the initial release
                                                                                           * just after the recording
                                                                                           * starts for DM_RSTP. */
-                    switch (macro_id) {
-                        case 1:
-                            dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
-                            break;
-                        case 2:
-                            dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
-                            break;
-                    }
-                    macro_id = 0;
+                    dynamic_macro_stop_recording();
                 }
                 return false;
 #ifdef DYNAMIC_MACRO_NO_NESTING
diff --git a/quantum/process_keycode/process_dynamic_macro.h b/quantum/process_keycode/process_dynamic_macro.h
index ab70726897..9841254af4 100644
--- a/quantum/process_keycode/process_dynamic_macro.h
+++ b/quantum/process_keycode/process_dynamic_macro.h
@@ -39,3 +39,4 @@ void dynamic_macro_record_start_user(int8_t direction);
 void dynamic_macro_play_user(int8_t direction);
 void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record);
 void dynamic_macro_record_end_user(int8_t direction);
+void dynamic_macro_stop_recording(void);