updated on Wed Jan 25 08:34:36 UTC 2012
[aur-mirror.git] / mpd-pausemode / pausemode.c
blob582ffeac1412b925fc81a00eff06cd283d4b11bc
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
20 THE SOFTWARE.
21 *******************************************************************************/
23 #include <libmpdclient.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #define _XOPEN_SOURCE 500
30 #include <unistd.h>
31 #include <signal.h>
33 static int cont;
34 static int pausemode;
36 void signal_quit(int signal){
37 cont = 0;
38 printf("quit\n");
41 void signal_setpause(int signal){
42 pausemode = 1;
43 printf("set pause on %i\n", pausemode);
46 void signal_unsetpause(int signal){
47 pausemode = 0;
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) {
60 mpd_Connection *cnx;
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);
75 if(cnx->error) {
76 fprintf(stderr, "connexion error: %s\n", cnx->errorStr);
77 mpd_closeConnection(cnx);
78 return 1;
81 char* current_song = NULL;
82 int current_mode = MPD_STATUS_STATE_PLAY;
83 int elapsed_time = 0;
84 int total_time = 0;
86 cont = 1;
87 pausemode = (argc >= 2) ? atoi(argv[1]) : 1;
89 int i = 0;
90 while(cont){
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);
108 return 1;
111 mpd_sendCurrentSongCommand(cnx);
112 mpd_nextListOkCommand(cnx);
114 int do_pause = 0;
116 while((entity = mpd_getNextInfoEntity(cnx))){
118 if(entity->type != MPD_INFO_ENTITY_TYPE_SONG){
119 mpd_freeInfoEntity(entity);
120 continue;
123 const char* new_song = entity->info.song->file;
124 //printf("current song: %s\n", new_song ? new_song : "NULL");
126 if(!new_song){
127 if(current_song) free(current_song);
128 current_song = NULL;
129 }else if(!current_song || strcmp(current_song, new_song)){
130 printf("new song: %s\n", new_song ? new_song : "NULL");
131 do_pause =
132 pausemode &&
133 current_song &&
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;
152 if(cnx->error) {
153 fprintf(stderr, "error: %s\n", cnx->errorStr);
154 mpd_closeConnection(cnx);
155 return 255;
158 if(do_pause){
159 printf("pause\n");
160 mpd_sendPauseCommand(cnx, 1);
161 if(cnx->error) {
162 fprintf(stderr, "pause error: %s\n", cnx->errorStr);
163 mpd_closeConnection(cnx);
164 return 255;
168 mpd_finishCommand(cnx);
170 if(cnx->error) {
171 fprintf(stderr, "finnish command error: %s\n", cnx->errorStr);
172 mpd_closeConnection(cnx);
173 return 255;
176 mpd_freeStatus(status);
178 usleep(SLEEP_STEP);
182 if(current_song) free(current_song);
184 mpd_closeConnection(cnx);
185 return 0;
189 /* kate: indent-width 4; space-indent on; replace-tabs off;
190 * kate: replace-tabs-save off; tab-width 8; remove-trailing-space on; */