]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
Rework drashna userspace to be more modular (#4544)
[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
20 userspace_config_t userspace_config;
21 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
22   #define DRASHNA_UNICODE_MODE UC_WIN
23 #else
24   // set to 2 for UC_WIN, set to 4 for UC_WINC
25   #define DRASHNA_UNICODE_MODE 2
26 #endif
27
28
29 // This block is for all of the gaming macros, as they were all doing
30 // the same thing, but with differring text sent.
31 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
32   if (!record->event.pressed || override) {
33     uint16_t keycode;
34     if (userspace_config.is_overwatch) {
35       keycode = KC_BSPC;
36     } else {
37       keycode = KC_ENTER;
38     }
39     clear_keyboard();
40     tap_code(keycode);
41     wait_ms(50);
42     send_string_with_delay(str, MACRO_TIMER);
43     wait_ms(50);
44     tap_code(KC_ENTER);
45   }
46   if (override) wait_ms(3000);
47   return false;
48 }
49
50 bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
51   static uint16_t this_timer;
52   if(pressed) {
53       this_timer= timer_read();
54   } else {
55       if (timer_elapsed(this_timer) < TAPPING_TERM){
56           register_code(code);
57           unregister_code(code);
58       } else {
59           register_code(mod_code);
60           register_code(code);
61           unregister_code(code);
62           unregister_code(mod_code);
63       }
64   }
65   return false;
66 }
67
68 bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
69   if(pressed) {
70       this_timer= timer_read();
71   } else {
72       if (timer_elapsed(this_timer) < TAPPING_TERM){
73           register_code(code);
74           unregister_code(code);
75       } else {
76           register_code(mod_code);
77           register_code(code);
78           unregister_code(code);
79           unregister_code(mod_code);
80       }
81   }
82   return false;
83 }
84
85 void bootmagic_lite(void) {
86   matrix_scan();
87   #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
88     wait_ms(DEBOUNCING_DELAY * 2);
89   #elif defined(DEBOUNCE) && DEBOUNCE > 0
90     wait_ms(DEBOUNCE * 2);
91   #else
92     wait_ms(30);
93   #endif
94   matrix_scan();
95    if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
96     bootloader_jump();
97   }
98 }
99
100 // Add reconfigurable functions here, for keymap customization
101 // This allows for a global, userspace functions, and continued
102 // customization of the keymap.  Use _keymap instead of _user
103 // functions in the keymaps
104 __attribute__ ((weak))
105 void matrix_init_keymap(void) {}
106
107 // Call user matrix init, set default RGB colors and then
108 // call the keymap's init function
109 void matrix_init_user(void) {
110   userspace_config.raw = eeconfig_read_user();
111
112   #ifdef BOOTLOADER_CATERINA
113     DDRD &= ~(1<<5);
114     PORTD &= ~(1<<5);
115
116     DDRB &= ~(1<<0);
117     PORTB &= ~(1<<0);
118   #endif
119
120   #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
121     set_unicode_input_mode(DRASHNA_UNICODE_MODE);
122     get_unicode_input_mode();
123   #endif //UNICODE_ENABLE
124   matrix_init_keymap();
125   #ifdef RGBLIGHT_ENABLE
126     matrix_init_rgb();
127   #endif //RGBLIGHT_ENABLE
128 }
129
130
131 __attribute__ ((weak))
132 void shutdown_keymap(void) {}
133
134 void shutdown_user (void) {
135   #ifdef RGBLIGHT_ENABLE
136     rgblight_enable_noeeprom();
137     rgblight_mode_noeeprom(1);
138     rgblight_setrgb_red();
139   #endif // RGBLIGHT_ENABLE
140   #ifdef RGB_MATRIX_ENABLE
141     uint16_t timer_start = timer_read();
142     rgb_matrix_set_color_all( 0xFF, 0x00, 0x00 );
143     while(timer_elapsed(timer_start) < 250) { wait_ms(1); }
144   #endif //RGB_MATRIX_ENABLE
145   shutdown_keymap();
146 }
147
148 __attribute__ ((weak))
149 void suspend_power_down_keymap(void) {}
150
151 void suspend_power_down_user(void) {
152     suspend_power_down_keymap();
153 }
154
155 __attribute__ ((weak))
156 void suspend_wakeup_init_keymap(void) {}
157
158 void suspend_wakeup_init_user(void) {
159   suspend_wakeup_init_keymap();
160 }
161
162
163 __attribute__ ((weak))
164 void matrix_scan_keymap(void) {}
165
166 // No global matrix scan code, so just run keymap's matrix
167 // scan function
168 void matrix_scan_user(void) {
169   static bool has_ran_yet;
170   if (!has_ran_yet) {
171     has_ran_yet = true;
172     startup_user();
173   }
174
175 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
176   run_diablo_macro_check();
177 #endif // TAP_DANCE_ENABLE
178
179 #ifdef RGBLIGHT_ENABLE
180   matrix_scan_rgb();
181 #endif // RGBLIGHT_ENABLE
182
183   matrix_scan_keymap();
184 }
185
186
187 __attribute__ ((weak))
188 uint32_t layer_state_set_keymap (uint32_t state) {
189   return state;
190 }
191
192 // on layer change, no matter where the change was initiated
193 // Then runs keymap's layer change check
194 uint32_t layer_state_set_user(uint32_t state) {
195   state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
196 #ifdef RGBLIGHT_ENABLE
197   state = layer_state_set_rgb(state);
198 #endif // RGBLIGHT_ENABLE
199   return layer_state_set_keymap (state);
200 }
201
202
203 __attribute__ ((weak))
204 uint32_t default_layer_state_set_keymap (uint32_t state) {
205   return state;
206 }
207
208 // Runs state check and changes underglow color and animation
209 uint32_t default_layer_state_set_user(uint32_t state) {
210   state = default_layer_state_set_keymap(state);
211 #ifdef RGBLIGHT_ENABLE
212   state = default_layer_state_set_rgb(state);
213 #endif // RGBLIGHT_ENABLE
214   return state;
215 }
216
217 __attribute__ ((weak))
218 void led_set_keymap(uint8_t usb_led) {}
219
220 // Any custom LED code goes here.
221 // So far, I only have keyboard specific code,
222 // So nothing goes here.
223 void led_set_user(uint8_t usb_led) {
224   led_set_keymap(usb_led);
225 }
226
227 __attribute__ ((weak))
228 void eeconfig_init_keymap(void) {}
229
230 void eeconfig_init_user(void) {
231   userspace_config.raw = 0;
232   userspace_config.rgb_layer_change = true;
233   eeconfig_update_user(userspace_config.raw);
234   #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
235     set_unicode_input_mode(DRASHNA_UNICODE_MODE);
236     get_unicode_input_mode();
237   #else
238     eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
239   #endif
240 }