You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.8 KiB

  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <stdint.h>
  16. #include "progmem.h"
  17. typedef uint8_t macro_t;
  18. #define MACRO_NONE (macro_t *)0
  19. #define MACRO(...) \
  20. ({ \
  21. static const macro_t __m[] PROGMEM = {__VA_ARGS__}; \
  22. &__m[0]; \
  23. })
  24. #define MACRO_GET(p) pgm_read_byte(p)
  25. // Sends press when the macro key is pressed, release when release, or tap_macro when the key has been tapped
  26. #define MACRO_TAP_HOLD(record, press, release, tap_macro) (((record)->event.pressed) ? (((record)->tap.count <= 0 || (record)->tap.interrupted) ? (press) : MACRO_NONE) : (((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (tap_macro) : (release)))
  27. // Holds down the modifier mod when the macro key is held, or sends macro instead when tapped
  28. #define MACRO_TAP_HOLD_MOD(record, macro, mod) MACRO_TAP_HOLD(record, (MACRO(D(mod), END)), MACRO(U(mod), END), macro)
  29. // Holds down the modifier mod when the macro key is held, or pressed a shifted key when tapped (eg: shift+3 for #)
  30. #define MACRO_TAP_SHFT_KEY_HOLD_MOD(record, key, mod) MACRO_TAP_HOLD_MOD(record, (MACRO(I(10), D(LSFT), T(key), U(LSFT), END)), mod)
  31. // Momentary switch layer when held, sends macro if tapped
  32. #define MACRO_TAP_HOLD_LAYER(record, macro, layer) \
  33. (((record)->event.pressed) ? (((record)->tap.count <= 0 || (record)->tap.interrupted) ? ({ \
  34. layer_on((layer)); \
  35. MACRO_NONE; \
  36. }) \
  37. : MACRO_NONE) \
  38. : (((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (macro) : ({ \
  39. layer_off((layer)); \
  40. MACRO_NONE; \
  41. })))
  42. // Momentary switch layer when held, presses a shifted key when tapped (eg: shift+3 for #)
  43. #define MACRO_TAP_SHFT_KEY_HOLD_LAYER(record, key, layer) MACRO_TAP_HOLD_LAYER(record, MACRO(I(10), D(LSFT), T(key), U(LSFT), END), layer)
  44. #ifndef NO_ACTION_MACRO
  45. void action_macro_play(const macro_t *macro_p);
  46. #else
  47. # define action_macro_play(macro)
  48. #endif
  49. /* Macro commands
  50. * code(0x04-73) // key down(1byte)
  51. * code(0x04-73) | 0x80 // key up(1byte)
  52. * { KEY_DOWN, code(0x04-0xff) } // key down(2bytes)
  53. * { KEY_UP, code(0x04-0xff) } // key up(2bytes)
  54. * WAIT // wait milli-seconds
  55. * INTERVAL // set interval between macro commands
  56. * END // stop macro execution
  57. *
  58. * Ideas(Not implemented):
  59. * modifiers
  60. * system usage
  61. * consumer usage
  62. * unicode usage
  63. * function call
  64. * conditionals
  65. * loop
  66. */
  67. enum macro_command_id {
  68. /* 0x00 - 0x03 */
  69. END = 0x00,
  70. KEY_DOWN,
  71. KEY_UP,
  72. /* 0x04 - 0x73 (reserved for keycode down) */
  73. /* 0x74 - 0x83 */
  74. WAIT = 0x74,
  75. INTERVAL,
  76. /* 0x84 - 0xf3 (reserved for keycode up) */
  77. /* 0xf4 - 0xff */
  78. };
  79. /* TODO: keycode:0x04-0x73 can be handled by 1byte command else 2bytes are needed
  80. * if keycode between 0x04 and 0x73
  81. * keycode / (keycode|0x80)
  82. * else
  83. * {KEY_DOWN, keycode} / {KEY_UP, keycode}
  84. */
  85. #define DOWN(key) KEY_DOWN, (key)
  86. #define UP(key) KEY_UP, (key)
  87. #define TYPE(key) DOWN(key), UP(key)
  88. #define WAIT(ms) WAIT, (ms)
  89. #define INTERVAL(ms) INTERVAL, (ms)
  90. /* key down */
  91. #define D(key) DOWN(KC_##key)
  92. /* key up */
  93. #define U(key) UP(KC_##key)
  94. /* key type */
  95. #define T(key) TYPE(KC_##key)
  96. /* wait */
  97. #define W(ms) WAIT(ms)
  98. /* interval */
  99. #define I(ms) INTERVAL(ms)
  100. /* for backward comaptibility */
  101. #define MD(key) DOWN(KC_##key)
  102. #define MU(key) UP(KC_##key)