smart_lock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2022 Eric Gebhart <e.a.gebhart@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // Derived from smart_layers by @Possumvibes
  15. // Derived from one shot_mod by @Callum.
  16. #include "smart_lock.h"
  17. #include USERSPACE_H
  18. /* print("string"): Print a simple string. */
  19. /* uprintf("%s string", var) */
  20. bool ignore_key(uint16_t keycode,
  21. const uint16_t *cond_keys){
  22. // look for non-cancel condition.
  23. // look for keys to ignore, if we match, we do nothing.
  24. for (; pgm_read_word(cond_keys) != COND_KEYS_END ; ++cond_keys){
  25. if (pgm_read_word(cond_keys) == keycode){
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. void deactivate_sml_layer(smart_lock_t *sml){
  32. layer_off(sml->thing);
  33. sml->active = false;
  34. }
  35. void deactivate_sml_mod(smart_lock_t *sml){
  36. unregister_mods(sml->thing);
  37. sml->active = false;
  38. }
  39. void deactivate_sml(smart_lock_t *sml){
  40. switch(sml->type){
  41. case sml_layer:
  42. deactivate_sml_layer(sml);
  43. case sml_mod:
  44. deactivate_sml_mod(sml);
  45. }
  46. }
  47. void sml_activate_layer(smart_lock_t *sml){
  48. sml->active = true;
  49. layer_on(sml->thing);
  50. }
  51. void sml_maybe_activate_mod(smart_lock_t *sml ){
  52. if (sml->active) {
  53. unregister_mods(sml->thing);
  54. } else {
  55. register_mods(sml->thing);
  56. }
  57. sml->active = !sml->active;
  58. }
  59. void sml_activate(smart_lock_t *sml){
  60. switch(sml->type){
  61. case sml_layer:
  62. sml_activate_layer(sml);
  63. break;
  64. case sml_mod:
  65. sml_maybe_activate_mod(sml);
  66. break;
  67. }
  68. }
  69. void update_smart_lock(uint16_t keycode) {
  70. #ifdef SMART_LOCK_ENABLE
  71. bool deactivate = false;
  72. smart_lock_t *sml;
  73. for (int i = 0; i < SML_LEN; ++i){
  74. sml = &smart_locks[i];
  75. // if it's a match,
  76. // maybe activate/deactivate it if we got it's keycode.
  77. if (sml->keycode == keycode){
  78. sml_activate(sml);
  79. return;
  80. }
  81. // deactivate what we need to.
  82. if(sml->active){
  83. deactivate = !ignore_key(keycode, &sml->keys[0]);
  84. if (deactivate){
  85. deactivate_sml(sml);
  86. }
  87. }
  88. }
  89. #endif
  90. return;
  91. }
  92. void process_smart_lock(uint16_t keycode, keyrecord_t *record) {
  93. if (record->event.pressed) {
  94. update_smart_lock(keycode);
  95. }
  96. }