]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - macway/keymap.c
new build method for macway
[max/tmk_keyboard.git] / macway / keymap.c
1 /* 
2  * Keymap for Macway mod
3  */
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/pgmspace.h>
7 #include "usb_keyboard.h"
8 #include "usb_keycodes.h"
9 #include "matrix.h"
10 #include "print.h"
11 #include "debug.h"
12 #include "util.h"
13 #include "keymap.h"
14
15
16 #define FN_KEYCODE(fn) (pgm_read_byte(&fn_keycode[(fn)]))
17 #define FN_LAYER(fn)   (pgm_read_byte(&fn_layer[(fn)]))
18 #define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)]))
19 #define KEYMAP( \
20     R1C1, R1C0, R2C0, R3C0, R4C0, R4C1, R5C1, R5C0, R6C0, R7C0, R8C0, R8C1, R6C1, R0C2, \
21     R1C2, R1C3, R2C3, R3C3, R4C3, R4C2, R5C2, R5C3, R6C3, R7C3, R8C3, R8C2, R6C2, \
22     R1C5, R1C4, R2C4, R3C4, R4C4, R4C5, R5C5, R5C4, R6C4, R7C4, R8C4, R8C5, R0C6, \
23     R6C7, R1C6, R2C6, R3C6, R4C6, R4C7, R5C7, R5C6, R6C6, R7C6, R8C7, R3C2, R3C5, \
24     R7C5, R2C2, R0C0, R0C7, R2C1, R0C4, R3C7, R2C7, R1C7 \
25 ) { \
26     { R0C0, KB_NO, R0C2,  KB_NO, R0C4, KB_NO, R0C6,  R0C7 }, \
27     { R1C0, R1C1,  R1C2,  R1C3,  R1C4, R1C5,  R1C6,  R1C7 }, \
28     { R2C0, R2C1,  R2C2,  R2C3,  R2C4, KB_NO, R2C6,  R2C7 }, \
29     { R3C0, KB_NO, R3C2,  R3C3,  R3C4, R3C5,  R3C6,  R3C7 }, \
30     { R4C0, R4C1,  R4C2,  R4C3,  R4C4, R4C5,  R4C6,  R4C7 }, \
31     { R5C0, R5C1,  R5C2,  R5C3,  R5C4, R5C5,  R5C6,  R5C7 }, \
32     { R6C0, R6C1,  R6C2,  R6C3,  R6C4, KB_NO, R6C6,  R6C7 }, \
33     { R7C0, KB_NO, KB_NO, R7C3,  R7C4, R7C5,  R7C6,  KB_NO }, \
34     { R8C0, R8C1,  R8C2,  R8C3,  R8C4, R8C5,  KB_NO, R8C7 } \
35 }
36
37
38 static int current_layer = 0;
39 static bool layer_used = false;
40
41 /* layer to change into while Fn key pressed */ 
42 static const int PROGMEM fn_layer[] = { 0, 1, 2, 3, 4, 0, 2, 3 };
43
44 /* keycode to sent when Fn key released without using layer keys. */
45 static const uint8_t PROGMEM fn_keycode[] = {
46     KB_NO,          // FN_0 [NOT USED]
47     KB_NO,          // FN_1 layer 1
48     KB_QUOTE,       // FN_2 layer 2
49     KB_SCOLON,      // FN_3 layer 3
50     KB_SPACE,       // FN_4 layer 4 [NOT USED]
51     KB_NO,          // FN_5 [NOT USED]
52     KB_NO,          // FN_6 layer 2
53     KB_NO           // FN_7 layer 3
54 };
55
56 static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
57     /* Layer 0: Default Layer
58      * ,-----------------------------------------------------------.
59      * |Esc|  1|  2|  3|  4|  5|  6|  7|  8|  9|  0|  -|  =|Backsp |
60      * |-----------------------------------------------------------|
61      * |Tab  |  Q|  W|  E|  R|  T|  Y|  U|  I|  O|  P|  [|  ]|     |
62      * |-----------------------------------------------------'     |
63      * |Contro|  A|  S|  D|  F|  G|  H|  J|  K|  L|Fn3|Fn2|Return  |
64      * |-----------------------------------------------------------|
65      * |Shift   |  Z|  X|  C|  V|  B|  N|  M|  ,|  .|  /|Shift |Fn1|
66      * |-----------------------------------------------------------|
67      * |Fn7|Gui |Alt  |Space                 |Fn6  |\  |`  |   |   |
68      * `-----------------------------------------------------------'
69      */
70     KEYMAP(KB_ESC, KB_1,   KB_2,   KB_3,   KB_4,   KB_5,   KB_6,   KB_7,   KB_8,   KB_9,   KB_0,   KB_MINS,KB_EQL, KB_BSPC, \
71            KB_TAB, KB_Q,   KB_W,   KB_E,   KB_R,   KB_T,   KB_Y,   KB_U,   KB_I,   KB_O,   KB_P,   KB_LBRC,KB_RBRC, \
72            KB_LCTL,KB_A,   KB_S,   KB_D,   KB_F,   KB_G,   KB_H,   KB_J,   KB_K,   KB_L,   FN_3,   FN_2,   KB_ENT, \
73            KB_LSFT,KB_Z,   KB_X,   KB_C,   KB_V,   KB_B,   KB_N,   KB_M,   KB_COMM,KB_DOT, KB_SLSH,KB_RSFT,FN_1, \
74            FN_7,   KB_LGUI,KB_LALT,KB_SPC, FN_6,   KB_BSLS,KB_GRV, KB_NO,  KB_NO),
75
76     /* Layer 1: HHKB mode (HHKB Fn)
77      * ,-----------------------------------------------------------.
78      * |Pow| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
79      * |-----------------------------------------------------------|
80      * |Caps |   |   |   |   |   |   |   |Psc|Slk|Pus|Up |   |     |
81      * |-----------------------------------------------------'     |
82      * |Contro|   |   |   |   |   |  *|  /|Hom|PgU|Lef|Rig|Enter   |
83      * |-----------------------------------------------------------|
84      * |Shift   |   |   |   |   |   |  +|  -|End|PgD|Dow|Shift |xxx|
85      * |-----------------------------------------------------------|
86      * |   |Gui |Alt  |                      |Alt  |   |   |   |   |
87      * `-----------------------------------------------------------'
88      */ 
89     KEYMAP(KB_PWR, KB_F1,  KB_F2,  KB_F3,  KB_F4,  KB_F5,  KB_F6,  KB_F7,  KB_F8,  KB_F9,  KB_F10, KB_F11, KB_F12, KB_DEL, \
90            KB_CAPS,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_PSCR,KB_SLCK,KB_BRK, KB_UP,  KB_NO, \
91            KB_LCTL,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KP_ASTR,KP_SLSH,KB_HOME,KB_PGUP,KB_LEFT,KB_RGHT,KB_ENT, \
92            KB_LSFT,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KP_PLUS,KP_MINS,KB_END, KB_PGDN,KB_DOWN,KB_RSFT,FN_1, \
93            KB_NO,  KB_LGUI,KB_LALT,KB_SPC, KB_RALT,KB_NO,  KB_NO,  KB_NO,  KB_NO),
94
95     /* Layer 2: Vi mode (Quote/Rmeta)
96      * ,-----------------------------------------------------------.
97      * |  `| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|   `   |
98      * |-----------------------------------------------------------|
99      * |  \  |Hom|PgD|Up |PgU|End|Hom|PgD|PgU|End|   |   |   |     |
100      * |-----------------------------------------------------'     |
101      * |Contro|   |Lef|Dow|Rig|   |Lef|Dow|Up |Rig|   |xxx|   \    |
102      * |-----------------------------------------------------------|
103      * |Shift   |   |   |   |   |   |Hom|PgD|PgU|End|   |Shift |   |
104      * |-----------------------------------------------------------|
105      * |   |Gui |Alt  |Space                 |xxxxx|   |   |   |   |
106      * `-----------------------------------------------------------'
107      */
108     KEYMAP(KB_GRV, KB_F1,  KB_F2,  KB_F3,  KB_F4,  KB_F5,  KB_F6,  KB_F7,  KB_F8,  KB_F9,  KB_F10, KB_F11, KB_F12, KB_GRV, \
109            KB_BSLS,KB_HOME,KB_PGDN,KB_UP,  KB_PGUP,KB_END, KB_HOME,KB_PGDN,KB_PGUP,KB_END, KB_NO,  KB_NO,  KB_NO, \
110            KB_LCTL,KB_NO,  KB_LEFT,KB_DOWN,KB_RGHT,KB_NO,  KB_LEFT,KB_DOWN,KB_UP,  KB_RGHT,KB_NO,  FN_2,   KB_BSLS, \
111            KB_LSFT,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_RSFT,KB_NO, \
112            KB_NO,  KB_LGUI,KB_LALT,KB_SPC, FN_6,   KB_NO,  KB_NO,  KB_NO,  KB_NO),
113
114     /* Layer 3: Mouse mode (Semicolon)
115      * ,-------------------------------------------------------- --.
116      * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
117      * |-----------------------------------------------------------|
118      * |Tab  |MwL|MwU|McU|MwD|MwR|MwL|MwD|MwU|MwR|   |   |   |     |
119      * |-----------------------------------------------------'     |
120      * |Contro|Mb1|Mb2|Mb3|   |   |McL|McD|McU|McR|xxx|   |Return  |
121      * |-----------------------------------------------------------|
122      * |Shift   |   |   |   |   |   |MwL|MwD|MwU|MwR|   |Shift |   |
123      * |-----------------------------------------------------------|
124      * |xxx|Gui |Alt  |Mb1                   |Alt  |   |   |   |   |
125      * `-----------------------------------------------------------'
126      * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel 
127      */
128     KEYMAP(KB_ESC, KB_F1,  KB_F2,  KB_F3,  KB_F4,  KB_F5,  KB_F6,  KB_F7,  KB_F8,  KB_F9,  KB_F10, KB_F11, KB_F12, KB_DEL, \
129            KB_TAB, MS_WH_L,MS_WH_U,MS_UP,  MS_WH_D,MS_WH_R,MS_WH_L,MS_WH_D,MS_WH_U,MS_WH_R,KB_NO,  KB_NO,  KB_NO, \
130            KB_LCTL,KB_NO,  MS_LEFT,MS_DOWN,MS_RGHT,KB_NO,  MS_LEFT,MS_DOWN,MS_UP,  MS_RGHT,FN_3,   KB_NO,  KB_ENT, \
131            KB_LSFT,KB_NO,  MS_DOWN,KB_NO,  KB_NO,  KB_NO,  MS_BTN2,MS_BTN1,MS_BTN2,MS_BTN3,KB_NO,  KB_RSFT,KB_NO, \
132            FN_7,   KB_LGUI,KB_LALT,MS_BTN1,KB_RALT,KB_NO,  KB_NO,  KB_NO,  KB_NO),
133
134     /* Layer 4: Matias half keyboard style (Space)
135      * ,-----------------------------------------------------------.
136      * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
137      * |-----------------------------------------------------------|
138      * |Backs|  P|  O|  I|  U|  Y|  T|  R|  E|  W|  Q|Tab|Tab|     |
139      * |-----------------------------------------------------'     |
140      * |Contro|  ;|  L|  K|  J|  H|  G|  F|  D|  S|  A|Con|Control |
141      * |-----------------------------------------------------------|
142      * |Shift   |  /|  .|  ,|  M|  N|  B|  V|  C|  X|  Z|Shift |   |
143      * |-----------------------------------------------------------|
144      * |   |Gui |Alt  |xxxxxxxxxxxxxxxxxxxxxx|Alt  |   |   |   |   |
145      * `-----------------------------------------------------------'
146      */
147     KEYMAP(KB_MINS,KB_0,   KB_9,   KB_8,   KB_7,   KB_6,   KB_5,   KB_4,   KB_3,   KB_2,   KB_1,   KB_NO,  KB_NO,  KB_ESC, \
148            KB_BSPC,KB_P,   KB_O,   KB_I,   KB_U,   KB_Y,   KB_T,   KB_R,   KB_E,   KB_W,   KB_Q,   KB_TAB, KB_TAB, \
149            KB_LCTL,KB_SCLN,KB_L,   KB_K,   KB_J,   KB_H,   KB_G,   KB_F,   KB_D,   KB_S,   KB_A,   KB_RCTL,KB_RCTL, \
150            KB_LSFT,KB_SLSH,KB_DOT, KB_COMM,KB_M,   KB_N,   KB_B,   KB_V,   KB_C,   KB_X,   KB_Z,   KB_RSFT,KB_NO, \
151            KB_NO,  KB_LGUI,KB_LALT,FN_4,   KB_RALT,KB_NO,  KB_NO,  KB_NO,  KB_NO),
152 };
153
154
155 uint8_t keymap_get_keycode(int row, int col)
156 {
157     return keymap_get_keycodel(current_layer, row, col);
158 }
159
160 uint8_t keymap_get_keycodel(int layer, int row, int col)
161 {
162     uint8_t code = KEYCODE(layer, row, col);
163     // normal key or mouse key
164     if (IS_KEY(code) || IS_MOUSE(code))
165         layer_used = true;
166     return code;
167 }
168
169 inline
170 int keymap_get_layer(void)
171 {
172     return current_layer;
173 }
174
175 inline
176 int keymap_set_layer(int layer)
177 {
178     current_layer = layer;
179     return current_layer;
180 }
181
182 inline
183 bool keymap_is_special_mode(int fn_bits)
184 {
185     return (keyboard_modifier_keys == (BIT_LCTRL | BIT_LSHIFT | BIT_LALT | BIT_LGUI));
186 }
187
188 void keymap_fn_proc(int fn_bits)
189 {
190     // layer switching
191     static int last_bits = 0;
192     static uint8_t last_mod = 0;
193
194     if (usb_keyboard_has_key() || fn_bits == last_bits) {
195         // do nothing during press other than Fn key 
196         return;
197     } else if (fn_bits == 0) {
198         // send key when Fn key is released without using the layer
199         if (!layer_used) {
200             uint8_t code = FN_KEYCODE(biton(last_bits));
201             if (code != KB_NO) {
202                 if (IS_MOD(code)) {
203                     keyboard_modifier_keys = last_mod | 1<<(code & 0x07);
204                 } else {
205                     keyboard_keys[0] = code;
206                     keyboard_modifier_keys = last_mod;
207                 }
208                 usb_keyboard_send();
209                 usb_keyboard_print();
210                 usb_keyboard_clear();
211             }
212         }
213         last_bits = 0;
214         last_mod = 0;
215         layer_used = false;
216         keymap_set_layer(0); // default layer
217     } else if ((fn_bits & (fn_bits - 1)) == 0) {
218         // switch layer when just one Fn Key is pressed
219         last_bits = fn_bits;
220         last_mod = keyboard_modifier_keys;
221         layer_used = false;
222         keymap_set_layer(FN_LAYER(biton(fn_bits)));
223         debug("layer: "); phex(current_layer); debug("(");
224         debug_bin(last_bits); debug(")\n");
225         debug("last_mod: "); debug_hex(last_mod); debug("\n");
226     }
227 }