Add support to buildzip.pl for Lua scripts
[kugel-rb.git] / apps / onplay.c
blob7ad22d5d70df9d24e310ee3f1889ea8c01f95d73
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
27 #include "debug.h"
28 #include "sprintf.h"
29 #include "lcd.h"
30 #include "dir.h"
31 #include "file.h"
32 #include "audio.h"
33 #include "menu.h"
34 #include "lang.h"
35 #include "playlist.h"
36 #include "button.h"
37 #include "kernel.h"
38 #include "keyboard.h"
39 #include "mp3data.h"
40 #include "metadata.h"
41 #include "screens.h"
42 #include "tree.h"
43 #include "buffer.h"
44 #include "settings.h"
45 #include "playlist_viewer.h"
46 #include "talk.h"
47 #include "onplay.h"
48 #include "filetypes.h"
49 #include "plugin.h"
50 #include "bookmark.h"
51 #include "action.h"
52 #include "splash.h"
53 #include "yesno.h"
54 #include "menus/exported_menus.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
58 #include "sound_menu.h"
59 #include "playlist_menu.h"
60 #include "playlist_catalog.h"
61 #ifdef HAVE_TAGCACHE
62 #include "tagtree.h"
63 #endif
64 #include "cuesheet.h"
65 #include "backdrop.h"
66 #include "pitchscreen.h"
67 #include "viewport.h"
69 static int context;
70 static char* selected_file = NULL;
71 static int selected_file_attr = 0;
72 static int onplay_result = ONPLAY_OK;
73 static char clipboard_selection[MAX_PATH];
74 static int clipboard_selection_attr = 0;
75 static bool clipboard_is_copy = false;
77 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
78 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
79 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
80 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
81 static const struct menu_item_ex name = \
82 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
83 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
84 { (void*)name##_},{.callback_and_desc = & name##__}};
86 #ifdef HAVE_LCD_BITMAP
87 static void draw_slider(void);
88 #else
89 #define draw_slider()
90 #endif
91 /* ----------------------------------------------------------------------- */
92 /* Displays the bookmark menu options for the user to decide. This is an */
93 /* interface function. */
94 /* ----------------------------------------------------------------------- */
96 static int bookmark_menu_callback(int action,
97 const struct menu_item_ex *this_item);
98 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
99 ID2P(LANG_BOOKMARK_MENU_CREATE),
100 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
101 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
102 ID2P(LANG_BOOKMARK_MENU_LIST),
103 bookmark_load_menu, NULL,
104 bookmark_menu_callback, Icon_Bookmark);
105 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
106 Icon_Bookmark, &bookmark_create_menu_item,
107 &bookmark_load_menu_item);
108 static int bookmark_menu_callback(int action,
109 const struct menu_item_ex *this_item)
111 (void)this_item;
112 switch (action)
114 case ACTION_REQUEST_MENUITEM:
115 if (this_item == &bookmark_load_menu_item)
117 if (bookmark_exist() == 0)
118 return ACTION_EXIT_MENUITEM;
120 /* hide the bookmark menu if there is no playback */
121 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
122 return ACTION_EXIT_MENUITEM;
123 break;
124 #ifdef HAVE_LCD_CHARCELLS
125 case ACTION_ENTER_MENUITEM:
126 status_set_param(true);
127 break;
128 #endif
129 case ACTION_EXIT_MENUITEM:
130 #ifdef HAVE_LCD_CHARCELLS
131 status_set_param(false);
132 #endif
133 settings_save();
134 break;
136 return action;
139 static bool list_viewers(void)
141 int ret = filetype_list_viewers(selected_file);
142 if (ret == PLUGIN_USB_CONNECTED)
143 onplay_result = ONPLAY_RELOAD_DIR;
144 return false;
147 static bool shuffle_playlist(void)
149 playlist_sort(NULL, true);
150 playlist_randomise(NULL, current_tick, true);
152 return false;
155 static bool save_playlist(void)
157 save_playlist_screen(NULL);
158 return false;
161 static bool add_to_playlist(int position, bool queue)
163 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
164 const char *lines[] = {
165 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
166 selected_file
168 const struct text_message message={lines, 2};
170 splash(0, ID2P(LANG_WAIT));
172 if (new_playlist)
173 playlist_create(NULL, NULL);
175 /* always set seed before inserting shuffled */
176 if (position == PLAYLIST_INSERT_SHUFFLED || position == PLAYLIST_INSERT_LAST_SHUFFLED)
178 srand(current_tick);
179 if (position == PLAYLIST_INSERT_LAST_SHUFFLED)
180 playlist_set_last_shuffled_start();
183 #ifdef HAVE_TAGCACHE
184 if (context == CONTEXT_ID3DB)
186 tagtree_insert_selection_playlist(position, queue);
188 else
189 #endif
191 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
192 playlist_insert_track(NULL, selected_file, position, queue, true);
193 else if (selected_file_attr & ATTR_DIRECTORY)
195 bool recurse = false;
197 if (global_settings.recursive_dir_insert != RECURSE_ASK)
198 recurse = (bool)global_settings.recursive_dir_insert;
199 else
201 /* Ask if user wants to recurse directory */
202 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
205 playlist_insert_directory(NULL, selected_file, position, queue,
206 recurse);
208 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
209 playlist_insert_playlist(NULL, selected_file, position, queue);
212 if (new_playlist && (playlist_amount() > 0))
214 /* nothing is currently playing so begin playing what we just
215 inserted */
216 if (global_settings.playlist_shuffle)
217 playlist_shuffle(current_tick, -1);
218 playlist_start(0,0);
219 onplay_result = ONPLAY_START_PLAY;
222 return false;
225 static bool view_playlist(void)
227 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
228 bool result;
230 result = playlist_viewer_ex(selected_file);
232 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
233 onplay_result == ONPLAY_OK)
234 /* playlist was started from viewer */
235 onplay_result = ONPLAY_START_PLAY;
237 return result;
240 static bool cat_add_to_a_playlist(void)
242 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
243 false, NULL);
246 static bool cat_add_to_a_new_playlist(void)
248 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
249 true, NULL);
253 static int cat_playlist_callback(int action,
254 const struct menu_item_ex *this_item);
255 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
256 catalog_view_playlists, 0, cat_playlist_callback,
257 Icon_Playlist);
258 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
259 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
260 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
261 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
262 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
263 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
264 &cat_add_to_new);
266 static int cat_playlist_callback(int action,
267 const struct menu_item_ex *this_item)
269 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
270 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
271 ((selected_file_attr & ATTR_DIRECTORY) == 0))
273 return ACTION_EXIT_MENUITEM;
276 switch (action)
278 case ACTION_REQUEST_MENUITEM:
279 if (this_item == &cat_view_lists)
281 if (context == CONTEXT_WPS)
282 return action;
284 else if (selected_file && /* set before calling this menu,
285 so safe */
286 ((audio_status() & AUDIO_STATUS_PLAY &&
287 context == CONTEXT_WPS) ||
288 context == CONTEXT_TREE))
290 return action;
292 else
293 return ACTION_EXIT_MENUITEM;
294 break;
296 return action;
300 /* CONTEXT_WPS playlist options */
301 MENUITEM_FUNCTION(playlist_viewer_item, 0,
302 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
303 NULL, NULL, Icon_Playlist);
304 MENUITEM_FUNCTION(search_playlist_item, 0,
305 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
306 NULL, NULL, Icon_Playlist);
307 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
308 save_playlist, NULL, NULL, Icon_Playlist);
309 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
310 shuffle_playlist, NULL, NULL, Icon_Playlist);
311 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
312 NULL, Icon_Playlist,
313 &playlist_viewer_item, &search_playlist_item,
314 &playlist_save_item, &reshuffle_item
317 /* CONTEXT_[TREE|ID3DB] playlist options */
318 static int playlist_insert_func(void *param)
320 add_to_playlist((intptr_t)param, false);
321 return 0;
323 static int playlist_queue_func(void *param)
325 add_to_playlist((intptr_t)param, true);
326 return 0;
328 static int treeplaylist_wplayback_callback(int action,
329 const struct menu_item_ex*
330 this_item)
332 (void)this_item;
333 switch (action)
335 case ACTION_REQUEST_MENUITEM:
336 if (audio_status() & AUDIO_STATUS_PLAY)
337 return action;
338 else
339 return ACTION_EXIT_MENUITEM;
340 break;
342 return action;
345 static int treeplaylist_callback(int action,
346 const struct menu_item_ex *this_item);
348 /* insert items */
349 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
350 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
351 treeplaylist_callback, Icon_Playlist);
352 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
353 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
354 treeplaylist_wplayback_callback, Icon_Playlist);
355 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
356 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
357 treeplaylist_wplayback_callback, Icon_Playlist);
358 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
359 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
360 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
361 Icon_Playlist);
362 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
363 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
364 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED, treeplaylist_callback,
365 Icon_Playlist);
366 /* queue items */
367 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
368 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
369 treeplaylist_wplayback_callback, Icon_Playlist);
370 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
371 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
372 treeplaylist_wplayback_callback, Icon_Playlist);
373 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
374 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
375 treeplaylist_wplayback_callback, Icon_Playlist);
376 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
377 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
378 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
379 treeplaylist_wplayback_callback, Icon_Playlist);
380 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
381 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
382 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
383 treeplaylist_callback, Icon_Playlist);
384 /* replace playlist */
385 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
386 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
387 treeplaylist_wplayback_callback, Icon_Playlist);
388 /* others */
390 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
391 view_playlist, NULL,
392 treeplaylist_callback, Icon_Playlist);
394 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
395 treeplaylist_callback, Icon_Playlist,
397 /* view */
398 &view_playlist_item,
400 /* insert */
401 &i_pl_item, &i_first_pl_item,
402 &i_last_pl_item, &i_shuf_pl_item,
403 &i_last_shuf_pl_item,
405 /* queue */
406 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
407 &q_shuf_pl_item,
408 &q_last_shuf_pl_item,
410 /* replace */
411 &replace_pl_item
413 static int treeplaylist_callback(int action,
414 const struct menu_item_ex *this_item)
416 (void)this_item;
417 switch (action)
419 case ACTION_REQUEST_MENUITEM:
420 if (this_item == &tree_playlist_menu)
422 if (((selected_file_attr & FILE_ATTR_MASK) ==
423 FILE_ATTR_AUDIO) ||
424 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
425 (selected_file_attr & ATTR_DIRECTORY))
427 return action;
429 else
430 return ACTION_EXIT_MENUITEM;
432 else if (this_item == &view_playlist_item)
434 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
435 context == CONTEXT_TREE)
436 return action;
437 else
438 return ACTION_EXIT_MENUITEM;
440 else if (this_item == &i_shuf_pl_item)
443 if (audio_status() & AUDIO_STATUS_PLAY)
445 return action;
447 else if ((this_item == &i_shuf_pl_item) &&
448 ((selected_file_attr & ATTR_DIRECTORY) ||
449 ((selected_file_attr & FILE_ATTR_MASK) ==
450 FILE_ATTR_M3U)))
452 return action;
454 return ACTION_EXIT_MENUITEM;
456 else if (this_item == &i_last_shuf_pl_item || this_item == &q_last_shuf_pl_item)
458 if ((playlist_amount() > 0) && (audio_status() & AUDIO_STATUS_PLAY) && (selected_file_attr & ATTR_DIRECTORY))
460 return action;
462 else
463 return ACTION_EXIT_MENUITEM;
465 break;
467 return action;
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 splash(0, dirname);
492 /* append name to current directory */
493 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
494 if (entry->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 /* inform the user which dir we're deleting */
502 result = remove_dir(dirname, len); /* recursion */
503 if (result)
504 break; /* or better continue, delete what we can? */
506 else
507 { /* remove a file */
508 draw_slider();
509 result = remove(dirname);
511 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
513 splash(HZ, ID2P(LANG_CANCEL));
514 result = -1;
515 break;
518 closedir(dir);
520 if (!result)
521 { /* remove the now empty directory */
522 dirname[dirlen] = '\0'; /* terminate to original length */
524 result = rmdir(dirname);
527 return result;
531 /* share code for file and directory deletion, saves space */
532 static bool delete_handler(bool is_dir)
534 char file_to_delete[MAX_PATH];
535 strcpy(file_to_delete, selected_file);
537 const char *lines[]={
538 ID2P(LANG_REALLY_DELETE),
539 file_to_delete
541 const char *yes_lines[]={
542 ID2P(LANG_DELETING),
543 file_to_delete
546 const struct text_message message={lines, 2};
547 const struct text_message yes_message={yes_lines, 2};
549 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
550 return false;
552 splash(0, str(LANG_DELETING));
554 int res;
555 if (is_dir)
557 char pathname[MAX_PATH]; /* space to go deep */
558 cpu_boost(true);
559 strlcpy(pathname, file_to_delete, sizeof(pathname));
560 res = remove_dir(pathname, sizeof(pathname));
561 cpu_boost(false);
563 else
564 res = remove(file_to_delete);
566 if (!res)
567 onplay_result = ONPLAY_RELOAD_DIR;
569 return false;
573 static bool delete_file(void)
575 return delete_handler(false);
578 static bool delete_dir(void)
580 return delete_handler(true);
583 #if LCD_DEPTH > 1
584 static bool set_backdrop(void)
586 /* load the image */
587 if(backdrop_load(BACKDROP_MAIN, selected_file)) {
588 splash(HZ, str(LANG_BACKDROP_LOADED));
589 set_file(selected_file, (char *)global_settings.backdrop_file,
590 MAX_FILENAME);
591 backdrop_show(BACKDROP_MAIN);
592 return true;
593 } else {
594 splash(HZ, str(LANG_BACKDROP_FAILED));
595 return false;
598 #endif
600 static bool rename_file(void)
602 char newname[MAX_PATH];
603 char* ptr = strrchr(selected_file, '/') + 1;
604 int pathlen = (ptr - selected_file);
605 strlcpy(newname, selected_file, sizeof(newname));
606 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
607 if (!strlen(newname + pathlen) ||
608 (rename(selected_file, newname) < 0)) {
609 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
610 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
612 else
613 onplay_result = ONPLAY_RELOAD_DIR;
616 return false;
619 static bool create_dir(void)
621 char dirname[MAX_PATH];
622 char *cwd;
623 int rc;
624 int pathlen;
626 cwd = getcwd(NULL, 0);
627 memset(dirname, 0, sizeof dirname);
629 snprintf(dirname, sizeof dirname, "%s/",
630 cwd[1] ? cwd : "");
632 pathlen = strlen(dirname);
633 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
634 if (rc < 0)
635 return false;
637 rc = mkdir(dirname);
638 if (rc < 0) {
639 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
640 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
641 str(LANG_FAILED));
642 } else {
643 onplay_result = ONPLAY_RELOAD_DIR;
646 return true;
649 static bool properties(void)
651 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
652 selected_file))
653 onplay_result = ONPLAY_RELOAD_DIR;
654 return false;
657 /* Store the current selection in the clipboard */
658 static bool clipboard_clip(bool copy)
660 clipboard_selection[0] = 0;
661 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
662 clipboard_selection_attr = selected_file_attr;
663 clipboard_is_copy = copy;
665 return true;
668 static bool clipboard_cut(void)
670 return clipboard_clip(false);
673 static bool clipboard_copy(void)
675 return clipboard_clip(true);
678 #ifdef HAVE_LCD_BITMAP
679 static void draw_slider(void)
681 int i;
682 FOR_NB_SCREENS(i)
684 struct viewport *vp = &(viewport_get_current_vp())[i];
685 show_busy_slider(&screens[i], vp->x,
686 (vp->y+vp->height)-2*screens[i].getcharheight(),
687 vp->width, 2*screens[i].getcharheight()-1);
688 screens[i].update();
691 #endif
693 /* Paste a file to a new directory. Will overwrite always. */
694 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
696 int src_fd, target_fd;
697 size_t buffersize;
698 ssize_t size, bytesread, byteswritten;
699 char *buffer;
700 bool result = false;
702 if (copy) {
703 /* See if we can get the plugin buffer for the file copy buffer */
704 buffer = (char *) plugin_get_buffer(&buffersize);
705 if (buffer == NULL || buffersize < 512) {
706 /* Not large enough, try for a disk sector worth of stack
707 instead */
708 buffersize = 512;
709 buffer = (char *) __builtin_alloca(buffersize);
712 if (buffer == NULL) {
713 return false;
716 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
717 size */
719 src_fd = open(src, O_RDONLY);
721 if (src_fd >= 0) {
722 target_fd = creat(target);
724 if (target_fd >= 0) {
725 result = true;
727 size = filesize(src_fd);
729 if (size == -1) {
730 result = false;
733 while(size > 0) {
734 bytesread = read(src_fd, buffer, buffersize);
736 if (bytesread == -1) {
737 result = false;
738 break;
741 size -= bytesread;
743 while(bytesread > 0) {
744 byteswritten = write(target_fd, buffer, bytesread);
746 if (byteswritten < 0) {
747 result = false;
748 size = 0;
749 break;
752 bytesread -= byteswritten;
753 draw_slider();
757 close(target_fd);
759 /* Copy failed. Cleanup. */
760 if (!result) {
761 remove(target);
765 close(src_fd);
767 } else {
768 result = rename(src, target) == 0;
769 #ifdef HAVE_MULTIVOLUME
770 if (!result) {
771 if (errno == EXDEV) {
772 /* Failed because cross volume rename doesn't work. Copy
773 instead */
774 result = clipboard_pastefile(src, target, true);
776 if (result) {
777 result = remove(src) == 0;
781 #endif
784 return result;
787 /* Paste a directory to a new location. Designed to be called by
788 clipboard_paste */
789 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
790 int targetlen, bool copy)
792 DIR *srcdir;
793 int srcdirlen = strlen(src);
794 int targetdirlen = strlen(target);
795 bool result = true;
797 if (!file_exists(target)) {
798 if (!copy) {
799 /* Just move the directory */
800 result = rename(src, target) == 0;
802 #ifdef HAVE_MULTIVOLUME
803 if (!result && errno == EXDEV) {
804 /* Try a copy as we're going across devices */
805 result = clipboard_pastedirectory(src, srclen, target,
806 targetlen, true);
808 /* If it worked, remove the source directory */
809 if (result) {
810 remove_dir(src, srclen);
813 #endif
814 return result;
815 } else {
816 /* Make a directory to copy things to */
817 result = mkdir(target) == 0;
821 /* Check if something went wrong already */
822 if (!result) {
823 return result;
826 srcdir = opendir(src);
827 if (!srcdir) {
828 return false;
831 /* This loop will exit as soon as there's a problem */
832 while(result)
834 struct dirent* entry;
835 /* walk through the directory content */
836 entry = readdir(srcdir);
837 if (!entry)
838 break;
840 /* append name to current directory */
841 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
842 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
843 entry->d_name);
845 DEBUGF("Copy %s to %s\n", src, target);
847 if (entry->attribute & ATTR_DIRECTORY)
848 { /* copy/move a subdirectory */
849 if (!strcmp((char *)entry->d_name, ".") ||
850 !strcmp((char *)entry->d_name, ".."))
851 continue; /* skip these */
853 result = clipboard_pastedirectory(src, srclen, target, targetlen,
854 copy); /* recursion */
856 else
857 { /* copy/move a file */
858 result = clipboard_pastefile(src, target, copy);
862 closedir(srcdir);
864 if (result) {
865 src[srcdirlen] = '\0'; /* terminate to original length */
866 target[targetdirlen] = '\0'; /* terminate to original length */
869 return result;
872 /* Paste the clipboard to the current directory */
873 static bool clipboard_paste(void)
875 char target[MAX_PATH];
876 char *cwd, *nameptr;
877 bool success;
879 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
880 static const struct text_message message={lines, 1};
882 /* Get the name of the current directory */
883 cwd = getcwd(NULL, 0);
885 /* Figure out the name of the selection */
886 nameptr = strrchr(clipboard_selection, '/');
888 /* Final target is current directory plus name of selection */
889 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
891 /* If the target existed but they choose not to overwite, exit */
892 if (file_exists(target) &&
893 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
894 return false;
897 if (clipboard_is_copy) {
898 splash(0, ID2P(LANG_COPYING));
900 else
902 splash(0, ID2P(LANG_MOVING));
905 /* Now figure out what we're doing */
906 cpu_boost(true);
907 if (clipboard_selection_attr & ATTR_DIRECTORY) {
908 /* Recursion. Set up external stack */
909 char srcpath[MAX_PATH];
910 char targetpath[MAX_PATH];
911 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
913 /* Do not allow the user to paste a directory into a dir they are
914 copying */
915 success = 0;
917 else
919 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
920 strlcpy(targetpath, target, sizeof(targetpath));
922 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
923 target, sizeof(targetpath), clipboard_is_copy);
925 if (success && !clipboard_is_copy)
927 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
928 remove_dir(srcpath, sizeof(srcpath));
931 } else {
932 success = clipboard_pastefile(clipboard_selection, target,
933 clipboard_is_copy);
935 cpu_boost(false);
937 /* Did it work? */
938 if (success) {
939 /* Reset everything */
940 clipboard_selection[0] = 0;
941 clipboard_selection_attr = 0;
942 clipboard_is_copy = false;
944 /* Force reload of the current directory */
945 onplay_result = ONPLAY_RELOAD_DIR;
946 } else {
947 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
948 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
949 str(LANG_FAILED));
952 return true;
955 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
956 #ifdef HAVE_TAGCACHE
957 static int set_rating_inline(void)
959 struct mp3entry* id3 = audio_current_track();
960 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
962 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
963 NULL, 1, 0, 10, NULL, NULL);
964 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
966 else
967 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
968 return 0;
970 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
972 (void)this_item;
973 switch (action)
975 case ACTION_REQUEST_MENUITEM:
976 if (!selected_file || !global_settings.runtimedb ||
977 !tagcache_is_usable())
978 return ACTION_EXIT_MENUITEM;
979 break;
981 return action;
983 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
984 set_rating_inline, NULL,
985 ratingitem_callback, Icon_Questionmark);
986 #endif
988 static bool view_cue(void)
990 struct mp3entry* id3 = audio_current_track();
991 if(id3 && id3->cuesheet)
993 browse_cuesheet(id3->cuesheet);
995 return false;
997 static int view_cue_item_callback(int action,
998 const struct menu_item_ex *this_item)
1000 (void)this_item;
1001 struct mp3entry* id3 = audio_current_track();
1002 switch (action)
1004 case ACTION_REQUEST_MENUITEM:
1005 if (!selected_file
1006 || !id3 || !id3->cuesheet)
1007 return ACTION_EXIT_MENUITEM;
1008 break;
1010 return action;
1012 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1013 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1015 /* CONTEXT_WPS items */
1016 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1017 browse_id3, NULL, NULL, Icon_NOICON);
1018 #ifdef HAVE_PITCHSCREEN
1019 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1020 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1021 #endif
1023 /* CONTEXT_[TREE|ID3DB] items */
1024 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1025 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1026 rename_file, NULL, clipboard_callback, Icon_NOICON);
1027 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1028 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1029 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1030 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1031 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1032 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1033 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1034 delete_file, NULL, clipboard_callback, Icon_NOICON);
1035 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1036 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1037 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1038 properties, NULL, clipboard_callback, Icon_NOICON);
1039 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1040 create_dir, NULL, clipboard_callback, Icon_NOICON);
1041 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1042 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1043 #if LCD_DEPTH > 1
1044 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1045 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1046 #endif
1047 #ifdef HAVE_RECORDING
1048 static bool set_recdir(void)
1050 strlcpy(global_settings.rec_directory,
1051 selected_file, MAX_FILENAME+1);
1052 settings_save();
1053 return false;
1055 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1056 set_recdir, NULL, clipboard_callback, Icon_Recording);
1057 #endif
1058 static bool add_to_faves(void)
1060 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1061 selected_file))
1062 onplay_result = ONPLAY_RELOAD_DIR;
1063 return false;
1065 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1066 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1069 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1071 switch (action)
1073 case ACTION_REQUEST_MENUITEM:
1074 #ifdef HAVE_MULTIVOLUME
1075 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1076 (this_item == &rename_file_item ||
1077 this_item == &delete_dir_item ||
1078 this_item == &clipboard_cut_item) )
1079 return ACTION_EXIT_MENUITEM;
1080 #endif
1081 if (context == CONTEXT_ID3DB)
1082 return ACTION_EXIT_MENUITEM;
1083 if (this_item == &clipboard_paste_item)
1084 { /* visible if there is something to paste */
1085 return (clipboard_selection[0] != 0) ?
1086 action : ACTION_EXIT_MENUITEM;
1088 else if (this_item == &create_dir_item)
1090 /* always visible */
1091 return action;
1093 else if ((this_item == &properties_item) ||
1094 (this_item == &rename_file_item) ||
1095 (this_item == &clipboard_cut_item) ||
1096 (this_item == &clipboard_copy_item) ||
1097 (this_item == &add_to_faves_item)
1100 /* requires an actual file */
1101 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1103 #if LCD_DEPTH > 1
1104 else if (this_item == &set_backdrop_item)
1106 if (selected_file)
1108 char *suffix = strrchr(selected_file, '.');
1109 if (suffix)
1111 if (strcasecmp(suffix, ".bmp") == 0)
1113 return action;
1117 return ACTION_EXIT_MENUITEM;
1119 #endif
1120 else if ((selected_file_attr & ATTR_DIRECTORY))
1122 if ((this_item == &delete_dir_item)
1124 return action;
1125 #ifdef HAVE_RECORDING
1126 else if (this_item == &set_recdir_item)
1127 return action;
1128 #endif
1130 else if (selected_file
1131 #ifdef HAVE_MULTIVOLUME
1132 /* no rename+delete for volumes */
1133 && !(selected_file_attr & ATTR_VOLUME)
1134 #endif
1137 if ((this_item == &delete_file_item) ||
1138 (this_item == &list_viewers_item))
1140 return action;
1143 return ACTION_EXIT_MENUITEM;
1144 break;
1146 return action;
1148 /* used when onplay() is called in the CONTEXT_WPS context */
1151 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1152 onplaymenu_callback, Icon_Audio,
1153 &wps_playlist_menu, &cat_playlist_menu,
1154 &sound_settings, &playback_settings,
1155 #ifdef HAVE_TAGCACHE
1156 &rating_item,
1157 #endif
1158 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1159 &delete_file_item, &view_cue_item,
1160 #ifdef HAVE_PITCHSCREEN
1161 &pitch_screen_item,
1162 #endif
1164 /* used when onplay() is not called in the CONTEXT_WPS context */
1165 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1166 onplaymenu_callback, Icon_file_view_menu,
1167 &tree_playlist_menu, &cat_playlist_menu,
1168 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1169 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1170 #if LCD_DEPTH > 1
1171 &set_backdrop_item,
1172 #endif
1173 &list_viewers_item, &create_dir_item, &properties_item,
1174 #ifdef HAVE_RECORDING
1175 &set_recdir_item,
1176 #endif
1177 &add_to_faves_item,
1179 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1181 (void)this_item;
1182 switch (action)
1184 case ACTION_TREE_STOP:
1185 if (this_item == &wps_onplay_menu)
1187 list_stop_handler();
1188 return ACTION_STD_CANCEL;
1190 break;
1191 case ACTION_EXIT_MENUITEM:
1192 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1193 break;
1195 return action;
1197 int onplay(char* file, int attr, int from)
1199 const struct menu_item_ex *menu;
1200 onplay_result = ONPLAY_OK;
1201 context = from;
1202 selected_file = file;
1203 selected_file_attr = attr;
1204 if (context == CONTEXT_WPS)
1205 menu = &wps_onplay_menu;
1206 else
1207 menu = &tree_onplay_menu;
1208 switch (do_menu(menu, NULL, NULL, false))
1210 case GO_TO_WPS:
1211 return ONPLAY_START_PLAY;
1212 case GO_TO_ROOT:
1213 case GO_TO_MAINMENU:
1214 return ONPLAY_MAINMENU;
1215 default:
1216 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;