wave/wave64: parse LIST chunk.
[kugel-rb.git] / apps / onplay.c
blob82a8bddae7ae5a2f7fd38e90c32aab6d5f24b0ba
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 "dir.h"
30 #include "file.h"
31 #include "audio.h"
32 #include "menu.h"
33 #include "lang.h"
34 #include "playlist.h"
35 #include "button.h"
36 #include "kernel.h"
37 #include "keyboard.h"
38 #include "mp3data.h"
39 #include "metadata.h"
40 #include "screens.h"
41 #include "tree.h"
42 #include "settings.h"
43 #include "playlist_viewer.h"
44 #include "talk.h"
45 #include "onplay.h"
46 #include "filetypes.h"
47 #include "plugin.h"
48 #include "bookmark.h"
49 #include "action.h"
50 #include "splash.h"
51 #include "yesno.h"
52 #include "menus/exported_menus.h"
53 #ifdef HAVE_LCD_BITMAP
54 #include "icons.h"
55 #endif
56 #include "sound_menu.h"
57 #include "playlist_menu.h"
58 #include "playlist_catalog.h"
59 #ifdef HAVE_TAGCACHE
60 #include "tagtree.h"
61 #endif
62 #include "cuesheet.h"
63 #include "statusbar-skinned.h"
64 #include "pitchscreen.h"
65 #include "viewport.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_exist() == 0)
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 | MENU_FUNC_HOTKEYABLE,
274 ID2P(LANG_INSERT),
275 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
276 NULL, Icon_Playlist);
277 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
278 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
279 treeplaylist_wplayback_callback, Icon_Playlist);
280 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
281 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
282 treeplaylist_wplayback_callback, Icon_Playlist);
283 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
284 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
285 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
286 treeplaylist_callback, Icon_Playlist);
287 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
288 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
289 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
290 treeplaylist_callback, Icon_Playlist);
291 /* queue items */
292 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
293 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
294 treeplaylist_wplayback_callback, Icon_Playlist);
295 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
296 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
297 treeplaylist_wplayback_callback, Icon_Playlist);
298 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
299 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
300 treeplaylist_wplayback_callback, Icon_Playlist);
301 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
302 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
303 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
304 treeplaylist_wplayback_callback, Icon_Playlist);
305 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
306 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
307 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
308 treeplaylist_callback, Icon_Playlist);
309 /* replace playlist */
310 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
311 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
312 treeplaylist_wplayback_callback, Icon_Playlist);
314 /* others */
315 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
316 view_playlist, NULL,
317 treeplaylist_callback, Icon_Playlist);
319 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
320 treeplaylist_callback, Icon_Playlist,
322 /* view */
323 &view_playlist_item,
325 /* insert */
326 &i_pl_item, &i_first_pl_item, &i_last_pl_item,
327 &i_shuf_pl_item, &i_last_shuf_pl_item,
328 /* queue */
330 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
331 &q_shuf_pl_item, &q_last_shuf_pl_item,
333 /* replace */
334 &replace_pl_item
336 static int treeplaylist_callback(int action,
337 const struct menu_item_ex *this_item)
339 switch (action)
341 case ACTION_REQUEST_MENUITEM:
342 if (this_item == &tree_playlist_menu)
344 if (((selected_file_attr & FILE_ATTR_MASK) ==
345 FILE_ATTR_AUDIO) ||
346 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
347 (selected_file_attr & ATTR_DIRECTORY))
349 return action;
352 else if (this_item == &view_playlist_item)
354 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
355 context == CONTEXT_TREE)
357 return action;
360 else if (this_item == &i_shuf_pl_item)
362 if ((audio_status() & AUDIO_STATUS_PLAY) ||
363 (selected_file_attr & ATTR_DIRECTORY) ||
364 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
366 return action;
369 else if (this_item == &i_last_shuf_pl_item ||
370 this_item == &q_last_shuf_pl_item)
372 if ((playlist_amount() > 0) &&
373 (audio_status() & AUDIO_STATUS_PLAY) &&
374 ((selected_file_attr & ATTR_DIRECTORY) ||
375 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
377 return action;
380 return ACTION_EXIT_MENUITEM;
381 break;
383 return action;
387 /* playlist catalog options */
388 static bool cat_add_to_a_playlist(void)
390 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
391 false, NULL);
394 static bool cat_add_to_a_new_playlist(void)
396 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
397 true, NULL);
400 static int cat_playlist_callback(int action,
401 const struct menu_item_ex *this_item);
402 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
403 catalog_view_playlists, 0,
404 cat_playlist_callback, Icon_Playlist);
405 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
406 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
407 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
408 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
409 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
410 cat_playlist_callback, Icon_Playlist,
411 &cat_view_lists, &cat_add_to_list, &cat_add_to_new);
413 static int cat_playlist_callback(int action,
414 const struct menu_item_ex *this_item)
416 if (!selected_file ||
417 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
418 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
419 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
421 return ACTION_EXIT_MENUITEM;
423 #ifdef HAVE_TAGCACHE
424 if (context == CONTEXT_ID3DB &&
425 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO))
427 return ACTION_EXIT_MENUITEM;
429 #endif
431 switch (action)
433 case ACTION_REQUEST_MENUITEM:
434 if (this_item == &cat_view_lists)
436 return action;
438 else if ((audio_status() & AUDIO_STATUS_PLAY) ||
439 context != CONTEXT_WPS)
441 return action;
443 else
444 return ACTION_EXIT_MENUITEM;
445 break;
447 return action;
450 #ifdef HAVE_LCD_BITMAP
451 static void draw_slider(void)
453 int i;
454 FOR_NB_SCREENS(i)
456 struct viewport vp;
457 int slider_height = 2*screens[i].getcharheight();
458 viewport_set_defaults(&vp, i);
459 screens[i].set_viewport(&vp);
460 show_busy_slider(&screens[i], 1, vp.height - slider_height,
461 vp.width-2, slider_height-1);
462 screens[i].update_viewport();
463 screens[i].set_viewport(NULL);
466 #else
467 #define draw_slider()
468 #endif
470 /* helper function to remove a non-empty directory */
471 static int remove_dir(char* dirname, int len)
473 int result = 0;
474 DIR* dir;
475 int dirlen = strlen(dirname);
477 dir = opendir(dirname);
478 if (!dir)
479 return -1; /* open error */
481 while(true)
483 struct dirent* entry;
484 /* walk through the directory content */
485 entry = readdir(dir);
486 if (!entry)
487 break;
489 dirname[dirlen] ='\0';
490 /* inform the user which dir we're deleting */
491 splash(0, dirname);
493 /* append name to current directory */
494 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
495 if (entry->attribute & ATTR_DIRECTORY)
496 { /* remove a subdirectory */
497 if (!strcmp((char *)entry->d_name, ".") ||
498 !strcmp((char *)entry->d_name, ".."))
499 continue; /* skip these */
501 result = remove_dir(dirname, len); /* recursion */
502 if (result)
503 break; /* or better continue, delete what we can? */
505 else
506 { /* remove a file */
507 draw_slider();
508 result = remove(dirname);
510 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
512 splash(HZ, ID2P(LANG_CANCEL));
513 result = -1;
514 break;
517 closedir(dir);
519 if (!result)
520 { /* remove the now empty directory */
521 dirname[dirlen] = '\0'; /* terminate to original length */
523 result = rmdir(dirname);
526 return result;
530 /* share code for file and directory deletion, saves space */
531 static bool delete_file_dir(void)
533 char file_to_delete[MAX_PATH];
534 strcpy(file_to_delete, selected_file);
536 const char *lines[]={
537 ID2P(LANG_REALLY_DELETE),
538 file_to_delete
540 const char *yes_lines[]={
541 ID2P(LANG_DELETING),
542 file_to_delete
545 const struct text_message message={lines, 2};
546 const struct text_message yes_message={yes_lines, 2};
548 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
549 return false;
551 splash(0, str(LANG_DELETING));
553 int res;
554 if (selected_file_attr & ATTR_DIRECTORY) /* true if directory */
556 char pathname[MAX_PATH]; /* space to go deep */
557 cpu_boost(true);
558 strlcpy(pathname, file_to_delete, sizeof(pathname));
559 res = remove_dir(pathname, sizeof(pathname));
560 cpu_boost(false);
562 else
563 res = remove(file_to_delete);
565 if (!res)
566 onplay_result = ONPLAY_RELOAD_DIR;
568 return (res == 0);
571 static bool rename_file(void)
573 char newname[MAX_PATH];
574 char* ptr = strrchr(selected_file, '/') + 1;
575 int pathlen = (ptr - selected_file);
576 strlcpy(newname, selected_file, sizeof(newname));
577 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
578 if (!strlen(newname + pathlen) ||
579 (rename(selected_file, newname) < 0)) {
580 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
581 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
583 else
584 onplay_result = ONPLAY_RELOAD_DIR;
587 return false;
590 static bool create_dir(void)
592 char dirname[MAX_PATH];
593 char *cwd;
594 int rc;
595 int pathlen;
597 cwd = getcwd(NULL, 0);
598 memset(dirname, 0, sizeof dirname);
600 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
602 pathlen = strlen(dirname);
603 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
604 if (rc < 0)
605 return false;
607 rc = mkdir(dirname);
608 if (rc < 0) {
609 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
610 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
611 str(LANG_FAILED));
612 } else {
613 onplay_result = ONPLAY_RELOAD_DIR;
616 return true;
619 /* Store the current selection in the clipboard */
620 static bool clipboard_clip(bool copy)
622 clipboard_selection[0] = 0;
623 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
624 clipboard_selection_attr = selected_file_attr;
625 clipboard_is_copy = copy;
627 return true;
630 static bool clipboard_cut(void)
632 return clipboard_clip(false);
635 static bool clipboard_copy(void)
637 return clipboard_clip(true);
640 /* Paste a file to a new directory. Will overwrite always. */
641 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
643 int src_fd, target_fd;
644 size_t buffersize;
645 ssize_t size, bytesread, byteswritten;
646 char *buffer;
647 bool result = false;
649 if (copy) {
650 /* See if we can get the plugin buffer for the file copy buffer */
651 buffer = (char *) plugin_get_buffer(&buffersize);
652 if (buffer == NULL || buffersize < 512) {
653 /* Not large enough, try for a disk sector worth of stack
654 instead */
655 buffersize = 512;
656 buffer = (char *) __builtin_alloca(buffersize);
659 if (buffer == NULL) {
660 return false;
663 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
664 size */
666 src_fd = open(src, O_RDONLY);
668 if (src_fd >= 0) {
669 target_fd = creat(target, 0666);
671 if (target_fd >= 0) {
672 result = true;
674 size = filesize(src_fd);
676 if (size == -1) {
677 result = false;
680 while(size > 0) {
681 bytesread = read(src_fd, buffer, buffersize);
683 if (bytesread == -1) {
684 result = false;
685 break;
688 size -= bytesread;
690 while(bytesread > 0) {
691 byteswritten = write(target_fd, buffer, bytesread);
693 if (byteswritten < 0) {
694 result = false;
695 size = 0;
696 break;
699 bytesread -= byteswritten;
700 draw_slider();
704 close(target_fd);
706 /* Copy failed. Cleanup. */
707 if (!result) {
708 remove(target);
712 close(src_fd);
714 } else {
715 result = rename(src, target) == 0;
716 #ifdef HAVE_MULTIVOLUME
717 if (!result) {
718 if (errno == EXDEV) {
719 /* Failed because cross volume rename doesn't work. Copy
720 instead */
721 result = clipboard_pastefile(src, target, true);
723 if (result) {
724 result = remove(src) == 0;
728 #endif
731 return result;
734 /* Paste a directory to a new location. Designed to be called by
735 clipboard_paste */
736 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
737 int targetlen, bool copy)
739 DIR *srcdir;
740 int srcdirlen = strlen(src);
741 int targetdirlen = strlen(target);
742 bool result = true;
744 if (!file_exists(target)) {
745 if (!copy) {
746 /* Just move the directory */
747 result = rename(src, target) == 0;
749 #ifdef HAVE_MULTIVOLUME
750 if (!result && errno == EXDEV) {
751 /* Try a copy as we're going across devices */
752 result = clipboard_pastedirectory(src, srclen, target,
753 targetlen, true);
755 /* If it worked, remove the source directory */
756 if (result) {
757 remove_dir(src, srclen);
760 #endif
761 return result;
762 } else {
763 /* Make a directory to copy things to */
764 result = mkdir(target) == 0;
768 /* Check if something went wrong already */
769 if (!result) {
770 return result;
773 srcdir = opendir(src);
774 if (!srcdir) {
775 return false;
778 /* This loop will exit as soon as there's a problem */
779 while(result)
781 struct dirent* entry;
782 /* walk through the directory content */
783 entry = readdir(srcdir);
784 if (!entry)
785 break;
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 (entry->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
935 static bool view_cue(void)
937 struct mp3entry* id3 = audio_current_track();
938 if(id3 && id3->cuesheet)
940 browse_cuesheet(id3->cuesheet);
942 return false;
944 static int view_cue_item_callback(int action,
945 const struct menu_item_ex *this_item)
947 (void)this_item;
948 struct mp3entry* id3 = audio_current_track();
949 switch (action)
951 case ACTION_REQUEST_MENUITEM:
952 if (!selected_file
953 || !id3 || !id3->cuesheet)
954 return ACTION_EXIT_MENUITEM;
955 break;
957 return action;
959 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
960 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
962 /* CONTEXT_WPS items */
963 MENUITEM_FUNCTION(browse_id3_item, MENU_FUNC_HOTKEYABLE,
964 ID2P(LANG_MENU_SHOW_ID3_INFO),
965 browse_id3, NULL, NULL, Icon_NOICON);
966 #ifdef HAVE_PITCHSCREEN
967 MENUITEM_FUNCTION(pitch_screen_item, MENU_FUNC_HOTKEYABLE,
968 ID2P(LANG_PITCH),
969 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
970 #endif
972 /* CONTEXT_[TREE|ID3DB] items */
973 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
974 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
975 rename_file, NULL, clipboard_callback, Icon_NOICON);
976 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
977 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
978 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
979 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
980 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
981 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
982 MENUITEM_FUNCTION(delete_file_item, MENU_FUNC_HOTKEYABLE, ID2P(LANG_DELETE),
983 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
984 MENUITEM_FUNCTION(delete_dir_item, MENU_FUNC_HOTKEYABLE, ID2P(LANG_DELETE_DIR),
985 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
986 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
987 create_dir, NULL, clipboard_callback, Icon_NOICON);
989 /* other items */
990 static bool list_viewers(void)
992 int ret = filetype_list_viewers(selected_file);
993 if (ret == PLUGIN_USB_CONNECTED)
994 onplay_result = ONPLAY_RELOAD_DIR;
995 return false;
998 static bool onplay_load_plugin(void *param)
1000 int ret = filetype_load_plugin((const char*)param, selected_file);
1001 if (ret == PLUGIN_USB_CONNECTED)
1002 onplay_result = ONPLAY_RELOAD_DIR;
1003 return false;
1006 MENUITEM_FUNCTION(list_viewers_item, MENU_FUNC_HOTKEYABLE,
1007 ID2P(LANG_ONPLAY_OPEN_WITH),
1008 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1009 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1010 onplay_load_plugin, (void *)"properties",
1011 clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(add_to_faves_item, MENU_FUNC_USEPARAM, ID2P(LANG_ADD_TO_FAVES),
1013 onplay_load_plugin, (void *)"shortcuts_append",
1014 clipboard_callback, Icon_NOICON);
1016 #if LCD_DEPTH > 1
1017 static bool set_backdrop(void)
1019 /* load the image */
1020 if(sb_set_backdrop(SCREEN_MAIN, selected_file)) {
1021 splash(HZ, str(LANG_BACKDROP_LOADED));
1022 set_file(selected_file, (char *)global_settings.backdrop_file,
1023 MAX_FILENAME);
1024 return true;
1025 } else {
1026 splash(HZ, str(LANG_BACKDROP_FAILED));
1027 return false;
1029 return true;
1031 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1032 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1033 #endif
1034 #ifdef HAVE_RECORDING
1035 static bool set_recdir(void)
1037 strlcpy(global_settings.rec_directory, 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
1045 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1047 switch (action)
1049 case ACTION_REQUEST_MENUITEM:
1050 #ifdef HAVE_MULTIVOLUME
1051 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1052 (this_item == &rename_file_item ||
1053 this_item == &delete_dir_item ||
1054 this_item == &clipboard_cut_item) )
1055 return ACTION_EXIT_MENUITEM;
1056 /* no rename+delete for volumes */
1057 if ((selected_file_attr & ATTR_VOLUME) &&
1058 (this_item == &delete_file_item ||
1059 this_item == &list_viewers_item))
1060 return ACTION_EXIT_MENUITEM;
1061 #endif
1062 #ifdef HAVE_TAGCACHE
1063 if (context == CONTEXT_ID3DB)
1065 if (((selected_file_attr & FILE_ATTR_MASK) ==
1066 FILE_ATTR_AUDIO) &&
1067 this_item == &properties_item)
1068 return action;
1069 return ACTION_EXIT_MENUITEM;
1071 #endif
1072 if (this_item == &clipboard_paste_item)
1073 { /* visible if there is something to paste */
1074 return (clipboard_selection[0] != 0) ?
1075 action : ACTION_EXIT_MENUITEM;
1077 else if (this_item == &create_dir_item)
1079 /* always visible */
1080 return action;
1082 else if (selected_file)
1084 /* requires an actual file */
1085 if (this_item == &rename_file_item ||
1086 this_item == &clipboard_cut_item ||
1087 this_item == &clipboard_copy_item ||
1088 this_item == &properties_item ||
1089 this_item == &add_to_faves_item)
1091 return action;
1093 else if ((selected_file_attr & ATTR_DIRECTORY))
1095 /* only for directories */
1096 if (this_item == &delete_dir_item
1097 #ifdef HAVE_RECORDING
1098 || this_item == &set_recdir_item
1099 #endif
1101 return action;
1103 else if (this_item == &delete_file_item ||
1104 this_item == &list_viewers_item)
1106 /* only for files */
1107 return action;
1109 #if LCD_DEPTH > 1
1110 else if (this_item == &set_backdrop_item)
1112 char *suffix = strrchr(selected_file, '.');
1113 if (suffix)
1115 if (strcasecmp(suffix, ".bmp") == 0)
1117 return action;
1121 #endif
1123 return ACTION_EXIT_MENUITEM;
1124 break;
1126 return action;
1129 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1130 /* used when onplay() is called in the CONTEXT_WPS context */
1131 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1132 onplaymenu_callback, Icon_Audio,
1133 &wps_playlist_menu, &cat_playlist_menu,
1134 &sound_settings, &playback_settings,
1135 #ifdef HAVE_TAGCACHE
1136 &rating_item,
1137 #endif
1138 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1139 &delete_file_item, &view_cue_item,
1140 #ifdef HAVE_PITCHSCREEN
1141 &pitch_screen_item,
1142 #endif
1144 /* used when onplay() is not called in the CONTEXT_WPS context */
1145 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1146 onplaymenu_callback, Icon_file_view_menu,
1147 &tree_playlist_menu, &cat_playlist_menu,
1148 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1149 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1150 #if LCD_DEPTH > 1
1151 &set_backdrop_item,
1152 #endif
1153 &list_viewers_item, &create_dir_item, &properties_item,
1154 #ifdef HAVE_RECORDING
1155 &set_recdir_item,
1156 #endif
1157 &add_to_faves_item,
1159 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1161 switch (action)
1163 case ACTION_TREE_STOP:
1164 if (this_item == &wps_onplay_menu)
1166 list_stop_handler();
1167 return ACTION_STD_CANCEL;
1169 break;
1170 case ACTION_EXIT_MENUITEM:
1171 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1172 break;
1174 return action;
1177 #ifdef HAVE_HOTKEY
1178 /* direct function calls, no need for menu callbacks */
1179 static bool delete_item(void)
1181 #ifdef HAVE_MULTIVOLUME
1182 /* no delete for volumes */
1183 if ((selected_file_attr & FAT_ATTR_VOLUME) ||
1184 (selected_file_attr & ATTR_VOLUME))
1185 return false;
1186 #endif
1187 return delete_file_dir();
1190 static bool open_with(void)
1192 /* only open files */
1193 if (selected_file_attr & ATTR_DIRECTORY)
1194 return false;
1195 #ifdef HAVE_MULTIVOLUME
1196 if (selected_file_attr & ATTR_VOLUME)
1197 return false;
1198 #endif
1199 return list_viewers();
1202 extern const struct menu_item_ex *selected_menu_item;
1203 extern bool hotkey_settable_menu;
1205 #define HOTKEY_ACTION_MASK 0x0FF /* Mask to apply to get the action (enum) */
1206 #define HOTKEY_CTX_WPS 0x100 /* Mask to apply to check whether it's for WPS */
1207 #define HOTKEY_CTX_TREE 0x200 /* Mask to apply to check whether it's for the tree */
1209 /* Any desired hotkey functions go here... */
1210 enum hotkey_action {
1211 HOTKEY_OFF = 0,
1212 HOTKEY_VIEW_PLAYLIST = 1,
1213 HOTKEY_SHOW_TRACK_INFO,
1214 HOTKEY_PITCHSCREEN,
1215 HOTKEY_OPEN_WITH,
1216 HOTKEY_DELETE,
1217 HOTKEY_INSERT,
1220 struct hotkey_assignment {
1221 int item; /* Bit or'd hotkey_action and HOTKEY_CTX_x */
1222 struct menu_func func; /* Function to run if this entry is selected */
1223 int return_code; /* What to return after the function is run */
1224 const struct menu_item_ex *menu_addr; /* Must have non-dynamic text, */
1225 /* i.e. have the flag MENU_HAS_DESC. E.g. be a MENUITEM_FUNCTION */
1226 /* For all possibilities see menu.h. */
1229 #define HOTKEY_FUNC(func, param) {{(void *)func}, param}
1231 /* ... and here. Order is not important. */
1232 static struct hotkey_assignment hotkey_items[] = {
1233 { HOTKEY_VIEW_PLAYLIST | HOTKEY_CTX_WPS,
1234 HOTKEY_FUNC(NULL, NULL),
1235 ONPLAY_PLAYLIST, &view_cur_playlist },
1236 { HOTKEY_SHOW_TRACK_INFO| HOTKEY_CTX_WPS,
1237 HOTKEY_FUNC(browse_id3, NULL),
1238 ONPLAY_RELOAD_DIR, &browse_id3_item },
1239 #ifdef HAVE_PITCHSCREEN
1240 { HOTKEY_PITCHSCREEN | HOTKEY_CTX_WPS,
1241 HOTKEY_FUNC(gui_syncpitchscreen_run, NULL),
1242 ONPLAY_RELOAD_DIR, &pitch_screen_item },
1243 #endif
1244 { HOTKEY_OPEN_WITH | HOTKEY_CTX_WPS | HOTKEY_CTX_TREE,
1245 HOTKEY_FUNC(open_with, NULL),
1246 ONPLAY_RELOAD_DIR, &list_viewers_item },
1247 { HOTKEY_DELETE | HOTKEY_CTX_WPS | HOTKEY_CTX_TREE,
1248 HOTKEY_FUNC(delete_item, NULL),
1249 ONPLAY_RELOAD_DIR, &delete_file_item },
1250 { HOTKEY_DELETE | HOTKEY_CTX_TREE,
1251 HOTKEY_FUNC(delete_item, NULL),
1252 ONPLAY_RELOAD_DIR, &delete_dir_item },
1253 { HOTKEY_INSERT | HOTKEY_CTX_TREE,
1254 HOTKEY_FUNC(playlist_insert_func, (intptr_t*)PLAYLIST_INSERT),
1255 ONPLAY_START_PLAY, &i_pl_item },
1258 static const int num_hotkey_items = sizeof(hotkey_items) / sizeof(hotkey_items[0]);
1260 /* Return the language ID for the input function */
1261 const char* get_hotkey_desc(int hk_func)
1263 int i;
1264 for (i = 0; i < num_hotkey_items; i++)
1266 if ((hotkey_items[i].item & HOTKEY_ACTION_MASK) == hk_func)
1267 return P2STR(hotkey_items[i].menu_addr->callback_and_desc->desc);
1270 return str(LANG_HOTKEY_NOT_SET);
1273 /* Execute the hotkey function, if listed for this screen */
1274 static int execute_hotkey(bool is_wps)
1276 int i;
1277 struct hotkey_assignment *this_item;
1278 const int context = is_wps ? HOTKEY_CTX_WPS : HOTKEY_CTX_TREE;
1279 const int this_hotkey = (is_wps ? global_settings.hotkey_wps :
1280 global_settings.hotkey_tree);
1282 /* search assignment struct for a match for the hotkey setting */
1283 for (i = 0; i < num_hotkey_items; i++)
1285 this_item = &hotkey_items[i];
1286 if ((this_item->item & context) &&
1287 ((this_item->item & HOTKEY_ACTION_MASK) == this_hotkey))
1289 /* run the associated function (with optional param), if any */
1290 const struct menu_func func = this_item->func;
1291 if (func.function != NULL)
1293 if (func.param != NULL)
1294 (*func.function_w_param)(func.param);
1295 else
1296 (*func.function)();
1298 /* return with the associated code */
1299 return this_item->return_code;
1303 /* no valid hotkey set */
1304 splash(HZ, ID2P(LANG_HOTKEY_NOT_SET));
1305 return ONPLAY_RELOAD_DIR;
1308 /* Set the hotkey to the current context menu function, if listed */
1309 static void set_hotkey(bool is_wps)
1311 int i;
1312 struct hotkey_assignment *this_item;
1313 const int context = is_wps ? HOTKEY_CTX_WPS : HOTKEY_CTX_TREE;
1314 int *hk_func = is_wps ? &global_settings.hotkey_wps :
1315 &global_settings.hotkey_tree;
1316 int this_hk;
1317 char *this_desc;
1318 bool match_found = false;
1320 /* search assignment struct for a function that matches the current menu item */
1321 for (i = 0; i < num_hotkey_items; i++)
1323 this_item = &hotkey_items[i];
1324 if ((this_item->item & context) &&
1325 (this_item->menu_addr == selected_menu_item))
1327 this_hk = this_item->item & HOTKEY_ACTION_MASK;
1328 this_desc = P2STR((selected_menu_item->callback_and_desc)->desc);
1329 match_found = true;
1330 break;
1334 /* ignore the hotkey if no match found or no change to setting */
1335 if (!match_found || (this_hk == *hk_func)) return;
1337 char line1_buf[100],
1338 line2_buf[100];
1339 char *line1 = line1_buf,
1340 *line2 = line2_buf;
1341 char **line1_ptr = &line1,
1342 **line2_ptr = &line2;
1343 const struct text_message message={(const char **)line1_ptr, 1};
1344 const struct text_message yes_message={(const char **)line2_ptr, 1};
1346 snprintf(line1, sizeof(line1_buf), str(LANG_SET_HOTKEY_QUESTION), this_desc);
1347 snprintf(line2, sizeof(line2_buf), str(LANG_HOTKEY_ASSIGNED), this_desc);
1349 /* confirm the hotkey setting change */
1350 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
1352 /* store the hotkey settings */
1353 *hk_func = this_hk;
1354 settings_save();
1357 #endif /* HOTKEY */
1359 int onplay(char* file, int attr, int from, bool hotkey)
1361 const struct menu_item_ex *menu;
1362 onplay_result = ONPLAY_OK;
1363 context = from;
1364 selected_file = file;
1365 selected_file_attr = attr;
1366 int menu_selection;
1367 #ifdef HAVE_HOTKEY
1368 if (hotkey)
1369 return execute_hotkey(context == CONTEXT_WPS);
1370 hotkey_settable_menu = true;
1371 #else
1372 (void)hotkey;
1373 #endif
1374 if (context == CONTEXT_WPS)
1375 menu = &wps_onplay_menu;
1376 else
1377 menu = &tree_onplay_menu;
1378 menu_selection = do_menu(menu, NULL, NULL, false);
1379 #ifdef HAVE_HOTKEY
1380 hotkey_settable_menu = false;
1381 switch (menu_selection)
1383 case MENU_SELECTED_HOTKEY:
1384 set_hotkey(context == CONTEXT_WPS);
1385 return ONPLAY_RELOAD_DIR;
1386 #else
1387 switch (menu_selection)
1389 #endif
1390 case GO_TO_WPS:
1391 return ONPLAY_START_PLAY;
1392 case GO_TO_ROOT:
1393 case GO_TO_MAINMENU:
1394 return ONPLAY_MAINMENU;
1395 case GO_TO_PLAYLIST_VIEWER:
1396 return ONPLAY_PLAYLIST;
1397 default:
1398 return onplay_result;