2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <chrono>
|
|
|
|
#include <mpd/client.h>
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "lemonbuddy.hpp"
|
|
|
|
#include "exception.hpp"
|
|
|
|
#include "utils/math.hpp"
|
|
|
|
|
|
|
|
namespace mpd
|
|
|
|
{
|
|
|
|
class Exception : public ::Exception
|
|
|
|
{
|
|
|
|
public:
|
2016-06-21 01:59:43 +00:00
|
|
|
Exception(std::string msg, bool clearable)
|
2016-05-19 14:41:06 +00:00
|
|
|
: ::Exception(msg + (clearable ? " (clearable)" : " (not clearable)")){}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClientError : public Exception
|
|
|
|
{
|
|
|
|
public:
|
2016-06-21 01:59:43 +00:00
|
|
|
explicit ClientError(std::string msg, mpd_error code, bool clearable)
|
2016-05-19 14:41:06 +00:00
|
|
|
: Exception("[mpd::ClientError::"+ std::to_string(code) +"] "+ msg, clearable){}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ServerError : public Exception
|
|
|
|
{
|
|
|
|
public:
|
2016-06-21 01:59:43 +00:00
|
|
|
ServerError(std::string msg, mpd_server_error code, bool clearable)
|
2016-05-19 14:41:06 +00:00
|
|
|
: Exception("[mpd::ServerError::"+ std::to_string(code) +"] "+ msg, clearable){}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
UNKNOWN = 1 << 0,
|
|
|
|
STOPPED = 1 << 1,
|
|
|
|
PLAYING = 1 << 2,
|
|
|
|
PAUSED = 1 << 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Song
|
|
|
|
{
|
|
|
|
Song(){}
|
2016-05-31 03:58:58 +00:00
|
|
|
explicit Song(mpd_song *song);
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
std::shared_ptr<mpd_song> song;
|
|
|
|
|
|
|
|
std::string get_artist();
|
|
|
|
std::string get_album();
|
|
|
|
std::string get_title();
|
2016-05-31 03:58:58 +00:00
|
|
|
// unsigned get_duration();
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
operator bool() {
|
|
|
|
return this->song.get() != nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-09 11:42:03 +00:00
|
|
|
class Connection;
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
struct Status
|
|
|
|
{
|
|
|
|
struct StatusDeleter
|
|
|
|
{
|
|
|
|
void operator()(mpd_status *status) {
|
|
|
|
mpd_status_free(status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
explicit Status(mpd_status *status);
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
std::unique_ptr<struct mpd_status, StatusDeleter> status;
|
|
|
|
std::unique_ptr<Song> song;
|
|
|
|
|
|
|
|
std::chrono::system_clock::time_point updated_at;
|
|
|
|
|
|
|
|
int state = UNKNOWN;
|
|
|
|
|
|
|
|
bool random = false,
|
|
|
|
repeat = false,
|
|
|
|
single = false;
|
|
|
|
|
|
|
|
int song_id;
|
|
|
|
|
|
|
|
unsigned long total_time;
|
|
|
|
unsigned long elapsed_time;
|
|
|
|
unsigned long elapsed_time_ms;
|
|
|
|
|
|
|
|
void set(std::unique_ptr<struct mpd_status, StatusDeleter> status);
|
2016-06-09 11:42:03 +00:00
|
|
|
|
|
|
|
void update(int event, std::unique_ptr<Connection>& connection);
|
2016-06-27 21:16:51 +00:00
|
|
|
void update(int event, Connection *connection);
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
void update_timer();
|
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
// unsigned get_total_time();
|
|
|
|
// unsigned get_elapsed_time();
|
2016-05-19 14:41:06 +00:00
|
|
|
unsigned get_elapsed_percentage();
|
|
|
|
std::string get_formatted_elapsed();
|
|
|
|
std::string get_formatted_total();
|
|
|
|
};
|
|
|
|
|
|
|
|
class Connection
|
|
|
|
{
|
|
|
|
struct ConnectionDeleter
|
|
|
|
{
|
|
|
|
void operator()(mpd_connection *connection)
|
|
|
|
{
|
|
|
|
if (connection == nullptr)
|
|
|
|
return;
|
|
|
|
//TRACE("Releasing mpd_connection");
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<mpd_connection, ConnectionDeleter> connection;
|
2016-06-09 11:42:03 +00:00
|
|
|
std::string host;
|
|
|
|
std::string password;
|
2016-06-29 09:06:33 +00:00
|
|
|
int port = 6600;
|
2016-05-19 14:41:06 +00:00
|
|
|
int timeout = 15;
|
|
|
|
|
|
|
|
bool mpd_command_list_active = false;
|
|
|
|
bool mpd_idle = false;
|
2016-06-29 09:06:33 +00:00
|
|
|
int mpd_fd = -1;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
void check_prerequisites();
|
|
|
|
void check_prerequisites_commands_list();
|
|
|
|
|
|
|
|
public:
|
2016-06-09 11:42:03 +00:00
|
|
|
Connection(std::string host, int port, std::string password)
|
|
|
|
: host(host), password(password), port(port) {}
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-01 14:29:17 +00:00
|
|
|
void connect();
|
2016-05-19 14:41:06 +00:00
|
|
|
void disconnect();
|
|
|
|
bool connected();
|
2016-05-31 03:58:58 +00:00
|
|
|
// bool retry_connection(int interval = 1);
|
2016-05-19 14:41:06 +00:00
|
|
|
void idle();
|
|
|
|
int noidle();
|
|
|
|
|
2016-06-21 01:59:43 +00:00
|
|
|
void set_host(std::string host) { this->host = host; }
|
2016-05-19 14:41:06 +00:00
|
|
|
void set_port(int port) { this->port = port; }
|
2016-06-21 01:59:43 +00:00
|
|
|
void set_password(std::string password) { this->password = password; }
|
2016-05-19 14:41:06 +00:00
|
|
|
void set_timeout(int timeout) { this->timeout = timeout; }
|
|
|
|
|
2016-06-27 21:16:51 +00:00
|
|
|
std::unique_ptr<Status> get_status(bool update = true);
|
|
|
|
std::unique_ptr<Status> get_status_safe(bool update = true);
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
std::unique_ptr<Song> get_song();
|
|
|
|
|
|
|
|
void play();
|
|
|
|
void pause(bool state);
|
2016-05-31 03:58:58 +00:00
|
|
|
// void toggle();
|
2016-05-19 14:41:06 +00:00
|
|
|
void stop();
|
|
|
|
void prev();
|
|
|
|
void next();
|
|
|
|
void seek(int percentage);
|
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
void set_repeat(bool mode);
|
|
|
|
void set_random(bool mode);
|
|
|
|
void set_single(bool mode);
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MpdStatus
|
|
|
|
{
|
2016-06-29 09:06:33 +00:00
|
|
|
bool random = false,
|
|
|
|
repeat = false,
|
|
|
|
single = false;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
std::string artist;
|
|
|
|
std::string album;
|
|
|
|
std::string title;
|
|
|
|
|
|
|
|
int elapsed_time = 0;
|
|
|
|
int total_time = 0;
|
|
|
|
|
|
|
|
float get_elapsed_percentage();
|
|
|
|
|
|
|
|
std::string get_formatted_elapsed();
|
|
|
|
std::string get_formatted_total();
|
|
|
|
|
|
|
|
operator bool() {
|
|
|
|
return !this->artist.empty() && !this->title.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|