when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / plugins / stats.c
blob12125a33e8c1fbbb49328bb32851a398893700ce
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
69 #elif CONFIG_KEYPAD == MROBE100_PAD
70 #define STATS_STOP BUTTON_POWER
72 #else
73 #error No keymap defined!
74 #endif
76 /* TODO: Better get the exts from the filetypes var in tree.c */
77 const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
78 "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
79 "shn","aif","aiff"};
81 void prn(const char *str, int y)
83 rb->lcd_puts(0,y,str);
84 #ifdef HAVE_REMOTE_LCD
85 rb->lcd_remote_puts(0,y,str);
86 #endif
89 void update_screen(void)
91 char buf[32];
93 rb->lcd_clear_display();
94 #ifdef HAVE_REMOTE_LCD
95 rb->lcd_remote_clear_display();
96 #endif
98 #ifdef HAVE_LCD_BITMAP
99 rb->snprintf(buf, sizeof(buf), "Files: %d", files);
100 prn(buf,0);
101 rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
102 prn(buf,1);
103 rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
104 prn(buf,2);
105 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
106 prn(buf,3);
107 #else
108 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
109 prn(buf,0);
110 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
111 prn(buf,1);
112 #endif
114 rb->lcd_update();
115 #ifdef HAVE_REMOTE_LCD
116 rb->lcd_remote_update();
117 #endif
120 void traversedir(char* location, char* name)
122 int button;
123 struct dirent *entry;
124 DIR* dir;
125 char fullpath[MAX_PATH];
126 int files_in_dir = 0;
128 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
129 dir = rb->opendir(fullpath);
130 if (dir) {
131 entry = rb->readdir(dir);
132 while (entry) {
133 if (abort == true)
134 break;
135 /* Skip .. and . */
136 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
138 if (entry->attribute & ATTR_DIRECTORY) {
139 traversedir(fullpath, entry->d_name);
140 dirs++;
142 else {
143 char *ptr = rb->strrchr(entry->d_name,'.');
144 files++; files_in_dir++;
145 /* Might want to only count .mp3, .ogg etc. */
146 if(ptr){
147 unsigned i;
148 ptr++;
149 for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
150 if(!rb->strcasecmp(ptr,music_exts[i])){
151 musicfiles++; break;
157 if (*rb->current_tick - lasttick > (HZ/2)) {
158 update_screen();
159 lasttick = *rb->current_tick;
160 button = rb->button_get(false);
161 if (button == STATS_STOP
162 #ifdef HAVE_REMOTE_LCD
163 || button == STATS_STOP_REMOTE
164 #endif
166 abort = true;
167 break;
171 entry = rb->readdir(dir);
173 rb->closedir(dir);
175 if (largestdir < files_in_dir)
176 largestdir = files_in_dir;
179 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
181 int button;
183 (void)parameter;
185 rb = api;
186 files = 0;
187 dirs = 0;
188 musicfiles = 0;
189 largestdir = 0;
190 abort = false;
192 rb->splash(HZ, "Counting...");
193 update_screen();
194 lasttick = *rb->current_tick;
196 traversedir("", "");
197 if (abort == true) {
198 rb->splash(HZ, "Aborted");
199 return PLUGIN_OK;
201 update_screen();
202 #ifdef HAVE_REMOTE_LCD
203 rb->remote_backlight_on();
204 #endif
205 rb->backlight_on();
206 rb->splash(HZ, "Done");
207 update_screen();
208 button = rb->button_get(true);
209 while (1) {
210 switch (button) {
211 #ifdef HAVE_REMOTE_LCD
212 case STATS_STOP_REMOTE:
213 #endif
214 case STATS_STOP:
215 return PLUGIN_OK;
216 break;
217 default:
218 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
219 return PLUGIN_USB_CONNECTED;
221 break;
223 return PLUGIN_OK;