]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/keymap.c
Add NO_ACTION_ONESHOT config option
[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 "action_macro.h"
24 #include "debug.h"
25
26
27 static action_t keycode_to_action(uint8_t keycode);
28
29
30 /* converts key to action */
31 action_t action_for_key(uint8_t layer, key_t key)
32 {
33     uint8_t keycode = keymap_key_to_keycode(layer, key);
34     switch (keycode) {
35         case KC_FN0 ... KC_FN31:
36             return keymap_fn_to_action(keycode);
37         default:
38             return keycode_to_action(keycode);
39     }
40 }
41
42
43 /* Macro */
44 __attribute__ ((weak))
45 const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
46 {
47     return MACRO_NONE;
48 }
49
50 /* Function */
51 __attribute__ ((weak))
52 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
53 {
54 }
55
56
57
58 /* translates keycode to action */
59 static action_t keycode_to_action(uint8_t keycode)
60 {
61     action_t action;
62     switch (keycode) {
63         case KC_A ... KC_EXSEL:
64         case KC_LCTRL ... KC_RGUI:
65             action.code = ACTION_KEY(keycode);
66             break;
67         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
68             action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
69             break;
70         case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
71             action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
72             break;
73         case KC_MS_UP ... KC_MS_ACCEL2:
74             action.code = ACTION_MOUSEKEY(keycode);
75             break;
76         case KC_TRNS:
77             action.code = ACTION_TRANSPARENT;
78             break;
79         default:
80             action.code = ACTION_NO;
81             break;
82     }
83     return action;
84 }
85
86
87
88 #ifdef USE_LEGACY_KEYMAP
89 /*
90  * Legacy keymap support
91  *      Consider using new keymap API instead.
92  */
93 __attribute__ ((weak))
94 uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
95 {
96     return keymap_get_keycode(layer, key.row, key.col);
97 }
98
99
100 /* Legacy keymap support */
101 __attribute__ ((weak))
102 action_t keymap_fn_to_action(uint8_t keycode)
103 {
104     action_t action = { .code = ACTION_NO };
105     switch (keycode) {
106         case KC_FN0 ... KC_FN31:
107             {
108                 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
109                 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
110                 if (key) {
111                     action.code = ACTION_KEYMAP_TAP_KEY(layer, key);
112                 } else {
113                     action.code = ACTION_KEYMAP_MOMENTARY(layer);
114                 }
115             }
116             return action;
117         default:
118             return action;
119     }
120 }
121 #endif