Skip to content

Commit

Permalink
[Packages] AudioControl: Add AppVM names support
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Bazulin <[email protected]>
  • Loading branch information
baz2142 committed Oct 3, 2024
1 parent b02f0c2 commit f49d11f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 6 deletions.
26 changes: 22 additions & 4 deletions packages/ghaf-audio-control/src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,24 @@ using namespace ghaf::AudioControl;
namespace
{

int GtkClient(std::string pulseAudioServerAddress)
std::vector<std::string> GetAppVmsList(const std::string& appVms)
{
std::vector<std::string> result;

std::istringstream iss(appVms);
std::string buf;

while (getline(iss, buf, ','))
result.push_back(buf);

return result;
}

int GtkClient(const std::string& pulseAudioServerAddress, const std::string& appVms)
{
auto app = Gtk::Application::create();

AudioControl audioControl{std::make_unique<Backend::PulseAudio::AudioControlBackend>(std::move(pulseAudioServerAddress))};
AudioControl audioControl{std::make_unique<Backend::PulseAudio::AudioControlBackend>(std::move(pulseAudioServerAddress)), GetAppVmsList(appVms)};

Gtk::ApplicationWindow window;
window.add(audioControl);
Expand All @@ -37,12 +50,17 @@ int main(int argc, char** argv)
{
Glib::ustring pulseServerAddress;
Glib::OptionEntry pulseServerOption;

pulseServerOption.set_long_name("pulseaudio_server");
pulseServerOption.set_description("PulseAudio server address");

Glib::ustring appVms;
Glib::OptionEntry appVmsOption;
appVmsOption.set_long_name("app_vms");
appVmsOption.set_description("AppVMs list");

Glib::OptionGroup options("Main", "Main");
options.add_entry(pulseServerOption, pulseServerAddress);
options.add_entry(appVmsOption, appVms);

Glib::OptionContext context("Application Options");
context.set_main_group(options);
Expand All @@ -60,5 +78,5 @@ int main(int argc, char** argv)
return 1;
}

return GtkClient(pulseServerAddress);
return GtkClient(pulseServerAddress, appVms);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AppList final : public Gtk::Box
public:
AppList();

void addVm(std::string appVmName);
void addDevice(IAudioControlBackend::ISinkInput::Ptr device);

void removeAllApps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ghaf::AudioControl
class AudioControl final : public Gtk::Box
{
public:
explicit AudioControl(std::unique_ptr<IAudioControlBackend> backend);
AudioControl(std::unique_ptr<IAudioControlBackend> backend, const std::vector<std::string> &appVmsList);
~AudioControl() override = default;

AudioControl(AudioControl&) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class GeneralDeviceImpl final
[[nodiscard]] pa_volume_t getPulseVolume() const;
[[nodiscard]] pa_cvolume getPulseChannelVolume() const noexcept;

[[nodiscard]] std::optional<std::string> getAppVmName() const noexcept;

[[nodiscard]] std::string getName() const;
[[nodiscard]] std::string getDescription() const;

Expand Down Expand Up @@ -78,6 +80,9 @@ class GeneralDeviceImpl final

bool m_isDeleted = false;
bool m_isEnabled = false;

std::optional<std::string> m_appVmName;

std::string m_name;
std::string m_description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class SinkInput final : public IAudioControlBackend::ISinkInput
return m_device.getDescription();
}

std::optional<std::string> getAppVmName() const
{
return m_device.getAppVmName();
}

void update(const pa_sink_input_info& info);

void markDeleted();
Expand Down
15 changes: 15 additions & 0 deletions packages/ghaf-audio-control/src/lib/src/AppList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <GhafAudioControl/AppList.hpp>
#include <GhafAudioControl/Backends/PulseAudio/SinkInput.hpp>
#include <GhafAudioControl/widgets/AppVmWidget.hpp>

#include <GhafAudioControl/utils/Logger.hpp>
Expand Down Expand Up @@ -36,6 +37,12 @@ std::optional<size_t> GetIndexByAppId(const Glib::RefPtr<Gio::ListStore<AppVmMod

std::string GetAppNameFromSinkInput(const IAudioControlBackend::ISinkInput::Ptr& device)
{
if (auto sinkInput = std::dynamic_pointer_cast<Backend::PulseAudio::SinkInput>(device))
{
if (const auto appVmName = sinkInput->getAppVmName())
return *appVmName;
}

return "Other";
}

Expand Down Expand Up @@ -70,6 +77,14 @@ AppList::AppList()
pack_start(m_listBox, Gtk::PACK_EXPAND_WIDGET);
}

void AppList::addVm(std::string appVmName)
{
if (const auto index = GetIndexByAppId(m_appsModel, appVmName))
return;

m_appsModel->append(AppVmModel::create(std::move(appVmName)));
}

void AppList::addDevice(IAudioControlBackend::ISinkInput::Ptr device)
{
const std::string appName = GetAppNameFromSinkInput(device);
Expand Down
5 changes: 4 additions & 1 deletion packages/ghaf-audio-control/src/lib/src/AudioControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ void OnPulseDeviceChanged(IAudioControlBackend::EventType eventType, IndexT inde
}
}

AudioControl::AudioControl(std::unique_ptr<IAudioControlBackend> backend)
AudioControl::AudioControl(std::unique_ptr<IAudioControlBackend> backend, const std::vector<std::string>& appVmsList)
: Gtk::Box(Gtk::ORIENTATION_HORIZONTAL)
, m_audioControl(std::move(backend))
{
for (const auto& appVm : appVmsList)
m_appList.addVm(appVm);

init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace ghaf::AudioControl::Backend::PulseAudio
{

constexpr auto PropertyAppVmName = "application.process.host";

GeneralDeviceImpl::GeneralDeviceImpl(const pa_sink_info& info, pa_context& context)
: m_index(info.index)
, m_cardIndex(info.card)
Expand Down Expand Up @@ -39,6 +41,7 @@ GeneralDeviceImpl::GeneralDeviceImpl(const pa_source_info& info, pa_context& con
GeneralDeviceImpl::GeneralDeviceImpl(const pa_sink_input_info& info, pa_context& context)
: m_index(info.index)
, m_cardIndex(0)
, m_appVmName(pa_proplist_gets(info.proplist, PropertyAppVmName))
, m_name(info.name)
, m_context(context)
, m_channel_map(info.channel_map)
Expand Down Expand Up @@ -99,6 +102,12 @@ GeneralDeviceImpl::GeneralDeviceImpl(const pa_source_output_info& info, pa_conte
return m_volume;
}

std::optional<std::string> GeneralDeviceImpl::getAppVmName() const noexcept
{
const std::lock_guard l{m_mutex};
return m_appVmName;
}

[[nodiscard]] std::string GeneralDeviceImpl::getName() const
{
const std::lock_guard l{m_mutex};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ AppVmWidget::AppVmWidget(Glib::RefPtr<AppVmModel> model)
m_revealer.set_transition_duration(RevealerAnimationTimeMs);
m_revealer.set_reveal_child();

m_appNameButton.set_halign(Gtk::Align::ALIGN_START);

add(m_appNameButton);
add(m_revealer);

Expand Down

0 comments on commit f49d11f

Please sign in to comment.