]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/dynamic_macro.h
Merge remote-tracking branch 'upstream/master' into dev
[qmk_firmware.git] / quantum / dynamic_macro.h
index a3ad61bc7e46161f4ee6dba1fe78ed56fd5fe6b2..9e7845c9928563be754a16101e0f353c632a5d9d 100644 (file)
@@ -1,3 +1,19 @@
+/* Copyright 2016 Jack Humbert
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
 #ifndef DYNAMIC_MACROS_H
 #define DYNAMIC_MACROS_H
@@ -8,8 +24,13 @@
 /* May be overridden with a custom value. Be aware that the effective
  * macro length is half of this value: each keypress is recorded twice
  * because of the down-event and up-event. This is not a bug, it's the
- * intended behavior. */
-#define DYNAMIC_MACRO_SIZE 256
+ * intended behavior.
+ *
+ * Usually it should be fine to set the macro size to at least 256 but
+ * there have been reports of it being too much in some users' cases,
+ * so 128 is considered a safe default.
+ */
+#define DYNAMIC_MACRO_SIZE 128
 #endif
 
 /* DYNAMIC_MACRO_RANGE must be set as the last element of user's
@@ -19,6 +40,7 @@
 enum dynamic_macro_keycodes {
     DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
     DYN_REC_START2,
+    DYN_REC_STOP,
     DYN_MACRO_PLAY1,
     DYN_MACRO_PLAY2,
 };
@@ -75,24 +97,29 @@ void dynamic_macro_play(
 /**
  * Record a single key in a dynamic macro.
  *
+ * @param macro_buffer[in] The start of the used macro buffer.
  * @param macro_pointer[in,out] The current buffer position.
- * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
+ * @param macro2_end[in] The last buffer element it is safe to use before overwriting the other macro.
  * @param direction[in]  Either +1 or -1, which way to iterate the buffer.
  * @param record[in]     The current keypress.
  */
 void dynamic_macro_record_key(
+    keyrecord_t *macro_buffer,
     keyrecord_t **macro_pointer,
-    keyrecord_t *macro_end2,
+    keyrecord_t *macro2_end,
     int8_t direction,
     keyrecord_t *record)
 {
-    if (*macro_pointer + direction != macro_end2) {
+    /* If we've just started recording, ignore all the key releases. */
+    if (!record->event.pressed && *macro_pointer == macro_buffer) {
+        return;
+    }
+
+    if (*macro_pointer - direction != macro2_end) {
         **macro_pointer = *record;
         *macro_pointer += direction;
     } else {
-        /* Notify about the end of buffer. The blinks are paired
-         * because they should happen on both down and up events. */
-        backlight_toggle();
+        dynamic_macro_led_blink();
     }
 }
 
@@ -188,9 +215,8 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
     } else {
         /* A macro is being recorded right now. */
         switch (keycode) {
-        case MO(_DYN):
-            /* Use the layer key used to access the macro recording as
-             * a stop button. */
+        case DYN_REC_STOP:
+            /* Stop the macro recording. */
             if (record->event.pressed) { /* Ignore the initial release
                                           * just after the recoding
                                           * starts. */
@@ -209,10 +235,10 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
             /* Store the key in the macro buffer and process it normally. */
             switch (macro_id) {
             case 1:
-                dynamic_macro_record_key(&macro_pointer, r_macro_end, +1, record);
+                dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
                 break;
             case 2:
-                dynamic_macro_record_key(&macro_pointer, macro_end, -1, record);
+                dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
                 break;
             }
             return true;