Correct the BBX clause (no visual changes)
[kugel-rb/myfork.git] / apps / scrobbler.c
blobb0f65158e8521f9a4bf8fe7e0dcc5b212098f8ec
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006-2008 Robert Keevil
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 Audioscrobbler spec at:
23 http://www.audioscrobbler.net/wiki/Portable_Player_Logging
26 #include "file.h"
27 #include "sprintf.h"
28 #include "playback.h"
29 #include "logf.h"
30 #include "metadata.h"
31 #include "kernel.h"
32 #include "audio.h"
33 #include "buffer.h"
34 #include "settings.h"
35 #include "ata_idle_notify.h"
36 #include "misc.h"
37 #include "appevents.h"
39 #if CONFIG_RTC
40 #include "time.h"
41 #include "timefuncs.h"
42 #endif
44 #include "scrobbler.h"
46 #define SCROBBLER_VERSION "1.1"
48 #if CONFIG_RTC
49 #define SCROBBLER_FILE "/.scrobbler.log"
50 #else
51 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
52 #endif
54 /* increment this on any code change that effects output */
55 #define SCROBBLER_REVISION " $Revision$"
57 #define SCROBBLER_MAX_CACHE 32
58 /* longest entry I've had is 323, add a safety margin */
59 #define SCROBBLER_CACHE_LEN 512
61 static char* scrobbler_cache;
63 static int cache_pos;
64 static struct mp3entry scrobbler_entry;
65 static bool pending = false;
66 static bool scrobbler_initialised = false;
67 #if CONFIG_RTC
68 static time_t timestamp;
69 #else
70 static unsigned long timestamp;
71 #endif
73 /* Crude work-around for Archos Sims - return a set amount */
74 #if (CONFIG_CODEC != SWCODEC) && defined(SIMULATOR)
75 unsigned long audio_prev_elapsed(void)
77 return 120000;
79 #endif
81 static void write_cache(void)
83 int i;
84 int fd;
86 /* If the file doesn't exist, create it.
87 Check at each write since file may be deleted at any time */
88 if(!file_exists(SCROBBLER_FILE))
90 fd = open(SCROBBLER_FILE, O_RDWR | O_CREAT);
91 if(fd >= 0)
93 fdprintf(fd, "#AUDIOSCROBBLER/" SCROBBLER_VERSION "\n"
94 "#TZ/UNKNOWN\n"
95 #if CONFIG_RTC
96 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION "\n");
97 #else
98 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION " Timeless\n");
99 #endif
101 close(fd);
103 else
105 logf("SCROBBLER: cannot create log file");
109 /* write the cache entries */
110 fd = open(SCROBBLER_FILE, O_WRONLY | O_APPEND);
111 if(fd >= 0)
113 logf("SCROBBLER: writing %d entries", cache_pos);
115 for ( i=0; i < cache_pos; i++ )
117 logf("SCROBBLER: write %d", i);
118 fdprintf(fd, "%s", scrobbler_cache+(SCROBBLER_CACHE_LEN*i));
120 close(fd);
122 else
124 logf("SCROBBLER: error writing file");
127 /* clear even if unsuccessful - don't want to overflow the buffer */
128 cache_pos = 0;
131 static bool scrobbler_flush_callback(void)
133 if (scrobbler_initialised && cache_pos)
134 write_cache();
135 return true;
138 static void add_to_cache(unsigned long play_length)
140 if ( cache_pos >= SCROBBLER_MAX_CACHE )
141 write_cache();
143 int ret;
144 char rating = 'S'; /* Skipped */
146 logf("SCROBBLER: add_to_cache[%d]", cache_pos);
148 if ( play_length > (scrobbler_entry.length/2) )
149 rating = 'L'; /* Listened */
151 if (scrobbler_entry.tracknum > 0)
153 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
154 SCROBBLER_CACHE_LEN,
155 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\t%s\n",
156 scrobbler_entry.artist,
157 scrobbler_entry.album?scrobbler_entry.album:"",
158 scrobbler_entry.title,
159 scrobbler_entry.tracknum,
160 (int)scrobbler_entry.length/1000,
161 rating,
162 (long)timestamp,
163 scrobbler_entry.mb_track_id?scrobbler_entry.mb_track_id:"");
164 } else {
165 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
166 SCROBBLER_CACHE_LEN,
167 "%s\t%s\t%s\t\t%d\t%c\t%ld\t%s\n",
168 scrobbler_entry.artist,
169 scrobbler_entry.album?scrobbler_entry.album:"",
170 scrobbler_entry.title,
171 (int)scrobbler_entry.length/1000,
172 rating,
173 (long)timestamp,
174 scrobbler_entry.mb_track_id?scrobbler_entry.mb_track_id:"");
177 if ( ret >= SCROBBLER_CACHE_LEN )
179 logf("SCROBBLER: entry too long:");
180 logf("SCROBBLER: %s", scrobbler_entry.path);
181 } else {
182 cache_pos++;
183 register_storage_idle_func(scrobbler_flush_callback);
188 void scrobbler_change_event(struct mp3entry *id)
190 /* add entry using the previous scrobbler_entry and timestamp */
191 if (pending)
192 add_to_cache(audio_prev_elapsed());
194 /* check if track was resumed > %50 played
195 check for blank artist or track name */
196 if ((id->elapsed > (id->length/2)) ||
197 (!id->artist ) || (!id->title ) )
199 pending = false;
200 logf("SCROBBLER: skipping file %s", id->path);
202 else
204 logf("SCROBBLER: add pending");
205 copy_mp3entry(&scrobbler_entry, id);
206 #if CONFIG_RTC
207 timestamp = mktime(get_time());
208 #else
209 timestamp = 0;
210 #endif
211 pending = true;
215 int scrobbler_init(void)
217 logf("SCROBBLER: init %d", global_settings.audioscrobbler);
219 if(!global_settings.audioscrobbler)
220 return -1;
222 scrobbler_cache = buffer_alloc(SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
224 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, scrobbler_change_event);
225 cache_pos = 0;
226 pending = false;
227 scrobbler_initialised = true;
229 return 1;
232 void scrobbler_flush_cache(void)
234 if (scrobbler_initialised)
236 /* Add any pending entries to the cache */
237 if(pending)
238 add_to_cache(audio_prev_elapsed());
240 /* Write the cache to disk if needed */
241 if (cache_pos)
242 write_cache();
244 pending = false;
248 void scrobbler_shutdown(void)
250 scrobbler_flush_cache();
252 if (scrobbler_initialised)
254 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, scrobbler_change_event);
255 scrobbler_initialised = false;
259 void scrobbler_poweroff(void)
261 if (scrobbler_initialised && pending)
263 if ( audio_status() )
264 add_to_cache(audio_current_track()->elapsed);
265 else
266 add_to_cache(audio_prev_elapsed());
268 /* scrobbler_shutdown is called later, the cache will be written
269 * make sure the final track isn't added twice when that happens */
270 pending = false;
274 bool scrobbler_is_enabled(void)
276 return scrobbler_initialised;