Increase the size of one of the look up tables. Fixes 'burst of static' in some files...
[kugel-rb.git] / apps / onplay.c
blobbcea88fd37ecc21e2815d206271a69d5ddfd4ee9
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 char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 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);
242 static bool cat_add_to_a_new_playlist(void)
244 return catalog_add_to_a_playlist(selected_file, selected_file_attr, true);
248 static int cat_playlist_callback(int action,
249 const struct menu_item_ex *this_item);
250 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
251 catalog_view_playlists, 0, cat_playlist_callback,
252 Icon_Playlist);
253 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
254 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
255 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
256 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
257 MAKE_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
258 Icon_Playlist, &cat_view_lists,
259 &cat_add_to_list, &cat_add_to_new );
261 static int cat_playlist_callback(int action,
262 const struct menu_item_ex *this_item)
264 switch (action)
266 case ACTION_REQUEST_MENUITEM:
267 if (this_item == &cat_view_lists)
269 if (context == CONTEXT_WPS)
270 return action;
272 else if (selected_file && /* set before calling this menu,
273 so safe */
274 ((audio_status() & AUDIO_STATUS_PLAY &&
275 context == CONTEXT_WPS) ||
276 context == CONTEXT_TREE))
278 return action;
280 else
281 return ACTION_EXIT_MENUITEM;
282 break;
284 return action;
288 /* CONTEXT_WPS playlist options */
289 MENUITEM_FUNCTION(playlist_viewer_item, 0,
290 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
291 NULL, NULL, Icon_Playlist);
292 MENUITEM_FUNCTION(search_playlist_item, 0,
293 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
294 NULL, NULL, Icon_Playlist);
295 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
296 save_playlist, NULL, NULL, Icon_Playlist);
297 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
298 shuffle_playlist, NULL, NULL, Icon_Playlist);
299 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
300 NULL, Icon_Playlist,
301 &playlist_viewer_item, &search_playlist_item,
302 &playlist_save_item, &reshuffle_item
305 /* CONTEXT_[TREE|ID3DB] playlist options */
306 static int playlist_insert_func(void *param)
308 add_to_playlist((intptr_t)param, false);
309 return 0;
311 static int playlist_queue_func(void *param)
313 add_to_playlist((intptr_t)param, true);
314 return 0;
316 static int treeplaylist_wplayback_callback(int action,
317 const struct menu_item_ex*
318 this_item)
320 (void)this_item;
321 switch (action)
323 case ACTION_REQUEST_MENUITEM:
324 if (audio_status() & AUDIO_STATUS_PLAY)
325 return action;
326 else
327 return ACTION_EXIT_MENUITEM;
328 break;
330 return action;
333 static int treeplaylist_callback(int action,
334 const struct menu_item_ex *this_item);
336 /* insert items */
337 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
338 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
339 treeplaylist_callback, Icon_Playlist);
340 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
341 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
342 treeplaylist_wplayback_callback, Icon_Playlist);
343 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
344 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
345 treeplaylist_wplayback_callback, Icon_Playlist);
346 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
347 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
348 treeplaylist_wplayback_callback, Icon_Playlist);
349 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
350 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
351 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
352 Icon_Playlist);
353 /* queue items */
354 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
355 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
356 treeplaylist_wplayback_callback, Icon_Playlist);
357 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
358 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
359 treeplaylist_wplayback_callback, Icon_Playlist);
360 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
361 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
362 treeplaylist_wplayback_callback, Icon_Playlist);
363 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
364 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
365 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
366 treeplaylist_wplayback_callback, Icon_Playlist);
367 /* replace playlist */
368 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
369 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
370 treeplaylist_wplayback_callback, Icon_Playlist);
371 /* others */
373 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
374 view_playlist, NULL,
375 treeplaylist_callback, Icon_Playlist);
377 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
378 treeplaylist_callback, Icon_Playlist,
380 /* view */
381 &view_playlist_item,
383 /* insert */
384 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
385 &i_last_pl_item, &i_shuf_pl_item,
387 /* queue */
388 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
389 &q_shuf_pl_item,
391 /* replace */
392 &replace_pl_item
394 static int treeplaylist_callback(int action,
395 const struct menu_item_ex *this_item)
397 (void)this_item;
398 switch (action)
400 case ACTION_REQUEST_MENUITEM:
401 if (this_item == &tree_playlist_menu)
403 if (((selected_file_attr & FILE_ATTR_MASK) ==
404 FILE_ATTR_AUDIO) ||
405 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
406 (selected_file_attr & ATTR_DIRECTORY))
408 return action;
410 else
411 return ACTION_EXIT_MENUITEM;
413 else if (this_item == &view_playlist_item)
415 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
416 context == CONTEXT_TREE)
417 return action;
418 else
419 return ACTION_EXIT_MENUITEM;
421 else if (this_item == &i_pl_item_no_play)
423 if (!(audio_status() & AUDIO_STATUS_PLAY))
425 return action;
427 else
428 return ACTION_EXIT_MENUITEM;
430 else if (this_item == &i_shuf_pl_item)
433 if (audio_status() & AUDIO_STATUS_PLAY)
435 return action;
437 else if ((this_item == &i_shuf_pl_item) &&
438 ((selected_file_attr & ATTR_DIRECTORY) ||
439 ((selected_file_attr & FILE_ATTR_MASK) ==
440 FILE_ATTR_M3U)))
442 return action;
444 return ACTION_EXIT_MENUITEM;
446 break;
448 return action;
451 /* helper function to remove a non-empty directory */
452 static int remove_dir(char* dirname, int len)
454 int result = 0;
455 DIR* dir;
456 int dirlen = strlen(dirname);
457 int i;
459 dir = opendir(dirname);
460 if (!dir)
461 return -1; /* open error */
463 while(true)
465 struct dirent* entry;
466 /* walk through the directory content */
467 entry = readdir(dir);
468 if (!entry)
469 break;
471 dirname[dirlen] ='\0';
472 FOR_NB_SCREENS(i){
473 screens[i].puts(0,1,dirname);
474 screens[i].update();
477 /* append name to current directory */
478 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
479 if (entry->attribute & ATTR_DIRECTORY)
480 { /* remove a subdirectory */
481 if (!strcmp((char *)entry->d_name, ".") ||
482 !strcmp((char *)entry->d_name, ".."))
483 continue; /* skip these */
485 /* inform the user which dir we're deleting */
487 result = remove_dir(dirname, len); /* recursion */
488 if (result)
489 break; /* or better continue, delete what we can? */
491 else
492 { /* remove a file */
493 draw_slider();
494 result = remove(dirname);
496 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
498 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
499 result = -1;
500 break;
503 closedir(dir);
505 if (!result)
506 { /* remove the now empty directory */
507 dirname[dirlen] = '\0'; /* terminate to original length */
509 result = rmdir(dirname);
512 return result;
516 /* share code for file and directory deletion, saves space */
517 static bool delete_handler(bool is_dir)
519 char *lines[]={
520 ID2P(LANG_REALLY_DELETE),
521 selected_file
523 char *yes_lines[]={
524 ID2P(LANG_DELETED),
525 selected_file
528 struct text_message message={lines, 2};
529 struct text_message yes_message={yes_lines, 2};
531 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
532 return false;
534 gui_syncsplash(0, str(LANG_DELETING));
536 int res;
537 if (is_dir)
539 char pathname[MAX_PATH]; /* space to go deep */
540 cpu_boost(true);
541 strncpy(pathname, selected_file, sizeof pathname);
542 res = remove_dir(pathname, sizeof(pathname));
543 cpu_boost(false);
545 else
546 res = remove(selected_file);
548 if (!res) {
549 onplay_result = ONPLAY_RELOAD_DIR;
551 return false;
555 static bool delete_file(void)
557 return delete_handler(false);
560 static bool delete_dir(void)
562 return delete_handler(true);
565 #if LCD_DEPTH > 1
566 static bool set_backdrop(void)
568 /* load the image */
569 if(load_main_backdrop(selected_file)) {
570 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
571 set_file(selected_file, (char *)global_settings.backdrop_file,
572 MAX_FILENAME);
573 show_main_backdrop();
574 return true;
575 } else {
576 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
577 return false;
580 #endif
582 static bool rename_file(void)
584 char newname[MAX_PATH];
585 char* ptr = strrchr(selected_file, '/') + 1;
586 int pathlen = (ptr - selected_file);
587 strncpy(newname, selected_file, sizeof newname);
588 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
589 if (!strlen(newname + pathlen) ||
590 (rename(selected_file, newname) < 0)) {
591 lcd_clear_display();
592 lcd_puts(0,0,str(LANG_RENAME));
593 lcd_puts(0,1,str(LANG_FAILED));
594 lcd_update();
595 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
596 sleep(HZ*2);
598 else
599 onplay_result = ONPLAY_RELOAD_DIR;
602 return false;
605 static bool create_dir(void)
607 char dirname[MAX_PATH];
608 char *cwd;
609 int rc;
610 int pathlen;
612 cwd = getcwd(NULL, 0);
613 memset(dirname, 0, sizeof dirname);
615 snprintf(dirname, sizeof dirname, "%s/",
616 cwd[1] ? cwd : "");
618 pathlen = strlen(dirname);
619 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
620 if (rc < 0)
621 return false;
623 rc = mkdir(dirname);
624 if (rc < 0) {
625 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
626 gui_syncsplash(HZ, (unsigned char *)"%s %s",
627 str(LANG_CREATE_DIR), str(LANG_FAILED));
628 } else {
629 onplay_result = ONPLAY_RELOAD_DIR;
632 return true;
635 static bool properties(void)
637 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
638 selected_file))
639 onplay_result = ONPLAY_RELOAD_DIR;
640 return false;
643 /* Store the current selection in the clipboard */
644 static bool clipboard_clip(bool copy)
646 clipboard_selection[0] = 0;
647 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
648 clipboard_selection_attr = selected_file_attr;
649 clipboard_is_copy = copy;
651 return true;
654 static bool clipboard_cut(void)
656 return clipboard_clip(false);
659 static bool clipboard_copy(void)
661 return clipboard_clip(true);
664 #ifdef HAVE_LCD_BITMAP
665 static void draw_slider(void)
667 int i;
668 FOR_NB_SCREENS(i)
670 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
671 LCD_WIDTH-2, 2*screens[i].char_height-1);
672 screens[i].update();
675 #endif
677 /* Paste a file to a new directory. Will overwrite always. */
678 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
680 int src_fd, target_fd;
681 size_t buffersize;
682 ssize_t size, bytesread, byteswritten;
683 char *buffer;
684 bool result = false;
686 if (copy) {
687 /* See if we can get the plugin buffer for the file copy buffer */
688 buffer = (char *) plugin_get_buffer(&buffersize);
689 if (buffer == NULL || buffersize < 512) {
690 /* Not large enough, try for a disk sector worth of stack
691 instead */
692 buffersize = 512;
693 buffer = (char *) __builtin_alloca(buffersize);
696 if (buffer == NULL) {
697 return false;
700 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
701 size */
703 src_fd = open(src, O_RDONLY);
705 if (src_fd >= 0) {
706 target_fd = creat(target);
708 if (target_fd >= 0) {
709 result = true;
711 size = filesize(src_fd);
713 if (size == -1) {
714 result = false;
717 while(size > 0) {
718 bytesread = read(src_fd, buffer, buffersize);
720 if (bytesread == -1) {
721 result = false;
722 break;
725 size -= bytesread;
727 while(bytesread > 0) {
728 byteswritten = write(target_fd, buffer, bytesread);
730 if (byteswritten == -1) {
731 result = false;
732 size = 0;
733 break;
736 bytesread -= byteswritten;
737 draw_slider();
741 close(target_fd);
743 /* Copy failed. Cleanup. */
744 if (!result) {
745 remove(target);
749 close(src_fd);
751 } else {
752 result = rename(src, target) == 0;
753 #ifdef HAVE_MULTIVOLUME
754 if (!result) {
755 if (errno == EXDEV) {
756 /* Failed because cross volume rename doesn't work. Copy
757 instead */
758 result = clipboard_pastefile(src, target, true);
760 if (result) {
761 result = remove(src) == 0;
765 #endif
768 return result;
771 /* Paste a directory to a new location. Designed to be called by
772 clipboard_paste */
773 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
774 int targetlen, bool copy)
776 DIR *srcdir;
777 int srcdirlen = strlen(src);
778 int targetdirlen = strlen(target);
779 int fd;
780 bool result = true;
782 /* Check if the target exists */
783 fd = open(target, O_RDONLY);
784 close(fd);
786 if (fd < 0) {
787 if (!copy) {
788 /* Just move the directory */
789 result = rename(src, target) == 0;
791 #ifdef HAVE_MULTIVOLUME
792 if (!result && errno == EXDEV) {
793 /* Try a copy as we're going across devices */
794 result = clipboard_pastedirectory(src, srclen, target,
795 targetlen, true);
797 /* If it worked, remove the source directory */
798 if (result) {
799 remove_dir(src, srclen);
802 #endif
803 return result;
804 } else {
805 /* Make a directory to copy things to */
806 result = mkdir(target) == 0;
810 /* Check if something went wrong already */
811 if (!result) {
812 return result;
815 srcdir = opendir(src);
816 if (!srcdir) {
817 return false;
820 /* This loop will exit as soon as there's a problem */
821 while(result)
823 struct dirent* entry;
824 /* walk through the directory content */
825 entry = readdir(srcdir);
826 if (!entry)
827 break;
829 /* append name to current directory */
830 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
831 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
832 entry->d_name);
834 DEBUGF("Copy %s to %s\n", src, target);
836 if (entry->attribute & ATTR_DIRECTORY)
837 { /* copy/move a subdirectory */
838 if (!strcmp((char *)entry->d_name, ".") ||
839 !strcmp((char *)entry->d_name, ".."))
840 continue; /* skip these */
842 result = clipboard_pastedirectory(src, srclen, target, targetlen,
843 copy); /* recursion */
845 else
846 { /* copy/move a file */
847 result = clipboard_pastefile(src, target, copy);
851 closedir(srcdir);
853 if (result) {
854 src[srcdirlen] = '\0'; /* terminate to original length */
855 target[targetdirlen] = '\0'; /* terminate to original length */
858 return result;
861 /* Paste the clipboard to the current directory */
862 static bool clipboard_paste(void)
864 char target[MAX_PATH];
865 char *cwd, *nameptr;
866 bool success;
867 int target_fd;
869 unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
870 struct text_message message={(char **)lines, 1};
872 /* Get the name of the current directory */
873 cwd = getcwd(NULL, 0);
875 /* Figure out the name of the selection */
876 nameptr = strrchr(clipboard_selection, '/');
878 /* Final target is current directory plus name of selection */
879 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
881 /* Check if we're going to overwrite */
882 target_fd = open(target, O_RDONLY);
883 close(target_fd);
885 /* If the target existed but they choose not to overwite, exit */
886 if (target_fd >= 0 &&
887 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
888 return false;
891 if (clipboard_is_copy) {
892 gui_syncsplash(0, ID2P(LANG_COPYING));
894 else
896 gui_syncsplash(0, ID2P(LANG_MOVING));
899 /* Now figure out what we're doing */
900 cpu_boost(true);
901 if (clipboard_selection_attr & ATTR_DIRECTORY) {
902 /* Recursion. Set up external stack */
903 char srcpath[MAX_PATH];
904 char targetpath[MAX_PATH];
905 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
907 /* Do not allow the user to paste a directory into a dir they are
908 copying */
909 success = 0;
911 else
913 strncpy(srcpath, clipboard_selection, sizeof srcpath);
914 strncpy(targetpath, target, sizeof targetpath);
916 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
917 target, sizeof(targetpath), clipboard_is_copy);
919 } else {
920 success = clipboard_pastefile(clipboard_selection, target,
921 clipboard_is_copy);
923 cpu_boost(false);
925 /* Did it work? */
926 if (success) {
927 /* Reset everything */
928 clipboard_selection[0] = 0;
929 clipboard_selection_attr = 0;
930 clipboard_is_copy = false;
932 /* Force reload of the current directory */
933 onplay_result = ONPLAY_RELOAD_DIR;
934 } else {
935 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
936 gui_syncsplash(HZ, (unsigned char *)"%s %s",
937 str(LANG_PASTE), str(LANG_FAILED));
940 return true;
943 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
944 #ifdef HAVE_TAGCACHE
945 static int set_rating_inline(void)
947 struct mp3entry* id3 = audio_current_track();
948 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
950 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
951 NULL, 1, 0, 10, NULL, NULL);
952 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
954 else
955 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
956 return 0;
958 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
960 (void)this_item;
961 switch (action)
963 case ACTION_REQUEST_MENUITEM:
964 if (!selected_file || !global_settings.runtimedb)
965 return ACTION_EXIT_MENUITEM;
966 break;
968 return action;
970 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
971 set_rating_inline, NULL,
972 ratingitem_callback, Icon_Questionmark);
973 #endif
975 static bool view_cue(void)
977 struct mp3entry* id3 = audio_current_track();
978 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
980 browse_cuesheet(curr_cue);
982 return false;
984 static int view_cue_item_callback(int action,
985 const struct menu_item_ex *this_item)
987 (void)this_item;
988 struct mp3entry* id3 = audio_current_track();
989 switch (action)
991 case ACTION_REQUEST_MENUITEM:
992 if (!selected_file || !cuesheet_is_enabled()
993 || !id3 || !id3->cuesheet_type)
994 return ACTION_EXIT_MENUITEM;
995 break;
997 return action;
999 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1000 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1002 /* CONTEXT_WPS items */
1003 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1004 browse_id3, NULL, NULL, Icon_NOICON);
1005 #ifdef HAVE_PITCHSCREEN
1006 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1007 pitch_screen, NULL, NULL, Icon_Audio);
1008 #endif
1009 #if CONFIG_CODEC == SWCODEC
1010 MENUITEM_FUNCTION(eq_menu_graphical_item, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
1011 eq_menu_graphical, NULL, NULL, Icon_Audio);
1012 MENUITEM_FUNCTION(eq_browse_presets_item, 0, ID2P(LANG_EQUALIZER_BROWSE),
1013 eq_browse_presets, NULL, NULL, Icon_Audio);
1014 #endif
1016 /* CONTEXT_[TREE|ID3DB] items */
1017 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1018 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1019 rename_file, NULL, clipboard_callback, Icon_NOICON);
1020 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1021 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1022 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1023 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1024 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1025 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1026 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1027 delete_file, NULL, clipboard_callback, Icon_NOICON);
1028 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1029 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1030 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1031 properties, NULL, clipboard_callback, Icon_NOICON);
1032 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1033 create_dir, NULL, clipboard_callback, Icon_NOICON);
1034 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1035 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1036 #if LCD_DEPTH > 1
1037 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1038 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1039 #endif
1040 #ifdef HAVE_RECORDING
1041 static bool set_recdir(void)
1043 strncpy(global_settings.rec_directory,
1044 selected_file, MAX_FILENAME+1);
1045 settings_save();
1046 return false;
1048 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1049 set_recdir, NULL, clipboard_callback, Icon_Recording);
1050 #endif
1051 static bool add_to_faves(void)
1053 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1054 selected_file))
1055 onplay_result = ONPLAY_RELOAD_DIR;
1056 return false;
1058 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1059 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1062 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1064 switch (action)
1066 case ACTION_REQUEST_MENUITEM:
1067 if (context == CONTEXT_ID3DB)
1068 return ACTION_EXIT_MENUITEM;
1069 if (this_item == &clipboard_paste_item)
1070 { /* visible if there is something to paste */
1071 return (clipboard_selection[0] != 0) ?
1072 action : ACTION_EXIT_MENUITEM;
1074 else if (this_item == &create_dir_item)
1076 /* always visible */
1077 return action;
1079 else if ((this_item == &properties_item) ||
1080 (this_item == &rename_file_item) ||
1081 (this_item == &clipboard_cut_item) ||
1082 (this_item == &clipboard_copy_item) ||
1083 (this_item == &add_to_faves_item)
1086 /* requires an actual file */
1087 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1089 #if LCD_DEPTH > 1
1090 else if (this_item == &set_backdrop_item)
1092 if (selected_file)
1094 char *suffix = strrchr(selected_file, '.');
1095 if (suffix)
1097 if (strcasecmp(suffix, ".bmp") == 0)
1099 return action;
1103 return ACTION_EXIT_MENUITEM;
1105 #endif
1106 else if ((selected_file_attr & ATTR_DIRECTORY))
1108 if ((this_item == &delete_dir_item)
1110 return action;
1111 #ifdef HAVE_RECORDING
1112 else if (this_item == &set_recdir_item)
1113 return action;
1114 #endif
1116 else if (selected_file
1117 #ifdef HAVE_MULTIVOLUME
1118 /* no rename+delete for volumes */
1119 && !(selected_file_attr & ATTR_VOLUME)
1120 #endif
1123 if ((this_item == &delete_file_item) ||
1124 (this_item == &list_viewers_item))
1126 return action;
1129 return ACTION_EXIT_MENUITEM;
1130 break;
1132 return action;
1134 /* used when onplay() is called in the CONTEXT_WPS context */
1137 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1138 onplaymenu_callback, Icon_Audio,
1139 &sound_settings, &wps_playlist_menu, &cat_playlist_menu,
1140 #ifdef HAVE_TAGCACHE
1141 &rating_item,
1142 #endif
1143 &bookmark_menu, &browse_id3_item, &delete_file_item, &view_cue_item,
1144 #ifdef HAVE_PITCHSCREEN
1145 &pitch_screen_item,
1146 #endif
1147 #if CONFIG_CODEC == SWCODEC
1148 &eq_menu_graphical_item, &eq_browse_presets_item,
1149 #endif
1151 /* used when onplay() is not called in the CONTEXT_WPS context */
1152 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1153 onplaymenu_callback, Icon_file_view_menu,
1154 &tree_playlist_menu, &cat_playlist_menu,
1155 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1156 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1157 #if LCD_DEPTH > 1
1158 &set_backdrop_item,
1159 #endif
1160 &list_viewers_item, &create_dir_item, &properties_item,
1161 #ifdef HAVE_RECORDING
1162 &set_recdir_item,
1163 #endif
1164 &add_to_faves_item,
1166 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1168 (void)this_item;
1169 switch (action)
1171 case ACTION_TREE_STOP:
1172 if (this_item == &wps_onplay_menu)
1174 list_stop_handler();
1175 return ACTION_STD_CANCEL;
1177 break;
1178 case ACTION_EXIT_MENUITEM:
1179 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1180 break;
1182 return action;
1184 int onplay(char* file, int attr, int from)
1186 const struct menu_item_ex *menu;
1187 onplay_result = ONPLAY_OK;
1188 context = from;
1189 selected_file = file;
1190 selected_file_attr = attr;
1191 if (context == CONTEXT_WPS)
1192 menu = &wps_onplay_menu;
1193 else
1194 menu = &tree_onplay_menu;
1195 switch (do_menu(menu, NULL, NULL, false))
1197 case GO_TO_WPS:
1198 return ONPLAY_START_PLAY;
1199 case GO_TO_ROOT:
1200 case GO_TO_MAINMENU:
1201 return ONPLAY_MAINMENU;
1202 default:
1203 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;