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.
24 #include <sys/types.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
);
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
))
60 case MPD_PLAYER_PAUSE
:
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");
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);
117 if(what
&MPD_CST_PERMISSION
){
118 printf(YELLOW
"Permission:"RESET
" Changed\n");
122 int main(int argc
, char **argv
)
125 int run
= 1, iport
= 6600;
126 char *hostname
= getenv("MPD_HOST");
127 char *port
= getenv("MPD_PORT");
128 char *password
= getenv("MPD_PASSWORD");
130 /* Make the input non blocking */
131 fdstdin
= open("/dev/stdin", O_NONBLOCK
|O_RDONLY
);
132 /* set correct hostname */
134 hostname
= "localhost";
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
);
145 mpd_set_connection_timeout(obj
, 10);
147 if(!mpd_connect(obj
))
150 mpd_send_password(obj
);
151 memset(buffer
, '\0', 20);
153 if(read(fdstdin
, buffer
, 1) > 0)
160 mpd_player_next(obj
);
163 mpd_player_prev(obj
);
166 mpd_player_play(obj
);
169 mpd_player_pause(obj
);
172 mpd_player_stop(obj
);
176 printf("Quitting....\n");
179 mpd_player_set_repeat(obj
, !mpd_player_get_repeat(obj
));
182 mpd_player_set_random(obj
, !mpd_player_get_random(obj
));
186 MpdData
*data
= mpd_playlist_get_changes(obj
,-1);
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
,
198 data
= mpd_data_get_next(data
);
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
);
212 case 'a': /*authentificate */
213 memset(buffer
, '\0',20);
214 if(read(fdstdin
,buffer
, 20))
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"));
229 mpd_status_set_volume(obj
, mpd_status_get_volume(obj
)+5);
232 mpd_status_set_volume(obj
, mpd_status_get_volume(obj
)-5);
235 debug_level
= (debug_level
> 0)?0:3;
236 printf(YELLOW
"Debug:"RESET
" %s\n", (debug_level
>0)? "Enabled":"Disabled");
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"\
252 "\tz:\t\tPrevious\n"\
256 printf("buffer: %s\n", buffer
);
261 mpd_status_update(obj
);
262 memset(buffer
, '\0', 20);
263 }while(!usleep(100000) && run
);