Split id3.c/h into metadata.c/h and metadata/mp3.c. Updated all references. Moved...
[kugel-rb.git] / apps / onplay.c
blob1735fdbbc2169b98e58eb9da51ba8a3ee3fce0ab
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 "statusbar.h"
46 #include "playlist_viewer.h"
47 #include "talk.h"
48 #include "onplay.h"
49 #include "filetypes.h"
50 #include "plugin.h"
51 #include "bookmark.h"
52 #include "action.h"
53 #include "splash.h"
54 #include "yesno.h"
55 #include "menus/exported_menus.h"
56 #ifdef HAVE_LCD_BITMAP
57 #include "icons.h"
58 #endif
59 #include "sound_menu.h"
60 #include "playlist_menu.h"
61 #include "playlist_catalog.h"
62 #ifdef HAVE_TAGCACHE
63 #include "tagtree.h"
64 #endif
65 #include "cuesheet.h"
66 #include "backdrop.h"
67 #include "pitchscreen.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_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
106 Icon_Bookmark, &bookmark_create_menu_item,
107 &bookmark_load_menu_item);
108 static int bookmark_menu_callback(int action,
109 const struct menu_item_ex *this_item)
111 (void)this_item;
112 switch (action)
114 case ACTION_REQUEST_MENUITEM:
115 if (this_item == &bookmark_load_menu_item)
117 if (bookmark_exist() == 0)
118 return ACTION_EXIT_MENUITEM;
120 /* hide the bookmark menu if there is no playback */
121 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
122 return ACTION_EXIT_MENUITEM;
123 break;
124 #ifdef HAVE_LCD_CHARCELLS
125 case ACTION_ENTER_MENUITEM:
126 status_set_param(true);
127 break;
128 #endif
129 case ACTION_EXIT_MENUITEM:
130 #ifdef HAVE_LCD_CHARCELLS
131 status_set_param(false);
132 #endif
133 settings_save();
134 break;
136 return action;
139 static bool list_viewers(void)
141 int ret = filetype_list_viewers(selected_file);
142 if (ret == PLUGIN_USB_CONNECTED)
143 onplay_result = ONPLAY_RELOAD_DIR;
144 return false;
147 static bool shuffle_playlist(void)
149 playlist_sort(NULL, true);
150 playlist_randomise(NULL, current_tick, true);
152 return false;
155 static bool save_playlist(void)
157 save_playlist_screen(NULL);
158 return false;
161 static bool add_to_playlist(int position, bool queue)
163 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
164 const char *lines[] = {
165 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
166 selected_file
168 const struct text_message message={lines, 2};
170 splash(0, ID2P(LANG_WAIT));
172 if (new_playlist)
173 playlist_create(NULL, NULL);
175 /* always set seed before inserting shuffled */
176 if (position == PLAYLIST_INSERT_SHUFFLED)
177 srand(current_tick);
179 #ifdef HAVE_TAGCACHE
180 if (context == CONTEXT_ID3DB)
182 tagtree_insert_selection_playlist(position, queue);
184 else
185 #endif
187 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
188 playlist_insert_track(NULL, selected_file, position, queue, true);
189 else if (selected_file_attr & ATTR_DIRECTORY)
191 bool recurse = false;
193 if (global_settings.recursive_dir_insert != RECURSE_ASK)
194 recurse = (bool)global_settings.recursive_dir_insert;
195 else
197 /* Ask if user wants to recurse directory */
198 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
201 playlist_insert_directory(NULL, selected_file, position, queue,
202 recurse);
204 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
205 playlist_insert_playlist(NULL, selected_file, position, queue);
208 if (new_playlist && (playlist_amount() > 0))
210 /* nothing is currently playing so begin playing what we just
211 inserted */
212 if (global_settings.playlist_shuffle)
213 playlist_shuffle(current_tick, -1);
214 playlist_start(0,0);
215 gui_syncstatusbar_draw(&statusbars, false);
216 onplay_result = ONPLAY_START_PLAY;
219 return false;
222 static bool view_playlist(void)
224 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
225 bool result;
227 result = playlist_viewer_ex(selected_file);
229 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
230 onplay_result == ONPLAY_OK)
231 /* playlist was started from viewer */
232 onplay_result = ONPLAY_START_PLAY;
234 return result;
237 static bool cat_add_to_a_playlist(void)
239 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
240 false, NULL);
243 static bool cat_add_to_a_new_playlist(void)
245 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
246 true, NULL);
250 static int cat_playlist_callback(int action,
251 const struct menu_item_ex *this_item);
252 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
253 catalog_view_playlists, 0, cat_playlist_callback,
254 Icon_Playlist);
255 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
256 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
257 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
258 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
259 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
260 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
261 &cat_add_to_new);
263 static int cat_playlist_callback(int action,
264 const struct menu_item_ex *this_item)
266 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
267 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
268 ((selected_file_attr & ATTR_DIRECTORY) == 0))
270 return ACTION_EXIT_MENUITEM;
273 switch (action)
275 case ACTION_REQUEST_MENUITEM:
276 if (this_item == &cat_view_lists)
278 if (context == CONTEXT_WPS)
279 return action;
281 else if (selected_file && /* set before calling this menu,
282 so safe */
283 ((audio_status() & AUDIO_STATUS_PLAY &&
284 context == CONTEXT_WPS) ||
285 context == CONTEXT_TREE))
287 return action;
289 else
290 return ACTION_EXIT_MENUITEM;
291 break;
293 return action;
297 /* CONTEXT_WPS playlist options */
298 MENUITEM_FUNCTION(playlist_viewer_item, 0,
299 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
300 NULL, NULL, Icon_Playlist);
301 MENUITEM_FUNCTION(search_playlist_item, 0,
302 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
303 NULL, NULL, Icon_Playlist);
304 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
305 save_playlist, NULL, NULL, Icon_Playlist);
306 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
307 shuffle_playlist, NULL, NULL, Icon_Playlist);
308 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
309 NULL, Icon_Playlist,
310 &playlist_viewer_item, &search_playlist_item,
311 &playlist_save_item, &reshuffle_item
314 /* CONTEXT_[TREE|ID3DB] playlist options */
315 static int playlist_insert_func(void *param)
317 add_to_playlist((intptr_t)param, false);
318 return 0;
320 static int playlist_queue_func(void *param)
322 add_to_playlist((intptr_t)param, true);
323 return 0;
325 static int treeplaylist_wplayback_callback(int action,
326 const struct menu_item_ex*
327 this_item)
329 (void)this_item;
330 switch (action)
332 case ACTION_REQUEST_MENUITEM:
333 if (audio_status() & AUDIO_STATUS_PLAY)
334 return action;
335 else
336 return ACTION_EXIT_MENUITEM;
337 break;
339 return action;
342 static int treeplaylist_callback(int action,
343 const struct menu_item_ex *this_item);
345 /* insert items */
346 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
347 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
348 treeplaylist_callback, Icon_Playlist);
349 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
350 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
351 treeplaylist_wplayback_callback, Icon_Playlist);
352 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
353 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
354 treeplaylist_wplayback_callback, Icon_Playlist);
355 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
356 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
357 treeplaylist_wplayback_callback, Icon_Playlist);
358 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
359 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
360 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
361 Icon_Playlist);
362 /* queue items */
363 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
364 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
365 treeplaylist_wplayback_callback, Icon_Playlist);
366 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
367 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
368 treeplaylist_wplayback_callback, Icon_Playlist);
369 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
370 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
371 treeplaylist_wplayback_callback, Icon_Playlist);
372 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
373 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
374 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
375 treeplaylist_wplayback_callback, Icon_Playlist);
376 /* replace playlist */
377 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
378 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
379 treeplaylist_wplayback_callback, Icon_Playlist);
380 /* others */
382 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
383 view_playlist, NULL,
384 treeplaylist_callback, Icon_Playlist);
386 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
387 treeplaylist_callback, Icon_Playlist,
389 /* view */
390 &view_playlist_item,
392 /* insert */
393 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
394 &i_last_pl_item, &i_shuf_pl_item,
396 /* queue */
397 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
398 &q_shuf_pl_item,
400 /* replace */
401 &replace_pl_item
403 static int treeplaylist_callback(int action,
404 const struct menu_item_ex *this_item)
406 (void)this_item;
407 switch (action)
409 case ACTION_REQUEST_MENUITEM:
410 if (this_item == &tree_playlist_menu)
412 if (((selected_file_attr & FILE_ATTR_MASK) ==
413 FILE_ATTR_AUDIO) ||
414 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
415 (selected_file_attr & ATTR_DIRECTORY))
417 return action;
419 else
420 return ACTION_EXIT_MENUITEM;
422 else if (this_item == &view_playlist_item)
424 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
425 context == CONTEXT_TREE)
426 return action;
427 else
428 return ACTION_EXIT_MENUITEM;
430 else if (this_item == &i_pl_item_no_play)
432 if (!(audio_status() & AUDIO_STATUS_PLAY))
434 return action;
436 else
437 return ACTION_EXIT_MENUITEM;
439 else if (this_item == &i_shuf_pl_item)
442 if (audio_status() & AUDIO_STATUS_PLAY)
444 return action;
446 else if ((this_item == &i_shuf_pl_item) &&
447 ((selected_file_attr & ATTR_DIRECTORY) ||
448 ((selected_file_attr & FILE_ATTR_MASK) ==
449 FILE_ATTR_M3U)))
451 return action;
453 return ACTION_EXIT_MENUITEM;
455 break;
457 return action;
460 /* helper function to remove a non-empty directory */
461 static int remove_dir(char* dirname, int len)
463 int result = 0;
464 DIR* dir;
465 int dirlen = strlen(dirname);
467 dir = opendir(dirname);
468 if (!dir)
469 return -1; /* open error */
471 while(true)
473 struct dirent* entry;
474 /* walk through the directory content */
475 entry = readdir(dir);
476 if (!entry)
477 break;
479 dirname[dirlen] ='\0';
480 splash(0, dirname);
482 /* append name to current directory */
483 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
484 if (entry->attribute & ATTR_DIRECTORY)
485 { /* remove a subdirectory */
486 if (!strcmp((char *)entry->d_name, ".") ||
487 !strcmp((char *)entry->d_name, ".."))
488 continue; /* skip these */
490 /* inform the user which dir we're deleting */
492 result = remove_dir(dirname, len); /* recursion */
493 if (result)
494 break; /* or better continue, delete what we can? */
496 else
497 { /* remove a file */
498 draw_slider();
499 result = remove(dirname);
501 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
503 splash(HZ, ID2P(LANG_CANCEL));
504 result = -1;
505 break;
508 closedir(dir);
510 if (!result)
511 { /* remove the now empty directory */
512 dirname[dirlen] = '\0'; /* terminate to original length */
514 result = rmdir(dirname);
517 return result;
521 /* share code for file and directory deletion, saves space */
522 static bool delete_handler(bool is_dir)
524 char file_to_delete[MAX_PATH];
525 strcpy(file_to_delete, selected_file);
527 const char *lines[]={
528 ID2P(LANG_REALLY_DELETE),
529 file_to_delete
531 const char *yes_lines[]={
532 ID2P(LANG_DELETING),
533 file_to_delete
536 struct text_message message={lines, 2};
537 struct text_message yes_message={yes_lines, 2};
539 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
540 return false;
542 splash(0, str(LANG_DELETING));
544 int res;
545 if (is_dir)
547 char pathname[MAX_PATH]; /* space to go deep */
548 cpu_boost(true);
549 strncpy(pathname, file_to_delete, sizeof pathname);
550 res = remove_dir(pathname, sizeof(pathname));
551 cpu_boost(false);
553 else
554 res = remove(file_to_delete);
556 if (!res)
557 onplay_result = ONPLAY_RELOAD_DIR;
559 return false;
563 static bool delete_file(void)
565 return delete_handler(false);
568 static bool delete_dir(void)
570 return delete_handler(true);
573 #if LCD_DEPTH > 1
574 static bool set_backdrop(void)
576 /* load the image */
577 if(load_main_backdrop(selected_file)) {
578 splash(HZ, str(LANG_BACKDROP_LOADED));
579 set_file(selected_file, (char *)global_settings.backdrop_file,
580 MAX_FILENAME);
581 show_main_backdrop();
582 return true;
583 } else {
584 splash(HZ, str(LANG_BACKDROP_FAILED));
585 return false;
588 #endif
590 static bool rename_file(void)
592 char newname[MAX_PATH];
593 char* ptr = strrchr(selected_file, '/') + 1;
594 int pathlen = (ptr - selected_file);
595 strncpy(newname, selected_file, sizeof newname);
596 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
597 if (!strlen(newname + pathlen) ||
598 (rename(selected_file, newname) < 0)) {
599 lcd_clear_display();
600 lcd_puts(0,0,str(LANG_RENAME));
601 lcd_puts(0,1,str(LANG_FAILED));
602 lcd_update();
603 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
604 sleep(HZ*2);
606 else
607 onplay_result = ONPLAY_RELOAD_DIR;
610 return false;
613 static bool create_dir(void)
615 char dirname[MAX_PATH];
616 char *cwd;
617 int rc;
618 int pathlen;
620 cwd = getcwd(NULL, 0);
621 memset(dirname, 0, sizeof dirname);
623 snprintf(dirname, sizeof dirname, "%s/",
624 cwd[1] ? cwd : "");
626 pathlen = strlen(dirname);
627 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
628 if (rc < 0)
629 return false;
631 rc = mkdir(dirname);
632 if (rc < 0) {
633 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
634 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
635 str(LANG_FAILED));
636 } else {
637 onplay_result = ONPLAY_RELOAD_DIR;
640 return true;
643 static bool properties(void)
645 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
646 selected_file))
647 onplay_result = ONPLAY_RELOAD_DIR;
648 return false;
651 /* Store the current selection in the clipboard */
652 static bool clipboard_clip(bool copy)
654 clipboard_selection[0] = 0;
655 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
656 clipboard_selection_attr = selected_file_attr;
657 clipboard_is_copy = copy;
659 return true;
662 static bool clipboard_cut(void)
664 return clipboard_clip(false);
667 static bool clipboard_copy(void)
669 return clipboard_clip(true);
672 #ifdef HAVE_LCD_BITMAP
673 static void draw_slider(void)
675 int i;
676 FOR_NB_SCREENS(i)
678 show_busy_slider(&screens[i], 1,
679 LCD_HEIGHT-2*screens[i].getcharheight(),
680 LCD_WIDTH-2, 2*screens[i].getcharheight()-1);
681 screens[i].update();
684 #endif
686 /* Paste a file to a new directory. Will overwrite always. */
687 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
689 int src_fd, target_fd;
690 size_t buffersize;
691 ssize_t size, bytesread, byteswritten;
692 char *buffer;
693 bool result = false;
695 if (copy) {
696 /* See if we can get the plugin buffer for the file copy buffer */
697 buffer = (char *) plugin_get_buffer(&buffersize);
698 if (buffer == NULL || buffersize < 512) {
699 /* Not large enough, try for a disk sector worth of stack
700 instead */
701 buffersize = 512;
702 buffer = (char *) __builtin_alloca(buffersize);
705 if (buffer == NULL) {
706 return false;
709 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
710 size */
712 src_fd = open(src, O_RDONLY);
714 if (src_fd >= 0) {
715 target_fd = creat(target);
717 if (target_fd >= 0) {
718 result = true;
720 size = filesize(src_fd);
722 if (size == -1) {
723 result = false;
726 while(size > 0) {
727 bytesread = read(src_fd, buffer, buffersize);
729 if (bytesread == -1) {
730 result = false;
731 break;
734 size -= bytesread;
736 while(bytesread > 0) {
737 byteswritten = write(target_fd, buffer, bytesread);
739 if (byteswritten == -1) {
740 result = false;
741 size = 0;
742 break;
745 bytesread -= byteswritten;
746 draw_slider();
750 close(target_fd);
752 /* Copy failed. Cleanup. */
753 if (!result) {
754 remove(target);
758 close(src_fd);
760 } else {
761 result = rename(src, target) == 0;
762 #ifdef HAVE_MULTIVOLUME
763 if (!result) {
764 if (errno == EXDEV) {
765 /* Failed because cross volume rename doesn't work. Copy
766 instead */
767 result = clipboard_pastefile(src, target, true);
769 if (result) {
770 result = remove(src) == 0;
774 #endif
777 return result;
780 /* Paste a directory to a new location. Designed to be called by
781 clipboard_paste */
782 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
783 int targetlen, bool copy)
785 DIR *srcdir;
786 int srcdirlen = strlen(src);
787 int targetdirlen = strlen(target);
788 bool result = true;
790 if (!file_exists(target)) {
791 if (!copy) {
792 /* Just move the directory */
793 result = rename(src, target) == 0;
795 #ifdef HAVE_MULTIVOLUME
796 if (!result && errno == EXDEV) {
797 /* Try a copy as we're going across devices */
798 result = clipboard_pastedirectory(src, srclen, target,
799 targetlen, true);
801 /* If it worked, remove the source directory */
802 if (result) {
803 remove_dir(src, srclen);
806 #endif
807 return result;
808 } else {
809 /* Make a directory to copy things to */
810 result = mkdir(target) == 0;
814 /* Check if something went wrong already */
815 if (!result) {
816 return result;
819 srcdir = opendir(src);
820 if (!srcdir) {
821 return false;
824 /* This loop will exit as soon as there's a problem */
825 while(result)
827 struct dirent* entry;
828 /* walk through the directory content */
829 entry = readdir(srcdir);
830 if (!entry)
831 break;
833 /* append name to current directory */
834 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
835 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
836 entry->d_name);
838 DEBUGF("Copy %s to %s\n", src, target);
840 if (entry->attribute & ATTR_DIRECTORY)
841 { /* copy/move a subdirectory */
842 if (!strcmp((char *)entry->d_name, ".") ||
843 !strcmp((char *)entry->d_name, ".."))
844 continue; /* skip these */
846 result = clipboard_pastedirectory(src, srclen, target, targetlen,
847 copy); /* recursion */
849 else
850 { /* copy/move a file */
851 result = clipboard_pastefile(src, target, copy);
855 closedir(srcdir);
857 if (result) {
858 src[srcdirlen] = '\0'; /* terminate to original length */
859 target[targetdirlen] = '\0'; /* terminate to original length */
862 return result;
865 /* Paste the clipboard to the current directory */
866 static bool clipboard_paste(void)
868 char target[MAX_PATH];
869 char *cwd, *nameptr;
870 bool success;
872 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
873 static const struct text_message message={lines, 1};
875 /* Get the name of the current directory */
876 cwd = getcwd(NULL, 0);
878 /* Figure out the name of the selection */
879 nameptr = strrchr(clipboard_selection, '/');
881 /* Final target is current directory plus name of selection */
882 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
884 /* If the target existed but they choose not to overwite, exit */
885 if (file_exists(target) &&
886 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
887 return false;
890 if (clipboard_is_copy) {
891 splash(0, ID2P(LANG_COPYING));
893 else
895 splash(0, ID2P(LANG_MOVING));
898 /* Now figure out what we're doing */
899 cpu_boost(true);
900 if (clipboard_selection_attr & ATTR_DIRECTORY) {
901 /* Recursion. Set up external stack */
902 char srcpath[MAX_PATH];
903 char targetpath[MAX_PATH];
904 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
906 /* Do not allow the user to paste a directory into a dir they are
907 copying */
908 success = 0;
910 else
912 strncpy(srcpath, clipboard_selection, sizeof srcpath);
913 strncpy(targetpath, target, sizeof targetpath);
915 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
916 target, sizeof(targetpath), clipboard_is_copy);
918 } else {
919 success = clipboard_pastefile(clipboard_selection, target,
920 clipboard_is_copy);
922 cpu_boost(false);
924 /* Did it work? */
925 if (success) {
926 /* Reset everything */
927 clipboard_selection[0] = 0;
928 clipboard_selection_attr = 0;
929 clipboard_is_copy = false;
931 /* Force reload of the current directory */
932 onplay_result = ONPLAY_RELOAD_DIR;
933 } else {
934 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
935 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
936 str(LANG_FAILED));
939 return true;
942 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
943 #ifdef HAVE_TAGCACHE
944 static int set_rating_inline(void)
946 struct mp3entry* id3 = audio_current_track();
947 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
949 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
950 NULL, 1, 0, 10, NULL, NULL);
951 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
953 else
954 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
955 return 0;
957 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
959 (void)this_item;
960 switch (action)
962 case ACTION_REQUEST_MENUITEM:
963 if (!selected_file || !global_settings.runtimedb)
964 return ACTION_EXIT_MENUITEM;
965 break;
967 return action;
969 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
970 set_rating_inline, NULL,
971 ratingitem_callback, Icon_Questionmark);
972 #endif
974 static bool view_cue(void)
976 struct mp3entry* id3 = audio_current_track();
977 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
979 browse_cuesheet(curr_cue);
981 return false;
983 static int view_cue_item_callback(int action,
984 const struct menu_item_ex *this_item)
986 (void)this_item;
987 struct mp3entry* id3 = audio_current_track();
988 switch (action)
990 case ACTION_REQUEST_MENUITEM:
991 if (!selected_file || !cuesheet_is_enabled()
992 || !id3 || !id3->cuesheet_type)
993 return ACTION_EXIT_MENUITEM;
994 break;
996 return action;
998 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
999 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1001 /* CONTEXT_WPS items */
1002 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1003 browse_id3, NULL, NULL, Icon_NOICON);
1004 #ifdef HAVE_PITCHSCREEN
1005 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1006 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1007 #endif
1009 /* CONTEXT_[TREE|ID3DB] items */
1010 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1011 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1012 rename_file, NULL, clipboard_callback, Icon_NOICON);
1013 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1014 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1015 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1016 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1017 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1018 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1019 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1020 delete_file, NULL, clipboard_callback, Icon_NOICON);
1021 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1022 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1023 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1024 properties, NULL, clipboard_callback, Icon_NOICON);
1025 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1026 create_dir, NULL, clipboard_callback, Icon_NOICON);
1027 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1028 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1029 #if LCD_DEPTH > 1
1030 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1031 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1032 #endif
1033 #ifdef HAVE_RECORDING
1034 static bool set_recdir(void)
1036 strncpy(global_settings.rec_directory,
1037 selected_file, MAX_FILENAME+1);
1038 settings_save();
1039 return false;
1041 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1042 set_recdir, NULL, clipboard_callback, Icon_Recording);
1043 #endif
1044 static bool add_to_faves(void)
1046 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1047 selected_file))
1048 onplay_result = ONPLAY_RELOAD_DIR;
1049 return false;
1051 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1052 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1055 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1057 switch (action)
1059 case ACTION_REQUEST_MENUITEM:
1060 #ifdef HAVE_MULTIVOLUME
1061 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1062 (this_item == &rename_file_item ||
1063 this_item == &delete_dir_item ||
1064 this_item == &clipboard_cut_item) )
1065 return ACTION_EXIT_MENUITEM;
1066 #endif
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 &wps_playlist_menu, &cat_playlist_menu,
1140 &sound_settings, &playback_settings,
1141 #ifdef HAVE_TAGCACHE
1142 &rating_item,
1143 #endif
1144 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1145 &delete_file_item, &view_cue_item,
1146 #ifdef HAVE_PITCHSCREEN
1147 &pitch_screen_item,
1148 #endif
1150 /* used when onplay() is not called in the CONTEXT_WPS context */
1151 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1152 onplaymenu_callback, Icon_file_view_menu,
1153 &tree_playlist_menu, &cat_playlist_menu,
1154 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1155 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1156 #if LCD_DEPTH > 1
1157 &set_backdrop_item,
1158 #endif
1159 &list_viewers_item, &create_dir_item, &properties_item,
1160 #ifdef HAVE_RECORDING
1161 &set_recdir_item,
1162 #endif
1163 &add_to_faves_item,
1165 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1167 (void)this_item;
1168 switch (action)
1170 case ACTION_TREE_STOP:
1171 if (this_item == &wps_onplay_menu)
1173 list_stop_handler();
1174 return ACTION_STD_CANCEL;
1176 break;
1177 case ACTION_EXIT_MENUITEM:
1178 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1179 break;
1181 return action;
1183 int onplay(char* file, int attr, int from)
1185 const struct menu_item_ex *menu;
1186 onplay_result = ONPLAY_OK;
1187 context = from;
1188 selected_file = file;
1189 selected_file_attr = attr;
1190 if (context == CONTEXT_WPS)
1191 menu = &wps_onplay_menu;
1192 else
1193 menu = &tree_onplay_menu;
1194 switch (do_menu(menu, NULL, NULL, false))
1196 case GO_TO_WPS:
1197 return ONPLAY_START_PLAY;
1198 case GO_TO_ROOT:
1199 case GO_TO_MAINMENU:
1200 return ONPLAY_MAINMENU;
1201 default:
1202 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;