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.

162 lines
4.3 KiB

  1. enum chord_states {
  2. IDLE,
  3. READY,
  4. ACTIVATED,
  5. DEACTIVATED,
  6. PRESS_FROM_ACTIVE,
  7. FINISHED_FROM_ACTIVE,
  8. IDLE_IN_DANCE,
  9. READY_IN_DANCE,
  10. FINISHED,
  11. LOCKED,
  12. READY_LOCKED,
  13. RESTART,
  14. IN_ONE_SHOT
  15. };
  16. struct Chord {
  17. uint32_t keycodes_hash;
  18. uint8_t pseudolayer;
  19. uint8_t* state;
  20. uint8_t* counter;
  21. uint16_t value1;
  22. uint8_t value2;
  23. void (*function) (const struct Chord*);
  24. };
  25. uint8_t current_pseudolayer = DEFAULT_PSEUDOLAYER;
  26. bool lock_next = false;
  27. uint16_t chord_timer = 0;
  28. uint16_t dance_timer = 0;
  29. bool autoshift_mode = true;
  30. uint8_t keycode_index = 0;
  31. uint8_t command_mode = 0;
  32. uint8_t command_ind = 0;
  33. bool in_leader_mode = false;
  34. uint8_t leader_ind = 0;
  35. uint16_t leader_timer = 0;
  36. uint8_t dynamic_macro_mode = false;
  37. uint8_t dynamic_macro_ind = 0;
  38. bool a_key_went_through = false;
  39. struct Chord* last_chord = NULL;
  40. bool handle_US_ANSI_shifted_keys(int16_t keycode, bool in) {
  41. bool is_US_ANSI_shifted = true;
  42. int16_t regular_keycode = KC_NO;
  43. switch (keycode) {
  44. case KC_TILDE:
  45. regular_keycode = KC_GRAVE;
  46. break;
  47. case KC_EXCLAIM:
  48. regular_keycode = KC_1;
  49. break;
  50. case KC_AT:
  51. regular_keycode = KC_2;
  52. break;
  53. case KC_HASH:
  54. regular_keycode = KC_3;
  55. break;
  56. case KC_DOLLAR:
  57. regular_keycode = KC_4;
  58. break;
  59. case KC_PERCENT:
  60. regular_keycode = KC_5;
  61. break;
  62. case KC_CIRCUMFLEX:
  63. regular_keycode = KC_6;
  64. break;
  65. case KC_AMPERSAND:
  66. regular_keycode = KC_7;
  67. break;
  68. case KC_ASTERISK:
  69. regular_keycode = KC_8;
  70. break;
  71. case KC_LEFT_PAREN:
  72. regular_keycode = KC_9;
  73. break;
  74. case KC_RIGHT_PAREN:
  75. regular_keycode = KC_0;
  76. break;
  77. case KC_UNDERSCORE:
  78. regular_keycode = KC_MINUS;
  79. break;
  80. case KC_PLUS:
  81. regular_keycode = KC_EQUAL;
  82. break;
  83. case KC_LEFT_CURLY_BRACE:
  84. regular_keycode = KC_LBRACKET;
  85. break;
  86. case KC_RIGHT_CURLY_BRACE:
  87. regular_keycode = KC_RBRACKET;
  88. break;
  89. case KC_PIPE:
  90. regular_keycode = KC_BSLASH;
  91. break;
  92. case KC_COLON:
  93. regular_keycode = KC_SCOLON;
  94. break;
  95. case KC_DOUBLE_QUOTE:
  96. regular_keycode = KC_QUOTE;
  97. break;
  98. case KC_LEFT_ANGLE_BRACKET:
  99. regular_keycode = KC_COMMA;
  100. break;
  101. case KC_RIGHT_ANGLE_BRACKET:
  102. regular_keycode = KC_DOT;
  103. break;
  104. case KC_QUESTION:
  105. regular_keycode = KC_SLASH;
  106. break;
  107. default:
  108. is_US_ANSI_shifted = false;
  109. }
  110. if (is_US_ANSI_shifted) {
  111. if (in) {
  112. register_code(KC_LSFT);
  113. register_code(regular_keycode);
  114. } else {
  115. unregister_code(regular_keycode);
  116. unregister_code(KC_LSFT);
  117. }
  118. }
  119. return is_US_ANSI_shifted;
  120. }
  121. void key_in(int16_t keycode) {
  122. if (command_mode == 1 && command_ind < COMMAND_MAX_LENGTH) {
  123. command_buffer[command_ind] = keycode;
  124. command_ind++;
  125. a_key_went_through = true;
  126. } else if (in_leader_mode && leader_ind < LEADER_MAX_LENGTH) {
  127. leader_buffer[leader_ind] = keycode;
  128. leader_ind++;
  129. a_key_went_through = true;
  130. } else if (dynamic_macro_mode && dynamic_macro_ind < DYNAMIC_MACRO_MAX_LENGTH) {
  131. dynamic_macro_buffer[dynamic_macro_ind] = keycode;
  132. dynamic_macro_ind++;
  133. a_key_went_through = true;
  134. } else {
  135. if (!handle_US_ANSI_shifted_keys(keycode, true)) {
  136. register_code(keycode);
  137. }
  138. send_keyboard_report();
  139. a_key_went_through = true;
  140. }
  141. }
  142. void key_out(int16_t keycode) {
  143. if (command_mode == 0) {
  144. if (!handle_US_ANSI_shifted_keys(keycode, false)) {
  145. if (command_mode == 0 && in_leader_mode == false && dynamic_macro_mode == false) {
  146. unregister_code(keycode);
  147. }
  148. }
  149. send_keyboard_report();
  150. }
  151. }
  152. void tap_key(int16_t keycode) {
  153. key_in(keycode);
  154. wait_ms(TAP_TIMEOUT);
  155. key_out(keycode);
  156. }