]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/keymap.c
Add keymap clear/reset action
[tmk_firmware.git] / common / keymap.c
1 /*
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
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 #include <avr/pgmspace.h>
18 #include "keymap.h"
19 #include "report.h"
20 #include "keycode.h"
21 #include "layer_switch.h"
22 #include "action.h"
23 #include "debug.h"
24
25
26 static action_t keycode_to_action(uint8_t keycode);
27
28 #ifdef USE_KEYMAP_V2
29 /* converts key to action */
30 action_t action_for_key(uint8_t layer, key_t key)
31 {
32     uint8_t keycode = keymap_key_to_keycode(layer, key);
33     switch (keycode) {
34         case KC_FN0 ... KC_FN31:
35             return keymap_fn_to_action(keycode);
36         default:
37             return keycode_to_action(keycode);
38     }
39 }
40
41 __attribute__ ((weak))
42 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
43 {
44 }
45 #else
46 /* 
47  * legacy keymap support
48  */
49 /* translation for legacy keymap */
50 action_t action_for_key(uint8_t layer, key_t key)
51 {
52     /* convert from legacy keycode to action */
53     /* layer 16-31 indicate 'overlay' but not supported in legacy keymap */
54     uint8_t keycode = keymap_get_keycode((layer & OVERLAY_MASK), key.row, key.col);
55     action_t action;
56     switch (keycode) {
57         case KC_FN0 ... KC_FN31:
58             {
59                 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
60                 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
61                 if (key) {
62                     action.code = ACTION_KEYMAP_TAP_KEY(layer, key);
63                 } else {
64                     action.code = ACTION_KEYMAP_MOMENTARY(layer);
65                 }
66             }
67             return action;
68         default:
69             return keycode_to_action(keycode);
70     }
71 }
72 /* not used for legacy keymap */
73 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
74 {
75 }
76 #endif
77
78
79
80 /* translates keycode to action */
81 static action_t keycode_to_action(uint8_t keycode)
82 {
83     action_t action;
84     switch (keycode) {
85         case KC_A ... KC_EXSEL:
86             action.code = ACTION_KEY(keycode);
87             break;
88         case KC_LCTRL ... KC_LGUI:
89             action.code = ACTION_LMOD(keycode);
90             break;
91         case KC_RCTRL ... KC_RGUI:
92             action.code = ACTION_RMOD(keycode);
93             break;
94         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
95             action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
96             break;
97         case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
98             action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
99             break;
100         case KC_MS_UP ... KC_MS_ACCEL2:
101             action.code = ACTION_MOUSEKEY(keycode);
102             break;
103         case KC_TRNS:
104             action.code = ACTION_TRANSPARENT;
105             break;
106         default:
107             action.code = ACTION_NO;
108             break;
109     }
110     return action;
111 }