fix some problems with the menu code:
[kugel-rb.git] / apps / plugins / stats.c
blobd77f04408b924b4deac3e9ff1e46c0b348e77093
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Jonas Haggqvist
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 ****************************************************************************/
21 #include "plugin.h"
23 PLUGIN_HEADER
25 static const struct plugin_api* rb;
26 static int files, dirs, musicfiles, largestdir;
27 static int lasttick;
28 static bool abort;
30 #if CONFIG_KEYPAD == PLAYER_PAD
31 #define STATS_STOP BUTTON_STOP
33 #elif (CONFIG_KEYPAD == RECORDER_PAD) \
34 || (CONFIG_KEYPAD == ONDIO_PAD) \
35 || (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
36 #define STATS_STOP BUTTON_OFF
38 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
39 || (CONFIG_KEYPAD == IRIVER_H300_PAD)
40 #define STATS_STOP BUTTON_OFF
41 #define STATS_STOP_REMOTE BUTTON_RC_STOP
43 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
44 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
45 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
46 #define STATS_STOP BUTTON_MENU
48 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
49 #define STATS_STOP BUTTON_PLAY
51 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
52 #define STATS_STOP BUTTON_POWER
53 #define STATS_STOP_REMOTE BUTTON_RC_PLAY
55 #elif CONFIG_KEYPAD == GIGABEAT_PAD
56 #define STATS_STOP BUTTON_POWER
58 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
59 (CONFIG_KEYPAD == SANSA_C200_PAD)
60 #define STATS_STOP BUTTON_POWER
62 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
63 #define STATS_STOP BUTTON_POWER
65 #elif CONFIG_KEYPAD == MROBE500_PAD
66 #define STATS_STOP BUTTON_POWER
68 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
69 #define STATS_STOP BUTTON_BACK
71 #elif CONFIG_KEYPAD == MROBE100_PAD
72 #define STATS_STOP BUTTON_POWER
74 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
75 #define STATS_STOP BUTTON_REC
76 #define STATS_STOP_REMOTE BUTTON_RC_REC
78 #elif CONFIG_KEYPAD == COWOND2_PAD
79 #define STATS_STOP BUTTON_POWER
81 #elif CONFIG_KEYPAD == IAUDIO67_PAD
82 #define STATS_STOP BUTTON_POWER
84 #else
85 #error No keymap defined!
86 #endif
88 /* TODO: Better get the exts from the filetypes var in tree.c */
89 const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
90 "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
91 "shn","aif","aiff","wma","wmv","spx","ape","adx",
92 "sid","mod","nsf","nsfe","spc","asf","mac"};
94 void prn(const char *str, int y)
96 rb->lcd_puts(0,y,str);
97 #ifdef HAVE_REMOTE_LCD
98 rb->lcd_remote_puts(0,y,str);
99 #endif
102 void update_screen(void)
104 char buf[32];
106 rb->lcd_clear_display();
107 #ifdef HAVE_REMOTE_LCD
108 rb->lcd_remote_clear_display();
109 #endif
111 #ifdef HAVE_LCD_BITMAP
112 rb->snprintf(buf, sizeof(buf), "Files: %d", files);
113 prn(buf,0);
114 rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
115 prn(buf,1);
116 rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
117 prn(buf,2);
118 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
119 prn(buf,3);
120 #else
121 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
122 prn(buf,0);
123 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
124 prn(buf,1);
125 #endif
127 rb->lcd_update();
128 #ifdef HAVE_REMOTE_LCD
129 rb->lcd_remote_update();
130 #endif
133 void traversedir(char* location, char* name)
135 int button;
136 struct dirent *entry;
137 DIR* dir;
138 char fullpath[MAX_PATH];
139 int files_in_dir = 0;
141 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
142 dir = rb->opendir(fullpath);
143 if (dir) {
144 entry = rb->readdir(dir);
145 while (entry) {
146 if (abort == true)
147 break;
148 /* Skip .. and . */
149 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
151 if (entry->attribute & ATTR_DIRECTORY) {
152 traversedir(fullpath, entry->d_name);
153 dirs++;
155 else {
156 char *ptr = rb->strrchr(entry->d_name,'.');
157 files++; files_in_dir++;
158 /* Might want to only count .mp3, .ogg etc. */
159 if(ptr){
160 unsigned i;
161 ptr++;
162 for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
163 if(!rb->strcasecmp(ptr,music_exts[i])){
164 musicfiles++; break;
170 if (*rb->current_tick - lasttick > (HZ/2)) {
171 update_screen();
172 lasttick = *rb->current_tick;
173 button = rb->button_get(false);
174 if (button == STATS_STOP
175 #ifdef HAVE_REMOTE_LCD
176 || button == STATS_STOP_REMOTE
177 #endif
179 abort = true;
180 break;
184 entry = rb->readdir(dir);
186 rb->closedir(dir);
188 if (largestdir < files_in_dir)
189 largestdir = files_in_dir;
192 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
194 int button;
196 (void)parameter;
198 rb = api;
199 files = 0;
200 dirs = 0;
201 musicfiles = 0;
202 largestdir = 0;
203 abort = false;
205 rb->splash(HZ, "Counting...");
206 update_screen();
207 lasttick = *rb->current_tick;
209 traversedir("", "");
210 if (abort == true) {
211 rb->splash(HZ, "Aborted");
212 return PLUGIN_OK;
214 update_screen();
215 #ifdef HAVE_REMOTE_LCD
216 rb->remote_backlight_on();
217 #endif
218 rb->backlight_on();
219 rb->splash(HZ, "Done");
220 update_screen();
221 while (1) {
222 button = rb->button_get(true);
223 switch (button) {
224 #ifdef HAVE_REMOTE_LCD
225 case STATS_STOP_REMOTE:
226 #endif
227 case STATS_STOP:
228 return PLUGIN_OK;
229 break;
230 default:
231 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
232 return PLUGIN_USB_CONNECTED;
234 break;
237 return PLUGIN_OK;