Fix issues with file-based bootloader installation:
[Rockbox.git] / apps / onplay.c
blob5374bc0a6156c3a4e5be995f308843a879e05a33
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##__}};
87 /* ----------------------------------------------------------------------- */
88 /* Displays the bookmark menu options for the user to decide. This is an */
89 /* interface function. */
90 /* ----------------------------------------------------------------------- */
92 static int bookmark_menu_callback(int action,
93 const struct menu_item_ex *this_item);
94 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
95 ID2P(LANG_BOOKMARK_MENU_CREATE),
96 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
97 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
98 ID2P(LANG_BOOKMARK_MENU_LIST),
99 bookmark_load_menu, NULL,
100 bookmark_menu_callback, Icon_Bookmark);
101 MAKE_MENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
102 Icon_Bookmark, &bookmark_create_menu_item, &bookmark_load_menu_item);
103 static int bookmark_menu_callback(int action,
104 const struct menu_item_ex *this_item)
106 (void)this_item;
107 switch (action)
109 case ACTION_REQUEST_MENUITEM:
110 if (this_item == &bookmark_load_menu_item)
112 if (bookmark_exist() == 0)
113 return ACTION_EXIT_MENUITEM;
115 /* hide the bookmark menu if there is no playback */
116 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
117 return ACTION_EXIT_MENUITEM;
118 break;
119 #ifdef HAVE_LCD_CHARCELLS
120 case ACTION_ENTER_MENUITEM:
121 status_set_param(true);
122 break;
123 #endif
124 case ACTION_EXIT_MENUITEM:
125 #ifdef HAVE_LCD_CHARCELLS
126 status_set_param(false);
127 #endif
128 settings_save();
129 break;
131 return action;
134 static bool list_viewers(void)
136 int ret = filetype_list_viewers(selected_file);
137 if (ret == PLUGIN_USB_CONNECTED)
138 onplay_result = ONPLAY_RELOAD_DIR;
139 return false;
142 static bool shuffle_playlist(void)
144 playlist_sort(NULL, true);
145 playlist_randomise(NULL, current_tick, true);
147 return false;
150 static bool save_playlist(void)
152 save_playlist_screen(NULL);
153 return false;
156 static bool add_to_playlist(int position, bool queue)
158 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
159 char *lines[] = {
160 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
161 selected_file
163 struct text_message message={lines, 2};
165 gui_syncsplash(0, ID2P(LANG_WAIT));
167 if (new_playlist)
168 playlist_create(NULL, NULL);
170 /* always set seed before inserting shuffled */
171 if (position == PLAYLIST_INSERT_SHUFFLED)
172 srand(current_tick);
174 #ifdef HAVE_TAGCACHE
175 if (context == CONTEXT_ID3DB)
177 tagtree_insert_selection_playlist(position, queue);
179 else
180 #endif
182 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
183 playlist_insert_track(NULL, selected_file, position, queue, true);
184 else if (selected_file_attr & ATTR_DIRECTORY)
186 bool recurse = false;
188 if (global_settings.recursive_dir_insert != RECURSE_ASK)
189 recurse = (bool)global_settings.recursive_dir_insert;
190 else
192 /* Ask if user wants to recurse directory */
193 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
196 playlist_insert_directory(NULL, selected_file, position, queue,
197 recurse);
199 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
200 playlist_insert_playlist(NULL, selected_file, position, queue);
203 if (new_playlist && (playlist_amount() > 0))
205 /* nothing is currently playing so begin playing what we just
206 inserted */
207 if (global_settings.playlist_shuffle)
208 playlist_shuffle(current_tick, -1);
209 playlist_start(0,0);
210 gui_syncstatusbar_draw(&statusbars, false);
211 onplay_result = ONPLAY_START_PLAY;
214 return false;
217 static bool view_playlist(void)
219 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
220 bool result;
222 result = playlist_viewer_ex(selected_file);
224 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
225 onplay_result == ONPLAY_OK)
226 /* playlist was started from viewer */
227 onplay_result = ONPLAY_START_PLAY;
229 return result;
232 static bool cat_add_to_a_playlist(void)
234 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
235 false);
238 static bool cat_add_to_a_new_playlist(void)
240 return catalog_add_to_a_playlist(selected_file, selected_file_attr, true);
244 static int cat_playlist_callback(int action,
245 const struct menu_item_ex *this_item);
246 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
247 catalog_view_playlists, 0, cat_playlist_callback,
248 Icon_Playlist);
249 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
250 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
251 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
252 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
253 MAKE_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
254 Icon_Playlist, &cat_view_lists,
255 &cat_add_to_list, &cat_add_to_new );
257 static int cat_playlist_callback(int action,
258 const struct menu_item_ex *this_item)
260 switch (action)
262 case ACTION_REQUEST_MENUITEM:
263 if (this_item == &cat_view_lists)
265 if (context == CONTEXT_WPS)
266 return action;
268 else if (selected_file && /* set before calling this menu,
269 so safe */
270 ((audio_status() & AUDIO_STATUS_PLAY &&
271 context == CONTEXT_WPS) ||
272 context == CONTEXT_TREE))
274 return action;
276 else
277 return ACTION_EXIT_MENUITEM;
278 break;
280 return action;
284 /* CONTEXT_WPS playlist options */
285 MENUITEM_FUNCTION(playlist_viewer_item, 0,
286 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
287 NULL, NULL, Icon_Playlist);
288 MENUITEM_FUNCTION(search_playlist_item, 0,
289 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
290 NULL, NULL, Icon_Playlist);
291 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
292 save_playlist, NULL, NULL, Icon_Playlist);
293 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
294 shuffle_playlist, NULL, NULL, Icon_Playlist);
295 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
296 NULL, Icon_Playlist,
297 &playlist_viewer_item, &search_playlist_item,
298 &playlist_save_item, &reshuffle_item
301 /* CONTEXT_[TREE|ID3DB] playlist options */
302 static int playlist_insert_func(void *param)
304 add_to_playlist((intptr_t)param, false);
305 return 0;
307 static int playlist_queue_func(void *param)
309 add_to_playlist((intptr_t)param, true);
310 return 0;
312 static int treeplaylist_wplayback_callback(int action,
313 const struct menu_item_ex*
314 this_item)
316 (void)this_item;
317 switch (action)
319 case ACTION_REQUEST_MENUITEM:
320 if (audio_status() & AUDIO_STATUS_PLAY)
321 return action;
322 else
323 return ACTION_EXIT_MENUITEM;
324 break;
326 return action;
329 static int treeplaylist_callback(int action,
330 const struct menu_item_ex *this_item);
332 /* insert items */
333 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
334 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
335 treeplaylist_callback, Icon_Playlist);
336 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
337 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
338 treeplaylist_wplayback_callback, Icon_Playlist);
339 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
340 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
341 treeplaylist_wplayback_callback, Icon_Playlist);
342 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
343 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
344 treeplaylist_wplayback_callback, Icon_Playlist);
345 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
346 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
347 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
348 Icon_Playlist);
349 /* queue items */
350 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
351 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
352 treeplaylist_wplayback_callback, Icon_Playlist);
353 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
354 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
355 treeplaylist_wplayback_callback, Icon_Playlist);
356 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
357 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
358 treeplaylist_wplayback_callback, Icon_Playlist);
359 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
360 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
361 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
362 treeplaylist_wplayback_callback, Icon_Playlist);
363 /* replace playlist */
364 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
365 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
366 treeplaylist_wplayback_callback, Icon_Playlist);
367 /* others */
369 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
370 view_playlist, NULL,
371 treeplaylist_callback, Icon_Playlist);
373 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
374 treeplaylist_callback, Icon_Playlist,
376 /* view */
377 &view_playlist_item,
379 /* insert */
380 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
381 &i_last_pl_item, &i_shuf_pl_item,
383 /* queue */
384 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
385 &q_shuf_pl_item,
387 /* replace */
388 &replace_pl_item
390 static int treeplaylist_callback(int action,
391 const struct menu_item_ex *this_item)
393 (void)this_item;
394 switch (action)
396 case ACTION_REQUEST_MENUITEM:
397 if (this_item == &tree_playlist_menu)
399 if (((selected_file_attr & FILE_ATTR_MASK) ==
400 FILE_ATTR_AUDIO) ||
401 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
402 (selected_file_attr & ATTR_DIRECTORY))
404 return action;
406 else
407 return ACTION_EXIT_MENUITEM;
409 else if (this_item == &view_playlist_item)
411 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
412 context == CONTEXT_TREE)
413 return action;
414 else
415 return ACTION_EXIT_MENUITEM;
417 else if (this_item == &i_pl_item_no_play)
419 if (!(audio_status() & AUDIO_STATUS_PLAY))
421 return action;
423 else
424 return ACTION_EXIT_MENUITEM;
426 else if (this_item == &i_shuf_pl_item)
429 if (audio_status() & AUDIO_STATUS_PLAY)
431 return action;
433 else if ((this_item == &i_shuf_pl_item) &&
434 ((selected_file_attr & ATTR_DIRECTORY) ||
435 ((selected_file_attr & FILE_ATTR_MASK) ==
436 FILE_ATTR_M3U)))
438 return action;
440 return ACTION_EXIT_MENUITEM;
442 break;
444 return action;
447 /* helper function to remove a non-empty directory */
448 static int remove_dir(char* dirname, int len)
450 int result = 0;
451 DIR* dir;
452 int dirlen = strlen(dirname);
453 int i;
455 dir = opendir(dirname);
456 if (!dir)
457 return -1; /* open error */
459 while(true)
461 struct dirent* entry;
462 /* walk through the directory content */
463 entry = readdir(dir);
464 if (!entry)
465 break;
467 dirname[dirlen] ='\0';
468 FOR_NB_SCREENS(i)
469 screens[i].puts(0,1,dirname);
471 /* append name to current directory */
472 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
473 if (entry->attribute & ATTR_DIRECTORY)
474 { /* remove a subdirectory */
475 if (!strcmp((char *)entry->d_name, ".") ||
476 !strcmp((char *)entry->d_name, ".."))
477 continue; /* skip these */
479 /* inform the user which dir we're deleting */
481 result = remove_dir(dirname, len); /* recursion */
482 if (result)
483 break; /* or better continue, delete what we can? */
485 else
486 { /* remove a file */
487 #ifdef HAVE_LCD_BITMAP
488 FOR_NB_SCREENS(i)
490 show_busy_slider(&screens[i], 2, 3*screens[i].char_height,
491 LCD_WIDTH-4, screens[i].char_height);
493 #endif
494 result = remove(dirname);
496 #ifdef HAVE_LCD_BITMAP
497 FOR_NB_SCREENS(i)
498 screens[i].update();
499 #endif
500 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
502 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
503 result = -1;
504 break;
507 closedir(dir);
509 if (!result)
510 { /* remove the now empty directory */
511 dirname[dirlen] = '\0'; /* terminate to original length */
513 result = rmdir(dirname);
516 return result;
520 /* share code for file and directory deletion, saves space */
521 static bool delete_handler(bool is_dir)
523 char *lines[]={
524 ID2P(LANG_REALLY_DELETE),
525 selected_file
527 char *yes_lines[]={
528 ID2P(LANG_DELETED),
529 selected_file
532 struct text_message message={lines, 2};
533 struct text_message yes_message={yes_lines, 2};
535 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
536 return false;
538 gui_syncsplash(0, str(LANG_DELETING));
540 int res;
541 if (is_dir)
543 char pathname[MAX_PATH]; /* space to go deep */
544 cpu_boost(true);
545 strncpy(pathname, selected_file, sizeof pathname);
546 res = remove_dir(pathname, sizeof(pathname));
547 cpu_boost(false);
549 else
550 res = remove(selected_file);
552 if (!res) {
553 onplay_result = ONPLAY_RELOAD_DIR;
555 return false;
559 static bool delete_file(void)
561 return delete_handler(false);
564 static bool delete_dir(void)
566 return delete_handler(true);
569 #if LCD_DEPTH > 1
570 static bool set_backdrop(void)
572 /* load the image */
573 if(load_main_backdrop(selected_file)) {
574 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
575 set_file(selected_file, (char *)global_settings.backdrop_file,
576 MAX_FILENAME);
577 show_main_backdrop();
578 return true;
579 } else {
580 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
581 return false;
584 #endif
586 static bool rename_file(void)
588 char newname[MAX_PATH];
589 char* ptr = strrchr(selected_file, '/') + 1;
590 int pathlen = (ptr - selected_file);
591 strncpy(newname, selected_file, sizeof newname);
592 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
593 if (!strlen(newname + pathlen) ||
594 (rename(selected_file, newname) < 0)) {
595 lcd_clear_display();
596 lcd_puts(0,0,str(LANG_RENAME));
597 lcd_puts(0,1,str(LANG_FAILED));
598 lcd_update();
599 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
600 sleep(HZ*2);
602 else
603 onplay_result = ONPLAY_RELOAD_DIR;
606 return false;
609 static bool create_dir(void)
611 char dirname[MAX_PATH];
612 char *cwd;
613 int rc;
614 int pathlen;
616 cwd = getcwd(NULL, 0);
617 memset(dirname, 0, sizeof dirname);
619 snprintf(dirname, sizeof dirname, "%s/",
620 cwd[1] ? cwd : "");
622 pathlen = strlen(dirname);
623 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
624 if (rc < 0)
625 return false;
627 rc = mkdir(dirname);
628 if (rc < 0) {
629 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
630 gui_syncsplash(HZ, (unsigned char *)"%s %s",
631 str(LANG_CREATE_DIR), str(LANG_FAILED));
632 } else {
633 onplay_result = ONPLAY_RELOAD_DIR;
636 return true;
639 static bool properties(void)
641 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
642 selected_file))
643 onplay_result = ONPLAY_RELOAD_DIR;
644 return false;
647 /* Store the current selection in the clipboard */
648 static bool clipboard_clip(bool copy)
650 clipboard_selection[0] = 0;
651 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
652 clipboard_selection_attr = selected_file_attr;
653 clipboard_is_copy = copy;
655 return true;
658 static bool clipboard_cut(void)
660 return clipboard_clip(false);
663 static bool clipboard_copy(void)
665 return clipboard_clip(true);
668 #ifdef HAVE_LCD_BITMAP
669 static inline void draw_slider(void)
671 int i;
672 FOR_NB_SCREENS(i)
674 show_busy_slider(&screens[i], 2, LCD_HEIGHT/4,
675 LCD_WIDTH-4, screens[i].char_height);
676 screens[i].update();
679 #else
680 #define draw_slider()
681 #endif
683 /* Paste a file to a new directory. Will overwrite always. */
684 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
686 int src_fd, target_fd;
687 size_t buffersize;
688 ssize_t size, bytesread, byteswritten;
689 char *buffer;
690 bool result = false;
692 if (copy) {
693 /* See if we can get the plugin buffer for the file copy buffer */
694 buffer = (char *) plugin_get_buffer(&buffersize);
695 if (buffer == NULL || buffersize < 512) {
696 /* Not large enough, try for a disk sector worth of stack
697 instead */
698 buffersize = 512;
699 buffer = (char *) __builtin_alloca(buffersize);
702 if (buffer == NULL) {
703 return false;
706 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
707 size */
709 src_fd = open(src, O_RDONLY);
711 if (src_fd >= 0) {
712 target_fd = creat(target);
714 if (target_fd >= 0) {
715 result = true;
717 size = filesize(src_fd);
719 if (size == -1) {
720 result = false;
723 while(size > 0) {
724 bytesread = read(src_fd, buffer, buffersize);
726 if (bytesread == -1) {
727 result = false;
728 break;
731 size -= bytesread;
733 while(bytesread > 0) {
734 byteswritten = write(target_fd, buffer, bytesread);
736 if (byteswritten == -1) {
737 result = false;
738 size = 0;
739 break;
742 bytesread -= byteswritten;
743 draw_slider();
747 close(target_fd);
749 /* Copy failed. Cleanup. */
750 if (!result) {
751 remove(target);
755 close(src_fd);
757 } else {
758 result = rename(src, target) == 0;
759 #ifdef HAVE_MULTIVOLUME
760 if (!result) {
761 if (errno == EXDEV) {
762 /* Failed because cross volume rename doesn't work. Copy
763 instead */
764 result = clipboard_pastefile(src, target, true);
766 if (result) {
767 result = remove(src) == 0;
771 #endif
774 return result;
777 /* Paste a directory to a new location. Designed to be called by
778 clipboard_paste */
779 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
780 int targetlen, bool copy)
782 DIR *srcdir;
783 int srcdirlen = strlen(src);
784 int targetdirlen = strlen(target);
785 int fd;
786 bool result = true;
788 /* Check if the target exists */
789 fd = open(target, O_RDONLY);
790 close(fd);
792 if (fd < 0) {
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;
873 int target_fd;
875 unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
876 struct text_message message={(char **)lines, 1};
878 /* Get the name of the current directory */
879 cwd = getcwd(NULL, 0);
881 /* Figure out the name of the selection */
882 nameptr = strrchr(clipboard_selection, '/');
884 /* Final target is current directory plus name of selection */
885 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
887 /* Check if we're going to overwrite */
888 target_fd = open(target, O_RDONLY);
889 close(target_fd);
891 /* If the target existed but they choose not to overwite, exit */
892 if (target_fd >= 0 &&
893 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
894 return false;
897 if (clipboard_is_copy) {
898 gui_syncsplash(0, ID2P(LANG_COPYING));
900 else
902 gui_syncsplash(0, ID2P(LANG_MOVING));
905 /* Now figure out what we're doing */
906 cpu_boost(true);
907 if (clipboard_selection_attr & ATTR_DIRECTORY) {
908 /* Recursion. Set up external stack */
909 char srcpath[MAX_PATH];
910 char targetpath[MAX_PATH];
911 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
913 /* Do not allow the user to paste a directory into a dir they are
914 copying */
915 success = 0;
917 else
919 strncpy(srcpath, clipboard_selection, sizeof srcpath);
920 strncpy(targetpath, target, sizeof targetpath);
922 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
923 target, sizeof(targetpath), clipboard_is_copy);
925 } else {
926 success = clipboard_pastefile(clipboard_selection, target,
927 clipboard_is_copy);
929 cpu_boost(false);
931 /* Did it work? */
932 if (success) {
933 /* Reset everything */
934 clipboard_selection[0] = 0;
935 clipboard_selection_attr = 0;
936 clipboard_is_copy = false;
938 /* Force reload of the current directory */
939 onplay_result = ONPLAY_RELOAD_DIR;
940 } else {
941 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
942 gui_syncsplash(HZ, (unsigned char *)"%s %s",
943 str(LANG_PASTE), str(LANG_FAILED));
946 return true;
949 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
950 #ifdef HAVE_TAGCACHE
951 static int set_rating_inline(void)
953 struct mp3entry* id3 = audio_current_track();
954 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
956 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
957 NULL, 1, 0, 10, NULL, NULL);
958 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
960 else
961 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
962 return 0;
964 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
966 (void)this_item;
967 switch (action)
969 case ACTION_REQUEST_MENUITEM:
970 if (!selected_file || !global_settings.runtimedb)
971 return ACTION_EXIT_MENUITEM;
972 break;
974 return action;
976 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
977 set_rating_inline, NULL,
978 ratingitem_callback, Icon_Questionmark);
979 #endif
981 static bool view_cue(void)
983 struct mp3entry* id3 = audio_current_track();
984 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
986 browse_cuesheet(curr_cue);
988 return false;
990 static int view_cue_item_callback(int action,
991 const struct menu_item_ex *this_item)
993 (void)this_item;
994 struct mp3entry* id3 = audio_current_track();
995 switch (action)
997 case ACTION_REQUEST_MENUITEM:
998 if (!selected_file || !cuesheet_is_enabled()
999 || !id3 || !id3->cuesheet_type)
1000 return ACTION_EXIT_MENUITEM;
1001 break;
1003 return action;
1005 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1006 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1008 /* CONTEXT_WPS items */
1009 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1010 browse_id3, NULL, NULL, Icon_NOICON);
1011 #ifdef HAVE_PITCHSCREEN
1012 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1013 pitch_screen, NULL, NULL, Icon_Audio);
1014 #endif
1015 #if CONFIG_CODEC == SWCODEC
1016 MENUITEM_FUNCTION(eq_menu_graphical_item, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
1017 eq_menu_graphical, NULL, NULL, Icon_Audio);
1018 MENUITEM_FUNCTION(eq_browse_presets_item, 0, ID2P(LANG_EQUALIZER_BROWSE),
1019 eq_browse_presets, NULL, NULL, Icon_Audio);
1020 #endif
1022 /* CONTEXT_[TREE|ID3DB] items */
1023 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1024 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1025 rename_file, NULL, clipboard_callback, Icon_NOICON);
1026 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1027 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1028 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1029 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1030 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1031 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1032 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1033 delete_file, NULL, clipboard_callback, Icon_NOICON);
1034 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1035 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1036 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1037 properties, NULL, clipboard_callback, Icon_NOICON);
1038 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1039 create_dir, NULL, clipboard_callback, Icon_NOICON);
1040 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1041 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1042 #if LCD_DEPTH > 1
1043 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1044 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1045 #endif
1046 #ifdef HAVE_RECORDING
1047 static bool set_recdir(void)
1049 strncpy(global_settings.rec_directory,
1050 selected_file, MAX_FILENAME+1);
1051 settings_save();
1052 return false;
1054 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1055 set_recdir, NULL, clipboard_callback, Icon_Recording);
1056 #endif
1057 static bool add_to_faves(void)
1059 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1060 selected_file))
1061 onplay_result = ONPLAY_RELOAD_DIR;
1062 return false;
1064 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1065 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1068 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1070 switch (action)
1072 case ACTION_REQUEST_MENUITEM:
1073 if (context == CONTEXT_ID3DB)
1074 return ACTION_EXIT_MENUITEM;
1075 if (this_item == &clipboard_paste_item)
1076 { /* visible if there is something to paste */
1077 return (clipboard_selection[0] != 0) ?
1078 action : ACTION_EXIT_MENUITEM;
1080 else if ((this_item == &create_dir_item) ||
1081 (this_item == &properties_item) ||
1082 (this_item == &rename_file_item) ||
1083 (this_item == &clipboard_cut_item) ||
1084 (this_item == &clipboard_copy_item) ||
1085 (this_item == &add_to_faves_item)
1088 /* always visible */
1089 return action;
1091 #if LCD_DEPTH > 1
1092 else if (this_item == &set_backdrop_item)
1094 if (selected_file)
1096 char *suffix = strrchr(selected_file, '.');
1097 if (suffix)
1099 if (strcasecmp(suffix, ".bmp") == 0)
1101 return action;
1105 return ACTION_EXIT_MENUITEM;
1107 #endif
1108 else if ((selected_file_attr & ATTR_DIRECTORY))
1110 if ((this_item == &delete_dir_item)
1112 return action;
1113 #ifdef HAVE_RECORDING
1114 else if (this_item == &set_recdir_item)
1115 return action;
1116 #endif
1118 else if (selected_file
1119 #ifdef HAVE_MULTIVOLUME
1120 /* no rename+delete for volumes */
1121 && !(selected_file_attr & ATTR_VOLUME)
1122 #endif
1125 if ((this_item == &delete_file_item) ||
1126 (this_item == &list_viewers_item))
1128 return action;
1131 return ACTION_EXIT_MENUITEM;
1132 break;
1134 return action;
1136 /* used when onplay() is called in the CONTEXT_WPS context */
1139 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1140 onplaymenu_callback, Icon_Audio,
1141 &sound_settings, &wps_playlist_menu, &cat_playlist_menu,
1142 #ifdef HAVE_TAGCACHE
1143 &rating_item,
1144 #endif
1145 &bookmark_menu, &browse_id3_item, &delete_file_item, &view_cue_item,
1146 #ifdef HAVE_PITCHSCREEN
1147 &pitch_screen_item,
1148 #endif
1149 #if CONFIG_CODEC == SWCODEC
1150 &eq_menu_graphical_item, &eq_browse_presets_item,
1151 #endif
1153 /* used when onplay() is not called in the CONTEXT_WPS context */
1154 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1155 onplaymenu_callback, Icon_file_view_menu,
1156 &tree_playlist_menu, &cat_playlist_menu,
1157 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1158 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1159 #if LCD_DEPTH > 1
1160 &set_backdrop_item,
1161 #endif
1162 &list_viewers_item, &create_dir_item, &properties_item,
1163 #ifdef HAVE_RECORDING
1164 &set_recdir_item,
1165 #endif
1166 &add_to_faves_item,
1168 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1170 (void)this_item;
1171 switch (action)
1173 case ACTION_TREE_STOP:
1174 if (this_item == &wps_onplay_menu)
1176 list_stop_handler();
1177 return ACTION_STD_CANCEL;
1179 break;
1180 case ACTION_EXIT_MENUITEM:
1181 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1182 break;
1184 return action;
1186 int onplay(char* file, int attr, int from)
1188 const struct menu_item_ex *menu;
1189 onplay_result = ONPLAY_OK;
1190 context = from;
1191 selected_file = file;
1192 selected_file_attr = attr;
1193 if (context == CONTEXT_WPS)
1194 menu = &wps_onplay_menu;
1195 else
1196 menu = &tree_onplay_menu;
1197 switch (do_menu(menu, NULL, NULL, false))
1199 case GO_TO_WPS:
1200 return ONPLAY_START_PLAY;
1201 case GO_TO_ROOT:
1202 case GO_TO_MAINMENU:
1203 return ONPLAY_MAINMENU;
1204 default:
1205 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;