Add 2008 to the copyright notice.
[Rockbox.git] / apps / plugins / stats.c
blob993943f97709219a3e1b267c52a2035df51d0271
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Jonas Haggqvist
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 ****************************************************************************/
19 #include "plugin.h"
21 PLUGIN_HEADER
23 static struct plugin_api* rb;
24 static int files, dirs, musicfiles, largestdir;
25 static int lasttick;
26 static bool abort;
28 #if CONFIG_KEYPAD == PLAYER_PAD
29 #define STATS_STOP BUTTON_STOP
31 #elif (CONFIG_KEYPAD == RECORDER_PAD) \
32 || (CONFIG_KEYPAD == ONDIO_PAD) \
33 || (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
34 #define STATS_STOP BUTTON_OFF
36 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
37 || (CONFIG_KEYPAD == IRIVER_H300_PAD)
38 #define STATS_STOP BUTTON_OFF
39 #define STATS_STOP_REMOTE BUTTON_RC_STOP
41 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
42 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
43 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
44 #define STATS_STOP BUTTON_MENU
46 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
47 #define STATS_STOP BUTTON_PLAY
49 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
50 #define STATS_STOP BUTTON_POWER
51 #define STATS_STOP_REMOTE BUTTON_RC_PLAY
53 #elif CONFIG_KEYPAD == GIGABEAT_PAD
54 #define STATS_STOP BUTTON_POWER
56 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
57 (CONFIG_KEYPAD == SANSA_C200_PAD)
58 #define STATS_STOP BUTTON_POWER
60 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
61 #define STATS_STOP BUTTON_POWER
63 #elif CONFIG_KEYPAD == MROBE500_PAD
64 #define STATS_STOP BUTTON_POWER
66 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
67 #define STATS_STOP BUTTON_BACK
68 #endif
70 /* TODO: Better get the exts from the filetypes var in tree.c */
71 const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
72 "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
73 "shn","aif","aiff"};
75 void prn(const char *str, int y)
77 rb->lcd_puts(0,y,str);
78 #ifdef HAVE_REMOTE_LCD
79 rb->lcd_remote_puts(0,y,str);
80 #endif
83 void update_screen(void)
85 char buf[32];
87 rb->lcd_clear_display();
88 #ifdef HAVE_REMOTE_LCD
89 rb->lcd_remote_clear_display();
90 #endif
92 #ifdef HAVE_LCD_BITMAP
93 rb->snprintf(buf, sizeof(buf), "Files: %d", files);
94 prn(buf,0);
95 rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
96 prn(buf,1);
97 rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
98 prn(buf,2);
99 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
100 prn(buf,3);
101 #else
102 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
103 prn(buf,0);
104 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
105 prn(buf,1);
106 #endif
108 rb->lcd_update();
109 #ifdef HAVE_REMOTE_LCD
110 rb->lcd_remote_update();
111 #endif
114 void traversedir(char* location, char* name)
116 int button;
117 struct dirent *entry;
118 DIR* dir;
119 char fullpath[MAX_PATH];
120 int files_in_dir = 0;
122 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
123 dir = rb->opendir(fullpath);
124 if (dir) {
125 entry = rb->readdir(dir);
126 while (entry) {
127 if (abort == true)
128 break;
129 /* Skip .. and . */
130 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
132 if (entry->attribute & ATTR_DIRECTORY) {
133 traversedir(fullpath, entry->d_name);
134 dirs++;
136 else {
137 char *ptr = rb->strrchr(entry->d_name,'.');
138 files++; files_in_dir++;
139 /* Might want to only count .mp3, .ogg etc. */
140 if(ptr){
141 unsigned i;
142 ptr++;
143 for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
144 if(!rb->strcasecmp(ptr,music_exts[i])){
145 musicfiles++; break;
151 if (*rb->current_tick - lasttick > (HZ/2)) {
152 update_screen();
153 lasttick = *rb->current_tick;
154 button = rb->button_get(false);
155 if (button == STATS_STOP
156 #ifdef HAVE_REMOTE_LCD
157 || button == STATS_STOP_REMOTE
158 #endif
160 abort = true;
161 break;
165 entry = rb->readdir(dir);
167 rb->closedir(dir);
169 if (largestdir < files_in_dir)
170 largestdir = files_in_dir;
173 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
175 int button;
177 (void)parameter;
179 rb = api;
180 files = 0;
181 dirs = 0;
182 musicfiles = 0;
183 largestdir = 0;
184 abort = false;
186 rb->splash(HZ, "Counting...");
187 update_screen();
188 lasttick = *rb->current_tick;
190 traversedir("", "");
191 if (abort == true) {
192 rb->splash(HZ, "Aborted");
193 return PLUGIN_OK;
195 update_screen();
196 #ifdef HAVE_REMOTE_LCD
197 rb->remote_backlight_on();
198 #endif
199 rb->backlight_on();
200 rb->splash(HZ, "Done");
201 update_screen();
202 button = rb->button_get(true);
203 while (1) {
204 switch (button) {
205 #ifdef HAVE_REMOTE_LCD
206 case STATS_STOP_REMOTE:
207 #endif
208 case STATS_STOP:
209 return PLUGIN_OK;
210 break;
211 default:
212 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
213 return PLUGIN_USB_CONNECTED;
215 break;
217 return PLUGIN_OK;