Don't require a configured inet interface when resolving names
[libmpd.git] / example / testcase / testcase.c
bloba7bbf24ec9fb9293a8c3a960710d656c2616705c
1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <libmpd/libmpd.h>
28 #include <libmpd/debug_printf.h>
29 #define RED "\x1b[31;01m"
30 #define DARKRED "\x1b[31;06m"
31 #define RESET "\x1b[0m"
32 #define GREEN "\x1b[32;06m"
33 #define YELLOW "\x1b[33;06m"
35 extern int debug_level;
36 void error_callback(MpdObj *mi,int errorid, char *msg, void *userdata)
38 printf(RED"Error "RESET""GREEN"%i:"RESET" '%s'\n", errorid, msg);
41 void status_changed(MpdObj *mi, ChangedStatusType what)
43 if(what&MPD_CST_SONGID)
45 mpd_Song *song = mpd_playlist_get_current_song(mi);
46 if(song)
48 printf(GREEN"Song:"RESET" %s - %s\n", song->artist, song->title);
52 if(what&MPD_CST_STATE)
54 printf(GREEN"State:"RESET);
55 switch(mpd_player_get_state(mi))
57 case MPD_PLAYER_PLAY:
58 printf("Playing\n");
59 break;
60 case MPD_PLAYER_PAUSE:
61 printf("Paused\n");
62 break;
63 case MPD_PLAYER_STOP:
64 printf("Stopped\n");
65 break;
66 default:
67 break;
70 if(what&MPD_CST_REPEAT){
71 printf(GREEN"Repeat:"RESET" %s\n", mpd_player_get_repeat(mi)? "On":"Off");
73 if(what&MPD_CST_RANDOM){
74 printf(GREEN"Random:"RESET" %s\n", mpd_player_get_random(mi)? "On":"Off");
76 if(what&MPD_CST_VOLUME){
77 printf(GREEN"Volume:"RESET" %03i%%\n",
78 mpd_status_get_volume(mi));
80 if(what&MPD_CST_CROSSFADE){
81 printf(GREEN"X-Fade:"RESET" %i sec.\n",
82 mpd_status_get_crossfade(mi));
84 if(what&MPD_CST_UPDATING)
86 if(mpd_status_db_is_updating(mi))
88 printf(GREEN"Started updating DB"RESET"\n");
90 else
92 printf(GREEN"Updating DB finished"RESET"\n");
95 if(what&MPD_CST_DATABASE)
97 printf(GREEN"Databased changed"RESET"\n");
99 if(what&MPD_CST_PLAYLIST)
101 printf(GREEN"Playlist changed"RESET"\n");
103 /* not yet implemented signals */
104 if(what&MPD_CST_AUDIO){
105 printf(GREEN"Audio Changed"RESET"\n");
107 if(what&MPD_CST_TOTAL_TIME){
108 printf(GREEN"Total song time changed:"RESET" %02i:%02i\n",
109 mpd_status_get_total_song_time(mi)/60,
110 mpd_status_get_total_song_time(mi)%60);
112 if(what&MPD_CST_ELAPSED_TIME){
113 /* printf(GREEN"Time elapsed changed:"RESET" %02i:%02i\n",
114 mpd_status_get_elapsed_song_time(mi)/60,
115 mpd_status_get_elapsed_song_time(mi)%60);
116 */ }
117 if(what&MPD_CST_PERMISSION){
118 printf(YELLOW"Permission:"RESET" Changed\n");
122 int main(int argc, char **argv)
124 int fdstdin = 0;
125 int run = 1, iport = 6600;
126 char *hostname = getenv("MPD_HOST");
127 char *port = getenv("MPD_PORT");
128 char *password = getenv("MPD_PASSWORD");
129 MpdObj *obj = NULL;
130 /* Make the input non blocking */
131 fdstdin = open("/dev/stdin", O_NONBLOCK|O_RDONLY);
132 /* set correct hostname */
133 if(!hostname) {
134 hostname = "localhost";
136 if(port){
137 iport = atoi(port);
139 /* Create mpd object */
140 obj = mpd_new(hostname, iport,password);
141 /* Connect signals */
142 mpd_signal_connect_error(obj,(ErrorCallback)error_callback, NULL);
143 mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed, NULL);
144 /* Set timeout */
145 mpd_set_connection_timeout(obj, 10);
147 if(!mpd_connect(obj))
149 char buffer[20];
150 mpd_send_password(obj);
151 memset(buffer, '\0', 20);
153 if(read(fdstdin, buffer, 1) > 0)
155 switch(buffer[0])
157 case '\n':
158 break;
159 case 'b':
160 mpd_player_next(obj);
161 break;
162 case 'z':
163 mpd_player_prev(obj);
164 break;
165 case 'x':
166 mpd_player_play(obj);
167 break;
168 case 'c':
169 mpd_player_pause(obj);
170 break;
171 case 'v':
172 mpd_player_stop(obj);
173 break;
174 case 'q':
175 run = 0;
176 printf("Quitting....\n");
177 break;
178 case 'r':
179 mpd_player_set_repeat(obj, !mpd_player_get_repeat(obj));
180 break;
181 case 's':
182 mpd_player_set_random(obj, !mpd_player_get_random(obj));
183 break;
184 case 'l':
186 MpdData *data = mpd_playlist_get_changes(obj,-1);
187 if(data)
189 printf(GREEN"Playlist:"RESET"\n");
192 if(data->type == MPD_DATA_TYPE_SONG)
194 printf(GREEN"%i"RESET": %s - %s\n", data->song->id,
195 data->song->artist,
196 data->song->title);
198 data = mpd_data_get_next(data);
199 }while(data);
201 break;
203 case 'p':
204 memset(buffer, '\0',20);
205 if(read(fdstdin,buffer, 20))
207 int id = atoi(buffer);
208 printf(GREEN"Playing:"RESET" %i\n", id);
209 mpd_player_play_id(obj,id);
211 break;
212 case 'a': /*authentificate */
213 memset(buffer, '\0',20);
214 if(read(fdstdin,buffer, 20))
216 int i;
218 for(i=0;i<20;i++)
220 if(buffer[i] == '\n')buffer[i] = '\0';
222 printf(GREEN"Authentificating:"RESET" \"%s\"\n", buffer);
223 mpd_set_password(obj,buffer);
224 mpd_send_password(obj);
225 printf(RED"permisssion:"RESET" %i\n", mpd_server_check_command_allowed(obj, "next"));
227 break;
228 case '+':
229 mpd_status_set_volume(obj, mpd_status_get_volume(obj)+5);
230 break;
231 case '-':
232 mpd_status_set_volume(obj, mpd_status_get_volume(obj)-5);
233 break;
234 case 'd':
235 debug_level = (debug_level > 0)?0:3;
236 printf(YELLOW"Debug:"RESET" %s\n", (debug_level >0)? "Enabled":"Disabled");
237 break;
238 case 'h':
239 printf("\th:\t\tHelp\n"\
240 "\td:\t\tToggle debug on/off\n"\
241 "\t+:\t\tIncrease volume\n"\
242 "\t-:\t\tDecrease volume\n"\
243 "\ta <pass>:\t Authentificate with pass\n"\
244 "\tp <id>:\t Play song with id\n"\
245 "\tl:\t\tList the playlist\n"\
246 "\ts:\t\tToggle shuffle mode\n"\
247 "\tr:\t\tToggle repeat\n"\
248 "\tq:\t\tQuit\n"\
249 "\tv:\t\tStop\n"\
250 "\tc:\t\tPause\n"\
251 "\tx:\t\tPlay\n"\
252 "\tz:\t\tPrevious\n"\
253 "\tb:\t\tNext\n");
254 break;
255 default:
256 printf("buffer: %s\n", buffer);
261 mpd_status_update(obj);
262 memset(buffer, '\0', 20);
263 }while(!usleep(100000) && run);
265 mpd_free(obj);
266 close(fdstdin);
267 return 1;