ncmpc  0.30
mpdclient.hxx
Go to the documentation of this file.
1 #ifndef MPDCLIENT_H
2 #define MPDCLIENT_H
3 
4 #include "config.h"
5 #include "Queue.hxx"
6 #include "Compiler.h"
7 
8 #include <mpd/client.h>
9 
10 #include <string>
11 
12 struct AsyncMpdConnect;
13 struct MpdQueue;
14 struct MpdIdleSource;
15 class FileList;
16 
17 struct mpdclient {
18 #ifdef ENABLE_ASYNC_CONNECT
19 
22  struct mpd_settings *settings;
23 
24 #ifndef WIN32
25 
31  struct mpd_settings *settings2;
32 #endif
33 
34 #else
35  const char *host;
36  unsigned port;
37 #endif
38 
39  const unsigned timeout_ms;
40 
41  const char *const password;
42 
43  /* playlist */
45 
46 #ifdef ENABLE_ASYNC_CONNECT
47  AsyncMpdConnect *async_connect = nullptr;
48 #endif
49 
50  struct mpd_connection *connection = nullptr;
51 
56  MpdIdleSource *source = nullptr;
57 
58  struct mpd_status *status = nullptr;
59  const struct mpd_song *song = nullptr;
60 
65  unsigned enter_idle_source_id = 0;
66 
71  unsigned connection_id = 0;
72 
73  int volume = -1;
74 
78  unsigned events = 0;
79 
80  enum mpd_state state = MPD_STATE_UNKNOWN;
81 
82 #if defined(ENABLE_ASYNC_CONNECT) && !defined(WIN32)
83  bool connecting2;
84 #endif
85 
90  bool idle = false;
91 
95  bool playing = false;
96 
100  bool playing_or_paused = false;
101 
102  mpdclient(const char *host, unsigned port,
103  unsigned _timeout_ms, const char *_password);
104 
106  Disconnect();
107 
108 #ifdef ENABLE_ASYNC_CONNECT
109  mpd_settings_free(settings);
110 
111 #ifndef WIN32
112  if (settings2 != nullptr)
113  mpd_settings_free(settings2);
114 #endif
115 #endif
116  }
117 
125  std::string GetSettingsName() const;
126 
127  bool IsConnected() const {
128  return connection != nullptr;
129  }
130 
135  gcc_pure
136  bool IsDead() const {
137  return connection == nullptr
138 #ifdef ENABLE_ASYNC_CONNECT
139  && async_connect == nullptr
140 #endif
141  ;
142  }
143 
144  gcc_pure
145  const struct mpd_song *GetCurrentSong() const {
146  return song != nullptr && playing_or_paused
147  ? song
148  : nullptr;
149  }
150 
151  void Connect();
152 
153  void Disconnect();
154 
155  bool HandleError();
156 
157  struct mpd_connection *GetConnection();
158 
159  bool FinishCommand() {
160  return mpd_response_finish(connection) || HandleError();
161  }
162 
163  bool Update();
164 
165 private:
166  bool UpdateQueue();
167  bool UpdateQueueChanges();
168 };
169 
170 enum {
175  MPD_IDLE_ALL = MPD_IDLE_DATABASE
176  | MPD_IDLE_STORED_PLAYLIST
177  | MPD_IDLE_QUEUE
178  | MPD_IDLE_PLAYER
179  | MPD_IDLE_MIXER
180  | MPD_IDLE_OUTPUT
181  | MPD_IDLE_OPTIONS
182  | MPD_IDLE_UPDATE
183  | MPD_IDLE_STICKER
184  | MPD_IDLE_SUBSCRIPTION
185  | MPD_IDLE_MESSAGE
186 };
187 
188 /*** MPD Commands **********************************************************/
189 
190 bool
191 mpdclient_cmd_crop(struct mpdclient *c);
192 
193 bool
194 mpdclient_cmd_clear(struct mpdclient *c);
195 
196 bool
197 mpdclient_cmd_volume(struct mpdclient *c, int value);
198 
199 bool
201 
202 bool
204 
205 bool
206 mpdclient_cmd_add_path(struct mpdclient *c, const char *path);
207 
208 bool
209 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song);
210 
211 bool
212 mpdclient_cmd_delete(struct mpdclient *c, int index);
213 
214 bool
215 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end);
216 
217 bool
218 mpdclient_cmd_move(struct mpdclient *c, unsigned dest, unsigned src);
219 
220 bool
221 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel);
222 
223 bool
224 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel);
225 
226 bool
227 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
228  const char *text);
229 
230 #endif
bool HandleError()
const char * host
Definition: mpdclient.hxx:35
bool FinishCommand()
Definition: mpdclient.hxx:159
bool mpdclient_cmd_clear(struct mpdclient *c)
gcc_pure bool IsDead() const
Definition: mpdclient.hxx:136
bool playing_or_paused
Definition: mpdclient.hxx:100
Definition: mpdclient.hxx:175
std::string GetSettingsName() const
bool idle
Definition: mpdclient.hxx:90
void Disconnect()
Definition: gidle.hxx:41
bool playing
Definition: mpdclient.hxx:95
~mpdclient()
Definition: mpdclient.hxx:105
unsigned enter_idle_source_id
Definition: mpdclient.hxx:65
MpdQueue playlist
Definition: mpdclient.hxx:44
struct mpd_connection * GetConnection()
gcc_pure const struct mpd_song * GetCurrentSong() const
Definition: mpdclient.hxx:145
bool mpdclient_cmd_add_path(struct mpdclient *c, const char *path)
bool mpdclient_cmd_crop(struct mpdclient *c)
bool mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
unsigned connection_id
Definition: mpdclient.hxx:71
MpdIdleSource * source
Definition: mpdclient.hxx:56
unsigned events
Definition: mpdclient.hxx:78
mpdclient(const char *host, unsigned port, unsigned _timeout_ms, const char *_password)
bool Update()
Definition: mpdclient.hxx:17
struct mpd_status * status
Definition: mpdclient.hxx:58
void Connect()
bool mpdclient_cmd_move(struct mpdclient *c, unsigned dest, unsigned src)
bool mpdclient_cmd_volume_down(struct mpdclient *c)
bool IsConnected() const
Definition: mpdclient.hxx:127
unsigned port
Definition: mpdclient.hxx:36
enum mpd_state state
Definition: mpdclient.hxx:80
bool mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
Definition: filelist.hxx:54
bool mpdclient_cmd_volume_up(struct mpdclient *c)
const struct mpd_song * song
Definition: mpdclient.hxx:59
bool mpdclient_cmd_volume(struct mpdclient *c, int value)
bool mpdclient_cmd_send_message(struct mpdclient *c, const char *channel, const char *text)
int volume
Definition: mpdclient.hxx:73
bool mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
const char *const password
Definition: mpdclient.hxx:41
#define gcc_pure
Definition: Compiler.h:101
bool mpdclient_cmd_delete(struct mpdclient *c, int index)
bool mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
struct mpd_connection * connection
Definition: mpdclient.hxx:50
const unsigned timeout_ms
Definition: mpdclient.hxx:39
Definition: Queue.hxx:38