Fix yellow
[Rockbox.git] / apps / scrobbler.c
blob85285cb911238c0a35181b1fc1746a6d28bac1d7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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
24 #include "file.h"
25 #include "sprintf.h"
26 #include "playback.h"
27 #include "logf.h"
28 #include "id3.h"
29 #include "kernel.h"
30 #include "audio.h"
31 #include "buffer.h"
32 #include "settings.h"
33 #include "ata_idle_notify.h"
35 #if CONFIG_RTC
36 #include "time.h"
37 #include "timefuncs.h"
38 #endif
40 #include "scrobbler.h"
42 #define SCROBBLER_VERSION "1.0"
44 #if CONFIG_RTC
45 #define SCROBBLER_FILE "/.scrobbler.log"
46 #else
47 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
48 #endif
50 /* increment this on any code change that effects output */
51 #define SCROBBLER_REVISION " $Revision$"
53 #define SCROBBLER_MAX_CACHE 32
54 /* longest entry I've had is 323, add a safety margin */
55 #define SCROBBLER_CACHE_LEN 512
57 static char* scrobbler_cache;
59 static int scrobbler_fd = -1;
60 static int cache_pos;
61 static struct mp3entry scrobbler_entry;
62 static bool pending = false;
63 static bool scrobbler_initialised = false;
64 static bool scrobbler_ata_callback = false;
65 #if CONFIG_RTC
66 static time_t timestamp;
67 #else
68 static unsigned long timestamp;
69 #endif
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)
75 return 120000;
77 #endif
79 static void write_cache(void)
81 int i;
83 scrobbler_ata_callback = false;
85 /* If the file doesn't exist, create it.
86 Check at each write since file may be deleted at any time */
87 scrobbler_fd = open(SCROBBLER_FILE, O_RDONLY);
88 if(scrobbler_fd < 0)
90 scrobbler_fd = open(SCROBBLER_FILE, O_RDWR | O_CREAT);
91 if(scrobbler_fd >= 0)
93 fdprintf(scrobbler_fd, "#AUDIOSCROBBLER/%s\n", SCROBBLER_VERSION);
94 fdprintf(scrobbler_fd, "#TZ/UNKNOWN\n");
95 #if CONFIG_RTC
96 fdprintf(scrobbler_fd,
97 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION "\n");
98 #else
99 fdprintf(scrobbler_fd,
100 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION " Timeless\n");
101 #endif
102 close(scrobbler_fd);
104 else
106 logf("SCROBBLER: cannot create log file");
109 close(scrobbler_fd);
110 scrobbler_fd = -1;
112 /* write the cache entries */
113 scrobbler_fd = open(SCROBBLER_FILE, O_WRONLY | O_APPEND);
114 if(scrobbler_fd >= 0)
116 logf("SCROBBLER: writing %d entries", cache_pos);
118 for ( i=0; i < cache_pos; i++ )
120 logf("SCROBBLER: write %d", i);
121 fdprintf(scrobbler_fd, "%s", scrobbler_cache+(SCROBBLER_CACHE_LEN*i));
123 close(scrobbler_fd);
125 else
127 logf("SCROBBLER: error writing file");
130 /* clear even if unsuccessful - don't want to overflow the buffer */
131 cache_pos = 0;
132 scrobbler_fd = -1;
135 static bool scrobbler_flush_callback(void)
137 if (scrobbler_initialised && cache_pos)
138 write_cache();
139 return true;
142 static void add_to_cache(unsigned long play_length)
144 if ( cache_pos >= SCROBBLER_MAX_CACHE )
145 write_cache();
147 int ret;
148 char rating = 'S'; /* Skipped */
150 logf("SCROBBLER: add_to_cache[%d]", cache_pos);
152 if ( play_length > (scrobbler_entry.length/2) )
153 rating = 'L'; /* Listened */
155 if (scrobbler_entry.tracknum > 0)
157 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
158 SCROBBLER_CACHE_LEN,
159 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\n",
160 scrobbler_entry.artist,
161 scrobbler_entry.album?scrobbler_entry.album:"",
162 scrobbler_entry.title,
163 scrobbler_entry.tracknum,
164 (int)scrobbler_entry.length/1000,
165 rating,
166 (long)timestamp);
167 } else {
168 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
169 SCROBBLER_CACHE_LEN,
170 "%s\t%s\t%s\t\t%d\t%c\t%ld\n",
171 scrobbler_entry.artist,
172 scrobbler_entry.album?scrobbler_entry.album:"",
173 scrobbler_entry.title,
174 (int)scrobbler_entry.length/1000,
175 rating,
176 (long)timestamp);
179 if ( ret >= SCROBBLER_CACHE_LEN )
181 logf("SCROBBLER: entry too long:");
182 logf("SCROBBLER: %s", scrobbler_entry.path);
183 } else {
184 cache_pos++;
185 if (!scrobbler_ata_callback)
187 register_ata_idle_func(scrobbler_flush_callback);
188 scrobbler_ata_callback = true;
194 void scrobbler_change_event(struct mp3entry *id)
196 /* add entry using the previous scrobbler_entry and timestamp */
197 if (pending)
198 add_to_cache(audio_prev_elapsed());
200 /* check if track was resumed > %50 played
201 check for blank artist or track name */
202 if ((id->elapsed > (id->length/2)) ||
203 (!id->artist ) || (!id->title ) )
205 pending = false;
206 logf("SCROBBLER: skipping file %s", id->path);
208 else
210 logf("SCROBBLER: add pending");
211 copy_mp3entry(&scrobbler_entry, id);
212 #if CONFIG_RTC
213 timestamp = mktime(get_time());
214 #else
215 timestamp = 0;
216 #endif
217 pending = true;
221 int scrobbler_init(void)
223 logf("SCROBBLER: init %d", global_settings.audioscrobbler);
225 if(!global_settings.audioscrobbler)
226 return -1;
228 scrobbler_cache = buffer_alloc(SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
230 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, scrobbler_change_event);
231 cache_pos = 0;
232 pending = false;
233 scrobbler_initialised = true;
235 return 1;
238 void scrobbler_flush_cache(void)
240 if (scrobbler_initialised)
242 /* Add any pending entries to the cache */
243 if(pending)
244 add_to_cache(audio_prev_elapsed());
246 /* Write the cache to disk if needed */
247 if (cache_pos)
248 write_cache();
250 pending = false;
254 void scrobbler_shutdown(void)
256 #ifndef SIMULATOR
257 if (scrobbler_ata_callback)
258 unregister_ata_idle_func(scrobbler_flush_callback, false);
259 #endif
261 scrobbler_flush_cache();
263 if (scrobbler_initialised)
265 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, scrobbler_change_event);
266 scrobbler_initialised = false;
270 void scrobbler_poweroff(void)
272 if (scrobbler_initialised && pending)
274 if ( audio_status() )
275 add_to_cache(audio_current_track()->elapsed);
276 else
277 add_to_cache(audio_prev_elapsed());
279 /* scrobbler_shutdown is called later, the cache will be written
280 * make sure the final track isn't added twice when that happens */
281 pending = false;
285 bool scrobbler_is_enabled(void)
287 return scrobbler_initialised;