Save a few bytes by changing unit selection strategy
[kugel-rb.git] / apps / plugins / stats.c
blob32d15cc1d33331919d324003433a9f1d35378131
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
65 #endif
67 /* TODO: Better get the exts from the filetypes var in tree.c */
68 const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
69 "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
70 "shn","aif","aiff"};
72 void prn(const char *str, int y)
74 rb->lcd_puts(0,y,str);
75 #ifdef HAVE_REMOTE_LCD
76 rb->lcd_remote_puts(0,y,str);
77 #endif
80 void update_screen(void)
82 char buf[32];
84 rb->lcd_clear_display();
85 #ifdef HAVE_REMOTE_LCD
86 rb->lcd_remote_clear_display();
87 #endif
89 #ifdef HAVE_LCD_BITMAP
90 rb->snprintf(buf, sizeof(buf), "Files: %d", files);
91 prn(buf,0);
92 rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
93 prn(buf,1);
94 rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
95 prn(buf,2);
96 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
97 prn(buf,3);
98 #else
99 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
100 prn(buf,0);
101 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
102 prn(buf,1);
103 #endif
105 rb->lcd_update();
106 #ifdef HAVE_REMOTE_LCD
107 rb->lcd_remote_update();
108 #endif
111 void traversedir(char* location, char* name)
113 int button;
114 struct dirent *entry;
115 DIR* dir;
116 char fullpath[MAX_PATH];
117 int files_in_dir = 0;
119 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
120 dir = rb->opendir(fullpath);
121 if (dir) {
122 entry = rb->readdir(dir);
123 while (entry) {
124 if (abort == true)
125 break;
126 /* Skip .. and . */
127 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
129 if (entry->attribute & ATTR_DIRECTORY) {
130 traversedir(fullpath, entry->d_name);
131 dirs++;
133 else {
134 char *ptr = rb->strrchr(entry->d_name,'.');
135 files++; files_in_dir++;
136 /* Might want to only count .mp3, .ogg etc. */
137 if(ptr){
138 unsigned i;
139 ptr++;
140 for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
141 if(!rb->strcasecmp(ptr,music_exts[i])){
142 musicfiles++; break;
148 if (*rb->current_tick - lasttick > (HZ/2)) {
149 update_screen();
150 lasttick = *rb->current_tick;
151 button = rb->button_get(false);
152 if (button == STATS_STOP
153 #ifdef HAVE_REMOTE_LCD
154 || button == STATS_STOP_REMOTE
155 #endif
157 abort = true;
158 break;
162 entry = rb->readdir(dir);
164 rb->closedir(dir);
166 if (largestdir < files_in_dir)
167 largestdir = files_in_dir;
170 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
172 int button;
174 (void)parameter;
176 rb = api;
177 files = 0;
178 dirs = 0;
179 musicfiles = 0;
180 largestdir = 0;
181 abort = false;
183 rb->splash(HZ, "Counting...");
184 update_screen();
185 lasttick = *rb->current_tick;
187 traversedir("", "");
188 if (abort == true) {
189 rb->splash(HZ, "Aborted");
190 return PLUGIN_OK;
192 update_screen();
193 #ifdef HAVE_REMOTE_LCD
194 rb->remote_backlight_on();
195 #endif
196 rb->backlight_on();
197 rb->splash(HZ, "Done");
198 update_screen();
199 button = rb->button_get(true);
200 while (1) {
201 switch (button) {
202 #ifdef HAVE_REMOTE_LCD
203 case STATS_STOP_REMOTE:
204 #endif
205 case STATS_STOP:
206 return PLUGIN_OK;
207 break;
208 default:
209 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
210 return PLUGIN_USB_CONNECTED;
212 break;
214 return PLUGIN_OK;