Code

Renamed ncmpcrc.sample config.sample.
[ncmpc.git] / libmpdclient.h
1 /* libmpdclient
2  * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
3  * This project's homepage is: http://www.musicpd.org
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
20 #ifndef LIBMPDCLIENT_H
21 #define LIBMPDCLIENT_H
23 #include <sys/time.h>
25 #define MPD_BUFFER_MAX_LENGTH   50000
26 #define MPD_WELCOME_MESSAGE     "OK MPD "
28 #define MPD_ERROR_TIMEOUT       10 /* timeout trying to talk to mpd */
29 #define MPD_ERROR_SYSTEM        11 /* system error */
30 #define MPD_ERROR_UNKHOST       12 /* unknown host */
31 #define MPD_ERROR_CONNPORT      13 /* problems connecting to port on host */
32 #define MPD_ERROR_NOTMPD        14 /* mpd not running on port at host */
33 #define MPD_ERROR_NORESPONSE    15 /* no response on attempting to connect */
34 #define MPD_ERROR_SENDING       16 /* error sending command */
35 #define MPD_ERROR_CONNCLOSED    17 /* connection closed by mpd */
36 #define MPD_ERROR_ACK           18 /* ACK returned! */
37 #define MPD_ERROR_BUFFEROVERRUN 19 /* Buffer was overrun! */
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /* internal stuff don't touch this struct */
44 typedef struct _mpd_ReturnElement {
45         char * name;
46         char * value;
47 } mpd_ReturnElement;
49 /* mpd_Connection
50  * holds info about connection to mpd
51  * use error, and errorStr to detect errors
52  */
53 typedef struct _mpd_Connection {
54         /* use this to check the version of mpd */
55         int version[3];
56         /* IMPORTANT, you want to get the error messages from here */
57         char errorStr[MPD_BUFFER_MAX_LENGTH+1];
58         /* this will be set to 1 if there is an error, 0 if not */
59         int error;
60         /* DON'T TOUCH any of the rest of this stuff */
61         int sock; 
62         char buffer[MPD_BUFFER_MAX_LENGTH+1];
63         int buflen;
64         int bufstart;
65         int doneProcessing;
66         int commandList;
67         mpd_ReturnElement * returnElement;
68         struct timeval timeout;
69 } mpd_Connection;
71 /* mpd_newConnection
72  * use this to open a new connection
73  * you should use mpd_closeConnection, when your done with the connection,
74  * even if an error has occurred
75  * _timeout_ is the connection timeout period in seconds
76  */
77 mpd_Connection * mpd_newConnection(const char * host, int port, float timeout);
79 void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout);
81 /* mpd_closeConnection
82  * use this to close a connection and free'ing subsequent memory
83  */
84 void mpd_closeConnection(mpd_Connection * connection);
86 /* mpd_clearError
87  * clears error
88  */
89 void mpd_clearError(mpd_Connection * connection);
91 /* STATUS STUFF */
93 /* use these with status.state to determine what state the player is in */
94 #define MPD_STATUS_STATE_UNKNOWN        0
95 #define MPD_STATUS_STATE_STOP           1
96 #define MPD_STATUS_STATE_PLAY           2
97 #define MPD_STATUS_STATE_PAUSE          3
99 /* us this with status.volume to determine if mpd has volume support */
100 #define MPD_STATUS_NO_VOLUME            -1
102 /* mpd_Status
103  * holds info return from status command
104  */
105 typedef struct mpd_Status {
106         /* 0-100, or MPD_STATUS_NO_VOLUME when there is no volume support */
107         int volume;
108         /* 1 if repeat is on, 0 otherwise */
109         int repeat;
110         /* 1 if random is on, 0 otherwise */
111         int random;
112         /* playlist length */
113         int playlistLength;
114         /* playlist, use this to determine when the playlist has changed */
115         long long playlist;
116         /* use with MPD_STATUS_STATE_* to determine state of player */
117         int state;
118         /* crossfade setting in seconds */
119         int crossfade;
120         /* if in PLAY or PAUSE state, this is the number of the currently
121          * playing song in the playlist, beginning with 0
122          */
123         int song;
124         /* time in seconds that have elapsed in the currently playing/paused
125          * song
126          */
127         int elapsedTime;
128         /* length in seconds of the currently playing/paused song */
129         int totalTime;
130         /* current bit rate in kbs */
131         int bitRate;
132         /* audio sample rate */
133         unsigned int sampleRate;
134         /* audio bits */
135         int bits;
136         /* audio channels */
137         int channels;
138         /* error */
139         char * error;
140 } mpd_Status;
142 /* mpd_getStatus
143  * returns status info, be sure to free it with mpd_freeStatus()
144  */
145 mpd_Status * mpd_getStatus(mpd_Connection * connection);
147 /* mpd_freeStatus
148  * free's status info malloc'd and returned by mpd_getStatus
149  */
150 void mpd_freeStatus(mpd_Status * status);
152 typedef struct _mpd_Stats {
153         int numberOfArtists;
154         int numberOfAlbums;
155         int numberOfSongs;
156         unsigned long uptime;
157         unsigned long dbUpdateTime;
158         unsigned long playTime;
159         unsigned long dbPlayTime;
160 } mpd_Stats;
162 mpd_Stats * mpd_getStats(mpd_Connection * connection);
164 void mpd_freeStats(mpd_Stats * stats);
166 /* SONG STUFF */
168 #define MPD_SONG_NO_TIME        -1
170 /* mpd_Song
171  * for storing song info returned by mpd
172  */
173 typedef struct _mpd_Song {
174         /* filename of song */
175         char * file;
176         /* artist, maybe NULL if there is no tag */
177         char * artist;
178         /* title, maybe NULL if there is no tag */
179         char * title;
180         /* album, maybe NULL if there is no tag */
181         char * album;
182         /* track, maybe NULL if there is no tag */
183         char * track;
184         /* length of song in seconds, check that it is not MPD_SONG_NO_TIME  */
185         int time;
186 } mpd_Song;
188 /* mpd_newSong
189  * use to allocate memory for a new mpd_Song
190  * file, artist, etc all initialized to NULL
191  * if your going to assign values to file, artist, etc
192  * be sure to malloc or strdup the memory
193  * use mpd_freeSong to free the memory for the mpd_Song, it will also
194  * free memory for file, artist, etc, so don't do it yourself
195  */
196 mpd_Song * mpd_newSong();
198 /* mpd_freeSong
199  * use to free memory allocated by mpd_newSong
200  * also it will free memory pointed to by file, artist, etc, so be careful
201  */
202 void mpd_freeSong(mpd_Song * song);
204 /* mpd_songDup
205  * works like strDup, but for a mpd_Song
206  */
207 mpd_Song * mpd_songDup(mpd_Song * song);
209 /* DIRECTORY STUFF */
211 /* mpd_Directory
212  * used to store info fro directory (right now that just the path)
213  */
214 typedef struct _mpd_Directory {
215         char * path;
216 } mpd_Directory;
218 /* mpd_newDirectory
219  * allocates memory for a new directory
220  * use mpd_freeDirectory to free this memory
221  */
222 mpd_Directory * mpd_newDirectory ();
224 /* mpd_freeDirectory
225  * used to free memory allocated with mpd_newDirectory, and it frees
226  * path of mpd_Directory, so be careful
227  */
228 void mpd_freeDirectory(mpd_Directory * directory);
230 /* mpd_directoryDup
231  * works like strdup, but for mpd_Directory
232  */
233 mpd_Directory * mpd_directoryDup(mpd_Directory * directory);
235 /* PLAYLISTFILE STUFF */
237 /* mpd_PlaylistFile
238  * stores info about playlist file returned by lsinfo
239  */
240 typedef struct _mpd_PlaylistFile {
241         char * path;
242 } mpd_PlaylistFile;
244 /* mpd_newPlaylistFile
245  * allocates memory for new mpd_PlaylistFile, path is set to NULL
246  * free this memory with mpd_freePlaylistFile
247  */
248 mpd_PlaylistFile * mpd_newPlaylistFile();
250 /* mpd_freePlaylist
251  * free memory allocated for freePlaylistFile, will also free
252  * path, so be careful
253  */
254 void mpd_freePlaylistFile(mpd_PlaylistFile * playlist);
256 /* mpd_playlistFileDup
257  * works like strdup, but for mpd_PlaylistFile
258  */
259 mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist);
261 /* INFO ENTITY STUFF */
263 /* the type of entity returned from one of the commands that generates info
264  * use in conjunction with mpd_InfoEntity.type
265  */
266 #define MPD_INFO_ENTITY_TYPE_DIRECTORY          0
267 #define MPD_INFO_ENTITY_TYPE_SONG               1
268 #define MPD_INFO_ENTITY_TYPE_PLAYLISTFILE       2
270 /* mpd_InfoEntity
271  * stores info on stuff returned info commands
272  */
273 typedef struct mpd_InfoEntity {
274         /* the type of entity, use with MPD_INFO_ENTITY_TYPE_* to determine
275          * what this entity is (song, directory, etc...)
276          */
277         int type;
278         /* the actual data you want, mpd_Song, mpd_Directory, etc */
279         union {
280                 mpd_Directory * directory;
281                 mpd_Song * song;
282                 mpd_PlaylistFile * playlistFile;
283         } info;
284 } mpd_InfoEntity;
286 mpd_InfoEntity * mpd_newInfoEntity();
288 void mpd_freeInfoEntity(mpd_InfoEntity * entity);
290 /* INFO COMMANDS AND STUFF */
292 /* use this function to loop over after calling Info/Listall functions */
293 mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection);
295 /* songNum of -1, means to display the whole list */
296 void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum);
298 void mpd_sendListallCommand(mpd_Connection * connection, const char * dir);
300 void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir);
302 void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir);
304 #define MPD_TABLE_ARTIST        0
305 #define MPD_TABLE_ALBUM         1
306 #define MPD_TABLE_TITLE         2
307 #define MPD_TABLE_FILENAME      3
309 void mpd_sendSearchCommand(mpd_Connection * connection, int table, 
310                 const char * str);
312 void mpd_sendFindCommand(mpd_Connection * connection, int table, 
313                 const char * str);
315 /* LIST TAG COMMANDS */
317 /* use this function fetch next artist entry, be sure to free the returned 
318  * string.  NULL means there are no more.  Best used with sendListArtists
319  */
320 char * mpd_getNextArtist(mpd_Connection * connection);
322 char * mpd_getNextAlbum(mpd_Connection * connection);
324 /* list artist or albums by artist, arg1 should be set to the artist if
325  * listing albums by a artist, otherwise NULL for listing all artists or albums
326  */
327 void mpd_sendListCommand(mpd_Connection * connection, int table, 
328                 const char * arg1);
330 void mpd_sendListAlbumsCommand(mpd_Connection * connection, 
331                 const char * artist);
333 /* SIMPLE COMMANDS */
335 void mpd_sendAddCommand(mpd_Connection * connection, const char * file);
337 void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum);
339 void mpd_sendSaveCommand(mpd_Connection * connection, const char * name);
341 void mpd_sendLoadCommand(mpd_Connection * connection, const char * name);
343 void mpd_sendRmCommand(mpd_Connection * connection, const char * name);
345 void mpd_sendShuffleCommand(mpd_Connection * connection);
347 void mpd_sendClearCommand(mpd_Connection * connection);
349 /* use this to start playing at the beginning, useful when in random mode */
350 #define MPD_PLAY_AT_BEGINNING   -1
352 void mpd_sendPlayCommand(mpd_Connection * connection, int songNum);
354 void mpd_sendStopCommand(mpd_Connection * connection);
356 void mpd_sendPauseCommand(mpd_Connection * connection);
358 void mpd_sendNextCommand(mpd_Connection * connection);
360 void mpd_sendPrevCommand(mpd_Connection * connection);
362 void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to);
364 void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2);
366 void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time);
368 void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode);
370 void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode);
372 void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange);
374 /* WARNING: don't use volume command, its depreacted */
375 void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange);
377 void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds);
379 void mpd_sendUpdateCommand(mpd_Connection * connection);
381 void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass);
383 /* after executing a command, when your done with it to get its status
384  * (you want to check connection->error for an error)
385  */
386 void mpd_finishCommand(mpd_Connection * connection);
388 /* command list stuff, use this to do things like add files very quickly */
389 void mpd_sendCommandListBegin(mpd_Connection * connection);
391 void mpd_sendCommandListEnd(mpd_Connection * connection);
393 #ifdef __cplusplus
395 #endif
397 #endif