logo

qmk_firmware

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

connection.c (3451B)


  1. // Copyright 2025 QMK
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "connection.h"
  4. #include "eeconfig.h"
  5. #include "usb_util.h"
  6. #include "util.h"
  7. #ifdef BLUETOOTH_ENABLE
  8. # include "bluetooth.h"
  9. #endif
  10. // ======== DEPRECATED DEFINES - DO NOT USE ========
  11. #ifdef OUTPUT_DEFAULT
  12. # undef CONNECTION_HOST_DEFAULT
  13. # define CONNECTION_HOST_DEFAULT OUTPUT_DEFAULT
  14. #endif
  15. __attribute__((weak)) void set_output_user(uint8_t output) {}
  16. // ========
  17. #define CONNECTION_HOST_INVALID 0xFF
  18. #ifndef CONNECTION_HOST_DEFAULT
  19. # define CONNECTION_HOST_DEFAULT CONNECTION_HOST_AUTO
  20. #endif
  21. static const connection_host_t host_candidates[] = {
  22. CONNECTION_HOST_AUTO,
  23. CONNECTION_HOST_USB,
  24. #ifdef BLUETOOTH_ENABLE
  25. CONNECTION_HOST_BLUETOOTH,
  26. #endif
  27. #if 0
  28. CONNECTION_HOST_2P4GHZ,
  29. #endif
  30. };
  31. #define HOST_CANDIDATES_COUNT ARRAY_SIZE(host_candidates)
  32. static connection_config_t config = {.desired_host = CONNECTION_HOST_INVALID};
  33. void eeconfig_update_connection_default(void) {
  34. config.desired_host = CONNECTION_HOST_DEFAULT;
  35. eeconfig_update_connection(&config);
  36. }
  37. void connection_init(void) {
  38. eeconfig_read_connection(&config);
  39. if (config.desired_host == CONNECTION_HOST_INVALID) {
  40. eeconfig_update_connection_default();
  41. }
  42. }
  43. __attribute__((weak)) void connection_host_changed_user(connection_host_t host) {}
  44. __attribute__((weak)) void connection_host_changed_kb(connection_host_t host) {}
  45. static void handle_host_changed(void) {
  46. connection_host_changed_user(config.desired_host);
  47. connection_host_changed_kb(config.desired_host);
  48. // TODO: Remove deprecated callback
  49. set_output_user(config.desired_host);
  50. }
  51. void connection_set_host_noeeprom(connection_host_t host) {
  52. if (config.desired_host == host) {
  53. return;
  54. }
  55. config.desired_host = host;
  56. handle_host_changed();
  57. }
  58. void connection_set_host(connection_host_t host) {
  59. connection_set_host_noeeprom(host);
  60. eeconfig_update_connection(&config);
  61. }
  62. void connection_next_host_noeeprom(void) {
  63. uint8_t next = 0;
  64. for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) {
  65. if (host_candidates[i] == config.desired_host) {
  66. next = i == HOST_CANDIDATES_COUNT - 1 ? 0 : i + 1;
  67. break;
  68. }
  69. }
  70. connection_set_host_noeeprom(host_candidates[next]);
  71. }
  72. void connection_next_host(void) {
  73. connection_next_host_noeeprom();
  74. eeconfig_update_connection(&config);
  75. }
  76. void connection_prev_host_noeeprom(void) {
  77. uint8_t next = 0;
  78. for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) {
  79. if (host_candidates[i] == config.desired_host) {
  80. next = i == 0 ? HOST_CANDIDATES_COUNT - 1 : i - 1;
  81. break;
  82. }
  83. }
  84. connection_set_host_noeeprom(host_candidates[next]);
  85. }
  86. void connection_prev_host(void) {
  87. connection_prev_host_noeeprom();
  88. eeconfig_update_connection(&config);
  89. }
  90. connection_host_t connection_get_host_raw(void) {
  91. return config.desired_host;
  92. }
  93. connection_host_t connection_auto_detect_host(void) {
  94. if (usb_connected_state()) {
  95. return CONNECTION_HOST_USB;
  96. }
  97. #ifdef BLUETOOTH_ENABLE
  98. if (bluetooth_is_connected()) {
  99. return CONNECTION_HOST_BLUETOOTH;
  100. }
  101. #endif
  102. return CONNECTION_HOST_NONE;
  103. }
  104. connection_host_t connection_get_host(void) {
  105. if (config.desired_host == CONNECTION_HOST_AUTO) {
  106. return connection_auto_detect_host();
  107. }
  108. return config.desired_host;
  109. }