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