Device detection based on USB PIDs. This is currently linux only and requires libusb...
[Rockbox.git] / apps / plugins / disktidy.c
blobaba78afb3ef9f27f7d6b2eef1d928fca9068ecb2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Dent
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
22 static struct plugin_api* rb;
24 /* function return values */
25 enum tidy_return
27 TIDY_RETURN_OK = 0,
28 TIDY_RETURN_ERROR = 1,
29 TIDY_RETURN_USB = 2,
30 TIDY_RETURN_ABORT = 3,
33 /* Which systems junk are we removing */
34 enum tidy_system
36 TIDY_MAC = 0,
37 TIDY_WIN = 1,
38 TIDY_BOTH = 2,
41 /* variable button definitions */
42 #if CONFIG_KEYPAD == PLAYER_PAD
43 #define TIDY_STOP BUTTON_STOP
45 #elif CONFIG_KEYPAD == RECORDER_PAD
46 #define TIDY_STOP BUTTON_OFF
48 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
49 #define TIDY_STOP BUTTON_OFF
51 #elif CONFIG_KEYPAD == ONDIO_PAD
52 #define TIDY_STOP BUTTON_OFF
54 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
55 (CONFIG_KEYPAD == IRIVER_H300_PAD)
56 #define TIDY_STOP BUTTON_OFF
58 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
59 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
60 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
61 #define TIDY_STOP BUTTON_MENU
63 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
64 #define TIDY_STOP BUTTON_POWER
66 #elif CONFIG_KEYPAD == GIGABEAT_PAD
67 #define TIDY_STOP BUTTON_POWER
69 #elif CONFIG_KEYPAD == SANSA_E200_PAD
70 #define TIDY_STOP BUTTON_POWER
72 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
73 #define TIDY_STOP BUTTON_POWER
75 #else
76 #error DISKTIDY: Unsupported keypad
77 #endif
80 void tidy_lcd_status(const char *name, int *removed)
82 char text[24]; /* "Cleaned up nnnnn items" */
84 /* display status text */
85 rb->lcd_clear_display();
86 rb->lcd_puts(0, 0, "Working ...");
87 rb->lcd_puts(0, 1, name);
88 rb->snprintf(text, 24, "Cleaned up %d items", *removed);
89 #ifdef HAVE_LCD_BITMAP
90 rb->lcd_puts(0, 2, text);
91 #endif
92 rb->lcd_update();
95 void tidy_get_absolute_path(struct dirent *entry, char *fullname,
96 const char* name)
98 /* gets absolute path using dirent and name */
99 rb->strcpy(fullname, name);
100 if (rb->strlen(name) > 1)
102 rb->strcat(fullname, "/");
104 rb->strcat(fullname, entry->d_name);
107 enum tidy_return tidy_removedir(const char *name, int *removed)
109 /* delete directory */
110 struct dirent *entry;
111 enum tidy_return status = TIDY_RETURN_OK;
112 int button;
113 DIR *dir;
114 char fullname[MAX_PATH];
116 /* display status text */
117 tidy_lcd_status(name, removed);
119 rb->yield();
121 dir = rb->opendir(name);
122 if (dir)
124 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
125 /* walk directory */
127 /* check for user input and usb connect */
128 button = rb->button_get(false);
129 if (button == TIDY_STOP)
131 rb->closedir(dir);
132 return TIDY_RETURN_ABORT;
134 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
136 rb->closedir(dir);
137 return TIDY_RETURN_USB;
140 rb->yield();
142 /* get absolute path */
143 tidy_get_absolute_path(entry, fullname, name);
145 if (entry->attribute & ATTR_DIRECTORY)
147 /* dir ignore "." and ".." */
148 if ((rb->strcmp(entry->d_name, ".") != 0) && \
149 (rb->strcmp(entry->d_name, "..") != 0))
151 tidy_removedir(fullname, removed);
154 else
156 /* file */
157 *removed += 1;
158 rb->remove(fullname);
161 rb->closedir(dir);
162 /* rmdir */
163 *removed += 1;
164 rb->rmdir(name);
166 else
168 status = TIDY_RETURN_ERROR;
170 return status;
173 enum tidy_return tidy_clean(const char *name, int *removed, \
174 enum tidy_system system)
176 /* deletes junk files and dirs left by system */
177 struct dirent *entry;
178 enum tidy_return status = TIDY_RETURN_OK;
179 int button;
180 int del; /* has the item been deleted */
181 DIR *dir;
182 char fullname[MAX_PATH];
184 /* display status text */
185 tidy_lcd_status(name, removed);
187 rb->yield();
189 dir = rb->opendir(name);
190 if (dir)
192 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
193 /* walk directory */
195 /* check for user input and usb connect */
196 button = rb->button_get(false);
197 if (button == TIDY_STOP)
199 rb->closedir(dir);
200 return TIDY_RETURN_ABORT;
202 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
204 rb->closedir(dir);
205 return TIDY_RETURN_USB;
208 rb->yield();
210 if (entry->attribute & ATTR_DIRECTORY)
212 /* directory ignore "." and ".." */
213 if ((rb->strcmp(entry->d_name, ".") != 0) && \
214 (rb->strcmp(entry->d_name, "..") != 0))
216 del = 0;
218 /* get absolute path */
219 tidy_get_absolute_path(entry, fullname, name);
221 /* check if we are in root directory "/" */
222 if (rb->strcmp(name, "/") == 0)
224 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
226 /* mac directories */
227 if (rb->strcmp(entry->d_name, ".Trashes") == 0)
229 /* delete dir */
230 tidy_removedir(fullname, removed);
231 del = 1;
235 if (del == 0)
237 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
239 /* windows directories */
240 if (rb->strcmp(entry->d_name, "Recycled") == 0 \
241 || rb->strcmp(entry->d_name, "System Volume Information") == 0)
243 /* delete dir */
244 tidy_removedir(fullname, removed);
245 del = 1;
251 if (del == 0)
253 /* dir not deleted so clean it */
254 status = tidy_clean(fullname, removed, system);
258 else
260 /* file */
261 del = 0;
263 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
265 /* remove mac files */
266 if ((rb->strcmp(entry->d_name, ".DS_Store") == 0) || \
267 (rb->strncmp(entry->d_name, "._", 2) == 0))
269 *removed += 1; /* increment removed files counter */
271 /* get absolute path */
272 char fullname[MAX_PATH];
273 tidy_get_absolute_path(entry, fullname, name);
275 /* delete file */
276 rb->remove(fullname);
277 del = 1;
281 if (del == 0)
283 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
285 /* remove windows files*/
286 if ((rb->strcmp(entry->d_name, "Thumbs.db") == 0))
288 *removed += 1; /* increment removed files counter */
290 /* get absolute path */
291 char fullname[MAX_PATH];
292 tidy_get_absolute_path(entry, fullname, name);
294 /* delete file */
295 rb->remove(fullname);
296 del = 1;
302 rb->closedir(dir);
303 return status;
305 else
307 return TIDY_RETURN_ERROR;
311 enum plugin_status tidy_do(enum tidy_system system)
313 /* clean disk and display num of items removed */
314 int removed = 0;
315 enum tidy_return status;
316 char text[24]; /* "Cleaned up nnnnn items" */
318 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
319 rb->cpu_boost(true);
320 #endif
322 status = tidy_clean("/", &removed, system);
324 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
325 rb->cpu_boost(false);
326 #endif
328 if ((status == TIDY_RETURN_OK) || (status == TIDY_RETURN_ABORT))
330 rb->lcd_clear_display();
331 rb->snprintf(text, 24, "Cleaned up %d items", removed);
332 if (status == TIDY_RETURN_ABORT)
334 rb->splash(HZ, "User aborted");
335 rb->lcd_clear_display();
337 rb->splash(HZ*2, text);
339 return status;
342 int tidy_lcd_menu(void)
344 int selection, ret = 2;
345 bool menu_quit = false;
347 MENUITEM_STRINGLIST(menu,"Disktidy Menu",NULL,"Start Cleaning",
348 "Files to Clean","Quit");
350 static const struct opt_items system_option[3] =
352 { "Mac", -1 },
353 { "Windows", -1 },
354 { "Both", -1 }
357 while (!menu_quit)
359 switch(rb->do_menu(&menu, &selection))
362 case 0:
363 menu_quit = true; /* start cleaning */
364 break;
366 case 1:
367 rb->set_option("Files to Clean", &ret, INT, system_option, 3, NULL);
368 break;
370 default:
371 ret = 99; /* exit plugin */
372 menu_quit = true;
373 break;
376 return ret;
379 /* this is the plugin entry point */
380 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
382 enum tidy_system system = TIDY_BOTH;
383 enum tidy_return status;
385 (void)parameter;
387 rb = api;
389 switch(tidy_lcd_menu())
391 case 0:
392 system = TIDY_MAC;
393 break;
394 case 1:
395 system = TIDY_WIN;
396 break;
397 case 2:
398 system = TIDY_BOTH;
399 break;
400 case 99:
401 return PLUGIN_OK;
402 default:
403 system = TIDY_BOTH;
406 while (true)
408 status = tidy_do(system);
410 switch (status)
412 case TIDY_RETURN_OK:
413 return PLUGIN_OK;
414 case TIDY_RETURN_ERROR:
415 return PLUGIN_ERROR;
416 case TIDY_RETURN_USB:
417 return PLUGIN_USB_CONNECTED;
418 case TIDY_RETURN_ABORT:
419 return PLUGIN_OK;
423 if (rb->default_event_handler(rb->button_get(false)) == SYS_USB_CONNECTED)
424 return PLUGIN_USB_CONNECTED;
426 rb->yield();
429 return PLUGIN_OK;