Fix returning too early (before cleanup) in RFAC, which led to not cancelling
[kugel-rb.git] / apps / plugins / random_folder_advance_config.c
blob28546a340c83af9241e92a231eece4b6ce76a44b
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->strncpy(path, path + 1, rb->strlen(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->strncpy(removed_dirs[num_replaced_dirs - 1], line + 2,
145 rb->strlen(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->strncpy(line, line + 1, rb->strlen(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");
237 char *list_get_name_cb(int selected_item, void* data, char* buf, size_t buf_len)
239 (void)data;
240 rb->strncpy(buf, list->folder[selected_item], buf_len);
241 return buf;
244 int load_list(void)
246 int myfd = rb->open(RFA_FILE,O_RDONLY);
247 if (myfd < 0)
248 return -1;
249 buffer = rb->plugin_get_audio_buffer((size_t *)&buffer_size);
250 if (!buffer)
252 return -2;
255 rb->read(myfd,buffer,buffer_size);
256 rb->close(myfd);
257 list = (struct file_format *)buffer;
259 return 0;
262 int save_list(void)
264 int myfd = rb->creat(RFA_FILE);
265 if (myfd < 0)
267 rb->splash(HZ, "Could Not Open " RFA_FILE);
268 return -1;
270 int dirs_count = 0, i = 0;
271 rb->write(myfd,&dirs_count,sizeof(int));
272 for ( ;i<list->count;i++)
274 if (list->folder[i][0] != ' ')
276 dirs_count++;
277 rb->write(myfd,list->folder[i],MAX_PATH);
280 rb->lseek(myfd,0,SEEK_SET);
281 rb->write(myfd,&dirs_count,sizeof(int));
282 rb->close(myfd);
284 return 1;
287 int edit_list(void)
289 struct gui_synclist lists;
290 bool exit = false;
291 int button,i;
292 int selection, ret = 0;
294 /* load the dat file if not already done */
295 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
297 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
298 return -1;
301 dirs_count = list->count;
303 rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
304 rb->gui_synclist_set_icon_callback(&lists,NULL);
305 rb->gui_synclist_set_nb_items(&lists,list->count);
306 rb->gui_synclist_limit_scroll(&lists,true);
307 rb->gui_synclist_select_item(&lists, 0);
309 while (!exit)
311 rb->gui_synclist_draw(&lists);
312 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
313 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
314 continue;
315 selection = rb->gui_synclist_get_sel_pos(&lists);
316 switch (button)
318 case ACTION_STD_OK:
319 list->folder[selection][0] = ' ';
320 list->folder[selection][1] = '\0';
321 break;
322 case ACTION_STD_CONTEXT:
324 int len;
325 MENUITEM_STRINGLIST(menu, "Remove Menu", NULL,
326 "Remove Folder", "Remove Folder Tree");
328 switch (rb->do_menu(&menu, NULL, NULL, false))
330 case 0:
331 list->folder[selection][0] = ' ';
332 list->folder[selection][1] = '\0';
333 break;
334 case 1:
336 char temp[MAX_PATH];
337 rb->strcpy(temp,list->folder[selection]);
338 len = rb->strlen(temp);
339 for (i=0;i<list->count;i++)
341 if (!rb->strncmp(list->folder[i],temp,len))
343 list->folder[i][0] = ' ';
344 list->folder[i][1] = '\0';
348 break;
351 break;
352 case ACTION_STD_CANCEL:
354 MENUITEM_STRINGLIST(menu, "Exit Menu", NULL,
355 "Save and Exit", "Ignore Changes and Exit");
357 switch (rb->do_menu(&menu, NULL, NULL, false))
359 case 0:
360 save_list();
361 case 1:
362 exit = true;
363 ret = -2;
366 break;
369 return ret;
372 int export_list_to_file_text(void)
374 int i = 0;
375 /* load the dat file if not already done */
376 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
378 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
379 return 0;
382 if (list->count <= 0)
384 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
385 return 0;
388 /* create and open the file */
389 int myfd = rb->creat(RFA_FILE_TEXT);
390 if (myfd < 0)
392 rb->splashf(HZ*4, "failed to open: fd = %d, file = %s",
393 myfd, RFA_FILE_TEXT);
394 return -1;
397 /* write each directory to file */
398 for (i = 0; i < list->count; i++)
400 if (list->folder[i][0] != ' ')
402 rb->fdprintf(myfd, "%s\n", list->folder[i]);
406 rb->close(myfd);
407 rb->splash(HZ, "Done");
408 return 1;
411 int import_list_from_file_text(void)
413 char line[MAX_PATH];
415 buffer = rb->plugin_get_audio_buffer((size_t *)&buffer_size);
416 if (buffer == NULL)
418 rb->splash(HZ*2, "failed to get audio buffer");
419 return -1;
422 int myfd = rb->open(RFA_FILE_TEXT, O_RDONLY);
423 if (myfd < 0)
425 rb->splashf(HZ*2, "failed to open: %s", RFA_FILE_TEXT);
426 return -1;
429 /* set the list structure, and initialize count */
430 list = (struct file_format *)buffer;
431 list->count = 0;
433 while ((rb->read_line(myfd, line, MAX_PATH - 1)) > 0)
435 /* copy the dir name, and skip the newline */
436 int len = rb->strlen(line);
437 /* remove CRs */
438 if (len > 0)
440 if (line[len-1] == 0x0A || line[len-1] == 0x0D)
441 line[len-1] = 0x00;
442 if (len > 1 &&
443 (line[len-2] == 0x0A || line[len-2] == 0x0D))
444 line[len-2] = 0x00;
447 rb->strcpy(list->folder[list->count++], line);
450 rb->close(myfd);
452 if (list->count == 0)
454 load_list();
456 else
458 save_list();
460 rb->splash(HZ, "Done");
461 return list->count;
464 int start_shuffled_play(void)
466 int *order;
467 size_t max_shuffle_size;
468 int i = 0;
470 /* get memory for shuffling */
471 order=rb->plugin_get_buffer(&max_shuffle_size);
472 max_shuffle_size/=sizeof(int);
473 if (order==NULL || max_shuffle_size==0)
475 rb->splashf(HZ*2, "Not enough memory for shuffling");
476 return 0;
479 /* load the dat file if not already done */
480 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
482 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
483 return 0;
486 if (list->count <= 0)
488 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
489 return 0;
492 /* shuffle the thing */
493 rb->srand(*rb->current_tick);
494 if(list->count>(int)max_shuffle_size)
496 rb->splashf(HZ*2, "Too many folders: %d (room for %d)", list->count,(int)max_shuffle_size);
497 return 0;
499 for(i=0;i<list->count;i++)
500 order[i]=i;
502 for(i = list->count - 1; i >= 0; i--)
504 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
505 int candidate = rb->rand() % (i + 1);
507 /* now swap the values at the 'i' and 'candidate' positions */
508 int store = order[candidate];
509 order[candidate] = order[i];
510 order[i] = store;
513 /* We don't want whatever is playing */
514 if (!(rb->playlist_remove_all_tracks(NULL) == 0
515 && rb->playlist_create(NULL, NULL) == 0))
517 rb->splashf(HZ*2, "Could not clear playlist");
518 return 0;
521 /* add the lot to the playlist */
522 for (i = 0; i < list->count; i++)
524 if (list->folder[order[i]][0] != ' ')
526 rb->playlist_insert_directory(NULL,list->folder[order[i]],PLAYLIST_INSERT_LAST,false,false);
528 if (rb->action_userabort(TIMEOUT_NOBLOCK))
530 break;
533 rb->splash(HZ, "Done");
534 rb->playlist_start(0,0);
535 return 1;
538 enum plugin_status main_menu(void)
540 bool exit = false;
541 MENUITEM_STRINGLIST(menu, "Main Menu", NULL,
542 "Generate Folder List",
543 "Edit Folder List",
544 "Export List To Textfile",
545 "Import List From Textfile",
546 "Play Shuffled",
547 "Quit");
549 while (!exit)
551 switch (rb->do_menu(&menu, NULL, NULL, false))
553 case 0: /* generate */
554 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
555 rb->cpu_boost(true);
556 #endif
557 generate();
558 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
559 rb->cpu_boost(false);
560 #endif
561 #ifdef HAVE_REMOTE_LCD
562 rb->remote_backlight_on();
563 #endif
564 rb->backlight_on();
565 break;
566 case 1:
567 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
568 rb->cpu_boost(true);
569 #endif
570 if (edit_list() < 0)
571 exit = true;
572 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
573 rb->cpu_boost(false);
574 #endif
575 #ifdef HAVE_REMOTE_LCD
576 rb->remote_backlight_on();
577 #endif
578 rb->backlight_on();
579 break;
580 case 2: /* export to textfile */
581 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
582 rb->cpu_boost(true);
583 #endif
584 export_list_to_file_text();
585 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
586 rb->cpu_boost(false);
587 #endif
588 #ifdef HAVE_REMOTE_LCD
589 rb->remote_backlight_on();
590 #endif
591 rb->backlight_on();
592 break;
593 case 3: /* import from textfile */
594 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
595 rb->cpu_boost(true);
596 #endif
597 import_list_from_file_text();
598 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
599 rb->cpu_boost(false);
600 #endif
601 #ifdef HAVE_REMOTE_LCD
602 rb->remote_backlight_on();
603 #endif
604 rb->backlight_on();
605 break;
606 case 4:
607 if (!start_shuffled_play())
608 return PLUGIN_ERROR;
609 else
610 return PLUGIN_GOTO_WPS;
611 case 5:
612 return PLUGIN_OK;
615 return PLUGIN_OK;
618 enum plugin_status plugin_start(const void* parameter)
620 (void)parameter;
622 abort = false;
624 return main_menu();