aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/process_keycode/process_magic.c
blob: 44dd5f0579552eef30319f0a8623359137a6cc5d (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
174
175
176
177
178
/* Copyright 2019 Jack Humbert
 *
 * 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 "process_magic.h"

#ifdef AUDIO_ENABLE
#    ifndef AG_NORM_SONG
#        define AG_NORM_SONG SONG(AG_NORM_SOUND)
#    endif
#    ifndef AG_SWAP_SONG
#        define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
#    endif
#    ifndef CG_NORM_SONG
#        define CG_NORM_SONG SONG(AG_NORM_SOUND)
#    endif
#    ifndef CG_SWAP_SONG
#        define CG_SWAP_SONG SONG(AG_SWAP_SOUND)
#    endif
float ag_norm_song[][2] = AG_NORM_SONG;
float ag_swap_song[][2] = AG_SWAP_SONG;
float cg_norm_song[][2] = CG_NORM_SONG;
float cg_swap_song[][2] = CG_SWAP_SONG;
#endif

/**
 * MAGIC actions (BOOTMAGIC without the boot)
 */
bool process_magic(uint16_t keycode, keyrecord_t *record) {
    // skip anything that isn't a keyup
    if (record->event.pressed) {
        switch (keycode) {
            case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_ALT_GUI:
            case MAGIC_SWAP_LCTL_LGUI ... MAGIC_EE_HANDS_RIGHT:
                /* keymap config */
                keymap_config.raw = eeconfig_read_keymap();
                switch (keycode) {
                    case MAGIC_SWAP_CONTROL_CAPSLOCK:
                        keymap_config.swap_control_capslock = true;
                        break;
                    case MAGIC_CAPSLOCK_TO_CONTROL:
                        keymap_config.capslock_to_control = true;
                        break;
                    case MAGIC_SWAP_LALT_LGUI:
                        keymap_config.swap_lalt_lgui = true;
                        break;
                    case MAGIC_SWAP_RALT_RGUI:
                        keymap_config.swap_ralt_rgui = true;
                        break;
                    case MAGIC_SWAP_LCTL_LGUI:
                        keymap_config.swap_lctl_lgui = true;
                        break;
                    case MAGIC_SWAP_RCTL_RGUI:
                        keymap_config.swap_rctl_rgui = true;
                        break;
                    case MAGIC_NO_GUI:
                        keymap_config.no_gui = true;
                        break;
                    case MAGIC_SWAP_GRAVE_ESC:
                        keymap_config.swap_grave_esc = true;
                        break;
                    case MAGIC_SWAP_BACKSLASH_BACKSPACE:
                        keymap_config.swap_backslash_backspace = true;
                        break;
                    case MAGIC_HOST_NKRO:
                        clear_keyboard();  // clear first buffer to prevent stuck keys
                        keymap_config.nkro = true;
                        break;
                    case MAGIC_SWAP_ALT_GUI:
                        keymap_config.swap_lalt_lgui = keymap_config.swap_ralt_rgui = true;
#ifdef AUDIO_ENABLE
                        PLAY_SONG(ag_swap_song);
#endif
                        break;
                    case MAGIC_SWAP_CTL_GUI:
                        keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = true;
#ifdef AUDIO_ENABLE
                        PLAY_SONG(cg_swap_song);
#endif
                        break;
                    case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
                        keymap_config.swap_control_capslock = false;
                        break;
                    case MAGIC_UNCAPSLOCK_TO_CONTROL:
                        keymap_config.capslock_to_control = false;
                        break;
                    case MAGIC_UNSWAP_LALT_LGUI:
                        keymap_config.swap_lalt_lgui = false;
                        break;
                    case MAGIC_UNSWAP_RALT_RGUI:
                        keymap_config.swap_ralt_rgui = false;
                        break;
                    case MAGIC_UNSWAP_LCTL_LGUI:
                        keymap_config.swap_lctl_lgui = false;
                        break;
                    case MAGIC_UNSWAP_RCTL_RGUI:
                        keymap_config.swap_rctl_rgui = false;
                        break;
                    case MAGIC_UNNO_GUI:
                        keymap_config.no_gui = false;
                        break;
                    case MAGIC_UNSWAP_GRAVE_ESC:
                        keymap_config.swap_grave_esc = false;
                        break;
                    case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
                        keymap_config.swap_backslash_backspace = false;
                        break;
                    case MAGIC_UNHOST_NKRO:
                        clear_keyboard();  // clear first buffer to prevent stuck keys
                        keymap_config.nkro = false;
                        break;
                    case MAGIC_UNSWAP_ALT_GUI:
                        keymap_config.swap_lalt_lgui = keymap_config.swap_ralt_rgui = false;
#ifdef AUDIO_ENABLE
                        PLAY_SONG(ag_norm_song);
#endif
                        break;
                    case MAGIC_UNSWAP_CTL_GUI:
                        keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = false;
#ifdef AUDIO_ENABLE
                        PLAY_SONG(cg_norm_song);
#endif
                        break;
                    case MAGIC_TOGGLE_ALT_GUI:
                        keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
                        keymap_config.swap_ralt_rgui = keymap_config.swap_lalt_lgui;
#ifdef AUDIO_ENABLE
                        if (keymap_config.swap_ralt_rgui) {
                            PLAY_SONG(ag_swap_song);
                        } else {
                            PLAY_SONG(ag_norm_song);
                        }
#endif
                        break;
                    case MAGIC_TOGGLE_CTL_GUI:
                        keymap_config.swap_lctl_lgui = !keymap_config.swap_lctl_lgui;
                        keymap_config.swap_rctl_rgui = keymap_config.swap_lctl_lgui;
#ifdef AUDIO_ENABLE
                        if (keymap_config.swap_rctl_rgui) {
                            PLAY_SONG(cg_swap_song);
                        } else {
                            PLAY_SONG(cg_norm_song);
                        }
#endif
                        break;
                    case MAGIC_TOGGLE_NKRO:
                        clear_keyboard();  // clear first buffer to prevent stuck keys
                        keymap_config.nkro = !keymap_config.nkro;
                        break;
                    case MAGIC_EE_HANDS_LEFT:
                        eeconfig_update_handedness(true);
                        break;
                    case MAGIC_EE_HANDS_RIGHT:
                        eeconfig_update_handedness(false);
                        break;
                }

                eeconfig_update_keymap(keymap_config.raw);
                clear_keyboard();  // clear to prevent stuck keys

                return false;
        }
    }

    // Not a magic keycode so continue processing
    return true;
}
* * @param[in] canp pointer to the @p CANDriver object * @param[out] crfp pointer to the buffer where the CAN frame is copied * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout (useful in an * event driven scenario where a thread never blocks * for I/O). * - @a TIME_INFINITE no timeout. * . * @return The operation result. * @retval RDY_OK a frame has been received and placed in the buffer. * @retval RDY_TIMEOUT The operation has timed out. * @retval RDY_RESET The driver has been stopped while waiting. * * @api */ msg_t canReceive(CANDriver *canp, CANRxFrame *crfp, systime_t timeout) { chDbgCheck((canp != NULL) && (crfp != NULL), "canReceive"); chSysLock(); chDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP), "canReceive(), #1", "invalid state"); while ((canp->state == CAN_SLEEP) || !can_lld_can_receive(canp)) { msg_t msg = chSemWaitTimeoutS(&canp->rxsem, timeout); if (msg != RDY_OK) { chSysUnlock(); return msg; } } can_lld_receive(canp, crfp); chSysUnlock(); return RDY_OK; } /** * @brief Returns the current status mask and clears it. * * @param[in] canp pointer to the @p CANDriver object * @return The status flags mask. * * @api */ canstatus_t canGetAndClearFlags(CANDriver *canp) { canstatus_t status; chSysLock(); status = canp->status; canp->status = 0; chSysUnlock(); return status; } #if CAN_USE_SLEEP_MODE || defined(__DOXYGEN__) /** * @brief Enters the sleep mode. * @details This function puts the CAN driver in sleep mode and broadcasts * the @p sleep_event event source. * @pre In order to use this function the option @p CAN_USE_SLEEP_MODE must * be enabled and the @p CAN_SUPPORTS_SLEEP mode must be supported * by the low level driver. * * @param[in] canp pointer to the @p CANDriver object * * @api */ void canSleep(CANDriver *canp) { chDbgCheck(canp != NULL, "canSleep"); chSysLock(); chDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP), "canSleep(), #1", "invalid state"); if (canp->state == CAN_READY) { can_lld_sleep(canp); canp->state = CAN_SLEEP; chEvtBroadcastI(&canp->sleep_event); chSchRescheduleS(); } chSysUnlock(); } /** * @brief Enforces leaving the sleep mode. * @note The sleep mode is supposed to be usually exited automatically by * an hardware event. * * @param[in] canp pointer to the @p CANDriver object */ void canWakeup(CANDriver *canp) { chDbgCheck(canp != NULL, "canWakeup"); chSysLock(); chDbgAssert((canp->state == CAN_READY) || (canp->state == CAN_SLEEP), "canWakeup(), #1", "invalid state"); if (canp->state == CAN_SLEEP) { can_lld_wakeup(canp); canp->state = CAN_READY; chEvtBroadcastI(&canp->wakeup_event); chSchRescheduleS(); } chSysUnlock(); } #endif /* CAN_USE_SLEEP_MODE */ #endif /* HAL_USE_CAN */ /** @} */