]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Big overhaul to Drashna keymaps and userspace (#3097)
[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 "version.h"
20 #include "eeprom.h"
21 #include "tap_dances.h"
22 #include "rgb_stuff.h"
23
24
25 float tone_copy[][2]            = SONG(SCROLL_LOCK_ON_SOUND);
26 float tone_paste[][2]           = SONG(SCROLL_LOCK_OFF_SOUND);
27
28 static uint16_t copy_paste_timer;
29 userspace_config_t userspace_config;
30
31 //  Helper Functions
32
33
34 // This block is for all of the gaming macros, as they were all doing
35 // the same thing, but with differring text sent.
36 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
37   if (!record->event.pressed || override) {
38     clear_keyboard();
39     tap(userspace_config.is_overwatch ? KC_BSPC : KC_ENTER);
40     wait_ms(50);
41     send_string(str);
42     wait_ms(50);
43     tap(KC_ENTER);
44   }
45   if (override) wait_ms(3000);
46   return false;
47 }
48
49 void tap(uint16_t keycode){ register_code(keycode); unregister_code(keycode); };
50
51
52 // Add reconfigurable functions here, for keymap customization
53 // This allows for a global, userspace functions, and continued
54 // customization of the keymap.  Use _keymap instead of _user
55 // functions in the keymaps
56 __attribute__ ((weak))
57 void matrix_init_keymap(void) {}
58
59 __attribute__ ((weak))
60 void matrix_scan_keymap(void) {}
61
62 __attribute__ ((weak))
63 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
64   return true;
65 }
66
67 __attribute__ ((weak))
68 bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
69   return true;
70 }
71
72 __attribute__ ((weak))
73 uint32_t layer_state_set_keymap (uint32_t state) {
74   return state;
75 }
76
77 __attribute__ ((weak))
78 void led_set_keymap(uint8_t usb_led) {}
79
80
81 // Call user matrix init, set default RGB colors and then
82 // call the keymap's init function
83 void matrix_init_user(void) {
84   userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
85
86 #ifdef AUDIO_CLICKY
87   clicky_enable = userspace_config.clicky_enable;
88 #endif
89
90 #ifdef BOOTLOADER_CATERINA
91   DDRD &= ~(1<<5);
92   PORTD &= ~(1<<5);
93
94   DDRB &= ~(1<<0);
95   PORTB &= ~(1<<0);
96 #endif
97
98
99 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
100         set_unicode_input_mode(UC_WINC);
101 #endif //UNICODE_ENABLE
102   matrix_init_rgb();
103   matrix_init_keymap();
104 }
105
106
107 // No global matrix scan code, so just run keymap's matrix
108 // scan function
109 void matrix_scan_user(void) {
110
111 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
112   run_diablo_macro_check();
113 #endif // TAP_DANCE_ENABLE
114
115 #ifdef RGBLIGHT_ENABLE
116   matrix_scan_rgb();
117 #endif // RGBLIGHT_ENABLE
118
119   matrix_scan_keymap();
120 }
121
122
123
124
125 // Defines actions tor my global custom keycodes. Defined in drashna.h file
126 // Then runs the _keymap's record handier if not processed here
127 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
128
129   // If console is enabled, it will print the matrix position and status of each key pressed
130 #ifdef KEYLOGGER_ENABLE
131   xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
132 #endif //KEYLOGGER_ENABLE
133
134   switch (keycode) {
135   case KC_QWERTY:
136     if (record->event.pressed) {
137       set_single_persistent_default_layer(_QWERTY);
138     }
139     return false;
140     break;
141   case KC_COLEMAK:
142     if (record->event.pressed) {
143       set_single_persistent_default_layer(_COLEMAK);
144     }
145     return false;
146     break;
147   case KC_DVORAK:
148     if (record->event.pressed) {
149       set_single_persistent_default_layer(_DVORAK);
150     }
151     return false;
152     break;
153   case KC_WORKMAN:
154     if (record->event.pressed) {
155       set_single_persistent_default_layer(_WORKMAN);
156     }
157     return false;
158     break;
159
160
161   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
162     if (!record->event.pressed) {
163       SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
164 #if  (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
165                    ":dfu"
166 #elif defined(BOOTLOADER_HALFKAY)
167                    ":teensy"
168 #elif defined(BOOTLOADER_CATERINA)
169                    ":avrdude"
170 #endif // bootloader options
171                    SS_TAP(X_ENTER));
172     }
173     return false;
174     break;
175
176
177   case KC_RESET: // Custom RESET code that sets RGBLights to RED
178     if (!record->event.pressed) {
179 #ifdef RGBLIGHT_ENABLE
180       rgblight_enable_noeeprom();
181       rgblight_mode_noeeprom(1);
182       rgblight_setrgb_red();
183 #endif // RGBLIGHT_ENABLE
184       reset_keyboard();
185     }
186     return false;
187     break;
188
189
190   case EPRM: // Resets EEPROM
191     if (record->event.pressed) {
192       eeconfig_init();
193       default_layer_set(1UL<<eeconfig_read_default_layer());
194       layer_state_set(layer_state);
195     }
196     return false;
197     break;
198   case VRSN: // Prints firmware version
199     if (record->event.pressed) {
200       SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
201     }
202     return false;
203     break;
204
205 /*  Code has been depreciated
206     case KC_SECRET_1 ... KC_SECRET_5: // Secrets!  Externally defined strings, not stored in repo
207       if (!record->event.pressed) {
208         clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
209         send_string(decoy_secret[keycode - KC_SECRET_1]);
210       }
211       return false;
212       break;
213 */
214
215 // These are a serious of gaming macros.
216 // Only enables for the viterbi, basically,
217 // to save on firmware space, since it's limited.
218 #ifdef MACROS_ENABLED
219   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
220     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
221 #ifdef RGBLIGHT_ENABLE
222     userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
223 #endif //RGBLIGHT_ENABLE
224     return false; break;
225   case KC_SALT:
226     return send_game_macro("Salt, salt, salt...", record, false);
227   case KC_MORESALT:
228     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
229   case KC_SALTHARD:
230     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
231   case KC_GOODGAME:
232     return send_game_macro("Good game, everyone!", record, false);
233   case KC_GLHF:
234     return send_game_macro("Good luck, have fun!!!", record, false);
235   case KC_SYMM:
236     return send_game_macro("Left click to win!", record, false);
237   case KC_JUSTGAME:
238     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);
239   case KC_TORB:
240     return send_game_macro("That was positively riveting!", record, false);
241   case KC_AIM:
242     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
243     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
244   case KC_C9:
245     return send_game_macro("OMG!!!  C9!!!", record, false);
246   case KC_GGEZ:
247     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
248 #endif // MACROS_ENABLED
249
250
251   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
252 #ifdef TAP_DANCE_ENABLE
253     if (record->event.pressed) {
254       uint8_t dtime;
255       for (dtime = 0; dtime < 4; dtime++) {
256         diablo_key_time[dtime] = diablo_times[0];
257       }
258     }
259 #endif // TAP_DANCE_ENABLE#endif
260     return false; break;
261
262
263   case KC_CCCV:                                    // One key copy/paste
264     if(record->event.pressed){
265       copy_paste_timer = timer_read();
266     } else {
267       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
268         register_code(KC_LCTL);
269         tap(KC_C);
270         unregister_code(KC_LCTL);
271 #ifdef AUDIO_ENABLE
272         PLAY_SONG(tone_copy);
273 #endif
274       } else {                                // Tap, paste
275         register_code(KC_LCTL);
276         tap(KC_V);
277         unregister_code(KC_LCTL);
278 #ifdef AUDIO_ENABLE
279         PLAY_SONG(tone_paste);
280 #endif
281       }
282     }
283     return false;
284     break;
285   case CLICKY_TOGGLE:
286 #ifdef AUDIO_CLICKY
287     userspace_config.clicky_enable = clicky_enable;
288     eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
289 #endif
290     break;
291 #ifdef UNICODE_ENABLE
292   case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
293     if (record->event.pressed) {
294       register_code(KC_RSFT);
295       tap(KC_9);
296       unregister_code(KC_RSFT);
297       process_unicode((0x256F | QK_UNICODE), record); // Arm
298       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
299       process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
300       process_unicode((0x00B0 | QK_UNICODE), record); // Eye
301       register_code(KC_RSFT);
302       tap(KC_0);
303       unregister_code(KC_RSFT);
304       process_unicode((0x256F | QK_UNICODE), record); // Arm
305       tap(KC_SPC);
306       process_unicode((0x0361 | QK_UNICODE), record); // Flippy
307       tap(KC_SPC);
308       process_unicode((0x253B | QK_UNICODE), record); // Table
309       process_unicode((0x2501 | QK_UNICODE), record); // Table
310       process_unicode((0x253B | QK_UNICODE), record); // Table
311     }
312     return false;
313     break;
314 #endif // UNICODE_ENABLE
315
316   }
317   return process_record_keymap(keycode, record) && process_record_secrets(keycode, record) && process_record_user_rgb(keycode, record);
318 }
319
320
321
322 // Runs state check and changes underglow color and animation
323 // on layer change, no matter where the change was initiated
324 // Then runs keymap's layer change check
325 uint32_t layer_state_set_user(uint32_t state) {
326   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
327 #ifdef RGBLIGHT_ENABLE
328   state = layer_state_set_rgb(state);
329 #endif // RGBLIGHT_ENABLE
330   return layer_state_set_keymap (state);
331 }
332
333
334 // Any custom LED code goes here.
335 // So far, I only have keyboard specific code,
336 // So nothing goes here.
337 void led_set_user(uint8_t usb_led) {
338   led_set_keymap(usb_led);
339 }