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.

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