spacerocks: Fix indentation and remove trailing spaces. Fix some minor bugs.
[kugel-rb.git] / apps / onplay.c
blob47a21a925084bab03af244c1f7b91ee7bfc74820
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 "settings.h"
44 #include "playlist_viewer.h"
45 #include "talk.h"
46 #include "onplay.h"
47 #include "filetypes.h"
48 #include "plugin.h"
49 #include "bookmark.h"
50 #include "action.h"
51 #include "splash.h"
52 #include "yesno.h"
53 #include "menus/exported_menus.h"
54 #ifdef HAVE_LCD_BITMAP
55 #include "icons.h"
56 #endif
57 #include "sound_menu.h"
58 #include "playlist_menu.h"
59 #include "playlist_catalog.h"
60 #ifdef HAVE_TAGCACHE
61 #include "tagtree.h"
62 #endif
63 #include "cuesheet.h"
64 #include "backdrop.h"
65 #include "pitchscreen.h"
66 #include "viewport.h"
68 static int context;
69 static char* selected_file = NULL;
70 static int selected_file_attr = 0;
71 static int onplay_result = ONPLAY_OK;
72 static char clipboard_selection[MAX_PATH];
73 static int clipboard_selection_attr = 0;
74 static bool clipboard_is_copy = false;
76 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
77 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
78 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
79 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
80 static const struct menu_item_ex name = \
81 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
82 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
83 { (void*)name##_},{.callback_and_desc = & name##__}};
85 #ifdef HAVE_LCD_BITMAP
86 static void draw_slider(void);
87 #else
88 #define draw_slider()
89 #endif
90 /* ----------------------------------------------------------------------- */
91 /* Displays the bookmark menu options for the user to decide. This is an */
92 /* interface function. */
93 /* ----------------------------------------------------------------------- */
95 static int bookmark_menu_callback(int action,
96 const struct menu_item_ex *this_item);
97 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
98 ID2P(LANG_BOOKMARK_MENU_CREATE),
99 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
100 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
101 ID2P(LANG_BOOKMARK_MENU_LIST),
102 bookmark_load_menu, NULL,
103 bookmark_menu_callback, Icon_Bookmark);
104 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
105 Icon_Bookmark, &bookmark_create_menu_item,
106 &bookmark_load_menu_item);
107 static int bookmark_menu_callback(int action,
108 const struct menu_item_ex *this_item)
110 (void)this_item;
111 switch (action)
113 case ACTION_REQUEST_MENUITEM:
114 if (this_item == &bookmark_load_menu_item)
116 if (bookmark_exist() == 0)
117 return ACTION_EXIT_MENUITEM;
119 /* hide the bookmark menu if there is no playback */
120 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
121 return ACTION_EXIT_MENUITEM;
122 break;
123 #ifdef HAVE_LCD_CHARCELLS
124 case ACTION_ENTER_MENUITEM:
125 status_set_param(true);
126 break;
127 #endif
128 case ACTION_EXIT_MENUITEM:
129 #ifdef HAVE_LCD_CHARCELLS
130 status_set_param(false);
131 #endif
132 settings_save();
133 break;
135 return action;
138 static bool list_viewers(void)
140 int ret = filetype_list_viewers(selected_file);
141 if (ret == PLUGIN_USB_CONNECTED)
142 onplay_result = ONPLAY_RELOAD_DIR;
143 return false;
146 static bool shuffle_playlist(void)
148 playlist_sort(NULL, true);
149 playlist_randomise(NULL, current_tick, true);
151 return false;
154 static bool save_playlist(void)
156 save_playlist_screen(NULL);
157 return false;
160 static bool add_to_playlist(int position, bool queue)
162 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
163 const char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 const struct text_message message={lines, 2};
169 splash(0, ID2P(LANG_WAIT));
171 if (new_playlist)
172 playlist_create(NULL, NULL);
174 /* always set seed before inserting shuffled */
175 if (position == PLAYLIST_INSERT_SHUFFLED ||
176 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)
442 if (audio_status() & AUDIO_STATUS_PLAY)
444 return action;
446 else if ((this_item == &i_shuf_pl_item) &&
447 ((selected_file_attr & ATTR_DIRECTORY) ||
448 ((selected_file_attr & FILE_ATTR_MASK) ==
449 FILE_ATTR_M3U)))
451 return action;
453 return ACTION_EXIT_MENUITEM;
455 else if (this_item == &i_last_shuf_pl_item ||
456 this_item == &q_last_shuf_pl_item)
458 if ((playlist_amount() > 0) &&
459 (audio_status() & AUDIO_STATUS_PLAY) &&
460 ((selected_file_attr & ATTR_DIRECTORY) ||
461 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
463 return action;
465 else
466 return ACTION_EXIT_MENUITEM;
468 break;
470 return action;
473 /* helper function to remove a non-empty directory */
474 static int remove_dir(char* dirname, int len)
476 int result = 0;
477 DIR* dir;
478 int dirlen = strlen(dirname);
480 dir = opendir(dirname);
481 if (!dir)
482 return -1; /* open error */
484 while(true)
486 struct dirent* entry;
487 /* walk through the directory content */
488 entry = readdir(dir);
489 if (!entry)
490 break;
492 dirname[dirlen] ='\0';
493 splash(0, dirname);
495 /* append name to current directory */
496 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
497 if (entry->attribute & ATTR_DIRECTORY)
498 { /* remove a subdirectory */
499 if (!strcmp((char *)entry->d_name, ".") ||
500 !strcmp((char *)entry->d_name, ".."))
501 continue; /* skip these */
503 /* inform the user which dir we're deleting */
505 result = remove_dir(dirname, len); /* recursion */
506 if (result)
507 break; /* or better continue, delete what we can? */
509 else
510 { /* remove a file */
511 draw_slider();
512 result = remove(dirname);
514 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
516 splash(HZ, ID2P(LANG_CANCEL));
517 result = -1;
518 break;
521 closedir(dir);
523 if (!result)
524 { /* remove the now empty directory */
525 dirname[dirlen] = '\0'; /* terminate to original length */
527 result = rmdir(dirname);
530 return result;
534 /* share code for file and directory deletion, saves space */
535 static bool delete_handler(bool is_dir)
537 char file_to_delete[MAX_PATH];
538 strcpy(file_to_delete, selected_file);
540 const char *lines[]={
541 ID2P(LANG_REALLY_DELETE),
542 file_to_delete
544 const char *yes_lines[]={
545 ID2P(LANG_DELETING),
546 file_to_delete
549 const struct text_message message={lines, 2};
550 const struct text_message yes_message={yes_lines, 2};
552 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
553 return false;
555 splash(0, str(LANG_DELETING));
557 int res;
558 if (is_dir)
560 char pathname[MAX_PATH]; /* space to go deep */
561 cpu_boost(true);
562 strlcpy(pathname, file_to_delete, sizeof(pathname));
563 res = remove_dir(pathname, sizeof(pathname));
564 cpu_boost(false);
566 else
567 res = remove(file_to_delete);
569 if (!res)
570 onplay_result = ONPLAY_RELOAD_DIR;
572 return false;
576 static bool delete_file(void)
578 return delete_handler(false);
581 static bool delete_dir(void)
583 return delete_handler(true);
586 #if LCD_DEPTH > 1
587 static bool set_backdrop(void)
589 /* load the image */
590 if(backdrop_load(BACKDROP_MAIN, selected_file)) {
591 splash(HZ, str(LANG_BACKDROP_LOADED));
592 set_file(selected_file, (char *)global_settings.backdrop_file,
593 MAX_FILENAME);
594 backdrop_show(BACKDROP_MAIN);
595 return true;
596 } else {
597 splash(HZ, str(LANG_BACKDROP_FAILED));
598 return false;
601 #endif
603 static bool rename_file(void)
605 char newname[MAX_PATH];
606 char* ptr = strrchr(selected_file, '/') + 1;
607 int pathlen = (ptr - selected_file);
608 strlcpy(newname, selected_file, sizeof(newname));
609 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
610 if (!strlen(newname + pathlen) ||
611 (rename(selected_file, newname) < 0)) {
612 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
613 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
615 else
616 onplay_result = ONPLAY_RELOAD_DIR;
619 return false;
622 static bool create_dir(void)
624 char dirname[MAX_PATH];
625 char *cwd;
626 int rc;
627 int pathlen;
629 cwd = getcwd(NULL, 0);
630 memset(dirname, 0, sizeof dirname);
632 snprintf(dirname, sizeof dirname, "%s/",
633 cwd[1] ? cwd : "");
635 pathlen = strlen(dirname);
636 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
637 if (rc < 0)
638 return false;
640 rc = mkdir(dirname);
641 if (rc < 0) {
642 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
643 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
644 str(LANG_FAILED));
645 } else {
646 onplay_result = ONPLAY_RELOAD_DIR;
649 return true;
652 static bool properties(void)
654 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
655 selected_file))
656 onplay_result = ONPLAY_RELOAD_DIR;
657 return false;
660 /* Store the current selection in the clipboard */
661 static bool clipboard_clip(bool copy)
663 clipboard_selection[0] = 0;
664 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
665 clipboard_selection_attr = selected_file_attr;
666 clipboard_is_copy = copy;
668 return true;
671 static bool clipboard_cut(void)
673 return clipboard_clip(false);
676 static bool clipboard_copy(void)
678 return clipboard_clip(true);
681 #ifdef HAVE_LCD_BITMAP
682 static void draw_slider(void)
684 int i;
685 FOR_NB_SCREENS(i)
687 struct viewport vp;
688 viewport_set_defaults(&vp, i);
689 screens[i].set_viewport(&vp);
690 show_busy_slider(&screens[i], vp.x,
691 (vp.y+vp.height)-2*screens[i].getcharheight(),
692 vp.width, 2*screens[i].getcharheight()-1);
693 screens[i].update();
696 #endif
698 /* Paste a file to a new directory. Will overwrite always. */
699 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
701 int src_fd, target_fd;
702 size_t buffersize;
703 ssize_t size, bytesread, byteswritten;
704 char *buffer;
705 bool result = false;
707 if (copy) {
708 /* See if we can get the plugin buffer for the file copy buffer */
709 buffer = (char *) plugin_get_buffer(&buffersize);
710 if (buffer == NULL || buffersize < 512) {
711 /* Not large enough, try for a disk sector worth of stack
712 instead */
713 buffersize = 512;
714 buffer = (char *) __builtin_alloca(buffersize);
717 if (buffer == NULL) {
718 return false;
721 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
722 size */
724 src_fd = open(src, O_RDONLY);
726 if (src_fd >= 0) {
727 target_fd = creat(target);
729 if (target_fd >= 0) {
730 result = true;
732 size = filesize(src_fd);
734 if (size == -1) {
735 result = false;
738 while(size > 0) {
739 bytesread = read(src_fd, buffer, buffersize);
741 if (bytesread == -1) {
742 result = false;
743 break;
746 size -= bytesread;
748 while(bytesread > 0) {
749 byteswritten = write(target_fd, buffer, bytesread);
751 if (byteswritten < 0) {
752 result = false;
753 size = 0;
754 break;
757 bytesread -= byteswritten;
758 draw_slider();
762 close(target_fd);
764 /* Copy failed. Cleanup. */
765 if (!result) {
766 remove(target);
770 close(src_fd);
772 } else {
773 result = rename(src, target) == 0;
774 #ifdef HAVE_MULTIVOLUME
775 if (!result) {
776 if (errno == EXDEV) {
777 /* Failed because cross volume rename doesn't work. Copy
778 instead */
779 result = clipboard_pastefile(src, target, true);
781 if (result) {
782 result = remove(src) == 0;
786 #endif
789 return result;
792 /* Paste a directory to a new location. Designed to be called by
793 clipboard_paste */
794 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
795 int targetlen, bool copy)
797 DIR *srcdir;
798 int srcdirlen = strlen(src);
799 int targetdirlen = strlen(target);
800 bool result = true;
802 if (!file_exists(target)) {
803 if (!copy) {
804 /* Just move the directory */
805 result = rename(src, target) == 0;
807 #ifdef HAVE_MULTIVOLUME
808 if (!result && errno == EXDEV) {
809 /* Try a copy as we're going across devices */
810 result = clipboard_pastedirectory(src, srclen, target,
811 targetlen, true);
813 /* If it worked, remove the source directory */
814 if (result) {
815 remove_dir(src, srclen);
818 #endif
819 return result;
820 } else {
821 /* Make a directory to copy things to */
822 result = mkdir(target) == 0;
826 /* Check if something went wrong already */
827 if (!result) {
828 return result;
831 srcdir = opendir(src);
832 if (!srcdir) {
833 return false;
836 /* This loop will exit as soon as there's a problem */
837 while(result)
839 struct dirent* entry;
840 /* walk through the directory content */
841 entry = readdir(srcdir);
842 if (!entry)
843 break;
845 /* append name to current directory */
846 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
847 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
848 entry->d_name);
850 DEBUGF("Copy %s to %s\n", src, target);
852 if (entry->attribute & ATTR_DIRECTORY)
853 { /* copy/move a subdirectory */
854 if (!strcmp((char *)entry->d_name, ".") ||
855 !strcmp((char *)entry->d_name, ".."))
856 continue; /* skip these */
858 result = clipboard_pastedirectory(src, srclen, target, targetlen,
859 copy); /* recursion */
861 else
862 { /* copy/move a file */
863 result = clipboard_pastefile(src, target, copy);
867 closedir(srcdir);
869 if (result) {
870 src[srcdirlen] = '\0'; /* terminate to original length */
871 target[targetdirlen] = '\0'; /* terminate to original length */
874 return result;
877 /* Paste the clipboard to the current directory */
878 static bool clipboard_paste(void)
880 char target[MAX_PATH];
881 char *cwd, *nameptr;
882 bool success;
884 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
885 static const struct text_message message={lines, 1};
887 /* Get the name of the current directory */
888 cwd = getcwd(NULL, 0);
890 /* Figure out the name of the selection */
891 nameptr = strrchr(clipboard_selection, '/');
893 /* Final target is current directory plus name of selection */
894 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
896 /* If the target existed but they choose not to overwite, exit */
897 if (file_exists(target) &&
898 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
899 return false;
902 if (clipboard_is_copy) {
903 splash(0, ID2P(LANG_COPYING));
905 else
907 splash(0, ID2P(LANG_MOVING));
910 /* Now figure out what we're doing */
911 cpu_boost(true);
912 if (clipboard_selection_attr & ATTR_DIRECTORY) {
913 /* Recursion. Set up external stack */
914 char srcpath[MAX_PATH];
915 char targetpath[MAX_PATH];
916 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
918 /* Do not allow the user to paste a directory into a dir they are
919 copying */
920 success = 0;
922 else
924 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
925 strlcpy(targetpath, target, sizeof(targetpath));
927 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
928 target, sizeof(targetpath), clipboard_is_copy);
930 if (success && !clipboard_is_copy)
932 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
933 remove_dir(srcpath, sizeof(srcpath));
936 } else {
937 success = clipboard_pastefile(clipboard_selection, target,
938 clipboard_is_copy);
940 cpu_boost(false);
942 /* Did it work? */
943 if (success) {
944 /* Reset everything */
945 clipboard_selection[0] = 0;
946 clipboard_selection_attr = 0;
947 clipboard_is_copy = false;
949 /* Force reload of the current directory */
950 onplay_result = ONPLAY_RELOAD_DIR;
951 } else {
952 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
953 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
954 str(LANG_FAILED));
957 return true;
960 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
961 #ifdef HAVE_TAGCACHE
962 static int set_rating_inline(void)
964 struct mp3entry* id3 = audio_current_track();
965 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
967 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
968 NULL, 1, 0, 10, NULL, NULL);
969 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
971 else
972 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
973 return 0;
975 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
977 (void)this_item;
978 switch (action)
980 case ACTION_REQUEST_MENUITEM:
981 if (!selected_file || !global_settings.runtimedb ||
982 !tagcache_is_usable())
983 return ACTION_EXIT_MENUITEM;
984 break;
986 return action;
988 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
989 set_rating_inline, NULL,
990 ratingitem_callback, Icon_Questionmark);
991 #endif
993 static bool view_cue(void)
995 struct mp3entry* id3 = audio_current_track();
996 if(id3 && id3->cuesheet)
998 browse_cuesheet(id3->cuesheet);
1000 return false;
1002 static int view_cue_item_callback(int action,
1003 const struct menu_item_ex *this_item)
1005 (void)this_item;
1006 struct mp3entry* id3 = audio_current_track();
1007 switch (action)
1009 case ACTION_REQUEST_MENUITEM:
1010 if (!selected_file
1011 || !id3 || !id3->cuesheet)
1012 return ACTION_EXIT_MENUITEM;
1013 break;
1015 return action;
1017 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1018 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1020 /* CONTEXT_WPS items */
1021 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1022 browse_id3, NULL, NULL, Icon_NOICON);
1023 #ifdef HAVE_PITCHSCREEN
1024 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1025 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1026 #endif
1028 /* CONTEXT_[TREE|ID3DB] items */
1029 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1030 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1031 rename_file, NULL, clipboard_callback, Icon_NOICON);
1032 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1033 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1034 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1035 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1036 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1037 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1038 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1039 delete_file, NULL, clipboard_callback, Icon_NOICON);
1040 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1041 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1042 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1043 properties, NULL, clipboard_callback, Icon_NOICON);
1044 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1045 create_dir, NULL, clipboard_callback, Icon_NOICON);
1046 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1047 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1048 #if LCD_DEPTH > 1
1049 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1050 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1051 #endif
1052 #ifdef HAVE_RECORDING
1053 static bool set_recdir(void)
1055 strlcpy(global_settings.rec_directory,
1056 selected_file, MAX_FILENAME+1);
1057 settings_save();
1058 return false;
1060 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1061 set_recdir, NULL, clipboard_callback, Icon_Recording);
1062 #endif
1063 static bool add_to_faves(void)
1065 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1066 selected_file))
1067 onplay_result = ONPLAY_RELOAD_DIR;
1068 return false;
1070 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1071 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1074 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1076 switch (action)
1078 case ACTION_REQUEST_MENUITEM:
1079 #ifdef HAVE_MULTIVOLUME
1080 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1081 (this_item == &rename_file_item ||
1082 this_item == &delete_dir_item ||
1083 this_item == &clipboard_cut_item) )
1084 return ACTION_EXIT_MENUITEM;
1085 #endif
1086 if (context == CONTEXT_ID3DB)
1087 return ACTION_EXIT_MENUITEM;
1088 if (this_item == &clipboard_paste_item)
1089 { /* visible if there is something to paste */
1090 return (clipboard_selection[0] != 0) ?
1091 action : ACTION_EXIT_MENUITEM;
1093 else if (this_item == &create_dir_item)
1095 /* always visible */
1096 return action;
1098 else if ((this_item == &properties_item) ||
1099 (this_item == &rename_file_item) ||
1100 (this_item == &clipboard_cut_item) ||
1101 (this_item == &clipboard_copy_item) ||
1102 (this_item == &add_to_faves_item)
1105 /* requires an actual file */
1106 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1108 #if LCD_DEPTH > 1
1109 else if (this_item == &set_backdrop_item)
1111 if (selected_file)
1113 char *suffix = strrchr(selected_file, '.');
1114 if (suffix)
1116 if (strcasecmp(suffix, ".bmp") == 0)
1118 return action;
1122 return ACTION_EXIT_MENUITEM;
1124 #endif
1125 else if ((selected_file_attr & ATTR_DIRECTORY))
1127 if ((this_item == &delete_dir_item)
1129 return action;
1130 #ifdef HAVE_RECORDING
1131 else if (this_item == &set_recdir_item)
1132 return action;
1133 #endif
1135 else if (selected_file
1136 #ifdef HAVE_MULTIVOLUME
1137 /* no rename+delete for volumes */
1138 && !(selected_file_attr & ATTR_VOLUME)
1139 #endif
1142 if ((this_item == &delete_file_item) ||
1143 (this_item == &list_viewers_item))
1145 return action;
1148 return ACTION_EXIT_MENUITEM;
1149 break;
1151 return action;
1153 /* used when onplay() is called in the CONTEXT_WPS context */
1156 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1157 onplaymenu_callback, Icon_Audio,
1158 &wps_playlist_menu, &cat_playlist_menu,
1159 &sound_settings, &playback_settings,
1160 #ifdef HAVE_TAGCACHE
1161 &rating_item,
1162 #endif
1163 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1164 &delete_file_item, &view_cue_item,
1165 #ifdef HAVE_PITCHSCREEN
1166 &pitch_screen_item,
1167 #endif
1169 /* used when onplay() is not called in the CONTEXT_WPS context */
1170 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1171 onplaymenu_callback, Icon_file_view_menu,
1172 &tree_playlist_menu, &cat_playlist_menu,
1173 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1174 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1175 #if LCD_DEPTH > 1
1176 &set_backdrop_item,
1177 #endif
1178 &list_viewers_item, &create_dir_item, &properties_item,
1179 #ifdef HAVE_RECORDING
1180 &set_recdir_item,
1181 #endif
1182 &add_to_faves_item,
1184 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1186 (void)this_item;
1187 switch (action)
1189 case ACTION_TREE_STOP:
1190 if (this_item == &wps_onplay_menu)
1192 list_stop_handler();
1193 return ACTION_STD_CANCEL;
1195 break;
1196 case ACTION_EXIT_MENUITEM:
1197 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1198 break;
1200 return action;
1202 int onplay(char* file, int attr, int from)
1204 const struct menu_item_ex *menu;
1205 onplay_result = ONPLAY_OK;
1206 context = from;
1207 selected_file = file;
1208 selected_file_attr = attr;
1209 if (context == CONTEXT_WPS)
1210 menu = &wps_onplay_menu;
1211 else
1212 menu = &tree_onplay_menu;
1213 switch (do_menu(menu, NULL, NULL, false))
1215 case GO_TO_WPS:
1216 return ONPLAY_START_PLAY;
1217 case GO_TO_ROOT:
1218 case GO_TO_MAINMENU:
1219 return ONPLAY_MAINMENU;
1220 default:
1221 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;