Fix warning about missing newline at the EOF
[maemo-rb.git] / apps / plugins / stats.c
blobda231b772c23ee09fe0317dc58fcb67d24d27ca7
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"
25 static int files, dirs, audiofiles, m3ufiles, imagefiles, videofiles, largestdir;
26 static int lasttick;
27 static bool cancel;
29 #if CONFIG_KEYPAD == PLAYER_PAD
30 #define STATS_STOP BUTTON_STOP
32 #elif (CONFIG_KEYPAD == RECORDER_PAD) \
33 || (CONFIG_KEYPAD == ONDIO_PAD) \
34 || (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
35 #define STATS_STOP BUTTON_OFF
37 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
38 || (CONFIG_KEYPAD == IRIVER_H300_PAD)
39 #define STATS_STOP BUTTON_OFF
40 #define STATS_STOP_REMOTE BUTTON_RC_STOP
42 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
43 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
44 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
45 #define STATS_STOP BUTTON_MENU
47 #elif (CONFIG_KEYPAD == IRIVER_IFP7XX_PAD) || \
48 (CONFIG_KEYPAD == SAMSUNG_YH_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 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
61 (CONFIG_KEYPAD == SANSA_M200_PAD) || \
62 (CONFIG_KEYPAD == SANSA_CONNECT_PAD)
63 #define STATS_STOP BUTTON_POWER
65 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
66 #define STATS_STOP BUTTON_HOME
68 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
69 #define STATS_STOP BUTTON_POWER
71 #elif CONFIG_KEYPAD == MROBE500_PAD
72 #define STATS_STOP BUTTON_POWER
73 #define STATS_STOP_REMOTE BUTTON_RC_DOWN
75 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD || \
76 CONFIG_KEYPAD == SAMSUNG_YPR0_PAD
77 #define STATS_STOP BUTTON_BACK
79 #elif CONFIG_KEYPAD == MROBE100_PAD
80 #define STATS_STOP BUTTON_POWER
81 #define STATS_STOP_REMOTE BUTTON_RC_DOWN
83 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
84 #define STATS_STOP BUTTON_REC
85 #define STATS_STOP_REMOTE BUTTON_RC_REC
87 #elif CONFIG_KEYPAD == COWON_D2_PAD
88 #define STATS_STOP BUTTON_POWER
90 #elif CONFIG_KEYPAD == IAUDIO67_PAD
91 #define STATS_STOP BUTTON_POWER
93 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
94 #define STATS_STOP BUTTON_BACK
96 #elif (CONFIG_KEYPAD == PHILIPS_HDD1630_PAD) || \
97 (CONFIG_KEYPAD == PHILIPS_HDD6330_PAD) || \
98 (CONFIG_KEYPAD == PHILIPS_SA9200_PAD)
99 #define STATS_STOP BUTTON_POWER
101 #elif CONFIG_KEYPAD == ONDAVX747_PAD
102 #define STATS_STOP BUTTON_POWER
103 #elif CONFIG_KEYPAD == ONDAVX777_PAD
104 #define STATS_STOP BUTTON_POWER
106 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
107 #define STATS_STOP BUTTON_REC
109 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
110 #define STATS_STOP BUTTON_REC
112 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
113 #define STATS_STOP BUTTON_REC
115 #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
116 #define STATS_STOP BUTTON_BACK
118 #else
119 #error No keymap defined!
120 #endif
122 /* we don't have yet a filetype attribute for image files */
123 static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
125 /* neither for video ones */
126 static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
128 static void prn(const char *str, int y)
130 rb->lcd_puts(0,y,str);
131 #ifdef HAVE_REMOTE_LCD
132 rb->lcd_remote_puts(0,y,str);
133 #endif
136 static void update_screen(void)
138 char buf[32];
140 rb->lcd_clear_display();
141 #ifdef HAVE_REMOTE_LCD
142 rb->lcd_remote_clear_display();
143 #endif
145 #ifdef HAVE_LCD_BITMAP
146 rb->snprintf(buf, sizeof(buf), "Total Files: %d", files);
147 prn(buf,0);
148 rb->snprintf(buf, sizeof(buf), "Audio: %d", audiofiles);
149 prn(buf,1);
150 rb->snprintf(buf, sizeof(buf), "Playlists: %d", m3ufiles);
151 prn(buf,2);
152 rb->snprintf(buf, sizeof(buf), "Images: %d", imagefiles);
153 prn(buf,3);
154 rb->snprintf(buf, sizeof(buf), "Videos: %d", videofiles);
155 prn(buf,4);
156 rb->snprintf(buf, sizeof(buf), "Directories: %d", dirs);
157 prn(buf,5);
158 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
159 prn(buf,6);
160 #else
161 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
162 prn(buf,0);
163 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
164 prn(buf,1);
165 #endif
167 rb->lcd_update();
168 #ifdef HAVE_REMOTE_LCD
169 rb->lcd_remote_update();
170 #endif
173 static void traversedir(char* location, char* name)
175 int button;
176 struct dirent *entry;
177 DIR* dir;
178 char fullpath[MAX_PATH];
179 int files_in_dir = 0;
181 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
182 dir = rb->opendir(fullpath);
183 if (dir) {
184 entry = rb->readdir(dir);
185 while (entry) {
186 if (cancel)
187 break;
188 /* Skip .. and . */
189 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
191 struct dirinfo info = rb->dir_get_info(dir, entry);
192 if (info.attribute & ATTR_DIRECTORY) {
193 traversedir(fullpath, entry->d_name);
194 dirs++;
196 else {
197 files_in_dir++; files++;
199 /* get the filetype from the filename */
200 int attr = rb->filetype_get_attr(entry->d_name);
201 switch (attr & FILE_ATTR_MASK)
203 case FILE_ATTR_AUDIO:
204 audiofiles++;
205 break;
207 case FILE_ATTR_M3U:
208 m3ufiles++;
209 break;
211 default:
213 /* use hardcoded filetype_exts to count
214 * image and video files until we get
215 * new attributes added to filetypes.h */
216 char *ptr = rb->strrchr(entry->d_name,'.');
217 if(ptr) {
218 unsigned i;
219 ptr++;
220 for(i=0;i<ARRAYLEN(image_exts);i++) {
221 if(!rb->strcasecmp(ptr,image_exts[i])) {
222 imagefiles++; break;
226 if (i >= ARRAYLEN(image_exts)) {
227 /* not found above - try video files */
228 for(i=0;i<ARRAYLEN(video_exts);i++) {
229 if(!rb->strcasecmp(ptr,video_exts[i])) {
230 videofiles++; break;
235 } /* default: */
236 } /* switch */
240 if (*rb->current_tick - lasttick > (HZ/2)) {
241 update_screen();
242 lasttick = *rb->current_tick;
243 button = rb->button_get(false);
244 if (button == STATS_STOP
245 #ifdef HAVE_REMOTE_LCD
246 || button == STATS_STOP_REMOTE
247 #endif
249 cancel = true;
250 break;
254 entry = rb->readdir(dir);
256 rb->closedir(dir);
258 if (largestdir < files_in_dir)
259 largestdir = files_in_dir;
262 /* this is the plugin entry point */
263 enum plugin_status plugin_start(const void* parameter)
265 int button;
267 (void)parameter;
269 files = 0;
270 dirs = 0;
271 audiofiles = 0;
272 m3ufiles = 0;
273 imagefiles = 0;
274 videofiles = 0;
275 largestdir = 0;
276 cancel = false;
278 rb->splash(HZ, "Counting...");
279 update_screen();
280 lasttick = *rb->current_tick;
282 traversedir("", "");
283 if (cancel) {
284 rb->splash(HZ, "Aborted");
285 return PLUGIN_OK;
287 update_screen();
288 #ifdef HAVE_REMOTE_LCD
289 rb->remote_backlight_on();
290 #endif
291 rb->backlight_on();
292 rb->splash(HZ, "Done");
293 update_screen();
294 while (1) {
295 button = rb->button_get(true);
296 switch (button) {
297 #ifdef HAVE_REMOTE_LCD
298 case STATS_STOP_REMOTE:
299 #endif
300 case STATS_STOP:
301 return PLUGIN_OK;
302 break;
303 default:
304 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
305 return PLUGIN_USB_CONNECTED;
307 break;
310 return PLUGIN_OK;