clean up some debugging output.
[kugel-rb.git] / apps / onplay.c
blob2a70415b51c54d6d4390a32042bc665881997a0d
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 "id3.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"
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 gui_syncsplash(0, ID2P(LANG_WAIT));
171 if (new_playlist)
172 playlist_create(NULL, NULL);
174 /* always set seed before inserting shuffled */
175 if (position == PLAYLIST_INSERT_SHUFFLED)
176 srand(current_tick);
178 #ifdef HAVE_TAGCACHE
179 if (context == CONTEXT_ID3DB)
181 tagtree_insert_selection_playlist(position, queue);
183 else
184 #endif
186 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
187 playlist_insert_track(NULL, selected_file, position, queue, true);
188 else if (selected_file_attr & ATTR_DIRECTORY)
190 bool recurse = false;
192 if (global_settings.recursive_dir_insert != RECURSE_ASK)
193 recurse = (bool)global_settings.recursive_dir_insert;
194 else
196 /* Ask if user wants to recurse directory */
197 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
200 playlist_insert_directory(NULL, selected_file, position, queue,
201 recurse);
203 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
204 playlist_insert_playlist(NULL, selected_file, position, queue);
207 if (new_playlist && (playlist_amount() > 0))
209 /* nothing is currently playing so begin playing what we just
210 inserted */
211 if (global_settings.playlist_shuffle)
212 playlist_shuffle(current_tick, -1);
213 playlist_start(0,0);
214 gui_syncstatusbar_draw(&statusbars, false);
215 onplay_result = ONPLAY_START_PLAY;
218 return false;
221 static bool view_playlist(void)
223 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
224 bool result;
226 result = playlist_viewer_ex(selected_file);
228 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
229 onplay_result == ONPLAY_OK)
230 /* playlist was started from viewer */
231 onplay_result = ONPLAY_START_PLAY;
233 return result;
236 static bool cat_add_to_a_playlist(void)
238 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
239 false, NULL);
242 static bool cat_add_to_a_new_playlist(void)
244 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
245 true, NULL);
249 static int cat_playlist_callback(int action,
250 const struct menu_item_ex *this_item);
251 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
252 catalog_view_playlists, 0, cat_playlist_callback,
253 Icon_Playlist);
254 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
255 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
256 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
257 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
258 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
259 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
260 &cat_add_to_new);
262 static int cat_playlist_callback(int action,
263 const struct menu_item_ex *this_item)
265 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
266 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
267 ((selected_file_attr & ATTR_DIRECTORY) == 0))
269 return ACTION_EXIT_MENUITEM;
272 switch (action)
274 case ACTION_REQUEST_MENUITEM:
275 if (this_item == &cat_view_lists)
277 if (context == CONTEXT_WPS)
278 return action;
280 else if (selected_file && /* set before calling this menu,
281 so safe */
282 ((audio_status() & AUDIO_STATUS_PLAY &&
283 context == CONTEXT_WPS) ||
284 context == CONTEXT_TREE))
286 return action;
288 else
289 return ACTION_EXIT_MENUITEM;
290 break;
292 return action;
296 /* CONTEXT_WPS playlist options */
297 MENUITEM_FUNCTION(playlist_viewer_item, 0,
298 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
299 NULL, NULL, Icon_Playlist);
300 MENUITEM_FUNCTION(search_playlist_item, 0,
301 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
302 NULL, NULL, Icon_Playlist);
303 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
304 save_playlist, NULL, NULL, Icon_Playlist);
305 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
306 shuffle_playlist, NULL, NULL, Icon_Playlist);
307 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
308 NULL, Icon_Playlist,
309 &playlist_viewer_item, &search_playlist_item,
310 &playlist_save_item, &reshuffle_item
313 /* CONTEXT_[TREE|ID3DB] playlist options */
314 static int playlist_insert_func(void *param)
316 add_to_playlist((intptr_t)param, false);
317 return 0;
319 static int playlist_queue_func(void *param)
321 add_to_playlist((intptr_t)param, true);
322 return 0;
324 static int treeplaylist_wplayback_callback(int action,
325 const struct menu_item_ex*
326 this_item)
328 (void)this_item;
329 switch (action)
331 case ACTION_REQUEST_MENUITEM:
332 if (audio_status() & AUDIO_STATUS_PLAY)
333 return action;
334 else
335 return ACTION_EXIT_MENUITEM;
336 break;
338 return action;
341 static int treeplaylist_callback(int action,
342 const struct menu_item_ex *this_item);
344 /* insert items */
345 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
346 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
347 treeplaylist_callback, Icon_Playlist);
348 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
349 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
350 treeplaylist_wplayback_callback, Icon_Playlist);
351 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
352 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
353 treeplaylist_wplayback_callback, Icon_Playlist);
354 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
355 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
356 treeplaylist_wplayback_callback, Icon_Playlist);
357 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
358 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
359 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
360 Icon_Playlist);
361 /* queue items */
362 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
363 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
364 treeplaylist_wplayback_callback, Icon_Playlist);
365 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
366 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
367 treeplaylist_wplayback_callback, Icon_Playlist);
368 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
369 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
370 treeplaylist_wplayback_callback, Icon_Playlist);
371 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
372 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
373 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
374 treeplaylist_wplayback_callback, Icon_Playlist);
375 /* replace playlist */
376 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
377 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
378 treeplaylist_wplayback_callback, Icon_Playlist);
379 /* others */
381 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
382 view_playlist, NULL,
383 treeplaylist_callback, Icon_Playlist);
385 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
386 treeplaylist_callback, Icon_Playlist,
388 /* view */
389 &view_playlist_item,
391 /* insert */
392 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
393 &i_last_pl_item, &i_shuf_pl_item,
395 /* queue */
396 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
397 &q_shuf_pl_item,
399 /* replace */
400 &replace_pl_item
402 static int treeplaylist_callback(int action,
403 const struct menu_item_ex *this_item)
405 (void)this_item;
406 switch (action)
408 case ACTION_REQUEST_MENUITEM:
409 if (this_item == &tree_playlist_menu)
411 if (((selected_file_attr & FILE_ATTR_MASK) ==
412 FILE_ATTR_AUDIO) ||
413 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
414 (selected_file_attr & ATTR_DIRECTORY))
416 return action;
418 else
419 return ACTION_EXIT_MENUITEM;
421 else if (this_item == &view_playlist_item)
423 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
424 context == CONTEXT_TREE)
425 return action;
426 else
427 return ACTION_EXIT_MENUITEM;
429 else if (this_item == &i_pl_item_no_play)
431 if (!(audio_status() & AUDIO_STATUS_PLAY))
433 return action;
435 else
436 return ACTION_EXIT_MENUITEM;
438 else if (this_item == &i_shuf_pl_item)
441 if (audio_status() & AUDIO_STATUS_PLAY)
443 return action;
445 else if ((this_item == &i_shuf_pl_item) &&
446 ((selected_file_attr & ATTR_DIRECTORY) ||
447 ((selected_file_attr & FILE_ATTR_MASK) ==
448 FILE_ATTR_M3U)))
450 return action;
452 return ACTION_EXIT_MENUITEM;
454 break;
456 return action;
459 /* helper function to remove a non-empty directory */
460 static int remove_dir(char* dirname, int len)
462 int result = 0;
463 DIR* dir;
464 int dirlen = strlen(dirname);
466 dir = opendir(dirname);
467 if (!dir)
468 return -1; /* open error */
470 while(true)
472 struct dirent* entry;
473 /* walk through the directory content */
474 entry = readdir(dir);
475 if (!entry)
476 break;
478 dirname[dirlen] ='\0';
479 gui_syncsplash(0, dirname);
481 /* append name to current directory */
482 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
483 if (entry->attribute & ATTR_DIRECTORY)
484 { /* remove a subdirectory */
485 if (!strcmp((char *)entry->d_name, ".") ||
486 !strcmp((char *)entry->d_name, ".."))
487 continue; /* skip these */
489 /* inform the user which dir we're deleting */
491 result = remove_dir(dirname, len); /* recursion */
492 if (result)
493 break; /* or better continue, delete what we can? */
495 else
496 { /* remove a file */
497 draw_slider();
498 result = remove(dirname);
500 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
502 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
503 result = -1;
504 break;
507 closedir(dir);
509 if (!result)
510 { /* remove the now empty directory */
511 dirname[dirlen] = '\0'; /* terminate to original length */
513 result = rmdir(dirname);
516 return result;
520 /* share code for file and directory deletion, saves space */
521 static bool delete_handler(bool is_dir)
523 char file_to_delete[MAX_PATH];
524 strcpy(file_to_delete, selected_file);
526 const char *lines[]={
527 ID2P(LANG_REALLY_DELETE),
528 file_to_delete
530 const char *yes_lines[]={
531 ID2P(LANG_DELETED),
532 file_to_delete
535 struct text_message message={lines, 2};
536 struct text_message yes_message={yes_lines, 2};
538 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
539 return false;
541 gui_syncsplash(0, str(LANG_DELETING));
543 int res;
544 if (is_dir)
546 char pathname[MAX_PATH]; /* space to go deep */
547 cpu_boost(true);
548 strncpy(pathname, file_to_delete, sizeof pathname);
549 res = remove_dir(pathname, sizeof(pathname));
550 cpu_boost(false);
552 else
553 res = remove(file_to_delete);
555 if (!res)
556 onplay_result = ONPLAY_RELOAD_DIR;
558 return false;
562 static bool delete_file(void)
564 return delete_handler(false);
567 static bool delete_dir(void)
569 return delete_handler(true);
572 #if LCD_DEPTH > 1
573 static bool set_backdrop(void)
575 /* load the image */
576 if(load_main_backdrop(selected_file)) {
577 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
578 set_file(selected_file, (char *)global_settings.backdrop_file,
579 MAX_FILENAME);
580 show_main_backdrop();
581 return true;
582 } else {
583 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
584 return false;
587 #endif
589 static bool rename_file(void)
591 char newname[MAX_PATH];
592 char* ptr = strrchr(selected_file, '/') + 1;
593 int pathlen = (ptr - selected_file);
594 strncpy(newname, selected_file, sizeof newname);
595 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
596 if (!strlen(newname + pathlen) ||
597 (rename(selected_file, newname) < 0)) {
598 lcd_clear_display();
599 lcd_puts(0,0,str(LANG_RENAME));
600 lcd_puts(0,1,str(LANG_FAILED));
601 lcd_update();
602 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
603 sleep(HZ*2);
605 else
606 onplay_result = ONPLAY_RELOAD_DIR;
609 return false;
612 static bool create_dir(void)
614 char dirname[MAX_PATH];
615 char *cwd;
616 int rc;
617 int pathlen;
619 cwd = getcwd(NULL, 0);
620 memset(dirname, 0, sizeof dirname);
622 snprintf(dirname, sizeof dirname, "%s/",
623 cwd[1] ? cwd : "");
625 pathlen = strlen(dirname);
626 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
627 if (rc < 0)
628 return false;
630 rc = mkdir(dirname);
631 if (rc < 0) {
632 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
633 gui_syncsplash(HZ, (unsigned char *)"%s %s",
634 str(LANG_CREATE_DIR), str(LANG_FAILED));
635 } else {
636 onplay_result = ONPLAY_RELOAD_DIR;
639 return true;
642 static bool properties(void)
644 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
645 selected_file))
646 onplay_result = ONPLAY_RELOAD_DIR;
647 return false;
650 /* Store the current selection in the clipboard */
651 static bool clipboard_clip(bool copy)
653 clipboard_selection[0] = 0;
654 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
655 clipboard_selection_attr = selected_file_attr;
656 clipboard_is_copy = copy;
658 return true;
661 static bool clipboard_cut(void)
663 return clipboard_clip(false);
666 static bool clipboard_copy(void)
668 return clipboard_clip(true);
671 #ifdef HAVE_LCD_BITMAP
672 static void draw_slider(void)
674 int i;
675 FOR_NB_SCREENS(i)
677 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
678 LCD_WIDTH-2, 2*screens[i].char_height-1);
679 screens[i].update();
682 #endif
684 /* Paste a file to a new directory. Will overwrite always. */
685 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
687 int src_fd, target_fd;
688 size_t buffersize;
689 ssize_t size, bytesread, byteswritten;
690 char *buffer;
691 bool result = false;
693 if (copy) {
694 /* See if we can get the plugin buffer for the file copy buffer */
695 buffer = (char *) plugin_get_buffer(&buffersize);
696 if (buffer == NULL || buffersize < 512) {
697 /* Not large enough, try for a disk sector worth of stack
698 instead */
699 buffersize = 512;
700 buffer = (char *) __builtin_alloca(buffersize);
703 if (buffer == NULL) {
704 return false;
707 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
708 size */
710 src_fd = open(src, O_RDONLY);
712 if (src_fd >= 0) {
713 target_fd = creat(target);
715 if (target_fd >= 0) {
716 result = true;
718 size = filesize(src_fd);
720 if (size == -1) {
721 result = false;
724 while(size > 0) {
725 bytesread = read(src_fd, buffer, buffersize);
727 if (bytesread == -1) {
728 result = false;
729 break;
732 size -= bytesread;
734 while(bytesread > 0) {
735 byteswritten = write(target_fd, buffer, bytesread);
737 if (byteswritten == -1) {
738 result = false;
739 size = 0;
740 break;
743 bytesread -= byteswritten;
744 draw_slider();
748 close(target_fd);
750 /* Copy failed. Cleanup. */
751 if (!result) {
752 remove(target);
756 close(src_fd);
758 } else {
759 result = rename(src, target) == 0;
760 #ifdef HAVE_MULTIVOLUME
761 if (!result) {
762 if (errno == EXDEV) {
763 /* Failed because cross volume rename doesn't work. Copy
764 instead */
765 result = clipboard_pastefile(src, target, true);
767 if (result) {
768 result = remove(src) == 0;
772 #endif
775 return result;
778 /* Paste a directory to a new location. Designed to be called by
779 clipboard_paste */
780 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
781 int targetlen, bool copy)
783 DIR *srcdir;
784 int srcdirlen = strlen(src);
785 int targetdirlen = strlen(target);
786 bool result = true;
788 if (!file_exists(target)) {
789 if (!copy) {
790 /* Just move the directory */
791 result = rename(src, target) == 0;
793 #ifdef HAVE_MULTIVOLUME
794 if (!result && errno == EXDEV) {
795 /* Try a copy as we're going across devices */
796 result = clipboard_pastedirectory(src, srclen, target,
797 targetlen, true);
799 /* If it worked, remove the source directory */
800 if (result) {
801 remove_dir(src, srclen);
804 #endif
805 return result;
806 } else {
807 /* Make a directory to copy things to */
808 result = mkdir(target) == 0;
812 /* Check if something went wrong already */
813 if (!result) {
814 return result;
817 srcdir = opendir(src);
818 if (!srcdir) {
819 return false;
822 /* This loop will exit as soon as there's a problem */
823 while(result)
825 struct dirent* entry;
826 /* walk through the directory content */
827 entry = readdir(srcdir);
828 if (!entry)
829 break;
831 /* append name to current directory */
832 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
833 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
834 entry->d_name);
836 DEBUGF("Copy %s to %s\n", src, target);
838 if (entry->attribute & ATTR_DIRECTORY)
839 { /* copy/move a subdirectory */
840 if (!strcmp((char *)entry->d_name, ".") ||
841 !strcmp((char *)entry->d_name, ".."))
842 continue; /* skip these */
844 result = clipboard_pastedirectory(src, srclen, target, targetlen,
845 copy); /* recursion */
847 else
848 { /* copy/move a file */
849 result = clipboard_pastefile(src, target, copy);
853 closedir(srcdir);
855 if (result) {
856 src[srcdirlen] = '\0'; /* terminate to original length */
857 target[targetdirlen] = '\0'; /* terminate to original length */
860 return result;
863 /* Paste the clipboard to the current directory */
864 static bool clipboard_paste(void)
866 char target[MAX_PATH];
867 char *cwd, *nameptr;
868 bool success;
870 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
871 static const struct text_message message={lines, 1};
873 /* Get the name of the current directory */
874 cwd = getcwd(NULL, 0);
876 /* Figure out the name of the selection */
877 nameptr = strrchr(clipboard_selection, '/');
879 /* Final target is current directory plus name of selection */
880 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
882 /* If the target existed but they choose not to overwite, exit */
883 if (file_exists(target) &&
884 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
885 return false;
888 if (clipboard_is_copy) {
889 gui_syncsplash(0, ID2P(LANG_COPYING));
891 else
893 gui_syncsplash(0, ID2P(LANG_MOVING));
896 /* Now figure out what we're doing */
897 cpu_boost(true);
898 if (clipboard_selection_attr & ATTR_DIRECTORY) {
899 /* Recursion. Set up external stack */
900 char srcpath[MAX_PATH];
901 char targetpath[MAX_PATH];
902 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
904 /* Do not allow the user to paste a directory into a dir they are
905 copying */
906 success = 0;
908 else
910 strncpy(srcpath, clipboard_selection, sizeof srcpath);
911 strncpy(targetpath, target, sizeof targetpath);
913 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
914 target, sizeof(targetpath), clipboard_is_copy);
916 } else {
917 success = clipboard_pastefile(clipboard_selection, target,
918 clipboard_is_copy);
920 cpu_boost(false);
922 /* Did it work? */
923 if (success) {
924 /* Reset everything */
925 clipboard_selection[0] = 0;
926 clipboard_selection_attr = 0;
927 clipboard_is_copy = false;
929 /* Force reload of the current directory */
930 onplay_result = ONPLAY_RELOAD_DIR;
931 } else {
932 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
933 gui_syncsplash(HZ, (unsigned char *)"%s %s",
934 str(LANG_PASTE), str(LANG_FAILED));
937 return true;
940 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
941 #ifdef HAVE_TAGCACHE
942 static int set_rating_inline(void)
944 struct mp3entry* id3 = audio_current_track();
945 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
947 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
948 NULL, 1, 0, 10, NULL, NULL);
949 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
951 else
952 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
953 return 0;
955 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
957 (void)this_item;
958 switch (action)
960 case ACTION_REQUEST_MENUITEM:
961 if (!selected_file || !global_settings.runtimedb)
962 return ACTION_EXIT_MENUITEM;
963 break;
965 return action;
967 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
968 set_rating_inline, NULL,
969 ratingitem_callback, Icon_Questionmark);
970 #endif
972 static bool view_cue(void)
974 struct mp3entry* id3 = audio_current_track();
975 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
977 browse_cuesheet(curr_cue);
979 return false;
981 static int view_cue_item_callback(int action,
982 const struct menu_item_ex *this_item)
984 (void)this_item;
985 struct mp3entry* id3 = audio_current_track();
986 switch (action)
988 case ACTION_REQUEST_MENUITEM:
989 if (!selected_file || !cuesheet_is_enabled()
990 || !id3 || !id3->cuesheet_type)
991 return ACTION_EXIT_MENUITEM;
992 break;
994 return action;
996 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
997 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
999 /* CONTEXT_WPS items */
1000 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1001 browse_id3, NULL, NULL, Icon_NOICON);
1002 #ifdef HAVE_PITCHSCREEN
1003 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1004 pitch_screen, NULL, NULL, Icon_Audio);
1005 #endif
1007 /* CONTEXT_[TREE|ID3DB] items */
1008 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1009 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1010 rename_file, NULL, clipboard_callback, Icon_NOICON);
1011 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1012 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1013 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1014 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1015 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1016 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1017 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1018 delete_file, NULL, clipboard_callback, Icon_NOICON);
1019 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1020 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1021 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1022 properties, NULL, clipboard_callback, Icon_NOICON);
1023 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1024 create_dir, NULL, clipboard_callback, Icon_NOICON);
1025 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1026 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1027 #if LCD_DEPTH > 1
1028 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1029 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1030 #endif
1031 #ifdef HAVE_RECORDING
1032 static bool set_recdir(void)
1034 strncpy(global_settings.rec_directory,
1035 selected_file, MAX_FILENAME+1);
1036 settings_save();
1037 return false;
1039 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1040 set_recdir, NULL, clipboard_callback, Icon_Recording);
1041 #endif
1042 static bool add_to_faves(void)
1044 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1045 selected_file))
1046 onplay_result = ONPLAY_RELOAD_DIR;
1047 return false;
1049 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1050 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1053 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1055 switch (action)
1057 case ACTION_REQUEST_MENUITEM:
1058 #ifdef HAVE_MULTIVOLUME
1059 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1060 (this_item == &rename_file_item ||
1061 this_item == &delete_dir_item ||
1062 this_item == &clipboard_cut_item) )
1063 return ACTION_EXIT_MENUITEM;
1064 #endif
1065 if (context == CONTEXT_ID3DB)
1066 return ACTION_EXIT_MENUITEM;
1067 if (this_item == &clipboard_paste_item)
1068 { /* visible if there is something to paste */
1069 return (clipboard_selection[0] != 0) ?
1070 action : ACTION_EXIT_MENUITEM;
1072 else if (this_item == &create_dir_item)
1074 /* always visible */
1075 return action;
1077 else if ((this_item == &properties_item) ||
1078 (this_item == &rename_file_item) ||
1079 (this_item == &clipboard_cut_item) ||
1080 (this_item == &clipboard_copy_item) ||
1081 (this_item == &add_to_faves_item)
1084 /* requires an actual file */
1085 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1087 #if LCD_DEPTH > 1
1088 else if (this_item == &set_backdrop_item)
1090 if (selected_file)
1092 char *suffix = strrchr(selected_file, '.');
1093 if (suffix)
1095 if (strcasecmp(suffix, ".bmp") == 0)
1097 return action;
1101 return ACTION_EXIT_MENUITEM;
1103 #endif
1104 else if ((selected_file_attr & ATTR_DIRECTORY))
1106 if ((this_item == &delete_dir_item)
1108 return action;
1109 #ifdef HAVE_RECORDING
1110 else if (this_item == &set_recdir_item)
1111 return action;
1112 #endif
1114 else if (selected_file
1115 #ifdef HAVE_MULTIVOLUME
1116 /* no rename+delete for volumes */
1117 && !(selected_file_attr & ATTR_VOLUME)
1118 #endif
1121 if ((this_item == &delete_file_item) ||
1122 (this_item == &list_viewers_item))
1124 return action;
1127 return ACTION_EXIT_MENUITEM;
1128 break;
1130 return action;
1132 /* used when onplay() is called in the CONTEXT_WPS context */
1135 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1136 onplaymenu_callback, Icon_Audio,
1137 &wps_playlist_menu, &cat_playlist_menu,
1138 &sound_settings, &playback_settings,
1139 #ifdef HAVE_TAGCACHE
1140 &rating_item,
1141 #endif
1142 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1143 &delete_file_item, &view_cue_item,
1144 #ifdef HAVE_PITCHSCREEN
1145 &pitch_screen_item,
1146 #endif
1148 /* used when onplay() is not called in the CONTEXT_WPS context */
1149 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1150 onplaymenu_callback, Icon_file_view_menu,
1151 &tree_playlist_menu, &cat_playlist_menu,
1152 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1153 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1154 #if LCD_DEPTH > 1
1155 &set_backdrop_item,
1156 #endif
1157 &list_viewers_item, &create_dir_item, &properties_item,
1158 #ifdef HAVE_RECORDING
1159 &set_recdir_item,
1160 #endif
1161 &add_to_faves_item,
1163 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1165 (void)this_item;
1166 switch (action)
1168 case ACTION_TREE_STOP:
1169 if (this_item == &wps_onplay_menu)
1171 list_stop_handler();
1172 return ACTION_STD_CANCEL;
1174 break;
1175 case ACTION_EXIT_MENUITEM:
1176 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1177 break;
1179 return action;
1181 int onplay(char* file, int attr, int from)
1183 const struct menu_item_ex *menu;
1184 onplay_result = ONPLAY_OK;
1185 context = from;
1186 selected_file = file;
1187 selected_file_attr = attr;
1188 if (context == CONTEXT_WPS)
1189 menu = &wps_onplay_menu;
1190 else
1191 menu = &tree_onplay_menu;
1192 switch (do_menu(menu, NULL, NULL, false))
1194 case GO_TO_WPS:
1195 return ONPLAY_START_PLAY;
1196 case GO_TO_ROOT:
1197 case GO_TO_MAINMENU:
1198 return ONPLAY_MAINMENU;
1199 default:
1200 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;