move x outside a loop, its value has to be in memory
[scrobby.git] / src / callback.cpp
blobbaccad0a3421adcd82158e976e7c9bd500b02298
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <curl/curl.h>
22 #include <cstring>
24 #include "callback.h"
25 #include "misc.h"
26 #include "scrobby.h"
27 #include "song.h"
29 using std::string;
31 MPD::State old_state = MPD::psUnknown;
32 MPD::State current_state = MPD::psUnknown;
34 extern Handshake handshake;
35 extern MPD::Song s;
37 extern pthread_mutex_t curl_lock;
38 extern pthread_mutex_t handshake_lock;
40 extern bool notify_about_now_playing;
42 void ScrobbyErrorCallback(MPD::Connection *, int, string errormessage, void *)
44 ignore_newlines(errormessage);
45 Log(llVerbose, "MPD: %s", errormessage.c_str());
48 void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void *)
50 if (changed.State)
52 old_state = current_state;
53 current_state = Mpd->GetState();
54 if (old_state == MPD::psStop && current_state == MPD::psPlay)
55 changed.SongID = 1;
57 if (changed.ElapsedTime)
59 static int crossfade;
60 if (Mpd->GetElapsedTime() == ((crossfade = Mpd->GetCrossfade()) ? crossfade : 0 ))
61 changed.SongID = 1;
62 s.Playback()++;
64 if (changed.SongID || (old_state == MPD::psPlay && current_state == MPD::psStop))
66 s.Submit();
68 // in this case allow entering only once
69 if (old_state == MPD::psPlay && current_state == MPD::psStop)
70 old_state = MPD::psUnknown;
72 if (Mpd->GetElapsedTime() < Mpd->GetCrossfade()+5)
73 s.SetStartTime();
75 if (current_state == MPD::psPlay || current_state == MPD::psPause)
77 s.SetData(Mpd->CurrentSong());
78 notify_about_now_playing = s.Data() && !s.isStream();
81 if (notify_about_now_playing)
83 pthread_mutex_lock(&handshake_lock);
84 if (s.Data() && (!s.Data()->artist || !s.Data()->title))
86 Log(llInfo, "Playing song with missing tags detected.");
88 else if (s.Data() && s.Data()->time <= 0)
90 Log(llInfo, "Playing song with unknown length detected.");
92 else if (s.Data() && s.Data()->artist && s.Data()->title)
94 Log(llVerbose, "Playing song detected: %s - %s", s.Data()->artist, s.Data()->title);
96 if (handshake.status == "OK" && !handshake.nowplaying_url.empty())
98 Log(llInfo, "Sending now playing notification...");
100 else
102 Log(llInfo, "Notification not sent due to problem with connection.");
103 goto NOTIFICATION_FAILED;
106 string result, postdata;
107 CURLcode code;
109 pthread_mutex_lock(&curl_lock);
110 CURL *np_notification = curl_easy_init();
112 char *c_artist = curl_easy_escape(np_notification, s.Data()->artist, 0);
113 char *c_title = curl_easy_escape(np_notification, s.Data()->title, 0);
114 char *c_album = s.Data()->album ? curl_easy_escape(np_notification, s.Data()->album, 0) : NULL;
115 char *c_track = s.Data()->track ? curl_easy_escape(np_notification, s.Data()->track, 0) : NULL;
117 postdata = "s=";
118 postdata += handshake.session_id;
119 postdata += "&a=";
120 postdata += c_artist;
121 postdata += "&t=";
122 postdata += c_title;
123 postdata += "&b=";
124 if (c_album)
125 postdata += c_album;
126 postdata += "&l=";
127 postdata += IntoStr(s.Data()->time);
128 postdata += "&n=";
129 if (c_track)
130 postdata += c_track;
131 postdata += "&m=";
133 curl_free(c_artist);
134 curl_free(c_title);
135 curl_free(c_album);
136 curl_free(c_track);
138 Log(llVerbose, "URL: %s", handshake.nowplaying_url.c_str());
139 Log(llVerbose, "Post data: %s", postdata.c_str());
141 curl_easy_setopt(np_notification, CURLOPT_URL, handshake.nowplaying_url.c_str());
142 curl_easy_setopt(np_notification, CURLOPT_POST, 1);
143 curl_easy_setopt(np_notification, CURLOPT_POSTFIELDS, postdata.c_str());
144 curl_easy_setopt(np_notification, CURLOPT_WRITEFUNCTION, write_data);
145 curl_easy_setopt(np_notification, CURLOPT_WRITEDATA, &result);
146 curl_easy_setopt(np_notification, CURLOPT_CONNECTTIMEOUT, curl_timeout);
147 code = curl_easy_perform(np_notification);
148 curl_easy_cleanup(np_notification);
149 pthread_mutex_unlock(&curl_lock);
151 ignore_newlines(result);
153 if (result == "OK")
155 Log(llInfo, "Notification about currently playing song sent.");
157 else
159 if (result.empty())
161 Log(llInfo, "Error while sending notification: %s", curl_easy_strerror(code));
163 else
165 Log(llInfo, "Audioscrobbler returned status %s", result.c_str());
167 goto NOTIFICATION_FAILED;
170 if (0)
172 NOTIFICATION_FAILED:
174 handshake.Clear(); // handshake probably failed if we are here, so reset it
175 Log(llVerbose, "Handshake status reset");
177 pthread_mutex_unlock(&handshake_lock);
178 notify_about_now_playing = 0;