1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-12-02 14:05:27 -05:00
polybar/include/adapters/mpd.hpp

175 lines
3.7 KiB
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#include <mpd/client.h>
#include <stdlib.h>
#include <chrono>
#include "common.hpp"
2016-11-25 07:55:15 -05:00
#include "errors.hpp"
2016-06-14 23:32:35 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-06-14 23:32:35 -04:00
2016-11-20 17:04:31 -05:00
// fwd
class logger;
namespace chrono = std::chrono;
2016-06-14 23:32:35 -04:00
namespace mpd {
DEFINE_ERROR(mpd_exception);
DEFINE_CHILD_ERROR(client_error, mpd_exception);
DEFINE_CHILD_ERROR(server_error, mpd_exception);
2016-11-02 15:22:45 -04: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 15:22:45 -04:00
enum class mpdstate {
UNKNOWN = 1 << 0,
STOPPED = 1 << 1,
PLAYING = 1 << 2,
PAUSED = 1 << 4,
};
2016-06-14 23:32:35 -04:00
namespace details {
struct mpd_connection_deleter {
2016-11-02 15:22:45 -04:00
void operator()(mpd_connection* conn);
2016-06-14 23:32:35 -04:00
};
struct mpd_status_deleter {
2016-11-02 15:22:45 -04:00
void operator()(mpd_status* status);
2016-06-14 23:32:35 -04:00
};
struct mpd_song_deleter {
2016-11-02 15:22:45 -04:00
void operator()(mpd_song* song);
2016-06-14 23:32:35 -04:00
};
}
2016-11-02 15:22:45 -04: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-14 23:32:35 -04:00
// }}}
2016-11-02 15:22:45 -04:00
// class : mpdsong {{{
2016-06-14 23:32:35 -04:00
class mpdsong {
public:
2016-11-02 15:22:45 -04: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 13:33:04 -05:00
string get_date();
2016-11-02 15:22:45 -04:00
unsigned get_duration();
2016-06-14 23:32:35 -04:00
private:
2016-11-02 15:22:45 -04:00
mpd_song_t m_song;
2016-06-14 23:32:35 -04:00
};
// }}}
2016-11-02 15:22:45 -04:00
// class : mpdconnection {{{
2016-06-14 23:32:35 -04:00
class mpdstatus;
class mpdconnection {
public:
2016-11-20 17:04:31 -05:00
explicit mpdconnection(
const logger& logger, string host, unsigned int port = 6600, string password = "", unsigned int timeout = 15);
2016-06-14 23:32:35 -04:00
2016-11-02 15:22:45 -04:00
void connect();
void disconnect();
bool connected();
bool retry_connection(int interval = 1);
2016-06-14 23:32:35 -04:00
2016-11-02 15:22:45 -04: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-14 23:32:35 -04:00
2016-11-02 15:22:45 -04: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-14 23:32:35 -04:00
private:
const logger& m_log;
2016-11-02 15:22:45 -04:00
mpd_connection_t m_connection;
2016-06-14 23:32:35 -04: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 15:22:45 -04:00
// class : mpdstatus {{{
2016-06-14 23:32:35 -04:00
class mpdstatus {
public:
2016-11-02 15:22:45 -04: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 15:22:45 -04: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-14 23:32:35 -04:00
private:
2016-11-02 15:22:45 -04:00
mpd_status_t m_status;
2016-06-14 23:32:35 -04:00
unique_ptr<mpdsong> m_song;
mpdstate m_state = mpdstate::UNKNOWN;
chrono::system_clock::time_point m_updated_at;
bool m_random = false;
bool m_repeat = false;
bool m_single = false;
int m_songid;
int m_queuelen;
2016-06-14 23:32:35 -04:00
unsigned long m_total_time;
unsigned long m_elapsed_time;
unsigned long m_elapsed_time_ms;
};
// }}}
}
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END