]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Update to drashna keymaps (#4365)
[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 }
177
178 void startup_user (void) {
179   #ifdef RGBLIGHT_ENABLE
180     matrix_init_rgb();
181   #endif //RGBLIGHT_ENABLE
182   startup_keymap();
183 }
184
185 void shutdown_user (void) {
186 #ifdef RGBLIGHT_ENABLE
187   rgblight_enable_noeeprom();
188   rgblight_mode_noeeprom(1);
189   rgblight_setrgb_red();
190 #endif // RGBLIGHT_ENABLE
191 #ifdef RGB_MATRIX_ENABLE
192   rgb_led led;
193   for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
194     led = g_rgb_leds[i];
195     if (led.matrix_co.raw < 0xFF) {
196       rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
197     }
198   }
199 #endif //RGB_MATRIX_ENABLE
200   shutdown_keymap();
201 }
202
203 void suspend_power_down_user(void) {
204     suspend_power_down_keymap();
205 }
206
207 void suspend_wakeup_init_user(void) {
208   suspend_wakeup_init_keymap();
209 }
210
211
212 // No global matrix scan code, so just run keymap's matrix
213 // scan function
214 void matrix_scan_user(void) {
215   static bool has_ran_yet;
216   if (!has_ran_yet) {
217     has_ran_yet = true;
218     startup_user();
219   }
220
221 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
222   run_diablo_macro_check();
223 #endif // TAP_DANCE_ENABLE
224
225 #ifdef RGBLIGHT_ENABLE
226   matrix_scan_rgb();
227 #endif // RGBLIGHT_ENABLE
228
229   matrix_scan_keymap();
230 }
231
232
233
234
235 // Defines actions tor my global custom keycodes. Defined in drashna.h file
236 // Then runs the _keymap's record handier if not processed here
237 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
238
239   // If console is enabled, it will print the matrix position and status of each key pressed
240 #ifdef KEYLOGGER_ENABLE
241   #if defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2)
242     xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.row, record->event.key.col, record->event.pressed);
243   #else
244     xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
245   #endif
246 #endif //KEYLOGGER_ENABLE
247
248   switch (keycode) {
249   case KC_QWERTY:
250     if (record->event.pressed) {
251       set_single_persistent_default_layer(_QWERTY);
252     }
253     break;
254   case KC_COLEMAK:
255     if (record->event.pressed) {
256       set_single_persistent_default_layer(_COLEMAK);
257     }
258     break;
259   case KC_DVORAK:
260     if (record->event.pressed) {
261       set_single_persistent_default_layer(_DVORAK);
262     }
263     break;
264   case KC_WORKMAN:
265     if (record->event.pressed) {
266       set_single_persistent_default_layer(_WORKMAN);
267     }
268     break;
269
270
271   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
272     if (!record->event.pressed) {
273       uint8_t temp_mod = get_mods();
274       uint8_t temp_osm = get_oneshot_mods();
275       clear_mods(); clear_oneshot_mods();
276       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP), 10);
277       if (temp_mod & MODS_SHIFT_MASK || temp_osm & MODS_SHIFT_MASK) {
278         #if defined(__ARM__)
279           send_string_with_delay_P(PSTR(":dfu-util"), 10);
280         #elif defined(BOOTLOADER_DFU)
281           send_string_with_delay_P(PSTR(":dfu"), 10);
282         #elif defined(BOOTLOADER_HALFKAY)
283           send_string_with_delay_P(PSTR(":teensy"), 10);
284         #elif defined(BOOTLOADER_CATERINA)
285           send_string_with_delay_P(PSTR(":avrdude"), 10);
286         #endif // bootloader options
287       }
288       #if defined(KEYBOARD_viterbi)
289         send_string_with_delay_P(PSTR(":dfu"), 10);
290       #endif
291       if (temp_mod & MODS_CTRL_MASK || temp_osm & MODS_CTRL_MASK) { send_string_with_delay_P(PSTR(" -j8 --output-sync"), 10); }
292       send_string_with_delay_P(PSTR(SS_TAP(X_ENTER)), 10);
293       set_mods(temp_mod);
294     }
295     break;
296
297   case EPRM: // Resets EEPROM
298     if (record->event.pressed) {
299       eeconfig_init();
300     }
301     break;
302   case VRSN: // Prints firmware version
303     if (record->event.pressed) {
304       send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
305     }
306     break;
307
308 // These are a serious of gaming macros.
309 // Only enables for the viterbi, basically,
310 // to save on firmware space, since it's limited.
311 #ifdef MACROS_ENABLED
312   case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
313     if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeconfig_update_user(userspace_config.raw); }
314 #ifdef RGBLIGHT_ENABLE
315     userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
316 #endif //RGBLIGHT_ENABLE
317     break;
318   case KC_SALT:
319     return send_game_macro("Salt, salt, salt...", record, false);
320   case KC_MORESALT:
321     return  send_game_macro("Please sir, can I have some more salt?!", record, false);
322   case KC_SALTHARD:
323     return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
324   case KC_GOODGAME:
325     return send_game_macro("Good game, everyone!", record, false);
326   case KC_GLHF:
327     return send_game_macro("Good luck, have fun!!!", record, false);
328   case KC_SYMM:
329     return send_game_macro("Left click to win!", record, false);
330   case KC_JUSTGAME:
331     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);
332   case KC_TORB:
333     return send_game_macro("That was positively riveting!", record, false);
334   case KC_AIM:
335     send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
336     return send_game_macro("Wait! That aim is TOO good!  You're clearly using an aim hack! CHEATER!", record, false);
337   case KC_C9:
338     return send_game_macro("OMG!!!  C9!!!", record, false);
339   case KC_GGEZ:
340     return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
341 #endif // MACROS_ENABLED
342
343
344   case KC_DIABLO_CLEAR:  // reset all Diablo timers, disabling them
345 #ifdef TAP_DANCE_ENABLE
346     if (record->event.pressed) {
347       uint8_t dtime;
348       for (dtime = 0; dtime < 4; dtime++) {
349         diablo_key_time[dtime] = diablo_times[0];
350       }
351     }
352 #endif // TAP_DANCE_ENABLE
353     break;
354
355
356   case KC_CCCV:                                    // One key copy/paste
357     if(record->event.pressed){
358       copy_paste_timer = timer_read();
359     } else {
360       if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) {   // Hold, copy
361         register_code(KC_LCTL);
362         tap_code(KC_C);
363         unregister_code(KC_LCTL);
364       } else {                                // Tap, paste
365         register_code(KC_LCTL);
366         tap_code(KC_V);
367         unregister_code(KC_LCTL);
368       }
369     }
370     break;
371 #ifdef UNICODE_ENABLE
372   case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
373     if (record->event.pressed) {
374       send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
375     }
376     break;
377   case UC_TABL: // ┬─┬ノ( º _ ºノ)
378     if (record->event.pressed) {
379       send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 30CE 0029");
380     }
381     break;
382   case UC_SHRG: // ¯\_(ツ)_/¯
383     if (record->event.pressed) {
384       send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
385     }
386     break;
387   case UC_DISA: // ಠ_ಠ
388     if (record->event.pressed) {
389       send_unicode_hex_string("0CA0 005F 0CA0");
390     }
391     break;
392 #endif
393   }
394   return process_record_keymap(keycode, record) &&
395 #ifdef RGBLIGHT_ENABLE
396     process_record_user_rgb(keycode, record) &&
397 #endif // RGBLIGHT_ENABLE
398     process_record_secrets(keycode, record);
399 }
400
401
402
403 // Runs state check and changes underglow color and animation
404 // on layer change, no matter where the change was initiated
405 // Then runs keymap's layer change check
406 uint32_t layer_state_set_user(uint32_t state) {
407   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
408 #ifdef RGBLIGHT_ENABLE
409   state = layer_state_set_rgb(state);
410 #endif // RGBLIGHT_ENABLE
411   return layer_state_set_keymap (state);
412 }
413
414
415 uint32_t default_layer_state_set_user(uint32_t state) {
416   return default_layer_state_set_keymap(state);
417 }
418
419
420 // Any custom LED code goes here.
421 // So far, I only have keyboard specific code,
422 // So nothing goes here.
423 void led_set_user(uint8_t usb_led) {
424   led_set_keymap(usb_led);
425 }
426
427 void eeconfig_init_user(void) {
428   userspace_config.raw = 0;
429   userspace_config.rgb_layer_change = true;
430   eeconfig_update_user(userspace_config.raw);
431   #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
432     set_unicode_input_mode(DRASHNA_UNICODE_MODE);
433     get_unicode_input_mode();
434   #else
435     eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
436   #endif
437 }