FS#12076 - DB stats resurrection: If the filename was changed, require
[kugel-rb.git] / apps / onplay.c
bloba2a3671d3e515e6fe85d687cb3330351a278307b
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 "lcd.h"
29 #include "file.h"
30 #include "audio.h"
31 #include "menu.h"
32 #include "lang.h"
33 #include "playlist.h"
34 #include "button.h"
35 #include "kernel.h"
36 #include "keyboard.h"
37 #include "mp3data.h"
38 #include "metadata.h"
39 #include "screens.h"
40 #include "tree.h"
41 #include "settings.h"
42 #include "playlist_viewer.h"
43 #include "talk.h"
44 #include "onplay.h"
45 #include "filetypes.h"
46 #include "plugin.h"
47 #include "bookmark.h"
48 #include "action.h"
49 #include "splash.h"
50 #include "yesno.h"
51 #include "menus/exported_menus.h"
52 #ifdef HAVE_LCD_BITMAP
53 #include "icons.h"
54 #endif
55 #include "sound_menu.h"
56 #include "playlist_menu.h"
57 #include "playlist_catalog.h"
58 #ifdef HAVE_TAGCACHE
59 #include "tagtree.h"
60 #endif
61 #include "cuesheet.h"
62 #include "statusbar-skinned.h"
63 #include "pitchscreen.h"
64 #include "viewport.h"
65 #include "filefuncs.h"
67 static int context;
68 static char* selected_file = NULL;
69 static int selected_file_attr = 0;
70 static int onplay_result = ONPLAY_OK;
71 static char clipboard_selection[MAX_PATH];
72 static int clipboard_selection_attr = 0;
73 static bool clipboard_is_copy = false;
75 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
76 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
77 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
78 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
79 static const struct menu_item_ex name = \
80 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
81 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
82 { (void*)name##_},{.callback_and_desc = & name##__}};
84 /* ----------------------------------------------------------------------- */
85 /* Displays the bookmark menu options for the user to decide. This is an */
86 /* interface function. */
87 /* ----------------------------------------------------------------------- */
89 static int bookmark_menu_callback(int action,
90 const struct menu_item_ex *this_item);
91 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
92 ID2P(LANG_BOOKMARK_MENU_CREATE),
93 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
94 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
95 ID2P(LANG_BOOKMARK_MENU_LIST),
96 bookmark_load_menu, NULL,
97 bookmark_menu_callback, Icon_Bookmark);
98 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU),
99 bookmark_menu_callback, Icon_Bookmark,
100 &bookmark_create_menu_item, &bookmark_load_menu_item);
101 static int bookmark_menu_callback(int action,
102 const struct menu_item_ex *this_item)
104 switch (action)
106 case ACTION_REQUEST_MENUITEM:
107 if (this_item == &bookmark_load_menu_item)
109 if (!bookmark_exists())
110 return ACTION_EXIT_MENUITEM;
112 /* hide the bookmark menu if there is no playback */
113 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
114 return ACTION_EXIT_MENUITEM;
115 break;
116 #ifdef HAVE_LCD_CHARCELLS
117 case ACTION_ENTER_MENUITEM:
118 status_set_param(true);
119 break;
120 #endif
121 case ACTION_EXIT_MENUITEM:
122 #ifdef HAVE_LCD_CHARCELLS
123 status_set_param(false);
124 #endif
125 settings_save();
126 break;
128 return action;
131 /* CONTEXT_WPS playlist options */
132 static bool shuffle_playlist(void)
134 playlist_sort(NULL, true);
135 playlist_randomise(NULL, current_tick, true);
137 return false;
139 static bool save_playlist(void)
141 save_playlist_screen(NULL);
142 return false;
145 extern struct menu_item_ex view_cur_playlist; /* from playlist_menu.c */
146 MENUITEM_FUNCTION(search_playlist_item, 0, ID2P(LANG_SEARCH_IN_PLAYLIST),
147 search_playlist, NULL, NULL, Icon_Playlist);
148 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
149 save_playlist, NULL, NULL, Icon_Playlist);
150 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
151 shuffle_playlist, NULL, NULL, Icon_Playlist);
152 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
153 NULL, Icon_Playlist,
154 &view_cur_playlist, &search_playlist_item,
155 &playlist_save_item, &reshuffle_item
158 /* CONTEXT_[TREE|ID3DB] playlist options */
159 static bool add_to_playlist(int position, bool queue)
161 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
162 const char *lines[] = {
163 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
164 selected_file
166 const struct text_message message={lines, 2};
168 splash(0, ID2P(LANG_WAIT));
170 if (new_playlist)
171 playlist_create(NULL, NULL);
173 /* always set seed before inserting shuffled */
174 if (position == PLAYLIST_INSERT_SHUFFLED ||
175 position == PLAYLIST_INSERT_LAST_SHUFFLED)
177 srand(current_tick);
178 if (position == PLAYLIST_INSERT_LAST_SHUFFLED)
179 playlist_set_last_shuffled_start();
182 #ifdef HAVE_TAGCACHE
183 if (context == CONTEXT_ID3DB)
185 tagtree_insert_selection_playlist(position, queue);
187 else
188 #endif
190 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
191 playlist_insert_track(NULL, selected_file, position, queue, true);
192 else if (selected_file_attr & ATTR_DIRECTORY)
194 bool recurse = false;
196 if (global_settings.recursive_dir_insert != RECURSE_ASK)
197 recurse = (bool)global_settings.recursive_dir_insert;
198 else
200 /* Ask if user wants to recurse directory */
201 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
204 playlist_insert_directory(NULL, selected_file, position, queue,
205 recurse);
207 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
208 playlist_insert_playlist(NULL, selected_file, position, queue);
211 if (new_playlist && (playlist_amount() > 0))
213 /* nothing is currently playing so begin playing what we just
214 inserted */
215 if (global_settings.playlist_shuffle)
216 playlist_shuffle(current_tick, -1);
217 playlist_start(0,0);
218 onplay_result = ONPLAY_START_PLAY;
221 return false;
224 static bool view_playlist(void)
226 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
227 bool result;
229 result = playlist_viewer_ex(selected_file);
231 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
232 onplay_result == ONPLAY_OK)
233 /* playlist was started from viewer */
234 onplay_result = ONPLAY_START_PLAY;
236 return result;
239 static int playlist_insert_func(void *param)
241 if (((intptr_t)param == PLAYLIST_REPLACE) && !warn_on_pl_erase())
242 return 0;
243 add_to_playlist((intptr_t)param, false);
244 return 0;
247 static int playlist_queue_func(void *param)
249 add_to_playlist((intptr_t)param, true);
250 return 0;
253 static int treeplaylist_wplayback_callback(int action,
254 const struct menu_item_ex* this_item)
256 (void)this_item;
257 switch (action)
259 case ACTION_REQUEST_MENUITEM:
260 if (audio_status() & AUDIO_STATUS_PLAY)
261 return action;
262 else
263 return ACTION_EXIT_MENUITEM;
264 break;
266 return action;
269 static int treeplaylist_callback(int action,
270 const struct menu_item_ex *this_item);
272 /* insert items */
273 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
274 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
275 NULL, Icon_Playlist);
276 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
277 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
278 treeplaylist_wplayback_callback, Icon_Playlist);
279 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
280 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
281 treeplaylist_wplayback_callback, Icon_Playlist);
282 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
283 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
284 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
285 treeplaylist_callback, Icon_Playlist);
286 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
287 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
288 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
289 treeplaylist_callback, Icon_Playlist);
290 /* queue items */
291 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
292 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
293 treeplaylist_wplayback_callback, Icon_Playlist);
294 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
295 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
296 treeplaylist_wplayback_callback, Icon_Playlist);
297 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
298 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
299 treeplaylist_wplayback_callback, Icon_Playlist);
300 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
301 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
302 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
303 treeplaylist_wplayback_callback, Icon_Playlist);
304 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
305 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
306 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
307 treeplaylist_callback, Icon_Playlist);
308 /* replace playlist */
309 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
310 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
311 treeplaylist_wplayback_callback, Icon_Playlist);
313 /* others */
314 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
315 view_playlist, NULL,
316 treeplaylist_callback, Icon_Playlist);
318 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
319 treeplaylist_callback, Icon_Playlist,
321 /* view */
322 &view_playlist_item,
324 /* insert */
325 &i_pl_item, &i_first_pl_item, &i_last_pl_item,
326 &i_shuf_pl_item, &i_last_shuf_pl_item,
327 /* queue */
329 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
330 &q_shuf_pl_item, &q_last_shuf_pl_item,
332 /* replace */
333 &replace_pl_item
335 static int treeplaylist_callback(int action,
336 const struct menu_item_ex *this_item)
338 switch (action)
340 case ACTION_REQUEST_MENUITEM:
341 if (this_item == &tree_playlist_menu)
343 if (((selected_file_attr & FILE_ATTR_MASK) ==
344 FILE_ATTR_AUDIO) ||
345 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
346 (selected_file_attr & ATTR_DIRECTORY))
348 return action;
351 else if (this_item == &view_playlist_item)
353 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
354 context == CONTEXT_TREE)
356 return action;
359 else if (this_item == &i_shuf_pl_item)
361 if ((audio_status() & AUDIO_STATUS_PLAY) ||
362 (selected_file_attr & ATTR_DIRECTORY) ||
363 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
365 return action;
368 else if (this_item == &i_last_shuf_pl_item ||
369 this_item == &q_last_shuf_pl_item)
371 if ((playlist_amount() > 0) &&
372 (audio_status() & AUDIO_STATUS_PLAY) &&
373 ((selected_file_attr & ATTR_DIRECTORY) ||
374 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
376 return action;
379 return ACTION_EXIT_MENUITEM;
380 break;
382 return action;
386 /* playlist catalog options */
387 static bool cat_add_to_a_playlist(void)
389 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
390 false, NULL);
393 static bool cat_add_to_a_new_playlist(void)
395 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
396 true, NULL);
399 static int cat_playlist_callback(int action,
400 const struct menu_item_ex *this_item);
401 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
402 catalog_view_playlists, 0,
403 cat_playlist_callback, Icon_Playlist);
404 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
405 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
406 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
407 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
408 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
409 cat_playlist_callback, Icon_Playlist,
410 &cat_view_lists, &cat_add_to_list, &cat_add_to_new);
412 static int cat_playlist_callback(int action,
413 const struct menu_item_ex *this_item)
415 if (!selected_file ||
416 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
417 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
418 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
420 return ACTION_EXIT_MENUITEM;
422 #ifdef HAVE_TAGCACHE
423 if (context == CONTEXT_ID3DB &&
424 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO))
426 return ACTION_EXIT_MENUITEM;
428 #endif
430 switch (action)
432 case ACTION_REQUEST_MENUITEM:
433 if (this_item == &cat_view_lists)
435 return action;
437 else if ((audio_status() & AUDIO_STATUS_PLAY) ||
438 context != CONTEXT_WPS)
440 return action;
442 else
443 return ACTION_EXIT_MENUITEM;
444 break;
446 return action;
449 #ifdef HAVE_LCD_BITMAP
450 static void draw_slider(void)
452 int i;
453 FOR_NB_SCREENS(i)
455 struct viewport vp;
456 int slider_height = 2*screens[i].getcharheight();
457 viewport_set_defaults(&vp, i);
458 screens[i].set_viewport(&vp);
459 show_busy_slider(&screens[i], 1, vp.height - slider_height,
460 vp.width-2, slider_height-1);
461 screens[i].update_viewport();
462 screens[i].set_viewport(NULL);
465 #else
466 #define draw_slider()
467 #endif
469 /* helper function to remove a non-empty directory */
470 static int remove_dir(char* dirname, int len)
472 int result = 0;
473 DIR* dir;
474 int dirlen = strlen(dirname);
476 dir = opendir(dirname);
477 if (!dir)
478 return -1; /* open error */
480 while(true)
482 struct dirent* entry;
483 /* walk through the directory content */
484 entry = readdir(dir);
485 if (!entry)
486 break;
487 struct dirinfo info = dir_get_info(dir, entry);
488 dirname[dirlen] ='\0';
489 /* inform the user which dir we're deleting */
490 splash(0, dirname);
492 /* append name to current directory */
493 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
494 if (info.attribute & ATTR_DIRECTORY)
495 { /* remove a subdirectory */
496 if (!strcmp((char *)entry->d_name, ".") ||
497 !strcmp((char *)entry->d_name, ".."))
498 continue; /* skip these */
500 result = remove_dir(dirname, len); /* recursion */
501 if (result)
502 break; /* or better continue, delete what we can? */
504 else
505 { /* remove a file */
506 draw_slider();
507 result = remove(dirname);
509 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
511 splash(HZ, ID2P(LANG_CANCEL));
512 result = -1;
513 break;
516 closedir(dir);
518 if (!result)
519 { /* remove the now empty directory */
520 dirname[dirlen] = '\0'; /* terminate to original length */
522 result = rmdir(dirname);
525 return result;
529 /* share code for file and directory deletion, saves space */
530 static bool delete_file_dir(void)
532 char file_to_delete[MAX_PATH];
533 strcpy(file_to_delete, selected_file);
535 const char *lines[]={
536 ID2P(LANG_REALLY_DELETE),
537 file_to_delete
539 const char *yes_lines[]={
540 ID2P(LANG_DELETING),
541 file_to_delete
544 const struct text_message message={lines, 2};
545 const struct text_message yes_message={yes_lines, 2};
547 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
548 return false;
550 splash(0, str(LANG_DELETING));
552 int res;
553 if (selected_file_attr & ATTR_DIRECTORY) /* true if directory */
555 char pathname[MAX_PATH]; /* space to go deep */
556 cpu_boost(true);
557 strlcpy(pathname, file_to_delete, sizeof(pathname));
558 res = remove_dir(pathname, sizeof(pathname));
559 cpu_boost(false);
561 else
562 res = remove(file_to_delete);
564 if (!res)
565 onplay_result = ONPLAY_RELOAD_DIR;
567 return (res == 0);
570 static bool rename_file(void)
572 char newname[MAX_PATH];
573 char* ptr = strrchr(selected_file, '/') + 1;
574 int pathlen = (ptr - selected_file);
575 strlcpy(newname, selected_file, sizeof(newname));
576 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
577 if (!strlen(newname + pathlen) ||
578 (rename(selected_file, newname) < 0)) {
579 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
580 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
582 else
583 onplay_result = ONPLAY_RELOAD_DIR;
586 return false;
589 static bool create_dir(void)
591 char dirname[MAX_PATH];
592 char *cwd;
593 int rc;
594 int pathlen;
596 cwd = getcwd(NULL, 0);
597 memset(dirname, 0, sizeof dirname);
599 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
601 pathlen = strlen(dirname);
602 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
603 if (rc < 0)
604 return false;
606 rc = mkdir(dirname);
607 if (rc < 0) {
608 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
609 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
610 str(LANG_FAILED));
611 } else {
612 onplay_result = ONPLAY_RELOAD_DIR;
615 return true;
618 /* Store the current selection in the clipboard */
619 static bool clipboard_clip(bool copy)
621 clipboard_selection[0] = 0;
622 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
623 clipboard_selection_attr = selected_file_attr;
624 clipboard_is_copy = copy;
626 return true;
629 static bool clipboard_cut(void)
631 return clipboard_clip(false);
634 static bool clipboard_copy(void)
636 return clipboard_clip(true);
639 /* Paste a file to a new directory. Will overwrite always. */
640 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
642 int src_fd, target_fd;
643 size_t buffersize;
644 ssize_t size, bytesread, byteswritten;
645 char *buffer;
646 bool result = false;
648 if (copy) {
649 /* See if we can get the plugin buffer for the file copy buffer */
650 buffer = (char *) plugin_get_buffer(&buffersize);
651 if (buffer == NULL || buffersize < 512) {
652 /* Not large enough, try for a disk sector worth of stack
653 instead */
654 buffersize = 512;
655 buffer = (char *) __builtin_alloca(buffersize);
658 if (buffer == NULL) {
659 return false;
662 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
663 size */
665 src_fd = open(src, O_RDONLY);
667 if (src_fd >= 0) {
668 target_fd = creat(target, 0666);
670 if (target_fd >= 0) {
671 result = true;
673 size = filesize(src_fd);
675 if (size == -1) {
676 result = false;
679 while(size > 0) {
680 bytesread = read(src_fd, buffer, buffersize);
682 if (bytesread == -1) {
683 result = false;
684 break;
687 size -= bytesread;
689 while(bytesread > 0) {
690 byteswritten = write(target_fd, buffer, bytesread);
692 if (byteswritten < 0) {
693 result = false;
694 size = 0;
695 break;
698 bytesread -= byteswritten;
699 draw_slider();
703 close(target_fd);
705 /* Copy failed. Cleanup. */
706 if (!result) {
707 remove(target);
711 close(src_fd);
713 } else {
714 result = rename(src, target) == 0;
715 #ifdef HAVE_MULTIVOLUME
716 if (!result) {
717 if (errno == EXDEV) {
718 /* Failed because cross volume rename doesn't work. Copy
719 instead */
720 result = clipboard_pastefile(src, target, true);
722 if (result) {
723 result = remove(src) == 0;
727 #endif
730 return result;
733 /* Paste a directory to a new location. Designed to be called by
734 clipboard_paste */
735 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
736 int targetlen, bool copy)
738 DIR *srcdir;
739 int srcdirlen = strlen(src);
740 int targetdirlen = strlen(target);
741 bool result = true;
743 if (!file_exists(target)) {
744 if (!copy) {
745 /* Just move the directory */
746 result = rename(src, target) == 0;
748 #ifdef HAVE_MULTIVOLUME
749 if (!result && errno == EXDEV) {
750 /* Try a copy as we're going across devices */
751 result = clipboard_pastedirectory(src, srclen, target,
752 targetlen, true);
754 /* If it worked, remove the source directory */
755 if (result) {
756 remove_dir(src, srclen);
759 #endif
760 return result;
761 } else {
762 /* Make a directory to copy things to */
763 result = mkdir(target) == 0;
767 /* Check if something went wrong already */
768 if (!result) {
769 return result;
772 srcdir = opendir(src);
773 if (!srcdir) {
774 return false;
777 /* This loop will exit as soon as there's a problem */
778 while(result)
780 struct dirent* entry;
781 /* walk through the directory content */
782 entry = readdir(srcdir);
783 if (!entry)
784 break;
786 struct dirinfo info = dir_get_info(srcdir, entry);
787 /* append name to current directory */
788 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
789 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
790 entry->d_name);
792 DEBUGF("Copy %s to %s\n", src, target);
794 if (info.attribute & ATTR_DIRECTORY)
795 { /* copy/move a subdirectory */
796 if (!strcmp((char *)entry->d_name, ".") ||
797 !strcmp((char *)entry->d_name, ".."))
798 continue; /* skip these */
800 result = clipboard_pastedirectory(src, srclen, target, targetlen,
801 copy); /* recursion */
803 else
804 { /* copy/move a file */
805 draw_slider();
806 result = clipboard_pastefile(src, target, copy);
810 closedir(srcdir);
812 if (result) {
813 src[srcdirlen] = '\0'; /* terminate to original length */
814 target[targetdirlen] = '\0'; /* terminate to original length */
817 return result;
820 /* Paste the clipboard to the current directory */
821 static bool clipboard_paste(void)
823 char target[MAX_PATH];
824 char *cwd, *nameptr;
825 bool success;
827 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
828 static const struct text_message message={lines, 1};
830 /* Get the name of the current directory */
831 cwd = getcwd(NULL, 0);
833 /* Figure out the name of the selection */
834 nameptr = strrchr(clipboard_selection, '/');
836 /* Final target is current directory plus name of selection */
837 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
839 /* If the target existed but they choose not to overwite, exit */
840 if (file_exists(target) &&
841 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
842 return false;
845 if (clipboard_is_copy) {
846 splash(0, ID2P(LANG_COPYING));
848 else
850 splash(0, ID2P(LANG_MOVING));
853 /* Now figure out what we're doing */
854 cpu_boost(true);
855 if (clipboard_selection_attr & ATTR_DIRECTORY) {
856 /* Recursion. Set up external stack */
857 char srcpath[MAX_PATH];
858 char targetpath[MAX_PATH];
859 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
861 /* Do not allow the user to paste a directory into a dir they are
862 copying */
863 success = 0;
865 else
867 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
868 strlcpy(targetpath, target, sizeof(targetpath));
870 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
871 target, sizeof(targetpath), clipboard_is_copy);
873 if (success && !clipboard_is_copy)
875 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
876 remove_dir(srcpath, sizeof(srcpath));
879 } else {
880 success = clipboard_pastefile(clipboard_selection, target,
881 clipboard_is_copy);
883 cpu_boost(false);
885 /* Did it work? */
886 if (success) {
887 /* Reset everything */
888 clipboard_selection[0] = 0;
889 clipboard_selection_attr = 0;
890 clipboard_is_copy = false;
892 /* Force reload of the current directory */
893 onplay_result = ONPLAY_RELOAD_DIR;
894 } else {
895 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
896 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
897 str(LANG_FAILED));
900 return true;
903 #ifdef HAVE_TAGCACHE
904 static int set_rating_inline(void)
906 struct mp3entry* id3 = audio_current_track();
907 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
909 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
910 NULL, 1, 0, 10, NULL, NULL);
911 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
913 else
914 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
915 return 0;
917 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
919 (void)this_item;
920 switch (action)
922 case ACTION_REQUEST_MENUITEM:
923 if (!selected_file || !global_settings.runtimedb ||
924 !tagcache_is_usable())
925 return ACTION_EXIT_MENUITEM;
926 break;
928 return action;
930 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
931 set_rating_inline, NULL,
932 ratingitem_callback, Icon_Questionmark);
933 #endif
934 #ifdef HAVE_PICTUREFLOW_INTEGRATION
935 MENUITEM_RETURNVALUE(pictureflow_item, ID2P(LANG_ONPLAY_PICTUREFLOW),
936 GO_TO_PICTUREFLOW, NULL, Icon_NOICON);
937 #endif
939 static bool view_cue(void)
941 struct mp3entry* id3 = audio_current_track();
942 if(id3 && id3->cuesheet)
944 browse_cuesheet(id3->cuesheet);
946 return false;
948 static int view_cue_item_callback(int action,
949 const struct menu_item_ex *this_item)
951 (void)this_item;
952 struct mp3entry* id3 = audio_current_track();
953 switch (action)
955 case ACTION_REQUEST_MENUITEM:
956 if (!selected_file
957 || !id3 || !id3->cuesheet)
958 return ACTION_EXIT_MENUITEM;
959 break;
961 return action;
963 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
964 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
966 /* CONTEXT_WPS items */
967 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
968 browse_id3, NULL, NULL, Icon_NOICON);
969 #ifdef HAVE_PITCHSCREEN
970 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
971 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
972 #endif
974 /* CONTEXT_[TREE|ID3DB] items */
975 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
976 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
977 rename_file, NULL, clipboard_callback, Icon_NOICON);
978 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
979 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
980 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
981 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
982 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
983 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
984 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
985 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
986 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
987 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
988 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
989 create_dir, NULL, clipboard_callback, Icon_NOICON);
991 /* other items */
992 static bool list_viewers(void)
994 int ret = filetype_list_viewers(selected_file);
995 if (ret == PLUGIN_USB_CONNECTED)
996 onplay_result = ONPLAY_RELOAD_DIR;
997 return false;
1000 static bool onplay_load_plugin(void *param)
1002 int ret = filetype_load_plugin((const char*)param, selected_file);
1003 if (ret == PLUGIN_USB_CONNECTED)
1004 onplay_result = ONPLAY_RELOAD_DIR;
1005 return false;
1008 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1009 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1010 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1011 onplay_load_plugin, (void *)"properties",
1012 clipboard_callback, Icon_NOICON);
1013 MENUITEM_FUNCTION(add_to_faves_item, MENU_FUNC_USEPARAM, ID2P(LANG_ADD_TO_FAVES),
1014 onplay_load_plugin, (void *)"shortcuts_append",
1015 clipboard_callback, Icon_NOICON);
1017 #if LCD_DEPTH > 1
1018 static bool set_backdrop(void)
1020 strlcpy(global_settings.backdrop_file, selected_file,
1021 sizeof(global_settings.backdrop_file));
1022 settings_save();
1023 skin_backdrop_load_setting();
1024 return true;
1026 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1027 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1028 #endif
1029 #ifdef HAVE_RECORDING
1030 static bool set_recdir(void)
1032 strlcpy(global_settings.rec_directory, selected_file,
1033 sizeof(global_settings.rec_directory));
1034 settings_save();
1035 return false;
1037 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1038 set_recdir, NULL, clipboard_callback, Icon_Recording);
1039 #endif
1040 static bool set_startdir(void)
1042 snprintf(global_settings.start_directory,
1043 sizeof(global_settings.start_directory),
1044 "%s/", selected_file);
1045 settings_save();
1046 return false;
1048 MENUITEM_FUNCTION(set_startdir_item, 0, ID2P(LANG_SET_AS_START_DIR),
1049 set_startdir, NULL, clipboard_callback, Icon_file_view_menu);
1051 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1053 switch (action)
1055 case ACTION_REQUEST_MENUITEM:
1056 #ifdef HAVE_MULTIVOLUME
1057 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1058 (this_item == &rename_file_item ||
1059 this_item == &delete_dir_item ||
1060 this_item == &clipboard_cut_item) )
1061 return ACTION_EXIT_MENUITEM;
1062 /* no rename+delete for volumes */
1063 if ((selected_file_attr & ATTR_VOLUME) &&
1064 (this_item == &delete_file_item ||
1065 this_item == &list_viewers_item))
1066 return ACTION_EXIT_MENUITEM;
1067 #endif
1068 #ifdef HAVE_TAGCACHE
1069 if (context == CONTEXT_ID3DB)
1071 if (((selected_file_attr & FILE_ATTR_MASK) ==
1072 FILE_ATTR_AUDIO) &&
1073 this_item == &properties_item)
1074 return action;
1075 return ACTION_EXIT_MENUITEM;
1077 #endif
1078 if (this_item == &clipboard_paste_item)
1079 { /* visible if there is something to paste */
1080 return (clipboard_selection[0] != 0) ?
1081 action : ACTION_EXIT_MENUITEM;
1083 else if (this_item == &create_dir_item)
1085 /* always visible */
1086 return action;
1088 else if (selected_file)
1090 /* requires an actual file */
1091 if (this_item == &rename_file_item ||
1092 this_item == &clipboard_cut_item ||
1093 this_item == &clipboard_copy_item ||
1094 this_item == &properties_item ||
1095 this_item == &add_to_faves_item)
1097 return action;
1099 else if ((selected_file_attr & ATTR_DIRECTORY))
1101 /* only for directories */
1102 if (this_item == &delete_dir_item ||
1103 this_item == &set_startdir_item
1104 #ifdef HAVE_RECORDING
1105 || this_item == &set_recdir_item
1106 #endif
1108 return action;
1110 else if (this_item == &delete_file_item ||
1111 this_item == &list_viewers_item)
1113 /* only for files */
1114 return action;
1116 #if LCD_DEPTH > 1
1117 else if (this_item == &set_backdrop_item)
1119 char *suffix = strrchr(selected_file, '.');
1120 if (suffix)
1122 if (strcasecmp(suffix, ".bmp") == 0)
1124 return action;
1128 #endif
1130 return ACTION_EXIT_MENUITEM;
1131 break;
1133 return action;
1136 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1137 /* used when onplay() is called in the CONTEXT_WPS context */
1138 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1139 onplaymenu_callback, Icon_Audio,
1140 &wps_playlist_menu, &cat_playlist_menu,
1141 &sound_settings, &playback_settings,
1142 #ifdef HAVE_TAGCACHE
1143 &rating_item,
1144 #endif
1145 &bookmark_menu,
1146 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1147 &pictureflow_item,
1148 #endif
1149 &browse_id3_item, &list_viewers_item,
1150 &delete_file_item, &view_cue_item,
1151 #ifdef HAVE_PITCHSCREEN
1152 &pitch_screen_item,
1153 #endif
1155 /* used when onplay() is not called in the CONTEXT_WPS context */
1156 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1157 onplaymenu_callback, Icon_file_view_menu,
1158 &tree_playlist_menu, &cat_playlist_menu,
1159 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1160 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1161 #if LCD_DEPTH > 1
1162 &set_backdrop_item,
1163 #endif
1164 &list_viewers_item, &create_dir_item, &properties_item,
1165 #ifdef HAVE_RECORDING
1166 &set_recdir_item,
1167 #endif
1168 &set_startdir_item, &add_to_faves_item,
1170 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1172 switch (action)
1174 case ACTION_TREE_STOP:
1175 if (this_item == &wps_onplay_menu)
1177 list_stop_handler();
1178 return ACTION_STD_CANCEL;
1180 break;
1181 case ACTION_EXIT_MENUITEM:
1182 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1183 break;
1185 return action;
1188 #ifdef HAVE_HOTKEY
1189 /* direct function calls, no need for menu callbacks */
1190 static bool delete_item(void)
1192 #ifdef HAVE_MULTIVOLUME
1193 /* no delete for volumes */
1194 if ((selected_file_attr & FAT_ATTR_VOLUME) ||
1195 (selected_file_attr & ATTR_VOLUME))
1196 return false;
1197 #endif
1198 return delete_file_dir();
1201 static bool open_with(void)
1203 /* only open files */
1204 if (selected_file_attr & ATTR_DIRECTORY)
1205 return false;
1206 #ifdef HAVE_MULTIVOLUME
1207 if (selected_file_attr & ATTR_VOLUME)
1208 return false;
1209 #endif
1210 return list_viewers();
1213 static int playlist_insert_shuffled(void)
1215 if ((audio_status() & AUDIO_STATUS_PLAY) ||
1216 (selected_file_attr & ATTR_DIRECTORY) ||
1217 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
1219 playlist_insert_func((intptr_t*)PLAYLIST_INSERT_SHUFFLED);
1220 return ONPLAY_START_PLAY;
1223 return ONPLAY_RELOAD_DIR;
1226 struct hotkey_assignment {
1227 int action; /* hotkey_action */
1228 int lang_id; /* Language ID */
1229 struct menu_func func; /* Function to run if this entry is selected */
1230 int return_code; /* What to return after the function is run */
1233 #define HOTKEY_FUNC(func, param) {{(void *)func}, param}
1235 /* Any desired hotkey functions go here, in the enum in onplay.h,
1236 and in the settings menu in settings_list.c. The order here
1237 is not important. */
1238 static struct hotkey_assignment hotkey_items[] = {
1239 { HOTKEY_VIEW_PLAYLIST, LANG_VIEW_DYNAMIC_PLAYLIST,
1240 HOTKEY_FUNC(NULL, NULL),
1241 ONPLAY_PLAYLIST },
1242 { HOTKEY_SHOW_TRACK_INFO, LANG_MENU_SHOW_ID3_INFO,
1243 HOTKEY_FUNC(browse_id3, NULL),
1244 ONPLAY_RELOAD_DIR },
1245 #ifdef HAVE_PITCHSCREEN
1246 { HOTKEY_PITCHSCREEN, LANG_PITCH,
1247 HOTKEY_FUNC(gui_syncpitchscreen_run, NULL),
1248 ONPLAY_RELOAD_DIR },
1249 #endif
1250 { HOTKEY_OPEN_WITH, LANG_ONPLAY_OPEN_WITH,
1251 HOTKEY_FUNC(open_with, NULL),
1252 ONPLAY_RELOAD_DIR },
1253 { HOTKEY_DELETE, LANG_DELETE,
1254 HOTKEY_FUNC(delete_item, NULL),
1255 ONPLAY_RELOAD_DIR },
1256 { HOTKEY_INSERT, LANG_INSERT,
1257 HOTKEY_FUNC(playlist_insert_func, (intptr_t*)PLAYLIST_INSERT),
1258 ONPLAY_RELOAD_DIR },
1259 { HOTKEY_INSERT_SHUFFLED, LANG_INSERT_SHUFFLED,
1260 HOTKEY_FUNC(playlist_insert_shuffled, NULL),
1261 ONPLAY_RELOAD_DIR },
1262 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1263 { HOTKEY_PICTUREFLOW, LANG_ONPLAY_PICTUREFLOW,
1264 HOTKEY_FUNC(NULL, NULL),
1265 ONPLAY_PICTUREFLOW },
1266 #endif
1269 /* Return the language ID for this action */
1270 int get_hotkey_lang_id(int action)
1272 int i = ARRAYLEN(hotkey_items);
1273 while (i--)
1275 if (hotkey_items[i].action == action)
1276 return hotkey_items[i].lang_id;
1279 return LANG_OFF;
1282 /* Execute the hotkey function, if listed */
1283 static int execute_hotkey(bool is_wps)
1285 int i = ARRAYLEN(hotkey_items);
1286 struct hotkey_assignment *this_item;
1287 const int action = (is_wps ? global_settings.hotkey_wps :
1288 global_settings.hotkey_tree);
1290 /* search assignment struct for a match for the hotkey setting */
1291 while (i--)
1293 this_item = &hotkey_items[i];
1294 if (this_item->action == action)
1296 /* run the associated function (with optional param), if any */
1297 const struct menu_func func = this_item->func;
1298 int func_return = ONPLAY_RELOAD_DIR;
1299 if (func.function != NULL)
1301 if (func.param != NULL)
1302 func_return = (*func.function_w_param)(func.param);
1303 else
1304 func_return = (*func.function)();
1306 /* return with the associated code */
1307 const int return_code = this_item->return_code;
1308 /* ONPLAY_OK here means to use the function return code */
1309 if (return_code == ONPLAY_OK)
1310 return func_return;
1311 return return_code;
1315 /* no valid hotkey set, ignore hotkey */
1316 return ONPLAY_RELOAD_DIR;
1318 #endif /* HOTKEY */
1320 int onplay(char* file, int attr, int from, bool hotkey)
1322 const struct menu_item_ex *menu;
1323 onplay_result = ONPLAY_OK;
1324 context = from;
1325 selected_file = file;
1326 selected_file_attr = attr;
1327 int menu_selection;
1328 #ifdef HAVE_HOTKEY
1329 if (hotkey)
1330 return execute_hotkey(context == CONTEXT_WPS);
1331 #else
1332 (void)hotkey;
1333 #endif
1334 if (context == CONTEXT_WPS)
1335 menu = &wps_onplay_menu;
1336 else
1337 menu = &tree_onplay_menu;
1338 menu_selection = do_menu(menu, NULL, NULL, false);
1340 switch (menu_selection)
1342 case GO_TO_WPS:
1343 return ONPLAY_START_PLAY;
1344 case GO_TO_ROOT:
1345 case GO_TO_MAINMENU:
1346 return ONPLAY_MAINMENU;
1347 case GO_TO_PLAYLIST_VIEWER:
1348 return ONPLAY_PLAYLIST;
1349 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1350 case GO_TO_PICTUREFLOW:
1351 return ONPLAY_PICTUREFLOW;
1352 #endif
1353 default:
1354 return onplay_result;