| 13 | 13 | #include <audacious/drct.h> | 
  | 14 | 14 | #include <audacious/debug.h> | 
  | 15 | 15 | #include <audacious/misc.h> | 
  |  | 16 | #include <audacious/playlist.h> | 
  | 16 | 17 | #include <audacious/plugin.h> | 
  | 17 | 18 |  | 
  | 18 | 19 | #define SCROBBLER_CLI_ID "aud" | 
  | ... | ... |  | 
  | 48 | 49 |         sc_curl_errbuf[CURL_ERROR_SIZE], | 
  | 49 | 50 |         *sc_major_error; | 
  | 50 | 51 |  | 
  |  | 52 | static int sc_submit_np(Tuple *tuple, int len); | 
  | 51 | 53 | static void dump_queue(); | 
  | 52 | 54 |  | 
  | 53 | 55 | /**** Queue stuff ****/ | 
  | ... | ... |  | 
  | 63 | 65 |     int utctime, track, len; | 
  | 64 | 66 |     int timeplayed; | 
  | 65 | 67 |     int numtries; | 
  |  | 68 |     bool_t is_http_source; | 
  | 66 | 69 |     void *next; | 
  | 67 | 70 | } item_t; | 
  | 68 | 71 |  | 
  | ... | ... |  | 
  | 137 | 140 |     return newitem; | 
  | 138 | 141 | } | 
  | 139 | 142 |  | 
  | 140 |  | static item_t *create_item(Tuple *tuple, int len) | 
  |  | 143 | static item_t *create_item(Tuple *tuple, int len, bool_t is_http_source) | 
  | 141 | 144 | { | 
  | 142 | 145 |     item_t *item; | 
  | 143 | 146 |     gchar *album, *artist, *title; | 
  | ... | ... |  | 
  | 172 | 175 |     else | 
  | 173 | 176 |         item->album = fmt_escape(""); | 
  | 174 | 177 |  | 
  |  | 178 |     item->is_http_source = is_http_source; | 
  |  | 179 |  | 
  | 175 | 180 |     item->next = NULL; | 
  | 176 | 181 |  | 
  | 177 | 182 |     return item; | 
  | ... | ... |  | 
  | 181 | 186 | { | 
  | 182 | 187 |     item_t *item; | 
  | 183 | 188 |  | 
  | 184 |  |     if ((item = create_item (tuple, len)) == NULL) | 
  |  | 189 |     if (!(item = create_item(tuple, len, FALSE))) | 
  | 185 | 190 |         return NULL; | 
  | 186 | 191 |  | 
  | 187 | 192 |     item->timeplayed = len; | 
  | ... | ... |  | 
  | 190 | 195 |     return q_additem(item); | 
  | 191 | 196 | } | 
  | 192 | 197 |  | 
  | 193 |  | static item_t *set_np(Tuple *tuple, int len) | 
  |  | 198 | static item_t *set_np(Tuple *tuple, int len, bool_t is_http_source) | 
  | 194 | 199 | { | 
  | 195 | 200 |     if (np_item != NULL) | 
  | 196 | 201 |         q_item_free (np_item); | 
  | 197 | 202 |  | 
  | 198 |  |     if ((np_item = create_item (tuple, len)) != NULL) | 
  |  | 203 |     if ((np_item = create_item(tuple, len, is_http_source))) | 
  | 199 | 204 |         AUDDBG ("Tracking now-playing track: %s - %s\n", np_item->artist, | 
  | 200 | 205 |          np_item->title); | 
  | 201 | 206 |  | 
  | ... | ... |  | 
  | 266 | 271 | /* isn't there better way for that? --desowin */ | 
  | 267 | 272 | gboolean sc_timeout(gpointer data) | 
  | 268 | 273 | { | 
  | 269 |  |     if (np_item) | 
  |  | 274 |     if (!np_item) | 
  |  | 275 |         return TRUE; | 
  |  | 276 |  | 
  |  | 277 |     if (aud_drct_get_playing() && !aud_drct_get_paused()) | 
  |  | 278 |         np_item->timeplayed++; | 
  |  | 279 |  | 
  |  | 280 |     if (np_item->is_http_source) | 
  | 270 | 281 |     { | 
  | 271 |  |         if (aud_drct_get_playing() && !aud_drct_get_paused()) | 
  | 272 |  |             np_item->timeplayed+=1; | 
  |  | 282 |         /* This might be a stream, so check if the track info has changed. */ | 
  |  | 283 |         int playlist = aud_playlist_get_playing(); | 
  |  | 284 |         int pos = aud_playlist_get_position(playlist); | 
  |  | 285 |         Tuple *tuple = aud_playlist_entry_get_tuple(playlist, pos, FALSE); | 
  |  | 286 |  | 
  |  | 287 |         if (!tuple) | 
  |  | 288 |             return TRUE; | 
  |  | 289 |  | 
  |  | 290 |         int len = tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000; | 
  |  | 291 |  | 
  |  | 292 |         /* Make up a length when we submit streaming to now playing. | 
  |  | 293 |          * We will change it before we actually scrobble the track. */ | 
  |  | 294 |         if (len < 1) | 
  |  | 295 |             len = 240; | 
  |  | 296 |  | 
  |  | 297 |         item_t *current_item = create_item(tuple, len, TRUE); | 
  |  | 298 |  | 
  |  | 299 |         if (!current_item) | 
  |  | 300 |         { | 
  |  | 301 |             tuple_unref(tuple); | 
  |  | 302 |             return TRUE; | 
  |  | 303 |         } | 
  |  | 304 |  | 
  |  | 305 |         if (strcmp(np_item->artist, current_item->artist) | 
  |  | 306 |          || strcmp(np_item->title, current_item->title) | 
  |  | 307 |          || strcmp (np_item->album, current_item->album) | 
  |  | 308 |          || np_item->track != current_item->track) | 
  |  | 309 |         { | 
  |  | 310 |             /* Now set the real length of the track. */ | 
  |  | 311 |             np_item->len = np_item->timeplayed; | 
  |  | 312 |  | 
  |  | 313 |             AUDDBG ("Submitting\n"); | 
  |  | 314 |  | 
  |  | 315 |             q_additem(np_item); | 
  |  | 316 |             np_item = current_item; | 
  |  | 317 |             dump_queue(); | 
  |  | 318 |  | 
  |  | 319 |             sc_submit_np(tuple, len); | 
  |  | 320 |         } | 
  |  | 321 |         else | 
  |  | 322 |         { | 
  |  | 323 |             q_item_free(current_item); | 
  | 273 | 324 |  | 
  |  | 325 |             if (np_item->timeplayed > np_item->len) | 
  |  | 326 |             { | 
  |  | 327 |                 /* Submit the track again to make sure it stays on now playing. | 
  |  | 328 |                  * This doubles the total length we have submitted. */ | 
  |  | 329 |                 sc_submit_np(tuple, np_item->len); | 
  |  | 330 |                 np_item->len *= 2; | 
  |  | 331 |             } | 
  |  | 332 |         } | 
  |  | 333 |  | 
  |  | 334 |         tuple_unref(tuple); | 
  |  | 335 |     } | 
  |  | 336 |     else | 
  |  | 337 |     { | 
  | 274 | 338 |         /* | 
  | 275 | 339 |          * Check our now-playing track to see if it should go into the queue | 
  | 276 | 340 |          */ | 
  | ... | ... |  | 
  | 288 | 352 |     return TRUE; | 
  | 289 | 353 | } | 
  | 290 | 354 |  | 
  |  | 355 | void sc_playback_end(void) | 
  |  | 356 | { | 
  |  | 357 |     if (np_item && np_item->is_http_source) | 
  |  | 358 |     { | 
  |  | 359 |         /* Now set the real length of the track. */ | 
  |  | 360 |         np_item->len = np_item->timeplayed; | 
  |  | 361 |  | 
  |  | 362 |         AUDDBG ("Submitting\n"); | 
  |  | 363 |  | 
  |  | 364 |         q_additem(np_item); | 
  |  | 365 |         np_item = NULL; | 
  |  | 366 |         dump_queue(); | 
  |  | 367 |     } | 
  |  | 368 | } | 
  |  | 369 |  | 
  | 291 | 370 | /* Error functions */ | 
  | 292 | 371 |  | 
  | 293 | 372 | static void sc_throw_error(char *errortxt) | 
  | ... | ... |  | 
  | 778 | 857 |     return i; | 
  | 779 | 858 | } | 
  | 780 | 859 |  | 
  | 781 |  | static int sc_submit_np(Tuple *tuple) | 
  |  | 860 | static int sc_submit_np(Tuple *tuple, int len) | 
  | 782 | 861 | { | 
  | 783 | 862 |     CURL *curl; | 
  | 784 | 863 |     /* struct HttpPost *post = NULL , *last = NULL; */ | 
  | ... | ... |  | 
  | 810 | 889 |         field_artist, | 
  | 811 | 890 |         field_title, | 
  | 812 | 891 |         field_album, | 
  | 813 |  |         tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000, | 
  |  | 892 |         len, | 
  | 814 | 893 |         tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL)); | 
  | 815 | 894 |         curl_free(field_artist); | 
  | 816 | 895 |         curl_free(field_title); | 
  | ... | ... |  | 
  | 1126 | 1205 |     AUDDBG("scrobbler starting up\n"); | 
  | 1127 | 1206 | } | 
  | 1128 | 1207 |  | 
  | 1129 |  | void sc_addentry(GMutex *mutex, Tuple *tuple, int len) | 
  |  | 1208 | void sc_addentry(GMutex *mutex, Tuple *tuple, int len, bool_t is_http_source) | 
  | 1130 | 1209 | { | 
  | 1131 | 1210 |     g_mutex_lock(mutex); | 
  | 1132 | 1211 |  | 
  | 1133 |  |     sc_submit_np(tuple); | 
  | 1134 |  |     set_np(tuple, len); | 
  |  | 1212 |     sc_submit_np(tuple, len); | 
  |  | 1213 |     set_np(tuple, len, is_http_source); | 
  | 1135 | 1214 |  | 
  | 1136 | 1215 |     /* | 
  | 1137 | 1216 |      * This will help make sure the queue will be saved on a nasty |