Use new MDCT library for libfaad. Speeds up AAC-LC by 2.5MHz.
[kugel-rb.git] / apps / onplay.c
blobda3900ddc88e72c07747bc6bae047a1c20929917
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
27 #include "debug.h"
28 #include "sprintf.h"
29 #include "lcd.h"
30 #include "dir.h"
31 #include "file.h"
32 #include "audio.h"
33 #include "menu.h"
34 #include "lang.h"
35 #include "playlist.h"
36 #include "button.h"
37 #include "kernel.h"
38 #include "keyboard.h"
39 #include "mp3data.h"
40 #include "metadata.h"
41 #include "screens.h"
42 #include "tree.h"
43 #include "settings.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 #include "playlist_menu.h"
59 #include "playlist_catalog.h"
60 #ifdef HAVE_TAGCACHE
61 #include "tagtree.h"
62 #endif
63 #include "cuesheet.h"
64 #include "statusbar-skinned.h"
65 #include "pitchscreen.h"
66 #include "viewport.h"
68 static int context;
69 static char* selected_file = NULL;
70 static int selected_file_attr = 0;
71 static int onplay_result = ONPLAY_OK;
72 static char clipboard_selection[MAX_PATH];
73 static int clipboard_selection_attr = 0;
74 static bool clipboard_is_copy = false;
76 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
77 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
78 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
79 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
80 static const struct menu_item_ex name = \
81 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
82 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
83 { (void*)name##_},{.callback_and_desc = & name##__}};
85 /* ----------------------------------------------------------------------- */
86 /* Displays the bookmark menu options for the user to decide. This is an */
87 /* interface function. */
88 /* ----------------------------------------------------------------------- */
90 static int bookmark_menu_callback(int action,
91 const struct menu_item_ex *this_item);
92 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
93 ID2P(LANG_BOOKMARK_MENU_CREATE),
94 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
95 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
96 ID2P(LANG_BOOKMARK_MENU_LIST),
97 bookmark_load_menu, NULL,
98 bookmark_menu_callback, Icon_Bookmark);
99 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU),
100 bookmark_menu_callback, Icon_Bookmark,
101 &bookmark_create_menu_item, &bookmark_load_menu_item);
102 static int bookmark_menu_callback(int action,
103 const struct menu_item_ex *this_item)
105 switch (action)
107 case ACTION_REQUEST_MENUITEM:
108 if (this_item == &bookmark_load_menu_item)
110 if (bookmark_exist() == 0)
111 return ACTION_EXIT_MENUITEM;
113 /* hide the bookmark menu if there is no playback */
114 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
115 return ACTION_EXIT_MENUITEM;
116 break;
117 #ifdef HAVE_LCD_CHARCELLS
118 case ACTION_ENTER_MENUITEM:
119 status_set_param(true);
120 break;
121 #endif
122 case ACTION_EXIT_MENUITEM:
123 #ifdef HAVE_LCD_CHARCELLS
124 status_set_param(false);
125 #endif
126 settings_save();
127 break;
129 return action;
132 /* CONTEXT_WPS playlist options */
133 static bool shuffle_playlist(void)
135 playlist_sort(NULL, true);
136 playlist_randomise(NULL, current_tick, true);
138 return false;
141 static bool save_playlist(void)
143 save_playlist_screen(NULL);
144 return false;
147 MENUITEM_FUNCTION(playlist_viewer_item, 0, ID2P(LANG_VIEW_DYNAMIC_PLAYLIST),
148 playlist_viewer, NULL, NULL, Icon_Playlist);
149 MENUITEM_FUNCTION(search_playlist_item, 0, ID2P(LANG_SEARCH_IN_PLAYLIST),
150 search_playlist, NULL, NULL, Icon_Playlist);
151 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
152 save_playlist, NULL, NULL, Icon_Playlist);
153 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
154 shuffle_playlist, NULL, NULL, Icon_Playlist);
155 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
156 NULL, Icon_Playlist,
157 &playlist_viewer_item, &search_playlist_item,
158 &playlist_save_item, &reshuffle_item
161 /* CONTEXT_[TREE|ID3DB] playlist options */
162 static bool add_to_playlist(int position, bool queue)
164 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
165 const char *lines[] = {
166 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
167 selected_file
169 const struct text_message message={lines, 2};
171 splash(0, ID2P(LANG_WAIT));
173 if (new_playlist)
174 playlist_create(NULL, NULL);
176 /* always set seed before inserting shuffled */
177 if (position == PLAYLIST_INSERT_SHUFFLED ||
178 position == PLAYLIST_INSERT_LAST_SHUFFLED)
180 srand(current_tick);
181 if (position == PLAYLIST_INSERT_LAST_SHUFFLED)
182 playlist_set_last_shuffled_start();
185 #ifdef HAVE_TAGCACHE
186 if (context == CONTEXT_ID3DB)
188 tagtree_insert_selection_playlist(position, queue);
190 else
191 #endif
193 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
194 playlist_insert_track(NULL, selected_file, position, queue, true);
195 else if (selected_file_attr & ATTR_DIRECTORY)
197 bool recurse = false;
199 if (global_settings.recursive_dir_insert != RECURSE_ASK)
200 recurse = (bool)global_settings.recursive_dir_insert;
201 else
203 /* Ask if user wants to recurse directory */
204 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
207 playlist_insert_directory(NULL, selected_file, position, queue,
208 recurse);
210 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
211 playlist_insert_playlist(NULL, selected_file, position, queue);
214 if (new_playlist && (playlist_amount() > 0))
216 /* nothing is currently playing so begin playing what we just
217 inserted */
218 if (global_settings.playlist_shuffle)
219 playlist_shuffle(current_tick, -1);
220 playlist_start(0,0);
221 onplay_result = ONPLAY_START_PLAY;
224 return false;
227 static bool view_playlist(void)
229 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
230 bool result;
232 result = playlist_viewer_ex(selected_file);
234 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
235 onplay_result == ONPLAY_OK)
236 /* playlist was started from viewer */
237 onplay_result = ONPLAY_START_PLAY;
239 return result;
242 static int playlist_insert_func(void *param)
244 if (((intptr_t)param == PLAYLIST_REPLACE) && !warn_on_pl_erase())
245 return 0;
246 add_to_playlist((intptr_t)param, false);
247 return 0;
250 static int playlist_queue_func(void *param)
252 add_to_playlist((intptr_t)param, true);
253 return 0;
256 static int treeplaylist_wplayback_callback(int action,
257 const struct menu_item_ex* this_item)
259 (void)this_item;
260 switch (action)
262 case ACTION_REQUEST_MENUITEM:
263 if (audio_status() & AUDIO_STATUS_PLAY)
264 return action;
265 else
266 return ACTION_EXIT_MENUITEM;
267 break;
269 return action;
272 static int treeplaylist_callback(int action,
273 const struct menu_item_ex *this_item);
275 /* insert items */
276 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
277 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
278 NULL, Icon_Playlist);
279 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
280 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
281 treeplaylist_wplayback_callback, Icon_Playlist);
282 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
283 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
284 treeplaylist_wplayback_callback, Icon_Playlist);
285 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
286 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
287 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
288 treeplaylist_callback, Icon_Playlist);
289 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
290 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
291 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
292 treeplaylist_callback, Icon_Playlist);
293 /* queue items */
294 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
295 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
296 treeplaylist_wplayback_callback, Icon_Playlist);
297 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
298 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
299 treeplaylist_wplayback_callback, Icon_Playlist);
300 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
301 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
302 treeplaylist_wplayback_callback, Icon_Playlist);
303 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
304 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
305 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
306 treeplaylist_wplayback_callback, Icon_Playlist);
307 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
308 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
309 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
310 treeplaylist_callback, Icon_Playlist);
311 /* replace playlist */
312 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
313 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
314 treeplaylist_wplayback_callback, Icon_Playlist);
316 /* others */
317 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
318 view_playlist, NULL,
319 treeplaylist_callback, Icon_Playlist);
321 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
322 treeplaylist_callback, Icon_Playlist,
324 /* view */
325 &view_playlist_item,
327 /* insert */
328 &i_pl_item, &i_first_pl_item, &i_last_pl_item,
329 &i_shuf_pl_item, &i_last_shuf_pl_item,
330 /* queue */
332 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
333 &q_shuf_pl_item, &q_last_shuf_pl_item,
335 /* replace */
336 &replace_pl_item
338 static int treeplaylist_callback(int action,
339 const struct menu_item_ex *this_item)
341 switch (action)
343 case ACTION_REQUEST_MENUITEM:
344 if (this_item == &tree_playlist_menu)
346 if (((selected_file_attr & FILE_ATTR_MASK) ==
347 FILE_ATTR_AUDIO) ||
348 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
349 (selected_file_attr & ATTR_DIRECTORY))
351 return action;
354 else if (this_item == &view_playlist_item)
356 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
357 context == CONTEXT_TREE)
359 return action;
362 else if (this_item == &i_shuf_pl_item)
364 if ((audio_status() & AUDIO_STATUS_PLAY) ||
365 (selected_file_attr & ATTR_DIRECTORY) ||
366 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
368 return action;
371 else if (this_item == &i_last_shuf_pl_item ||
372 this_item == &q_last_shuf_pl_item)
374 if ((playlist_amount() > 0) &&
375 (audio_status() & AUDIO_STATUS_PLAY) &&
376 ((selected_file_attr & ATTR_DIRECTORY) ||
377 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
379 return action;
382 return ACTION_EXIT_MENUITEM;
383 break;
385 return action;
389 /* playlist catalog options */
390 static bool cat_add_to_a_playlist(void)
392 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
393 false, NULL);
396 static bool cat_add_to_a_new_playlist(void)
398 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
399 true, NULL);
402 static int cat_playlist_callback(int action,
403 const struct menu_item_ex *this_item);
404 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
405 catalog_view_playlists, 0,
406 cat_playlist_callback, Icon_Playlist);
407 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
408 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
409 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
410 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
411 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
412 cat_playlist_callback, Icon_Playlist,
413 &cat_view_lists, &cat_add_to_list, &cat_add_to_new);
415 static int cat_playlist_callback(int action,
416 const struct menu_item_ex *this_item)
418 if (!selected_file ||
419 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
420 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
421 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
423 return ACTION_EXIT_MENUITEM;
426 switch (action)
428 case ACTION_REQUEST_MENUITEM:
429 if (this_item == &cat_view_lists)
431 return action;
433 else if (((audio_status() & AUDIO_STATUS_PLAY &&
434 context == CONTEXT_WPS) ||
435 context == CONTEXT_TREE))
437 return action;
439 else
440 return ACTION_EXIT_MENUITEM;
441 break;
443 return action;
446 #ifdef HAVE_LCD_BITMAP
447 static void draw_slider(void)
449 int i;
450 FOR_NB_SCREENS(i)
452 struct viewport vp;
453 int slider_height = 2*screens[i].getcharheight();
454 viewport_set_defaults(&vp, i);
455 screens[i].set_viewport(&vp);
456 show_busy_slider(&screens[i], 1, vp.height - slider_height,
457 vp.width-2, slider_height-1);
458 screens[i].update_viewport();
459 screens[i].set_viewport(NULL);
462 #else
463 #define draw_slider()
464 #endif
466 /* helper function to remove a non-empty directory */
467 static int remove_dir(char* dirname, int len)
469 int result = 0;
470 DIR* dir;
471 int dirlen = strlen(dirname);
473 dir = opendir(dirname);
474 if (!dir)
475 return -1; /* open error */
477 while(true)
479 struct dirent* entry;
480 /* walk through the directory content */
481 entry = readdir(dir);
482 if (!entry)
483 break;
485 dirname[dirlen] ='\0';
486 /* inform the user which dir we're deleting */
487 splash(0, dirname);
489 /* append name to current directory */
490 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
491 if (entry->attribute & ATTR_DIRECTORY)
492 { /* remove a subdirectory */
493 if (!strcmp((char *)entry->d_name, ".") ||
494 !strcmp((char *)entry->d_name, ".."))
495 continue; /* skip these */
497 result = remove_dir(dirname, len); /* recursion */
498 if (result)
499 break; /* or better continue, delete what we can? */
501 else
502 { /* remove a file */
503 draw_slider();
504 result = remove(dirname);
506 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
508 splash(HZ, ID2P(LANG_CANCEL));
509 result = -1;
510 break;
513 closedir(dir);
515 if (!result)
516 { /* remove the now empty directory */
517 dirname[dirlen] = '\0'; /* terminate to original length */
519 result = rmdir(dirname);
522 return result;
526 /* share code for file and directory deletion, saves space */
527 static bool delete_handler(bool is_dir)
529 char file_to_delete[MAX_PATH];
530 strcpy(file_to_delete, selected_file);
532 const char *lines[]={
533 ID2P(LANG_REALLY_DELETE),
534 file_to_delete
536 const char *yes_lines[]={
537 ID2P(LANG_DELETING),
538 file_to_delete
541 const struct text_message message={lines, 2};
542 const struct text_message yes_message={yes_lines, 2};
544 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
545 return false;
547 splash(0, str(LANG_DELETING));
549 int res;
550 if (is_dir)
552 char pathname[MAX_PATH]; /* space to go deep */
553 cpu_boost(true);
554 strlcpy(pathname, file_to_delete, sizeof(pathname));
555 res = remove_dir(pathname, sizeof(pathname));
556 cpu_boost(false);
558 else
559 res = remove(file_to_delete);
561 if (!res)
562 onplay_result = ONPLAY_RELOAD_DIR;
564 return (res == 0);
567 static bool delete_file(void)
569 return delete_handler(false);
572 static bool delete_dir(void)
574 return delete_handler(true);
577 static bool rename_file(void)
579 char newname[MAX_PATH];
580 char* ptr = strrchr(selected_file, '/') + 1;
581 int pathlen = (ptr - selected_file);
582 strlcpy(newname, selected_file, sizeof(newname));
583 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
584 if (!strlen(newname + pathlen) ||
585 (rename(selected_file, newname) < 0)) {
586 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
587 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
589 else
590 onplay_result = ONPLAY_RELOAD_DIR;
593 return false;
596 static bool create_dir(void)
598 char dirname[MAX_PATH];
599 char *cwd;
600 int rc;
601 int pathlen;
603 cwd = getcwd(NULL, 0);
604 memset(dirname, 0, sizeof dirname);
606 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
608 pathlen = strlen(dirname);
609 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
610 if (rc < 0)
611 return false;
613 rc = mkdir(dirname);
614 if (rc < 0) {
615 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
616 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
617 str(LANG_FAILED));
618 } else {
619 onplay_result = ONPLAY_RELOAD_DIR;
622 return true;
625 /* Store the current selection in the clipboard */
626 static bool clipboard_clip(bool copy)
628 clipboard_selection[0] = 0;
629 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
630 clipboard_selection_attr = selected_file_attr;
631 clipboard_is_copy = copy;
633 return true;
636 static bool clipboard_cut(void)
638 return clipboard_clip(false);
641 static bool clipboard_copy(void)
643 return clipboard_clip(true);
646 /* Paste a file to a new directory. Will overwrite always. */
647 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
649 int src_fd, target_fd;
650 size_t buffersize;
651 ssize_t size, bytesread, byteswritten;
652 char *buffer;
653 bool result = false;
655 if (copy) {
656 /* See if we can get the plugin buffer for the file copy buffer */
657 buffer = (char *) plugin_get_buffer(&buffersize);
658 if (buffer == NULL || buffersize < 512) {
659 /* Not large enough, try for a disk sector worth of stack
660 instead */
661 buffersize = 512;
662 buffer = (char *) __builtin_alloca(buffersize);
665 if (buffer == NULL) {
666 return false;
669 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
670 size */
672 src_fd = open(src, O_RDONLY);
674 if (src_fd >= 0) {
675 target_fd = creat(target);
677 if (target_fd >= 0) {
678 result = true;
680 size = filesize(src_fd);
682 if (size == -1) {
683 result = false;
686 while(size > 0) {
687 bytesread = read(src_fd, buffer, buffersize);
689 if (bytesread == -1) {
690 result = false;
691 break;
694 size -= bytesread;
696 while(bytesread > 0) {
697 byteswritten = write(target_fd, buffer, bytesread);
699 if (byteswritten < 0) {
700 result = false;
701 size = 0;
702 break;
705 bytesread -= byteswritten;
706 draw_slider();
710 close(target_fd);
712 /* Copy failed. Cleanup. */
713 if (!result) {
714 remove(target);
718 close(src_fd);
720 } else {
721 result = rename(src, target) == 0;
722 #ifdef HAVE_MULTIVOLUME
723 if (!result) {
724 if (errno == EXDEV) {
725 /* Failed because cross volume rename doesn't work. Copy
726 instead */
727 result = clipboard_pastefile(src, target, true);
729 if (result) {
730 result = remove(src) == 0;
734 #endif
737 return result;
740 /* Paste a directory to a new location. Designed to be called by
741 clipboard_paste */
742 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
743 int targetlen, bool copy)
745 DIR *srcdir;
746 int srcdirlen = strlen(src);
747 int targetdirlen = strlen(target);
748 bool result = true;
750 if (!file_exists(target)) {
751 if (!copy) {
752 /* Just move the directory */
753 result = rename(src, target) == 0;
755 #ifdef HAVE_MULTIVOLUME
756 if (!result && errno == EXDEV) {
757 /* Try a copy as we're going across devices */
758 result = clipboard_pastedirectory(src, srclen, target,
759 targetlen, true);
761 /* If it worked, remove the source directory */
762 if (result) {
763 remove_dir(src, srclen);
766 #endif
767 return result;
768 } else {
769 /* Make a directory to copy things to */
770 result = mkdir(target) == 0;
774 /* Check if something went wrong already */
775 if (!result) {
776 return result;
779 srcdir = opendir(src);
780 if (!srcdir) {
781 return false;
784 /* This loop will exit as soon as there's a problem */
785 while(result)
787 struct dirent* entry;
788 /* walk through the directory content */
789 entry = readdir(srcdir);
790 if (!entry)
791 break;
793 /* append name to current directory */
794 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
795 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
796 entry->d_name);
798 DEBUGF("Copy %s to %s\n", src, target);
800 if (entry->attribute & ATTR_DIRECTORY)
801 { /* copy/move a subdirectory */
802 if (!strcmp((char *)entry->d_name, ".") ||
803 !strcmp((char *)entry->d_name, ".."))
804 continue; /* skip these */
806 result = clipboard_pastedirectory(src, srclen, target, targetlen,
807 copy); /* recursion */
809 else
810 { /* copy/move a file */
811 draw_slider();
812 result = clipboard_pastefile(src, target, copy);
816 closedir(srcdir);
818 if (result) {
819 src[srcdirlen] = '\0'; /* terminate to original length */
820 target[targetdirlen] = '\0'; /* terminate to original length */
823 return result;
826 /* Paste the clipboard to the current directory */
827 static bool clipboard_paste(void)
829 char target[MAX_PATH];
830 char *cwd, *nameptr;
831 bool success;
833 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
834 static const struct text_message message={lines, 1};
836 /* Get the name of the current directory */
837 cwd = getcwd(NULL, 0);
839 /* Figure out the name of the selection */
840 nameptr = strrchr(clipboard_selection, '/');
842 /* Final target is current directory plus name of selection */
843 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
845 /* If the target existed but they choose not to overwite, exit */
846 if (file_exists(target) &&
847 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
848 return false;
851 if (clipboard_is_copy) {
852 splash(0, ID2P(LANG_COPYING));
854 else
856 splash(0, ID2P(LANG_MOVING));
859 /* Now figure out what we're doing */
860 cpu_boost(true);
861 if (clipboard_selection_attr & ATTR_DIRECTORY) {
862 /* Recursion. Set up external stack */
863 char srcpath[MAX_PATH];
864 char targetpath[MAX_PATH];
865 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
867 /* Do not allow the user to paste a directory into a dir they are
868 copying */
869 success = 0;
871 else
873 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
874 strlcpy(targetpath, target, sizeof(targetpath));
876 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
877 target, sizeof(targetpath), clipboard_is_copy);
879 if (success && !clipboard_is_copy)
881 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
882 remove_dir(srcpath, sizeof(srcpath));
885 } else {
886 success = clipboard_pastefile(clipboard_selection, target,
887 clipboard_is_copy);
889 cpu_boost(false);
891 /* Did it work? */
892 if (success) {
893 /* Reset everything */
894 clipboard_selection[0] = 0;
895 clipboard_selection_attr = 0;
896 clipboard_is_copy = false;
898 /* Force reload of the current directory */
899 onplay_result = ONPLAY_RELOAD_DIR;
900 } else {
901 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
902 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
903 str(LANG_FAILED));
906 return true;
909 #ifdef HAVE_TAGCACHE
910 static int set_rating_inline(void)
912 struct mp3entry* id3 = audio_current_track();
913 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
915 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
916 NULL, 1, 0, 10, NULL, NULL);
917 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
919 else
920 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
921 return 0;
923 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
925 (void)this_item;
926 switch (action)
928 case ACTION_REQUEST_MENUITEM:
929 if (!selected_file || !global_settings.runtimedb ||
930 !tagcache_is_usable())
931 return ACTION_EXIT_MENUITEM;
932 break;
934 return action;
936 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
937 set_rating_inline, NULL,
938 ratingitem_callback, Icon_Questionmark);
939 #endif
941 static bool view_cue(void)
943 struct mp3entry* id3 = audio_current_track();
944 if(id3 && id3->cuesheet)
946 browse_cuesheet(id3->cuesheet);
948 return false;
950 static int view_cue_item_callback(int action,
951 const struct menu_item_ex *this_item)
953 (void)this_item;
954 struct mp3entry* id3 = audio_current_track();
955 switch (action)
957 case ACTION_REQUEST_MENUITEM:
958 if (!selected_file
959 || !id3 || !id3->cuesheet)
960 return ACTION_EXIT_MENUITEM;
961 break;
963 return action;
965 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
966 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
968 /* CONTEXT_WPS items */
969 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
970 browse_id3, NULL, NULL, Icon_NOICON);
971 #ifdef HAVE_PITCHSCREEN
972 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
973 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
974 #endif
976 /* CONTEXT_[TREE|ID3DB] items */
977 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
978 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
979 rename_file, NULL, clipboard_callback, Icon_NOICON);
980 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
981 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
982 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
983 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
984 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
985 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
986 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
987 delete_file, NULL, clipboard_callback, Icon_NOICON);
988 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
989 delete_dir, NULL, clipboard_callback, Icon_NOICON);
990 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
991 create_dir, NULL, clipboard_callback, Icon_NOICON);
993 /* other items */
994 static bool list_viewers(void)
996 int ret = filetype_list_viewers(selected_file);
997 if (ret == PLUGIN_USB_CONNECTED)
998 onplay_result = ONPLAY_RELOAD_DIR;
999 return false;
1002 static bool onplay_load_plugin(void *param)
1004 int ret = filetype_load_plugin((const char*)param, selected_file);
1005 if (ret == PLUGIN_USB_CONNECTED)
1006 onplay_result = ONPLAY_RELOAD_DIR;
1007 return false;
1010 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1011 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1013 onplay_load_plugin, (void *)"properties",
1014 clipboard_callback, Icon_NOICON);
1015 MENUITEM_FUNCTION(add_to_faves_item, MENU_FUNC_USEPARAM, ID2P(LANG_ADD_TO_FAVES),
1016 onplay_load_plugin, (void *)"shortcuts_append",
1017 clipboard_callback, Icon_NOICON);
1019 #if LCD_DEPTH > 1
1020 static bool set_backdrop(void)
1022 /* load the image */
1023 if(sb_set_backdrop(SCREEN_MAIN, selected_file)) {
1024 splash(HZ, str(LANG_BACKDROP_LOADED));
1025 set_file(selected_file, (char *)global_settings.backdrop_file,
1026 MAX_FILENAME);
1027 return true;
1028 } else {
1029 splash(HZ, str(LANG_BACKDROP_FAILED));
1030 return false;
1032 return true;
1034 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1035 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1036 #endif
1037 #ifdef HAVE_RECORDING
1038 static bool set_recdir(void)
1040 strlcpy(global_settings.rec_directory, selected_file, MAX_FILENAME+1);
1041 settings_save();
1042 return false;
1044 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1045 set_recdir, NULL, clipboard_callback, Icon_Recording);
1046 #endif
1048 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1050 switch (action)
1052 case ACTION_REQUEST_MENUITEM:
1053 #ifdef HAVE_MULTIVOLUME
1054 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1055 (this_item == &rename_file_item ||
1056 this_item == &delete_dir_item ||
1057 this_item == &clipboard_cut_item) )
1058 return ACTION_EXIT_MENUITEM;
1059 /* no rename+delete for volumes */
1060 if ((selected_file_attr & ATTR_VOLUME) &&
1061 (this_item == &delete_file_item ||
1062 this_item == &list_viewers_item))
1063 return ACTION_EXIT_MENUITEM;
1064 #endif
1065 #ifdef HAVE_TAGCACHE
1066 if (context == CONTEXT_ID3DB)
1067 return ACTION_EXIT_MENUITEM;
1068 #endif
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 (selected_file)
1081 /* requires an actual file */
1082 if (this_item == &rename_file_item ||
1083 this_item == &clipboard_cut_item ||
1084 this_item == &clipboard_copy_item ||
1085 this_item == &properties_item ||
1086 this_item == &add_to_faves_item)
1088 return action;
1090 else if ((selected_file_attr & ATTR_DIRECTORY))
1092 /* only for directories */
1093 if (this_item == &delete_dir_item
1094 #ifdef HAVE_RECORDING
1095 || this_item == &set_recdir_item
1096 #endif
1098 return action;
1100 else if (this_item == &delete_file_item ||
1101 this_item == &list_viewers_item)
1103 /* only for files */
1104 return action;
1106 #if LCD_DEPTH > 1
1107 else if (this_item == &set_backdrop_item)
1109 char *suffix = strrchr(selected_file, '.');
1110 if (suffix)
1112 if (strcasecmp(suffix, ".bmp") == 0)
1114 return action;
1118 #endif
1120 return ACTION_EXIT_MENUITEM;
1121 break;
1123 return action;
1126 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1127 /* used when onplay() is called in the CONTEXT_WPS context */
1128 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1129 onplaymenu_callback, Icon_Audio,
1130 &wps_playlist_menu, &cat_playlist_menu,
1131 &sound_settings, &playback_settings,
1132 #ifdef HAVE_TAGCACHE
1133 &rating_item,
1134 #endif
1135 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1136 &delete_file_item, &view_cue_item,
1137 #ifdef HAVE_PITCHSCREEN
1138 &pitch_screen_item,
1139 #endif
1141 /* used when onplay() is not called in the CONTEXT_WPS context */
1142 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1143 onplaymenu_callback, Icon_file_view_menu,
1144 &tree_playlist_menu, &cat_playlist_menu,
1145 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1146 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1147 #if LCD_DEPTH > 1
1148 &set_backdrop_item,
1149 #endif
1150 &list_viewers_item, &create_dir_item, &properties_item,
1151 #ifdef HAVE_RECORDING
1152 &set_recdir_item,
1153 #endif
1154 &add_to_faves_item,
1156 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1158 switch (action)
1160 case ACTION_TREE_STOP:
1161 if (this_item == &wps_onplay_menu)
1163 list_stop_handler();
1164 return ACTION_STD_CANCEL;
1166 break;
1167 case ACTION_EXIT_MENUITEM:
1168 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1169 break;
1171 return action;
1173 int onplay(char* file, int attr, int from)
1175 const struct menu_item_ex *menu;
1176 onplay_result = ONPLAY_OK;
1177 context = from;
1178 selected_file = file;
1179 selected_file_attr = attr;
1180 if (context == CONTEXT_WPS)
1181 menu = &wps_onplay_menu;
1182 else
1183 menu = &tree_onplay_menu;
1184 switch (do_menu(menu, NULL, NULL, false))
1186 case GO_TO_WPS:
1187 return ONPLAY_START_PLAY;
1188 case GO_TO_ROOT:
1189 case GO_TO_MAINMENU:
1190 return ONPLAY_MAINMENU;
1191 default:
1192 return onplay_result;