output: add _get_plugin()
[libmpdclient.git] / src / example.c
blob34e65b095650bc9871c4b4ac9394e4d83b4d442b
1 /* libmpdclient
2 (c) 2003-2017 The Music Player Daemon Project
3 This project's homepage is: http://www.musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of the Music Player Daemon nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <mpd/client.h>
34 #include <mpd/status.h>
35 #include <mpd/entity.h>
36 #include <mpd/search.h>
37 #include <mpd/tag.h>
38 #include <mpd/message.h>
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
45 static int
46 handle_error(struct mpd_connection *c)
48 assert(mpd_connection_get_error(c) != MPD_ERROR_SUCCESS);
50 fprintf(stderr, "%s\n", mpd_connection_get_error_message(c));
51 mpd_connection_free(c);
52 return EXIT_FAILURE;
55 static void
56 print_tag(const struct mpd_song *song, enum mpd_tag_type type,
57 const char *label)
59 unsigned i = 0;
60 const char *value;
62 while ((value = mpd_song_get_tag(song, type, i++)) != NULL)
63 printf("%s: %s\n", label, value);
66 int main(int argc, char ** argv) {
67 struct mpd_connection *conn;
69 conn = mpd_connection_new(NULL, 0, 30000);
71 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
72 return handle_error(conn);
75 int i;
76 for(i=0;i<3;i++) {
77 printf("version[%i]: %i\n",i,
78 mpd_connection_get_server_version(conn)[i]);
82 if(argc==1) {
83 struct mpd_status * status;
84 struct mpd_song *song;
85 const struct mpd_audio_format *audio_format;
87 mpd_command_list_begin(conn, true);
88 mpd_send_status(conn);
89 mpd_send_current_song(conn);
90 mpd_command_list_end(conn);
92 status = mpd_recv_status(conn);
93 if (status == NULL)
94 return handle_error(conn);
96 printf("volume: %i\n", mpd_status_get_volume(status));
97 printf("repeat: %i\n", mpd_status_get_repeat(status));
98 printf("queue version: %u\n", mpd_status_get_queue_version(status));
99 printf("queue length: %i\n", mpd_status_get_queue_length(status));
100 if (mpd_status_get_error(status) != NULL)
101 printf("error: %s\n", mpd_status_get_error(status));
103 if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
104 mpd_status_get_state(status) == MPD_STATE_PAUSE) {
105 printf("song: %i\n", mpd_status_get_song_pos(status));
106 printf("elaspedTime: %i\n",mpd_status_get_elapsed_time(status));
107 printf("elasped_ms: %u\n", mpd_status_get_elapsed_ms(status));
108 printf("totalTime: %i\n", mpd_status_get_total_time(status));
109 printf("bitRate: %i\n", mpd_status_get_kbit_rate(status));
112 audio_format = mpd_status_get_audio_format(status);
113 if (audio_format != NULL) {
114 printf("sampleRate: %i\n", audio_format->sample_rate);
115 printf("bits: %i\n", audio_format->bits);
116 printf("channels: %i\n", audio_format->channels);
119 mpd_status_free(status);
121 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
122 return handle_error(conn);
124 mpd_response_next(conn);
126 while ((song = mpd_recv_song(conn)) != NULL) {
127 printf("uri: %s\n", mpd_song_get_uri(song));
128 print_tag(song, MPD_TAG_ARTIST, "artist");
129 print_tag(song, MPD_TAG_ALBUM, "album");
130 print_tag(song, MPD_TAG_TITLE, "title");
131 print_tag(song, MPD_TAG_TRACK, "track");
132 print_tag(song, MPD_TAG_NAME, "name");
133 print_tag(song, MPD_TAG_DATE, "date");
135 if (mpd_song_get_duration(song) > 0) {
136 printf("time: %u\n", mpd_song_get_duration(song));
139 printf("pos: %u\n", mpd_song_get_pos(song));
141 mpd_song_free(song);
144 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
145 !mpd_response_finish(conn))
146 return handle_error(conn);
148 else if(argc==3 && strcmp(argv[1],"lsinfo")==0) {
149 struct mpd_entity * entity;
151 if (!mpd_send_list_meta(conn, argv[2]))
152 return handle_error(conn);
154 while ((entity = mpd_recv_entity(conn)) != NULL) {
155 const struct mpd_song *song;
156 const struct mpd_directory *dir;
157 const struct mpd_playlist *pl;
159 switch (mpd_entity_get_type(entity)) {
160 case MPD_ENTITY_TYPE_UNKNOWN:
161 break;
163 case MPD_ENTITY_TYPE_SONG:
164 song = mpd_entity_get_song(entity);
165 printf("uri: %s\n", mpd_song_get_uri(song));
166 print_tag(song, MPD_TAG_ARTIST, "artist");
167 print_tag(song, MPD_TAG_ALBUM, "album");
168 print_tag(song, MPD_TAG_TITLE, "title");
169 print_tag(song, MPD_TAG_TRACK, "track");
170 break;
172 case MPD_ENTITY_TYPE_DIRECTORY:
173 dir = mpd_entity_get_directory(entity);
174 printf("directory: %s\n", mpd_directory_get_path(dir));
175 break;
177 case MPD_ENTITY_TYPE_PLAYLIST:
178 pl = mpd_entity_get_playlist(entity);
179 printf("playlist: %s\n",
180 mpd_playlist_get_path(pl));
181 break;
184 mpd_entity_free(entity);
187 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
188 !mpd_response_finish(conn))
189 return handle_error(conn);
191 else if(argc==2 && strcmp(argv[1],"artists")==0) {
192 struct mpd_pair *pair;
194 if (!mpd_search_db_tags(conn, MPD_TAG_ARTIST) ||
195 !mpd_search_commit(conn))
196 return handle_error(conn);
198 while ((pair = mpd_recv_pair_tag(conn,
199 MPD_TAG_ARTIST)) != NULL) {
200 printf("%s\n", pair->value);
201 mpd_return_pair(conn, pair);
204 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
205 !mpd_response_finish(conn))
206 return handle_error(conn);
207 } else if (argc == 2 && strcmp(argv[1], "playlists") == 0) {
208 if (!mpd_send_list_playlists(conn))
209 return handle_error(conn);
211 struct mpd_playlist *playlist;
212 while ((playlist = mpd_recv_playlist(conn)) != NULL) {
213 printf("%s\n",
214 mpd_playlist_get_path(playlist));
215 mpd_playlist_free(playlist);
218 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
219 !mpd_response_finish(conn))
220 return handle_error(conn);
221 } else if (argc == 2 && strcmp(argv[1], "idle") == 0) {
222 enum mpd_idle idle = mpd_run_idle(conn);
223 if (idle == 0 &&
224 mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
225 return handle_error(conn);
227 for (unsigned j = 0;; ++j) {
228 enum mpd_idle i = 1 << j;
229 const char *name = mpd_idle_name(i);
231 if (name == NULL)
232 break;
234 if (idle & i)
235 printf("%s\n", name);
237 } else if (argc == 3 && strcmp(argv[1], "subscribe") == 0) {
238 /* subscribe to a channel and print all messages */
240 if (!mpd_run_subscribe(conn, argv[2]))
241 return handle_error(conn);
243 while (mpd_run_idle_mask(conn, MPD_IDLE_MESSAGE) != 0) {
244 if (!mpd_send_read_messages(conn))
245 return handle_error(conn);
247 struct mpd_message *msg;
248 while ((msg = mpd_recv_message(conn)) != NULL) {
249 printf("%s\n", mpd_message_get_text(msg));
250 mpd_message_free(msg);
253 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
254 !mpd_response_finish(conn))
255 return handle_error(conn);
258 return handle_error(conn);
259 } else if (argc == 2 && strcmp(argv[1], "channels") == 0) {
260 /* print a list of channels */
262 if (!mpd_send_channels(conn))
263 return handle_error(conn);
265 struct mpd_pair *pair;
266 while ((pair = mpd_recv_channel_pair(conn)) != NULL) {
267 printf("%s\n", pair->value);
268 mpd_return_pair(conn, pair);
271 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS ||
272 !mpd_response_finish(conn))
273 return handle_error(conn);
274 } else if (argc == 4 && strcmp(argv[1], "message") == 0) {
275 /* send a message to a channel */
277 if (!mpd_run_send_message(conn, argv[2], argv[3]))
278 return handle_error(conn);
281 mpd_connection_free(conn);
283 return 0;