Revert the recent change to bufread()
[Rockbox.git] / apps / scrobbler.c
blob01c704afabdb645ba4e77404a743fb5f48814c9f
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)
186 scrobbler_ata_callback = register_ata_idle_func(scrobbler_flush_callback);
191 void scrobbler_change_event(struct mp3entry *id)
193 /* add entry using the previous scrobbler_entry and timestamp */
194 if (pending)
195 add_to_cache(audio_prev_elapsed());
197 /* check if track was resumed > %50 played
198 check for blank artist or track name */
199 if ((id->elapsed > (id->length/2)) ||
200 (!id->artist ) || (!id->title ) )
202 pending = false;
203 logf("SCROBBLER: skipping file %s", id->path);
205 else
207 logf("SCROBBLER: add pending");
208 copy_mp3entry(&scrobbler_entry, id);
209 #if CONFIG_RTC
210 timestamp = mktime(get_time());
211 #else
212 timestamp = 0;
213 #endif
214 pending = true;
218 int scrobbler_init(void)
220 logf("SCROBBLER: init %d", global_settings.audioscrobbler);
222 if(!global_settings.audioscrobbler)
223 return -1;
225 scrobbler_cache = buffer_alloc(SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
227 audio_set_track_changed_event(&scrobbler_change_event);
228 cache_pos = 0;
229 pending = false;
230 scrobbler_initialised = true;
232 return 1;
235 void scrobbler_flush_cache(void)
237 if (scrobbler_initialised)
239 /* Add any pending entries to the cache */
240 if(pending)
241 add_to_cache(audio_prev_elapsed());
243 /* Write the cache to disk if needed */
244 if (cache_pos)
245 write_cache();
247 pending = false;
251 void scrobbler_shutdown(void)
253 #ifndef SIMULATOR
254 if (scrobbler_ata_callback)
255 unregister_ata_idle_func(scrobbler_flush_callback, false);
256 #endif
258 scrobbler_flush_cache();
260 if (scrobbler_initialised)
262 audio_set_track_changed_event(NULL);
263 scrobbler_initialised = false;
267 void scrobbler_poweroff(void)
269 if (scrobbler_initialised && pending)
271 if ( audio_status() )
272 add_to_cache(audio_current_track()->elapsed);
273 else
274 add_to_cache(audio_prev_elapsed());
276 /* scrobbler_shutdown is called later, the cache will be written
277 * make sure the final track isn't added twice when that happens */
278 pending = false;
282 bool scrobbler_is_enabled(void)
284 return scrobbler_initialised;