You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
8.5 KiB

  1. /*
  2. * Copyright 2021 Jonas Gessner
  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. #pragma once
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include "action_layer.h"
  22. /**
  23. * Key overrides allow you to send a different key-modifier combination or perform a custom action when a certain modifier-key combination is pressed.
  24. *
  25. * For example, you may configure a key override to send the delete key when shift + backspace are pressed together, or that your volume keys become screen brightness keys when holding ctrl. The possibilities are quite vast and the documentation contains a few examples for inspiration.
  26. *
  27. * See the documentation and examples here: https://docs.qmk.fm/#/feature_key_overrides
  28. */
  29. /** Bitfield with various options controlling the behavior of a key override. */
  30. typedef enum {
  31. /** Allow activating when the trigger key is pressed down. */
  32. ko_option_activation_trigger_down = (1 << 0),
  33. /** Allow activating when a necessary modifier is pressed down. */
  34. ko_option_activation_required_mod_down = (1 << 1),
  35. /** Allow activating when a negative modifier is released. */
  36. ko_option_activation_negative_mod_up = (1 << 2),
  37. ko_options_all_activations = ko_option_activation_negative_mod_up | ko_option_activation_required_mod_down | ko_option_activation_trigger_down,
  38. /** If set, any of the modifiers in trigger_mods will be enough to activate the override (logical OR of modifiers). If not set, all the modifiers in trigger_mods have to be pressed (logical AND of modifiers). */
  39. ko_option_one_mod = (1 << 3),
  40. /** If set, the trigger key will never be registered again after the override is deactivated. */
  41. ko_option_no_reregister_trigger = (1 << 4),
  42. /** If set, the override will not deactivate when another key is pressed down. Use only if you really know you need this. */
  43. ko_option_no_unregister_on_other_key_down = (1 << 5),
  44. /** The default options used by the ko_make_xxx functions. */
  45. ko_options_default = ko_options_all_activations,
  46. } ko_option_t;
  47. /** Defines a single key override */
  48. typedef struct {
  49. // The non-modifier keycode that triggers the override. This keycode, and the necessary modifiers (trigger_mods) must be pressed to activate this override. Set this to the keycode of the key that should activate the override. Set to KC_NO to require only the necessary modifiers to be pressed and no non-modifier.
  50. uint16_t trigger;
  51. // Which mods need to be down for activation. If both sides of a modifier are set (e.g. left ctrl and right ctrl) then only one is required to be pressed (e.g. left ctrl suffices). Use the MOD_MASK_XXX and MOD_BIT() macros for this.
  52. uint8_t trigger_mods;
  53. // This is a BITMASK (!), defining which layers this override applies to. To use this override on layer i set the ith bit (1 << i).
  54. layer_state_t layers;
  55. // Which modifiers cannot be down. It must hold that (active_mods & negative_mod_mask) == 0, otherwise the key override will not be activated. An active override will be deactivated once this is no longer true.
  56. uint8_t negative_mod_mask;
  57. // Modifiers to 'suppress' while the override is active. To suppress a modifier means that even though the modifier key is held down, the host OS sees the modifier as not pressed. Can be used to suppress the trigger modifiers, as a trivial example.
  58. uint8_t suppressed_mods;
  59. // The complex keycode to send as replacement when this override is triggered. This can be a simple keycode, a key-modifier combination (e.g. C(KC_A)), or KC_NO (to register no replacement keycode). Use in combination with suppressed_mods to get the correct modifiers to be sent.
  60. uint16_t replacement;
  61. // Options controlling the behavior of the override, such as what actions are allowed to activate the override.
  62. ko_option_t options;
  63. // If not NULL, this function will be called right before the replacement key is registered, along with the provided context and a flag indicating whether the override was activated or deactivated. This function allows you to run some custom actions for specific key overrides. If you return `false`, the replacement key is not registered/unregistered as it would normally. Return `true` to register and unregister the override normally.
  64. bool (*custom_action)(bool activated, void *context);
  65. // A context that will be passed to the custom action function.
  66. void *context;
  67. // If this points to false this override will not be used. Set to NULL to always have this override enabled.
  68. bool *enabled;
  69. } key_override_t;
  70. /** Define this as a null-terminated array of pointers to key overrides. These key overrides will be used by qmk. */
  71. extern const key_override_t **key_overrides;
  72. /** Turns key overrides on */
  73. void key_override_on(void);
  74. /** Turns key overrides off */
  75. void key_override_off(void);
  76. /** Toggles key overrides on */
  77. void key_override_toggle(void);
  78. /** Returns whether key overrides are enabled */
  79. bool key_override_is_enabled(void);
  80. /** Handling of key overrides and its implemented keycodes */
  81. bool process_key_override(const uint16_t keycode, const keyrecord_t *const record);
  82. /** Perform any deferred keys */
  83. void key_override_task(void);
  84. /**
  85. * Preferrably use these macros to create key overrides. They fix many of the options to a standard setting that should satisfy most basic use-cases. Only directly create a key_override_t struct when you really need to.
  86. */
  87. // clang-format off
  88. /**
  89. * Convenience initializer to create a basic key override. Activates the override on all layers.
  90. */
  91. #define ko_make_basic(trigger_mods, trigger_key, replacement_key) \
  92. ko_make_with_layers(trigger_mods, trigger_key, replacement_key, ~0)
  93. /**
  94. * Convenience initializer to create a basic key override. Provide a bitmap (of type layer_state_t) with the bits set for each layer on which the override should activate.
  95. */
  96. #define ko_make_with_layers(trigger_mods, trigger_key, replacement_key, layers) \
  97. ko_make_with_layers_and_negmods(trigger_mods, trigger_key, replacement_key, layers, 0)
  98. /**
  99. * Convenience initializer to create a basic key override. Provide a bitmap with the bits set for each layer on which the override should activate. Also provide a negative modifier mask, that is used to define which modifiers may not be pressed.
  100. */
  101. #define ko_make_with_layers_and_negmods(trigger_mods, trigger_key, replacement_key, layers, negative_mask) \
  102. ko_make_with_layers_negmods_and_options(trigger_mods, trigger_key, replacement_key, layers, negative_mask, ko_options_default)
  103. /**
  104. * Convenience initializer to create a basic key override. Provide a bitmap with the bits set for each layer on which the override should activate. Also provide a negative modifier mask, that is used to define which modifiers may not be pressed. Provide options for additional control of the behavior of the override.
  105. */
  106. #define ko_make_with_layers_negmods_and_options(trigger_mods_, trigger_key, replacement_key, layer_mask, negative_mask, options_) \
  107. ((const key_override_t){ \
  108. .trigger_mods = (trigger_mods_), \
  109. .layers = (layer_mask), \
  110. .suppressed_mods = (trigger_mods_), \
  111. .options = (options_), \
  112. .negative_mod_mask = (negative_mask), \
  113. .custom_action = NULL, \
  114. .context = NULL, \
  115. .trigger = (trigger_key), \
  116. .replacement = (replacement_key), \
  117. .enabled = NULL \
  118. })
  119. // clang-format on