Accept FS#9111 - only exit te stats plugin if the exit button is pressed
[Rockbox.git] / apps / plugins / stats.c
blob21d7862541a85523f9cfd36c40735b8264450535
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 #else
82 #error No keymap defined!
83 #endif
85 /* TODO: Better get the exts from the filetypes var in tree.c */
86 const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
87 "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
88 "shn","aif","aiff","wma","wmv","spx","ape","adx",
89 "sid","mod","nsf","nsfe","spc","asf","mac"};
91 void prn(const char *str, int y)
93 rb->lcd_puts(0,y,str);
94 #ifdef HAVE_REMOTE_LCD
95 rb->lcd_remote_puts(0,y,str);
96 #endif
99 void update_screen(void)
101 char buf[32];
103 rb->lcd_clear_display();
104 #ifdef HAVE_REMOTE_LCD
105 rb->lcd_remote_clear_display();
106 #endif
108 #ifdef HAVE_LCD_BITMAP
109 rb->snprintf(buf, sizeof(buf), "Files: %d", files);
110 prn(buf,0);
111 rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
112 prn(buf,1);
113 rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
114 prn(buf,2);
115 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
116 prn(buf,3);
117 #else
118 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
119 prn(buf,0);
120 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
121 prn(buf,1);
122 #endif
124 rb->lcd_update();
125 #ifdef HAVE_REMOTE_LCD
126 rb->lcd_remote_update();
127 #endif
130 void traversedir(char* location, char* name)
132 int button;
133 struct dirent *entry;
134 DIR* dir;
135 char fullpath[MAX_PATH];
136 int files_in_dir = 0;
138 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
139 dir = rb->opendir(fullpath);
140 if (dir) {
141 entry = rb->readdir(dir);
142 while (entry) {
143 if (abort == true)
144 break;
145 /* Skip .. and . */
146 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
148 if (entry->attribute & ATTR_DIRECTORY) {
149 traversedir(fullpath, entry->d_name);
150 dirs++;
152 else {
153 char *ptr = rb->strrchr(entry->d_name,'.');
154 files++; files_in_dir++;
155 /* Might want to only count .mp3, .ogg etc. */
156 if(ptr){
157 unsigned i;
158 ptr++;
159 for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
160 if(!rb->strcasecmp(ptr,music_exts[i])){
161 musicfiles++; break;
167 if (*rb->current_tick - lasttick > (HZ/2)) {
168 update_screen();
169 lasttick = *rb->current_tick;
170 button = rb->button_get(false);
171 if (button == STATS_STOP
172 #ifdef HAVE_REMOTE_LCD
173 || button == STATS_STOP_REMOTE
174 #endif
176 abort = true;
177 break;
181 entry = rb->readdir(dir);
183 rb->closedir(dir);
185 if (largestdir < files_in_dir)
186 largestdir = files_in_dir;
189 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
191 int button;
193 (void)parameter;
195 rb = api;
196 files = 0;
197 dirs = 0;
198 musicfiles = 0;
199 largestdir = 0;
200 abort = false;
202 rb->splash(HZ, "Counting...");
203 update_screen();
204 lasttick = *rb->current_tick;
206 traversedir("", "");
207 if (abort == true) {
208 rb->splash(HZ, "Aborted");
209 return PLUGIN_OK;
211 update_screen();
212 #ifdef HAVE_REMOTE_LCD
213 rb->remote_backlight_on();
214 #endif
215 rb->backlight_on();
216 rb->splash(HZ, "Done");
217 update_screen();
218 while (1) {
219 button = rb->button_get(true);
220 switch (button) {
221 #ifdef HAVE_REMOTE_LCD
222 case STATS_STOP_REMOTE:
223 #endif
224 case STATS_STOP:
225 return PLUGIN_OK;
226 break;
227 default:
228 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
229 return PLUGIN_USB_CONNECTED;
231 break;
234 return PLUGIN_OK;