]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
b2219e9aa745b534c68f0284e587d3082d92cef2
[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 uint16_t copy_paste_timer;
25 //  Helper Functions
26
27
28 // This block is for all of the gaming macros, as they were all doing
29 // the same thing, but with differring text sent.
30 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
31   if (!record->event.pressed || override) {
32     uint16_t keycode;
33     if (userspace_config.is_overwatch) {
34       keycode = KC_BSPC;
35     } else {
36       keycode = KC_ENTER;
37     }
38     clear_keyboard();
39     tap_code(keycode);
40     wait_ms(50);
41     send_string_with_delay(str, MACRO_TIMER);
42     wait_ms(50);
43     tap_code(KC_ENTER);
44   }
45   if (override) wait_ms(3000);
46   return false;
47 }
48
49 bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
50   static uint16_t this_timer;
51   if(pressed) {
52       this_timer= timer_read();
53   } else {
54       if (timer_elapsed(this_timer) < TAPPING_TERM){
55           register_code(code);
56           unregister_code(code);
57       } else {
58           register_code(mod_code);
59           register_code(code);
60           unregister_code(code);
61           unregister_code(mod_code);
62       }
63   }
64   return false;
65 }
66
67 bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
68   if(pressed) {
69       this_timer= timer_read();
70   } else {
71       if (timer_elapsed(this_timer) < TAPPING_TERM){
72           register_code(code);
73           unregister_code(code);
74       } else {
75           register_code(mod_code);
76           register_code(code);
77           unregister_code(code);
78           unregister_code(mod_code);
79       }
80   }
81   return false;
82 }
83
84 void bootmagic_lite(void) {
85   matrix_scan();
86   #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
87     wait_ms(DEBOUNCING_DELAY * 2);
88   #elif defined(DEBOUNCE) && DEBOUNCE > 0
89     wait_ms(DEBOUNCE * 2);
90   #else
91     wait_ms(30);
92   #endif
93   matrix_scan();
94    if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
95     bootloader_jump();
96   }
97 }
98
99 // Add reconfigurable functions here, for keymap customization
100 // This allows for a global, userspace functions, and continued
101 // customization of the keymap.  Use _keymap instead of _user
102 // functions in the keymaps
103 __attribute__ ((weak))
104 void matrix_init_keymap(void) {}
105
106 __attribute__ ((weak))
107 void startup_keymap(void) {}
108
109 __attribute__ ((weak))
110 void shutdown_keymap(void) {}
111
112 __attribute__ ((weak))
113 void suspend_power_down_keymap(void) {}
114
115 __attribute__ ((weak))
116 void suspend_wakeup_init_keymap(void) {}
117
118 __attribute__ ((weak))
119 void matrix_scan_keymap(void) {}
120
121 __attribute__ ((weak))
122 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
123   return true;
124 }
125
126 __attribute__ ((weak))
127 bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
128   return true;
129 }
130
131
132 __attribute__ ((weak))
133 uint32_t layer_state_set_keymap (uint32_t state) {
134   return state;
135 }
136
137 __attribute__ ((weak))
138 uint32_t default_layer_state_set_keymap (uint32_t state) {
139   return state;
140 }
141
142 __attribute__ ((weak))
143 void led_set_keymap(uint8_t usb_led) {}
144
145 __attribute__ ((weak))
146 void eeconfig_init_keymap(void) {}
147
148 // Call user matrix init, set default RGB colors and then
149 // call the keymap's init function
150 void matrix_init_user(void) {
151   #if !defined(BOOTMAGIC_LITE) && !defined(BOOTMAGIC_ENABLE)
152     bootmagic_lite();
153   #endif
154
155   userspace_config.raw = eeconfig_read_user();
156
157 #ifdef BOOTLOADER_CATERINA
158   DDRD &= ~(1<<5);
159   PORTD &= ~(1<<5);
160
161   DDRB &= ~(1<<0);
162   PORTB &= ~(1<<0);
163 #endif
164
165 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
166   if (eeprom_read_byte(EECONFIG_UNICODEMODE) != UC_WIN) {
167     set_unicode_input_mode(UC_WIN);
168   }
169 #endif //UNICODE_ENABLE
170   matrix_init_keymap();
171 }
172
173 void startup_user (void) {
174   #ifdef RGBLIGHT_ENABLE
175     matrix_init_rgb();
176   #endif //RGBLIGHT_ENABLE
177   startup_keymap();
178 }
179
180 void shutdown_user (void) {
181 #ifdef RGBLIGHT_ENABLE
182   rgblight_enable_noeeprom();
183   rgblight_mode_noeeprom(1);
184   rgblight_setrgb_red();
185 #endif // RGBLIGHT_ENABLE
186 #ifdef RGB_MATRIX_ENABLE
187   rgb_led led;
188   for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
189     led = g_rgb_leds[i];
190     if (led.matrix_co.raw < 0xFF) {
191       rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
192     }
193   }
194 #endif //RGB_MATRIX_ENABLE
195   shutdown_keymap();
196 }
197
198 void suspend_power_down_user(void) {
199     suspend_power_down_keymap();
200 }
201
202 void suspend_wakeup_init_user(void) {
203   suspend_wakeup_init_keymap();
204 }
205
206
207 // No global matrix scan code, so just run keymap's matrix
208 // scan function
209 void matrix_scan_user(void) {
210   static bool has_ran_yet;
211   if (!has_ran_yet) {
212     has_ran_yet = true;
213     startup_user();
214   }
215
216 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
217   run_diablo_macro_check();
218 #endif // TAP_DANCE_ENABLE
219
220 #ifdef RGBLIGHT_ENABLE
221   matrix_scan_rgb();
222 #endif // RGBLIGHT_ENABLE
223
224   matrix_scan_keymap();
225 }
226
227
228
229
230 // Defines actions tor my global custom keycodes. Defined in drashna.h file
231 // Then runs the _keymap's record handier if not processed here
232 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
233
234   // If console is enabled, it will print the matrix position and status of each key pressed
235 #ifdef KEYLOGGER_ENABLE
236   #if defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2)
237     xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.row, record->event.key.col, record->event.pressed);
238   #else
239     xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
240   #endif
241 #endif //KEYLOGGER_ENABLE
242
243   switch (keycode) {
244   case KC_QWERTY:
245     if (record->event.pressed) {
246       set_single_persistent_default_layer(_QWERTY);
247     }
248     break;
249   case KC_COLEMAK:
250     if (record->event.pressed) {
251       set_single_persistent_default_layer(_COLEMAK);
252     }
253     break;
254   case KC_DVORAK:
255     if (record->event.pressed) {
256       set_single_persistent_default_layer(_DVORAK);
257     }
258     break;
259   case KC_WORKMAN:
260     if (record->event.pressed) {
261       set_single_persistent_default_layer(_WORKMAN);
262     }
263     break;
264
265
266   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
267     if (!record->event.pressed) {
268       uint8_t temp_mod = get_mods();
269       clear_mods();
270       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP), 10);
271       if (temp_mod & MODS_SHIFT_MASK) {
272         #if defined(__ARM__)
273           send_string_with_delay_P(PSTR(":dfu-util"), 10);
274         #elif defined(BOOTLOADER_DFU)
275           send_string_with_delay_P(PSTR(":dfu"), 10);
276         #elif defined(BOOTLOADER_HALFKAY)
277           send_string_with_delay_P(PSTR(":teensy"), 10);
278         #elif defined(BOOTLOADER_CATERINA)
279           send_string_with_delay_P(PSTR(":avrdude"), 10);
280         #endif // bootloader options
281       }
282       #if defined(KEYBOARD_viterbi)
283         send_string_with_delay_P(PSTR(":dfu"), 10);
284       #endif
285       if (temp_mod & MODS_CTRL_MASK) { send_string_with_delay_P(PSTR(" -j8 --output-sync"), 10); }
286       send_string_with_delay_P(PSTR(SS_TAP(X_ENTER)), 10);
287       set_mods(temp_mod);
288     }
289     break;
290
291   case EPRM: // Resets EEPROM
292     if (record->event.pressed) {
293       eeconfig_init();
294     }
295     break;
296   case VRSN: // Prints firmware version
297     if (record->event.pressed) {
298       send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
299     }
300     break;
301
302 // These are a serious of gaming macros.
303 // Only enables for the viterbi, basically,
304 // to save on firmware space, since it's limited.
305 #ifdef MACROS_ENABLED
306   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
307     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeconfig_update_user(userspace_config.raw); }
308 #ifdef RGBLIGHT_ENABLE
309     userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
310 #endif //RGBLIGHT_ENABLE
311     break;
312   case KC_SALT:
313     return send_game_macro("Salt, salt, salt...", record, false);
314   case KC_MORESALT:
315     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
316   case KC_SALTHARD:
317     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
318   case KC_GOODGAME:
319     return send_game_macro("Good game, everyone!", record, false);
320   case KC_GLHF:
321     return send_game_macro("Good luck, have fun!!!", record, false);
322   case KC_SYMM:
323     return send_game_macro("Left click to win!", record, false);
324   case KC_JUSTGAME:
325     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);
326   case KC_TORB:
327     return send_game_macro("That was positively riveting!", record, false);
328   case KC_AIM:
329     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
330     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
331   case KC_C9:
332     return send_game_macro("OMG!!!  C9!!!", record, false);
333   case KC_GGEZ:
334     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
335 #endif // MACROS_ENABLED
336
337
338   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
339 #ifdef TAP_DANCE_ENABLE
340     if (record->event.pressed) {
341       uint8_t dtime;
342       for (dtime = 0; dtime < 4; dtime++) {
343         diablo_key_time[dtime] = diablo_times[0];
344       }
345     }
346 #endif // TAP_DANCE_ENABLE
347     break;
348
349
350   case KC_CCCV:                                    // One key copy/paste
351     if(record->event.pressed){
352       copy_paste_timer = timer_read();
353     } else {
354       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
355         register_code(KC_LCTL);
356         tap_code(KC_C);
357         unregister_code(KC_LCTL);
358       } else {                                // Tap, paste
359         register_code(KC_LCTL);
360         tap_code(KC_V);
361         unregister_code(KC_LCTL);
362       }
363     }
364     break;
365 #ifdef UNICODE_ENABLE
366   case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
367     if (record->event.pressed) {
368       send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
369     }
370     break;
371   case UC_TABL: // ┬─┬ノ( º _ ºノ)
372     if (record->event.pressed) {
373       send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 30CE 0029");
374     }
375     break;
376   case UC_SHRG: // ¯\_(ツ)_/¯
377     if (record->event.pressed) {
378       send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
379     }
380     break;
381   case UC_DISA: // ಠ_ಠ
382     if (record->event.pressed) {
383       send_unicode_hex_string("0CA0 005F 0CA0");
384     }
385     break;
386 #endif
387   }
388   return process_record_keymap(keycode, record) &&
389 #ifdef RGBLIGHT_ENABLE
390     process_record_user_rgb(keycode, record) &&
391 #endif // RGBLIGHT_ENABLE
392     process_record_secrets(keycode, record);
393 }
394
395
396
397 // Runs state check and changes underglow color and animation
398 // on layer change, no matter where the change was initiated
399 // Then runs keymap's layer change check
400 uint32_t layer_state_set_user(uint32_t state) {
401   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
402 #ifdef RGBLIGHT_ENABLE
403   state = layer_state_set_rgb(state);
404 #endif // RGBLIGHT_ENABLE
405   return layer_state_set_keymap (state);
406 }
407
408
409 uint32_t default_layer_state_set_user(uint32_t state) {
410   return default_layer_state_set_keymap(state);
411 }
412
413
414 // Any custom LED code goes here.
415 // So far, I only have keyboard specific code,
416 // So nothing goes here.
417 void led_set_user(uint8_t usb_led) {
418   led_set_keymap(usb_led);
419 }
420
421 void eeconfig_init_user(void) {
422   userspace_config.raw = 0;
423   userspace_config.rgb_layer_change = true;
424   eeconfig_update_user(userspace_config.raw);
425 }