logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_layer_lock.c (3123B)


  1. // Copyright 2022-2023 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. #include "layer_lock.h"
  15. #include "process_layer_lock.h"
  16. #include "quantum_keycodes.h"
  17. #include "action_util.h"
  18. // The current lock state. The kth bit is on if layer k is locked.
  19. extern layer_state_t locked_layers;
  20. // Handles an event on an `MO` or `TT` layer switch key.
  21. static inline bool handle_mo_or_tt(uint8_t layer, keyrecord_t* record) {
  22. if (is_layer_locked(layer)) {
  23. if (record->event.pressed) { // On press, unlock the layer.
  24. layer_lock_invert(layer);
  25. }
  26. return false; // Skip default handling.
  27. }
  28. return true;
  29. }
  30. bool process_layer_lock(uint16_t keycode, keyrecord_t* record) {
  31. #ifndef NO_ACTION_LAYER
  32. layer_lock_activity_trigger();
  33. // The intention is that locked layers remain on. If something outside of
  34. // this feature turned any locked layers off, unlock them.
  35. if ((locked_layers & ~layer_state) != 0) {
  36. layer_lock_set_kb(locked_layers &= layer_state);
  37. }
  38. if (keycode == QK_LAYER_LOCK) {
  39. if (record->event.pressed) { // The layer lock key was pressed.
  40. layer_lock_invert(get_highest_layer(layer_state));
  41. }
  42. return false;
  43. }
  44. switch (keycode) {
  45. case QK_MOMENTARY ... QK_MOMENTARY_MAX: // `MO(layer)` keys.
  46. return handle_mo_or_tt(QK_MOMENTARY_GET_LAYER(keycode), record);
  47. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: // `TT(layer)`.
  48. return handle_mo_or_tt(QK_LAYER_TAP_TOGGLE_GET_LAYER(keycode), record);
  49. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX: { // `LM(layer, mod)`.
  50. uint8_t layer = QK_LAYER_MOD_GET_LAYER(keycode);
  51. if (is_layer_locked(layer)) {
  52. if (record->event.pressed) { // On press, unlock the layer.
  53. layer_lock_invert(layer);
  54. } else { // On release, clear the mods.
  55. clear_mods();
  56. send_keyboard_report();
  57. }
  58. return false; // Skip default handling.
  59. }
  60. } break;
  61. # ifndef NO_ACTION_TAPPING
  62. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: // `LT(layer, key)` keys.
  63. if (record->tap.count == 0 && !record->event.pressed && is_layer_locked(QK_LAYER_TAP_GET_LAYER(keycode))) {
  64. // Release event on a held layer-tap key where the layer is locked.
  65. return false; // Skip default handling so that layer stays on.
  66. }
  67. break;
  68. # endif // NO_ACTION_TAPPING
  69. }
  70. #endif // NO_ACTION_LAYER
  71. return true;
  72. }