Skip to content

Commit

Permalink
feat: support default speaker switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Apr 10, 2024
1 parent 961e0e9 commit e1ccea1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ include("cmake/cpm.cmake")

CPMFindPackage(
NAME rohrkabel
VERSION 3.1
VERSION 4.1
GIT_REPOSITORY "https://github.com/Curve/rohrkabel"
)

Expand All @@ -105,7 +105,7 @@ CPMFindPackage(

CPMFindPackage(
NAME glaze
VERSION 2.4.0
VERSION 2.4.5
GIT_REPOSITORY "https://github.com/stephenberry/glaze"
)

Expand Down
17 changes: 16 additions & 1 deletion private/patchbay.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
#include <map>
#include <thread>
#include <atomic>
#include <string_view>

#include <rohrkabel/global.hpp>

#include <rohrkabel/node/node.hpp>
#include <rohrkabel/port/port.hpp>
#include <rohrkabel/link/link.hpp>

#include <rohrkabel/metadata/events.hpp>
#include <rohrkabel/metadata/metadata.hpp>

#include <rohrkabel/registry/events.hpp>
#include <rohrkabel/registry/registry.hpp>

namespace vencord
Expand Down Expand Up @@ -40,8 +46,11 @@ namespace vencord
link_options options;

private:
std::unique_ptr<pw::node> virt_mic;
std::optional<vencord::speaker> speaker;
std::unique_ptr<pw::metadata_listener> listener;

private:
std::unique_ptr<pw::node> virt_mic;
std::multimap<std::uint32_t, pw::link> created;

private:
Expand All @@ -68,8 +77,14 @@ namespace vencord
private:
void create_mic();
void cleanup(bool);

private:
void relink_all();
void relink(std::uint32_t);

private:
void meta_update(std::string_view, pw::metadata_property);

private:
template <typename T>
void bind(const pw::global &);
Expand Down
68 changes: 55 additions & 13 deletions src/patchbay.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ namespace vencord
virt_mic.reset();
}

void patchbay::impl::relink_all()
{
cleanup(false);

for (const auto &[id, info] : nodes)
{
on_node(id);
}

for (const auto &[id, info] : links)
{
on_link(id);
}

logger::get()->debug("[patchbay] (relink_all) relinked all targets");
}

void patchbay::impl::relink(std::uint32_t id)
{
created.erase(id);
Expand Down Expand Up @@ -177,6 +194,29 @@ namespace vencord
}
}

void patchbay::impl::meta_update(std::string_view key, pw::metadata_property prop)
{
logger::get()->debug(R"([patchbay] (meta_update) metadata property changed: "{}" (value: "{}"))", key,
prop.value);

if (key != "default.audio.sink")
{
return;
}

auto parsed = glz::read_json<pw_metadata_name>(prop.value);

if (!parsed.has_value())
{
logger::get()->warn("[patchbay] (meta_update) failed to parse speaker");
return;
}

speaker.emplace(parsed->name);

relink_all();
}

template <>
void patchbay::impl::add_global<pw::node>(pw::node &node)
{
Expand Down Expand Up @@ -265,13 +305,15 @@ namespace vencord
auto props = data.properties();
const auto name = data.props()["metadata.name"];

logger::get()->trace(R"([patchbay] (add_global) new metadata: {} (name: "{}"))", data.id(), name);

if (name != "default")
{
return;
}

metadata = std::make_unique<pw::metadata>(std::move(data));
logger::get()->info("[patchbay] (add_global) found metadata: {}", metadata->id());
logger::get()->info("[patchbay] (add_global) found default metadata: {}", metadata->id());

auto parsed = glz::read_json<pw_metadata_name>(props["default.audio.sink"].value);

Expand All @@ -281,10 +323,18 @@ namespace vencord
return;
}

logger::get()->debug("[patchbay] (add_global) speakers name: \"{}\"", parsed->name);
listener = std::make_unique<pw::metadata_listener>(metadata->listen());
logger::get()->debug("[patchbay] (add_global) speaker name: \"{}\"", parsed->name);

speaker.emplace(parsed->name);

listener->on<pw::metadata_event::property>(
[this](auto... args)
{
meta_update(args...);
return 0;
});

for (const auto &[id, info] : nodes)
{
on_node(id);
Expand All @@ -309,6 +359,8 @@ namespace vencord
template <>
void patchbay::impl::add_global<const pw::global>(const pw::global &global)
{
logger::get()->trace(R"([patchbay] (add_global) new global: {} (type: "{}"))", global.id, global.type);

if (global.type == pw::node::type)
{
bind<pw::node>(global);
Expand Down Expand Up @@ -461,19 +513,9 @@ namespace vencord
create_mic();
}

cleanup(false);

options = std::move(req);

for (const auto &[id, info] : nodes)
{
on_node(id);
}

for (const auto &[id, info] : links)
{
on_link(id);
}
relink_all();
}

template <>
Expand Down

0 comments on commit e1ccea1

Please sign in to comment.