1 #ifndef MPDCLIENT_H
2 #define MPDCLIENT_H
4 #include "config.h"
5 #include "playlist.h"
6 #include "Compiler.h"
8 #include <mpd/client.h>
10 struct filelist;
12 struct mpdclient {
13 #ifdef ENABLE_ASYNC_CONNECT
14 struct mpd_settings *settings;
15 #else
16 const char *host;
17 unsigned port;
18 #endif
20 unsigned timeout_ms;
22 const char *password;
24 /* playlist */
25 struct mpdclient_playlist playlist;
27 #ifdef ENABLE_ASYNC_CONNECT
28 struct aconnect *async_connect;
29 #endif
31 struct mpd_connection *connection;
33 /**
34 * Tracks idle events. It is automatically called by
35 * mpdclient_get_connection().
36 */
37 struct mpd_glib_source *source;
39 struct mpd_status *status;
40 const struct mpd_song *song;
42 /**
43 * The GLib source id which re-enters MPD idle mode before the
44 * next main loop interation.
45 */
46 unsigned enter_idle_source_id;
48 /**
49 * This attribute is incremented whenever the connection changes
50 * (i.e. on disconnection and (re-)connection).
51 */
52 unsigned connection_id;
54 int volume;
56 /**
57 * A bit mask of idle events occurred since the last update.
58 */
59 enum mpd_idle events;
61 /**
62 * This attribute is true when the connection is currently in
63 * "idle" mode, and the #mpd_glib_source waits for an event.
64 */
65 bool idle;
67 /**
68 * Is MPD currently playing?
69 */
70 bool playing;
71 };
73 enum {
74 /**
75 * all idle events the version of libmpdclient, ncmpc is compiled
76 * against, supports
77 */
78 MPD_IDLE_ALL = MPD_IDLE_DATABASE
79 | MPD_IDLE_STORED_PLAYLIST
80 | MPD_IDLE_QUEUE
81 | MPD_IDLE_PLAYER
82 | MPD_IDLE_MIXER
83 | MPD_IDLE_OUTPUT
84 | MPD_IDLE_OPTIONS
85 | MPD_IDLE_UPDATE
86 | MPD_IDLE_STICKER
87 | MPD_IDLE_SUBSCRIPTION
88 | MPD_IDLE_MESSAGE
89 };
91 /** functions ***************************************************************/
93 bool
94 mpdclient_handle_error(struct mpdclient *c);
96 static inline bool
97 mpdclient_finish_command(struct mpdclient *c)
98 {
99 return mpd_response_finish(c->connection)
100 ? true : mpdclient_handle_error(c);
101 }
103 struct mpdclient *
104 mpdclient_new(const gchar *host, unsigned port,
105 unsigned timeout_ms, const gchar *password);
107 void mpdclient_free(struct mpdclient *c);
109 /**
110 * Determine a human-readable "name" of the settings currently used to
111 * connect to MPD.
112 *
113 * @return an allocated string that needs to be freed (with g_free())
114 * by the caller
115 */
116 char *
117 mpdclient_settings_name(const struct mpdclient *c);
119 gcc_pure
120 static inline bool
121 mpdclient_is_connected(const struct mpdclient *c)
122 {
123 return c->connection != NULL;
124 }
126 /**
127 * Is this object "dead"? i.e. not connected and not currently doing
128 * anything to connect.
129 */
130 gcc_pure
131 static inline bool
132 mpdclient_is_dead(const struct mpdclient *c)
133 {
134 return c->connection == NULL
135 #ifdef ENABLE_ASYNC_CONNECT
136 && c->async_connect == NULL
137 #endif
138 ;
139 }
141 gcc_pure
142 static inline bool
143 mpdclient_is_playing(const struct mpdclient *c)
144 {
145 return c->status != NULL &&
146 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
147 mpd_status_get_state(c->status) == MPD_STATE_PAUSE);
148 }
150 gcc_pure
151 static inline const struct mpd_song *
152 mpdclient_get_current_song(const struct mpdclient *c)
153 {
154 return c->song != NULL && mpdclient_is_playing(c)
155 ? c->song
156 : NULL;
157 }
159 void
160 mpdclient_connect(struct mpdclient *c);
162 void
163 mpdclient_disconnect(struct mpdclient *c);
165 bool
166 mpdclient_update(struct mpdclient *c);
168 struct mpd_connection *
169 mpdclient_get_connection(struct mpdclient *c);
171 /*** MPD Commands **********************************************************/
173 bool
174 mpdclient_cmd_crop(struct mpdclient *c);
176 bool
177 mpdclient_cmd_clear(struct mpdclient *c);
179 bool
180 mpdclient_cmd_volume(struct mpdclient *c, gint value);
182 bool
183 mpdclient_cmd_volume_up(struct mpdclient *c);
185 bool
186 mpdclient_cmd_volume_down(struct mpdclient *c);
188 bool
189 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path);
191 bool
192 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song);
194 bool
195 mpdclient_cmd_delete(struct mpdclient *c, gint index);
197 bool
198 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end);
200 bool
201 mpdclient_cmd_move(struct mpdclient *c, unsigned dest, unsigned src);
203 bool
204 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel);
206 bool
207 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel);
209 bool
210 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
211 const char *text);
213 bool
214 mpdclient_send_read_messages(struct mpdclient *c);
216 struct mpd_message *
217 mpdclient_recv_message(struct mpdclient *c);
219 /*** playlist functions **************************************************/
221 /* update the complete playlist */
222 bool
223 mpdclient_playlist_update(struct mpdclient *c);
225 /* get playlist changes */
226 bool
227 mpdclient_playlist_update_changes(struct mpdclient *c);
229 /* add all songs in filelist to the playlist */
230 bool
231 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl);
233 #endif