Skip to content

Commit afd7619

Browse files
dsaedtlerRytoEX
authored andcommitted
frontend: Add OBS::Canvas class
1 parent 47360a9 commit afd7619

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

frontend/cmake/ui-utility.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ target_sources(
3333
utility/MultitrackVideoOutput.hpp
3434
utility/obf.c
3535
utility/obf.h
36+
utility/OBSCanvas.cpp
37+
utility/OBSCanvas.hpp
3638
utility/OBSEventFilter.hpp
3739
utility/OBSProxyStyle.cpp
3840
utility/OBSProxyStyle.hpp

frontend/utility/OBSCanvas.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/******************************************************************************
2+
Copyright (C) 2025 by Dennis Sädtler <[email protected]>
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+
18+
#include "OBSCanvas.hpp"
19+
20+
#include <utility>
21+
22+
namespace OBS {
23+
24+
Canvas::Canvas(obs_canvas_t *canvas) : canvas(canvas) {}
25+
26+
Canvas::Canvas(Canvas &&other) noexcept
27+
{
28+
canvas = std::exchange(other.canvas, nullptr);
29+
}
30+
31+
Canvas::~Canvas() noexcept
32+
{
33+
if (!canvas)
34+
return;
35+
36+
obs_canvas_remove(canvas);
37+
obs_canvas_release(canvas);
38+
canvas = nullptr;
39+
}
40+
41+
Canvas &Canvas::operator=(Canvas &&other) noexcept
42+
{
43+
canvas = std::exchange(other.canvas, canvas);
44+
45+
return *this;
46+
}
47+
48+
std::optional<OBSDataAutoRelease> Canvas::Save() const
49+
{
50+
if (!canvas)
51+
return std::nullopt;
52+
if (obs_data_t *saved = obs_save_canvas(canvas))
53+
return saved;
54+
55+
return std::nullopt;
56+
}
57+
58+
std::optional<Canvas> Canvas::Load(obs_data_t *data)
59+
{
60+
if (OBSDataAutoRelease canvas_data = obs_data_get_obj(data, "info")) {
61+
if (obs_canvas_t *canvas = obs_load_canvas(canvas_data)) {
62+
return canvas;
63+
}
64+
}
65+
66+
return std::nullopt;
67+
}
68+
69+
std::vector<Canvas> Canvas::LoadCanvases(obs_data_array_t *canvases)
70+
{
71+
auto cb = [](obs_data_t *data, void *param) -> void {
72+
auto vec = static_cast<std::vector<Canvas> *>(param);
73+
if (auto canvas = Canvas::Load(data))
74+
vec->emplace_back(std::move(*canvas));
75+
};
76+
77+
std::vector<Canvas> ret;
78+
obs_data_array_enum(canvases, cb, &ret);
79+
80+
return ret;
81+
}
82+
83+
OBSDataArrayAutoRelease Canvas::SaveCanvases(const std::vector<Canvas> &canvases)
84+
{
85+
OBSDataArrayAutoRelease savedCanvases = obs_data_array_create();
86+
87+
for (auto &canvas : canvases) {
88+
auto canvas_data = canvas.Save();
89+
if (!canvas_data)
90+
continue;
91+
92+
OBSDataAutoRelease data = obs_data_create();
93+
obs_data_set_obj(data, "info", *canvas_data);
94+
obs_data_array_push_back(savedCanvases, data);
95+
}
96+
97+
return savedCanvases;
98+
}
99+
100+
} // namespace OBS

frontend/utility/OBSCanvas.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/******************************************************************************
2+
Copyright (C) 2025 by Dennis Sädtler <[email protected]>
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+
18+
#pragma once
19+
20+
#include <optional>
21+
#include <vector>
22+
23+
#include "obs.h"
24+
#include "obs.hpp"
25+
26+
namespace OBS {
27+
class Canvas {
28+
29+
public:
30+
Canvas(obs_canvas_t *canvas);
31+
Canvas(Canvas &&other) noexcept;
32+
33+
~Canvas() noexcept;
34+
35+
// No default or copy/move constructors
36+
Canvas() = delete;
37+
Canvas(Canvas &other) = delete;
38+
39+
Canvas &operator=(Canvas &&other) noexcept;
40+
41+
operator obs_canvas_t *() const { return canvas; }
42+
43+
[[nodiscard]] std::optional<OBSDataAutoRelease> Save() const;
44+
static std::optional<Canvas> Load(obs_data_t *data);
45+
static std::vector<Canvas> LoadCanvases(obs_data_array_t *canvases);
46+
static OBSDataArrayAutoRelease SaveCanvases(const std::vector<Canvas> &canvases);
47+
48+
private:
49+
obs_canvas_t *canvas = nullptr;
50+
};
51+
} // namespace OBS

0 commit comments

Comments
 (0)