1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
29 static int dirs_count
;
31 #define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
32 #define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt"
33 #define RFA_FILE_TEXT ROCKBOX_DIR "/folder_advance_list.txt"
34 #define MAX_REMOVED_DIRS 10
38 int num_replaced_dirs
= 0;
39 char removed_dirs
[MAX_REMOVED_DIRS
][MAX_PATH
];
42 char folder
[][MAX_PATH
];
44 struct file_format
*list
= NULL
;
46 void update_screen(bool clear
)
51 rb
->snprintf(buf
,sizeof(buf
),"Folders: %d",dirs_count
);
55 rb
->screens
[i
]->clear_display();
56 rb
->screens
[i
]->putsxy(0,0,buf
);
57 rb
->screens
[i
]->update();
61 void traversedir(char* location
, char* name
)
65 char fullpath
[MAX_PATH
], path
[MAX_PATH
];
69 rb
->snprintf(fullpath
, sizeof(fullpath
), "%s/%s", location
, name
);
70 dir
= rb
->opendir(fullpath
);
72 entry
= rb
->readdir(dir
);
77 if (entry
->d_name
[0] == '.')
79 if ( !rb
->strcmp(entry
->d_name
,".")
80 || !rb
->strcmp(entry
->d_name
,"..")
81 || !rb
->strcmp(entry
->d_name
,".rockbox"))
87 /* check if path is removed directory, if so dont enter it */
88 rb
->snprintf(path
, MAX_PATH
, "%s/%s", fullpath
, entry
->d_name
);
90 rb
->strlcpy(path
, path
+ 1, sizeof(path
));
91 for(i
= 0; i
< num_replaced_dirs
; i
++)
93 if(!rb
->strcmp(path
, removed_dirs
[i
]))
102 struct dirinfo info
= rb
->dir_get_info(dir
, entry
);
103 if (info
.attribute
& ATTR_DIRECTORY
) {
106 rb
->snprintf(path
,MAX_PATH
,"%s/%s",fullpath
,entry
->d_name
);
107 start
= &path
[rb
->strlen(path
)];
108 rb
->memset(start
,0,&path
[MAX_PATH
-1]-start
);
109 rb
->write(fd
,path
,MAX_PATH
);
110 traversedir(fullpath
, entry
->d_name
);
113 if (*rb
->current_tick
- lasttick
> (HZ
/2)) {
114 update_screen(false);
115 lasttick
= *rb
->current_tick
;
116 if (rb
->action_userabort(TIMEOUT_NOBLOCK
))
123 entry
= rb
->readdir(dir
);
129 bool custom_dir(void)
132 char *starts
, line
[MAX_PATH
], formatted_line
[MAX_PATH
];
137 /* populate removed dirs array */
138 if((fd2
= rb
->open(RFADIR_FILE
,O_RDONLY
)) >= 0)
140 while ((rb
->read_line(fd2
, line
, MAX_PATH
- 1)) > 0)
142 if ((line
[0] == '-') && (line
[1] == '/') &&
143 (num_replaced_dirs
< MAX_REMOVED_DIRS
))
145 num_replaced_dirs
++;
146 rb
->strlcpy(removed_dirs
[num_replaced_dirs
- 1], line
+ 2,
153 if((fd2
= rb
->open(RFADIR_FILE
,O_RDONLY
)) >= 0)
155 while ((rb
->read_line(fd2
, line
, MAX_PATH
- 1)) > 0)
157 /* blank lines and removed dirs ignored */
158 if (rb
->strlen(line
) && ((line
[0] != '-') || (line
[1] != '/')))
160 /* remove preceeding '/'s from the line */
161 while(line
[0] == '/')
162 rb
->strlcpy(line
, line
+ 1, sizeof(line
));
164 rb
->snprintf(formatted_line
, MAX_PATH
, "/%s", line
);
166 dir_check
= rb
->opendir(formatted_line
);
170 rb
->closedir(dir_check
);
171 starts
= &formatted_line
[rb
->strlen(formatted_line
)];
172 rb
->memset(starts
, 0, &formatted_line
[MAX_PATH
-1]-starts
);
173 bool write_line
= true;
175 for(i
= 0; i
< num_replaced_dirs
; i
++)
177 if(!rb
->strcmp(line
, removed_dirs
[i
]))
187 rb
->write(fd
, formatted_line
, MAX_PATH
);
190 traversedir("", line
);
195 rb
->snprintf(buf
,sizeof(buf
),"Not found:");
198 rb
->screens
[i
]->puts(0,0,buf
);
199 rb
->screens
[i
]->puts(0, errors
, line
);
201 update_screen(false);
207 /* Press button to continue */
208 rb
->get_action(CONTEXT_STD
, TIMEOUT_BLOCK
);
219 fd
= rb
->open(RFA_FILE
,O_CREAT
|O_WRONLY
, 0666);
220 rb
->write(fd
,&dirs_count
,sizeof(int));
223 rb
->splashf(HZ
, "Couldnt open %s", RFA_FILE
);
226 #ifndef HAVE_LCD_CHARCELLS
229 lasttick
= *rb
->current_tick
;
234 rb
->lseek(fd
,0,SEEK_SET
);
235 rb
->write(fd
,&dirs_count
,sizeof(int));
237 rb
->splash(HZ
, "Done");
240 static const char* list_get_name_cb(int selected_item
, void* data
,
241 char* buf
, size_t buf_len
)
244 rb
->strlcpy(buf
, list
->folder
[selected_item
], buf_len
);
250 int myfd
= rb
->open(RFA_FILE
,O_RDONLY
);
253 buffer
= rb
->plugin_get_audio_buffer(&buffer_size
);
259 rb
->read(myfd
,buffer
,buffer_size
);
261 list
= (struct file_format
*)buffer
;
268 int myfd
= rb
->creat(RFA_FILE
, 0666);
271 rb
->splash(HZ
, "Could Not Open " RFA_FILE
);
274 int dirs_count
= 0, i
= 0;
275 rb
->write(myfd
,&dirs_count
,sizeof(int));
276 for ( ;i
<list
->count
;i
++)
278 if (list
->folder
[i
][0] != ' ')
281 rb
->write(myfd
,list
->folder
[i
],MAX_PATH
);
284 rb
->lseek(myfd
,0,SEEK_SET
);
285 rb
->write(myfd
,&dirs_count
,sizeof(int));
293 struct gui_synclist lists
;
296 int selection
, ret
= 0;
298 /* load the dat file if not already done */
299 if ((list
== NULL
|| list
->count
== 0) && (i
= load_list()) != 0)
301 rb
->splashf(HZ
*2, "Could not load %s, rv = %d", RFA_FILE
, i
);
305 dirs_count
= list
->count
;
307 rb
->gui_synclist_init(&lists
,list_get_name_cb
,0, false, 1, NULL
);
308 rb
->gui_synclist_set_icon_callback(&lists
,NULL
);
309 rb
->gui_synclist_set_nb_items(&lists
,list
->count
);
310 rb
->gui_synclist_limit_scroll(&lists
,true);
311 rb
->gui_synclist_select_item(&lists
, 0);
315 rb
->gui_synclist_draw(&lists
);
316 button
= rb
->get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
317 if (rb
->gui_synclist_do_button(&lists
,&button
,LIST_WRAP_UNLESS_HELD
))
319 selection
= rb
->gui_synclist_get_sel_pos(&lists
);
323 list
->folder
[selection
][0] = ' ';
324 list
->folder
[selection
][1] = '\0';
326 case ACTION_STD_CONTEXT
:
329 MENUITEM_STRINGLIST(menu
, "Remove Menu", NULL
,
330 "Remove Folder", "Remove Folder Tree");
332 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
335 list
->folder
[selection
][0] = ' ';
336 list
->folder
[selection
][1] = '\0';
341 rb
->strcpy(temp
,list
->folder
[selection
]);
342 len
= rb
->strlen(temp
);
343 for (i
=0;i
<list
->count
;i
++)
345 if (!rb
->strncmp(list
->folder
[i
],temp
,len
))
347 list
->folder
[i
][0] = ' ';
348 list
->folder
[i
][1] = '\0';
356 case ACTION_STD_CANCEL
:
358 MENUITEM_STRINGLIST(menu
, "Exit Menu", NULL
,
359 "Save and Exit", "Ignore Changes and Exit");
361 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
376 int export_list_to_file_text(void)
379 /* load the dat file if not already done */
380 if ((list
== NULL
|| list
->count
== 0) && (i
= load_list()) != 0)
382 rb
->splashf(HZ
*2, "Could not load %s, rv = %d", RFA_FILE
, i
);
386 if (list
->count
<= 0)
388 rb
->splashf(HZ
*2, "no dirs in list file: %s", RFA_FILE
);
392 /* create and open the file */
393 int myfd
= rb
->creat(RFA_FILE_TEXT
, 0666);
396 rb
->splashf(HZ
*4, "failed to open: fd = %d, file = %s",
397 myfd
, RFA_FILE_TEXT
);
401 /* write each directory to file */
402 for (i
= 0; i
< list
->count
; i
++)
404 if (list
->folder
[i
][0] != ' ')
406 rb
->fdprintf(myfd
, "%s\n", list
->folder
[i
]);
411 rb
->splash(HZ
, "Done");
415 int import_list_from_file_text(void)
419 buffer
= rb
->plugin_get_audio_buffer(&buffer_size
);
422 rb
->splash(HZ
*2, "failed to get audio buffer");
426 int myfd
= rb
->open(RFA_FILE_TEXT
, O_RDONLY
);
429 rb
->splashf(HZ
*2, "failed to open: %s", RFA_FILE_TEXT
);
433 /* set the list structure, and initialize count */
434 list
= (struct file_format
*)buffer
;
437 while ((rb
->read_line(myfd
, line
, MAX_PATH
- 1)) > 0)
439 /* copy the dir name, and skip the newline */
440 int len
= rb
->strlen(line
);
444 if (line
[len
-1] == 0x0A || line
[len
-1] == 0x0D)
447 (line
[len
-2] == 0x0A || line
[len
-2] == 0x0D))
451 rb
->strcpy(list
->folder
[list
->count
++], line
);
456 if (list
->count
== 0)
464 rb
->splash(HZ
, "Done");
468 int start_shuffled_play(void)
471 size_t max_shuffle_size
;
474 /* get memory for shuffling */
475 order
=rb
->plugin_get_buffer(&max_shuffle_size
);
476 max_shuffle_size
/=sizeof(int);
477 if (order
==NULL
|| max_shuffle_size
==0)
479 rb
->splashf(HZ
*2, "Not enough memory for shuffling");
483 /* load the dat file if not already done */
484 if ((list
== NULL
|| list
->count
== 0) && (i
= load_list()) != 0)
486 rb
->splashf(HZ
*2, "Could not load %s, rv = %d", RFA_FILE
, i
);
490 if (list
->count
<= 0)
492 rb
->splashf(HZ
*2, "no dirs in list file: %s", RFA_FILE
);
496 /* shuffle the thing */
497 rb
->srand(*rb
->current_tick
);
498 if(list
->count
>(int)max_shuffle_size
)
500 rb
->splashf(HZ
*2, "Too many folders: %d (room for %d)", list
->count
,(int)max_shuffle_size
);
503 for(i
=0;i
<list
->count
;i
++)
506 for(i
= list
->count
- 1; i
>= 0; i
--)
508 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
509 int candidate
= rb
->rand() % (i
+ 1);
511 /* now swap the values at the 'i' and 'candidate' positions */
512 int store
= order
[candidate
];
513 order
[candidate
] = order
[i
];
517 /* We don't want whatever is playing */
518 if (!(rb
->playlist_remove_all_tracks(NULL
) == 0
519 && rb
->playlist_create(NULL
, NULL
) == 0))
521 rb
->splashf(HZ
*2, "Could not clear playlist");
525 /* add the lot to the playlist */
526 for (i
= 0; i
< list
->count
; i
++)
528 if (list
->folder
[order
[i
]][0] != ' ')
530 rb
->playlist_insert_directory(NULL
,list
->folder
[order
[i
]],PLAYLIST_INSERT_LAST
,false,false);
532 if (rb
->action_userabort(TIMEOUT_NOBLOCK
))
537 rb
->splash(HZ
, "Done");
538 rb
->playlist_start(0,0);
542 enum plugin_status
main_menu(void)
545 MENUITEM_STRINGLIST(menu
, "Main Menu", NULL
,
546 "Generate Folder List",
548 "Export List To Textfile",
549 "Import List From Textfile",
555 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
557 case 0: /* generate */
558 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
562 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
563 rb
->cpu_boost(false);
565 #ifdef HAVE_REMOTE_LCD
566 rb
->remote_backlight_on();
571 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
576 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
577 rb
->cpu_boost(false);
579 #ifdef HAVE_REMOTE_LCD
580 rb
->remote_backlight_on();
584 case 2: /* export to textfile */
585 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
588 export_list_to_file_text();
589 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
590 rb
->cpu_boost(false);
592 #ifdef HAVE_REMOTE_LCD
593 rb
->remote_backlight_on();
597 case 3: /* import from textfile */
598 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
601 import_list_from_file_text();
602 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
603 rb
->cpu_boost(false);
605 #ifdef HAVE_REMOTE_LCD
606 rb
->remote_backlight_on();
611 if (!start_shuffled_play())
614 return PLUGIN_GOTO_WPS
;
622 enum plugin_status
plugin_start(const void* parameter
)