smart_lock.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. /*
  3. Copyright 2018-2022 Eric Gebhart <e.a.gebhart@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include QMK_KEYBOARD_H
  16. #include USERSPACE_H
  17. #ifdef SMART_LOCK_ENABLE
  18. typedef enum {
  19. sml_layer,
  20. sml_mod
  21. } smart_lock_type;
  22. typedef struct {
  23. bool active;
  24. const uint16_t *keys;
  25. uint16_t keycode;
  26. uint16_t thing;
  27. smart_lock_type type;
  28. } smart_lock_t;
  29. // smart layer, smart mods
  30. #undef SMLL
  31. #undef SMLM
  32. #define SMLL(key, layer, ...)
  33. #define SMLM(key, mod, ...) // to replace mod_lock..
  34. #define COND_KEYS_END 0
  35. #define CONCATENATE_SA(a, ...) a ## __VA_ARGS__
  36. #define CONCATENATE_S(a, ...) a ## __VA_ARGS__
  37. #define CAT_S(a, ...) CONCATENATE_S(a, __VA_ARGS__)
  38. #define MK_SKEY(KC) CONCATENATE_S(sml_, KC)
  39. #define MK_ARRAY(KC) \
  40. const uint16_t PROGMEM CONCATENATE_SA(sml_, KC)[]
  41. // to create an enum and find how many...
  42. #define S_ENUM(kc, layer, ...) CAT_S(sml__, kc),
  43. // create a const array of the condkeys for each SML
  44. #define S_DATA(kc, thing, ...) MK_ARRAY(kc) = {__VA_ARGS__, COND_KEYS_END};
  45. // create a list of smart_lock structs. Two names, one for mod one for layer to be concise.
  46. #define S_SMART_LOCK(kc, layer, ...) {false, MK_SKEY(kc), kc, layer, sml_layer},
  47. #define M_SMART_LOCK(kc, mod, ...) {false, MK_SKEY(kc), kc, mod, sml_mod},
  48. #define SML(sk, sa, st, stype) \
  49. { .keys = &(sk)[0], .keycode = (sa), .thing = (st), .smart_lock_type = stype}
  50. #define K_SMLM(key, mod...) [MK_SKEY(key)] = SML(MK_SKEY(key), key, mod, sml_mod),
  51. #define K_SMLL(key, layer...) [MK_SKEY(key)] = SML(MK_SKEY(key), key, layer, sml_layer),
  52. // Set everything up
  53. // - Create enum of names, (sml_keycode). Used as indexes in the arrays.
  54. // avoids using the keycodes which would create a sparse/large array.
  55. // - Create array of conditional locks..
  56. // - Create array of the conditional keys for the locks, by name.
  57. // Create Enum
  58. #undef SMLL
  59. #undef SMLM
  60. #define SMLL S_ENUM
  61. #define SMLM S_ENUM
  62. // find how many
  63. enum smart_locks {
  64. #include "smart_lock.def"
  65. SML_LENGTH
  66. };
  67. uint16_t SML_LEN = SML_LENGTH;
  68. // Bake locks into mem, name, ignore/cancel keys
  69. #undef SMLL
  70. #undef SMLM
  71. #undef TOGG
  72. #define SMLL S_DATA
  73. #define SMLM S_DATA
  74. #include "smart_lock.def"
  75. #undef SMLL
  76. #undef SMLM
  77. // Fill array of locks by name, kc, layer/mod.
  78. #define SMLL S_SMART_LOCK
  79. #define SMLM M_SMART_LOCK
  80. smart_lock_t smart_locks[] = {
  81. #include "smart_lock.def"
  82. };
  83. #undef SMLL
  84. #undef SMLM
  85. #endif