Code

wreadln: use memmove() instead of an temporary buffer
[ncmpc.git] / src / libmpdclient.h
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.
31 */
33 #ifndef LIBMPDCLIENT_H
34 #define LIBMPDCLIENT_H
36 #include "song.h"
38 #ifdef WIN32
39 #  define __W32API_USE_DLLIMPORT__ 1
40 #endif
42 #include <sys/time.h>
43 #include <stddef.h>
45 #define MPD_WELCOME_MESSAGE     "OK MPD "
47 #define MPD_ERROR_TIMEOUT       10 /* timeout trying to talk to mpd */
48 #define MPD_ERROR_SYSTEM        11 /* system error */
49 #define MPD_ERROR_UNKHOST       12 /* unknown host */
50 #define MPD_ERROR_CONNPORT      13 /* problems connecting to port on host */
51 #define MPD_ERROR_NOTMPD        14 /* mpd not running on port at host */
52 #define MPD_ERROR_NORESPONSE    15 /* no response on attempting to connect */
53 #define MPD_ERROR_SENDING       16 /* error sending command */
54 #define MPD_ERROR_CONNCLOSED    17 /* connection closed by mpd */
55 #define MPD_ERROR_ACK           18 /* ACK returned! */
56 #define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */
58 #define MPD_ACK_ERROR_UNK       -1
59 #define MPD_ERROR_AT_UNK        -1
61 #define MPD_ACK_ERROR_NOT_LIST                  1
62 #define MPD_ACK_ERROR_ARG                       2
63 #define MPD_ACK_ERROR_PASSWORD                  3
64 #define MPD_ACK_ERROR_PERMISSION                4
65 #define MPD_ACK_ERROR_UNKNOWN_CMD               5
67 #define MPD_ACK_ERROR_NO_EXIST                  50
68 #define MPD_ACK_ERROR_PLAYLIST_MAX              51
69 #define MPD_ACK_ERROR_SYSTEM                    52
70 #define MPD_ACK_ERROR_PLAYLIST_LOAD             53
71 #define MPD_ACK_ERROR_UPDATE_ALREADY            54
72 #define MPD_ACK_ERROR_PLAYER_SYNC               55
73 #define MPD_ACK_ERROR_EXIST                     56
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
79 typedef enum mpd_TagItems
80 {
81         MPD_TAG_ITEM_ARTIST,
82         MPD_TAG_ITEM_ALBUM,
83         MPD_TAG_ITEM_TITLE,
84         MPD_TAG_ITEM_TRACK,
85         MPD_TAG_ITEM_NAME,
86         MPD_TAG_ITEM_GENRE,
87         MPD_TAG_ITEM_DATE,
88         MPD_TAG_ITEM_COMPOSER,
89         MPD_TAG_ITEM_PERFORMER,
90         MPD_TAG_ITEM_COMMENT,
91         MPD_TAG_ITEM_DISC,
92         MPD_TAG_ITEM_FILENAME,
93         MPD_TAG_NUM_OF_ITEM_TYPES
94 }mpd_TagItems;
96 extern const char *const mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES];
98 /* internal stuff don't touch this struct */
99 typedef struct _mpd_ReturnElement {
100         char * name;
101         char * value;
102 } mpd_ReturnElement;
104 /* mpd_Connection
105  * holds info about connection to mpd
106  * use error, and errorStr to detect errors
107  */
108 typedef struct _mpd_Connection {
109         /* use this to check the version of mpd */
110         int version[3];
111         /* IMPORTANT, you want to get the error messages from here */
112         char errorStr[512];
113         int errorCode;
114         int errorAt;
115         /* this will be set to MPD_ERROR_* if there is an error, 0 if not */
116         int error;
117         /* DON'T TOUCH any of the rest of this stuff */
118         int sock;
119         char buffer[16384];
120         size_t buflen;
121         size_t bufstart;
122         int doneProcessing;
123         int listOks;
124         int doneListOk;
125         int commandList;
126         mpd_ReturnElement * returnElement;
127         struct timeval timeout;
128         char *request;
129 } mpd_Connection;
131 /* mpd_newConnection
132  * use this to open a new connection
133  * you should use mpd_closeConnection, when your done with the connection,
134  * even if an error has occurred
135  * _timeout_ is the connection timeout period in seconds
136  */
137 mpd_Connection * mpd_newConnection(const char * host, int port, float timeout);
139 void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout);
141 /* mpd_closeConnection
142  * use this to close a connection and free'ing subsequent memory
143  */
144 void mpd_closeConnection(mpd_Connection * connection);
146 /* mpd_clearError
147  * clears error
148  */
149 void mpd_clearError(mpd_Connection * connection);
151 /* STATUS STUFF */
153 /* use these with status.state to determine what state the player is in */
154 #define MPD_STATUS_STATE_UNKNOWN        0
155 #define MPD_STATUS_STATE_STOP           1
156 #define MPD_STATUS_STATE_PLAY           2
157 #define MPD_STATUS_STATE_PAUSE          3
159 /* us this with status.volume to determine if mpd has volume support */
160 #define MPD_STATUS_NO_VOLUME            -1
162 /* mpd_Status
163  * holds info return from status command
164  */
165 typedef struct mpd_Status {
166         /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */
167         int volume;
168         /* 1 if repeat is on, 0 otherwise */
169         int repeat;
170         /* 1 if random is on, 0 otherwise */
171         int random;
172         /* playlist length */
173         int playlistLength;
174         /* playlist, use this to determine when the playlist has changed */
175         long long playlist;
176         /* use with MPD_STATUS_STATE_* to determine state of player */
177         int state;
178         /* crossfade setting in seconds */
179         int crossfade;
180         /* if a song is currently selected (always the case when state is
181          * PLAY or PAUSE), this is the position of the currently
182          * playing song in the playlist, beginning with 0
183          */
184         int song;
185         /* Song ID of the currently selected song */
186         int songid;
187         /* time in seconds that have elapsed in the currently playing/paused
188          * song
189          */
190         int elapsedTime;
191         /* length in seconds of the currently playing/paused song */
192         int totalTime;
193         /* current bit rate in kbs */
194         int bitRate;
195         /* audio sample rate */
196         unsigned int sampleRate;
197         /* audio bits */
198         int bits;
199         /* audio channels */
200         int channels;
201         /* 1 if mpd is updating, 0 otherwise */
202         int updatingDb;
203         /* error */
204         char * error;
205 } mpd_Status;
207 void mpd_sendStatusCommand(mpd_Connection * connection);
209 /* mpd_getStatus
210  * returns status info, be sure to free it with mpd_freeStatus()
211  * call this after mpd_sendStatusCommand()
212  */
213 mpd_Status * mpd_getStatus(mpd_Connection * connection);
215 /* mpd_freeStatus
216  * free's status info malloc'd and returned by mpd_getStatus
217  */
218 void mpd_freeStatus(mpd_Status * status);
220 typedef struct _mpd_Stats {
221         int numberOfArtists;
222         int numberOfAlbums;
223         int numberOfSongs;
224         unsigned long uptime;
225         unsigned long dbUpdateTime;
226         unsigned long playTime;
227         unsigned long dbPlayTime;
228 } mpd_Stats;
230 void mpd_sendStatsCommand(mpd_Connection * connection);
232 mpd_Stats * mpd_getStats(mpd_Connection * connection);
234 void mpd_freeStats(mpd_Stats * stats);
236 /* DIRECTORY STUFF */
238 /* mpd_Directory
239  * used to store info fro directory (right now that just the path)
240  */
241 typedef struct _mpd_Directory {
242         char * path;
243 } mpd_Directory;
245 /* mpd_newDirectory
246  * allocates memory for a new directory
247  * use mpd_freeDirectory to free this memory
248  */
249 mpd_Directory * mpd_newDirectory(void);
251 /* mpd_freeDirectory
252  * used to free memory allocated with mpd_newDirectory, and it frees
253  * path of mpd_Directory, so be careful
254  */
255 void mpd_freeDirectory(mpd_Directory * directory);
257 /* mpd_directoryDup
258  * works like strdup, but for mpd_Directory
259  */
260 mpd_Directory * mpd_directoryDup(mpd_Directory * directory);
262 /* PLAYLISTFILE STUFF */
264 /* mpd_PlaylistFile
265  * stores info about playlist file returned by lsinfo
266  */
267 typedef struct _mpd_PlaylistFile {
268         char * path;
269 } mpd_PlaylistFile;
271 /* mpd_newPlaylistFile
272  * allocates memory for new mpd_PlaylistFile, path is set to NULL
273  * free this memory with mpd_freePlaylistFile
274  */
275 mpd_PlaylistFile * mpd_newPlaylistFile(void);
277 /* mpd_freePlaylist
278  * free memory allocated for freePlaylistFile, will also free
279  * path, so be careful
280  */
281 void mpd_freePlaylistFile(mpd_PlaylistFile * playlist);
283 /* mpd_playlistFileDup
284  * works like strdup, but for mpd_PlaylistFile
285  */
286 mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist);
288 /* INFO ENTITY STUFF */
290 /* the type of entity returned from one of the commands that generates info
291  * use in conjunction with mpd_InfoEntity.type
292  */
293 #define MPD_INFO_ENTITY_TYPE_DIRECTORY          0
294 #define MPD_INFO_ENTITY_TYPE_SONG               1
295 #define MPD_INFO_ENTITY_TYPE_PLAYLISTFILE       2
297 /* mpd_InfoEntity
298  * stores info on stuff returned info commands
299  */
300 typedef struct mpd_InfoEntity {
301         /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
302          * what this entity is (song, directory, etc...)
303          */
304         int type;
305         /* the actual data you want, mpd_Song, mpd_Directory, etc */
306         union {
307                 mpd_Directory * directory;
308                 mpd_Song * song;
309                 mpd_PlaylistFile * playlistFile;
310         } info;
311 } mpd_InfoEntity;
313 mpd_InfoEntity * mpd_newInfoEntity(void);
315 void mpd_freeInfoEntity(mpd_InfoEntity * entity);
317 /* INFO COMMANDS AND STUFF */
319 /* use this function to loop over after calling Info/Listall functions */
320 mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection);
322 /* fetches the currently seeletect song (the song referenced by status->song
323  * and status->songid*/
324 void mpd_sendCurrentSongCommand(mpd_Connection * connection);
326 /* songNum of -1, means to display the whole list */
327 void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum);
329 /* songId of -1, means to display the whole list */
330 void mpd_sendPlaylistIdCommand(mpd_Connection * connection, int songId);
332 /* use this to get the changes in the playlist since version _playlist_ */
333 void mpd_sendPlChangesCommand(mpd_Connection * connection, long long playlist);
335 /**
336  * @param connection: A valid and connected mpd_Connection.
337  * @param playlist: The playlist version you want the diff with.
338  * A more bandwidth efficient version of the mpd_sendPlChangesCommand.
339  * It only returns the pos+id of the changes song.
340  */
341 void mpd_sendPlChangesPosIdCommand(mpd_Connection * connection, long long playlist);
343 /* recursivel fetches all songs/dir/playlists in "dir* (no metadata is
344  * returned) */
345 void mpd_sendListallCommand(mpd_Connection * connection, const char * dir);
347 /* same as sendListallCommand, but also metadata is returned */
348 void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir);
350 /* non-recursive version of ListallInfo */
351 void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir);
353 #define MPD_TABLE_ARTIST        0
354 #define MPD_TABLE_ALBUM         1
355 #define MPD_TABLE_TITLE         2
356 #define MPD_TABLE_FILENAME      3
358 void mpd_sendSearchCommand(mpd_Connection * connection, int table,
359                 const char * str);
361 void mpd_sendFindCommand(mpd_Connection * connection, int table,
362                 const char * str);
364 /* LIST TAG COMMANDS */
366 /* use this function fetch next artist entry, be sure to free the returned
367  * string.  NULL means there are no more.  Best used with sendListArtists
368  */
369 char * mpd_getNextArtist(mpd_Connection * connection);
371 char * mpd_getNextAlbum(mpd_Connection * connection);
373 char * mpd_getNextTag(mpd_Connection *connection, int table);
375 /* list artist or albums by artist, arg1 should be set to the artist if
376  * listing albums by a artist, otherwise NULL for listing all artists or albums
377  */
378 void mpd_sendListCommand(mpd_Connection * connection, int table,
379                 const char * arg1);
381 /* SIMPLE COMMANDS */
383 void mpd_sendAddCommand(mpd_Connection * connection, const char * file);
385 void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum);
387 void mpd_sendDeleteIdCommand(mpd_Connection * connection, int songNum);
389 void mpd_sendSaveCommand(mpd_Connection * connection, const char * name);
391 void mpd_sendLoadCommand(mpd_Connection * connection, const char * name);
393 void mpd_sendRmCommand(mpd_Connection * connection, const char * name);
395 void mpd_sendShuffleCommand(mpd_Connection * connection);
397 void mpd_sendClearCommand(mpd_Connection * connection);
399 /* use this to start playing at the beginning, useful when in random mode */
400 #define MPD_PLAY_AT_BEGINNING   -1
402 void mpd_sendPlayCommand(mpd_Connection * connection, int songNum);
404 void mpd_sendPlayIdCommand(mpd_Connection * connection, int songNum);
406 void mpd_sendStopCommand(mpd_Connection * connection);
408 void mpd_sendPauseCommand(mpd_Connection * connection, int pauseMode);
410 void mpd_sendNextCommand(mpd_Connection * connection);
412 void mpd_sendPrevCommand(mpd_Connection * connection);
414 void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to);
416 void mpd_sendMoveIdCommand(mpd_Connection * connection, int from, int to);
418 void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2);
420 void mpd_sendSwapIdCommand(mpd_Connection * connection, int song1, int song2);
422 void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time);
424 void mpd_sendSeekIdCommand(mpd_Connection * connection, int song, int time);
426 void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode);
428 void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode);
430 void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange);
432 /* WARNING: don't use volume command, its depreacted */
433 void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange);
435 void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds);
437 void mpd_sendUpdateCommand(mpd_Connection * connection, const char *path);
439 /* returns the update job id, call this after a update command*/
440 int mpd_getUpdateId(mpd_Connection * connection);
442 void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass);
444 /* after executing a command, when your done with it to get its status
445  * (you want to check connection->error for an error)
446  */
447 void mpd_finishCommand(mpd_Connection * connection);
449 /* command list stuff, use this to do things like add files very quickly */
450 void mpd_sendCommandListBegin(mpd_Connection * connection);
452 void mpd_sendCommandListOkBegin(mpd_Connection * connection);
454 void mpd_sendCommandListEnd(mpd_Connection * connection);
456 /* advance to the next listOk
457  * returns 0 if advanced to the next list_OK,
458  * returns -1 if it advanced to an OK or ACK */
459 int mpd_nextListOkCommand(mpd_Connection * connection);
461 typedef struct _mpd_OutputEntity {
462         int id;
463         char * name;
464         int enabled;
465 } mpd_OutputEntity;
467 void mpd_sendOutputsCommand(mpd_Connection * connection);
469 mpd_OutputEntity * mpd_getNextOutput(mpd_Connection * connection);
471 void mpd_sendEnableOutputCommand(mpd_Connection * connection, int outputId);
473 void mpd_sendDisableOutputCommand(mpd_Connection * connection, int outputId);
475 void mpd_freeOutputElement(mpd_OutputEntity * output);
477 /**
478  * @param connection a #mpd_Connection
479  *
480  * Queries mpd for the allowed commands
481  */
482 void mpd_sendCommandsCommand(mpd_Connection * connection);
483 /**
484  * @param connection a #mpd_Connection
485  *
486  * Queries mpd for the not allowed commands
487  */
488 void mpd_sendNotCommandsCommand(mpd_Connection * connection);
490 /**
491  * @param connection a #mpd_Connection
492  *
493  * returns the next supported command.
494  *
495  * @returns a string, needs to be free'ed
496  */
497 char *mpd_getNextCommand(mpd_Connection *connection);
499 /**
500  * @param connection a MpdConnection
501  * @param path  the path to the playlist.
502  *
503  * List the content, with full metadata, of a stored playlist.
504  *
505  */
506 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path);
507 /**
508  * @param connection a MpdConnection
509  * @param path  the path to the playlist.
510  *
511  * List the content of a stored playlist.
512  *
513  */
514 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path);
516 /**
517  * @param connection a #mpd_Connection
518  * @param exact if to match exact
519  *
520  * starts a search, use mpd_addConstraintSearch to add
521  * a constraint to the search, and mpd_commitSearch to do the actual search
522  */
523 void mpd_startSearch(mpd_Connection * connection,int exact);
524 /**
525  * @param connection a #mpd_Connection
526  * @param field
527  * @param name
528  *
529  */
530 void mpd_addConstraintSearch(mpd_Connection *connection, int field, char *name);
531 /**
532  * @param connection a #mpd_Connection
533  *
534  */
535 void mpd_commitSearch(mpd_Connection *connection);
537 /**
538  * @param connection a #mpd_Connection
539  * @param field The field to search
540  *
541  * starts a search for fields... f.e. get a list of artists would be:
542  * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
543  * mpd_commitSearch(connection);
544  *
545  * or get a list of artist in genre "jazz" would be:
546  * @code
547  * mpd_startFieldSearch(connection, MPD_TAG_ITEM_ARTIST);
548  * mpd_addConstraintSearch(connection, MPD_TAG_ITEM_GENRE, "jazz")
549  * mpd_commitSearch(connection);
550  * @endcode
551  *
552  * mpd_startSearch will return  a list of songs (and you need mpd_getNextInfoEntity)
553  * this one will return a list of only one field (the field specified with field) and you need
554  * mpd_getNextTag to get the results
555  */
556 void mpd_startFieldSearch(mpd_Connection * connection,int field);
557 #ifdef __cplusplus
559 #endif
561 #endif