* Onda VX747: add browse screen, pitchscreen, context menu, quickscreen, rewind...
[kugel-rb.git] / apps / onplay.c
blob3052f8dfdbb3d83977688dfc59cf5f568ef15874
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 "buffer.h"
44 #include "settings.h"
45 #include "playlist_viewer.h"
46 #include "talk.h"
47 #include "onplay.h"
48 #include "filetypes.h"
49 #include "plugin.h"
50 #include "bookmark.h"
51 #include "action.h"
52 #include "splash.h"
53 #include "yesno.h"
54 #include "menus/exported_menus.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
58 #include "sound_menu.h"
59 #include "playlist_menu.h"
60 #include "playlist_catalog.h"
61 #ifdef HAVE_TAGCACHE
62 #include "tagtree.h"
63 #endif
64 #include "cuesheet.h"
65 #include "backdrop.h"
66 #include "pitchscreen.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 #ifdef HAVE_LCD_BITMAP
86 static void draw_slider(void);
87 #else
88 #define draw_slider()
89 #endif
90 /* ----------------------------------------------------------------------- */
91 /* Displays the bookmark menu options for the user to decide. This is an */
92 /* interface function. */
93 /* ----------------------------------------------------------------------- */
95 static int bookmark_menu_callback(int action,
96 const struct menu_item_ex *this_item);
97 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
98 ID2P(LANG_BOOKMARK_MENU_CREATE),
99 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
100 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
101 ID2P(LANG_BOOKMARK_MENU_LIST),
102 bookmark_load_menu, NULL,
103 bookmark_menu_callback, Icon_Bookmark);
104 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
105 Icon_Bookmark, &bookmark_create_menu_item,
106 &bookmark_load_menu_item);
107 static int bookmark_menu_callback(int action,
108 const struct menu_item_ex *this_item)
110 (void)this_item;
111 switch (action)
113 case ACTION_REQUEST_MENUITEM:
114 if (this_item == &bookmark_load_menu_item)
116 if (bookmark_exist() == 0)
117 return ACTION_EXIT_MENUITEM;
119 /* hide the bookmark menu if there is no playback */
120 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
121 return ACTION_EXIT_MENUITEM;
122 break;
123 #ifdef HAVE_LCD_CHARCELLS
124 case ACTION_ENTER_MENUITEM:
125 status_set_param(true);
126 break;
127 #endif
128 case ACTION_EXIT_MENUITEM:
129 #ifdef HAVE_LCD_CHARCELLS
130 status_set_param(false);
131 #endif
132 settings_save();
133 break;
135 return action;
138 static bool list_viewers(void)
140 int ret = filetype_list_viewers(selected_file);
141 if (ret == PLUGIN_USB_CONNECTED)
142 onplay_result = ONPLAY_RELOAD_DIR;
143 return false;
146 static bool shuffle_playlist(void)
148 playlist_sort(NULL, true);
149 playlist_randomise(NULL, current_tick, true);
151 return false;
154 static bool save_playlist(void)
156 save_playlist_screen(NULL);
157 return false;
160 static bool add_to_playlist(int position, bool queue)
162 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
163 const char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 const struct text_message message={lines, 2};
169 splash(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 onplay_result = ONPLAY_START_PLAY;
217 return false;
220 static bool view_playlist(void)
222 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
223 bool result;
225 result = playlist_viewer_ex(selected_file);
227 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
228 onplay_result == ONPLAY_OK)
229 /* playlist was started from viewer */
230 onplay_result = ONPLAY_START_PLAY;
232 return result;
235 static bool cat_add_to_a_playlist(void)
237 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
238 false, NULL);
241 static bool cat_add_to_a_new_playlist(void)
243 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
244 true, NULL);
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_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
258 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
259 &cat_add_to_new);
261 static int cat_playlist_callback(int action,
262 const struct menu_item_ex *this_item)
264 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
265 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
266 ((selected_file_attr & ATTR_DIRECTORY) == 0))
268 return ACTION_EXIT_MENUITEM;
271 switch (action)
273 case ACTION_REQUEST_MENUITEM:
274 if (this_item == &cat_view_lists)
276 if (context == CONTEXT_WPS)
277 return action;
279 else if (selected_file && /* set before calling this menu,
280 so safe */
281 ((audio_status() & AUDIO_STATUS_PLAY &&
282 context == CONTEXT_WPS) ||
283 context == CONTEXT_TREE))
285 return action;
287 else
288 return ACTION_EXIT_MENUITEM;
289 break;
291 return action;
295 /* CONTEXT_WPS playlist options */
296 MENUITEM_FUNCTION(playlist_viewer_item, 0,
297 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
298 NULL, NULL, Icon_Playlist);
299 MENUITEM_FUNCTION(search_playlist_item, 0,
300 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
301 NULL, NULL, Icon_Playlist);
302 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
303 save_playlist, NULL, NULL, Icon_Playlist);
304 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
305 shuffle_playlist, NULL, NULL, Icon_Playlist);
306 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
307 NULL, Icon_Playlist,
308 &playlist_viewer_item, &search_playlist_item,
309 &playlist_save_item, &reshuffle_item
312 /* CONTEXT_[TREE|ID3DB] playlist options */
313 static int playlist_insert_func(void *param)
315 add_to_playlist((intptr_t)param, false);
316 return 0;
318 static int playlist_queue_func(void *param)
320 add_to_playlist((intptr_t)param, true);
321 return 0;
323 static int treeplaylist_wplayback_callback(int action,
324 const struct menu_item_ex*
325 this_item)
327 (void)this_item;
328 switch (action)
330 case ACTION_REQUEST_MENUITEM:
331 if (audio_status() & AUDIO_STATUS_PLAY)
332 return action;
333 else
334 return ACTION_EXIT_MENUITEM;
335 break;
337 return action;
340 static int treeplaylist_callback(int action,
341 const struct menu_item_ex *this_item);
343 /* insert items */
344 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
345 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
346 treeplaylist_callback, Icon_Playlist);
347 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
348 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
349 treeplaylist_wplayback_callback, Icon_Playlist);
350 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
351 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
352 treeplaylist_wplayback_callback, Icon_Playlist);
353 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
354 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
355 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
356 Icon_Playlist);
357 /* queue items */
358 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
359 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
360 treeplaylist_wplayback_callback, Icon_Playlist);
361 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
362 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
363 treeplaylist_wplayback_callback, Icon_Playlist);
364 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
365 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
366 treeplaylist_wplayback_callback, Icon_Playlist);
367 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
368 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
369 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
370 treeplaylist_wplayback_callback, Icon_Playlist);
371 /* replace playlist */
372 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
373 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
374 treeplaylist_wplayback_callback, Icon_Playlist);
375 /* others */
377 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
378 view_playlist, NULL,
379 treeplaylist_callback, Icon_Playlist);
381 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
382 treeplaylist_callback, Icon_Playlist,
384 /* view */
385 &view_playlist_item,
387 /* insert */
388 &i_pl_item, &i_first_pl_item,
389 &i_last_pl_item, &i_shuf_pl_item,
391 /* queue */
392 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
393 &q_shuf_pl_item,
395 /* replace */
396 &replace_pl_item
398 static int treeplaylist_callback(int action,
399 const struct menu_item_ex *this_item)
401 (void)this_item;
402 switch (action)
404 case ACTION_REQUEST_MENUITEM:
405 if (this_item == &tree_playlist_menu)
407 if (((selected_file_attr & FILE_ATTR_MASK) ==
408 FILE_ATTR_AUDIO) ||
409 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
410 (selected_file_attr & ATTR_DIRECTORY))
412 return action;
414 else
415 return ACTION_EXIT_MENUITEM;
417 else if (this_item == &view_playlist_item)
419 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
420 context == CONTEXT_TREE)
421 return action;
422 else
423 return ACTION_EXIT_MENUITEM;
425 else if (this_item == &i_shuf_pl_item)
428 if (audio_status() & AUDIO_STATUS_PLAY)
430 return action;
432 else if ((this_item == &i_shuf_pl_item) &&
433 ((selected_file_attr & ATTR_DIRECTORY) ||
434 ((selected_file_attr & FILE_ATTR_MASK) ==
435 FILE_ATTR_M3U)))
437 return action;
439 return ACTION_EXIT_MENUITEM;
441 break;
443 return action;
446 /* helper function to remove a non-empty directory */
447 static int remove_dir(char* dirname, int len)
449 int result = 0;
450 DIR* dir;
451 int dirlen = strlen(dirname);
453 dir = opendir(dirname);
454 if (!dir)
455 return -1; /* open error */
457 while(true)
459 struct dirent* entry;
460 /* walk through the directory content */
461 entry = readdir(dir);
462 if (!entry)
463 break;
465 dirname[dirlen] ='\0';
466 splash(0, dirname);
468 /* append name to current directory */
469 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
470 if (entry->attribute & ATTR_DIRECTORY)
471 { /* remove a subdirectory */
472 if (!strcmp((char *)entry->d_name, ".") ||
473 !strcmp((char *)entry->d_name, ".."))
474 continue; /* skip these */
476 /* inform the user which dir we're deleting */
478 result = remove_dir(dirname, len); /* recursion */
479 if (result)
480 break; /* or better continue, delete what we can? */
482 else
483 { /* remove a file */
484 draw_slider();
485 result = remove(dirname);
487 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
489 splash(HZ, ID2P(LANG_CANCEL));
490 result = -1;
491 break;
494 closedir(dir);
496 if (!result)
497 { /* remove the now empty directory */
498 dirname[dirlen] = '\0'; /* terminate to original length */
500 result = rmdir(dirname);
503 return result;
507 /* share code for file and directory deletion, saves space */
508 static bool delete_handler(bool is_dir)
510 char file_to_delete[MAX_PATH];
511 strcpy(file_to_delete, selected_file);
513 const char *lines[]={
514 ID2P(LANG_REALLY_DELETE),
515 file_to_delete
517 const char *yes_lines[]={
518 ID2P(LANG_DELETING),
519 file_to_delete
522 const struct text_message message={lines, 2};
523 const struct text_message yes_message={yes_lines, 2};
525 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
526 return false;
528 splash(0, str(LANG_DELETING));
530 int res;
531 if (is_dir)
533 char pathname[MAX_PATH]; /* space to go deep */
534 cpu_boost(true);
535 strlcpy(pathname, file_to_delete, sizeof(pathname));
536 res = remove_dir(pathname, sizeof(pathname));
537 cpu_boost(false);
539 else
540 res = remove(file_to_delete);
542 if (!res)
543 onplay_result = ONPLAY_RELOAD_DIR;
545 return false;
549 static bool delete_file(void)
551 return delete_handler(false);
554 static bool delete_dir(void)
556 return delete_handler(true);
559 #if LCD_DEPTH > 1
560 static bool set_backdrop(void)
562 /* load the image */
563 if(backdrop_load(BACKDROP_MAIN, selected_file)) {
564 splash(HZ, str(LANG_BACKDROP_LOADED));
565 set_file(selected_file, (char *)global_settings.backdrop_file,
566 MAX_FILENAME);
567 backdrop_show(BACKDROP_MAIN);
568 return true;
569 } else {
570 splash(HZ, str(LANG_BACKDROP_FAILED));
571 return false;
574 #endif
576 static bool rename_file(void)
578 char newname[MAX_PATH];
579 char* ptr = strrchr(selected_file, '/') + 1;
580 int pathlen = (ptr - selected_file);
581 strlcpy(newname, selected_file, sizeof(newname));
582 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
583 if (!strlen(newname + pathlen) ||
584 (rename(selected_file, newname) < 0)) {
585 lcd_clear_display();
586 lcd_puts(0,0,str(LANG_RENAME));
587 lcd_puts(0,1,str(LANG_FAILED));
588 lcd_update();
589 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
590 sleep(HZ*2);
592 else
593 onplay_result = ONPLAY_RELOAD_DIR;
596 return false;
599 static bool create_dir(void)
601 char dirname[MAX_PATH];
602 char *cwd;
603 int rc;
604 int pathlen;
606 cwd = getcwd(NULL, 0);
607 memset(dirname, 0, sizeof dirname);
609 snprintf(dirname, sizeof dirname, "%s/",
610 cwd[1] ? cwd : "");
612 pathlen = strlen(dirname);
613 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
614 if (rc < 0)
615 return false;
617 rc = mkdir(dirname);
618 if (rc < 0) {
619 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
620 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
621 str(LANG_FAILED));
622 } else {
623 onplay_result = ONPLAY_RELOAD_DIR;
626 return true;
629 static bool properties(void)
631 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
632 selected_file))
633 onplay_result = ONPLAY_RELOAD_DIR;
634 return false;
637 /* Store the current selection in the clipboard */
638 static bool clipboard_clip(bool copy)
640 clipboard_selection[0] = 0;
641 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
642 clipboard_selection_attr = selected_file_attr;
643 clipboard_is_copy = copy;
645 return true;
648 static bool clipboard_cut(void)
650 return clipboard_clip(false);
653 static bool clipboard_copy(void)
655 return clipboard_clip(true);
658 #ifdef HAVE_LCD_BITMAP
659 static void draw_slider(void)
661 int i;
662 FOR_NB_SCREENS(i)
664 show_busy_slider(&screens[i], 1,
665 LCD_HEIGHT-2*screens[i].getcharheight(),
666 LCD_WIDTH-2, 2*screens[i].getcharheight()-1);
667 screens[i].update();
670 #endif
672 /* Paste a file to a new directory. Will overwrite always. */
673 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
675 int src_fd, target_fd;
676 size_t buffersize;
677 ssize_t size, bytesread, byteswritten;
678 char *buffer;
679 bool result = false;
681 if (copy) {
682 /* See if we can get the plugin buffer for the file copy buffer */
683 buffer = (char *) plugin_get_buffer(&buffersize);
684 if (buffer == NULL || buffersize < 512) {
685 /* Not large enough, try for a disk sector worth of stack
686 instead */
687 buffersize = 512;
688 buffer = (char *) __builtin_alloca(buffersize);
691 if (buffer == NULL) {
692 return false;
695 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
696 size */
698 src_fd = open(src, O_RDONLY);
700 if (src_fd >= 0) {
701 target_fd = creat(target);
703 if (target_fd >= 0) {
704 result = true;
706 size = filesize(src_fd);
708 if (size == -1) {
709 result = false;
712 while(size > 0) {
713 bytesread = read(src_fd, buffer, buffersize);
715 if (bytesread == -1) {
716 result = false;
717 break;
720 size -= bytesread;
722 while(bytesread > 0) {
723 byteswritten = write(target_fd, buffer, bytesread);
725 if (byteswritten < 0) {
726 result = false;
727 size = 0;
728 break;
731 bytesread -= byteswritten;
732 draw_slider();
736 close(target_fd);
738 /* Copy failed. Cleanup. */
739 if (!result) {
740 remove(target);
744 close(src_fd);
746 } else {
747 result = rename(src, target) == 0;
748 #ifdef HAVE_MULTIVOLUME
749 if (!result) {
750 if (errno == EXDEV) {
751 /* Failed because cross volume rename doesn't work. Copy
752 instead */
753 result = clipboard_pastefile(src, target, true);
755 if (result) {
756 result = remove(src) == 0;
760 #endif
763 return result;
766 /* Paste a directory to a new location. Designed to be called by
767 clipboard_paste */
768 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
769 int targetlen, bool copy)
771 DIR *srcdir;
772 int srcdirlen = strlen(src);
773 int targetdirlen = strlen(target);
774 bool result = true;
776 if (!file_exists(target)) {
777 if (!copy) {
778 /* Just move the directory */
779 result = rename(src, target) == 0;
781 #ifdef HAVE_MULTIVOLUME
782 if (!result && errno == EXDEV) {
783 /* Try a copy as we're going across devices */
784 result = clipboard_pastedirectory(src, srclen, target,
785 targetlen, true);
787 /* If it worked, remove the source directory */
788 if (result) {
789 remove_dir(src, srclen);
792 #endif
793 return result;
794 } else {
795 /* Make a directory to copy things to */
796 result = mkdir(target) == 0;
800 /* Check if something went wrong already */
801 if (!result) {
802 return result;
805 srcdir = opendir(src);
806 if (!srcdir) {
807 return false;
810 /* This loop will exit as soon as there's a problem */
811 while(result)
813 struct dirent* entry;
814 /* walk through the directory content */
815 entry = readdir(srcdir);
816 if (!entry)
817 break;
819 /* append name to current directory */
820 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
821 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
822 entry->d_name);
824 DEBUGF("Copy %s to %s\n", src, target);
826 if (entry->attribute & ATTR_DIRECTORY)
827 { /* copy/move a subdirectory */
828 if (!strcmp((char *)entry->d_name, ".") ||
829 !strcmp((char *)entry->d_name, ".."))
830 continue; /* skip these */
832 result = clipboard_pastedirectory(src, srclen, target, targetlen,
833 copy); /* recursion */
835 else
836 { /* copy/move a file */
837 result = clipboard_pastefile(src, target, copy);
841 closedir(srcdir);
843 if (result) {
844 src[srcdirlen] = '\0'; /* terminate to original length */
845 target[targetdirlen] = '\0'; /* terminate to original length */
848 return result;
851 /* Paste the clipboard to the current directory */
852 static bool clipboard_paste(void)
854 char target[MAX_PATH];
855 char *cwd, *nameptr;
856 bool success;
858 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
859 static const struct text_message message={lines, 1};
861 /* Get the name of the current directory */
862 cwd = getcwd(NULL, 0);
864 /* Figure out the name of the selection */
865 nameptr = strrchr(clipboard_selection, '/');
867 /* Final target is current directory plus name of selection */
868 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
870 /* If the target existed but they choose not to overwite, exit */
871 if (file_exists(target) &&
872 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
873 return false;
876 if (clipboard_is_copy) {
877 splash(0, ID2P(LANG_COPYING));
879 else
881 splash(0, ID2P(LANG_MOVING));
884 /* Now figure out what we're doing */
885 cpu_boost(true);
886 if (clipboard_selection_attr & ATTR_DIRECTORY) {
887 /* Recursion. Set up external stack */
888 char srcpath[MAX_PATH];
889 char targetpath[MAX_PATH];
890 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
892 /* Do not allow the user to paste a directory into a dir they are
893 copying */
894 success = 0;
896 else
898 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
899 strlcpy(targetpath, target, sizeof(targetpath));
901 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
902 target, sizeof(targetpath), clipboard_is_copy);
904 if (success && !clipboard_is_copy)
906 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
907 remove_dir(srcpath, sizeof(srcpath));
910 } else {
911 success = clipboard_pastefile(clipboard_selection, target,
912 clipboard_is_copy);
914 cpu_boost(false);
916 /* Did it work? */
917 if (success) {
918 /* Reset everything */
919 clipboard_selection[0] = 0;
920 clipboard_selection_attr = 0;
921 clipboard_is_copy = false;
923 /* Force reload of the current directory */
924 onplay_result = ONPLAY_RELOAD_DIR;
925 } else {
926 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
927 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
928 str(LANG_FAILED));
931 return true;
934 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
935 #ifdef HAVE_TAGCACHE
936 static int set_rating_inline(void)
938 struct mp3entry* id3 = audio_current_track();
939 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
941 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
942 NULL, 1, 0, 10, NULL, NULL);
943 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
945 else
946 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
947 return 0;
949 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
951 (void)this_item;
952 switch (action)
954 case ACTION_REQUEST_MENUITEM:
955 if (!selected_file || !global_settings.runtimedb ||
956 !tagcache_is_usable())
957 return ACTION_EXIT_MENUITEM;
958 break;
960 return action;
962 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
963 set_rating_inline, NULL,
964 ratingitem_callback, Icon_Questionmark);
965 #endif
967 static bool view_cue(void)
969 struct mp3entry* id3 = audio_current_track();
970 if(id3 && id3->cuesheet)
972 browse_cuesheet(id3->cuesheet);
974 return false;
976 static int view_cue_item_callback(int action,
977 const struct menu_item_ex *this_item)
979 (void)this_item;
980 struct mp3entry* id3 = audio_current_track();
981 switch (action)
983 case ACTION_REQUEST_MENUITEM:
984 if (!selected_file
985 || !id3 || !id3->cuesheet)
986 return ACTION_EXIT_MENUITEM;
987 break;
989 return action;
991 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
992 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
994 /* CONTEXT_WPS items */
995 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
996 browse_id3, NULL, NULL, Icon_NOICON);
997 #ifdef HAVE_PITCHSCREEN
998 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
999 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1000 #endif
1002 /* CONTEXT_[TREE|ID3DB] items */
1003 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1004 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1005 rename_file, NULL, clipboard_callback, Icon_NOICON);
1006 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1007 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1008 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1009 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1010 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1011 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1013 delete_file, NULL, clipboard_callback, Icon_NOICON);
1014 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1015 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1016 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1017 properties, NULL, clipboard_callback, Icon_NOICON);
1018 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1019 create_dir, NULL, clipboard_callback, Icon_NOICON);
1020 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1021 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1022 #if LCD_DEPTH > 1
1023 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1024 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1025 #endif
1026 #ifdef HAVE_RECORDING
1027 static bool set_recdir(void)
1029 strlcpy(global_settings.rec_directory,
1030 selected_file, MAX_FILENAME+1);
1031 settings_save();
1032 return false;
1034 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1035 set_recdir, NULL, clipboard_callback, Icon_Recording);
1036 #endif
1037 static bool add_to_faves(void)
1039 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1040 selected_file))
1041 onplay_result = ONPLAY_RELOAD_DIR;
1042 return false;
1044 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1045 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
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 #endif
1060 if (context == CONTEXT_ID3DB)
1061 return ACTION_EXIT_MENUITEM;
1062 if (this_item == &clipboard_paste_item)
1063 { /* visible if there is something to paste */
1064 return (clipboard_selection[0] != 0) ?
1065 action : ACTION_EXIT_MENUITEM;
1067 else if (this_item == &create_dir_item)
1069 /* always visible */
1070 return action;
1072 else if ((this_item == &properties_item) ||
1073 (this_item == &rename_file_item) ||
1074 (this_item == &clipboard_cut_item) ||
1075 (this_item == &clipboard_copy_item) ||
1076 (this_item == &add_to_faves_item)
1079 /* requires an actual file */
1080 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1082 #if LCD_DEPTH > 1
1083 else if (this_item == &set_backdrop_item)
1085 if (selected_file)
1087 char *suffix = strrchr(selected_file, '.');
1088 if (suffix)
1090 if (strcasecmp(suffix, ".bmp") == 0)
1092 return action;
1096 return ACTION_EXIT_MENUITEM;
1098 #endif
1099 else if ((selected_file_attr & ATTR_DIRECTORY))
1101 if ((this_item == &delete_dir_item)
1103 return action;
1104 #ifdef HAVE_RECORDING
1105 else if (this_item == &set_recdir_item)
1106 return action;
1107 #endif
1109 else if (selected_file
1110 #ifdef HAVE_MULTIVOLUME
1111 /* no rename+delete for volumes */
1112 && !(selected_file_attr & ATTR_VOLUME)
1113 #endif
1116 if ((this_item == &delete_file_item) ||
1117 (this_item == &list_viewers_item))
1119 return action;
1122 return ACTION_EXIT_MENUITEM;
1123 break;
1125 return action;
1127 /* used when onplay() is called in the CONTEXT_WPS context */
1130 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1131 onplaymenu_callback, Icon_Audio,
1132 &wps_playlist_menu, &cat_playlist_menu,
1133 &sound_settings, &playback_settings,
1134 #ifdef HAVE_TAGCACHE
1135 &rating_item,
1136 #endif
1137 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1138 &delete_file_item, &view_cue_item,
1139 #ifdef HAVE_PITCHSCREEN
1140 &pitch_screen_item,
1141 #endif
1143 /* used when onplay() is not called in the CONTEXT_WPS context */
1144 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1145 onplaymenu_callback, Icon_file_view_menu,
1146 &tree_playlist_menu, &cat_playlist_menu,
1147 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1148 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1149 #if LCD_DEPTH > 1
1150 &set_backdrop_item,
1151 #endif
1152 &list_viewers_item, &create_dir_item, &properties_item,
1153 #ifdef HAVE_RECORDING
1154 &set_recdir_item,
1155 #endif
1156 &add_to_faves_item,
1158 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1160 (void)this_item;
1161 switch (action)
1163 case ACTION_TREE_STOP:
1164 if (this_item == &wps_onplay_menu)
1166 list_stop_handler();
1167 return ACTION_STD_CANCEL;
1169 break;
1170 case ACTION_EXIT_MENUITEM:
1171 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1172 break;
1174 return action;
1176 int onplay(char* file, int attr, int from)
1178 const struct menu_item_ex *menu;
1179 onplay_result = ONPLAY_OK;
1180 context = from;
1181 selected_file = file;
1182 selected_file_attr = attr;
1183 if (context == CONTEXT_WPS)
1184 menu = &wps_onplay_menu;
1185 else
1186 menu = &tree_onplay_menu;
1187 switch (do_menu(menu, NULL, NULL, false))
1189 case GO_TO_WPS:
1190 return ONPLAY_START_PLAY;
1191 case GO_TO_ROOT:
1192 case GO_TO_MAINMENU:
1193 return ONPLAY_MAINMENU;
1194 default:
1195 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;