summaryrefslogtreecommitdiff
path: root/keyboards/xiudi/xd002/keymaps/multilayer_rgb/keymap.c
blob: 4ae28fd91b97ebef4a639dbdb04affc574dea9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright 2020 elmo-space<mail@elmo.space>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include QMK_KEYBOARD_H
#include "rgblite.h"

// Defines names for use in layer keycodes and the keymap
enum layer_names {
    _BASE,
    _FUNC,
};

// set your keycodes here
#define layer1_left  KC_PAUS
#define layer1_right KC_MPLY
#define layer2_left  KC_MPRV
#define layer2_right KC_MNXT


enum custom_keycodes {
  LEFT1 = SAFE_RANGE,
  RIGHT1,
  LEFT2,
  RIGHT2
};

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  [_BASE] = LAYOUT(
    LEFT1,  RIGHT1
  ),
  [_FUNC] = LAYOUT(
    LEFT2,  RIGHT2
  )
};

static bool key1_down = false;
static bool key2_down = false;
static char layer_switched = 0; // this prevents the individual keys getting triggered after a layerchange

bool process_record_user(uint16_t keycode, keyrecord_t *record)
{
    switch (keycode)
    {
    case LEFT1:
        if (record->event.pressed)
        {
            if (key2_down)
            {
                layer_on(1);
                layer_switched = 2;
                return false;
                break;
            }
            key1_down = true;
        }
        else {
            if (layer_switched > 0) {
                --layer_switched;
                key1_down = false;
            }
            else {
                tap_code(layer1_left);
                key1_down = false;
            }
        }
        return false;
        break;
    case RIGHT1:
        if (record->event.pressed)
        {
            if (key1_down)
            {
                layer_on(1);
                layer_switched = 2;
                return false;
                break;
            }
            key2_down = true;
        }
        else {
            if (layer_switched > 0) {
                --layer_switched;
                key2_down = false;
            }
            else {
                tap_code(layer1_right);
                key2_down = false;
            }
        }
        return false;
        break;
    case LEFT2:
        if (record->event.pressed)
        {
            if (key2_down)
            {
                layer_off(1);
                layer_switched = 2;
                return false;
                break;
            }
            key1_down = true;
        }
        else {
            if (layer_switched > 0) {
                --layer_switched;
                key1_down = false;
            }
            else {
                tap_code(layer2_left);
                key1_down = false;
            }
        }
        return false;
        break;
    case RIGHT2:
        if (record->event.pressed)
        {
            if (key1_down)
            {
                layer_off(1);
                layer_switched = 2;
                return false;
                break;
            }
            key2_down = true;
        }
        else {
            if (layer_switched > 0) {
                --layer_switched;
                key2_down = false;
            }
            else {
                tap_code(layer2_right);
                key2_down = false;
            }
        }
        return false;
        break;
    }
    return true;
};

// layer colors
layer_state_t layer_state_set_user(layer_state_t state) {
    switch (get_highest_layer(state)) {
    case _FUNC:
        rgblite_setrgb(RGB_RED);
        break;
    default: //  for any other layers, or the default layer
        rgblite_setrgb(RGB_GREEN);
        break;
    }
  return state;
}

// default color
void keyboard_post_init_user(void) {
    rgblite_setrgb(RGB_GREEN);
}