action_macro.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef ACTION_MACRO_H
  15. #define ACTION_MACRO_H
  16. #include <stdint.h>
  17. #include "progmem.h"
  18. typedef uint8_t macro_t;
  19. #define MACRO_NONE (macro_t *)0
  20. #define MACRO(...) \
  21. ({ \
  22. static const macro_t __m[] PROGMEM = {__VA_ARGS__}; \
  23. &__m[0]; \
  24. })
  25. #define MACRO_GET(p) pgm_read_byte(p)
  26. // Sends press when the macro key is pressed, release when release, or tap_macro when the key has been tapped
  27. #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)))
  28. // Holds down the modifier mod when the macro key is held, or sends macro instead when tapped
  29. #define MACRO_TAP_HOLD_MOD(record, macro, mod) MACRO_TAP_HOLD(record, (MACRO(D(mod), END)), MACRO(U(mod), END), macro)
  30. // Holds down the modifier mod when the macro key is held, or pressed a shifted key when tapped (eg: shift+3 for #)
  31. #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)
  32. // Momentary switch layer when held, sends macro if tapped
  33. #define MACRO_TAP_HOLD_LAYER(record, macro, layer) \
  34. (((record)->event.pressed) ? (((record)->tap.count <= 0 || (record)->tap.interrupted) ? ({ \
  35. layer_on((layer)); \
  36. MACRO_NONE; \
  37. }) \
  38. : MACRO_NONE) \
  39. : (((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (macro) : ({ \
  40. layer_off((layer)); \
  41. MACRO_NONE; \
  42. })))
  43. // Momentary switch layer when held, presses a shifted key when tapped (eg: shift+3 for #)
  44. #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)
  45. #ifndef NO_ACTION_MACRO
  46. void action_macro_play(const macro_t *macro_p);
  47. #else
  48. # define action_macro_play(macro)
  49. #endif
  50. /* Macro commands
  51. * code(0x04-73) // key down(1byte)
  52. * code(0x04-73) | 0x80 // key up(1byte)
  53. * { KEY_DOWN, code(0x04-0xff) } // key down(2bytes)
  54. * { KEY_UP, code(0x04-0xff) } // key up(2bytes)
  55. * WAIT // wait milli-seconds
  56. * INTERVAL // set interval between macro commands
  57. * END // stop macro execution
  58. *
  59. * Ideas(Not implemented):
  60. * modifiers
  61. * system usage
  62. * consumer usage
  63. * unicode usage
  64. * function call
  65. * conditionals
  66. * loop
  67. */
  68. enum macro_command_id {
  69. /* 0x00 - 0x03 */
  70. END = 0x00,
  71. KEY_DOWN,
  72. KEY_UP,
  73. /* 0x04 - 0x73 (reserved for keycode down) */
  74. /* 0x74 - 0x83 */
  75. WAIT = 0x74,
  76. INTERVAL,
  77. /* 0x84 - 0xf3 (reserved for keycode up) */
  78. /* 0xf4 - 0xff */
  79. };
  80. /* TODO: keycode:0x04-0x73 can be handled by 1byte command else 2bytes are needed
  81. * if keycode between 0x04 and 0x73
  82. * keycode / (keycode|0x80)
  83. * else
  84. * {KEY_DOWN, keycode} / {KEY_UP, keycode}
  85. */
  86. #define DOWN(key) KEY_DOWN, (key)
  87. #define UP(key) KEY_UP, (key)
  88. #define TYPE(key) DOWN(key), UP(key)
  89. #define WAIT(ms) WAIT, (ms)
  90. #define INTERVAL(ms) INTERVAL, (ms)
  91. /* key down */
  92. #define D(key) DOWN(KC_##key)
  93. /* key up */
  94. #define U(key) UP(KC_##key)
  95. /* key type */
  96. #define T(key) TYPE(KC_##key)
  97. /* wait */
  98. #define W(ms) WAIT(ms)
  99. /* interval */
  100. #define I(ms) INTERVAL(ms)
  101. /* for backward comaptibility */
  102. #define MD(key) DOWN(KC_##key)
  103. #define MU(key) UP(KC_##key)
  104. #endif /* ACTION_MACRO_H */