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