1 /*******************************************************************************
2 Copyright (c) 2007 Mildred <silkensedai@online.fr>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *******************************************************************************/
23 #include <libmpdclient.h>
29 #define _XOPEN_SOURCE 500
36 void signal_quit(int signal
){
41 void signal_setpause(int signal
){
43 printf("set pause on %i\n", pausemode
);
46 void signal_unsetpause(int signal
){
48 printf("set pause off %i\n", pausemode
);
51 void signal_togglepause(int signal
){
52 pausemode
= !pausemode
;
53 printf("set pause toggle %i\n", pausemode
);
56 #define is_end_of_song(elapsed_time,total_time) ((elapsed_time)+2>(total_time))
57 #define SLEEP_STEP 100
59 int main(int argc
, char **argv
) {
62 char* hostname
= getenv("MPD_HOST");
63 char* port
= getenv("MPD_PORT");
64 if(!hostname
) hostname
= "localhost";
65 if(!port
) port
= "6600";
67 sigset(SIGINT
, signal_quit
);
68 sigset(SIGQUIT
, signal_quit
);
69 sigset(SIGUSR1
, signal_setpause
);
70 sigset(SIGUSR2
, signal_unsetpause
);
71 sigset(SIGHUP
, signal_togglepause
);
73 cnx
= mpd_newConnection(hostname
, atoi(port
), 10);
76 fprintf(stderr
, "connexion error: %s\n", cnx
->errorStr
);
77 mpd_closeConnection(cnx
);
81 char* current_song
= NULL
;
82 int current_mode
= MPD_STATUS_STATE_PLAY
;
87 pausemode
= (argc
>= 2) ? atoi(argv
[1]) : 1;
92 //printf("current song[1]: %s\n", current_song ? current_song : "NULL");
94 mpd_Status
* status
= NULL
;
95 mpd_InfoEntity
* entity
= NULL
;
97 //mpd_sendCommandListOkBegin(cnx);
98 //mpd_sendStatusCommand(cnx);
99 //mpd_sendCurrentSongCommand(cnx);
100 //mpd_sendCommandListEnd(cnx);
102 mpd_sendStatusCommand(cnx
);
103 mpd_nextListOkCommand(cnx
);
105 if(!(status
= mpd_getStatus(cnx
))) {
106 fprintf(stderr
, "status error: %s\n", cnx
->errorStr
);
107 mpd_closeConnection(cnx
);
111 mpd_sendCurrentSongCommand(cnx
);
112 mpd_nextListOkCommand(cnx
);
116 while((entity
= mpd_getNextInfoEntity(cnx
))){
118 if(entity
->type
!= MPD_INFO_ENTITY_TYPE_SONG
){
119 mpd_freeInfoEntity(entity
);
123 const char* new_song
= entity
->info
.song
->file
;
124 //printf("current song: %s\n", new_song ? new_song : "NULL");
127 if(current_song
) free(current_song
);
129 }else if(!current_song
|| strcmp(current_song
, new_song
)){
130 printf("new song: %s\n", new_song
? new_song
: "NULL");
134 current_mode
== MPD_STATUS_STATE_PLAY
&&
135 is_end_of_song(elapsed_time
, total_time
);
136 printf("song: %i/%i (end of song: %i)\n",
137 elapsed_time
, total_time
, do_pause
);
138 size_t len
= strlen(new_song
);
139 if(current_song
) free(current_song
);
140 current_song
= malloc(len
+1);
141 memcpy(current_song
, new_song
, len
+1);
144 mpd_freeInfoEntity(entity
);
148 current_mode
= status
->state
;
149 elapsed_time
= status
->elapsedTime
;
150 total_time
= status
->totalTime
;
153 fprintf(stderr
, "error: %s\n", cnx
->errorStr
);
154 mpd_closeConnection(cnx
);
160 mpd_sendPauseCommand(cnx
, 1);
162 fprintf(stderr
, "pause error: %s\n", cnx
->errorStr
);
163 mpd_closeConnection(cnx
);
168 mpd_finishCommand(cnx
);
171 fprintf(stderr
, "finnish command error: %s\n", cnx
->errorStr
);
172 mpd_closeConnection(cnx
);
176 mpd_freeStatus(status
);
182 if(current_song
) free(current_song
);
184 mpd_closeConnection(cnx
);
189 /* kate: indent-width 4; space-indent on; replace-tabs off;
190 * kate: replace-tabs-save off; tab-width 8; remove-trailing-space on; */