visualizer: refresh screen immediately after clearing it
[ncmpcpp.git] / src / mpdpp.h
bloba4148e46032086d5ba05615d3fc95d64b906838c
1 /***************************************************************************
2 * Copyright (C) 2008-2010 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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 _MPDPP_H
22 #define _MPDPP_H
24 #include <vector>
26 #include <mpd/client.h>
27 #include "song.h"
29 namespace MPD
31 namespace Message
33 extern const char *PartOfSongsAdded;
34 extern const char *FullPlaylist;
35 extern const char *FunctionDisabledFilteringEnabled;
38 enum ItemType { itDirectory, itSong, itPlaylist };
39 enum PlayerState { psUnknown, psStop, psPlay, psPause };
40 enum ReplayGainMode { rgmOff, rgmTrack, rgmAlbum };
42 struct Item
44 Item() : song(0) { }
45 Song *song;
46 ItemType type;
47 std::string name;
50 struct StatusChanges
52 StatusChanges() : Playlist(0), SongID(0), Database(0), DBUpdating(0), Volume(0), ElapsedTime(0), Crossfade(0), Random(0), Repeat(0), Single(0), Consume(0), PlayerState(0), StatusFlags(0), Outputs(0) { }
53 bool Playlist:1;
54 bool SongID:1;
55 bool Database:1;
56 bool DBUpdating:1;
57 bool Volume:1;
58 bool ElapsedTime:1;
59 bool Crossfade:1;
60 bool Random:1;
61 bool Repeat:1;
62 bool Single:1;
63 bool Consume:1;
64 bool PlayerState:1;
65 bool StatusFlags:1;
66 bool Outputs:1;
69 typedef std::pair<std::string, bool> Output;
71 typedef std::vector<Item> ItemList;
72 typedef std::vector<Song *> SongList;
73 typedef std::vector<std::string> TagList;
74 typedef std::vector<Output> OutputList;
76 void FreeSongList(SongList &);
77 void FreeItemList(ItemList &);
79 class Connection
81 typedef void (*StatusUpdater) (Connection *, StatusChanges, void *);
82 typedef void (*ErrorHandler) (Connection *, int, const char *, void *);
84 public:
85 Connection();
86 ~Connection();
88 bool Connect();
89 bool Connected() const;
90 void Disconnect();
92 const std::string & GetHostname() { return itsHost; }
93 int GetPort() { return itsPort; }
95 float Version() const;
97 void SetIdleEnabled(bool val) { isIdleEnabled = val; }
98 bool SupportsIdle() const { return supportsIdle; }
99 void OrderDataFetching() { hasData = 1; }
100 int GetFD() const { return itsFD; }
102 void SetHostname(const std::string &);
103 void SetPort(int port) { itsPort = port; }
104 void SetTimeout(int timeout) { itsTimeout = timeout; }
105 void SetPassword(const std::string &password) { itsPassword = password; }
106 bool SendPassword();
108 void SetStatusUpdater(StatusUpdater, void *);
109 void SetErrorHandler(ErrorHandler, void *);
110 void UpdateStatus();
111 void UpdateStats();
112 bool UpdateDirectory(const std::string &);
114 void Play();
115 void Play(int);
116 void PlayID(int);
117 void Pause(bool);
118 void Toggle();
119 void Stop();
120 void Next();
121 void Prev();
122 void Move(unsigned, unsigned);
123 void Swap(unsigned, unsigned);
124 void Seek(unsigned);
125 void Shuffle();
126 void ClearPlaylist();
128 bool isPlaying() const { return GetState() > psStop; }
130 PlayerState GetState() const { return itsCurrentStatus ? PlayerState(mpd_status_get_state(itsCurrentStatus)) : psUnknown; }
131 PlayerState GetOldState() const { return itsOldStatus ? PlayerState(mpd_status_get_state(itsOldStatus)) : psUnknown; }
132 bool GetRepeat() const { return itsCurrentStatus ? mpd_status_get_repeat(itsCurrentStatus) : 0; }
133 bool GetRandom() const { return itsCurrentStatus ? mpd_status_get_random(itsCurrentStatus) : 0; }
134 bool GetSingle() const { return itsCurrentStatus ? mpd_status_get_single(itsCurrentStatus) : 0; }
135 bool GetConsume() const { return itsCurrentStatus ? mpd_status_get_consume(itsCurrentStatus) : 0; }
136 bool GetDBIsUpdating() const { return itsCurrentStatus ? mpd_status_get_update_id(itsCurrentStatus) : 0; }
137 int GetVolume() const { return itsCurrentStatus ? mpd_status_get_volume(itsCurrentStatus) : -1; }
138 unsigned GetCrossfade() const { return itsCurrentStatus ? mpd_status_get_crossfade(itsCurrentStatus) : 0; }
139 unsigned GetPlaylistID() const { return itsCurrentStatus ? mpd_status_get_queue_version(itsCurrentStatus) : 0; }
140 unsigned GetOldPlaylistID() const { return itsOldStatus ? mpd_status_get_queue_version(itsOldStatus) : 0; }
141 unsigned GetElapsedTime() const { return itsCurrentStatus ? itsElapsed : 0; }
142 int GetTotalTime() const { return itsCurrentStatus ? mpd_status_get_total_time(itsCurrentStatus) : 0; }
143 unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; }
145 unsigned NumberOfArtists() const { return itsStats ? mpd_stats_get_number_of_artists(itsStats) : 0; }
146 unsigned NumberOfAlbums() const { return itsStats ? mpd_stats_get_number_of_albums(itsStats) : 0; }
147 unsigned NumberOfSongs() const { return itsStats ? mpd_stats_get_number_of_songs(itsStats) : 0; }
148 unsigned long Uptime() const { return itsStats ? mpd_stats_get_uptime(itsStats) : 0; }
149 unsigned long DBUpdateTime() const { return itsStats ? mpd_stats_get_db_update_time(itsStats) : 0; }
150 unsigned long PlayTime() const { return itsStats ? mpd_stats_get_play_time(itsStats) : 0; }
151 unsigned long DBPlayTime() const { return itsStats ? mpd_stats_get_db_play_time(itsStats) : 0; }
153 size_t GetMaxPlaylistLength() const { return itsMaxPlaylistLength; }
154 size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; }
155 void GetPlaylistChanges(unsigned, SongList &);
157 const std::string & GetErrorMessage() const { return itsErrorMessage; }
159 Song GetCurrentSong();
160 int GetCurrentSongPos() const;
161 Song GetSong(const std::string &);
162 void GetPlaylistContent(const std::string &, SongList &);
164 void SetRepeat(bool);
165 void SetRandom(bool);
166 void SetSingle(bool);
167 void SetConsume(bool);
168 void SetCrossfade(unsigned);
169 void SetVolume(unsigned);
171 std::string GetReplayGainMode();
172 void SetReplayGainMode(ReplayGainMode);
174 int AddSong(const std::string &, int = -1); // returns id of added song
175 int AddSong(const Song &, int = -1); // returns id of added song
176 bool AddRandomTag(mpd_tag_type, size_t);
177 bool AddRandomSongs(size_t);
178 void Add(const std::string &path);
179 bool Delete(unsigned);
180 bool DeleteID(unsigned);
181 bool Delete(const std::string &, unsigned);
182 void StartCommandsList();
183 bool CommitCommandsList();
185 bool DeletePlaylist(const std::string &);
186 bool SavePlaylist(const std::string &);
187 void ClearPlaylist(const std::string &);
188 void AddToPlaylist(const std::string &, const Song &);
189 void AddToPlaylist(const std::string &, const std::string &);
190 void Move(const std::string &, int, int);
191 bool Rename(const std::string &, const std::string &);
193 void StartSearch(bool);
194 void StartFieldSearch(mpd_tag_type);
195 void AddSearch(mpd_tag_type, const std::string &) const;
196 void AddSearchAny(const std::string &str) const;
197 void AddSearchURI(const std::string &str) const;
198 void CommitSearch(SongList &);
199 void CommitSearch(TagList &);
201 void GetPlaylists(TagList &);
202 void GetList(TagList &, mpd_tag_type);
203 void GetDirectory(const std::string &, ItemList &);
204 void GetDirectoryRecursive(const std::string &, SongList &);
205 void GetSongs(const std::string &, SongList &);
206 void GetDirectories(const std::string &, TagList &);
208 void GetOutputs(OutputList &);
209 bool EnableOutput(int);
210 bool DisableOutput(int);
212 void GetURLHandlers(TagList &v);
213 void GetTagTypes(TagList &v);
215 private:
216 void GoIdle();
217 int GoBusy();
219 int CheckForErrors();
221 mpd_connection *itsConnection;
222 bool isCommandsListEnabled;
224 std::string itsErrorMessage;
225 size_t itsMaxPlaylistLength;
227 int itsFD;
228 bool isIdle;
229 bool isIdleEnabled;
230 bool supportsIdle;
231 bool hasData;
233 std::string itsHost;
234 int itsPort;
235 int itsTimeout;
236 std::string itsPassword;
238 mpd_status *itsCurrentStatus;
239 mpd_status *itsOldStatus;
240 mpd_stats *itsStats;
242 unsigned itsElapsed;
243 time_t itsElapsedTimer[2];
245 StatusChanges itsChanges;
247 StatusUpdater itsUpdater;
248 void *itsStatusUpdaterUserdata;
249 ErrorHandler itsErrorHandler;
250 void *itsErrorHandlerUserdata;
252 mpd_tag_type itsSearchedField;
256 extern MPD::Connection Mpd;
258 #endif