Fix errors and warnings.
[kugel-rb.git] / apps / onplay.c
blob03981c6310d68db5d141e5a5c463959a6059f7a6
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_CURRENT_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;
385 void onplay_show_playlist_menu(char* track_name)
387 selected_file = track_name;
388 selected_file_attr = FILE_ATTR_AUDIO;
389 do_menu(&tree_playlist_menu, NULL, NULL, false);
392 /* playlist catalog options */
393 static bool cat_add_to_a_playlist(void)
395 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
396 false, NULL);
399 static bool cat_add_to_a_new_playlist(void)
401 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
402 true, NULL);
404 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
405 static bool set_catalogdir(void)
407 catalog_set_directory(selected_file);
408 settings_save();
409 return false;
411 MENUITEM_FUNCTION(set_catalogdir_item, 0, ID2P(LANG_SET_AS_PLAYLISTCAT_DIR),
412 set_catalogdir, NULL, clipboard_callback, Icon_Playlist);
414 static int cat_playlist_callback(int action,
415 const struct menu_item_ex *this_item);
416 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
417 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
418 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
419 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
420 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
421 cat_playlist_callback, Icon_Playlist,
422 &cat_add_to_list, &cat_add_to_new, &set_catalogdir_item);
424 void onplay_show_playlist_cat_menu(char* track_name)
426 selected_file = track_name;
427 selected_file_attr = FILE_ATTR_AUDIO;
428 do_menu(&cat_playlist_menu, NULL, NULL, false);
431 static int cat_playlist_callback(int action,
432 const struct menu_item_ex *this_item)
434 (void)this_item;
435 if (!selected_file ||
436 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
437 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
438 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
440 return ACTION_EXIT_MENUITEM;
442 #ifdef HAVE_TAGCACHE
443 if (context == CONTEXT_ID3DB &&
444 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO))
446 return ACTION_EXIT_MENUITEM;
448 #endif
450 switch (action)
452 case ACTION_REQUEST_MENUITEM:
453 if ((audio_status() & AUDIO_STATUS_PLAY) || context != CONTEXT_WPS)
455 return action;
457 else
458 return ACTION_EXIT_MENUITEM;
459 break;
461 return action;
464 #ifdef HAVE_LCD_BITMAP
465 static void draw_slider(void)
467 int i;
468 FOR_NB_SCREENS(i)
470 struct viewport vp;
471 int slider_height = 2*screens[i].getcharheight();
472 viewport_set_defaults(&vp, i);
473 screens[i].set_viewport(&vp);
474 show_busy_slider(&screens[i], 1, vp.height - slider_height,
475 vp.width-2, slider_height-1);
476 screens[i].update_viewport();
477 screens[i].set_viewport(NULL);
480 #else
481 #define draw_slider()
482 #endif
484 /* helper function to remove a non-empty directory */
485 static int remove_dir(char* dirname, int len)
487 int result = 0;
488 DIR* dir;
489 int dirlen = strlen(dirname);
491 dir = opendir(dirname);
492 if (!dir)
493 return -1; /* open error */
495 while(true)
497 struct dirent* entry;
498 /* walk through the directory content */
499 entry = readdir(dir);
500 if (!entry)
501 break;
502 struct dirinfo info = dir_get_info(dir, entry);
503 dirname[dirlen] ='\0';
504 /* inform the user which dir we're deleting */
505 splash(0, dirname);
507 /* append name to current directory */
508 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
509 if (info.attribute & ATTR_DIRECTORY)
510 { /* remove a subdirectory */
511 if (!strcmp((char *)entry->d_name, ".") ||
512 !strcmp((char *)entry->d_name, ".."))
513 continue; /* skip these */
515 result = remove_dir(dirname, len); /* recursion */
516 if (result)
517 break; /* or better continue, delete what we can? */
519 else
520 { /* remove a file */
521 draw_slider();
522 result = remove(dirname);
524 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
526 splash(HZ, ID2P(LANG_CANCEL));
527 result = -1;
528 break;
531 closedir(dir);
533 if (!result)
534 { /* remove the now empty directory */
535 dirname[dirlen] = '\0'; /* terminate to original length */
537 result = rmdir(dirname);
540 return result;
544 /* share code for file and directory deletion, saves space */
545 static bool delete_file_dir(void)
547 char file_to_delete[MAX_PATH];
548 strcpy(file_to_delete, selected_file);
550 const char *lines[]={
551 ID2P(LANG_REALLY_DELETE),
552 file_to_delete
554 const char *yes_lines[]={
555 ID2P(LANG_DELETING),
556 file_to_delete
559 const struct text_message message={lines, 2};
560 const struct text_message yes_message={yes_lines, 2};
562 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
563 return false;
565 splash(0, str(LANG_DELETING));
567 int res;
568 if (selected_file_attr & ATTR_DIRECTORY) /* true if directory */
570 char pathname[MAX_PATH]; /* space to go deep */
571 cpu_boost(true);
572 strlcpy(pathname, file_to_delete, sizeof(pathname));
573 res = remove_dir(pathname, sizeof(pathname));
574 cpu_boost(false);
576 else
577 res = remove(file_to_delete);
579 if (!res)
580 onplay_result = ONPLAY_RELOAD_DIR;
582 return (res == 0);
585 static bool rename_file(void)
587 char newname[MAX_PATH];
588 char* ptr = strrchr(selected_file, '/') + 1;
589 int pathlen = (ptr - selected_file);
590 strlcpy(newname, selected_file, sizeof(newname));
591 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
592 if (!strlen(newname + pathlen) ||
593 (rename(selected_file, newname) < 0)) {
594 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
595 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
597 else
598 onplay_result = ONPLAY_RELOAD_DIR;
601 return false;
604 static bool create_dir(void)
606 char dirname[MAX_PATH];
607 char *cwd;
608 int rc;
609 int pathlen;
611 cwd = getcwd(NULL, 0);
612 memset(dirname, 0, sizeof dirname);
614 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
616 pathlen = strlen(dirname);
617 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
618 if (rc < 0)
619 return false;
621 rc = mkdir(dirname);
622 if (rc < 0) {
623 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
624 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
625 str(LANG_FAILED));
626 } else {
627 onplay_result = ONPLAY_RELOAD_DIR;
630 return true;
633 /* Store the current selection in the clipboard */
634 static bool clipboard_clip(bool copy)
636 clipboard_selection[0] = 0;
637 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
638 clipboard_selection_attr = selected_file_attr;
639 clipboard_is_copy = copy;
641 return true;
644 static bool clipboard_cut(void)
646 return clipboard_clip(false);
649 static bool clipboard_copy(void)
651 return clipboard_clip(true);
654 /* Paste a file to a new directory. Will overwrite always. */
655 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
657 int src_fd, target_fd;
658 size_t buffersize;
659 ssize_t size, bytesread, byteswritten;
660 char *buffer;
661 bool result = false;
663 if (copy) {
664 /* See if we can get the plugin buffer for the file copy buffer */
665 buffer = (char *) plugin_get_buffer(&buffersize);
666 if (buffer == NULL || buffersize < 512) {
667 /* Not large enough, try for a disk sector worth of stack
668 instead */
669 buffersize = 512;
670 buffer = (char *) __builtin_alloca(buffersize);
673 if (buffer == NULL) {
674 return false;
677 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
678 size */
680 src_fd = open(src, O_RDONLY);
682 if (src_fd >= 0) {
683 target_fd = creat(target, 0666);
685 if (target_fd >= 0) {
686 result = true;
688 size = filesize(src_fd);
690 if (size == -1) {
691 result = false;
694 while(size > 0) {
695 bytesread = read(src_fd, buffer, buffersize);
697 if (bytesread == -1) {
698 result = false;
699 break;
702 size -= bytesread;
704 while(bytesread > 0) {
705 byteswritten = write(target_fd, buffer, bytesread);
707 if (byteswritten < 0) {
708 result = false;
709 size = 0;
710 break;
713 bytesread -= byteswritten;
714 draw_slider();
718 close(target_fd);
720 /* Copy failed. Cleanup. */
721 if (!result) {
722 remove(target);
726 close(src_fd);
728 } else {
729 result = rename(src, target) == 0;
730 #ifdef HAVE_MULTIVOLUME
731 if (!result) {
732 if (errno == EXDEV) {
733 /* Failed because cross volume rename doesn't work. Copy
734 instead */
735 result = clipboard_pastefile(src, target, true);
737 if (result) {
738 result = remove(src) == 0;
742 #endif
745 return result;
748 /* Paste a directory to a new location. Designed to be called by
749 clipboard_paste */
750 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
751 int targetlen, bool copy)
753 DIR *srcdir;
754 int srcdirlen = strlen(src);
755 int targetdirlen = strlen(target);
756 bool result = true;
758 if (!file_exists(target)) {
759 if (!copy) {
760 /* Just move the directory */
761 result = rename(src, target) == 0;
763 #ifdef HAVE_MULTIVOLUME
764 if (!result && errno == EXDEV) {
765 /* Try a copy as we're going across devices */
766 result = clipboard_pastedirectory(src, srclen, target,
767 targetlen, true);
769 /* If it worked, remove the source directory */
770 if (result) {
771 remove_dir(src, srclen);
774 #endif
775 return result;
776 } else {
777 /* Make a directory to copy things to */
778 result = mkdir(target) == 0;
782 /* Check if something went wrong already */
783 if (!result) {
784 return result;
787 srcdir = opendir(src);
788 if (!srcdir) {
789 return false;
792 /* This loop will exit as soon as there's a problem */
793 while(result)
795 struct dirent* entry;
796 /* walk through the directory content */
797 entry = readdir(srcdir);
798 if (!entry)
799 break;
801 struct dirinfo info = dir_get_info(srcdir, entry);
802 /* append name to current directory */
803 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
804 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
805 entry->d_name);
807 DEBUGF("Copy %s to %s\n", src, target);
809 if (info.attribute & ATTR_DIRECTORY)
810 { /* copy/move a subdirectory */
811 if (!strcmp((char *)entry->d_name, ".") ||
812 !strcmp((char *)entry->d_name, ".."))
813 continue; /* skip these */
815 result = clipboard_pastedirectory(src, srclen, target, targetlen,
816 copy); /* recursion */
818 else
819 { /* copy/move a file */
820 draw_slider();
821 result = clipboard_pastefile(src, target, copy);
825 closedir(srcdir);
827 if (result) {
828 src[srcdirlen] = '\0'; /* terminate to original length */
829 target[targetdirlen] = '\0'; /* terminate to original length */
832 return result;
835 /* Paste the clipboard to the current directory */
836 static bool clipboard_paste(void)
838 char target[MAX_PATH];
839 char *cwd, *nameptr;
840 bool success;
842 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
843 static const struct text_message message={lines, 1};
845 /* Get the name of the current directory */
846 cwd = getcwd(NULL, 0);
848 /* Figure out the name of the selection */
849 nameptr = strrchr(clipboard_selection, '/');
851 /* Final target is current directory plus name of selection */
852 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
854 /* If the target existed but they choose not to overwite, exit */
855 if (file_exists(target) &&
856 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
857 return false;
860 if (clipboard_is_copy) {
861 splash(0, ID2P(LANG_COPYING));
863 else
865 splash(0, ID2P(LANG_MOVING));
868 /* Now figure out what we're doing */
869 cpu_boost(true);
870 if (clipboard_selection_attr & ATTR_DIRECTORY) {
871 /* Recursion. Set up external stack */
872 char srcpath[MAX_PATH];
873 char targetpath[MAX_PATH];
874 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
876 /* Do not allow the user to paste a directory into a dir they are
877 copying */
878 success = 0;
880 else
882 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
883 strlcpy(targetpath, target, sizeof(targetpath));
885 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
886 target, sizeof(targetpath), clipboard_is_copy);
888 if (success && !clipboard_is_copy)
890 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
891 remove_dir(srcpath, sizeof(srcpath));
894 } else {
895 success = clipboard_pastefile(clipboard_selection, target,
896 clipboard_is_copy);
898 cpu_boost(false);
900 /* Did it work? */
901 if (success) {
902 /* Reset everything */
903 clipboard_selection[0] = 0;
904 clipboard_selection_attr = 0;
905 clipboard_is_copy = false;
907 /* Force reload of the current directory */
908 onplay_result = ONPLAY_RELOAD_DIR;
909 } else {
910 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
911 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
912 str(LANG_FAILED));
915 return true;
918 #ifdef HAVE_TAGCACHE
919 static int set_rating_inline(void)
921 struct mp3entry* id3 = audio_current_track();
922 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
924 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
925 NULL, 1, 0, 10, NULL, NULL);
926 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
928 else
929 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
930 return 0;
932 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
934 (void)this_item;
935 switch (action)
937 case ACTION_REQUEST_MENUITEM:
938 if (!selected_file || !global_settings.runtimedb ||
939 !tagcache_is_usable())
940 return ACTION_EXIT_MENUITEM;
941 break;
943 return action;
945 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
946 set_rating_inline, NULL,
947 ratingitem_callback, Icon_Questionmark);
948 #endif
949 #ifdef HAVE_PICTUREFLOW_INTEGRATION
950 MENUITEM_RETURNVALUE(pictureflow_item, ID2P(LANG_ONPLAY_PICTUREFLOW),
951 GO_TO_PICTUREFLOW, NULL, Icon_NOICON);
952 #endif
954 static bool view_cue(void)
956 struct mp3entry* id3 = audio_current_track();
957 if(id3 && id3->cuesheet)
959 browse_cuesheet(id3->cuesheet);
961 return false;
963 static int view_cue_item_callback(int action,
964 const struct menu_item_ex *this_item)
966 (void)this_item;
967 struct mp3entry* id3 = audio_current_track();
968 switch (action)
970 case ACTION_REQUEST_MENUITEM:
971 if (!selected_file
972 || !id3 || !id3->cuesheet)
973 return ACTION_EXIT_MENUITEM;
974 break;
976 return action;
978 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
979 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
981 /* CONTEXT_WPS items */
982 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
983 browse_id3, NULL, NULL, Icon_NOICON);
984 #ifdef HAVE_PITCHSCREEN
985 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
986 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
987 #endif
989 /* CONTEXT_[TREE|ID3DB] items */
990 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
991 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
992 rename_file, NULL, clipboard_callback, Icon_NOICON);
993 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
994 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
995 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
996 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
997 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
998 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
999 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1000 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
1001 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1002 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
1003 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1004 create_dir, NULL, clipboard_callback, Icon_NOICON);
1006 /* other items */
1007 static bool list_viewers(void)
1009 int ret = filetype_list_viewers(selected_file);
1010 if (ret == PLUGIN_USB_CONNECTED)
1011 onplay_result = ONPLAY_RELOAD_DIR;
1012 return false;
1015 static bool onplay_load_plugin(void *param)
1017 int ret = filetype_load_plugin((const char*)param, selected_file);
1018 if (ret == PLUGIN_USB_CONNECTED)
1019 onplay_result = ONPLAY_RELOAD_DIR;
1020 return false;
1023 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1024 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1025 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1026 onplay_load_plugin, (void *)"properties",
1027 clipboard_callback, Icon_NOICON);
1028 MENUITEM_FUNCTION(add_to_faves_item, MENU_FUNC_USEPARAM, ID2P(LANG_ADD_TO_FAVES),
1029 onplay_load_plugin, (void *)"shortcuts_append",
1030 clipboard_callback, Icon_NOICON);
1032 #if LCD_DEPTH > 1
1033 static bool set_backdrop(void)
1035 strlcpy(global_settings.backdrop_file, selected_file,
1036 sizeof(global_settings.backdrop_file));
1037 settings_save();
1038 skin_backdrop_load_setting();
1039 return true;
1041 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1042 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1043 #endif
1044 #ifdef HAVE_RECORDING
1045 static bool set_recdir(void)
1047 strlcpy(global_settings.rec_directory, selected_file,
1048 sizeof(global_settings.rec_directory));
1049 settings_save();
1050 return false;
1052 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1053 set_recdir, NULL, clipboard_callback, Icon_Recording);
1054 #endif
1055 static bool set_startdir(void)
1057 snprintf(global_settings.start_directory,
1058 sizeof(global_settings.start_directory),
1059 "%s/", selected_file);
1060 settings_save();
1061 return false;
1063 MENUITEM_FUNCTION(set_startdir_item, 0, ID2P(LANG_SET_AS_START_DIR),
1064 set_startdir, NULL, clipboard_callback, Icon_file_view_menu);
1066 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1068 switch (action)
1070 case ACTION_REQUEST_MENUITEM:
1071 #ifdef HAVE_MULTIVOLUME
1072 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1073 (this_item == &rename_file_item ||
1074 this_item == &delete_dir_item ||
1075 this_item == &clipboard_cut_item) )
1076 return ACTION_EXIT_MENUITEM;
1077 /* no rename+delete for volumes */
1078 if ((selected_file_attr & ATTR_VOLUME) &&
1079 (this_item == &delete_file_item ||
1080 this_item == &list_viewers_item))
1081 return ACTION_EXIT_MENUITEM;
1082 #endif
1083 #ifdef HAVE_TAGCACHE
1084 if (context == CONTEXT_ID3DB)
1086 if (((selected_file_attr & FILE_ATTR_MASK) ==
1087 FILE_ATTR_AUDIO) &&
1088 this_item == &properties_item)
1089 return action;
1090 return ACTION_EXIT_MENUITEM;
1092 #endif
1093 if (this_item == &clipboard_paste_item)
1094 { /* visible if there is something to paste */
1095 return (clipboard_selection[0] != 0) ?
1096 action : ACTION_EXIT_MENUITEM;
1098 else if (this_item == &create_dir_item)
1100 /* always visible */
1101 return action;
1103 else if (selected_file)
1105 /* requires an actual file */
1106 if (this_item == &rename_file_item ||
1107 this_item == &clipboard_cut_item ||
1108 this_item == &clipboard_copy_item ||
1109 this_item == &properties_item ||
1110 this_item == &add_to_faves_item)
1112 return action;
1114 else if ((selected_file_attr & ATTR_DIRECTORY))
1116 /* only for directories */
1117 if (this_item == &delete_dir_item ||
1118 this_item == &set_startdir_item ||
1119 this_item == &set_catalogdir_item
1120 #ifdef HAVE_RECORDING
1121 || this_item == &set_recdir_item
1122 #endif
1124 return action;
1126 else if (this_item == &delete_file_item ||
1127 this_item == &list_viewers_item)
1129 /* only for files */
1130 return action;
1132 #if LCD_DEPTH > 1
1133 else if (this_item == &set_backdrop_item)
1135 char *suffix = strrchr(selected_file, '.');
1136 if (suffix)
1138 if (strcasecmp(suffix, ".bmp") == 0)
1140 return action;
1144 #endif
1146 return ACTION_EXIT_MENUITEM;
1147 break;
1149 return action;
1152 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1153 /* used when onplay() is called in the CONTEXT_WPS context */
1154 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1155 onplaymenu_callback, Icon_Audio,
1156 &wps_playlist_menu, &cat_playlist_menu,
1157 &sound_settings, &playback_settings,
1158 #ifdef HAVE_TAGCACHE
1159 &rating_item,
1160 #endif
1161 &bookmark_menu,
1162 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1163 &pictureflow_item,
1164 #endif
1165 &browse_id3_item, &list_viewers_item,
1166 &delete_file_item, &view_cue_item,
1167 #ifdef HAVE_PITCHSCREEN
1168 &pitch_screen_item,
1169 #endif
1171 /* used when onplay() is not called in the CONTEXT_WPS context */
1172 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1173 onplaymenu_callback, Icon_file_view_menu,
1174 &tree_playlist_menu, &cat_playlist_menu,
1175 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1176 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1177 #if LCD_DEPTH > 1
1178 &set_backdrop_item,
1179 #endif
1180 &list_viewers_item, &create_dir_item, &properties_item,
1181 #ifdef HAVE_RECORDING
1182 &set_recdir_item,
1183 #endif
1184 &set_startdir_item, &add_to_faves_item,
1186 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1188 switch (action)
1190 case ACTION_TREE_STOP:
1191 if (this_item == &wps_onplay_menu)
1193 list_stop_handler();
1194 return ACTION_STD_CANCEL;
1196 break;
1197 case ACTION_EXIT_MENUITEM:
1198 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1199 break;
1201 return action;
1204 #ifdef HAVE_HOTKEY
1205 /* direct function calls, no need for menu callbacks */
1206 static bool delete_item(void)
1208 #ifdef HAVE_MULTIVOLUME
1209 /* no delete for volumes */
1210 if ((selected_file_attr & FAT_ATTR_VOLUME) ||
1211 (selected_file_attr & ATTR_VOLUME))
1212 return false;
1213 #endif
1214 return delete_file_dir();
1217 static bool open_with(void)
1219 /* only open files */
1220 if (selected_file_attr & ATTR_DIRECTORY)
1221 return false;
1222 #ifdef HAVE_MULTIVOLUME
1223 if (selected_file_attr & ATTR_VOLUME)
1224 return false;
1225 #endif
1226 return list_viewers();
1229 static int playlist_insert_shuffled(void)
1231 if ((audio_status() & AUDIO_STATUS_PLAY) ||
1232 (selected_file_attr & ATTR_DIRECTORY) ||
1233 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
1235 playlist_insert_func((intptr_t*)PLAYLIST_INSERT_SHUFFLED);
1236 return ONPLAY_START_PLAY;
1239 return ONPLAY_RELOAD_DIR;
1242 struct hotkey_assignment {
1243 int action; /* hotkey_action */
1244 int lang_id; /* Language ID */
1245 struct menu_func func; /* Function to run if this entry is selected */
1246 int return_code; /* What to return after the function is run */
1249 #define HOTKEY_FUNC(func, param) {{(void *)func}, param}
1251 /* Any desired hotkey functions go here, in the enum in onplay.h,
1252 and in the settings menu in settings_list.c. The order here
1253 is not important. */
1254 static struct hotkey_assignment hotkey_items[] = {
1255 { HOTKEY_VIEW_PLAYLIST, LANG_VIEW_DYNAMIC_PLAYLIST,
1256 HOTKEY_FUNC(NULL, NULL),
1257 ONPLAY_PLAYLIST },
1258 { HOTKEY_SHOW_TRACK_INFO, LANG_MENU_SHOW_ID3_INFO,
1259 HOTKEY_FUNC(browse_id3, NULL),
1260 ONPLAY_RELOAD_DIR },
1261 #ifdef HAVE_PITCHSCREEN
1262 { HOTKEY_PITCHSCREEN, LANG_PITCH,
1263 HOTKEY_FUNC(gui_syncpitchscreen_run, NULL),
1264 ONPLAY_RELOAD_DIR },
1265 #endif
1266 { HOTKEY_OPEN_WITH, LANG_ONPLAY_OPEN_WITH,
1267 HOTKEY_FUNC(open_with, NULL),
1268 ONPLAY_RELOAD_DIR },
1269 { HOTKEY_DELETE, LANG_DELETE,
1270 HOTKEY_FUNC(delete_item, NULL),
1271 ONPLAY_RELOAD_DIR },
1272 { HOTKEY_INSERT, LANG_INSERT,
1273 HOTKEY_FUNC(playlist_insert_func, (intptr_t*)PLAYLIST_INSERT),
1274 ONPLAY_RELOAD_DIR },
1275 { HOTKEY_INSERT_SHUFFLED, LANG_INSERT_SHUFFLED,
1276 HOTKEY_FUNC(playlist_insert_shuffled, NULL),
1277 ONPLAY_RELOAD_DIR },
1278 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1279 { HOTKEY_PICTUREFLOW, LANG_ONPLAY_PICTUREFLOW,
1280 HOTKEY_FUNC(NULL, NULL),
1281 ONPLAY_PICTUREFLOW },
1282 #endif
1285 /* Return the language ID for this action */
1286 int get_hotkey_lang_id(int action)
1288 int i = ARRAYLEN(hotkey_items);
1289 while (i--)
1291 if (hotkey_items[i].action == action)
1292 return hotkey_items[i].lang_id;
1295 return LANG_OFF;
1298 /* Execute the hotkey function, if listed */
1299 static int execute_hotkey(bool is_wps)
1301 int i = ARRAYLEN(hotkey_items);
1302 struct hotkey_assignment *this_item;
1303 const int action = (is_wps ? global_settings.hotkey_wps :
1304 global_settings.hotkey_tree);
1306 /* search assignment struct for a match for the hotkey setting */
1307 while (i--)
1309 this_item = &hotkey_items[i];
1310 if (this_item->action == action)
1312 /* run the associated function (with optional param), if any */
1313 const struct menu_func func = this_item->func;
1314 int func_return = ONPLAY_RELOAD_DIR;
1315 if (func.function != NULL)
1317 if (func.param != NULL)
1318 func_return = (*func.function_w_param)(func.param);
1319 else
1320 func_return = (*func.function)();
1322 /* return with the associated code */
1323 const int return_code = this_item->return_code;
1324 /* ONPLAY_OK here means to use the function return code */
1325 if (return_code == ONPLAY_OK)
1326 return func_return;
1327 return return_code;
1331 /* no valid hotkey set, ignore hotkey */
1332 return ONPLAY_RELOAD_DIR;
1334 #endif /* HOTKEY */
1336 int onplay(char* file, int attr, int from, bool hotkey)
1338 const struct menu_item_ex *menu;
1339 onplay_result = ONPLAY_OK;
1340 context = from;
1341 selected_file = file;
1342 selected_file_attr = attr;
1343 int menu_selection;
1344 #ifdef HAVE_HOTKEY
1345 if (hotkey)
1346 return execute_hotkey(context == CONTEXT_WPS);
1347 #else
1348 (void)hotkey;
1349 #endif
1351 push_current_activity(ACTIVITY_CONTEXTMENU);
1352 if (context == CONTEXT_WPS)
1353 menu = &wps_onplay_menu;
1354 else
1355 menu = &tree_onplay_menu;
1356 menu_selection = do_menu(menu, NULL, NULL, false);
1357 pop_current_activity();
1359 switch (menu_selection)
1361 case GO_TO_WPS:
1362 return ONPLAY_START_PLAY;
1363 case GO_TO_ROOT:
1364 case GO_TO_MAINMENU:
1365 return ONPLAY_MAINMENU;
1366 case GO_TO_PLAYLIST_VIEWER:
1367 return ONPLAY_PLAYLIST;
1368 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1369 case GO_TO_PICTUREFLOW:
1370 return ONPLAY_PICTUREFLOW;
1371 #endif
1372 default:
1373 return onplay_result;