Forgot to remove wps_data.remove_wps in checkwps too.
[kugel-rb.git] / apps / onplay.c
blob88961a459eb31a36b622b6f3aaf6a160c332d46d
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 ||
177 position == PLAYLIST_INSERT_LAST_SHUFFLED)
179 srand(current_tick);
180 if (position == PLAYLIST_INSERT_LAST_SHUFFLED)
181 playlist_set_last_shuffled_start();
184 #ifdef HAVE_TAGCACHE
185 if (context == CONTEXT_ID3DB)
187 tagtree_insert_selection_playlist(position, queue);
189 else
190 #endif
192 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
193 playlist_insert_track(NULL, selected_file, position, queue, true);
194 else if (selected_file_attr & ATTR_DIRECTORY)
196 bool recurse = false;
198 if (global_settings.recursive_dir_insert != RECURSE_ASK)
199 recurse = (bool)global_settings.recursive_dir_insert;
200 else
202 /* Ask if user wants to recurse directory */
203 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
206 playlist_insert_directory(NULL, selected_file, position, queue,
207 recurse);
209 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
210 playlist_insert_playlist(NULL, selected_file, position, queue);
213 if (new_playlist && (playlist_amount() > 0))
215 /* nothing is currently playing so begin playing what we just
216 inserted */
217 if (global_settings.playlist_shuffle)
218 playlist_shuffle(current_tick, -1);
219 playlist_start(0,0);
220 onplay_result = ONPLAY_START_PLAY;
223 return false;
226 static bool view_playlist(void)
228 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
229 bool result;
231 result = playlist_viewer_ex(selected_file);
233 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
234 onplay_result == ONPLAY_OK)
235 /* playlist was started from viewer */
236 onplay_result = ONPLAY_START_PLAY;
238 return result;
241 static bool cat_add_to_a_playlist(void)
243 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
244 false, NULL);
247 static bool cat_add_to_a_new_playlist(void)
249 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
250 true, NULL);
254 static int cat_playlist_callback(int action,
255 const struct menu_item_ex *this_item);
256 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
257 catalog_view_playlists, 0, cat_playlist_callback,
258 Icon_Playlist);
259 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
260 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
261 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
262 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
263 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
264 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
265 &cat_add_to_new);
267 static int cat_playlist_callback(int action,
268 const struct menu_item_ex *this_item)
270 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
271 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
272 ((selected_file_attr & ATTR_DIRECTORY) == 0))
274 return ACTION_EXIT_MENUITEM;
277 switch (action)
279 case ACTION_REQUEST_MENUITEM:
280 if (this_item == &cat_view_lists)
282 if (context == CONTEXT_WPS)
283 return action;
285 else if (selected_file && /* set before calling this menu,
286 so safe */
287 ((audio_status() & AUDIO_STATUS_PLAY &&
288 context == CONTEXT_WPS) ||
289 context == CONTEXT_TREE))
291 return action;
293 else
294 return ACTION_EXIT_MENUITEM;
295 break;
297 return action;
301 /* CONTEXT_WPS playlist options */
302 MENUITEM_FUNCTION(playlist_viewer_item, 0,
303 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
304 NULL, NULL, Icon_Playlist);
305 MENUITEM_FUNCTION(search_playlist_item, 0,
306 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
307 NULL, NULL, Icon_Playlist);
308 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
309 save_playlist, NULL, NULL, Icon_Playlist);
310 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
311 shuffle_playlist, NULL, NULL, Icon_Playlist);
312 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
313 NULL, Icon_Playlist,
314 &playlist_viewer_item, &search_playlist_item,
315 &playlist_save_item, &reshuffle_item
318 /* CONTEXT_[TREE|ID3DB] playlist options */
319 static int playlist_insert_func(void *param)
321 add_to_playlist((intptr_t)param, false);
322 return 0;
324 static int playlist_queue_func(void *param)
326 add_to_playlist((intptr_t)param, true);
327 return 0;
329 static int treeplaylist_wplayback_callback(int action,
330 const struct menu_item_ex*
331 this_item)
333 (void)this_item;
334 switch (action)
336 case ACTION_REQUEST_MENUITEM:
337 if (audio_status() & AUDIO_STATUS_PLAY)
338 return action;
339 else
340 return ACTION_EXIT_MENUITEM;
341 break;
343 return action;
346 static int treeplaylist_callback(int action,
347 const struct menu_item_ex *this_item);
349 /* insert items */
350 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
351 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
352 treeplaylist_callback, Icon_Playlist);
353 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
354 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
355 treeplaylist_wplayback_callback, Icon_Playlist);
356 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
357 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
358 treeplaylist_wplayback_callback, Icon_Playlist);
359 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
360 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
361 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
362 Icon_Playlist);
363 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
364 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
365 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED, treeplaylist_callback,
366 Icon_Playlist);
367 /* queue items */
368 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
369 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
370 treeplaylist_wplayback_callback, Icon_Playlist);
371 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
372 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
373 treeplaylist_wplayback_callback, Icon_Playlist);
374 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
375 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
376 treeplaylist_wplayback_callback, Icon_Playlist);
377 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
378 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
379 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
380 treeplaylist_wplayback_callback, Icon_Playlist);
381 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
382 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
383 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
384 treeplaylist_callback, Icon_Playlist);
385 /* replace playlist */
386 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
387 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
388 treeplaylist_wplayback_callback, Icon_Playlist);
389 /* others */
391 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
392 view_playlist, NULL,
393 treeplaylist_callback, Icon_Playlist);
395 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
396 treeplaylist_callback, Icon_Playlist,
398 /* view */
399 &view_playlist_item,
401 /* insert */
402 &i_pl_item, &i_first_pl_item,
403 &i_last_pl_item, &i_shuf_pl_item,
404 &i_last_shuf_pl_item,
406 /* queue */
407 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
408 &q_shuf_pl_item,
409 &q_last_shuf_pl_item,
411 /* replace */
412 &replace_pl_item
414 static int treeplaylist_callback(int action,
415 const struct menu_item_ex *this_item)
417 (void)this_item;
418 switch (action)
420 case ACTION_REQUEST_MENUITEM:
421 if (this_item == &tree_playlist_menu)
423 if (((selected_file_attr & FILE_ATTR_MASK) ==
424 FILE_ATTR_AUDIO) ||
425 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
426 (selected_file_attr & ATTR_DIRECTORY))
428 return action;
430 else
431 return ACTION_EXIT_MENUITEM;
433 else if (this_item == &view_playlist_item)
435 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
436 context == CONTEXT_TREE)
437 return action;
438 else
439 return ACTION_EXIT_MENUITEM;
441 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 ||
457 this_item == &q_last_shuf_pl_item)
459 if ((playlist_amount() > 0) &&
460 (audio_status() & AUDIO_STATUS_PLAY) &&
461 ((selected_file_attr & ATTR_DIRECTORY) ||
462 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
464 return action;
466 else
467 return ACTION_EXIT_MENUITEM;
469 break;
471 return action;
474 /* helper function to remove a non-empty directory */
475 static int remove_dir(char* dirname, int len)
477 int result = 0;
478 DIR* dir;
479 int dirlen = strlen(dirname);
481 dir = opendir(dirname);
482 if (!dir)
483 return -1; /* open error */
485 while(true)
487 struct dirent* entry;
488 /* walk through the directory content */
489 entry = readdir(dir);
490 if (!entry)
491 break;
493 dirname[dirlen] ='\0';
494 splash(0, dirname);
496 /* append name to current directory */
497 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
498 if (entry->attribute & ATTR_DIRECTORY)
499 { /* remove a subdirectory */
500 if (!strcmp((char *)entry->d_name, ".") ||
501 !strcmp((char *)entry->d_name, ".."))
502 continue; /* skip these */
504 /* inform the user which dir we're deleting */
506 result = remove_dir(dirname, len); /* recursion */
507 if (result)
508 break; /* or better continue, delete what we can? */
510 else
511 { /* remove a file */
512 draw_slider();
513 result = remove(dirname);
515 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
517 splash(HZ, ID2P(LANG_CANCEL));
518 result = -1;
519 break;
522 closedir(dir);
524 if (!result)
525 { /* remove the now empty directory */
526 dirname[dirlen] = '\0'; /* terminate to original length */
528 result = rmdir(dirname);
531 return result;
535 /* share code for file and directory deletion, saves space */
536 static bool delete_handler(bool is_dir)
538 char file_to_delete[MAX_PATH];
539 strcpy(file_to_delete, selected_file);
541 const char *lines[]={
542 ID2P(LANG_REALLY_DELETE),
543 file_to_delete
545 const char *yes_lines[]={
546 ID2P(LANG_DELETING),
547 file_to_delete
550 const struct text_message message={lines, 2};
551 const struct text_message yes_message={yes_lines, 2};
553 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
554 return false;
556 splash(0, str(LANG_DELETING));
558 int res;
559 if (is_dir)
561 char pathname[MAX_PATH]; /* space to go deep */
562 cpu_boost(true);
563 strlcpy(pathname, file_to_delete, sizeof(pathname));
564 res = remove_dir(pathname, sizeof(pathname));
565 cpu_boost(false);
567 else
568 res = remove(file_to_delete);
570 if (!res)
571 onplay_result = ONPLAY_RELOAD_DIR;
573 return false;
577 static bool delete_file(void)
579 return delete_handler(false);
582 static bool delete_dir(void)
584 return delete_handler(true);
587 #if LCD_DEPTH > 1
588 static bool set_backdrop(void)
590 /* load the image */
591 if(backdrop_load(BACKDROP_MAIN, selected_file)) {
592 splash(HZ, str(LANG_BACKDROP_LOADED));
593 set_file(selected_file, (char *)global_settings.backdrop_file,
594 MAX_FILENAME);
595 backdrop_show(BACKDROP_MAIN);
596 return true;
597 } else {
598 splash(HZ, str(LANG_BACKDROP_FAILED));
599 return false;
602 #endif
604 static bool rename_file(void)
606 char newname[MAX_PATH];
607 char* ptr = strrchr(selected_file, '/') + 1;
608 int pathlen = (ptr - selected_file);
609 strlcpy(newname, selected_file, sizeof(newname));
610 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
611 if (!strlen(newname + pathlen) ||
612 (rename(selected_file, newname) < 0)) {
613 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
614 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
616 else
617 onplay_result = ONPLAY_RELOAD_DIR;
620 return false;
623 static bool create_dir(void)
625 char dirname[MAX_PATH];
626 char *cwd;
627 int rc;
628 int pathlen;
630 cwd = getcwd(NULL, 0);
631 memset(dirname, 0, sizeof dirname);
633 snprintf(dirname, sizeof dirname, "%s/",
634 cwd[1] ? cwd : "");
636 pathlen = strlen(dirname);
637 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
638 if (rc < 0)
639 return false;
641 rc = mkdir(dirname);
642 if (rc < 0) {
643 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
644 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
645 str(LANG_FAILED));
646 } else {
647 onplay_result = ONPLAY_RELOAD_DIR;
650 return true;
653 static bool properties(void)
655 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
656 selected_file))
657 onplay_result = ONPLAY_RELOAD_DIR;
658 return false;
661 /* Store the current selection in the clipboard */
662 static bool clipboard_clip(bool copy)
664 clipboard_selection[0] = 0;
665 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
666 clipboard_selection_attr = selected_file_attr;
667 clipboard_is_copy = copy;
669 return true;
672 static bool clipboard_cut(void)
674 return clipboard_clip(false);
677 static bool clipboard_copy(void)
679 return clipboard_clip(true);
682 #ifdef HAVE_LCD_BITMAP
683 static void draw_slider(void)
685 int i;
686 FOR_NB_SCREENS(i)
688 struct viewport *vp = &(viewport_get_current_vp())[i];
689 show_busy_slider(&screens[i], vp->x,
690 (vp->y+vp->height)-2*screens[i].getcharheight(),
691 vp->width, 2*screens[i].getcharheight()-1);
692 screens[i].update();
695 #endif
697 /* Paste a file to a new directory. Will overwrite always. */
698 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
700 int src_fd, target_fd;
701 size_t buffersize;
702 ssize_t size, bytesread, byteswritten;
703 char *buffer;
704 bool result = false;
706 if (copy) {
707 /* See if we can get the plugin buffer for the file copy buffer */
708 buffer = (char *) plugin_get_buffer(&buffersize);
709 if (buffer == NULL || buffersize < 512) {
710 /* Not large enough, try for a disk sector worth of stack
711 instead */
712 buffersize = 512;
713 buffer = (char *) __builtin_alloca(buffersize);
716 if (buffer == NULL) {
717 return false;
720 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
721 size */
723 src_fd = open(src, O_RDONLY);
725 if (src_fd >= 0) {
726 target_fd = creat(target);
728 if (target_fd >= 0) {
729 result = true;
731 size = filesize(src_fd);
733 if (size == -1) {
734 result = false;
737 while(size > 0) {
738 bytesread = read(src_fd, buffer, buffersize);
740 if (bytesread == -1) {
741 result = false;
742 break;
745 size -= bytesread;
747 while(bytesread > 0) {
748 byteswritten = write(target_fd, buffer, bytesread);
750 if (byteswritten < 0) {
751 result = false;
752 size = 0;
753 break;
756 bytesread -= byteswritten;
757 draw_slider();
761 close(target_fd);
763 /* Copy failed. Cleanup. */
764 if (!result) {
765 remove(target);
769 close(src_fd);
771 } else {
772 result = rename(src, target) == 0;
773 #ifdef HAVE_MULTIVOLUME
774 if (!result) {
775 if (errno == EXDEV) {
776 /* Failed because cross volume rename doesn't work. Copy
777 instead */
778 result = clipboard_pastefile(src, target, true);
780 if (result) {
781 result = remove(src) == 0;
785 #endif
788 return result;
791 /* Paste a directory to a new location. Designed to be called by
792 clipboard_paste */
793 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
794 int targetlen, bool copy)
796 DIR *srcdir;
797 int srcdirlen = strlen(src);
798 int targetdirlen = strlen(target);
799 bool result = true;
801 if (!file_exists(target)) {
802 if (!copy) {
803 /* Just move the directory */
804 result = rename(src, target) == 0;
806 #ifdef HAVE_MULTIVOLUME
807 if (!result && errno == EXDEV) {
808 /* Try a copy as we're going across devices */
809 result = clipboard_pastedirectory(src, srclen, target,
810 targetlen, true);
812 /* If it worked, remove the source directory */
813 if (result) {
814 remove_dir(src, srclen);
817 #endif
818 return result;
819 } else {
820 /* Make a directory to copy things to */
821 result = mkdir(target) == 0;
825 /* Check if something went wrong already */
826 if (!result) {
827 return result;
830 srcdir = opendir(src);
831 if (!srcdir) {
832 return false;
835 /* This loop will exit as soon as there's a problem */
836 while(result)
838 struct dirent* entry;
839 /* walk through the directory content */
840 entry = readdir(srcdir);
841 if (!entry)
842 break;
844 /* append name to current directory */
845 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
846 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
847 entry->d_name);
849 DEBUGF("Copy %s to %s\n", src, target);
851 if (entry->attribute & ATTR_DIRECTORY)
852 { /* copy/move a subdirectory */
853 if (!strcmp((char *)entry->d_name, ".") ||
854 !strcmp((char *)entry->d_name, ".."))
855 continue; /* skip these */
857 result = clipboard_pastedirectory(src, srclen, target, targetlen,
858 copy); /* recursion */
860 else
861 { /* copy/move a file */
862 result = clipboard_pastefile(src, target, copy);
866 closedir(srcdir);
868 if (result) {
869 src[srcdirlen] = '\0'; /* terminate to original length */
870 target[targetdirlen] = '\0'; /* terminate to original length */
873 return result;
876 /* Paste the clipboard to the current directory */
877 static bool clipboard_paste(void)
879 char target[MAX_PATH];
880 char *cwd, *nameptr;
881 bool success;
883 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
884 static const struct text_message message={lines, 1};
886 /* Get the name of the current directory */
887 cwd = getcwd(NULL, 0);
889 /* Figure out the name of the selection */
890 nameptr = strrchr(clipboard_selection, '/');
892 /* Final target is current directory plus name of selection */
893 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
895 /* If the target existed but they choose not to overwite, exit */
896 if (file_exists(target) &&
897 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
898 return false;
901 if (clipboard_is_copy) {
902 splash(0, ID2P(LANG_COPYING));
904 else
906 splash(0, ID2P(LANG_MOVING));
909 /* Now figure out what we're doing */
910 cpu_boost(true);
911 if (clipboard_selection_attr & ATTR_DIRECTORY) {
912 /* Recursion. Set up external stack */
913 char srcpath[MAX_PATH];
914 char targetpath[MAX_PATH];
915 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
917 /* Do not allow the user to paste a directory into a dir they are
918 copying */
919 success = 0;
921 else
923 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
924 strlcpy(targetpath, target, sizeof(targetpath));
926 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
927 target, sizeof(targetpath), clipboard_is_copy);
929 if (success && !clipboard_is_copy)
931 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
932 remove_dir(srcpath, sizeof(srcpath));
935 } else {
936 success = clipboard_pastefile(clipboard_selection, target,
937 clipboard_is_copy);
939 cpu_boost(false);
941 /* Did it work? */
942 if (success) {
943 /* Reset everything */
944 clipboard_selection[0] = 0;
945 clipboard_selection_attr = 0;
946 clipboard_is_copy = false;
948 /* Force reload of the current directory */
949 onplay_result = ONPLAY_RELOAD_DIR;
950 } else {
951 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
952 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
953 str(LANG_FAILED));
956 return true;
959 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
960 #ifdef HAVE_TAGCACHE
961 static int set_rating_inline(void)
963 struct mp3entry* id3 = audio_current_track();
964 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
966 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
967 NULL, 1, 0, 10, NULL, NULL);
968 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
970 else
971 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
972 return 0;
974 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
976 (void)this_item;
977 switch (action)
979 case ACTION_REQUEST_MENUITEM:
980 if (!selected_file || !global_settings.runtimedb ||
981 !tagcache_is_usable())
982 return ACTION_EXIT_MENUITEM;
983 break;
985 return action;
987 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
988 set_rating_inline, NULL,
989 ratingitem_callback, Icon_Questionmark);
990 #endif
992 static bool view_cue(void)
994 struct mp3entry* id3 = audio_current_track();
995 if(id3 && id3->cuesheet)
997 browse_cuesheet(id3->cuesheet);
999 return false;
1001 static int view_cue_item_callback(int action,
1002 const struct menu_item_ex *this_item)
1004 (void)this_item;
1005 struct mp3entry* id3 = audio_current_track();
1006 switch (action)
1008 case ACTION_REQUEST_MENUITEM:
1009 if (!selected_file
1010 || !id3 || !id3->cuesheet)
1011 return ACTION_EXIT_MENUITEM;
1012 break;
1014 return action;
1016 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1017 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1019 /* CONTEXT_WPS items */
1020 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1021 browse_id3, NULL, NULL, Icon_NOICON);
1022 #ifdef HAVE_PITCHSCREEN
1023 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1024 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
1025 #endif
1027 /* CONTEXT_[TREE|ID3DB] items */
1028 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1029 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1030 rename_file, NULL, clipboard_callback, Icon_NOICON);
1031 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1032 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1033 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1034 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1035 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1036 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1037 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1038 delete_file, NULL, clipboard_callback, Icon_NOICON);
1039 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1040 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1041 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1042 properties, NULL, clipboard_callback, Icon_NOICON);
1043 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1044 create_dir, NULL, clipboard_callback, Icon_NOICON);
1045 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1046 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1047 #if LCD_DEPTH > 1
1048 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1049 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1050 #endif
1051 #ifdef HAVE_RECORDING
1052 static bool set_recdir(void)
1054 strlcpy(global_settings.rec_directory,
1055 selected_file, MAX_FILENAME+1);
1056 settings_save();
1057 return false;
1059 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1060 set_recdir, NULL, clipboard_callback, Icon_Recording);
1061 #endif
1062 static bool add_to_faves(void)
1064 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1065 selected_file))
1066 onplay_result = ONPLAY_RELOAD_DIR;
1067 return false;
1069 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1070 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1073 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1075 switch (action)
1077 case ACTION_REQUEST_MENUITEM:
1078 #ifdef HAVE_MULTIVOLUME
1079 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1080 (this_item == &rename_file_item ||
1081 this_item == &delete_dir_item ||
1082 this_item == &clipboard_cut_item) )
1083 return ACTION_EXIT_MENUITEM;
1084 #endif
1085 if (context == CONTEXT_ID3DB)
1086 return ACTION_EXIT_MENUITEM;
1087 if (this_item == &clipboard_paste_item)
1088 { /* visible if there is something to paste */
1089 return (clipboard_selection[0] != 0) ?
1090 action : ACTION_EXIT_MENUITEM;
1092 else if (this_item == &create_dir_item)
1094 /* always visible */
1095 return action;
1097 else if ((this_item == &properties_item) ||
1098 (this_item == &rename_file_item) ||
1099 (this_item == &clipboard_cut_item) ||
1100 (this_item == &clipboard_copy_item) ||
1101 (this_item == &add_to_faves_item)
1104 /* requires an actual file */
1105 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1107 #if LCD_DEPTH > 1
1108 else if (this_item == &set_backdrop_item)
1110 if (selected_file)
1112 char *suffix = strrchr(selected_file, '.');
1113 if (suffix)
1115 if (strcasecmp(suffix, ".bmp") == 0)
1117 return action;
1121 return ACTION_EXIT_MENUITEM;
1123 #endif
1124 else if ((selected_file_attr & ATTR_DIRECTORY))
1126 if ((this_item == &delete_dir_item)
1128 return action;
1129 #ifdef HAVE_RECORDING
1130 else if (this_item == &set_recdir_item)
1131 return action;
1132 #endif
1134 else if (selected_file
1135 #ifdef HAVE_MULTIVOLUME
1136 /* no rename+delete for volumes */
1137 && !(selected_file_attr & ATTR_VOLUME)
1138 #endif
1141 if ((this_item == &delete_file_item) ||
1142 (this_item == &list_viewers_item))
1144 return action;
1147 return ACTION_EXIT_MENUITEM;
1148 break;
1150 return action;
1152 /* used when onplay() is called in the CONTEXT_WPS context */
1155 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1156 onplaymenu_callback, Icon_Audio,
1157 &wps_playlist_menu, &cat_playlist_menu,
1158 &sound_settings, &playback_settings,
1159 #ifdef HAVE_TAGCACHE
1160 &rating_item,
1161 #endif
1162 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1163 &delete_file_item, &view_cue_item,
1164 #ifdef HAVE_PITCHSCREEN
1165 &pitch_screen_item,
1166 #endif
1168 /* used when onplay() is not called in the CONTEXT_WPS context */
1169 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1170 onplaymenu_callback, Icon_file_view_menu,
1171 &tree_playlist_menu, &cat_playlist_menu,
1172 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1173 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1174 #if LCD_DEPTH > 1
1175 &set_backdrop_item,
1176 #endif
1177 &list_viewers_item, &create_dir_item, &properties_item,
1178 #ifdef HAVE_RECORDING
1179 &set_recdir_item,
1180 #endif
1181 &add_to_faves_item,
1183 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1185 (void)this_item;
1186 switch (action)
1188 case ACTION_TREE_STOP:
1189 if (this_item == &wps_onplay_menu)
1191 list_stop_handler();
1192 return ACTION_STD_CANCEL;
1194 break;
1195 case ACTION_EXIT_MENUITEM:
1196 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1197 break;
1199 return action;
1201 int onplay(char* file, int attr, int from)
1203 const struct menu_item_ex *menu;
1204 onplay_result = ONPLAY_OK;
1205 context = from;
1206 selected_file = file;
1207 selected_file_attr = attr;
1208 if (context == CONTEXT_WPS)
1209 menu = &wps_onplay_menu;
1210 else
1211 menu = &tree_onplay_menu;
1212 switch (do_menu(menu, NULL, NULL, false))
1214 case GO_TO_WPS:
1215 return ONPLAY_START_PLAY;
1216 case GO_TO_ROOT:
1217 case GO_TO_MAINMENU:
1218 return ONPLAY_MAINMENU;
1219 default:
1220 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;