2017-09-07 23:27:21 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <pulse/pulseaudio.h>
|
2022-03-14 17:11:46 -04:00
|
|
|
|
2022-05-04 02:58:53 -04:00
|
|
|
#include <atomic>
|
2017-09-07 23:27:21 -04:00
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "errors.hpp"
|
2022-03-14 17:11:46 -04:00
|
|
|
#include "settings.hpp"
|
2017-09-07 23:27:21 -04:00
|
|
|
#include "utils/math.hpp"
|
|
|
|
// fwd
|
|
|
|
struct pa_context;
|
|
|
|
struct pa_threaded_mainloop;
|
|
|
|
struct pa_cvolume;
|
|
|
|
typedef struct pa_context pa_context;
|
|
|
|
typedef struct pa_threaded_mainloop pa_threaded_mainloop;
|
|
|
|
|
|
|
|
POLYBAR_NS
|
2017-10-29 01:50:51 -04:00
|
|
|
class logger;
|
2017-09-07 23:27:21 -04:00
|
|
|
|
|
|
|
DEFINE_ERROR(pulseaudio_error);
|
|
|
|
|
|
|
|
class pulseaudio {
|
|
|
|
// events to add to our queue
|
2018-04-14 20:04:48 -04:00
|
|
|
enum class evtype { NEW = 0, CHANGE, REMOVE, SERVER };
|
2017-09-07 23:27:21 -04:00
|
|
|
using queue = std::queue<evtype>;
|
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
public:
|
|
|
|
explicit pulseaudio(const logger& logger, string&& sink_name, bool m_max_volume);
|
|
|
|
~pulseaudio();
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
pulseaudio(const pulseaudio& o) = delete;
|
|
|
|
pulseaudio& operator=(const pulseaudio& o) = delete;
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
const string& get_name();
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
bool wait();
|
|
|
|
int process_events();
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
int get_volume();
|
|
|
|
double get_decibels();
|
|
|
|
void set_volume(float percentage);
|
|
|
|
void inc_volume(int delta_perc);
|
|
|
|
void set_mute(bool mode);
|
|
|
|
void toggle_mute();
|
|
|
|
bool is_muted();
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
private:
|
|
|
|
void update_volume(pa_operation* o);
|
|
|
|
static void check_mute_callback(pa_context* context, const pa_sink_info* info, int eol, void* userdata);
|
|
|
|
static void get_sink_volume_callback(pa_context* context, const pa_sink_info* info, int is_last, void* userdata);
|
|
|
|
static void subscribe_callback(pa_context* context, pa_subscription_event_type_t t, uint32_t idx, void* userdata);
|
|
|
|
static void simple_callback(pa_context* context, int success, void* userdata);
|
|
|
|
static void sink_info_callback(pa_context* context, const pa_sink_info* info, int eol, void* userdata);
|
|
|
|
static void context_state_callback(pa_context* context, void* userdata);
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
inline void wait_loop(pa_operation* op, pa_threaded_mainloop* loop);
|
2017-09-16 00:10:25 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
const logger& m_log;
|
2017-10-29 01:50:51 -04:00
|
|
|
|
2022-05-04 02:58:53 -04:00
|
|
|
/**
|
|
|
|
* Has context_state_callback signalled the mainloop during connection.
|
|
|
|
*/
|
|
|
|
std::atomic_bool m_state_callback_signal{false};
|
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
// used for temporary callback results
|
|
|
|
int success{0};
|
|
|
|
pa_cvolume cv{};
|
|
|
|
bool muted{false};
|
|
|
|
// default sink name
|
|
|
|
static constexpr auto DEFAULT_SINK = "@DEFAULT_SINK@";
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
pa_context* m_context{nullptr};
|
|
|
|
pa_threaded_mainloop* m_mainloop{nullptr};
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
queue m_events;
|
2017-09-07 23:27:21 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
// specified sink name
|
|
|
|
string spec_s_name;
|
|
|
|
string s_name;
|
|
|
|
uint32_t m_index{0};
|
2018-04-09 19:13:04 -04:00
|
|
|
|
2022-03-14 17:11:46 -04:00
|
|
|
pa_volume_t m_max_volume{PA_VOLUME_UI_MAX};
|
2017-09-07 23:27:21 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|