fix red.
[kugel-rb/myfork.git] / apps / plugins / random_folder_advance_config.c
blobba3f0d3f7e5dc3ae66668fae0d5942fa39e2b53c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jonathan Gordon
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"
22 #include "file.h"
24 PLUGIN_HEADER
26 static bool abort;
27 static int fd;
28 static int dirs_count;
29 static int lasttick;
30 #define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
31 #define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt"
32 #define RFA_FILE_TEXT ROCKBOX_DIR "/folder_advance_list.txt"
33 #define MAX_REMOVED_DIRS 10
35 char *buffer = NULL;
36 ssize_t buffer_size;
37 int num_replaced_dirs = 0;
38 char removed_dirs[MAX_REMOVED_DIRS][MAX_PATH];
39 struct file_format {
40 int count;
41 char folder[][MAX_PATH];
43 struct file_format *list = NULL;
45 void update_screen(bool clear)
47 char buf[15];
48 int i;
50 rb->snprintf(buf,sizeof(buf),"Folders: %d",dirs_count);
51 FOR_NB_SCREENS(i)
53 if(clear)
54 rb->screens[i]->clear_display();
55 rb->screens[i]->putsxy(0,0,buf);
56 rb->screens[i]->update();
60 void traversedir(char* location, char* name)
62 struct dirent *entry;
63 DIR* dir;
64 char fullpath[MAX_PATH], path[MAX_PATH];
65 bool check = false;
66 int i;
68 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
69 dir = rb->opendir(fullpath);
70 if (dir) {
71 entry = rb->readdir(dir);
72 while (entry) {
73 if (abort == true)
74 break;
75 /* Skip .. and . */
76 if (entry->d_name[0] == '.')
78 if ( !rb->strcmp(entry->d_name,".")
79 || !rb->strcmp(entry->d_name,"..")
80 || !rb->strcmp(entry->d_name,".rockbox"))
81 check = false;
82 else check = true;
84 else check = true;
86 /* check if path is removed directory, if so dont enter it */
87 rb->snprintf(path, MAX_PATH, "%s/%s", fullpath, entry->d_name);
88 while(path[0] == '/')
89 rb->strlcpy(path, path + 1, sizeof(path));
90 for(i = 0; i < num_replaced_dirs; i++)
92 if(!rb->strcmp(path, removed_dirs[i]))
94 check = false;
95 break;
99 if (check)
101 if (entry->attribute & ATTR_DIRECTORY) {
102 char *start;
103 dirs_count++;
104 rb->snprintf(path,MAX_PATH,"%s/%s",fullpath,entry->d_name);
105 start = &path[rb->strlen(path)];
106 rb->memset(start,0,&path[MAX_PATH-1]-start);
107 rb->write(fd,path,MAX_PATH);
108 traversedir(fullpath, entry->d_name);
111 if (*rb->current_tick - lasttick > (HZ/2)) {
112 update_screen(false);
113 lasttick = *rb->current_tick;
114 if (rb->action_userabort(TIMEOUT_NOBLOCK))
116 abort = true;
117 break;
121 entry = rb->readdir(dir);
123 rb->closedir(dir);
127 bool custom_dir(void)
129 DIR* dir_check;
130 char *starts, line[MAX_PATH], formatted_line[MAX_PATH];
131 static int fd2;
132 char buf[11];
133 int i, errors = 0;
135 /* populate removed dirs array */
136 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) > 0)
138 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
140 if ((line[0] == '-') && (line[1] == '/') &&
141 (num_replaced_dirs < MAX_REMOVED_DIRS))
143 num_replaced_dirs ++;
144 rb->strlcpy(removed_dirs[num_replaced_dirs - 1], line + 2,
145 sizeof(line));
148 rb->close(fd2);
151 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) > 0)
153 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
155 /* blank lines and removed dirs ignored */
156 if (rb->strlen(line) && ((line[0] != '-') || (line[1] != '/')))
158 /* remove preceeding '/'s from the line */
159 while(line[0] == '/')
160 rb->strlcpy(line, line + 1, sizeof(line));
162 rb->snprintf(formatted_line, MAX_PATH, "/%s", line);
164 dir_check = rb->opendir(formatted_line);
166 if (dir_check)
168 rb->closedir(dir_check);
169 starts = &formatted_line[rb->strlen(formatted_line)];
170 rb->memset(starts, 0, &formatted_line[MAX_PATH-1]-starts);
171 bool write_line = true;
173 for(i = 0; i < num_replaced_dirs; i++)
175 if(!rb->strcmp(line, removed_dirs[i]))
177 write_line = false;
178 break;
182 if(write_line)
184 dirs_count++;
185 rb->write(fd, formatted_line, MAX_PATH);
188 traversedir("", line);
190 else
192 errors ++;
193 rb->snprintf(buf,sizeof(buf),"Not found:");
194 FOR_NB_SCREENS(i)
196 rb->screens[i]->puts(0,0,buf);
197 rb->screens[i]->puts(0, errors, line);
199 update_screen(false);
203 rb->close(fd2);
204 if(errors)
205 /* Press button to continue */
206 rb->get_action(CONTEXT_STD, TIMEOUT_BLOCK);
208 else
209 return false;
210 return true;
213 void generate(void)
215 dirs_count = 0;
216 abort = false;
217 fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY);
218 rb->write(fd,&dirs_count,sizeof(int));
219 if (fd < 0)
221 rb->splashf(HZ, "Couldnt open %s", RFA_FILE);
222 return;
224 #ifndef HAVE_LCD_CHARCELLS
225 update_screen(true);
226 #endif
227 lasttick = *rb->current_tick;
229 if(!custom_dir())
230 traversedir("", "");
232 rb->lseek(fd,0,SEEK_SET);
233 rb->write(fd,&dirs_count,sizeof(int));
234 rb->close(fd);
235 rb->splash(HZ, "Done");
238 static const char* list_get_name_cb(int selected_item, void* data,
239 char* buf, size_t buf_len)
241 (void)data;
242 rb->strlcpy(buf, list->folder[selected_item], buf_len);
243 return buf;
246 int load_list(void)
248 int myfd = rb->open(RFA_FILE,O_RDONLY);
249 if (myfd < 0)
250 return -1;
251 buffer = rb->plugin_get_audio_buffer((size_t *)&buffer_size);
252 if (!buffer)
254 return -2;
257 rb->read(myfd,buffer,buffer_size);
258 rb->close(myfd);
259 list = (struct file_format *)buffer;
261 return 0;
264 int save_list(void)
266 int myfd = rb->creat(RFA_FILE);
267 if (myfd < 0)
269 rb->splash(HZ, "Could Not Open " RFA_FILE);
270 return -1;
272 int dirs_count = 0, i = 0;
273 rb->write(myfd,&dirs_count,sizeof(int));
274 for ( ;i<list->count;i++)
276 if (list->folder[i][0] != ' ')
278 dirs_count++;
279 rb->write(myfd,list->folder[i],MAX_PATH);
282 rb->lseek(myfd,0,SEEK_SET);
283 rb->write(myfd,&dirs_count,sizeof(int));
284 rb->close(myfd);
286 return 1;
289 int edit_list(void)
291 struct gui_synclist lists;
292 bool exit = false;
293 int button,i;
294 int selection, ret = 0;
296 /* load the dat file if not already done */
297 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
299 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
300 return -1;
303 dirs_count = list->count;
305 rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
306 rb->gui_synclist_set_icon_callback(&lists,NULL);
307 rb->gui_synclist_set_nb_items(&lists,list->count);
308 rb->gui_synclist_limit_scroll(&lists,true);
309 rb->gui_synclist_select_item(&lists, 0);
311 while (!exit)
313 rb->gui_synclist_draw(&lists);
314 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
315 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
316 continue;
317 selection = rb->gui_synclist_get_sel_pos(&lists);
318 switch (button)
320 case ACTION_STD_OK:
321 list->folder[selection][0] = ' ';
322 list->folder[selection][1] = '\0';
323 break;
324 case ACTION_STD_CONTEXT:
326 int len;
327 MENUITEM_STRINGLIST(menu, "Remove Menu", NULL,
328 "Remove Folder", "Remove Folder Tree");
330 switch (rb->do_menu(&menu, NULL, NULL, false))
332 case 0:
333 list->folder[selection][0] = ' ';
334 list->folder[selection][1] = '\0';
335 break;
336 case 1:
338 char temp[MAX_PATH];
339 rb->strcpy(temp,list->folder[selection]);
340 len = rb->strlen(temp);
341 for (i=0;i<list->count;i++)
343 if (!rb->strncmp(list->folder[i],temp,len))
345 list->folder[i][0] = ' ';
346 list->folder[i][1] = '\0';
350 break;
353 break;
354 case ACTION_STD_CANCEL:
356 MENUITEM_STRINGLIST(menu, "Exit Menu", NULL,
357 "Save and Exit", "Ignore Changes and Exit");
359 switch (rb->do_menu(&menu, NULL, NULL, false))
361 case 0:
362 save_list();
363 case 1:
364 exit = true;
365 ret = -2;
368 break;
371 return ret;
374 int export_list_to_file_text(void)
376 int i = 0;
377 /* load the dat file if not already done */
378 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
380 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
381 return 0;
384 if (list->count <= 0)
386 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
387 return 0;
390 /* create and open the file */
391 int myfd = rb->creat(RFA_FILE_TEXT);
392 if (myfd < 0)
394 rb->splashf(HZ*4, "failed to open: fd = %d, file = %s",
395 myfd, RFA_FILE_TEXT);
396 return -1;
399 /* write each directory to file */
400 for (i = 0; i < list->count; i++)
402 if (list->folder[i][0] != ' ')
404 rb->fdprintf(myfd, "%s\n", list->folder[i]);
408 rb->close(myfd);
409 rb->splash(HZ, "Done");
410 return 1;
413 int import_list_from_file_text(void)
415 char line[MAX_PATH];
417 buffer = rb->plugin_get_audio_buffer((size_t *)&buffer_size);
418 if (buffer == NULL)
420 rb->splash(HZ*2, "failed to get audio buffer");
421 return -1;
424 int myfd = rb->open(RFA_FILE_TEXT, O_RDONLY);
425 if (myfd < 0)
427 rb->splashf(HZ*2, "failed to open: %s", RFA_FILE_TEXT);
428 return -1;
431 /* set the list structure, and initialize count */
432 list = (struct file_format *)buffer;
433 list->count = 0;
435 while ((rb->read_line(myfd, line, MAX_PATH - 1)) > 0)
437 /* copy the dir name, and skip the newline */
438 int len = rb->strlen(line);
439 /* remove CRs */
440 if (len > 0)
442 if (line[len-1] == 0x0A || line[len-1] == 0x0D)
443 line[len-1] = 0x00;
444 if (len > 1 &&
445 (line[len-2] == 0x0A || line[len-2] == 0x0D))
446 line[len-2] = 0x00;
449 rb->strcpy(list->folder[list->count++], line);
452 rb->close(myfd);
454 if (list->count == 0)
456 load_list();
458 else
460 save_list();
462 rb->splash(HZ, "Done");
463 return list->count;
466 int start_shuffled_play(void)
468 int *order;
469 size_t max_shuffle_size;
470 int i = 0;
472 /* get memory for shuffling */
473 order=rb->plugin_get_buffer(&max_shuffle_size);
474 max_shuffle_size/=sizeof(int);
475 if (order==NULL || max_shuffle_size==0)
477 rb->splashf(HZ*2, "Not enough memory for shuffling");
478 return 0;
481 /* load the dat file if not already done */
482 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
484 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
485 return 0;
488 if (list->count <= 0)
490 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
491 return 0;
494 /* shuffle the thing */
495 rb->srand(*rb->current_tick);
496 if(list->count>(int)max_shuffle_size)
498 rb->splashf(HZ*2, "Too many folders: %d (room for %d)", list->count,(int)max_shuffle_size);
499 return 0;
501 for(i=0;i<list->count;i++)
502 order[i]=i;
504 for(i = list->count - 1; i >= 0; i--)
506 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
507 int candidate = rb->rand() % (i + 1);
509 /* now swap the values at the 'i' and 'candidate' positions */
510 int store = order[candidate];
511 order[candidate] = order[i];
512 order[i] = store;
515 /* We don't want whatever is playing */
516 if (!(rb->playlist_remove_all_tracks(NULL) == 0
517 && rb->playlist_create(NULL, NULL) == 0))
519 rb->splashf(HZ*2, "Could not clear playlist");
520 return 0;
523 /* add the lot to the playlist */
524 for (i = 0; i < list->count; i++)
526 if (list->folder[order[i]][0] != ' ')
528 rb->playlist_insert_directory(NULL,list->folder[order[i]],PLAYLIST_INSERT_LAST,false,false);
530 if (rb->action_userabort(TIMEOUT_NOBLOCK))
532 break;
535 rb->splash(HZ, "Done");
536 rb->playlist_start(0,0);
537 return 1;
540 enum plugin_status main_menu(void)
542 bool exit = false;
543 MENUITEM_STRINGLIST(menu, "Main Menu", NULL,
544 "Generate Folder List",
545 "Edit Folder List",
546 "Export List To Textfile",
547 "Import List From Textfile",
548 "Play Shuffled",
549 "Quit");
551 while (!exit)
553 switch (rb->do_menu(&menu, NULL, NULL, false))
555 case 0: /* generate */
556 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
557 rb->cpu_boost(true);
558 #endif
559 generate();
560 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
561 rb->cpu_boost(false);
562 #endif
563 #ifdef HAVE_REMOTE_LCD
564 rb->remote_backlight_on();
565 #endif
566 rb->backlight_on();
567 break;
568 case 1:
569 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
570 rb->cpu_boost(true);
571 #endif
572 if (edit_list() < 0)
573 exit = true;
574 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
575 rb->cpu_boost(false);
576 #endif
577 #ifdef HAVE_REMOTE_LCD
578 rb->remote_backlight_on();
579 #endif
580 rb->backlight_on();
581 break;
582 case 2: /* export to textfile */
583 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
584 rb->cpu_boost(true);
585 #endif
586 export_list_to_file_text();
587 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
588 rb->cpu_boost(false);
589 #endif
590 #ifdef HAVE_REMOTE_LCD
591 rb->remote_backlight_on();
592 #endif
593 rb->backlight_on();
594 break;
595 case 3: /* import from textfile */
596 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
597 rb->cpu_boost(true);
598 #endif
599 import_list_from_file_text();
600 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
601 rb->cpu_boost(false);
602 #endif
603 #ifdef HAVE_REMOTE_LCD
604 rb->remote_backlight_on();
605 #endif
606 rb->backlight_on();
607 break;
608 case 4:
609 if (!start_shuffled_play())
610 return PLUGIN_ERROR;
611 else
612 return PLUGIN_GOTO_WPS;
613 case 5:
614 return PLUGIN_OK;
617 return PLUGIN_OK;
620 enum plugin_status plugin_start(const void* parameter)
622 (void)parameter;
624 abort = false;
626 return main_menu();