keymap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright 2020 Jay Greco
  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 QMK_KEYBOARD_H
  17. enum layer_names {
  18. _BASE,
  19. _VIA1,
  20. _VIA2,
  21. _VIA3
  22. };
  23. #define KC_DISC_MUTE KC_F23
  24. #define KC_DISC_DEAF KC_F24
  25. #define NUM_CUST_KEYCODES (_NUM_CUST_KCS - SAFE_RANGE)
  26. #define VIA_KEYCODE_RANGE 0x5F80
  27. enum custom_keycodes {
  28. PROG = SAFE_RANGE,
  29. DISC_MUTE,
  30. DISC_DEAF,
  31. SUPER_ALT_TAB,
  32. _NUM_CUST_KCS,
  33. };
  34. // Macro variables
  35. bool is_alt_tab_active = false;
  36. uint16_t alt_tab_timer = 0;
  37. bool muted = false;
  38. bool deafened = false;
  39. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  40. [_BASE] = LAYOUT_all(
  41. KC_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_HOME,
  42. KC_F13, KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL,
  43. KC_F14, KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_PGUP,
  44. KC_F15, KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_PGDN,
  45. KC_F16, KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, MO(_VIA1), KC_RALT, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT
  46. ),
  47. [_VIA1] = LAYOUT_all(
  48. RESET, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, KC_END,
  49. RGB_TOG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  50. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  51. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  52. _______, _______, _______, _______, _______, _______, _______, _______, KC_MPRV, KC_MPLY, KC_MNXT
  53. ),
  54. [_VIA2] = LAYOUT_all(
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  58. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  59. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  60. ),
  61. [_VIA3] = LAYOUT_all(
  62. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  64. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  65. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  66. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  67. ),
  68. };
  69. void map_via_keycode(uint16_t * keycode) {
  70. if (abs(*keycode - VIA_KEYCODE_RANGE) < NUM_CUST_KEYCODES) { //make into macro?
  71. dprintf("VIA custom keycode found, mapping to QMK keycode.\n");
  72. uint16_t new_keycode = (*keycode - VIA_KEYCODE_RANGE) + SAFE_RANGE;
  73. dprintf("VIA KC: %u QMK KC: %u\n", *keycode, new_keycode);
  74. *keycode = new_keycode;
  75. }
  76. }
  77. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  78. map_via_keycode(&keycode);
  79. // Send keystrokes to host keyboard, if connected (see readme)
  80. process_record_remote_kb(keycode, record);
  81. switch(keycode) {
  82. case PROG:
  83. if (record->event.pressed) {
  84. rgblight_disable_noeeprom();
  85. bootloader_jump();
  86. }
  87. break;
  88. case DISC_MUTE:
  89. if (record->event.pressed) {
  90. tap_code(KC_DISC_MUTE);
  91. if (!rgblight_is_enabled()) break;
  92. if (muted) {
  93. rgblight_enable_noeeprom();
  94. } else {
  95. rgblight_timer_disable();
  96. uint8_t val = rgblight_get_val();
  97. rgblight_sethsv_range(255, 255, val, 0, 1);
  98. }
  99. muted = !muted;
  100. }
  101. break;
  102. case DISC_DEAF:
  103. if (record->event.pressed) {
  104. tap_code(KC_DISC_DEAF);
  105. if (!rgblight_is_enabled()) break;
  106. if (deafened) {
  107. rgblight_enable_noeeprom();
  108. } else {
  109. rgblight_timer_disable();
  110. uint8_t val = rgblight_get_val();
  111. rgblight_sethsv_range(255, 255, val, 0, RGBLED_NUM-1);
  112. }
  113. deafened = !deafened;
  114. }
  115. break;
  116. case SUPER_ALT_TAB:
  117. if (record->event.pressed) {
  118. if (!is_alt_tab_active) {
  119. is_alt_tab_active = true;
  120. register_code(KC_LALT);
  121. }
  122. alt_tab_timer = timer_read();
  123. register_code(KC_TAB);
  124. } else {
  125. unregister_code(KC_TAB);
  126. }
  127. break;
  128. default:
  129. break;
  130. }
  131. return true;
  132. }
  133. bool encoder_update_user(uint8_t index, bool clockwise) {
  134. // Encoder is mapped to volume functions by default
  135. if (clockwise) {
  136. tap_code(KC_VOLU);
  137. } else {
  138. tap_code(KC_VOLD);
  139. }
  140. return true;
  141. }
  142. void matrix_init_user(void) {
  143. // Initialize remote keyboard, if connected (see readme)
  144. matrix_init_remote_kb();
  145. }
  146. void matrix_scan_user(void) {
  147. // Scan and parse keystrokes from remote keyboard, if connected (see readme)
  148. matrix_scan_remote_kb();
  149. if (is_alt_tab_active) {
  150. if (timer_elapsed(alt_tab_timer) > 1000) {
  151. unregister_code(KC_LALT);
  152. is_alt_tab_active = false;
  153. }
  154. }
  155. }