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.

129 lines
3.5 KiB

  1. /* Copyright 2021 Joshua T.
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "num_word.h"
  17. static uint16_t num_word_timer = 0;
  18. static bool is_num_word_on = false;
  19. bool is_num_word_enabled(void) {
  20. return is_num_word_on;
  21. }
  22. void enable_num_word(void) {
  23. if (is_num_word_on) return;
  24. is_num_word_on = true;
  25. layer_on(L_NUMBERS);
  26. }
  27. void disable_num_word(void) {
  28. if (!is_num_word_on) return;
  29. is_num_word_on = false;
  30. layer_off(L_NUMBERS);
  31. }
  32. void toggle_num_word(void) {
  33. if (is_num_word_on) {
  34. disable_num_word();
  35. }
  36. else {
  37. enable_num_word();
  38. }
  39. }
  40. bool should_terminate_num_word(uint16_t keycode, const keyrecord_t *record) {
  41. switch (keycode) {
  42. // Keycodes which should not disable num word mode.
  43. // We could probably be more brief with these definitions by using
  44. // a couple more ranges, but I believe "explicit is better than
  45. // implicit"
  46. case KC_1 ... KC_0:
  47. case KC_EQL:
  48. case KC_SCLN:
  49. case KC_MINS:
  50. case KC_DOT:
  51. // Numpad keycodes
  52. case KC_P1 ... KC_P0:
  53. case KC_PSLS ... KC_PPLS:
  54. case KC_PDOT:
  55. // Misc
  56. case KC_UNDS:
  57. case KC_BSPC:
  58. return false;
  59. default:
  60. if (record->event.pressed) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. // Should be unreachable
  66. return false;
  67. }
  68. bool process_record_num_word(uint16_t keycode, const keyrecord_t *record) {
  69. // Handle the custom keycodes that go with this feature
  70. if (keycode == NUMWORD) {
  71. if (record->event.pressed) {
  72. enable_num_word();
  73. num_word_timer = timer_read();
  74. return false;
  75. }
  76. else {
  77. if (timer_elapsed(num_word_timer) > TAPPING_TERM) {
  78. // If the user held the key longer than TAPPING_TERM,
  79. // consider it a hold, and disable the behavior on
  80. // key release.
  81. disable_num_word();
  82. return false;
  83. }
  84. }
  85. }
  86. // Other than the custom keycodes, nothing else in this feature will
  87. // activate if the behavior is not on, so allow QMK to handle the
  88. // event as usual
  89. if (!is_num_word_on) return true;
  90. // Nothing else acts on key release, either
  91. if (!record->event.pressed) {
  92. return true;
  93. }
  94. // Get the base keycode of a mod or layer tap key
  95. switch (keycode) {
  96. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  97. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  98. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  99. // Earlier return if this has not been considered tapped yet
  100. if (record->tap.count == 0)
  101. return true;
  102. keycode = keycode & 0xFF;
  103. break;
  104. default:
  105. break;
  106. }
  107. if (should_terminate_num_word(keycode, record)) {
  108. disable_num_word();
  109. }
  110. return true;
  111. }