]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Yet another update to drashna keymaps and userspace (#3787)
[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 #ifdef AUDIO_CLICKY
137   clicky_enable = userspace_config.clicky_enable;
138 #endif
139
140 #ifdef BOOTLOADER_CATERINA
141   DDRD &= ~(1<<5);
142   PORTD &= ~(1<<5);
143
144   DDRB &= ~(1<<0);
145   PORTB &= ~(1<<0);
146 #endif
147
148
149 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
150         set_unicode_input_mode(UC_WINC);
151 #endif //UNICODE_ENABLE
152   matrix_init_keymap();
153 }
154
155 void startup_user (void) {
156   #ifdef RGBLIGHT_ENABLE
157     matrix_init_rgb();
158   #endif //RGBLIGHT_ENABLE
159   startup_keymap();
160 }
161
162 void shutdown_user (void) {
163 #ifdef RGBLIGHT_ENABLE
164   rgblight_enable_noeeprom();
165   rgblight_mode_noeeprom(1);
166   rgblight_setrgb_red();
167 #endif // RGBLIGHT_ENABLE
168 #ifdef RGB_MATRIX_ENABLE
169   rgb_led led;
170   for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
171     led = g_rgb_leds[i];
172     if (led.matrix_co.raw < 0xFF) {
173       rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
174     }
175   }
176 #endif //RGB_MATRIX_ENABLE
177   shutdown_keymap();
178 }
179
180 void suspend_power_down_user(void)
181 {
182     suspend_power_down_keymap();
183 }
184
185 void suspend_wakeup_init_user(void)
186 {
187   suspend_wakeup_init_keymap();
188   #ifdef KEYBOARD_ergodox_ez
189   wait_ms(10);
190   #endif
191 }
192
193
194 // No global matrix scan code, so just run keymap's matrix
195 // scan function
196 void matrix_scan_user(void) {
197   static bool has_ran_yet;
198   if (!has_ran_yet) {
199     has_ran_yet = true;
200     startup_user();
201   }
202
203 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
204   run_diablo_macro_check();
205 #endif // TAP_DANCE_ENABLE
206
207 #ifdef RGBLIGHT_ENABLE
208   matrix_scan_rgb();
209 #endif // RGBLIGHT_ENABLE
210
211   matrix_scan_keymap();
212 }
213
214
215
216
217 // Defines actions tor my global custom keycodes. Defined in drashna.h file
218 // Then runs the _keymap's record handier if not processed here
219 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
220
221   // If console is enabled, it will print the matrix position and status of each key pressed
222 #ifdef KEYLOGGER_ENABLE
223   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
224 #endif //KEYLOGGER_ENABLE
225
226   switch (keycode) {
227   case KC_QWERTY:
228     if (record->event.pressed) {
229       set_single_persistent_default_layer(_QWERTY);
230     }
231     return false;
232     break;
233   case KC_COLEMAK:
234     if (record->event.pressed) {
235       set_single_persistent_default_layer(_COLEMAK);
236     }
237     return false;
238     break;
239   case KC_DVORAK:
240     if (record->event.pressed) {
241       set_single_persistent_default_layer(_DVORAK);
242     }
243     return false;
244     break;
245   case KC_WORKMAN:
246     if (record->event.pressed) {
247       set_single_persistent_default_layer(_WORKMAN);
248     }
249     return false;
250     break;
251
252
253   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
254     if (!record->event.pressed) {
255       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
256 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_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_byte(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 CLICKY_TOGGLE:
342 #ifdef AUDIO_CLICKY
343     userspace_config.clicky_enable = clicky_enable;
344     eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
345 #endif
346     break;
347 #ifdef UNICODE_ENABLE
348   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
349     if (record->event.pressed) {
350       register_code(KC_RSFT);
351       tap(KC_9);
352       unregister_code(KC_RSFT);
353       process_unicode((0x256F | QK_UNICODE), record); // Arm
354       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
355       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
356       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
357       register_code(KC_RSFT);
358       tap(KC_0);
359       unregister_code(KC_RSFT);
360       process_unicode((0x256F | QK_UNICODE), record); // Arm
361       tap(KC_SPC);
362       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
363       tap(KC_SPC);
364       process_unicode((0x253B | QK_UNICODE), record); // Table
365       process_unicode((0x2501 | QK_UNICODE), record); // Table
366       process_unicode((0x253B | QK_UNICODE), record); // Table
367     }
368     return false;
369     break;
370 #endif // UNICODE_ENABLE
371
372   }
373   return process_record_keymap(keycode, record) &&
374 #ifdef RGBLIGHT_ENABLE
375     process_record_user_rgb(keycode, record) &&
376 #endif // RGBLIGHT_ENABLE
377     process_record_secrets(keycode, record);
378 }
379
380
381
382 // Runs state check and changes underglow color and animation
383 // on layer change, no matter where the change was initiated
384 // Then runs keymap's layer change check
385 uint32_t layer_state_set_user(uint32_t state) {
386   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
387 #ifdef RGBLIGHT_ENABLE
388   state = layer_state_set_rgb(state);
389 #endif // RGBLIGHT_ENABLE
390   return layer_state_set_keymap (state);
391 }
392
393
394 uint32_t default_layer_state_set_kb(uint32_t state) {
395   return default_layer_state_set_keymap (state);
396 }
397
398
399 // Any custom LED code goes here.
400 // So far, I only have keyboard specific code,
401 // So nothing goes here.
402 void led_set_user(uint8_t usb_led) {
403   led_set_keymap(usb_led);
404 }