jpeg,png: some minor changes.
[kugel-rb.git] / apps / onplay.c
blob8bb71e15558862b08331c32375caf40e1419aece
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 "statusbar-skinned.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 if (((intptr_t)param == PLAYLIST_REPLACE) && !warn_on_pl_erase())
321 return 0;
322 add_to_playlist((intptr_t)param, false);
323 return 0;
325 static int playlist_queue_func(void *param)
327 add_to_playlist((intptr_t)param, true);
328 return 0;
330 static int treeplaylist_wplayback_callback(int action,
331 const struct menu_item_ex*
332 this_item)
334 (void)this_item;
335 switch (action)
337 case ACTION_REQUEST_MENUITEM:
338 if (audio_status() & AUDIO_STATUS_PLAY)
339 return action;
340 else
341 return ACTION_EXIT_MENUITEM;
342 break;
344 return action;
347 static int treeplaylist_callback(int action,
348 const struct menu_item_ex *this_item);
350 /* insert items */
351 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
352 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
353 treeplaylist_callback, Icon_Playlist);
354 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
355 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
356 treeplaylist_wplayback_callback, Icon_Playlist);
357 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
358 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
359 treeplaylist_wplayback_callback, Icon_Playlist);
360 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
361 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
362 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
363 Icon_Playlist);
364 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
365 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
366 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED, treeplaylist_callback,
367 Icon_Playlist);
368 /* queue items */
369 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
370 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
371 treeplaylist_wplayback_callback, Icon_Playlist);
372 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
373 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
374 treeplaylist_wplayback_callback, Icon_Playlist);
375 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
376 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
377 treeplaylist_wplayback_callback, Icon_Playlist);
378 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
379 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
380 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
381 treeplaylist_wplayback_callback, Icon_Playlist);
382 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
383 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
384 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
385 treeplaylist_callback, Icon_Playlist);
386 /* replace playlist */
387 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
388 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
389 treeplaylist_wplayback_callback, Icon_Playlist);
390 /* others */
392 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
393 view_playlist, NULL,
394 treeplaylist_callback, Icon_Playlist);
396 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
397 treeplaylist_callback, Icon_Playlist,
399 /* view */
400 &view_playlist_item,
402 /* insert */
403 &i_pl_item, &i_first_pl_item,
404 &i_last_pl_item, &i_shuf_pl_item,
405 &i_last_shuf_pl_item,
407 /* queue */
408 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
409 &q_shuf_pl_item,
410 &q_last_shuf_pl_item,
412 /* replace */
413 &replace_pl_item
415 static int treeplaylist_callback(int action,
416 const struct menu_item_ex *this_item)
418 (void)this_item;
419 switch (action)
421 case ACTION_REQUEST_MENUITEM:
422 if (this_item == &tree_playlist_menu)
424 if (((selected_file_attr & FILE_ATTR_MASK) ==
425 FILE_ATTR_AUDIO) ||
426 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
427 (selected_file_attr & ATTR_DIRECTORY))
429 return action;
431 else
432 return ACTION_EXIT_MENUITEM;
434 else if (this_item == &view_playlist_item)
436 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
437 context == CONTEXT_TREE)
438 return action;
439 else
440 return ACTION_EXIT_MENUITEM;
442 else if (this_item == &i_shuf_pl_item)
444 if (audio_status() & AUDIO_STATUS_PLAY)
446 return action;
448 else if ((this_item == &i_shuf_pl_item) &&
449 ((selected_file_attr & ATTR_DIRECTORY) ||
450 ((selected_file_attr & FILE_ATTR_MASK) ==
451 FILE_ATTR_M3U)))
453 return action;
455 return ACTION_EXIT_MENUITEM;
457 else if (this_item == &i_last_shuf_pl_item ||
458 this_item == &q_last_shuf_pl_item)
460 if ((playlist_amount() > 0) &&
461 (audio_status() & AUDIO_STATUS_PLAY) &&
462 ((selected_file_attr & ATTR_DIRECTORY) ||
463 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
465 return action;
467 else
468 return ACTION_EXIT_MENUITEM;
470 break;
472 return action;
475 /* helper function to remove a non-empty directory */
476 static int remove_dir(char* dirname, int len)
478 int result = 0;
479 DIR* dir;
480 int dirlen = strlen(dirname);
482 dir = opendir(dirname);
483 if (!dir)
484 return -1; /* open error */
486 while(true)
488 struct dirent* entry;
489 /* walk through the directory content */
490 entry = readdir(dir);
491 if (!entry)
492 break;
494 dirname[dirlen] ='\0';
495 splash(0, dirname);
497 /* append name to current directory */
498 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
499 if (entry->attribute & ATTR_DIRECTORY)
500 { /* remove a subdirectory */
501 if (!strcmp((char *)entry->d_name, ".") ||
502 !strcmp((char *)entry->d_name, ".."))
503 continue; /* skip these */
505 /* inform the user which dir we're deleting */
507 result = remove_dir(dirname, len); /* recursion */
508 if (result)
509 break; /* or better continue, delete what we can? */
511 else
512 { /* remove a file */
513 draw_slider();
514 result = remove(dirname);
516 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
518 splash(HZ, ID2P(LANG_CANCEL));
519 result = -1;
520 break;
523 closedir(dir);
525 if (!result)
526 { /* remove the now empty directory */
527 dirname[dirlen] = '\0'; /* terminate to original length */
529 result = rmdir(dirname);
532 return result;
536 /* share code for file and directory deletion, saves space */
537 static bool delete_handler(bool is_dir)
539 char file_to_delete[MAX_PATH];
540 strcpy(file_to_delete, selected_file);
542 const char *lines[]={
543 ID2P(LANG_REALLY_DELETE),
544 file_to_delete
546 const char *yes_lines[]={
547 ID2P(LANG_DELETING),
548 file_to_delete
551 const struct text_message message={lines, 2};
552 const struct text_message yes_message={yes_lines, 2};
554 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
555 return false;
557 splash(0, str(LANG_DELETING));
559 int res;
560 if (is_dir)
562 char pathname[MAX_PATH]; /* space to go deep */
563 cpu_boost(true);
564 strlcpy(pathname, file_to_delete, sizeof(pathname));
565 res = remove_dir(pathname, sizeof(pathname));
566 cpu_boost(false);
568 else
569 res = remove(file_to_delete);
571 if (!res)
572 onplay_result = ONPLAY_RELOAD_DIR;
574 return (res == 0);
578 static bool delete_file(void)
580 return delete_handler(false);
583 static bool delete_dir(void)
585 return delete_handler(true);
588 #if LCD_DEPTH > 1
589 static bool set_backdrop(void)
591 /* load the image */
592 if(sb_set_backdrop(SCREEN_MAIN, selected_file)) {
593 splash(HZ, str(LANG_BACKDROP_LOADED));
594 set_file(selected_file, (char *)global_settings.backdrop_file,
595 MAX_FILENAME);
596 return true;
597 } else {
598 splash(HZ, str(LANG_BACKDROP_FAILED));
599 return false;
601 return true;
603 #endif
605 static bool rename_file(void)
607 char newname[MAX_PATH];
608 char* ptr = strrchr(selected_file, '/') + 1;
609 int pathlen = (ptr - selected_file);
610 strlcpy(newname, selected_file, sizeof(newname));
611 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
612 if (!strlen(newname + pathlen) ||
613 (rename(selected_file, newname) < 0)) {
614 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
615 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
617 else
618 onplay_result = ONPLAY_RELOAD_DIR;
621 return false;
624 static bool create_dir(void)
626 char dirname[MAX_PATH];
627 char *cwd;
628 int rc;
629 int pathlen;
631 cwd = getcwd(NULL, 0);
632 memset(dirname, 0, sizeof dirname);
634 snprintf(dirname, sizeof dirname, "%s/",
635 cwd[1] ? cwd : "");
637 pathlen = strlen(dirname);
638 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
639 if (rc < 0)
640 return false;
642 rc = mkdir(dirname);
643 if (rc < 0) {
644 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
645 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
646 str(LANG_FAILED));
647 } else {
648 onplay_result = ONPLAY_RELOAD_DIR;
651 return true;
654 static bool properties(void)
656 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
657 selected_file))
658 onplay_result = ONPLAY_RELOAD_DIR;
659 return false;
662 /* Store the current selection in the clipboard */
663 static bool clipboard_clip(bool copy)
665 clipboard_selection[0] = 0;
666 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
667 clipboard_selection_attr = selected_file_attr;
668 clipboard_is_copy = copy;
670 return true;
673 static bool clipboard_cut(void)
675 return clipboard_clip(false);
678 static bool clipboard_copy(void)
680 return clipboard_clip(true);
683 #ifdef HAVE_LCD_BITMAP
684 static void draw_slider(void)
686 int i;
687 FOR_NB_SCREENS(i)
689 struct viewport vp;
690 viewport_set_defaults(&vp, i);
691 screens[i].set_viewport(&vp);
692 show_busy_slider(&screens[i], vp.x,
693 (vp.y+vp.height)-2*screens[i].getcharheight(),
694 vp.width, 2*screens[i].getcharheight()-1);
695 screens[i].update();
698 #endif
700 /* Paste a file to a new directory. Will overwrite always. */
701 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
703 int src_fd, target_fd;
704 size_t buffersize;
705 ssize_t size, bytesread, byteswritten;
706 char *buffer;
707 bool result = false;
709 if (copy) {
710 /* See if we can get the plugin buffer for the file copy buffer */
711 buffer = (char *) plugin_get_buffer(&buffersize);
712 if (buffer == NULL || buffersize < 512) {
713 /* Not large enough, try for a disk sector worth of stack
714 instead */
715 buffersize = 512;
716 buffer = (char *) __builtin_alloca(buffersize);
719 if (buffer == NULL) {
720 return false;
723 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
724 size */
726 src_fd = open(src, O_RDONLY);
728 if (src_fd >= 0) {
729 target_fd = creat(target);
731 if (target_fd >= 0) {
732 result = true;
734 size = filesize(src_fd);
736 if (size == -1) {
737 result = false;
740 while(size > 0) {
741 bytesread = read(src_fd, buffer, buffersize);
743 if (bytesread == -1) {
744 result = false;
745 break;
748 size -= bytesread;
750 while(bytesread > 0) {
751 byteswritten = write(target_fd, buffer, bytesread);
753 if (byteswritten < 0) {
754 result = false;
755 size = 0;
756 break;
759 bytesread -= byteswritten;
760 draw_slider();
764 close(target_fd);
766 /* Copy failed. Cleanup. */
767 if (!result) {
768 remove(target);
772 close(src_fd);
774 } else {
775 result = rename(src, target) == 0;
776 #ifdef HAVE_MULTIVOLUME
777 if (!result) {
778 if (errno == EXDEV) {
779 /* Failed because cross volume rename doesn't work. Copy
780 instead */
781 result = clipboard_pastefile(src, target, true);
783 if (result) {
784 result = remove(src) == 0;
788 #endif
791 return result;
794 /* Paste a directory to a new location. Designed to be called by
795 clipboard_paste */
796 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
797 int targetlen, bool copy)
799 DIR *srcdir;
800 int srcdirlen = strlen(src);
801 int targetdirlen = strlen(target);
802 bool result = true;
804 if (!file_exists(target)) {
805 if (!copy) {
806 /* Just move the directory */
807 result = rename(src, target) == 0;
809 #ifdef HAVE_MULTIVOLUME
810 if (!result && errno == EXDEV) {
811 /* Try a copy as we're going across devices */
812 result = clipboard_pastedirectory(src, srclen, target,
813 targetlen, true);
815 /* If it worked, remove the source directory */
816 if (result) {
817 remove_dir(src, srclen);
820 #endif
821 return result;
822 } else {
823 /* Make a directory to copy things to */
824 result = mkdir(target) == 0;
828 /* Check if something went wrong already */
829 if (!result) {
830 return result;
833 srcdir = opendir(src);
834 if (!srcdir) {
835 return false;
838 /* This loop will exit as soon as there's a problem */
839 while(result)
841 struct dirent* entry;
842 /* walk through the directory content */
843 entry = readdir(srcdir);
844 if (!entry)
845 break;
847 /* append name to current directory */
848 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
849 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
850 entry->d_name);
852 DEBUGF("Copy %s to %s\n", src, target);
854 if (entry->attribute & ATTR_DIRECTORY)
855 { /* copy/move a subdirectory */
856 if (!strcmp((char *)entry->d_name, ".") ||
857 !strcmp((char *)entry->d_name, ".."))
858 continue; /* skip these */
860 result = clipboard_pastedirectory(src, srclen, target, targetlen,
861 copy); /* recursion */
863 else
864 { /* copy/move a file */
865 result = clipboard_pastefile(src, target, copy);
869 closedir(srcdir);
871 if (result) {
872 src[srcdirlen] = '\0'; /* terminate to original length */
873 target[targetdirlen] = '\0'; /* terminate to original length */
876 return result;
879 /* Paste the clipboard to the current directory */
880 static bool clipboard_paste(void)
882 char target[MAX_PATH];
883 char *cwd, *nameptr;
884 bool success;
886 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
887 static const struct text_message message={lines, 1};
889 /* Get the name of the current directory */
890 cwd = getcwd(NULL, 0);
892 /* Figure out the name of the selection */
893 nameptr = strrchr(clipboard_selection, '/');
895 /* Final target is current directory plus name of selection */
896 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
898 /* If the target existed but they choose not to overwite, exit */
899 if (file_exists(target) &&
900 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
901 return false;
904 if (clipboard_is_copy) {
905 splash(0, ID2P(LANG_COPYING));
907 else
909 splash(0, ID2P(LANG_MOVING));
912 /* Now figure out what we're doing */
913 cpu_boost(true);
914 if (clipboard_selection_attr & ATTR_DIRECTORY) {
915 /* Recursion. Set up external stack */
916 char srcpath[MAX_PATH];
917 char targetpath[MAX_PATH];
918 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
920 /* Do not allow the user to paste a directory into a dir they are
921 copying */
922 success = 0;
924 else
926 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
927 strlcpy(targetpath, target, sizeof(targetpath));
929 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
930 target, sizeof(targetpath), clipboard_is_copy);
932 if (success && !clipboard_is_copy)
934 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
935 remove_dir(srcpath, sizeof(srcpath));
938 } else {
939 success = clipboard_pastefile(clipboard_selection, target,
940 clipboard_is_copy);
942 cpu_boost(false);
944 /* Did it work? */
945 if (success) {
946 /* Reset everything */
947 clipboard_selection[0] = 0;
948 clipboard_selection_attr = 0;
949 clipboard_is_copy = false;
951 /* Force reload of the current directory */
952 onplay_result = ONPLAY_RELOAD_DIR;
953 } else {
954 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
955 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
956 str(LANG_FAILED));
959 return true;
962 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
963 #ifdef HAVE_TAGCACHE
964 static int set_rating_inline(void)
966 struct mp3entry* id3 = audio_current_track();
967 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
969 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
970 NULL, 1, 0, 10, NULL, NULL);
971 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
973 else
974 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
975 return 0;
977 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
979 (void)this_item;
980 switch (action)
982 case ACTION_REQUEST_MENUITEM:
983 if (!selected_file || !global_settings.runtimedb ||
984 !tagcache_is_usable())
985 return ACTION_EXIT_MENUITEM;
986 break;
988 return action;
990 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
991 set_rating_inline, NULL,
992 ratingitem_callback, Icon_Questionmark);
993 #endif
995 static bool view_cue(void)
997 struct mp3entry* id3 = audio_current_track();
998 if(id3 && id3->cuesheet)
1000 browse_cuesheet(id3->cuesheet);
1002 return false;
1004 static int view_cue_item_callback(int action,
1005 const struct menu_item_ex *this_item)
1007 (void)this_item;
1008 struct mp3entry* id3 = audio_current_track();
1009 switch (action)
1011 case ACTION_REQUEST_MENUITEM:
1012 if (!selected_file
1013 || !id3 || !id3->cuesheet)
1014 return ACTION_EXIT_MENUITEM;
1015 break;
1017 return action;
1019 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1020 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1022 /* CONTEXT_WPS items */
1023 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1024 browse_id3, NULL, NULL, Icon_NOICON);
1025 #ifdef HAVE_PITCHSCREEN
1026 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1027 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1028 #endif
1030 /* CONTEXT_[TREE|ID3DB] items */
1031 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1032 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1033 rename_file, NULL, clipboard_callback, Icon_NOICON);
1034 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1035 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1036 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1037 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1038 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1039 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1040 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1041 delete_file, NULL, clipboard_callback, Icon_NOICON);
1042 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1043 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1044 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1045 properties, NULL, clipboard_callback, Icon_NOICON);
1046 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1047 create_dir, NULL, clipboard_callback, Icon_NOICON);
1048 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1049 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1050 #if LCD_DEPTH > 1
1051 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1052 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1053 #endif
1054 #ifdef HAVE_RECORDING
1055 static bool set_recdir(void)
1057 strlcpy(global_settings.rec_directory,
1058 selected_file, MAX_FILENAME+1);
1059 settings_save();
1060 return false;
1062 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1063 set_recdir, NULL, clipboard_callback, Icon_Recording);
1064 #endif
1065 static bool add_to_faves(void)
1067 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1068 selected_file))
1069 onplay_result = ONPLAY_RELOAD_DIR;
1070 return false;
1072 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1073 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1076 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1078 switch (action)
1080 case ACTION_REQUEST_MENUITEM:
1081 #ifdef HAVE_MULTIVOLUME
1082 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1083 (this_item == &rename_file_item ||
1084 this_item == &delete_dir_item ||
1085 this_item == &clipboard_cut_item) )
1086 return ACTION_EXIT_MENUITEM;
1087 #endif
1088 if (context == CONTEXT_ID3DB)
1089 return ACTION_EXIT_MENUITEM;
1090 if (this_item == &clipboard_paste_item)
1091 { /* visible if there is something to paste */
1092 return (clipboard_selection[0] != 0) ?
1093 action : ACTION_EXIT_MENUITEM;
1095 else if (this_item == &create_dir_item)
1097 /* always visible */
1098 return action;
1100 else if ((this_item == &properties_item) ||
1101 (this_item == &rename_file_item) ||
1102 (this_item == &clipboard_cut_item) ||
1103 (this_item == &clipboard_copy_item) ||
1104 (this_item == &add_to_faves_item)
1107 /* requires an actual file */
1108 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1110 #if LCD_DEPTH > 1
1111 else if (this_item == &set_backdrop_item)
1113 if (selected_file)
1115 char *suffix = strrchr(selected_file, '.');
1116 if (suffix)
1118 if (strcasecmp(suffix, ".bmp") == 0)
1120 return action;
1124 return ACTION_EXIT_MENUITEM;
1126 #endif
1127 else if ((selected_file_attr & ATTR_DIRECTORY))
1129 if ((this_item == &delete_dir_item)
1131 return action;
1132 #ifdef HAVE_RECORDING
1133 else if (this_item == &set_recdir_item)
1134 return action;
1135 #endif
1137 else if (selected_file
1138 #ifdef HAVE_MULTIVOLUME
1139 /* no rename+delete for volumes */
1140 && !(selected_file_attr & ATTR_VOLUME)
1141 #endif
1144 if ((this_item == &delete_file_item) ||
1145 (this_item == &list_viewers_item))
1147 return action;
1150 return ACTION_EXIT_MENUITEM;
1151 break;
1153 return action;
1155 /* used when onplay() is called in the CONTEXT_WPS context */
1158 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1159 onplaymenu_callback, Icon_Audio,
1160 &wps_playlist_menu, &cat_playlist_menu,
1161 &sound_settings, &playback_settings,
1162 #ifdef HAVE_TAGCACHE
1163 &rating_item,
1164 #endif
1165 &bookmark_menu, &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 &add_to_faves_item,
1186 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1188 (void)this_item;
1189 switch (action)
1191 case ACTION_TREE_STOP:
1192 if (this_item == &wps_onplay_menu)
1194 list_stop_handler();
1195 return ACTION_STD_CANCEL;
1197 break;
1198 case ACTION_EXIT_MENUITEM:
1199 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1200 break;
1202 return action;
1204 int onplay(char* file, int attr, int from)
1206 const struct menu_item_ex *menu;
1207 onplay_result = ONPLAY_OK;
1208 context = from;
1209 selected_file = file;
1210 selected_file_attr = attr;
1211 if (context == CONTEXT_WPS)
1212 menu = &wps_onplay_menu;
1213 else
1214 menu = &tree_onplay_menu;
1215 switch (do_menu(menu, NULL, NULL, false))
1217 case GO_TO_WPS:
1218 return ONPLAY_START_PLAY;
1219 case GO_TO_ROOT:
1220 case GO_TO_MAINMENU:
1221 return ONPLAY_MAINMENU;
1222 default:
1223 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;