keymap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include QMK_KEYBOARD_H
  2. #include "tetris_text.h"
  3. // Helpful defines
  4. #define GRAVE_MODS (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)|MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)|MOD_BIT(KC_LALT)|MOD_BIT(KC_RALT))
  5. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  6. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  7. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  8. // entirely and just use numbers.
  9. #define _BL 0
  10. #define _FL 1
  11. #define _CL 2
  12. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  13. /* Keymap _BL: Base Layer (Default Layer)
  14. */
  15. [_BL] = LAYOUT(
  16. F(0), 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_GRV, KC_BSPC, KC_PGUP,
  17. 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_PGDN,
  18. 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_NUHS, KC_ENT,
  19. 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_RO, KC_RSFT, KC_UP,
  20. KC_LCTL, KC_LGUI, KC_LALT, KC_MHEN, KC_SPC,KC_SPC, KC_HENK, KC_RALT, KC_RCTL, MO(_FL), KC_LEFT, KC_DOWN, KC_RGHT),
  21. /* Keymap _FL: Function Layer
  22. */
  23. [_FL] = LAYOUT(
  24. KC_GRV, 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_DEL, BL_STEP,
  25. _______, _______, _______,_______,_______,F(1) ,_______,_______,KC_PSCR,KC_SLCK, KC_PAUS, _______, _______, _______, _______,
  26. _______, _______, MO(_CL),_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______,
  27. _______, _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, KC_PGUP,
  28. _______, _______, _______, _______, _______,_______, _______, _______, _______, MO(_FL), KC_HOME, KC_PGDN, KC_END),
  29. /* Keymap _CL: Control layer
  30. */
  31. [_CL] = LAYOUT(
  32. _______, _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, RGB_TOG, RGB_VAI,
  33. _______, _______, _______,_______,RESET, _______,_______,_______,_______,_______, _______, _______, _______, _______, RGB_VAD,
  34. _______, _______, MO(_CL),_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______,
  35. MO(_FL), _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, MO(_FL), RGB_SAI,
  36. _______, _______, _______,_______, RGB_MOD, RGB_MOD, _______, _______, _______, _______, RGB_HUD, RGB_SAD, RGB_HUI),
  37. };
  38. /* This is a list of user defined functions. F(N) corresponds to item N
  39. of this list.
  40. */
  41. const uint16_t PROGMEM fn_actions[] = {
  42. [0] = ACTION_FUNCTION(0), // Calls action_function()
  43. [1] = ACTION_FUNCTION(1)
  44. };
  45. static uint8_t tetris_key_presses = 0;
  46. static uint16_t tetris_timer = 0;
  47. static uint8_t tetris_running = 0;
  48. static int tetris_keypress = 0;
  49. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) {
  50. static uint8_t mods_pressed;
  51. static bool mod_flag;
  52. switch (id) {
  53. case 0:
  54. // clueboard specific hook to make escape quite tetris
  55. if (tetris_running) {
  56. tetris_running = 0;
  57. return;
  58. }
  59. /* Handle the combined Grave/Esc key
  60. */
  61. mods_pressed = get_mods()&GRAVE_MODS; // Check to see what mods are pressed
  62. if (record->event.pressed) {
  63. /* The key is being pressed.
  64. */
  65. if (mods_pressed) {
  66. mod_flag = true;
  67. add_key(KC_GRV);
  68. send_keyboard_report();
  69. } else {
  70. add_key(KC_ESC);
  71. send_keyboard_report();
  72. }
  73. } else {
  74. /* The key is being released.
  75. */
  76. if (mod_flag) {
  77. mod_flag = false;
  78. del_key(KC_GRV);
  79. send_keyboard_report();
  80. } else {
  81. del_key(KC_ESC);
  82. send_keyboard_report();
  83. }
  84. }
  85. break;
  86. case 1:
  87. if (record->event.pressed) {
  88. tetris_running = 1;
  89. tetris_timer = 0;
  90. tetris_keypress = 0;
  91. // set randomness using total number of key presses
  92. tetris_start(tetris_key_presses);
  93. }
  94. break;
  95. }
  96. }
  97. /*
  98. * Set up tetris
  99. */
  100. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  101. if (record->event.pressed) {
  102. tetris_key_presses++;
  103. }
  104. if (tetris_running && record->event.pressed) {
  105. tetris_keypress = 0;
  106. switch (keycode) {
  107. case KC_UP: tetris_keypress = 1; break;
  108. case KC_LEFT: tetris_keypress = 2; break;
  109. case KC_DOWN: tetris_keypress = 3; break;
  110. case KC_RIGHT: tetris_keypress = 4; break;
  111. // Make ESC stop tetris (on keyboards other than clueboard)
  112. // case KC_ESC: tetris_running = 0; return false;
  113. }
  114. if (tetris_keypress != 0) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. // Runs constantly in the background, in a loop.
  121. void matrix_scan_user(void) {
  122. if (tetris_running) {
  123. tetris_timer++;
  124. if (tetris_timer > 1000) {
  125. // every 1000 times this is run is about 100 ms.
  126. if (!tetris_tick(100)) {
  127. // game over
  128. tetris_running = 0;
  129. }
  130. tetris_timer = 0;
  131. }
  132. }
  133. }
  134. void send_keycode(uint16_t keycode) {
  135. register_code(keycode);
  136. unregister_code(keycode);
  137. }
  138. void send_keycode_shift(uint16_t keycode) {
  139. register_code(KC_LSFT);
  140. register_code(keycode);
  141. unregister_code(keycode);
  142. unregister_code(KC_LSFT);
  143. }
  144. void tetris_send_up(void) {
  145. send_keycode(KC_UP);
  146. }
  147. void tetris_send_left(void) {
  148. send_keycode(KC_LEFT);
  149. }
  150. void tetris_send_down(void) {
  151. send_keycode(KC_DOWN);
  152. }
  153. void tetris_send_right(void) {
  154. send_keycode(KC_RGHT);
  155. }
  156. void tetris_send_backspace(void) {
  157. send_keycode(KC_BSPC);
  158. }
  159. void tetris_send_delete(void) {
  160. send_keycode(KC_DEL);
  161. }
  162. void tetris_send_string(const char *s) {
  163. for (int i = 0; s[i] != 0; i++) {
  164. if (s[i] >= 'a' && s[i] <= 'z') {
  165. send_keycode(KC_A + (s[i] - 'a'));
  166. } else if (s[i] >= 'A' && s[i] <= 'Z') {
  167. send_keycode_shift(KC_A + (s[i] - 'A'));
  168. } else if (s[i] >= '1' && s[i] <= '9') {
  169. send_keycode(KC_1 + (s[i] - '1'));
  170. } else {
  171. switch (s[i]) {
  172. case ' ': send_keycode(KC_SPACE); break;
  173. case '.': send_keycode(KC_DOT); break;
  174. case '0': send_keycode(KC_0); break;
  175. }
  176. }
  177. }
  178. }
  179. void tetris_send_newline(void) {
  180. send_keycode(KC_ENT);
  181. }
  182. int tetris_get_keypress(void) {
  183. int out = tetris_keypress;
  184. tetris_keypress = 0;
  185. return out;
  186. }