updated on Sun Jan 22 16:00:49 UTC 2012
[aur-mirror.git] / mpd-notify / mpd-notify.c
blob956f432cb8d3ba13f0fb348958096bd5cb51f9c6
1 /**********************************************************
2 MPD-NOTIFY
3 # By Msdark (msdark@archilinux.us)
5 # Copyright (C) 2008 Matías Hernández
8 # Licensed under the GNU General Public License Version 3
9 # Ver LICENSE
10 **********************************************************/
12 #include "libmpdclient.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <stdlib.h>
18 mpd_Connection * conn;
19 int reconectar(char *hostname,char *port){
20 printf("Desconectando....\n");
21 mpd_closeConnection(conn);
22 conn = 0;
23 printf("Reconectando...\n");
24 sleep(15);
25 conn = mpd_newConnection(hostname,atoi(port),10);
26 if(conn->error){
27 mpd_closeConnection(conn);
28 return -1;
30 printf("Conectado a MPD\n");
31 return 0;
34 int conectar(char *hostname,char *port){
35 conn = 0;
36 conn = mpd_newConnection(hostname,atoi(port),10);
37 if(conn->error){
38 mpd_closeConnection(conn);
39 return -1;
41 return 0;
44 int main(){
45 char *hostname,*port;
46 char info[500]="NULL",notify[41]="/usr/bin/notify-send -t 2000 -u normal ";
47 char instruccion[1000];
48 hostname = getenv("MPD_HOST");
49 port = getenv("MPD_PORT");
50 int elapsed;
51 int last_id = -1;
52 mpd_Status *status;
53 conectar(hostname,port);
54 while(1){
55 mpd_sendCommandListOkBegin(conn);
56 mpd_sendStatusCommand(conn);
57 mpd_sendCurrentSongCommand(conn);
58 mpd_sendCommandListEnd(conn);
60 if((status = mpd_getStatus(conn))==NULL){
61 mpd_finishCommand(conn);
62 reconectar(hostname,port);
63 printf("\tMPD Stopped\n");
64 continue;
66 if(status->state==MPD_STATUS_STATE_PAUSE){
67 printf("Esta pausado\n");
68 continue;
69 }else{
70 if(status->state!=MPD_STATUS_STATE_PLAY){
71 printf("Esta detenido\n");
72 continue;
75 mpd_freeStatus(status);
76 mpd_nextListOkCommand(conn);
77 mpd_InfoEntity *entity;
78 entity = mpd_getNextInfoEntity(conn);
79 mpd_Song *song = entity->info.song;
80 if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG){
81 mpd_freeInfoEntity(entity);
82 continue;
84 if(song->id != last_id){
85 if(song->artist && song->title){
86 strcpy(info,"\"");
87 strcat(info,song->artist);
88 strcat(info,"\n");
89 if(song->album){
90 strcat(info,"Album: ");
91 strcat(info,song->album);
92 strcat(info,"\n");
94 strcat(info,"Tema: ");
95 strcat(info,song->title);
96 strcat(info,"\"");
97 strcpy(instruccion,notify);
98 strcat(instruccion,info);
100 system(instruccion);
101 }else{
102 strcpy(instruccion,notify);
103 strcat(instruccion,"\"");
104 strcat(instruccion,song->file);
105 strcat(instruccion,"\"");
106 system(instruccion);
108 last_id = song->id;
110 mpd_freeInfoEntity(entity);
111 mpd_finishCommand(conn);
114 mpd_closeConnection(conn);
115 return 0;