|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// |
| 16 | +// For full documentation, see |
| 17 | +// https://getreuer.info/posts/keyboards/layer-lock |
| 18 | + |
| 19 | +#include "layer_lock.h" |
| 20 | + |
| 21 | +// The current lock state. The kth bit is on if layer k is locked. |
| 22 | +static layer_state_t locked_layers = 0; |
| 23 | + |
| 24 | +bool process_layer_lock(uint16_t keycode, keyrecord_t* record, |
| 25 | + uint16_t lock_keycode) { |
| 26 | + // The intention is that locked layers remain on. If something outside of |
| 27 | + // this feature turned any locked layers off, unlock them. |
| 28 | + if ((locked_layers & ~layer_state) != 0) { |
| 29 | + layer_lock_set_user(locked_layers &= layer_state); |
| 30 | + } |
| 31 | + |
| 32 | + if (keycode == lock_keycode) { |
| 33 | + if (record->event.pressed) { // The layer lock key was pressed. |
| 34 | + layer_lock_invert(get_highest_layer(layer_state)); |
| 35 | + } |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + switch (keycode) { |
| 40 | + case QK_MOMENTARY ... QK_MOMENTARY_MAX: // `MO(layer)` keys. |
| 41 | + case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: { // `TT(layer)`. |
| 42 | + const uint8_t layer = keycode & 255; |
| 43 | + if (is_layer_locked(layer)) { |
| 44 | + // Event on `MO` or `TT` key where layer is locked. |
| 45 | + if (record->event.pressed) { // On press, unlock the layer. |
| 46 | + layer_lock_invert(layer); |
| 47 | + } |
| 48 | + return false; // Skip default handling. |
| 49 | + } |
| 50 | + } break; |
| 51 | + |
| 52 | +#ifndef NO_ACTION_TAPPING |
| 53 | + case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: // `LT(layer, key)` keys. |
| 54 | + if (record->tap.count == 0 && !record->event.pressed && |
| 55 | + is_layer_locked((keycode >> 8) & 15)) { |
| 56 | + // Release event on a held layer-tap key where the layer is locked. |
| 57 | + return false; // Skip default handling so that layer stays on. |
| 58 | + } |
| 59 | + break; |
| 60 | +#endif // NO_ACTION_TAPPING |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +bool is_layer_locked(uint8_t layer) { |
| 67 | + return locked_layers & ((layer_state_t)1 << layer); |
| 68 | +} |
| 69 | + |
| 70 | +void layer_lock_invert(uint8_t layer) { |
| 71 | + const layer_state_t mask = (layer_state_t)1 << layer; |
| 72 | + if ((locked_layers & mask) == 0) { // Layer is being locked. |
| 73 | +#ifndef NO_ACTION_ONESHOT |
| 74 | + if (layer == get_oneshot_layer()) { |
| 75 | + reset_oneshot_layer(); // Reset so that OSL doesn't turn layer off. |
| 76 | + } |
| 77 | +#endif // NO_ACTION_ONESHOT |
| 78 | + layer_on(layer); |
| 79 | + } else { // Layer is being unlocked. |
| 80 | + layer_off(layer); |
| 81 | + } |
| 82 | + layer_lock_set_user(locked_layers ^= mask); |
| 83 | +} |
| 84 | + |
| 85 | +// Implement layer_lock_on/off by deferring to layer_lock_invert. |
| 86 | +void layer_lock_on(uint8_t layer) { |
| 87 | + if (!is_layer_locked(layer)) { layer_lock_invert(layer); } |
| 88 | +} |
| 89 | + |
| 90 | +void layer_lock_off(uint8_t layer) { |
| 91 | + if (is_layer_locked(layer)) { layer_lock_invert(layer); } |
| 92 | +} |
| 93 | + |
| 94 | +__attribute__((weak)) void layer_lock_set_user(layer_state_t locked_layers) {} |
| 95 | + |
0 commit comments