Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ using json = nlohmann::json;

constexpr std::string_view api_url {"https://api.spotify.com"};

Client::Client(token_t token_info, const Config& config)
Client::Client(token_t token_info, Config config)
: m_token_info(std::move(token_info))
, m_config(config)
, m_config(std::move(config))
{
}

Expand Down
2 changes: 1 addition & 1 deletion source/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using json = nlohmann::json;
class Client
{
public:
Client(token_t token_info, const Config& config);
Client(token_t token_info, Config config);

[[nodiscard]] cpr::Response api_request(std::string_view endpoint);
[[nodiscard]] cpr::Response put_api_request(std::string_view endpoint, const cpr::Payload& payload);
Expand Down
10 changes: 10 additions & 0 deletions source/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <filesystem>
Expand Down Expand Up @@ -26,9 +27,12 @@ constexpr std::string_view volume_increment_key = "volume_increment";
constexpr std::string_view volume_up_key = "volume_up";
constexpr std::string_view volume_down_key = "volume_down";
constexpr std::string_view batch_delay_key = "batch_delay_ms";
constexpr std::string_view poll_rate_key = "poll_rate_ms";

constexpr std::chrono::milliseconds default_batch_delay {100};
constexpr uint32_t default_volume_increment = 1;
constexpr std::chrono::milliseconds default_poll_rate {250};
constexpr std::chrono::milliseconds min_poll_rate {100};

Config::Config()
{
Expand Down Expand Up @@ -89,6 +93,7 @@ void Config::parse_config_file(const std::filesystem::path& path)

m_config[volume_increment_key] = default_volume_increment;
m_config[batch_delay_key] = default_batch_delay.count();
m_config[poll_rate_key] = default_poll_rate.count();

new_config_file << m_config;
new_config_file.close();
Expand Down Expand Up @@ -188,6 +193,11 @@ std::chrono::milliseconds Config::batch_delay() const
return std::chrono::milliseconds(m_config.value(batch_delay_key, default_batch_delay.count()));
}

std::chrono::milliseconds Config::poll_rate() const
{
return std::max(std::chrono::milliseconds(m_config.value(poll_rate_key, default_poll_rate.count())), min_poll_rate);
}

void Config::get_user_input(const std::string_view prompt, std::string& input, bool not_empty)
{
input.clear();
Expand Down
1 change: 1 addition & 0 deletions source/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Config
keycode get_volume_down() const;
volume volume_increment() const;
std::chrono::milliseconds batch_delay() const;
std::chrono::milliseconds poll_rate() const;

bool is_default_down() const;
bool is_default_up() const;
Expand Down
38 changes: 21 additions & 17 deletions source/VolumeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ VolumeController::VolumeController(const Config& config, Client& client)
, m_volume_up_keycode(config.is_default_up() ? VK_VOLUME_UP : config.get_volume_up())
, m_volume_down_keycode(config.is_default_down() ? VK_VOLUME_DOWN : config.get_volume_down())
, m_client_thread(std::thread(&VolumeController::set_volume_loop, this))
, m_update_current_volume_thread(std::jthread(&VolumeController::update_current_volume_loop, this))
{
}

Expand All @@ -43,15 +44,10 @@ void VolumeController::set_desktop_device()
m_desktop_device_id = desktop;
}

[[nodiscard]] volume VolumeController::get_volume() const
{
return m_volume;
}

void VolumeController::decrease_volume()
{
{
std::unique_lock const lock(m_volume_mutex);
std::lock_guard const lock(m_volume_mutex);
// Assume that the API call will succeed
m_volume = m_volume - m_config.volume_increment();
m_volume_queue.push(m_volume);
Expand All @@ -66,7 +62,7 @@ void VolumeController::decrease_volume()
void VolumeController::increase_volume()
{
{
std::unique_lock const lock(m_volume_mutex);
std::lock_guard const lock(m_volume_mutex);
// Assume that the API call will succeed
m_volume = m_volume + m_config.volume_increment();
m_volume_queue.push(m_volume);
Expand Down Expand Up @@ -117,16 +113,6 @@ void VolumeController::print_keys()
key_hooks::start_print_vkey();
}

[[nodiscard]] keycode VolumeController::volume_up_keycode() const
{
return m_volume_up_keycode;
}

[[nodiscard]] keycode VolumeController::volume_down_keycode() const
{
return m_volume_down_keycode;
}

void VolumeController::set_volume_loop()
{
while (true) {
Expand All @@ -141,4 +127,22 @@ void VolumeController::set_volume_loop()
}
}

void VolumeController::update_current_volume_loop()
{
while (true) {
{
std::lock_guard const lock(m_volume_mutex);
if (m_volume_queue.empty()) {
std::optional<volume> current_volume = m_client.get_current_playing_volume();
if (current_volume.has_value()) {
{
m_volume = current_volume.value();
}
}
}
}
std::this_thread::sleep_for(m_config.poll_rate());
}
}

} // namespace spotify_volume_controller
10 changes: 7 additions & 3 deletions source/VolumeController.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class VolumeController

void set_desktop_device();

[[nodiscard]] volume get_volume() const;
[[nodiscard]] keycode volume_up_keycode() const;
[[nodiscard]] keycode volume_down_keycode() const;
[[nodiscard]] volume get_volume() const { return m_volume; }
[[nodiscard]] keycode volume_up_keycode() const { return m_volume_up_keycode; }
[[nodiscard]] keycode volume_down_keycode() const { return m_volume_down_keycode; }
void decrease_volume();
void increase_volume();

Expand All @@ -63,14 +63,18 @@ class VolumeController
void set_volume(volume new_volume);
void set_volume_loop();
void set_volume_to_desktop_device_volume();
void update_current_volume_loop();
volume m_volume;
const Config m_config;
Client m_client;
const keycode m_volume_up_keycode;
const keycode m_volume_down_keycode;
std::optional<std::string> m_desktop_device_id;

std::thread m_client_thread;
std::jthread m_update_current_volume_thread;
std::jthread m_notify_timer_thread;

std::mutex m_volume_mutex;
std::condition_variable m_volume_cv;
std::queue<volume> m_volume_queue;
Expand Down
Loading