1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_MPDPP_H
22 #define NCMPCPP_MPDPP_H
29 #include <mpd/client.h>
34 enum ItemType
{ itDirectory
, itSong
, itPlaylist
};
35 enum PlayerState
{ psUnknown
, psStop
, psPlay
, psPause
};
36 enum ReplayGainMode
{ rgmOff
, rgmTrack
, rgmAlbum
};
38 struct ClientError
: public std::exception
40 ClientError(mpd_error code_
, std::string msg
, bool clearable_
)
41 : m_code(code_
), m_msg(msg
), m_clearable(clearable_
) { }
42 virtual ~ClientError() noexcept
{ }
44 virtual const char *what() const noexcept
{ return m_msg
.c_str(); }
45 mpd_error
code() const { return m_code
; }
46 bool clearable() const { return m_clearable
; }
54 struct ServerError
: public std::exception
56 ServerError(mpd_server_error code_
, std::string msg
, bool clearable_
)
57 : m_code(code_
), m_msg(msg
), m_clearable(clearable_
) { }
58 virtual ~ServerError() noexcept
{ }
60 virtual const char *what() const noexcept
{ return m_msg
.c_str(); }
61 mpd_server_error
code() const { return m_code
; }
62 bool clearable() const { return m_clearable
; }
65 mpd_server_error m_code
;
72 friend class Connection
;
74 bool empty() const { return m_stats
.get() == nullptr; }
76 unsigned artists() const { return mpd_stats_get_number_of_artists(m_stats
.get()); }
77 unsigned albums() const { return mpd_stats_get_number_of_albums(m_stats
.get()); }
78 unsigned songs() const { return mpd_stats_get_number_of_songs(m_stats
.get()); }
79 unsigned long playTime() const { return mpd_stats_get_play_time(m_stats
.get()); }
80 unsigned long uptime() const { return mpd_stats_get_uptime(m_stats
.get()); }
81 unsigned long dbUpdateTime() const { return mpd_stats_get_db_update_time(m_stats
.get()); }
82 unsigned long dbPlayTime() const { return mpd_stats_get_db_play_time(m_stats
.get()); }
85 Statistics(mpd_stats
*stats
) : m_stats(stats
, mpd_stats_free
) { }
87 std::shared_ptr
<mpd_stats
> m_stats
;
92 friend class Connection
;
96 void clear() { m_status
.reset(); }
97 bool empty() const { return m_status
.get() == nullptr; }
99 int volume() const { return mpd_status_get_volume(m_status
.get()); }
100 bool repeat() const { return mpd_status_get_repeat(m_status
.get()); }
101 bool random() const { return mpd_status_get_random(m_status
.get()); }
102 bool single() const { return mpd_status_get_single(m_status
.get()); }
103 bool consume() const { return mpd_status_get_consume(m_status
.get()); }
104 unsigned playlistLength() const { return mpd_status_get_queue_length(m_status
.get()); }
105 unsigned playlistVersion() const { return mpd_status_get_queue_version(m_status
.get()); }
106 PlayerState
playerState() const { return PlayerState(mpd_status_get_state(m_status
.get())); }
107 unsigned crossfade() const { return mpd_status_get_crossfade(m_status
.get()); }
108 int currentSongPosition() const { return mpd_status_get_song_pos(m_status
.get()); }
109 int currentSongID() const { return mpd_status_get_song_id(m_status
.get()); }
110 int nextSongPosition() const { return mpd_status_get_next_song_pos(m_status
.get()); }
111 int nextSongID() const { return mpd_status_get_next_song_id(m_status
.get()); }
112 unsigned elapsedTime() const { return mpd_status_get_elapsed_time(m_status
.get()); }
113 unsigned totalTime() const { return mpd_status_get_total_time(m_status
.get()); }
114 unsigned kbps() const { return mpd_status_get_kbit_rate(m_status
.get()); }
115 unsigned updateID() const { return mpd_status_get_update_id(m_status
.get()); }
116 const char *error() const { return mpd_status_get_error(m_status
.get()); }
119 Status(mpd_status
*status
) : m_status(status
, mpd_status_free
) { }
121 std::shared_ptr
<mpd_status
> m_status
;
126 std::shared_ptr
<Song
> song
;
133 Output(const std::string
&name_
, bool enabled
) : m_name(name_
), m_enabled(enabled
) { }
135 const std::string
&name() const { return m_name
; }
136 bool isEnabled() const { return m_enabled
; }
143 typedef std::vector
<Item
> ItemList
;
144 typedef std::vector
<std::string
> StringList
;
145 typedef std::vector
<Output
> OutputList
;
149 typedef void (*ErrorHandler
) (Connection
*, int, const char *, void *);
151 typedef std::function
<void(Item
)> ItemConsumer
;
152 typedef std::function
<void(Output
)> OutputConsumer
;
153 typedef std::function
<void(Song
)> SongConsumer
;
154 typedef std::function
<void(std::string
)> StringConsumer
;
161 bool Connected() const;
164 const std::string
&GetHostname() { return m_host
; }
165 int GetPort() { return m_port
; }
167 unsigned Version() const;
169 int GetFD() const { return m_fd
; }
171 void SetHostname(const std::string
&);
172 void SetPort(int port
) { m_port
= port
; }
173 void SetTimeout(int timeout
) { m_timeout
= timeout
; }
174 void SetPassword(const std::string
&password
) { m_password
= password
; }
177 Statistics
getStatistics();
180 void UpdateDirectory(const std::string
&);
190 void Move(unsigned int from
, unsigned int to
);
191 void Swap(unsigned, unsigned);
192 void Seek(unsigned int pos
, unsigned int where
);
194 void ClearMainPlaylist();
196 void GetPlaylistChanges(unsigned, SongConsumer f
);
198 Song
GetCurrentlyPlayingSong();
199 Song
GetSong(const std::string
&);
200 void GetPlaylistContent(const std::string
&name
, SongConsumer f
);
201 void GetPlaylistContentNoInfo(const std::string
&name
, SongConsumer f
);
203 void GetSupportedExtensions(std::set
<std::string
> &);
205 void SetRepeat(bool);
206 void SetRandom(bool);
207 void SetSingle(bool);
208 void SetConsume(bool);
209 void SetCrossfade(unsigned);
210 void SetVolume(unsigned int vol
);
212 std::string
GetReplayGainMode();
213 void SetReplayGainMode(ReplayGainMode
);
215 void SetPriority(const MPD::Song
&s
, int prio
);
217 int AddSong(const std::string
&, int = -1); // returns id of added song
218 int AddSong(const Song
&, int = -1); // returns id of added song
219 bool AddRandomTag(mpd_tag_type
, size_t);
220 bool AddRandomSongs(size_t);
221 void Add(const std::string
&path
);
222 void Delete(unsigned int pos
);
223 void PlaylistDelete(const std::string
&playlist
, unsigned int pos
);
224 void StartCommandsList();
225 void CommitCommandsList();
227 void DeletePlaylist(const std::string
&name
);
228 void LoadPlaylist(const std::string
&name
);
229 void SavePlaylist(const std::string
&);
230 void ClearPlaylist(const std::string
&playlist
);
231 void AddToPlaylist(const std::string
&, const Song
&);
232 void AddToPlaylist(const std::string
&, const std::string
&);
233 void PlaylistMove(const std::string
&path
, int from
, int to
);
234 void Rename(const std::string
&from
, const std::string
&to
);
236 void StartSearch(bool);
237 void StartFieldSearch(mpd_tag_type
);
238 void AddSearch(mpd_tag_type item
, const std::string
&str
) const;
239 void AddSearchAny(const std::string
&str
) const;
240 void AddSearchURI(const std::string
&str
) const;
241 void CommitSearchSongs(SongConsumer f
);
242 void CommitSearchTags(StringConsumer f
);
244 void GetPlaylists(StringConsumer f
);
245 void GetList(mpd_tag_type type
, StringConsumer f
);
246 void GetDirectory(const std::string
&directory
, ItemConsumer f
);
247 void GetDirectoryRecursive(const std::string
&directory
, SongConsumer f
);
248 void GetSongs(const std::string
&directory
, SongConsumer f
);
249 void GetDirectories(const std::string
&directory
, StringConsumer f
);
251 void GetOutputs(OutputConsumer f
);
252 void EnableOutput(int id
);
253 void DisableOutput(int id
);
255 void GetURLHandlers(StringConsumer f
);
256 void GetTagTypes(StringConsumer f
);
263 void checkConnection() const;
265 void prechecksNoCommandsList();
266 void checkErrors() const;
268 mpd_connection
*m_connection
;
269 bool m_command_list_active
;
277 std::string m_password
;
279 mpd_tag_type m_searched_field
;
284 extern MPD::Connection Mpd
;
286 #endif // NCMPCPP_MPDPP_H