polybar/include/adapters/alsa.hpp

84 lines
1.6 KiB
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include <stdio.h>
#include <functional>
#include <string>
#include <alsa/asoundlib.h>
#include "common.hpp"
#include "config.hpp"
#include "utils/threading.hpp"
2016-06-15 03:32:35 +00:00
LEMONBUDDY_NS
DEFINE_ERROR(alsa_exception);
DEFINE_CHILD_ERROR(alsa_ctl_interface_error, alsa_exception);
DEFINE_CHILD_ERROR(alsa_mixer_error, alsa_exception);
// class definition : alsa_ctl_interface {{{
template <typename T>
2016-06-15 03:32:35 +00:00
void throw_exception(string&& message, int error_code) {
const char* snd_error = snd_strerror(error_code);
if (snd_error != nullptr)
message += ": " + string{snd_error};
2016-06-15 03:32:35 +00:00
throw T(message.c_str());
}
class alsa_ctl_interface {
public:
2016-11-02 19:22:45 +00:00
explicit alsa_ctl_interface(int numid);
~alsa_ctl_interface();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
int get_numid();
bool wait(int timeout = -1);
bool test_device_plugged();
void process_events();
2016-06-15 03:32:35 +00:00
private:
int m_numid = 0;
threading_util::spin_lock m_lock;
2016-06-15 03:32:35 +00:00
snd_hctl_t* m_hctl = nullptr;
snd_hctl_elem_t* m_elem = nullptr;
snd_ctl_t* m_ctl = nullptr;
snd_ctl_elem_info_t* m_info = nullptr;
snd_ctl_elem_value_t* m_value = nullptr;
snd_ctl_elem_id_t* m_id = nullptr;
};
// }}}
// class definition : alsa_mixer {{{
class alsa_mixer {
public:
2016-11-02 19:22:45 +00:00
explicit alsa_mixer(string mixer_control_name);
~alsa_mixer();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
string get_name();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
bool wait(int timeout = -1);
int process_events();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
int get_volume();
void set_volume(float percentage);
void set_mute(bool mode);
void toggle_mute();
bool is_muted();
2016-06-15 03:32:35 +00:00
private:
string m_name;
threading_util::spin_lock m_lock;
2016-06-15 03:32:35 +00:00
snd_mixer_t* m_hardwaremixer = nullptr;
snd_mixer_elem_t* m_mixerelement = nullptr;
};
// }}}
LEMONBUDDY_NS_END