move code responsible for screen resize to SIGWINCH handler
[ncmpcpp.git] / src / mpdpp.h
blob9b72b67a77dd409634126149ec2a3d263677c679
1 /***************************************************************************
2 * Copyright (C) 2008-2009 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) { }
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;
68 typedef std::pair<std::string, bool> Output;
70 typedef std::vector<Item> ItemList;
71 typedef std::vector<Song *> SongList;
72 typedef std::vector<std::string> TagList;
73 typedef std::vector<Output> OutputList;
75 void FreeSongList(SongList &);
76 void FreeItemList(ItemList &);
78 class Connection
80 typedef void (*StatusUpdater) (Connection *, StatusChanges, void *);
81 typedef void (*ErrorHandler) (Connection *, int, const char *, void *);
83 public:
84 Connection();
85 ~Connection();
87 bool Connect();
88 bool Connected() const;
89 void Disconnect();
91 const std::string & GetHostname() { return itsHost; }
92 int GetPort() { return itsPort; }
94 float Version() const;
96 bool SupportsIdle() const { return supportsIdle; }
97 void OrderDataFetching() { hasData = 1; }
98 int GetFD() const { return itsFD; }
100 void SetHostname(const std::string &);
101 void SetPort(int port) { itsPort = port; }
102 void SetTimeout(int timeout) { itsTimeout = timeout; }
103 void SetPassword(const std::string &password) { itsPassword = password; }
104 bool SendPassword();
106 void SetStatusUpdater(StatusUpdater, void *);
107 void SetErrorHandler(ErrorHandler, void *);
108 void UpdateStatus();
109 void UpdateStats();
110 bool UpdateDirectory(const std::string &);
112 void Play();
113 void Play(int);
114 void PlayID(int);
115 void Pause(bool);
116 void Toggle();
117 void Stop();
118 void Next();
119 void Prev();
120 void Move(unsigned, unsigned);
121 void Swap(unsigned, unsigned);
122 void Seek(unsigned);
123 void Shuffle();
124 void ClearPlaylist();
126 bool isPlaying() const { return GetState() > psStop; }
128 PlayerState GetState() const{ return itsCurrentStatus ? PlayerState(mpd_status_get_state(itsCurrentStatus)) : psUnknown; }
129 bool GetRepeat() const { return itsCurrentStatus ? mpd_status_get_repeat(itsCurrentStatus) : 0; }
130 bool GetRandom() const { return itsCurrentStatus ? mpd_status_get_random(itsCurrentStatus) : 0; }
131 bool GetSingle() const { return itsCurrentStatus ? mpd_status_get_single(itsCurrentStatus) : 0; }
132 bool GetConsume() const { return itsCurrentStatus ? mpd_status_get_consume(itsCurrentStatus) : 0; }
133 bool GetDBIsUpdating() const { return itsCurrentStatus ? mpd_status_get_update_id(itsCurrentStatus) : 0; }
134 int GetVolume() const { return itsCurrentStatus ? mpd_status_get_volume(itsCurrentStatus) : -1; }
135 unsigned GetCrossfade() const { return itsCurrentStatus ? mpd_status_get_crossfade(itsCurrentStatus) : 0; }
136 unsigned GetPlaylistID() const { return itsCurrentStatus ? mpd_status_get_queue_version(itsCurrentStatus) : 0; }
137 unsigned GetOldPlaylistID() const { return itsOldStatus ? mpd_status_get_queue_version(itsOldStatus) : 0; }
138 unsigned GetElapsedTime() const { return itsCurrentStatus ? itsElapsed : 0; }
139 int GetTotalTime() const { return itsCurrentStatus ? mpd_status_get_total_time(itsCurrentStatus) : 0; }
140 unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; }
142 unsigned NumberOfArtists() const { return itsStats ? mpd_stats_get_number_of_artists(itsStats) : 0; }
143 unsigned NumberOfAlbums() const { return itsStats ? mpd_stats_get_number_of_albums(itsStats) : 0; }
144 unsigned NumberOfSongs() const { return itsStats ? mpd_stats_get_number_of_songs(itsStats) : 0; }
145 unsigned long Uptime() const { return itsStats ? mpd_stats_get_uptime(itsStats) : 0; }
146 unsigned long DBUpdateTime() const { return itsStats ? mpd_stats_get_db_update_time(itsStats) : 0; }
147 unsigned long PlayTime() const { return itsStats ? mpd_stats_get_play_time(itsStats) : 0; }
148 unsigned long DBPlayTime() const { return itsStats ? mpd_stats_get_db_play_time(itsStats) : 0; }
150 size_t GetMaxPlaylistLength() const { return itsMaxPlaylistLength; }
151 size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; }
152 void GetPlaylistChanges(unsigned, SongList &);
154 const std::string & GetErrorMessage() const { return itsErrorMessage; }
155 int GetErrorCode() const { return itsErrorCode; }
157 Song GetCurrentSong();
158 int GetCurrentSongPos() const;
159 Song GetSong(const std::string &);
160 void GetPlaylistContent(const std::string &, SongList &);
162 void SetRepeat(bool);
163 void SetRandom(bool);
164 void SetSingle(bool);
165 void SetConsume(bool);
166 void SetCrossfade(unsigned);
167 void SetVolume(unsigned);
169 std::string GetReplayGainMode();
170 void SetReplayGainMode(ReplayGainMode);
172 int AddSong(const std::string &, int = -1); // returns id of added song
173 int AddSong(const Song &, int = -1); // returns id of added song
174 bool AddRandomSongs(size_t);
175 void Add(const std::string &path);
176 void Delete(unsigned);
177 void DeleteID(unsigned);
178 void Delete(const std::string &, unsigned);
179 void StartCommandsList();
180 bool CommitCommandsList();
182 bool DeletePlaylist(const std::string &);
183 bool SavePlaylist(const std::string &);
184 void ClearPlaylist(const std::string &);
185 void AddToPlaylist(const std::string &, const Song &);
186 void AddToPlaylist(const std::string &, const std::string &);
187 void Move(const std::string &, int, int);
188 bool Rename(const std::string &, const std::string &);
190 void StartSearch(bool);
191 void StartFieldSearch(mpd_tag_type);
192 void AddSearch(mpd_tag_type, const std::string &);
193 void CommitSearch(SongList &);
194 void CommitSearch(TagList &);
196 void GetPlaylists(TagList &);
197 void GetList(TagList &, mpd_tag_type);
198 void GetAlbums(const std::string &, TagList &);
199 void GetDirectory(const std::string &, ItemList &);
200 void GetDirectoryRecursive(const std::string &, SongList &);
201 void GetSongs(const std::string &, SongList &);
202 void GetDirectories(const std::string &, TagList &);
204 void GetOutputs(OutputList &);
205 bool EnableOutput(int);
206 bool DisableOutput(int);
208 void GetURLHandlers(TagList &v);
209 void GetTagTypes(TagList &v);
211 private:
212 void GoIdle();
213 int GoBusy();
215 int CheckForErrors();
217 mpd_connection *itsConnection;
218 bool isCommandsListEnabled;
220 std::string itsErrorMessage;
221 int itsErrorCode;
222 size_t itsMaxPlaylistLength;
224 int itsFD;
225 bool isIdle;
226 bool supportsIdle;
227 bool hasData;
229 std::string itsHost;
230 int itsPort;
231 int itsTimeout;
232 std::string itsPassword;
234 mpd_status *itsCurrentStatus;
235 mpd_status *itsOldStatus;
236 mpd_stats *itsStats;
238 unsigned itsElapsed;
239 time_t itsElapsedTimer[2];
241 StatusChanges itsChanges;
243 StatusUpdater itsUpdater;
244 void *itsStatusUpdaterUserdata;
245 ErrorHandler itsErrorHandler;
246 void *itsErrorHandlerUserdata;
248 mpd_tag_type itsSearchedField;
252 extern MPD::Connection Mpd;
254 #endif