polybar/include/adapters/mpd.hpp

181 lines
3.8 KiB
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include <mpd/client.h>
#include <stdlib.h>
#include <chrono>
2017-01-13 12:02:51 +00:00
#include <csignal>
2016-06-15 03:32:35 +00:00
#include "common.hpp"
2016-11-25 12:55:15 +00:00
#include "errors.hpp"
2016-06-15 03:32:35 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
2016-11-20 22:04:31 +00:00
// fwd
class logger;
namespace chrono = std::chrono;
2016-06-15 03:32:35 +00:00
namespace mpd {
2017-01-13 12:02:51 +00:00
extern sig_atomic_t g_connection_closed;
2016-06-15 03:32:35 +00:00
DEFINE_ERROR(mpd_exception);
DEFINE_CHILD_ERROR(client_error, mpd_exception);
DEFINE_CHILD_ERROR(server_error, mpd_exception);
2016-11-02 19:22:45 +00:00
void check_connection(mpd_connection* conn);
void check_errors(mpd_connection* conn);
// types details {{{
enum class connection_state { NONE = 0, CONNECTED, DISCONNECTED };
2016-11-02 19:22:45 +00:00
enum class mpdstate {
UNKNOWN = 1 << 0,
STOPPED = 1 << 1,
PLAYING = 1 << 2,
PAUSED = 1 << 4,
};
2016-06-15 03:32:35 +00:00
namespace details {
struct mpd_connection_deleter {
2016-11-02 19:22:45 +00:00
void operator()(mpd_connection* conn);
2016-06-15 03:32:35 +00:00
};
struct mpd_status_deleter {
2016-11-02 19:22:45 +00:00
void operator()(mpd_status* status);
2016-06-15 03:32:35 +00:00
};
struct mpd_song_deleter {
2016-11-02 19:22:45 +00:00
void operator()(mpd_song* song);
2016-06-15 03:32:35 +00:00
};
}
2016-11-02 19:22:45 +00:00
using mpd_connection_t = unique_ptr<mpd_connection, details::mpd_connection_deleter>;
using mpd_status_t = unique_ptr<mpd_status, details::mpd_status_deleter>;
using mpd_song_t = unique_ptr<mpd_song, details::mpd_song_deleter>;
2016-06-15 03:32:35 +00:00
// }}}
2016-11-02 19:22:45 +00:00
// class : mpdsong {{{
2016-06-15 03:32:35 +00:00
class mpdsong {
public:
2016-11-02 19:22:45 +00:00
explicit mpdsong(mpd_song_t&& song) : m_song(forward<decltype(song)>(song)) {}
operator bool();
string get_artist();
string get_album();
string get_title();
2016-12-04 18:33:04 +00:00
string get_date();
2016-11-02 19:22:45 +00:00
unsigned get_duration();
2016-06-15 03:32:35 +00:00
private:
2016-11-02 19:22:45 +00:00
mpd_song_t m_song;
2016-06-15 03:32:35 +00:00
};
// }}}
2016-11-02 19:22:45 +00:00
// class : mpdconnection {{{
2016-06-15 03:32:35 +00:00
class mpdstatus;
class mpdconnection {
public:
2016-11-20 22:04:31 +00:00
explicit mpdconnection(
const logger& logger, string host, unsigned int port = 6600, string password = "", unsigned int timeout = 15);
2017-01-13 12:02:51 +00:00
~mpdconnection();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
void connect();
void disconnect();
bool connected();
bool retry_connection(int interval = 1);
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
int get_fd();
void idle();
int noidle();
unique_ptr<mpdstatus> get_status();
unique_ptr<mpdstatus> get_status_safe();
unique_ptr<mpdsong> get_song();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
void play();
void pause(bool state);
void toggle();
void stop();
void prev();
void next();
void seek(int songid, int pos);
void set_repeat(bool mode);
void set_random(bool mode);
void set_single(bool mode);
operator mpd_connection_t::element_type*();
protected:
void check_prerequisites();
void check_prerequisites_commands_list();
2016-06-15 03:32:35 +00:00
private:
const logger& m_log;
2016-12-15 02:30:41 +00:00
mpd_connection_t m_connection{};
2016-06-15 03:32:35 +00:00
2017-01-13 12:02:51 +00:00
struct sigaction m_signal_action {};
2016-06-15 03:32:35 +00:00
bool m_listactive = false;
bool m_idle = false;
int m_fd = -1;
string m_host;
unsigned int m_port;
string m_password;
unsigned int m_timeout;
};
// }}}
2016-11-02 19:22:45 +00:00
// class : mpdstatus {{{
2016-06-15 03:32:35 +00:00
class mpdstatus {
public:
2016-11-02 19:22:45 +00:00
explicit mpdstatus(mpdconnection* conn, bool autoupdate = true);
void fetch_data(mpdconnection* conn);
void update(int event, mpdconnection* connection);
void update_timer();
bool random() const;
bool repeat() const;
bool single() const;
bool match_state(mpdstate state) const;
int get_songid() const;
int get_queuelen() const;
2016-11-02 19:22:45 +00:00
unsigned get_total_time() const;
unsigned get_elapsed_time() const;
unsigned get_elapsed_percentage();
string get_formatted_elapsed();
string get_formatted_total();
int get_seek_position(int percentage);
2016-06-15 03:32:35 +00:00
private:
2016-12-15 02:30:41 +00:00
mpd_status_t m_status{};
unique_ptr<mpdsong> m_song{};
mpdstate m_state{mpdstate::UNKNOWN};
chrono::system_clock::time_point m_updated_at{};
2016-06-15 03:32:35 +00:00
2016-12-15 02:30:41 +00:00
bool m_random{false};
bool m_repeat{false};
bool m_single{false};
2016-06-15 03:32:35 +00:00
2016-12-15 02:30:41 +00:00
int m_songid{0};
int m_queuelen{0};
2016-06-15 03:32:35 +00:00
2016-12-15 02:30:41 +00:00
unsigned long m_total_time{0UL};
unsigned long m_elapsed_time{0UL};
unsigned long m_elapsed_time_ms{0UL};
2016-06-15 03:32:35 +00:00
};
// }}}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END