1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Robert Keevil
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 Audioscrobbler spec at:
21 http://www.audioscrobbler.net/wiki/Portable_Player_Logging
33 #include "ata_idle_notify.h"
38 #include "timefuncs.h"
41 #include "scrobbler.h"
43 #define SCROBBLER_VERSION "1.0"
46 #define SCROBBLER_FILE "/.scrobbler.log"
48 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
51 /* increment this on any code change that effects output */
52 #define SCROBBLER_REVISION " $Revision$"
54 #define SCROBBLER_MAX_CACHE 32
55 /* longest entry I've had is 323, add a safety margin */
56 #define SCROBBLER_CACHE_LEN 512
58 static char* scrobbler_cache
;
61 static struct mp3entry scrobbler_entry
;
62 static bool pending
= false;
63 static bool scrobbler_initialised
= false;
64 static bool scrobbler_ata_callback
= false;
66 static time_t timestamp
;
68 static unsigned long timestamp
;
71 /* Crude work-around for Archos Sims - return a set amount */
72 #if (CONFIG_CODEC != SWCODEC) && defined(SIMULATOR)
73 unsigned long audio_prev_elapsed(void)
79 static void write_cache(void)
84 scrobbler_ata_callback
= false;
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
);
93 fdprintf(fd
, "#AUDIOSCROBBLER/" SCROBBLER_VERSION
"\n"
96 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
"\n");
98 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
" Timeless\n");
105 logf("SCROBBLER: cannot create log file");
109 /* write the cache entries */
110 fd
= open(SCROBBLER_FILE
, O_WRONLY
| O_APPEND
);
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
));
124 logf("SCROBBLER: error writing file");
127 /* clear even if unsuccessful - don't want to overflow the buffer */
131 static bool scrobbler_flush_callback(void)
133 if (scrobbler_initialised
&& cache_pos
)
138 static void add_to_cache(unsigned long play_length
)
140 if ( cache_pos
>= SCROBBLER_MAX_CACHE
)
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
),
155 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\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,
164 ret
= snprintf(scrobbler_cache
+(SCROBBLER_CACHE_LEN
*cache_pos
),
166 "%s\t%s\t%s\t\t%d\t%c\t%ld\n",
167 scrobbler_entry
.artist
,
168 scrobbler_entry
.album
?scrobbler_entry
.album
:"",
169 scrobbler_entry
.title
,
170 (int)scrobbler_entry
.length
/1000,
175 if ( ret
>= SCROBBLER_CACHE_LEN
)
177 logf("SCROBBLER: entry too long:");
178 logf("SCROBBLER: %s", scrobbler_entry
.path
);
181 if (!scrobbler_ata_callback
)
183 register_ata_idle_func(scrobbler_flush_callback
);
184 scrobbler_ata_callback
= true;
190 void scrobbler_change_event(struct mp3entry
*id
)
192 /* add entry using the previous scrobbler_entry and timestamp */
194 add_to_cache(audio_prev_elapsed());
196 /* check if track was resumed > %50 played
197 check for blank artist or track name */
198 if ((id
->elapsed
> (id
->length
/2)) ||
199 (!id
->artist
) || (!id
->title
) )
202 logf("SCROBBLER: skipping file %s", id
->path
);
206 logf("SCROBBLER: add pending");
207 copy_mp3entry(&scrobbler_entry
, id
);
209 timestamp
= mktime(get_time());
217 int scrobbler_init(void)
219 logf("SCROBBLER: init %d", global_settings
.audioscrobbler
);
221 if(!global_settings
.audioscrobbler
)
224 scrobbler_cache
= buffer_alloc(SCROBBLER_MAX_CACHE
*SCROBBLER_CACHE_LEN
);
226 add_event(PLAYBACK_EVENT_TRACK_CHANGE
, false, scrobbler_change_event
);
229 scrobbler_initialised
= true;
234 void scrobbler_flush_cache(void)
236 if (scrobbler_initialised
)
238 /* Add any pending entries to the cache */
240 add_to_cache(audio_prev_elapsed());
242 /* Write the cache to disk if needed */
250 void scrobbler_shutdown(void)
253 if (scrobbler_ata_callback
)
255 unregister_ata_idle_func(scrobbler_flush_callback
, false);
256 scrobbler_ata_callback
= false;
260 scrobbler_flush_cache();
262 if (scrobbler_initialised
)
264 remove_event(PLAYBACK_EVENT_TRACK_CHANGE
, scrobbler_change_event
);
265 scrobbler_initialised
= false;
269 void scrobbler_poweroff(void)
271 if (scrobbler_initialised
&& pending
)
273 if ( audio_status() )
274 add_to_cache(audio_current_track()->elapsed
);
276 add_to_cache(audio_prev_elapsed());
278 /* scrobbler_shutdown is called later, the cache will be written
279 * make sure the final track isn't added twice when that happens */
284 bool scrobbler_is_enabled(void)
286 return scrobbler_initialised
;