qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

action_layer.h (5861B)


      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 
     18 #pragma once
     19 
     20 #include <stdint.h>
     21 #include "keyboard.h"
     22 #include "action.h"
     23 #include "bitwise.h"
     24 
     25 #ifdef DYNAMIC_KEYMAP_ENABLE
     26 #    ifndef DYNAMIC_KEYMAP_LAYER_COUNT
     27 #        define DYNAMIC_KEYMAP_LAYER_COUNT 4
     28 #    endif
     29 #    define MAX_LAYER DYNAMIC_KEYMAP_LAYER_COUNT
     30 #    if DYNAMIC_KEYMAP_LAYER_COUNT <= 8
     31 #        ifndef LAYER_STATE_8BIT
     32 #            define LAYER_STATE_8BIT
     33 #        endif
     34 #    elif DYNAMIC_KEYMAP_LAYER_COUNT <= 16
     35 #        ifndef LAYER_STATE_16BIT
     36 #            define LAYER_STATE_16BIT
     37 #        endif
     38 #    else
     39 #        ifndef LAYER_STATE_32BIT
     40 #            define LAYER_STATE_32BIT
     41 #        endif
     42 #    endif
     43 #endif
     44 
     45 #if !defined(LAYER_STATE_8BIT) && !defined(LAYER_STATE_16BIT) && !defined(LAYER_STATE_32BIT)
     46 #    define LAYER_STATE_16BIT
     47 #endif
     48 
     49 #if defined(LAYER_STATE_8BIT)
     50 typedef uint8_t layer_state_t;
     51 #    define MAX_LAYER_BITS 3
     52 #    ifndef MAX_LAYER
     53 #        define MAX_LAYER 8
     54 #    endif
     55 #    define get_highest_layer(state) biton(state)
     56 #elif defined(LAYER_STATE_16BIT)
     57 typedef uint16_t layer_state_t;
     58 #    define MAX_LAYER_BITS 4
     59 #    ifndef MAX_LAYER
     60 #        define MAX_LAYER 16
     61 #    endif
     62 #    define get_highest_layer(state) biton16(state)
     63 #elif defined(LAYER_STATE_32BIT)
     64 typedef uint32_t layer_state_t;
     65 #    define MAX_LAYER_BITS 5
     66 #    ifndef MAX_LAYER
     67 #        define MAX_LAYER 32
     68 #    endif
     69 #    define get_highest_layer(state) biton32(state)
     70 #else
     71 #    error Layer Mask size not specified.  HOW?!
     72 #endif
     73 
     74 /*
     75  * Default Layer
     76  */
     77 extern layer_state_t default_layer_state;
     78 void                 default_layer_debug(void);
     79 void                 default_layer_set(layer_state_t state);
     80 
     81 __attribute__((weak)) layer_state_t default_layer_state_set_modules(layer_state_t state);
     82 __attribute__((weak)) layer_state_t default_layer_state_set_kb(layer_state_t state);
     83 __attribute__((weak)) layer_state_t default_layer_state_set_user(layer_state_t state);
     84 
     85 #ifndef NO_ACTION_LAYER
     86 /* bitwise operation */
     87 void default_layer_or(layer_state_t state);
     88 void default_layer_and(layer_state_t state);
     89 void default_layer_xor(layer_state_t state);
     90 #else
     91 #    define default_layer_or(state)
     92 #    define default_layer_and(state)
     93 #    define default_layer_xor(state)
     94 #endif
     95 
     96 /*
     97  * Keymap Layer
     98  */
     99 #ifndef NO_ACTION_LAYER
    100 extern layer_state_t layer_state;
    101 
    102 void layer_state_set(layer_state_t state);
    103 bool layer_state_is(uint8_t layer);
    104 bool layer_state_cmp(layer_state_t layer1, uint8_t layer2);
    105 
    106 void layer_debug(void);
    107 void layer_clear(void);
    108 void layer_move(uint8_t layer);
    109 void layer_on(uint8_t layer);
    110 void layer_off(uint8_t layer);
    111 void layer_invert(uint8_t layer);
    112 /* bitwise operation */
    113 void          layer_or(layer_state_t state);
    114 void          layer_and(layer_state_t state);
    115 void          layer_xor(layer_state_t state);
    116 layer_state_t layer_state_set_user(layer_state_t state);
    117 layer_state_t layer_state_set_kb(layer_state_t state);
    118 layer_state_t layer_state_set_modules(layer_state_t state);
    119 
    120 /**
    121  * @brief Applies the tri layer to global layer state. Not be used in layer_state_set_(kb|user) functions.
    122  *
    123  * @param layer1 First layer to check for tri layer
    124  * @param layer2 Second layer to check for tri layer
    125  * @param layer3 Layer to activate if both other layers are enabled
    126  */
    127 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
    128 /**
    129  * @brief Applies the tri layer behavior to supplied layer bitmask, without using layer functions.
    130  *
    131  * @param state Original layer bitmask to check and modify
    132  * @param layer1 First layer to check for tri layer
    133  * @param layer2 Second layer to check for tri layer
    134  * @param layer3 Layer to activate if both other layers are enabled
    135  * @return layer_state_t returns a modified layer bitmask with tri layer modifications applied
    136  */
    137 layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
    138 #else
    139 #    define layer_state 0
    140 
    141 #    define layer_state_set(layer)
    142 #    define layer_state_is(layer) (layer == 0)
    143 #    define layer_state_cmp(state, layer) (state == 0 ? layer == 0 : (state & (layer_state_t)1 << layer) != 0)
    144 
    145 #    define layer_debug()
    146 #    define layer_clear()
    147 #    define layer_move(layer) (void)layer
    148 #    define layer_on(layer) (void)layer
    149 #    define layer_off(layer) (void)layer
    150 #    define layer_invert(layer) (void)layer
    151 #    define layer_or(state) (void)state
    152 #    define layer_and(state) (void)state
    153 #    define layer_xor(state) (void)state
    154 #    define layer_state_set_modules(state) (void)state
    155 #    define layer_state_set_kb(state) (void)state
    156 #    define layer_state_set_user(state) (void)state
    157 #    define update_tri_layer(layer1, layer2, layer3)
    158 #    define update_tri_layer_state(state, layer1, layer2, layer3) (void)state
    159 #endif
    160 
    161 /* pressed actions cache */
    162 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
    163 
    164 void    update_source_layers_cache(keypos_t key, uint8_t layer);
    165 uint8_t read_source_layers_cache(keypos_t key);
    166 #endif
    167 action_t store_or_get_action(bool pressed, keypos_t key);
    168 
    169 /* return the topmost non-transparent layer currently associated with key */
    170 uint8_t layer_switch_get_layer(keypos_t key);
    171 
    172 /* return action depending on current layer status */
    173 action_t layer_switch_get_action(keypos_t key);