Got it working for two hosts, getting the rest should be rather easy, so
[state-utils.git] / src / libmpdclient.h
blob2587fd08c080a2c119a4e06d6b047110f5397fa2
1 /* libmpdclient
2 (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
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 #ifndef LIBMPDCLIENT_H
34 #define LIBMPDCLIENT_H
36 #ifdef WIN32
37 # define __W32API_USE_DLLIMPORT__ 1
38 #endif
40 #include <sys/time.h>
41 #include <stdarg.h>
42 #define MPD_BUFFER_MAX_LENGTH 50000
43 #define MPD_WELCOME_MESSAGE "OK MPD "
45 #define MPD_ERROR_TIMEOUT 10 /* timeout trying to talk to mpd */
46 #define MPD_ERROR_SYSTEM 11 /* system error */
47 #define MPD_ERROR_UNKHOST 12 /* unknown host */
48 #define MPD_ERROR_CONNPORT 13 /* problems connecting to port on host */
49 #define MPD_ERROR_NOTMPD 14 /* mpd not running on port at host */
50 #define MPD_ERROR_NORESPONSE 15 /* no response on attempting to connect */
51 #define MPD_ERROR_SENDING 16 /* error sending command */
52 #define MPD_ERROR_CONNCLOSED 17 /* connection closed by mpd */
53 #define MPD_ERROR_ACK 18 /* ACK returned! */
54 #define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */
56 #define MPD_ACK_ERROR_UNK -1
57 #define MPD_ERROR_AT_UNK -1
59 #define MPD_ACK_ERROR_NOT_LIST 1
60 #define MPD_ACK_ERROR_ARG 2
61 #define MPD_ACK_ERROR_PASSWORD 3
62 #define MPD_ACK_ERROR_PERMISSION 4
63 #define MPD_ACK_ERROR_UNKNOWN_CMD 5
65 #define MPD_ACK_ERROR_NO_EXIST 50
66 #define MPD_ACK_ERROR_PLAYLIST_MAX 51
67 #define MPD_ACK_ERROR_SYSTEM 52
68 #define MPD_ACK_ERROR_PLAYLIST_LOAD 53
69 #define MPD_ACK_ERROR_UPDATE_ALREADY 54
70 #define MPD_ACK_ERROR_PLAYER_SYNC 55
71 #define MPD_ACK_ERROR_EXIST 56
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
77 typedef enum mpd_TagItems {
78 MPD_TAG_ITEM_ARTIST,
79 MPD_TAG_ITEM_ALBUM,
80 MPD_TAG_ITEM_TITLE,
81 MPD_TAG_ITEM_TRACK,
82 MPD_TAG_ITEM_NAME,
83 MPD_TAG_ITEM_GENRE,
84 MPD_TAG_ITEM_DATE,
85 MPD_TAG_ITEM_COMPOSER,
86 MPD_TAG_ITEM_PERFORMER,
87 MPD_TAG_ITEM_COMMENT,
88 MPD_TAG_ITEM_DISC,
89 MPD_TAG_ITEM_FILENAME,
90 MPD_TAG_NUM_OF_ITEM_TYPES
91 } mpd_TagItems;
93 extern char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES];
95 /* internal stuff don't touch this struct */
96 typedef struct _mpd_ReturnElement {
97 char *name;
98 char *value;
99 } mpd_ReturnElement;
101 /* mpd_Connection
102 * holds info about connection to mpd
103 * use error, and errorStr to detect errors
105 typedef struct _mpd_Connection {
106 /* use this to check the version of mpd */
107 int version[3];
108 /* IMPORTANT, you want to get the error messages from here */
109 char errorStr[MPD_BUFFER_MAX_LENGTH + 1];
110 int errorCode;
111 int errorAt;
112 /* this will be set to MPD_ERROR_* if there is an error, 0 if not */
113 int error;
114 /* DON'T TOUCH any of the rest of this stuff */
115 int sock;
116 char buffer[MPD_BUFFER_MAX_LENGTH + 1];
117 int buflen;
118 int bufstart;
119 int doneProcessing;
120 int listOks;
121 int doneListOk;
122 int commandList;
123 mpd_ReturnElement *returnElement;
124 struct timeval timeout;
125 char *request;
126 } mpd_Connection;
128 /* mpd_newConnection
129 * use this to open a new connection
130 * you should use mpd_closeConnection, when your done with the connection,
131 * even if an error has occurred
132 * _timeout_ is the connection timeout period in seconds
134 mpd_Connection *mpd_newConnection(const char *host, int port,
135 float timeout);
137 void mpd_setConnectionTimeout(mpd_Connection * connection,
138 float timeout);
140 /* mpd_closeConnection
141 * use this to close a connection and free'ing subsequent memory
143 void mpd_closeConnection(mpd_Connection * connection);
145 /* mpd_clearError
146 * clears error
148 void mpd_clearError(mpd_Connection * connection);
150 /* STATUS STUFF */
152 /* use these with status.state to determine what state the player is in */
153 #define MPD_STATUS_STATE_UNKNOWN 0
154 #define MPD_STATUS_STATE_STOP 1
155 #define MPD_STATUS_STATE_PLAY 2
156 #define MPD_STATUS_STATE_PAUSE 3
158 /* us this with status.volume to determine if mpd has volume support */
159 #define MPD_STATUS_NO_VOLUME -1
161 /* mpd_Status
162 * holds info return from status command
164 typedef struct mpd_Status {
165 /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */
166 int volume;
167 /* 1 if repeat is on, 0 otherwise */
168 int repeat;
169 /* 1 if random is on, 0 otherwise */
170 int random;
171 /* playlist length */
172 int playlistLength;
173 /* playlist, use this to determine when the playlist has changed */
174 long long playlist;
175 /* use with MPD_STATUS_STATE_* to determine state of player */
176 int state;
177 /* crossfade setting in seconds */
178 int crossfade;
179 /* if a song is currently selected (always the case when state is
180 * PLAY or PAUSE), this is the position of the currently
181 * playing song in the playlist, beginning with 0
183 int song;
184 /* Song ID of the currently selected song */
185 int songid;
186 /* time in seconds that have elapsed in the currently playing/paused
187 * song
189 int elapsedTime;
190 /* length in seconds of the currently playing/paused song */
191 int totalTime;
192 /* current bit rate in kbs */
193 int bitRate;
194 /* audio sample rate */
195 unsigned int sampleRate;
196 /* audio bits */
197 int bits;
198 /* audio channels */
199 int channels;
200 /* 1 if mpd is updating, 0 otherwise */
201 int updatingDb;
202 /* error */
203 char *error;
204 } mpd_Status;
206 void mpd_sendStatusCommand(mpd_Connection * connection);
208 /* mpd_getStatus
209 * returns status info, be sure to free it with mpd_freeStatus()
210 * call this after mpd_sendStatusCommand()
212 mpd_Status *mpd_getStatus(mpd_Connection * connection);
214 /* mpd_freeStatus
215 * free's status info malloc'd and returned by mpd_getStatus
217 void mpd_freeStatus(mpd_Status * status);
219 typedef struct _mpd_Stats {
220 int numberOfArtists;
221 int numberOfAlbums;
222 int numberOfSongs;
223 unsigned long uptime;
224 unsigned long dbUpdateTime;
225 unsigned long playTime;
226 unsigned long dbPlayTime;
227 } mpd_Stats;
229 void mpd_sendStatsCommand(mpd_Connection * connection);
231 mpd_Stats *mpd_getStats(mpd_Connection * connection);
233 void mpd_freeStats(mpd_Stats * stats);
235 /* SONG STUFF */
237 #define MPD_SONG_NO_TIME -1
238 #define MPD_SONG_NO_NUM -1
239 #define MPD_SONG_NO_ID -1
241 /* mpd_Song
242 * for storing song info returned by mpd
244 typedef struct _mpd_Song {
245 /* filename of song */
246 char *file;
247 /* artist, maybe NULL if there is no tag */
248 char *artist;
249 /* title, maybe NULL if there is no tag */
250 char *title;
251 /* album, maybe NULL if there is no tag */
252 char *album;
253 /* track, maybe NULL if there is no tag */
254 char *track;
255 /* name, maybe NULL if there is no tag; it's the name of the current
256 * song, f.e. the icyName of the stream */
257 char *name;
258 /* date */
259 char *date;
261 /* added by qball */
262 /* Genre */
263 char *genre;
264 /* Composer */
265 char *composer;
266 /* Disc */
267 char *disc;
268 /* Comment */
269 char *comment;
271 /* length of song in seconds, check that it is not MPD_SONG_NO_TIME */
272 int time;
273 /* if plchanges/playlistinfo/playlistid used, is the position of the
274 * song in the playlist */
275 int pos;
276 /* song id for a song in the playlist */
277 int id;
278 } mpd_Song;
280 /* mpd_newSong
281 * use to allocate memory for a new mpd_Song
282 * file, artist, etc all initialized to NULL
283 * if your going to assign values to file, artist, etc
284 * be sure to malloc or strdup the memory
285 * use mpd_freeSong to free the memory for the mpd_Song, it will also
286 * free memory for file, artist, etc, so don't do it yourself
288 mpd_Song *mpd_newSong(void);
290 /* mpd_freeSong
291 * use to free memory allocated by mpd_newSong
292 * also it will free memory pointed to by file, artist, etc, so be careful
294 void mpd_freeSong(mpd_Song * song);
296 /* mpd_songDup
297 * works like strDup, but for a mpd_Song
299 mpd_Song *mpd_songDup(mpd_Song * song);
301 /* DIRECTORY STUFF */
303 /* mpd_Directory
304 * used to store info fro directory (right now that just the path)
306 typedef struct _mpd_Directory {
307 char *path;
308 } mpd_Directory;
310 /* mpd_newDirectory
311 * allocates memory for a new directory
312 * use mpd_freeDirectory to free this memory
314 mpd_Directory *mpd_newDirectory(void);
316 /* mpd_freeDirectory
317 * used to free memory allocated with mpd_newDirectory, and it frees
318 * path of mpd_Directory, so be careful
320 void mpd_freeDirectory(mpd_Directory * directory);
322 /* mpd_directoryDup
323 * works like strdup, but for mpd_Directory
325 mpd_Directory *mpd_directoryDup(mpd_Directory * directory);
327 /* PLAYLISTFILE STUFF */
329 /* mpd_PlaylistFile
330 * stores info about playlist file returned by lsinfo
332 typedef struct _mpd_PlaylistFile {
333 char *path;
334 } mpd_PlaylistFile;
336 /* mpd_newPlaylistFile
337 * allocates memory for new mpd_PlaylistFile, path is set to NULL
338 * free this memory with mpd_freePlaylistFile
340 mpd_PlaylistFile *mpd_newPlaylistFile(void);
342 /* mpd_freePlaylist
343 * free memory allocated for freePlaylistFile, will also free
344 * path, so be careful
346 void mpd_freePlaylistFile(mpd_PlaylistFile * playlist);
348 /* mpd_playlistFileDup
349 * works like strdup, but for mpd_PlaylistFile
351 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile * playlist);
353 /* INFO ENTITY STUFF */
355 /* the type of entity returned from one of the commands that generates info
356 * use in conjunction with mpd_InfoEntity.type
358 #define MPD_INFO_ENTITY_TYPE_DIRECTORY 0
359 #define MPD_INFO_ENTITY_TYPE_SONG 1
360 #define MPD_INFO_ENTITY_TYPE_PLAYLISTFILE 2
362 /* mpd_InfoEntity
363 * stores info on stuff returned info commands
365 typedef struct mpd_InfoEntity {
366 /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
367 * what this entity is (song, directory, etc...)
369 int type;
370 /* the actual data you want, mpd_Song, mpd_Directory, etc */
371 union {
372 mpd_Directory *directory;
373 mpd_Song *song;
374 mpd_PlaylistFile *playlistFile;
375 } info;
376 } mpd_InfoEntity;
378 mpd_InfoEntity *mpd_newInfoEntity(void);
380 void mpd_freeInfoEntity(mpd_InfoEntity * entity);
382 /* INFO COMMANDS AND STUFF */
384 /* use this function to loop over after calling Info/Listall functions */
385 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection * connection);
387 /* fetches the currently seeletect song (the song referenced by status->song
388 * and status->songid*/
389 void mpd_sendCurrentSongCommand(mpd_Connection * connection);
391 /* songNum of -1, means to display the whole list */
392 void mpd_sendPlaylistInfoCommand(mpd_Connection * connection,
393 int songNum);
395 /* songId of -1, means to display the whole list */
396 void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int songId);
398 /* use this to get the changes in the playlist since version _playlist_ */
399 void mpd_sendPlChangesCommand(mpd_Connection * connection,
400 long long playlist);
403 * @param connection: A valid and connected mpd_Connection.
404 * @param playlist: The playlist version you want the diff with.
405 * A more bandwidth efficient version of the mpd_sendPlChangesCommand.
406 * It only returns the pos+id of the changes song.
408 void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection,
409 long long playlist);
411 /* recursivel fetches all songs/dir/playlists in "dir* (no metadata is
412 * returned) */
413 void mpd_sendListallCommand(mpd_Connection * connection,
414 const char *dir);
416 /* same as sendListallCommand, but also metadata is returned */
417 void mpd_sendListallInfoCommand(mpd_Connection * connection,
418 const char *dir);
420 /* non-recursive version of ListallInfo */
421 void mpd_sendLsInfoCommand(mpd_Connection * connection,
422 const char *dir);
424 #define MPD_TABLE_ARTIST 0
425 #define MPD_TABLE_ALBUM 1
426 #define MPD_TABLE_TITLE 2
427 #define MPD_TABLE_FILENAME 3
429 void mpd_sendSearchCommand(mpd_Connection * connection, int table,
430 const char *str);
432 void mpd_sendFindCommand(mpd_Connection * connection, int table,
433 const char *str);
435 /* LIST TAG COMMANDS */
437 /* use this function fetch next artist entry, be sure to free the returned
438 * string. NULL means there are no more. Best used with sendListArtists
440 char *mpd_getNextArtist(mpd_Connection * connection);
442 char *mpd_getNextAlbum(mpd_Connection * connection);
444 char *mpd_getNextTag(mpd_Connection * connection, int table);
446 /* list artist or albums by artist, arg1 should be set to the artist if
447 * listing albums by a artist, otherwise NULL for listing all artists or albums
449 void mpd_sendListCommand(mpd_Connection * connection, int table,
450 const char *arg1);
452 /* SIMPLE COMMANDS */
454 void mpd_sendAddCommand(mpd_Connection * connection, const char *file);
456 void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum);
458 void mpd_sendDeleteIdCommand(mpd_Connection * connection, int songNum);
460 void mpd_sendSaveCommand(mpd_Connection * connection, const char *name);
462 void mpd_sendLoadCommand(mpd_Connection * connection, const char *name);
464 void mpd_sendRmCommand(mpd_Connection * connection, const char *name);
466 void mpd_sendShuffleCommand(mpd_Connection * connection);
468 void mpd_sendClearCommand(mpd_Connection * connection);
470 /* use this to start playing at the beginning, useful when in random mode */
471 #define MPD_PLAY_AT_BEGINNING -1
473 void mpd_sendPlayCommand(mpd_Connection * connection, int songNum);
475 void mpd_sendPlayIdCommand(mpd_Connection * connection, int songNum);
477 void mpd_sendStopCommand(mpd_Connection * connection);
479 void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode);
481 void mpd_sendNextCommand(mpd_Connection * connection);
483 void mpd_sendPrevCommand(mpd_Connection * connection);
485 void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to);
487 void mpd_sendMoveIdCommand(mpd_Connection * connection, int from,
488 int to);
490 void mpd_sendSwapCommand(mpd_Connection * connection, int song1,
491 int song2);
493 void mpd_sendSwapIdCommand(mpd_Connection * connection, int song1,
494 int song2);
496 void mpd_sendSeekCommand(mpd_Connection * connection, int song,
497 int time);
499 void mpd_sendSeekIdCommand(mpd_Connection * connection, int song,
500 int time);
502 void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode);
504 void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode);
506 void mpd_sendSetvolCommand(mpd_Connection * connection,
507 int volumeChange);
509 /* WARNING: don't use volume command, its depreacted */
510 void mpd_sendVolumeCommand(mpd_Connection * connection,
511 int volumeChange);
513 void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds);
515 void mpd_sendUpdateCommand(mpd_Connection * connection, char *path);
517 /* returns the update job id, call this after a update command*/
518 int mpd_getUpdateId(mpd_Connection * connection);
520 void mpd_sendPasswordCommand(mpd_Connection * connection,
521 const char *pass);
523 /* after executing a command, when your done with it to get its status
524 * (you want to check connection->error for an error)
526 void mpd_finishCommand(mpd_Connection * connection);
528 /* command list stuff, use this to do things like add files very quickly */
529 void mpd_sendCommandListBegin(mpd_Connection * connection);
531 void mpd_sendCommandListOkBegin(mpd_Connection * connection);
533 void mpd_sendCommandListEnd(mpd_Connection * connection);
535 /* advance to the next listOk
536 * returns 0 if advanced to the next list_OK,
537 * returns -1 if it advanced to an OK or ACK */
538 int mpd_nextListOkCommand(mpd_Connection * connection);
540 typedef struct _mpd_OutputEntity {
541 int id;
542 char *name;
543 int enabled;
544 } mpd_OutputEntity;
546 void mpd_sendOutputsCommand(mpd_Connection * connection);
548 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection * connection);
550 void mpd_sendEnableOutputCommand(mpd_Connection * connection,
551 int outputId);
553 void mpd_sendDisableOutputCommand(mpd_Connection * connection,
554 int outputId);
556 void mpd_freeOutputElement(mpd_OutputEntity * output);
559 * @param connection a #mpd_Connection
561 * Queries mpd for the allowed commands
563 void mpd_sendCommandsCommand(mpd_Connection * connection);
565 * @param connection a #mpd_Connection
567 * Queries mpd for the not allowed commands
569 void mpd_sendNotCommandsCommand(mpd_Connection * connection);
572 * @param connection a #mpd_Connection
574 * returns the next supported command.
576 * @returns a string, needs to be free'ed
578 char *mpd_getNextCommand(mpd_Connection * connection);
581 * @param connection a MpdConnection
582 * @param path the path to the playlist.
584 * List the content, with full metadata, of a stored playlist.
587 void mpd_sendListPlaylistInfoCommand(mpd_Connection * connection,
588 char *path);
590 * @param connection a MpdConnection
591 * @param path the path to the playlist.
593 * List the content of a stored playlist.
596 void mpd_sendListPlaylistCommand(mpd_Connection * connection,
597 char *path);
600 * @param connection a #mpd_Connection
601 * @param exact if to match exact
603 * starts a search, use mpd_addConstraintSearch to add
604 * a constraint to the search, and mpd_commitSearch to do the actual search
606 void mpd_startSearch(mpd_Connection * connection, int exact);
608 * @param connection a #mpd_Connection
609 * @param field
610 * @param name
613 void mpd_addConstraintSearch(mpd_Connection * connection, int field,
614 char *name);
616 * @param connection a #mpd_Connection
619 void mpd_commitSearch(mpd_Connection * connection);
622 * @param connection a #mpd_Connection
623 * @param field The field to search
625 * starts a search for fields... f.e. get a list of artists would be:
626 * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
627 * mpd_commitSearch(connection);
629 * or get a list of artist in genre "jazz" would be:
630 * @code
631 * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
632 * mpd_addConstraintSearch(connection, MPD_TAG_ITEM_GENRE, "jazz")
633 * mpd_commitSearch(connection);
634 * @endcode
636 * mpd_startSearch will return a list of songs (and you need mpd_getNextInfoEntity)
637 * this one will return a list of only one field (the field specified with field) and you need
638 * mpd_getNextTag to get the results
640 void mpd_startFieldSearch(mpd_Connection * connection, int field);
641 #ifdef __cplusplus
643 #endif
644 #endif