Treat unknown RaaA platforms like SDL for last.FM scrobbler log file. Fix yellow
[kugel-rb.git] / apps / scrobbler.c
blob14cb4d30d8d97de815321d1f808d3d9f90d48f43
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 <stdio.h>
27 #include <config.h>
28 #include "file.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 "filefuncs.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 /* increment this on any code change that effects output */
49 #define SCROBBLER_REVISION " $Revision$"
51 #define SCROBBLER_MAX_CACHE 32
52 /* longest entry I've had is 323, add a safety margin */
53 #define SCROBBLER_CACHE_LEN 512
55 static char* scrobbler_cache;
57 static int cache_pos;
58 static struct mp3entry scrobbler_entry;
59 static bool pending = false;
60 static bool scrobbler_initialised = false;
61 #if CONFIG_RTC
62 static time_t timestamp;
63 #else
64 static unsigned long timestamp;
65 #endif
67 /* Crude work-around for Archos Sims - return a set amount */
68 #if (CONFIG_CODEC != SWCODEC) && (CONFIG_PLATFORM & PLATFORM_HOSTED)
69 unsigned long audio_prev_elapsed(void)
71 return 120000;
73 #endif
75 static void get_scrobbler_filename(char *path, size_t size)
77 int used;
79 #if CONFIG_RTC
80 const char *base_filename = ".scrobbler.log";
81 #else
82 const char *base_filename = ".scrobbler-timeless.log";
83 #endif
85 /* Get location of USB mass storage area */
86 #ifdef APPLICATION
87 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
88 used = snprintf(path, size, "/home/user/MyDocs/%s", base_filename);
89 #elif (CONFIG_PLATFORM & PLATFORM_ANDROID)
90 used = snprintf(path, size, "/sdcard/%s", base_filename);
91 #else /* SDL/unknown RaaA build */
92 used = snprintf(path, size, "%s/%s", ROCKBOX_DIR, base_filename);
93 #endif /* (CONFIG_PLATFORM & PLATFORM_MAEMO) */
95 #else
96 used = snprintf(path, size, "/%s", base_filename);
97 #endif
99 if (used >= (int)size)
101 logf("SCROBBLER: not enough buffer space for log file");
102 memset(path, 0, size);
106 static void write_cache(void)
108 int i;
109 int fd;
111 char scrobbler_file[MAX_PATH];
112 get_scrobbler_filename(scrobbler_file, MAX_PATH);
114 /* If the file doesn't exist, create it.
115 Check at each write since file may be deleted at any time */
116 if(!file_exists(scrobbler_file))
118 fd = open(scrobbler_file, O_RDWR | O_CREAT, 0666);
119 if(fd >= 0)
121 fdprintf(fd, "#AUDIOSCROBBLER/" SCROBBLER_VERSION "\n"
122 "#TZ/UNKNOWN\n"
123 #if CONFIG_RTC
124 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION "\n");
125 #else
126 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION " Timeless\n");
127 #endif
129 close(fd);
131 else
133 logf("SCROBBLER: cannot create log file");
137 /* write the cache entries */
138 fd = open(scrobbler_file, O_WRONLY | O_APPEND);
139 if(fd >= 0)
141 logf("SCROBBLER: writing %d entries", cache_pos);
143 for ( i=0; i < cache_pos; i++ )
145 logf("SCROBBLER: write %d", i);
146 fdprintf(fd, "%s", scrobbler_cache+(SCROBBLER_CACHE_LEN*i));
148 close(fd);
150 else
152 logf("SCROBBLER: error writing file");
155 /* clear even if unsuccessful - don't want to overflow the buffer */
156 cache_pos = 0;
159 static void scrobbler_flush_callback(void *data)
161 (void)data;
162 if (scrobbler_initialised && cache_pos)
163 write_cache();
166 static void add_to_cache(unsigned long play_length)
168 if ( cache_pos >= SCROBBLER_MAX_CACHE )
169 write_cache();
171 int ret;
172 char rating = 'S'; /* Skipped */
174 logf("SCROBBLER: add_to_cache[%d]", cache_pos);
176 if ( play_length > (scrobbler_entry.length/2) )
177 rating = 'L'; /* Listened */
179 if (scrobbler_entry.tracknum > 0)
181 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
182 SCROBBLER_CACHE_LEN,
183 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\t%s\n",
184 scrobbler_entry.artist,
185 scrobbler_entry.album?scrobbler_entry.album:"",
186 scrobbler_entry.title,
187 scrobbler_entry.tracknum,
188 (int)scrobbler_entry.length/1000,
189 rating,
190 (long)timestamp,
191 scrobbler_entry.mb_track_id?scrobbler_entry.mb_track_id:"");
192 } else {
193 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
194 SCROBBLER_CACHE_LEN,
195 "%s\t%s\t%s\t\t%d\t%c\t%ld\t%s\n",
196 scrobbler_entry.artist,
197 scrobbler_entry.album?scrobbler_entry.album:"",
198 scrobbler_entry.title,
199 (int)scrobbler_entry.length/1000,
200 rating,
201 (long)timestamp,
202 scrobbler_entry.mb_track_id?scrobbler_entry.mb_track_id:"");
205 if ( ret >= SCROBBLER_CACHE_LEN )
207 logf("SCROBBLER: entry too long:");
208 logf("SCROBBLER: %s", scrobbler_entry.path);
209 } else {
210 cache_pos++;
211 register_storage_idle_func(scrobbler_flush_callback);
216 static void scrobbler_change_event(void *data)
218 struct mp3entry *id = (struct mp3entry*)data;
219 /* add entry using the previous scrobbler_entry and timestamp */
220 if (pending)
221 add_to_cache(audio_prev_elapsed());
223 /* check if track was resumed > %50 played
224 check for blank artist or track name */
225 if ((id->elapsed > (id->length/2)) ||
226 (!id->artist ) || (!id->title ) )
228 pending = false;
229 logf("SCROBBLER: skipping file %s", id->path);
231 else
233 logf("SCROBBLER: add pending");
234 copy_mp3entry(&scrobbler_entry, id);
235 #if CONFIG_RTC
236 timestamp = mktime(get_time());
237 #else
238 timestamp = 0;
239 #endif
240 pending = true;
244 int scrobbler_init(void)
246 logf("SCROBBLER: init %d", global_settings.audioscrobbler);
248 if(!global_settings.audioscrobbler)
249 return -1;
251 scrobbler_cache = buffer_alloc(SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
253 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, scrobbler_change_event);
254 cache_pos = 0;
255 pending = false;
256 scrobbler_initialised = true;
258 return 1;
261 void scrobbler_flush_cache(void)
263 if (scrobbler_initialised)
265 /* Add any pending entries to the cache */
266 if(pending)
267 add_to_cache(audio_prev_elapsed());
269 /* Write the cache to disk if needed */
270 if (cache_pos)
271 write_cache();
273 pending = false;
277 void scrobbler_shutdown(void)
279 scrobbler_flush_cache();
281 if (scrobbler_initialised)
283 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, scrobbler_change_event);
284 scrobbler_initialised = false;
288 void scrobbler_poweroff(void)
290 if (scrobbler_initialised && pending)
292 if ( audio_status() )
293 add_to_cache(audio_current_track()->elapsed);
294 else
295 add_to_cache(audio_prev_elapsed());
297 /* scrobbler_shutdown is called later, the cache will be written
298 * make sure the final track isn't added twice when that happens */
299 pending = false;
303 bool scrobbler_is_enabled(void)
305 return scrobbler_initialised;