obs-service.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  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. #include "obs-internal.h"
  15. static inline const struct obs_service_info *find_service(const char *id)
  16. {
  17. size_t i;
  18. for (i = 0; i < obs->service_types.num; i++)
  19. if (strcmp(obs->service_types.array[i].id, id) == 0)
  20. return obs->service_types.array+i;
  21. return NULL;
  22. }
  23. const char *obs_service_getdisplayname(const char *id, const char *locale)
  24. {
  25. const struct obs_service_info *info = find_service(id);
  26. return (info != NULL) ? info->getname(locale) : NULL;
  27. }
  28. obs_service_t obs_service_create(const char *id, const char *name,
  29. obs_data_t settings)
  30. {
  31. const struct obs_service_info *info = find_service(id);
  32. struct obs_service *service;
  33. if (!info) {
  34. blog(LOG_ERROR, "Service '%s' not found", id);
  35. return NULL;
  36. }
  37. service = bzalloc(sizeof(struct obs_service));
  38. if (!obs_context_data_init(&service->context, settings, name)) {
  39. bfree(service);
  40. return NULL;
  41. }
  42. service->info = *info;
  43. obs_context_data_insert(&service->context,
  44. &obs->data.services_mutex,
  45. &obs->data.first_service);
  46. return service;
  47. }
  48. void obs_service_destroy(obs_service_t service)
  49. {
  50. if (service) {
  51. obs_context_data_remove(&service->context);
  52. if (service->context.data)
  53. service->info.destroy(service->context.data);
  54. obs_context_data_free(&service->context);
  55. bfree(service);
  56. }
  57. }
  58. static inline obs_data_t get_defaults(const struct obs_service_info *info)
  59. {
  60. obs_data_t settings = obs_data_create();
  61. if (info->defaults)
  62. info->defaults(settings);
  63. return settings;
  64. }
  65. obs_data_t obs_service_defaults(const char *id)
  66. {
  67. const struct obs_service_info *info = find_service(id);
  68. return (info) ? get_defaults(info) : NULL;
  69. }
  70. obs_properties_t obs_get_service_properties(const char *id, const char *locale)
  71. {
  72. const struct obs_service_info *info = find_service(id);
  73. if (info && info->properties) {
  74. obs_data_t defaults = get_defaults(info);
  75. obs_properties_t properties;
  76. properties = info->properties(locale);
  77. obs_properties_apply_settings(properties, defaults);
  78. obs_data_release(defaults);
  79. return properties;
  80. }
  81. return NULL;
  82. }
  83. obs_properties_t obs_service_properties(obs_service_t service,
  84. const char *locale)
  85. {
  86. if (service && service->info.properties) {
  87. obs_properties_t props;
  88. props = service->info.properties(locale);
  89. obs_properties_apply_settings(props, service->context.settings);
  90. return props;
  91. }
  92. return NULL;
  93. }
  94. void obs_service_update(obs_service_t service, obs_data_t settings)
  95. {
  96. if (!service) return;
  97. obs_data_apply(service->context.settings, settings);
  98. if (service->info.update)
  99. service->info.update(service->context.data,
  100. service->context.settings);
  101. }
  102. obs_data_t obs_service_get_settings(obs_service_t service)
  103. {
  104. if (!service)
  105. return NULL;
  106. obs_data_addref(service->context.settings);
  107. return service->context.settings;
  108. }
  109. signal_handler_t obs_service_signalhandler(obs_service_t service)
  110. {
  111. return service ? service->context.signals : NULL;
  112. }
  113. proc_handler_t obs_service_prochandler(obs_service_t service)
  114. {
  115. return service ? service->context.procs : NULL;
  116. }
  117. const char *obs_service_get_url(obs_service_t service)
  118. {
  119. if (!service || !service->info.get_url) return NULL;
  120. return service->info.get_url(service->context.data);
  121. }
  122. const char *obs_service_get_key(obs_service_t service)
  123. {
  124. if (!service || !service->info.get_key) return NULL;
  125. return service->info.get_key(service->context.data);
  126. }