OBSCanvas.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /******************************************************************************
  2. Copyright (C) 2025 by Dennis Sädtler <[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 "OBSCanvas.hpp"
  15. #include <utility>
  16. namespace OBS {
  17. Canvas::Canvas(obs_canvas_t *canvas) : canvas(canvas) {}
  18. Canvas::Canvas(Canvas &&other) noexcept
  19. {
  20. canvas = std::exchange(other.canvas, nullptr);
  21. }
  22. Canvas::~Canvas() noexcept
  23. {
  24. if (!canvas)
  25. return;
  26. obs_canvas_remove(canvas);
  27. obs_canvas_release(canvas);
  28. canvas = nullptr;
  29. }
  30. Canvas &Canvas::operator=(Canvas &&other) noexcept
  31. {
  32. canvas = std::exchange(other.canvas, canvas);
  33. return *this;
  34. }
  35. std::optional<OBSDataAutoRelease> Canvas::Save() const
  36. {
  37. if (!canvas)
  38. return std::nullopt;
  39. if (obs_data_t *saved = obs_save_canvas(canvas))
  40. return saved;
  41. return std::nullopt;
  42. }
  43. std::unique_ptr<Canvas> Canvas::Load(obs_data_t *data)
  44. {
  45. if (OBSDataAutoRelease canvas_data = obs_data_get_obj(data, "info")) {
  46. if (obs_canvas_t *canvas = obs_load_canvas(canvas_data)) {
  47. return std::make_unique<Canvas>(canvas);
  48. }
  49. }
  50. return nullptr;
  51. }
  52. std::vector<Canvas> Canvas::LoadCanvases(obs_data_array_t *canvases)
  53. {
  54. auto cb = [](obs_data_t *data, void *param) -> void {
  55. auto vec = static_cast<std::vector<Canvas> *>(param);
  56. if (auto canvas = Canvas::Load(data))
  57. vec->emplace_back(std::move(*canvas));
  58. };
  59. std::vector<Canvas> ret;
  60. obs_data_array_enum(canvases, cb, &ret);
  61. return ret;
  62. }
  63. OBSDataArrayAutoRelease Canvas::SaveCanvases(const std::vector<Canvas> &canvases)
  64. {
  65. OBSDataArrayAutoRelease savedCanvases = obs_data_array_create();
  66. for (auto &canvas : canvases) {
  67. auto canvas_data = canvas.Save();
  68. if (!canvas_data)
  69. continue;
  70. OBSDataAutoRelease data = obs_data_create();
  71. obs_data_set_obj(data, "info", *canvas_data);
  72. obs_data_array_push_back(savedCanvases, data);
  73. }
  74. return savedCanvases;
  75. }
  76. } // namespace OBS