]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Improved dynamic keymaps (#3972)
[qmk_firmware.git] / users / drashna / drashna.c
1 /*
2 Copyright 2017 Christopher Courtney <drashna@live.com> @drashna
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "drashna.h"
19 #include "tap_dances.h"
20 #include "rgb_stuff.h"
21
22 userspace_config_t userspace_config;
23
24 //  Helper Functions
25
26
27 // This block is for all of the gaming macros, as they were all doing
28 // the same thing, but with differring text sent.
29 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
30   if (!record->event.pressed || override) {
31     uint16_t keycode;
32     if (userspace_config.is_overwatch) {
33       keycode = KC_BSPC;
34     } else {
35       keycode = KC_ENTER;
36     }
37     clear_keyboard();
38     tap(keycode);
39     wait_ms(50);
40     send_string_with_delay(str, MACRO_TIMER);
41     wait_ms(50);
42     tap(KC_ENTER);
43   }
44   if (override) wait_ms(3000);
45   return false;
46 }
47
48 bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
49   static uint16_t this_timer;
50   if(pressed) {
51       this_timer= timer_read();
52   } else {
53       if (timer_elapsed(this_timer) < TAPPING_TERM){
54           register_code(code);
55           unregister_code(code);
56       } else {
57           register_code(mod_code);
58           register_code(code);
59           unregister_code(code);
60           unregister_code(mod_code);
61       }
62   }
63   return false;
64 }
65
66 bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
67   if(pressed) {
68       this_timer= timer_read();
69   } else {
70       if (timer_elapsed(this_timer) < TAPPING_TERM){
71           register_code(code);
72           unregister_code(code);
73       } else {
74           register_code(mod_code);
75           register_code(code);
76           unregister_code(code);
77           unregister_code(mod_code);
78       }
79   }
80   return false;
81 }
82
83 // Add reconfigurable functions here, for keymap customization
84 // This allows for a global, userspace functions, and continued
85 // customization of the keymap.  Use _keymap instead of _user
86 // functions in the keymaps
87 __attribute__ ((weak))
88 void matrix_init_keymap(void) {}
89
90 __attribute__ ((weak))
91 void startup_keymap(void) {}
92
93 __attribute__ ((weak))
94 void shutdown_keymap(void) {}
95
96 __attribute__ ((weak))
97 void suspend_power_down_keymap(void) {}
98
99 __attribute__ ((weak))
100 void suspend_wakeup_init_keymap(void) {}
101
102 __attribute__ ((weak))
103 void matrix_scan_keymap(void) {}
104
105 __attribute__ ((weak))
106 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
107   return true;
108 }
109
110 __attribute__ ((weak))
111 bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
112   return true;
113 }
114
115
116 __attribute__ ((weak))
117 uint32_t layer_state_set_keymap (uint32_t state) {
118   return state;
119 }
120
121 __attribute__ ((weak))
122 uint32_t default_layer_state_set_keymap (uint32_t state) {
123   return state;
124 }
125
126 __attribute__ ((weak))
127 void led_set_keymap(uint8_t usb_led) {}
128
129
130
131 // Call user matrix init, set default RGB colors and then
132 // call the keymap's init function
133 void matrix_init_user(void) {
134   userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
135
136
137 #ifdef BOOTLOADER_CATERINA
138   DDRD &= ~(1<<5);
139   PORTD &= ~(1<<5);
140
141   DDRB &= ~(1<<0);
142   PORTB &= ~(1<<0);
143 #endif
144
145 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
146         set_unicode_input_mode(UC_WINC);
147 #endif //UNICODE_ENABLE
148   matrix_init_keymap();
149 }
150
151 void startup_user (void) {
152   #ifdef RGBLIGHT_ENABLE
153     matrix_init_rgb();
154   #endif //RGBLIGHT_ENABLE
155   startup_keymap();
156 }
157
158 void shutdown_user (void) {
159 #ifdef RGBLIGHT_ENABLE
160   rgblight_enable_noeeprom();
161   rgblight_mode_noeeprom(1);
162   rgblight_setrgb_red();
163 #endif // RGBLIGHT_ENABLE
164 #ifdef RGB_MATRIX_ENABLE
165   rgb_led led;
166   for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
167     led = g_rgb_leds[i];
168     if (led.matrix_co.raw < 0xFF) {
169       rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
170     }
171   }
172 #endif //RGB_MATRIX_ENABLE
173   shutdown_keymap();
174 }
175
176 void suspend_power_down_user(void)
177 {
178     suspend_power_down_keymap();
179 }
180
181 void suspend_wakeup_init_user(void)
182 {
183   suspend_wakeup_init_keymap();
184   #ifdef KEYBOARD_ergodox_ez
185   wait_ms(10);
186   #endif
187 }
188
189
190 // No global matrix scan code, so just run keymap's matrix
191 // scan function
192 void matrix_scan_user(void) {
193   static bool has_ran_yet;
194   if (!has_ran_yet) {
195     has_ran_yet = true;
196     startup_user();
197   }
198
199 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
200   run_diablo_macro_check();
201 #endif // TAP_DANCE_ENABLE
202
203 #ifdef RGBLIGHT_ENABLE
204   matrix_scan_rgb();
205 #endif // RGBLIGHT_ENABLE
206
207   matrix_scan_keymap();
208 }
209
210
211
212
213 // Defines actions tor my global custom keycodes. Defined in drashna.h file
214 // Then runs the _keymap's record handier if not processed here
215 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
216
217   // If console is enabled, it will print the matrix position and status of each key pressed
218 #ifdef KEYLOGGER_ENABLE
219   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
220 #endif //KEYLOGGER_ENABLE
221
222   switch (keycode) {
223   case KC_QWERTY:
224     if (record->event.pressed) {
225       set_single_persistent_default_layer(_QWERTY);
226     }
227     return false;
228     break;
229   case KC_COLEMAK:
230     if (record->event.pressed) {
231       set_single_persistent_default_layer(_COLEMAK);
232     }
233     return false;
234     break;
235   case KC_DVORAK:
236     if (record->event.pressed) {
237       set_single_persistent_default_layer(_DVORAK);
238     }
239     return false;
240     break;
241   case KC_WORKMAN:
242     if (record->event.pressed) {
243       set_single_persistent_default_layer(_WORKMAN);
244     }
245     return false;
246     break;
247
248
249   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
250     if (!record->event.pressed) {
251       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
252 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
253                    ":dfu"
254 #elif defined(BOOTLOADER_HALFKAY)
255                    ":teensy"
256 #elif defined(BOOTLOADER_CATERINA)
257                    ":avrdude"
258 #endif // bootloader options
259                    SS_TAP(X_ENTER)), 10);
260     }
261     return false;
262     break;
263
264   case EPRM: // Resets EEPROM
265     if (record->event.pressed) {
266       eeconfig_init();
267       default_layer_set(1UL<<eeconfig_read_default_layer());
268       layer_state_set(layer_state);
269     }
270     return false;
271     break;
272   case VRSN: // Prints firmware version
273     if (record->event.pressed) {
274       send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
275     }
276     return false;
277     break;
278
279 /*  Code has been depreciated
280     case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
281       if (!record->event.pressed) {
282         clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
283         send_string(decoy_secret[keycode - KC_SECRET_1]);
284       }
285       return false;
286       break;
287 */
288
289 // These are a serious of gaming macros.
290 // Only enables for the viterbi, basically,
291 // to save on firmware space, since it's limited.
292 #ifdef MACROS_ENABLED
293   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
294     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
295 #ifdef RGBLIGHT_ENABLE
296     userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
297 #endif //RGBLIGHT_ENABLE
298     return false; break;
299   case KC_SALT:
300     return send_game_macro("Salt, salt, salt...", record, false);
301   case KC_MORESALT:
302     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
303   case KC_SALTHARD:
304     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
305   case KC_GOODGAME:
306     return send_game_macro("Good game, everyone!", record, false);
307   case KC_GLHF:
308     return send_game_macro("Good luck, have fun!!!", record, false);
309   case KC_SYMM:
310     return send_game_macro("Left click to win!", record, false);
311   case KC_JUSTGAME:
312     return send_game_macro("It may be a game, but if you don't want to actually try, please go play AI, so that people that actually want to take the game seriously and \"get good\" have a place to do so without trolls like you throwing games.", record, false);
313   case KC_TORB:
314     return send_game_macro("That was positively riveting!", record, false);
315   case KC_AIM:
316     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
317     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
318   case KC_C9:
319     return send_game_macro("OMG!!!  C9!!!", record, false);
320   case KC_GGEZ:
321     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
322 #endif // MACROS_ENABLED
323
324
325   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
326 #ifdef TAP_DANCE_ENABLE
327     if (record->event.pressed) {
328       uint8_t dtime;
329       for (dtime = 0; dtime < 4; dtime++) {
330         diablo_key_time[dtime] = diablo_times[0];
331       }
332     }
333 #endif // TAP_DANCE_ENABLE
334     return false; break;
335
336
337   case KC_CCCV:                                    // One key copy/paste
338     if(record->event.pressed){
339       copy_paste_timer = timer_read();
340     } else {
341       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
342         register_code(KC_LCTL);
343         tap(KC_C);
344         unregister_code(KC_LCTL);
345 #ifdef AUDIO_ENABLE
346         PLAY_SONG(tone_copy);
347 #endif
348       } else {                                // Tap, paste
349         register_code(KC_LCTL);
350         tap(KC_V);
351         unregister_code(KC_LCTL);
352 #ifdef AUDIO_ENABLE
353         PLAY_SONG(tone_paste);
354 #endif
355       }
356     }
357     return false;
358     break;
359 #ifdef UNICODE_ENABLE
360   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
361     if (record->event.pressed) {
362       register_code(KC_RSFT);
363       tap(KC_9);
364       unregister_code(KC_RSFT);
365       process_unicode((0x256F | QK_UNICODE), record); // Arm
366       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
367       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
368       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
369       register_code(KC_RSFT);
370       tap(KC_0);
371       unregister_code(KC_RSFT);
372       process_unicode((0x256F | QK_UNICODE), record); // Arm
373       tap(KC_SPC);
374       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
375       tap(KC_SPC);
376       process_unicode((0x253B | QK_UNICODE), record); // Table
377       process_unicode((0x2501 | QK_UNICODE), record); // Table
378       process_unicode((0x253B | QK_UNICODE), record); // Table
379     }
380     return false;
381     break;
382 #endif // UNICODE_ENABLE
383
384   }
385   return process_record_keymap(keycode, record) &&
386 #ifdef RGBLIGHT_ENABLE
387     process_record_user_rgb(keycode, record) &&
388 #endif // RGBLIGHT_ENABLE
389     process_record_secrets(keycode, record);
390 }
391
392
393
394 // Runs state check and changes underglow color and animation
395 // on layer change, no matter where the change was initiated
396 // Then runs keymap's layer change check
397 uint32_t layer_state_set_user(uint32_t state) {
398   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
399 #ifdef RGBLIGHT_ENABLE
400   state = layer_state_set_rgb(state);
401 #endif // RGBLIGHT_ENABLE
402   return layer_state_set_keymap (state);
403 }
404
405
406 uint32_t default_layer_state_set_kb(uint32_t state) {
407   return default_layer_state_set_keymap (state);
408 }
409
410
411 // Any custom LED code goes here.
412 // So far, I only have keyboard specific code,
413 // So nothing goes here.
414 void led_set_user(uint8_t usb_led) {
415   led_set_keymap(usb_led);
416 }