]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/mitosis/keymaps/default/keymap.c
ec440ed759006e39c3c5a9da1db066ece20bc6fe
[qmk_firmware.git] / keyboards / mitosis / keymaps / default / keymap.c
1 // this is the style you want to emulate.
2 // This is the canonical layout file for the Quantum project. If you want to add another keyboard,
3
4 #include QMK_KEYBOARD_H
5
6 // Each layer gets a name for readability, which is then used in the keymap matrix below.
7 // The underscores don't mean anything - you can have a layer called STUFF or any other name.
8 // Layer names don't all need to be of the same length, obviously, and you can also skip them
9 // entirely and just use numbers.
10 enum mitosis_layers
11 {
12         _MALT,
13         _SHIFTED,
14         _FUNCTION,
15         _FUNCSHIFT
16 };
17
18 enum mitosis_keycodes
19 {
20   FNKEY = SAFE_RANGE,
21   SHIFT
22 };
23
24
25 // Macro definitions for readability
26 enum mitosis_macros
27 {
28         VOLU,
29         VOLD,
30         ESCM
31 };
32
33 #define LONGPRESS_DELAY 150
34 #define LAYER_TOGGLE_DELAY 300
35
36 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
37
38   [_MALT] = LAYOUT( /* Malt Layout, customised for reduced columns (ex: quote and shift locations) */
39     KC_Q,    KC_P,    KC_Y,    KC_C,    KC_B,           KC_V,    KC_M,    KC_U,    KC_Z,    KC_L,
40     KC_A,    KC_N,    KC_I,    KC_S,    KC_F,           KC_D,    KC_T,    KC_H,    KC_O,    KC_R,
41     KC_COMM, KC_DOT,  KC_J,    KC_G,    KC_SLSH,        KC_SCLN, KC_W,    KC_K,    KC_QUOT, KC_X,
42              M(VOLU), M(ESCM), KC_TAB,  KC_LCTL,        KC_LALT, KC_ENT,  KC_DEL,  KC_PGUP,
43              M(VOLD), KC_LGUI, KC_E,    FNKEY,          SHIFT,   KC_SPC,  KC_BSPC, KC_PGDN
44   ),
45
46
47   [_SHIFTED] = LAYOUT( /* Shifted Layer, layered so that tri_layer can be used, or selectively
48                                          able to modify individual key's shifted behaviour */
49     _______, _______, _______, _______, _______,       _______, _______, _______, _______, _______,
50     _______, _______, _______, _______, _______,       _______, _______, _______, _______, _______,
51     _______, _______, _______, _______, _______,       _______, _______, _______, _______, _______,
52              _______, _______, _______, _______,       _______, _______, _______, _______,
53              _______, _______, _______, _______,       _______, _______, _______, _______
54   ),
55
56
57
58   [_FUNCTION] = LAYOUT( /* Function Layer, primary alternative layer featuring numpad on right hand,
59                                            cursor keys on left hand, and all symbols*/
60     KC_AMPR, KC_PERC, KC_UP,   KC_CIRC, KC_PIPE,       KC_LBRC, KC_7,    KC_8,    KC_9,    KC_MINS,
61     KC_AT,   KC_LEFT, KC_DOWN, KC_RGHT, KC_HASH,       KC_LPRN, KC_4,    KC_5,    KC_6,    KC_PLUS,
62     KC_ASTR, KC_UNDS, KC_EXLM, KC_DLR,  KC_BSLS,       KC_LCBR, KC_1,    KC_2,    KC_3,    KC_ENT,
63              KC_HOME, KC_GRV,  KC_PWR,  _______,       _______, KC_EQL,  KC_TILD, KC_DOT,
64              KC_END,  _______, _______, _______,       _______, KC_0,    _______, KC_PSCR
65   ),
66
67
68   [_FUNCSHIFT] = LAYOUT( /* Function Shifted Layer, secondary alternative layer with closing brackets,
69                                                     and F-keys under their numpad equivalents*/
70     _______, _______, _______, _______, _______,       KC_RBRC, KC_F7,   KC_F8,   KC_F9,   KC_F10,
71     _______, _______, _______, _______, _______,       KC_RPRN, KC_F4,   KC_F5,   KC_F6,   KC_F11,
72     _______, _______, _______, _______, _______,       KC_RCBR, KC_F1,   KC_F2,   KC_F3,   KC_F12,
73              _______, _______, _______, _______,       _______, _______, _______, _______,
74              _______, _______, _______, _______,       _______, _______, _______, _______
75   )
76
77 };
78
79 static uint16_t key_timer;
80
81 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
82 {
83   // MACRODOWN only works in this function
84     switch(id) {
85
86         //switch multiplexing for media, short tap for volume up, long press for play/pause
87         case VOLU:
88             if (record->event.pressed) {
89                 key_timer = timer_read(); // if the key is being pressed, we start the timer.
90                 } else { // this means the key was just released, so we can figure out how long it was pressed for (tap or "held down").
91                 if (timer_elapsed(key_timer) > LONGPRESS_DELAY) { // LONGPRESS_DELAY being 150ms, the threshhold we pick for counting something as a tap.
92                   return MACRO(T(MPLY), END);
93                 } else {
94                   return MACRO(T(VOLU), END);
95                 }
96                 }
97                 break;
98
99                 //switch multiplexing for media, short tap for volume down, long press for next track
100         case VOLD:
101             if (record->event.pressed) {
102                 key_timer = timer_read();
103                 } else {
104                 if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
105                   return MACRO(T(MNXT), END);
106                 } else {
107                   return MACRO(T(VOLD), END);
108                 }
109                 }
110                 break;
111
112         //switch multiplexing for escape, short tap for escape, long press for context menu
113         case ESCM:
114             if (record->event.pressed) {
115                 key_timer = timer_read();
116                 } else {
117                 if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
118                   return MACRO(T(APP), END);
119                 } else {
120                   return MACRO(T(ESC), END);
121                 }
122                 }
123                 break;
124
125         break;
126     }
127     return MACRO_NONE;
128 };
129
130 static bool singular_key = false;
131
132 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
133
134         uint8_t layer;
135   layer = biton32(layer_state);  // get the current layer
136
137   //custom layer handling for tri_layer,
138   switch (keycode) {
139   case FNKEY:
140         if (record->event.pressed) {
141       key_timer = timer_read();
142       singular_key = true;
143         layer_on(_FUNCTION);
144         } else {
145       if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
146         layer_off(_FUNCTION);
147       }
148         }
149     update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
150         return false;
151         break;
152   //SHIFT is handled as LSHIFT in the general case
153   case SHIFT:
154         if (record->event.pressed) {
155       key_timer = timer_read();
156       singular_key = true;
157         layer_on(_SHIFTED);
158         register_code(KC_LSFT);
159         } else {
160         if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
161         layer_off(_SHIFTED);
162           unregister_code(KC_LSFT);
163       }
164     }
165     update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
166         return false;
167         break;
168
169   //If any other key was pressed during the layer mod hold period,
170   //then the layer mod was used momentarily, and should block latching
171   default:
172     singular_key = false;
173     break;
174   }
175
176   //FUNCSHIFT has been shifted by the SHIFT handling, some keys need to be excluded
177   if (layer == _FUNCSHIFT) {
178         //F1-F12 should be sent as unshifted keycodes,
179         //and ] needs to be unshifted or it is sent as }
180         if ( (keycode >= KC_F1 && keycode <= KC_F12)
181            || keycode == KC_RBRC ) {
182                 if (record->event.pressed) {
183               unregister_mods(MOD_LSFT);
184           } else {
185               register_mods(MOD_LSFT);
186           }
187         }
188   }
189
190   return true;
191 };
192
193 void matrix_scan_user(void) {
194     uint8_t layer = biton32(layer_state);
195
196     switch (layer) {
197         case _MALT:
198                 set_led_off;
199                 break;
200         case _FUNCTION:
201             set_led_blue;
202             break;
203         case _SHIFTED:
204             set_led_red;
205             break;
206         case _FUNCSHIFT:
207                 set_led_green;
208                 break;
209         default:
210             break;
211     }
212 };
213