disable device tree during autodetection to give some (small) visual feedback.
[Rockbox.git] / apps / onplay.c
blob076f90ca59f6a540557c468d27b068b079764eb9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
25 #include "debug.h"
26 #include "sprintf.h"
27 #include "lcd.h"
28 #include "dir.h"
29 #include "file.h"
30 #include "audio.h"
31 #include "menu.h"
32 #include "lang.h"
33 #include "playlist.h"
34 #include "button.h"
35 #include "kernel.h"
36 #include "keyboard.h"
37 #include "mp3data.h"
38 #include "id3.h"
39 #include "screens.h"
40 #include "tree.h"
41 #include "buffer.h"
42 #include "settings.h"
43 #include "statusbar.h"
44 #include "playlist_viewer.h"
45 #include "talk.h"
46 #include "onplay.h"
47 #include "filetypes.h"
48 #include "plugin.h"
49 #include "bookmark.h"
50 #include "action.h"
51 #include "splash.h"
52 #include "yesno.h"
53 #include "menus/exported_menus.h"
54 #ifdef HAVE_LCD_BITMAP
55 #include "icons.h"
56 #endif
57 #include "sound_menu.h"
58 #if CONFIG_CODEC == SWCODEC
59 #include "menus/eq_menu.h"
60 #endif
61 #include "playlist_menu.h"
62 #include "playlist_catalog.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagtree.h"
65 #endif
66 #include "cuesheet.h"
67 #include "backdrop.h"
69 static int context;
70 static char* selected_file = NULL;
71 static int selected_file_attr = 0;
72 static int onplay_result = ONPLAY_OK;
73 static char clipboard_selection[MAX_PATH];
74 static int clipboard_selection_attr = 0;
75 static bool clipboard_is_copy = false;
77 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
78 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
79 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
80 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
81 static const struct menu_item_ex name = \
82 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
83 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
84 { (void*)name##_},{.callback_and_desc = & name##__}};
86 #ifdef HAVE_LCD_BITMAP
87 static void draw_slider(void);
88 #else
89 #define draw_slider()
90 #endif
91 /* ----------------------------------------------------------------------- */
92 /* Displays the bookmark menu options for the user to decide. This is an */
93 /* interface function. */
94 /* ----------------------------------------------------------------------- */
96 static int bookmark_menu_callback(int action,
97 const struct menu_item_ex *this_item);
98 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
99 ID2P(LANG_BOOKMARK_MENU_CREATE),
100 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
101 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
102 ID2P(LANG_BOOKMARK_MENU_LIST),
103 bookmark_load_menu, NULL,
104 bookmark_menu_callback, Icon_Bookmark);
105 MAKE_MENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
106 Icon_Bookmark, &bookmark_create_menu_item, &bookmark_load_menu_item);
107 static int bookmark_menu_callback(int action,
108 const struct menu_item_ex *this_item)
110 (void)this_item;
111 switch (action)
113 case ACTION_REQUEST_MENUITEM:
114 if (this_item == &bookmark_load_menu_item)
116 if (bookmark_exist() == 0)
117 return ACTION_EXIT_MENUITEM;
119 /* hide the bookmark menu if there is no playback */
120 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
121 return ACTION_EXIT_MENUITEM;
122 break;
123 #ifdef HAVE_LCD_CHARCELLS
124 case ACTION_ENTER_MENUITEM:
125 status_set_param(true);
126 break;
127 #endif
128 case ACTION_EXIT_MENUITEM:
129 #ifdef HAVE_LCD_CHARCELLS
130 status_set_param(false);
131 #endif
132 settings_save();
133 break;
135 return action;
138 static bool list_viewers(void)
140 int ret = filetype_list_viewers(selected_file);
141 if (ret == PLUGIN_USB_CONNECTED)
142 onplay_result = ONPLAY_RELOAD_DIR;
143 return false;
146 static bool shuffle_playlist(void)
148 playlist_sort(NULL, true);
149 playlist_randomise(NULL, current_tick, true);
151 return false;
154 static bool save_playlist(void)
156 save_playlist_screen(NULL);
157 return false;
160 static bool add_to_playlist(int position, bool queue)
162 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
163 const char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 const struct text_message message={lines, 2};
169 gui_syncsplash(0, ID2P(LANG_WAIT));
171 if (new_playlist)
172 playlist_create(NULL, NULL);
174 /* always set seed before inserting shuffled */
175 if (position == PLAYLIST_INSERT_SHUFFLED)
176 srand(current_tick);
178 #ifdef HAVE_TAGCACHE
179 if (context == CONTEXT_ID3DB)
181 tagtree_insert_selection_playlist(position, queue);
183 else
184 #endif
186 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
187 playlist_insert_track(NULL, selected_file, position, queue, true);
188 else if (selected_file_attr & ATTR_DIRECTORY)
190 bool recurse = false;
192 if (global_settings.recursive_dir_insert != RECURSE_ASK)
193 recurse = (bool)global_settings.recursive_dir_insert;
194 else
196 /* Ask if user wants to recurse directory */
197 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
200 playlist_insert_directory(NULL, selected_file, position, queue,
201 recurse);
203 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
204 playlist_insert_playlist(NULL, selected_file, position, queue);
207 if (new_playlist && (playlist_amount() > 0))
209 /* nothing is currently playing so begin playing what we just
210 inserted */
211 if (global_settings.playlist_shuffle)
212 playlist_shuffle(current_tick, -1);
213 playlist_start(0,0);
214 gui_syncstatusbar_draw(&statusbars, false);
215 onplay_result = ONPLAY_START_PLAY;
218 return false;
221 static bool view_playlist(void)
223 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
224 bool result;
226 result = playlist_viewer_ex(selected_file);
228 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
229 onplay_result == ONPLAY_OK)
230 /* playlist was started from viewer */
231 onplay_result = ONPLAY_START_PLAY;
233 return result;
236 static bool cat_add_to_a_playlist(void)
238 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
239 false, NULL);
242 static bool cat_add_to_a_new_playlist(void)
244 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
245 true, NULL);
249 static int cat_playlist_callback(int action,
250 const struct menu_item_ex *this_item);
251 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
252 catalog_view_playlists, 0, cat_playlist_callback,
253 Icon_Playlist);
254 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
255 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
256 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
257 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
258 MAKE_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
259 Icon_Playlist, &cat_view_lists,
260 &cat_add_to_list, &cat_add_to_new );
262 static int cat_playlist_callback(int action,
263 const struct menu_item_ex *this_item)
265 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
266 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
267 ((selected_file_attr & ATTR_DIRECTORY) == 0))
269 return ACTION_EXIT_MENUITEM;
272 switch (action)
274 case ACTION_REQUEST_MENUITEM:
275 if (this_item == &cat_view_lists)
277 if (context == CONTEXT_WPS)
278 return action;
280 else if (selected_file && /* set before calling this menu,
281 so safe */
282 ((audio_status() & AUDIO_STATUS_PLAY &&
283 context == CONTEXT_WPS) ||
284 context == CONTEXT_TREE))
286 return action;
288 else
289 return ACTION_EXIT_MENUITEM;
290 break;
292 return action;
296 /* CONTEXT_WPS playlist options */
297 MENUITEM_FUNCTION(playlist_viewer_item, 0,
298 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
299 NULL, NULL, Icon_Playlist);
300 MENUITEM_FUNCTION(search_playlist_item, 0,
301 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
302 NULL, NULL, Icon_Playlist);
303 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
304 save_playlist, NULL, NULL, Icon_Playlist);
305 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
306 shuffle_playlist, NULL, NULL, Icon_Playlist);
307 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
308 NULL, Icon_Playlist,
309 &playlist_viewer_item, &search_playlist_item,
310 &playlist_save_item, &reshuffle_item
313 /* CONTEXT_[TREE|ID3DB] playlist options */
314 static int playlist_insert_func(void *param)
316 add_to_playlist((intptr_t)param, false);
317 return 0;
319 static int playlist_queue_func(void *param)
321 add_to_playlist((intptr_t)param, true);
322 return 0;
324 static int treeplaylist_wplayback_callback(int action,
325 const struct menu_item_ex*
326 this_item)
328 (void)this_item;
329 switch (action)
331 case ACTION_REQUEST_MENUITEM:
332 if (audio_status() & AUDIO_STATUS_PLAY)
333 return action;
334 else
335 return ACTION_EXIT_MENUITEM;
336 break;
338 return action;
341 static int treeplaylist_callback(int action,
342 const struct menu_item_ex *this_item);
344 /* insert items */
345 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
346 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
347 treeplaylist_callback, Icon_Playlist);
348 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
349 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
350 treeplaylist_wplayback_callback, Icon_Playlist);
351 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
352 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
353 treeplaylist_wplayback_callback, Icon_Playlist);
354 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
355 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
356 treeplaylist_wplayback_callback, Icon_Playlist);
357 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
358 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
359 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
360 Icon_Playlist);
361 /* queue items */
362 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
363 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
364 treeplaylist_wplayback_callback, Icon_Playlist);
365 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
366 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
367 treeplaylist_wplayback_callback, Icon_Playlist);
368 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
369 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
370 treeplaylist_wplayback_callback, Icon_Playlist);
371 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
372 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
373 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
374 treeplaylist_wplayback_callback, Icon_Playlist);
375 /* replace playlist */
376 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
377 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
378 treeplaylist_wplayback_callback, Icon_Playlist);
379 /* others */
381 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
382 view_playlist, NULL,
383 treeplaylist_callback, Icon_Playlist);
385 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
386 treeplaylist_callback, Icon_Playlist,
388 /* view */
389 &view_playlist_item,
391 /* insert */
392 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
393 &i_last_pl_item, &i_shuf_pl_item,
395 /* queue */
396 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
397 &q_shuf_pl_item,
399 /* replace */
400 &replace_pl_item
402 static int treeplaylist_callback(int action,
403 const struct menu_item_ex *this_item)
405 (void)this_item;
406 switch (action)
408 case ACTION_REQUEST_MENUITEM:
409 if (this_item == &tree_playlist_menu)
411 if (((selected_file_attr & FILE_ATTR_MASK) ==
412 FILE_ATTR_AUDIO) ||
413 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
414 (selected_file_attr & ATTR_DIRECTORY))
416 return action;
418 else
419 return ACTION_EXIT_MENUITEM;
421 else if (this_item == &view_playlist_item)
423 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
424 context == CONTEXT_TREE)
425 return action;
426 else
427 return ACTION_EXIT_MENUITEM;
429 else if (this_item == &i_pl_item_no_play)
431 if (!(audio_status() & AUDIO_STATUS_PLAY))
433 return action;
435 else
436 return ACTION_EXIT_MENUITEM;
438 else if (this_item == &i_shuf_pl_item)
441 if (audio_status() & AUDIO_STATUS_PLAY)
443 return action;
445 else if ((this_item == &i_shuf_pl_item) &&
446 ((selected_file_attr & ATTR_DIRECTORY) ||
447 ((selected_file_attr & FILE_ATTR_MASK) ==
448 FILE_ATTR_M3U)))
450 return action;
452 return ACTION_EXIT_MENUITEM;
454 break;
456 return action;
459 /* helper function to remove a non-empty directory */
460 static int remove_dir(char* dirname, int len)
462 int result = 0;
463 DIR* dir;
464 int dirlen = strlen(dirname);
465 int i;
467 dir = opendir(dirname);
468 if (!dir)
469 return -1; /* open error */
471 while(true)
473 struct dirent* entry;
474 /* walk through the directory content */
475 entry = readdir(dir);
476 if (!entry)
477 break;
479 dirname[dirlen] ='\0';
480 FOR_NB_SCREENS(i){
481 screens[i].puts(0,1,dirname);
482 screens[i].update();
485 /* append name to current directory */
486 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
487 if (entry->attribute & ATTR_DIRECTORY)
488 { /* remove a subdirectory */
489 if (!strcmp((char *)entry->d_name, ".") ||
490 !strcmp((char *)entry->d_name, ".."))
491 continue; /* skip these */
493 /* inform the user which dir we're deleting */
495 result = remove_dir(dirname, len); /* recursion */
496 if (result)
497 break; /* or better continue, delete what we can? */
499 else
500 { /* remove a file */
501 draw_slider();
502 result = remove(dirname);
504 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
506 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
507 result = -1;
508 break;
511 closedir(dir);
513 if (!result)
514 { /* remove the now empty directory */
515 dirname[dirlen] = '\0'; /* terminate to original length */
517 result = rmdir(dirname);
520 return result;
524 /* share code for file and directory deletion, saves space */
525 static bool delete_handler(bool is_dir)
527 char file_to_delete[MAX_PATH];
528 strcpy(file_to_delete, selected_file);
530 const char *lines[]={
531 ID2P(LANG_REALLY_DELETE),
532 file_to_delete
534 const char *yes_lines[]={
535 ID2P(LANG_DELETED),
536 file_to_delete
539 struct text_message message={lines, 2};
540 struct text_message yes_message={yes_lines, 2};
542 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
543 return false;
545 gui_syncsplash(0, str(LANG_DELETING));
547 int res;
548 if (is_dir)
550 char pathname[MAX_PATH]; /* space to go deep */
551 cpu_boost(true);
552 strncpy(pathname, file_to_delete, sizeof pathname);
553 res = remove_dir(pathname, sizeof(pathname));
554 cpu_boost(false);
556 else
557 res = remove(file_to_delete);
559 if (!res)
560 onplay_result = ONPLAY_RELOAD_DIR;
562 return false;
566 static bool delete_file(void)
568 return delete_handler(false);
571 static bool delete_dir(void)
573 return delete_handler(true);
576 #if LCD_DEPTH > 1
577 static bool set_backdrop(void)
579 /* load the image */
580 if(load_main_backdrop(selected_file)) {
581 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
582 set_file(selected_file, (char *)global_settings.backdrop_file,
583 MAX_FILENAME);
584 show_main_backdrop();
585 return true;
586 } else {
587 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
588 return false;
591 #endif
593 static bool rename_file(void)
595 char newname[MAX_PATH];
596 char* ptr = strrchr(selected_file, '/') + 1;
597 int pathlen = (ptr - selected_file);
598 strncpy(newname, selected_file, sizeof newname);
599 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
600 if (!strlen(newname + pathlen) ||
601 (rename(selected_file, newname) < 0)) {
602 lcd_clear_display();
603 lcd_puts(0,0,str(LANG_RENAME));
604 lcd_puts(0,1,str(LANG_FAILED));
605 lcd_update();
606 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
607 sleep(HZ*2);
609 else
610 onplay_result = ONPLAY_RELOAD_DIR;
613 return false;
616 static bool create_dir(void)
618 char dirname[MAX_PATH];
619 char *cwd;
620 int rc;
621 int pathlen;
623 cwd = getcwd(NULL, 0);
624 memset(dirname, 0, sizeof dirname);
626 snprintf(dirname, sizeof dirname, "%s/",
627 cwd[1] ? cwd : "");
629 pathlen = strlen(dirname);
630 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
631 if (rc < 0)
632 return false;
634 rc = mkdir(dirname);
635 if (rc < 0) {
636 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
637 gui_syncsplash(HZ, (unsigned char *)"%s %s",
638 str(LANG_CREATE_DIR), str(LANG_FAILED));
639 } else {
640 onplay_result = ONPLAY_RELOAD_DIR;
643 return true;
646 static bool properties(void)
648 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
649 selected_file))
650 onplay_result = ONPLAY_RELOAD_DIR;
651 return false;
654 /* Store the current selection in the clipboard */
655 static bool clipboard_clip(bool copy)
657 clipboard_selection[0] = 0;
658 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
659 clipboard_selection_attr = selected_file_attr;
660 clipboard_is_copy = copy;
662 return true;
665 static bool clipboard_cut(void)
667 return clipboard_clip(false);
670 static bool clipboard_copy(void)
672 return clipboard_clip(true);
675 #ifdef HAVE_LCD_BITMAP
676 static void draw_slider(void)
678 int i;
679 FOR_NB_SCREENS(i)
681 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
682 LCD_WIDTH-2, 2*screens[i].char_height-1);
683 screens[i].update();
686 #endif
688 /* Paste a file to a new directory. Will overwrite always. */
689 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
691 int src_fd, target_fd;
692 size_t buffersize;
693 ssize_t size, bytesread, byteswritten;
694 char *buffer;
695 bool result = false;
697 if (copy) {
698 /* See if we can get the plugin buffer for the file copy buffer */
699 buffer = (char *) plugin_get_buffer(&buffersize);
700 if (buffer == NULL || buffersize < 512) {
701 /* Not large enough, try for a disk sector worth of stack
702 instead */
703 buffersize = 512;
704 buffer = (char *) __builtin_alloca(buffersize);
707 if (buffer == NULL) {
708 return false;
711 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
712 size */
714 src_fd = open(src, O_RDONLY);
716 if (src_fd >= 0) {
717 target_fd = creat(target);
719 if (target_fd >= 0) {
720 result = true;
722 size = filesize(src_fd);
724 if (size == -1) {
725 result = false;
728 while(size > 0) {
729 bytesread = read(src_fd, buffer, buffersize);
731 if (bytesread == -1) {
732 result = false;
733 break;
736 size -= bytesread;
738 while(bytesread > 0) {
739 byteswritten = write(target_fd, buffer, bytesread);
741 if (byteswritten == -1) {
742 result = false;
743 size = 0;
744 break;
747 bytesread -= byteswritten;
748 draw_slider();
752 close(target_fd);
754 /* Copy failed. Cleanup. */
755 if (!result) {
756 remove(target);
760 close(src_fd);
762 } else {
763 result = rename(src, target) == 0;
764 #ifdef HAVE_MULTIVOLUME
765 if (!result) {
766 if (errno == EXDEV) {
767 /* Failed because cross volume rename doesn't work. Copy
768 instead */
769 result = clipboard_pastefile(src, target, true);
771 if (result) {
772 result = remove(src) == 0;
776 #endif
779 return result;
782 /* Paste a directory to a new location. Designed to be called by
783 clipboard_paste */
784 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
785 int targetlen, bool copy)
787 DIR *srcdir;
788 int srcdirlen = strlen(src);
789 int targetdirlen = strlen(target);
790 bool result = true;
792 if (!file_exists(target)) {
793 if (!copy) {
794 /* Just move the directory */
795 result = rename(src, target) == 0;
797 #ifdef HAVE_MULTIVOLUME
798 if (!result && errno == EXDEV) {
799 /* Try a copy as we're going across devices */
800 result = clipboard_pastedirectory(src, srclen, target,
801 targetlen, true);
803 /* If it worked, remove the source directory */
804 if (result) {
805 remove_dir(src, srclen);
808 #endif
809 return result;
810 } else {
811 /* Make a directory to copy things to */
812 result = mkdir(target) == 0;
816 /* Check if something went wrong already */
817 if (!result) {
818 return result;
821 srcdir = opendir(src);
822 if (!srcdir) {
823 return false;
826 /* This loop will exit as soon as there's a problem */
827 while(result)
829 struct dirent* entry;
830 /* walk through the directory content */
831 entry = readdir(srcdir);
832 if (!entry)
833 break;
835 /* append name to current directory */
836 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
837 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
838 entry->d_name);
840 DEBUGF("Copy %s to %s\n", src, target);
842 if (entry->attribute & ATTR_DIRECTORY)
843 { /* copy/move a subdirectory */
844 if (!strcmp((char *)entry->d_name, ".") ||
845 !strcmp((char *)entry->d_name, ".."))
846 continue; /* skip these */
848 result = clipboard_pastedirectory(src, srclen, target, targetlen,
849 copy); /* recursion */
851 else
852 { /* copy/move a file */
853 result = clipboard_pastefile(src, target, copy);
857 closedir(srcdir);
859 if (result) {
860 src[srcdirlen] = '\0'; /* terminate to original length */
861 target[targetdirlen] = '\0'; /* terminate to original length */
864 return result;
867 /* Paste the clipboard to the current directory */
868 static bool clipboard_paste(void)
870 char target[MAX_PATH];
871 char *cwd, *nameptr;
872 bool success;
874 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
875 static const struct text_message message={lines, 1};
877 /* Get the name of the current directory */
878 cwd = getcwd(NULL, 0);
880 /* Figure out the name of the selection */
881 nameptr = strrchr(clipboard_selection, '/');
883 /* Final target is current directory plus name of selection */
884 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
886 /* If the target existed but they choose not to overwite, exit */
887 if (file_exists(target) &&
888 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
889 return false;
892 if (clipboard_is_copy) {
893 gui_syncsplash(0, ID2P(LANG_COPYING));
895 else
897 gui_syncsplash(0, ID2P(LANG_MOVING));
900 /* Now figure out what we're doing */
901 cpu_boost(true);
902 if (clipboard_selection_attr & ATTR_DIRECTORY) {
903 /* Recursion. Set up external stack */
904 char srcpath[MAX_PATH];
905 char targetpath[MAX_PATH];
906 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
908 /* Do not allow the user to paste a directory into a dir they are
909 copying */
910 success = 0;
912 else
914 strncpy(srcpath, clipboard_selection, sizeof srcpath);
915 strncpy(targetpath, target, sizeof targetpath);
917 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
918 target, sizeof(targetpath), clipboard_is_copy);
920 } else {
921 success = clipboard_pastefile(clipboard_selection, target,
922 clipboard_is_copy);
924 cpu_boost(false);
926 /* Did it work? */
927 if (success) {
928 /* Reset everything */
929 clipboard_selection[0] = 0;
930 clipboard_selection_attr = 0;
931 clipboard_is_copy = false;
933 /* Force reload of the current directory */
934 onplay_result = ONPLAY_RELOAD_DIR;
935 } else {
936 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
937 gui_syncsplash(HZ, (unsigned char *)"%s %s",
938 str(LANG_PASTE), str(LANG_FAILED));
941 return true;
944 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
945 #ifdef HAVE_TAGCACHE
946 static int set_rating_inline(void)
948 struct mp3entry* id3 = audio_current_track();
949 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
951 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
952 NULL, 1, 0, 10, NULL, NULL);
953 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
955 else
956 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
957 return 0;
959 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
961 (void)this_item;
962 switch (action)
964 case ACTION_REQUEST_MENUITEM:
965 if (!selected_file || !global_settings.runtimedb)
966 return ACTION_EXIT_MENUITEM;
967 break;
969 return action;
971 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
972 set_rating_inline, NULL,
973 ratingitem_callback, Icon_Questionmark);
974 #endif
976 static bool view_cue(void)
978 struct mp3entry* id3 = audio_current_track();
979 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
981 browse_cuesheet(curr_cue);
983 return false;
985 static int view_cue_item_callback(int action,
986 const struct menu_item_ex *this_item)
988 (void)this_item;
989 struct mp3entry* id3 = audio_current_track();
990 switch (action)
992 case ACTION_REQUEST_MENUITEM:
993 if (!selected_file || !cuesheet_is_enabled()
994 || !id3 || !id3->cuesheet_type)
995 return ACTION_EXIT_MENUITEM;
996 break;
998 return action;
1000 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1001 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1003 /* CONTEXT_WPS items */
1004 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1005 browse_id3, NULL, NULL, Icon_NOICON);
1006 #ifdef HAVE_PITCHSCREEN
1007 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1008 pitch_screen, NULL, NULL, Icon_Audio);
1009 #endif
1010 #if CONFIG_CODEC == SWCODEC
1011 MENUITEM_FUNCTION(eq_menu_graphical_item, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
1012 eq_menu_graphical, NULL, NULL, Icon_Audio);
1013 MENUITEM_FUNCTION(eq_browse_presets_item, 0, ID2P(LANG_EQUALIZER_BROWSE),
1014 eq_browse_presets, NULL, NULL, Icon_Audio);
1015 #endif
1017 /* study mode setting toggling */
1019 static char* study_mode_toggle_get_name(int selected_item, void * data,
1020 char *buffer)
1022 (void)selected_item;
1023 (void)data;
1024 snprintf(buffer, MAX_PATH,
1025 global_settings.study_mode ? str(LANG_DISABLE_STUDY_MODE)
1026 : str(LANG_ENABLE_STUDY_MODE));
1027 return buffer;
1030 static int study_mode_toggle_speak_item(int selected_item, void * data)
1032 (void)selected_item;
1033 (void)data;
1034 talk_id(global_settings.study_mode ? LANG_DISABLE_STUDY_MODE
1035 : LANG_ENABLE_STUDY_MODE, false);
1036 return 0;
1039 static int toggle_study_mode(void * param)
1041 (void)param;
1042 global_settings.study_mode ^= 1;
1043 return 0;
1046 MENUITEM_FUNCTION_DYNTEXT(study_mode_toggle, 0,
1047 toggle_study_mode, NULL,
1048 study_mode_toggle_get_name,
1049 study_mode_toggle_speak_item,
1050 NULL, NULL, Icon_NOICON);
1052 /* CONTEXT_[TREE|ID3DB] items */
1053 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1054 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1055 rename_file, NULL, clipboard_callback, Icon_NOICON);
1056 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1057 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1058 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1059 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1060 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1061 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1062 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1063 delete_file, NULL, clipboard_callback, Icon_NOICON);
1064 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1065 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1066 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1067 properties, NULL, clipboard_callback, Icon_NOICON);
1068 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1069 create_dir, NULL, clipboard_callback, Icon_NOICON);
1070 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1071 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1072 #if LCD_DEPTH > 1
1073 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1074 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1075 #endif
1076 #ifdef HAVE_RECORDING
1077 static bool set_recdir(void)
1079 strncpy(global_settings.rec_directory,
1080 selected_file, MAX_FILENAME+1);
1081 settings_save();
1082 return false;
1084 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1085 set_recdir, NULL, clipboard_callback, Icon_Recording);
1086 #endif
1087 static bool add_to_faves(void)
1089 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1090 selected_file))
1091 onplay_result = ONPLAY_RELOAD_DIR;
1092 return false;
1094 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1095 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1098 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1100 switch (action)
1102 case ACTION_REQUEST_MENUITEM:
1103 #ifdef HAVE_MULTIVOLUME
1104 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1105 (this_item == &rename_file_item ||
1106 this_item == &delete_dir_item ||
1107 this_item == &clipboard_cut_item) )
1108 return ACTION_EXIT_MENUITEM;
1109 #endif
1110 if (context == CONTEXT_ID3DB)
1111 return ACTION_EXIT_MENUITEM;
1112 if (this_item == &clipboard_paste_item)
1113 { /* visible if there is something to paste */
1114 return (clipboard_selection[0] != 0) ?
1115 action : ACTION_EXIT_MENUITEM;
1117 else if (this_item == &create_dir_item)
1119 /* always visible */
1120 return action;
1122 else if ((this_item == &properties_item) ||
1123 (this_item == &rename_file_item) ||
1124 (this_item == &clipboard_cut_item) ||
1125 (this_item == &clipboard_copy_item) ||
1126 (this_item == &add_to_faves_item)
1129 /* requires an actual file */
1130 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1132 #if LCD_DEPTH > 1
1133 else if (this_item == &set_backdrop_item)
1135 if (selected_file)
1137 char *suffix = strrchr(selected_file, '.');
1138 if (suffix)
1140 if (strcasecmp(suffix, ".bmp") == 0)
1142 return action;
1146 return ACTION_EXIT_MENUITEM;
1148 #endif
1149 else if ((selected_file_attr & ATTR_DIRECTORY))
1151 if ((this_item == &delete_dir_item)
1153 return action;
1154 #ifdef HAVE_RECORDING
1155 else if (this_item == &set_recdir_item)
1156 return action;
1157 #endif
1159 else if (selected_file
1160 #ifdef HAVE_MULTIVOLUME
1161 /* no rename+delete for volumes */
1162 && !(selected_file_attr & ATTR_VOLUME)
1163 #endif
1166 if ((this_item == &delete_file_item) ||
1167 (this_item == &list_viewers_item))
1169 return action;
1172 return ACTION_EXIT_MENUITEM;
1173 break;
1175 return action;
1177 /* used when onplay() is called in the CONTEXT_WPS context */
1180 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1181 onplaymenu_callback, Icon_Audio,
1182 &sound_settings, &wps_playlist_menu, &cat_playlist_menu,
1183 #ifdef HAVE_TAGCACHE
1184 &rating_item,
1185 #endif
1186 &bookmark_menu, &browse_id3_item, &delete_file_item, &view_cue_item,
1187 #ifdef HAVE_PITCHSCREEN
1188 &pitch_screen_item,
1189 #endif
1190 #if CONFIG_CODEC == SWCODEC
1191 &eq_menu_graphical_item, &eq_browse_presets_item,
1192 #endif
1193 &study_mode_toggle,
1195 /* used when onplay() is not called in the CONTEXT_WPS context */
1196 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1197 onplaymenu_callback, Icon_file_view_menu,
1198 &tree_playlist_menu, &cat_playlist_menu,
1199 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1200 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1201 #if LCD_DEPTH > 1
1202 &set_backdrop_item,
1203 #endif
1204 &list_viewers_item, &create_dir_item, &properties_item,
1205 #ifdef HAVE_RECORDING
1206 &set_recdir_item,
1207 #endif
1208 &add_to_faves_item,
1210 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1212 (void)this_item;
1213 switch (action)
1215 case ACTION_TREE_STOP:
1216 if (this_item == &wps_onplay_menu)
1218 list_stop_handler();
1219 return ACTION_STD_CANCEL;
1221 break;
1222 case ACTION_EXIT_MENUITEM:
1223 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1224 break;
1226 return action;
1228 int onplay(char* file, int attr, int from)
1230 const struct menu_item_ex *menu;
1231 onplay_result = ONPLAY_OK;
1232 context = from;
1233 selected_file = file;
1234 selected_file_attr = attr;
1235 if (context == CONTEXT_WPS)
1236 menu = &wps_onplay_menu;
1237 else
1238 menu = &tree_onplay_menu;
1239 switch (do_menu(menu, NULL, NULL, false))
1241 case GO_TO_WPS:
1242 return ONPLAY_START_PLAY;
1243 case GO_TO_ROOT:
1244 case GO_TO_MAINMENU:
1245 return ONPLAY_MAINMENU;
1246 default:
1247 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;