keymap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "via_extras.h"
  18. #define _BASE 0
  19. #define _VIA1 1
  20. #define _VIA2 2
  21. #define _VIA3 3
  22. #define KC_DISC_MUTE KC_F23
  23. #define KC_DISC_DEAF KC_F24
  24. #define NUM_CUST_KEYCODES (_NUM_CUST_KCS - SAFE_RANGE)
  25. #define VIA_KEYCODE_RANGE 0x5F80
  26. enum custom_keycodes {
  27. PROG = SAFE_RANGE,
  28. DISC_MUTE,
  29. DISC_DEAF,
  30. SUPER_ALT_TAB,
  31. _NUM_CUST_KCS,
  32. };
  33. // Macro variables
  34. bool is_alt_tab_active = false;
  35. uint16_t alt_tab_timer = 0;
  36. bool muted = false;
  37. bool deafened = false;
  38. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  39. [_BASE] = LAYOUT_all(
  40. 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,
  41. 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,
  42. 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,
  43. 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,
  44. KC_F16, KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, MO(_VIA1), KC_RALT, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT
  45. ),
  46. [_VIA1] = LAYOUT_all(
  47. 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_HOME, KC_INS,
  48. RGB_TOG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  49. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  50. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  51. _______, _______, _______, _______, _______, _______, _______, _______, KC_MPRV, KC_MPLY, KC_MNXT
  52. ),
  53. [_VIA2] = LAYOUT_all(
  54. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  58. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  59. ),
  60. [_VIA3] = LAYOUT_all(
  61. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  62. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  64. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  65. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  66. ),
  67. };
  68. void map_via_keycode(uint16_t * keycode) {
  69. if (abs(*keycode - VIA_KEYCODE_RANGE) < NUM_CUST_KEYCODES) { //make into macro?
  70. dprintf("VIA custom keycode found, mapping to QMK keycode.\n");
  71. uint16_t new_keycode = (*keycode - VIA_KEYCODE_RANGE) + SAFE_RANGE;
  72. dprintf("VIA KC: %u QMK KC: %u\n", *keycode, new_keycode);
  73. *keycode = new_keycode;
  74. }
  75. }
  76. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  77. map_via_keycode(&keycode);
  78. // Send keystrokes to host keyboard, if connected (see readme)
  79. process_record_remote_kb(keycode, record);
  80. switch(keycode) {
  81. case PROG:
  82. if (record->event.pressed) {
  83. rgblight_disable_noeeprom();
  84. bootloader_jump();
  85. }
  86. break;
  87. case DISC_MUTE:
  88. if (record->event.pressed) {
  89. tap_code(KC_DISC_MUTE);
  90. if (!rgblight_is_enabled()) break;
  91. if (muted) {
  92. rgblight_enable_noeeprom();
  93. } else {
  94. rgblight_timer_disable();
  95. uint8_t val = rgblight_get_val();
  96. rgblight_sethsv_range(255, 255, val, 0, 1);
  97. }
  98. muted = !muted;
  99. }
  100. break;
  101. case DISC_DEAF:
  102. if (record->event.pressed) {
  103. tap_code(KC_DISC_DEAF);
  104. if (!rgblight_is_enabled()) break;
  105. if (deafened) {
  106. rgblight_enable_noeeprom();
  107. } else {
  108. rgblight_timer_disable();
  109. uint8_t val = rgblight_get_val();
  110. rgblight_sethsv_range(255, 255, val, 0, RGBLED_NUM-1);
  111. }
  112. deafened = !deafened;
  113. }
  114. break;
  115. case SUPER_ALT_TAB:
  116. if (record->event.pressed) {
  117. if (!is_alt_tab_active) {
  118. is_alt_tab_active = true;
  119. register_code(KC_LALT);
  120. }
  121. alt_tab_timer = timer_read();
  122. register_code(KC_TAB);
  123. } else {
  124. unregister_code(KC_TAB);
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. return true;
  131. }
  132. void matrix_init_user(void) {
  133. // Initialize remote keyboard, if connected (see readme)
  134. matrix_init_remote_kb();
  135. }
  136. void matrix_scan_user(void) {
  137. // Scan and parse keystrokes from remote keyboard, if connected (see readme)
  138. matrix_scan_remote_kb();
  139. if (is_alt_tab_active) {
  140. if (timer_elapsed(alt_tab_timer) > 1000) {
  141. unregister_code(KC_LALT);
  142. is_alt_tab_active = false;
  143. }
  144. }
  145. }