action_macro.h 4.1 KB

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