skin parser: skip \t characters at the begining of lines to allow for more readable...
[maemo-rb.git] / apps / onplay.c
blob143745d366ae3ebcd01a9d71b76a102b89884b31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
27 #include "debug.h"
28 #include "lcd.h"
29 #include "file.h"
30 #include "audio.h"
31 #include "menu.h"
32 #include "lang.h"
33 #include "playlist.h"
34 #include "button.h"
35 #include "kernel.h"
36 #include "keyboard.h"
37 #include "mp3data.h"
38 #include "metadata.h"
39 #include "screens.h"
40 #include "tree.h"
41 #include "settings.h"
42 #include "playlist_viewer.h"
43 #include "talk.h"
44 #include "onplay.h"
45 #include "filetypes.h"
46 #include "plugin.h"
47 #include "bookmark.h"
48 #include "action.h"
49 #include "splash.h"
50 #include "yesno.h"
51 #include "menus/exported_menus.h"
52 #ifdef HAVE_LCD_BITMAP
53 #include "icons.h"
54 #endif
55 #include "sound_menu.h"
56 #include "playlist_menu.h"
57 #include "playlist_catalog.h"
58 #ifdef HAVE_TAGCACHE
59 #include "tagtree.h"
60 #endif
61 #include "cuesheet.h"
62 #include "statusbar-skinned.h"
63 #include "pitchscreen.h"
64 #include "viewport.h"
65 #include "filefuncs.h"
66 #include "shortcuts.h"
68 static int context;
69 static const 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 /* ----------------------------------------------------------------------- */
86 /* Displays the bookmark menu options for the user to decide. This is an */
87 /* interface function. */
88 /* ----------------------------------------------------------------------- */
90 static int bookmark_menu_callback(int action,
91 const struct menu_item_ex *this_item);
92 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
93 ID2P(LANG_BOOKMARK_MENU_CREATE),
94 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
95 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
96 ID2P(LANG_BOOKMARK_MENU_LIST),
97 bookmark_load_menu, NULL,
98 bookmark_menu_callback, Icon_Bookmark);
99 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU),
100 bookmark_menu_callback, Icon_Bookmark,
101 &bookmark_create_menu_item, &bookmark_load_menu_item);
102 static int bookmark_menu_callback(int action,
103 const struct menu_item_ex *this_item)
105 switch (action)
107 case ACTION_REQUEST_MENUITEM:
108 if (this_item == &bookmark_load_menu_item)
110 if (!bookmark_exists())
111 return ACTION_EXIT_MENUITEM;
113 /* hide the bookmark menu if there is no playback */
114 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
115 return ACTION_EXIT_MENUITEM;
116 break;
117 #ifdef HAVE_LCD_CHARCELLS
118 case ACTION_ENTER_MENUITEM:
119 status_set_param(true);
120 break;
121 #endif
122 case ACTION_EXIT_MENUITEM:
123 #ifdef HAVE_LCD_CHARCELLS
124 status_set_param(false);
125 #endif
126 settings_save();
127 break;
129 return action;
132 /* CONTEXT_WPS playlist options */
133 static bool shuffle_playlist(void)
135 playlist_sort(NULL, true);
136 playlist_randomise(NULL, current_tick, true);
138 return false;
140 static bool save_playlist(void)
142 save_playlist_screen(NULL);
143 return false;
146 extern struct menu_item_ex view_cur_playlist; /* from playlist_menu.c */
147 MENUITEM_FUNCTION(search_playlist_item, 0, ID2P(LANG_SEARCH_IN_PLAYLIST),
148 search_playlist, NULL, NULL, Icon_Playlist);
149 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
150 save_playlist, NULL, NULL, Icon_Playlist);
151 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
152 shuffle_playlist, NULL, NULL, Icon_Playlist);
153 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
154 NULL, Icon_Playlist,
155 &view_cur_playlist, &search_playlist_item,
156 &playlist_save_item, &reshuffle_item
159 /* CONTEXT_[TREE|ID3DB] playlist options */
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 int playlist_insert_func(void *param)
242 if (((intptr_t)param == PLAYLIST_REPLACE) && !warn_on_pl_erase())
243 return 0;
244 add_to_playlist((intptr_t)param, false);
245 return 0;
248 static int playlist_queue_func(void *param)
250 add_to_playlist((intptr_t)param, true);
251 return 0;
254 static int treeplaylist_wplayback_callback(int action,
255 const struct menu_item_ex* this_item)
257 (void)this_item;
258 switch (action)
260 case ACTION_REQUEST_MENUITEM:
261 if (audio_status() & AUDIO_STATUS_PLAY)
262 return action;
263 else
264 return ACTION_EXIT_MENUITEM;
265 break;
267 return action;
270 static int treeplaylist_callback(int action,
271 const struct menu_item_ex *this_item);
273 /* insert items */
274 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
275 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
276 NULL, Icon_Playlist);
277 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
278 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
279 treeplaylist_wplayback_callback, Icon_Playlist);
280 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
281 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
282 treeplaylist_wplayback_callback, Icon_Playlist);
283 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
284 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
285 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
286 treeplaylist_callback, Icon_Playlist);
287 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
288 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
289 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
290 treeplaylist_callback, Icon_Playlist);
291 /* queue items */
292 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
293 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
294 treeplaylist_wplayback_callback, Icon_Playlist);
295 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
296 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
297 treeplaylist_wplayback_callback, Icon_Playlist);
298 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
299 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
300 treeplaylist_wplayback_callback, Icon_Playlist);
301 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
302 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
303 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
304 treeplaylist_wplayback_callback, Icon_Playlist);
305 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
306 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
307 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
308 treeplaylist_callback, Icon_Playlist);
309 /* replace playlist */
310 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
311 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
312 treeplaylist_wplayback_callback, Icon_Playlist);
314 /* others */
315 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
316 view_playlist, NULL,
317 treeplaylist_callback, Icon_Playlist);
319 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_CURRENT_PLAYLIST),
320 treeplaylist_callback, Icon_Playlist,
322 /* view */
323 &view_playlist_item,
325 /* insert */
326 &i_pl_item, &i_first_pl_item, &i_last_pl_item,
327 &i_shuf_pl_item, &i_last_shuf_pl_item,
328 /* queue */
330 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
331 &q_shuf_pl_item, &q_last_shuf_pl_item,
333 /* replace */
334 &replace_pl_item
336 static int treeplaylist_callback(int action,
337 const struct menu_item_ex *this_item)
339 switch (action)
341 case ACTION_REQUEST_MENUITEM:
342 if (this_item == &tree_playlist_menu)
344 if (((selected_file_attr & FILE_ATTR_MASK) ==
345 FILE_ATTR_AUDIO) ||
346 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
347 (selected_file_attr & ATTR_DIRECTORY))
349 return action;
352 else if (this_item == &view_playlist_item)
354 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
355 context == CONTEXT_TREE)
357 return action;
360 else if (this_item == &i_shuf_pl_item)
362 if ((audio_status() & AUDIO_STATUS_PLAY) ||
363 (selected_file_attr & ATTR_DIRECTORY) ||
364 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
366 return action;
369 else if (this_item == &i_last_shuf_pl_item ||
370 this_item == &q_last_shuf_pl_item)
372 if ((playlist_amount() > 0) &&
373 (audio_status() & AUDIO_STATUS_PLAY) &&
374 ((selected_file_attr & ATTR_DIRECTORY) ||
375 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
377 return action;
380 return ACTION_EXIT_MENUITEM;
381 break;
383 return action;
386 void onplay_show_playlist_menu(char* path)
388 selected_file = path;
389 if (dir_exists(path))
390 selected_file_attr = ATTR_DIRECTORY;
391 else
392 selected_file_attr = filetype_get_attr(path);
393 do_menu(&tree_playlist_menu, NULL, NULL, false);
396 /* playlist catalog options */
397 static bool cat_add_to_a_playlist(void)
399 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
400 false, NULL);
403 static bool cat_add_to_a_new_playlist(void)
405 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
406 true, NULL);
408 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
409 static bool set_catalogdir(void)
411 catalog_set_directory(selected_file);
412 settings_save();
413 return false;
415 MENUITEM_FUNCTION(set_catalogdir_item, 0, ID2P(LANG_SET_AS_PLAYLISTCAT_DIR),
416 set_catalogdir, NULL, clipboard_callback, Icon_Playlist);
418 static int cat_playlist_callback(int action,
419 const struct menu_item_ex *this_item);
420 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
421 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
422 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
423 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
424 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
425 cat_playlist_callback, Icon_Playlist,
426 &cat_add_to_list, &cat_add_to_new, &set_catalogdir_item);
428 void onplay_show_playlist_cat_menu(char* track_name)
430 selected_file = track_name;
431 selected_file_attr = FILE_ATTR_AUDIO;
432 do_menu(&cat_playlist_menu, NULL, NULL, false);
435 static int cat_playlist_callback(int action,
436 const struct menu_item_ex *this_item)
438 (void)this_item;
439 if (!selected_file ||
440 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
441 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
442 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
444 return ACTION_EXIT_MENUITEM;
446 #ifdef HAVE_TAGCACHE
447 if (context == CONTEXT_ID3DB &&
448 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO))
450 return ACTION_EXIT_MENUITEM;
452 #endif
454 switch (action)
456 case ACTION_REQUEST_MENUITEM:
457 if ((audio_status() & AUDIO_STATUS_PLAY) || context != CONTEXT_WPS)
459 return action;
461 else
462 return ACTION_EXIT_MENUITEM;
463 break;
465 return action;
468 #ifdef HAVE_LCD_BITMAP
469 static void draw_slider(void)
471 FOR_NB_SCREENS(i)
473 struct viewport vp;
474 int slider_height = 2*screens[i].getcharheight();
475 viewport_set_defaults(&vp, i);
476 screens[i].set_viewport(&vp);
477 show_busy_slider(&screens[i], 1, vp.height - slider_height,
478 vp.width-2, slider_height-1);
479 screens[i].update_viewport();
480 screens[i].set_viewport(NULL);
483 #else
484 #define draw_slider()
485 #endif
487 /* helper function to remove a non-empty directory */
488 static int remove_dir(char* dirname, int len)
490 int result = 0;
491 DIR* dir;
492 int dirlen = strlen(dirname);
494 dir = opendir(dirname);
495 if (!dir)
496 return -1; /* open error */
498 while(true)
500 struct dirent* entry;
501 /* walk through the directory content */
502 entry = readdir(dir);
503 if (!entry)
504 break;
505 struct dirinfo info = dir_get_info(dir, entry);
506 dirname[dirlen] ='\0';
507 /* inform the user which dir we're deleting */
508 splash(0, dirname);
510 /* append name to current directory */
511 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
512 if (info.attribute & ATTR_DIRECTORY)
513 { /* remove a subdirectory */
514 if (!strcmp((char *)entry->d_name, ".") ||
515 !strcmp((char *)entry->d_name, ".."))
516 continue; /* skip these */
518 result = remove_dir(dirname, len); /* recursion */
519 if (result)
520 break; /* or better continue, delete what we can? */
522 else
523 { /* remove a file */
524 draw_slider();
525 result = remove(dirname);
527 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
529 splash(HZ, ID2P(LANG_CANCEL));
530 result = -1;
531 break;
534 closedir(dir);
536 if (!result)
537 { /* remove the now empty directory */
538 dirname[dirlen] = '\0'; /* terminate to original length */
540 result = rmdir(dirname);
543 return result;
547 /* share code for file and directory deletion, saves space */
548 static bool delete_file_dir(void)
550 char file_to_delete[MAX_PATH];
551 strcpy(file_to_delete, selected_file);
553 const char *lines[]={
554 ID2P(LANG_REALLY_DELETE),
555 file_to_delete
557 const char *yes_lines[]={
558 ID2P(LANG_DELETING),
559 file_to_delete
562 const struct text_message message={lines, 2};
563 const struct text_message yes_message={yes_lines, 2};
565 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
566 return false;
568 splash(0, str(LANG_DELETING));
570 int res;
571 if (selected_file_attr & ATTR_DIRECTORY) /* true if directory */
573 char pathname[MAX_PATH]; /* space to go deep */
574 cpu_boost(true);
575 strlcpy(pathname, file_to_delete, sizeof(pathname));
576 res = remove_dir(pathname, sizeof(pathname));
577 cpu_boost(false);
579 else
580 res = remove(file_to_delete);
582 if (!res)
583 onplay_result = ONPLAY_RELOAD_DIR;
585 return (res == 0);
588 static bool rename_file(void)
590 char newname[MAX_PATH];
591 char* ptr = strrchr(selected_file, '/') + 1;
592 int pathlen = (ptr - selected_file);
593 strlcpy(newname, selected_file, sizeof(newname));
594 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
595 if (!strlen(newname + pathlen) ||
596 (rename(selected_file, newname) < 0)) {
597 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
598 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
600 else
601 onplay_result = ONPLAY_RELOAD_DIR;
604 return false;
607 static bool create_dir(void)
609 char dirname[MAX_PATH];
610 char *cwd;
611 int rc;
612 int pathlen;
614 cwd = getcwd(NULL, 0);
615 memset(dirname, 0, sizeof dirname);
617 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
619 pathlen = strlen(dirname);
620 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
621 if (rc < 0)
622 return false;
624 rc = mkdir(dirname);
625 if (rc < 0) {
626 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
627 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
628 str(LANG_FAILED));
629 } else {
630 onplay_result = ONPLAY_RELOAD_DIR;
633 return true;
636 /* Store the current selection in the clipboard */
637 static bool clipboard_clip(bool copy)
639 clipboard_selection[0] = 0;
640 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
641 clipboard_selection_attr = selected_file_attr;
642 clipboard_is_copy = copy;
644 return true;
647 static bool clipboard_cut(void)
649 return clipboard_clip(false);
652 static bool clipboard_copy(void)
654 return clipboard_clip(true);
657 /* Paste a file to a new directory. Will overwrite always. */
658 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
660 int src_fd, target_fd;
661 size_t buffersize;
662 ssize_t size, bytesread, byteswritten;
663 char *buffer;
664 bool result = false;
666 if (copy) {
667 /* See if we can get the plugin buffer for the file copy buffer */
668 buffer = (char *) plugin_get_buffer(&buffersize);
669 if (buffer == NULL || buffersize < 512) {
670 /* Not large enough, try for a disk sector worth of stack
671 instead */
672 buffersize = 512;
673 buffer = (char *) __builtin_alloca(buffersize);
676 if (buffer == NULL) {
677 return false;
680 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
681 size */
683 src_fd = open(src, O_RDONLY);
685 if (src_fd >= 0) {
686 target_fd = creat(target, 0666);
688 if (target_fd >= 0) {
689 result = true;
691 size = filesize(src_fd);
693 if (size == -1) {
694 result = false;
697 while(size > 0) {
698 bytesread = read(src_fd, buffer, buffersize);
700 if (bytesread == -1) {
701 result = false;
702 break;
705 size -= bytesread;
707 while(bytesread > 0) {
708 byteswritten = write(target_fd, buffer, bytesread);
710 if (byteswritten < 0) {
711 result = false;
712 size = 0;
713 break;
716 bytesread -= byteswritten;
717 draw_slider();
721 close(target_fd);
723 /* Copy failed. Cleanup. */
724 if (!result) {
725 remove(target);
729 close(src_fd);
731 } else {
732 result = rename(src, target) == 0;
733 #ifdef HAVE_MULTIVOLUME
734 if (!result) {
735 if (errno == EXDEV) {
736 /* Failed because cross volume rename doesn't work. Copy
737 instead */
738 result = clipboard_pastefile(src, target, true);
740 if (result) {
741 result = remove(src) == 0;
745 #endif
748 return result;
751 /* Paste a directory to a new location. Designed to be called by
752 clipboard_paste */
753 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
754 int targetlen, bool copy)
756 DIR *srcdir;
757 int srcdirlen = strlen(src);
758 int targetdirlen = strlen(target);
759 bool result = true;
761 if (!file_exists(target)) {
762 if (!copy) {
763 /* Just move the directory */
764 result = rename(src, target) == 0;
766 #ifdef HAVE_MULTIVOLUME
767 if (!result && errno == EXDEV) {
768 /* Try a copy as we're going across devices */
769 result = clipboard_pastedirectory(src, srclen, target,
770 targetlen, true);
772 /* If it worked, remove the source directory */
773 if (result) {
774 remove_dir(src, srclen);
777 #endif
778 return result;
779 } else {
780 /* Make a directory to copy things to */
781 result = mkdir(target) == 0;
785 /* Check if something went wrong already */
786 if (!result) {
787 return result;
790 srcdir = opendir(src);
791 if (!srcdir) {
792 return false;
795 /* This loop will exit as soon as there's a problem */
796 while(result)
798 struct dirent* entry;
799 /* walk through the directory content */
800 entry = readdir(srcdir);
801 if (!entry)
802 break;
804 struct dirinfo info = dir_get_info(srcdir, entry);
805 /* append name to current directory */
806 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
807 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
808 entry->d_name);
810 DEBUGF("Copy %s to %s\n", src, target);
812 if (info.attribute & ATTR_DIRECTORY)
813 { /* copy/move a subdirectory */
814 if (!strcmp((char *)entry->d_name, ".") ||
815 !strcmp((char *)entry->d_name, ".."))
816 continue; /* skip these */
818 result = clipboard_pastedirectory(src, srclen, target, targetlen,
819 copy); /* recursion */
821 else
822 { /* copy/move a file */
823 draw_slider();
824 result = clipboard_pastefile(src, target, copy);
828 closedir(srcdir);
830 if (result) {
831 src[srcdirlen] = '\0'; /* terminate to original length */
832 target[targetdirlen] = '\0'; /* terminate to original length */
835 return result;
838 /* Paste the clipboard to the current directory */
839 static bool clipboard_paste(void)
841 char target[MAX_PATH];
842 char *cwd, *nameptr;
843 bool success;
845 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
846 static const struct text_message message={lines, 1};
848 /* Get the name of the current directory */
849 cwd = getcwd(NULL, 0);
851 /* Figure out the name of the selection */
852 nameptr = strrchr(clipboard_selection, '/');
854 /* Final target is current directory plus name of selection */
855 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
857 /* If the target existed but they choose not to overwite, exit */
858 if (file_exists(target) &&
859 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
860 return false;
863 if (clipboard_is_copy) {
864 splash(0, ID2P(LANG_COPYING));
866 else
868 splash(0, ID2P(LANG_MOVING));
871 /* Now figure out what we're doing */
872 cpu_boost(true);
873 if (clipboard_selection_attr & ATTR_DIRECTORY) {
874 /* Recursion. Set up external stack */
875 char srcpath[MAX_PATH];
876 char targetpath[MAX_PATH];
877 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
879 /* Do not allow the user to paste a directory into a dir they are
880 copying */
881 success = 0;
883 else
885 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
886 strlcpy(targetpath, target, sizeof(targetpath));
888 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
889 target, sizeof(targetpath), clipboard_is_copy);
891 if (success && !clipboard_is_copy)
893 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
894 remove_dir(srcpath, sizeof(srcpath));
897 } else {
898 success = clipboard_pastefile(clipboard_selection, target,
899 clipboard_is_copy);
901 cpu_boost(false);
903 /* Did it work? */
904 if (success) {
905 /* Reset everything */
906 clipboard_selection[0] = 0;
907 clipboard_selection_attr = 0;
908 clipboard_is_copy = false;
910 /* Force reload of the current directory */
911 onplay_result = ONPLAY_RELOAD_DIR;
912 } else {
913 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
914 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
915 str(LANG_FAILED));
918 return true;
921 #ifdef HAVE_TAGCACHE
922 static int set_rating_inline(void)
924 struct mp3entry* id3 = audio_current_track();
925 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
927 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
928 NULL, 1, 0, 10, NULL, NULL);
929 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
931 else
932 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
933 return 0;
935 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
937 (void)this_item;
938 switch (action)
940 case ACTION_REQUEST_MENUITEM:
941 if (!selected_file || !global_settings.runtimedb ||
942 !tagcache_is_usable())
943 return ACTION_EXIT_MENUITEM;
944 break;
946 return action;
948 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
949 set_rating_inline, NULL,
950 ratingitem_callback, Icon_Questionmark);
951 #endif
952 #ifdef HAVE_PICTUREFLOW_INTEGRATION
953 MENUITEM_RETURNVALUE(pictureflow_item, ID2P(LANG_ONPLAY_PICTUREFLOW),
954 GO_TO_PICTUREFLOW, NULL, Icon_NOICON);
955 #endif
957 static bool view_cue(void)
959 struct mp3entry* id3 = audio_current_track();
960 if(id3 && id3->cuesheet)
962 browse_cuesheet(id3->cuesheet);
964 return false;
966 static int view_cue_item_callback(int action,
967 const struct menu_item_ex *this_item)
969 (void)this_item;
970 struct mp3entry* id3 = audio_current_track();
971 switch (action)
973 case ACTION_REQUEST_MENUITEM:
974 if (!selected_file
975 || !id3 || !id3->cuesheet)
976 return ACTION_EXIT_MENUITEM;
977 break;
979 return action;
981 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
982 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
985 static int browse_id3_wrapper(void)
987 if (browse_id3())
988 return GO_TO_ROOT;
989 return GO_TO_PREVIOUS;
992 /* CONTEXT_WPS items */
993 MENUITEM_FUNCTION(browse_id3_item, MENU_FUNC_CHECK_RETVAL, ID2P(LANG_MENU_SHOW_ID3_INFO),
994 browse_id3_wrapper, NULL, NULL, Icon_NOICON);
995 #ifdef HAVE_PITCHSCREEN
996 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
997 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
998 #endif
1000 /* CONTEXT_[TREE|ID3DB] items */
1001 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1002 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1003 rename_file, NULL, clipboard_callback, Icon_NOICON);
1004 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1005 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1006 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1007 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1008 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1009 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1010 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1011 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1013 delete_file_dir, NULL, clipboard_callback, Icon_NOICON);
1014 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1015 create_dir, NULL, clipboard_callback, Icon_NOICON);
1017 /* other items */
1018 static bool list_viewers(void)
1020 int ret = filetype_list_viewers(selected_file);
1021 if (ret == PLUGIN_USB_CONNECTED)
1022 onplay_result = ONPLAY_RELOAD_DIR;
1023 return false;
1026 static bool onplay_load_plugin(void *param)
1028 int ret = filetype_load_plugin((const char*)param, selected_file);
1029 if (ret == PLUGIN_USB_CONNECTED)
1030 onplay_result = ONPLAY_RELOAD_DIR;
1031 return false;
1034 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1035 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1036 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1037 onplay_load_plugin, (void *)"properties",
1038 clipboard_callback, Icon_NOICON);
1039 static bool onplay_add_to_shortcuts(void)
1041 shortcuts_add(SHORTCUT_BROWSER, selected_file);
1042 return false;
1044 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1045 onplay_add_to_shortcuts, NULL,
1046 clipboard_callback, Icon_NOICON);
1048 #if LCD_DEPTH > 1
1049 static bool set_backdrop(void)
1051 strlcpy(global_settings.backdrop_file, selected_file,
1052 sizeof(global_settings.backdrop_file));
1053 settings_save();
1054 skin_backdrop_load_setting();
1055 skin_backdrop_show(sb_get_backdrop(SCREEN_MAIN));
1056 return true;
1058 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1059 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1060 #endif
1061 #ifdef HAVE_RECORDING
1062 static bool set_recdir(void)
1064 strlcpy(global_settings.rec_directory, selected_file,
1065 sizeof(global_settings.rec_directory));
1066 settings_save();
1067 return false;
1069 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1070 set_recdir, NULL, clipboard_callback, Icon_Recording);
1071 #endif
1072 static bool set_startdir(void)
1074 snprintf(global_settings.start_directory,
1075 sizeof(global_settings.start_directory),
1076 "%s/", selected_file);
1077 settings_save();
1078 return false;
1080 MENUITEM_FUNCTION(set_startdir_item, 0, ID2P(LANG_SET_AS_START_DIR),
1081 set_startdir, NULL, clipboard_callback, Icon_file_view_menu);
1083 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1085 switch (action)
1087 case ACTION_REQUEST_MENUITEM:
1088 #ifdef HAVE_MULTIVOLUME
1089 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1090 (this_item == &rename_file_item ||
1091 this_item == &delete_dir_item ||
1092 this_item == &clipboard_cut_item) )
1093 return ACTION_EXIT_MENUITEM;
1094 /* no rename+delete for volumes */
1095 if ((selected_file_attr & ATTR_VOLUME) &&
1096 (this_item == &delete_file_item ||
1097 this_item == &list_viewers_item))
1098 return ACTION_EXIT_MENUITEM;
1099 #endif
1100 #ifdef HAVE_TAGCACHE
1101 if (context == CONTEXT_ID3DB)
1103 if (((selected_file_attr & FILE_ATTR_MASK) ==
1104 FILE_ATTR_AUDIO) &&
1105 this_item == &properties_item)
1106 return action;
1107 return ACTION_EXIT_MENUITEM;
1109 #endif
1110 if (this_item == &clipboard_paste_item)
1111 { /* visible if there is something to paste */
1112 return (clipboard_selection[0] != 0) ?
1113 action : ACTION_EXIT_MENUITEM;
1115 else if (this_item == &create_dir_item)
1117 /* always visible */
1118 return action;
1120 else if (selected_file)
1122 /* requires an actual file */
1123 if (this_item == &rename_file_item ||
1124 this_item == &clipboard_cut_item ||
1125 this_item == &clipboard_copy_item ||
1126 this_item == &properties_item ||
1127 this_item == &add_to_faves_item)
1129 return action;
1131 else if ((selected_file_attr & ATTR_DIRECTORY))
1133 /* only for directories */
1134 if (this_item == &delete_dir_item ||
1135 this_item == &set_startdir_item ||
1136 this_item == &set_catalogdir_item
1137 #ifdef HAVE_RECORDING
1138 || this_item == &set_recdir_item
1139 #endif
1141 return action;
1143 else if (this_item == &delete_file_item ||
1144 this_item == &list_viewers_item)
1146 /* only for files */
1147 return action;
1149 #if LCD_DEPTH > 1
1150 else if (this_item == &set_backdrop_item)
1152 char *suffix = strrchr(selected_file, '.');
1153 if (suffix)
1155 if (strcasecmp(suffix, ".bmp") == 0)
1157 return action;
1161 #endif
1163 return ACTION_EXIT_MENUITEM;
1164 break;
1166 return action;
1169 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1170 /* used when onplay() is called in the CONTEXT_WPS context */
1171 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1172 onplaymenu_callback, Icon_Audio,
1173 &wps_playlist_menu, &cat_playlist_menu,
1174 &sound_settings, &playback_settings,
1175 #ifdef HAVE_TAGCACHE
1176 &rating_item,
1177 #endif
1178 &bookmark_menu,
1179 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1180 &pictureflow_item,
1181 #endif
1182 &browse_id3_item, &list_viewers_item,
1183 &delete_file_item, &view_cue_item,
1184 #ifdef HAVE_PITCHSCREEN
1185 &pitch_screen_item,
1186 #endif
1188 /* used when onplay() is not called in the CONTEXT_WPS context */
1189 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1190 onplaymenu_callback, Icon_file_view_menu,
1191 &tree_playlist_menu, &cat_playlist_menu,
1192 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1193 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1194 #if LCD_DEPTH > 1
1195 &set_backdrop_item,
1196 #endif
1197 &list_viewers_item, &create_dir_item, &properties_item,
1198 #ifdef HAVE_RECORDING
1199 &set_recdir_item,
1200 #endif
1201 &set_startdir_item, &add_to_faves_item,
1203 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1205 switch (action)
1207 case ACTION_TREE_STOP:
1208 if (this_item == &wps_onplay_menu)
1210 list_stop_handler();
1211 return ACTION_STD_CANCEL;
1213 break;
1214 case ACTION_EXIT_MENUITEM:
1215 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1216 break;
1218 return action;
1221 #ifdef HAVE_HOTKEY
1222 /* direct function calls, no need for menu callbacks */
1223 static bool delete_item(void)
1225 #ifdef HAVE_MULTIVOLUME
1226 /* no delete for volumes */
1227 if ((selected_file_attr & FAT_ATTR_VOLUME) ||
1228 (selected_file_attr & ATTR_VOLUME))
1229 return false;
1230 #endif
1231 return delete_file_dir();
1234 static bool open_with(void)
1236 /* only open files */
1237 if (selected_file_attr & ATTR_DIRECTORY)
1238 return false;
1239 #ifdef HAVE_MULTIVOLUME
1240 if (selected_file_attr & ATTR_VOLUME)
1241 return false;
1242 #endif
1243 return list_viewers();
1246 static int playlist_insert_shuffled(void)
1248 if ((audio_status() & AUDIO_STATUS_PLAY) ||
1249 (selected_file_attr & ATTR_DIRECTORY) ||
1250 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
1252 playlist_insert_func((intptr_t*)PLAYLIST_INSERT_SHUFFLED);
1253 return ONPLAY_START_PLAY;
1256 return ONPLAY_RELOAD_DIR;
1259 struct hotkey_assignment {
1260 int action; /* hotkey_action */
1261 int lang_id; /* Language ID */
1262 struct menu_func func; /* Function to run if this entry is selected */
1263 int return_code; /* What to return after the function is run */
1266 #define HOTKEY_FUNC(func, param) {{(void *)func}, param}
1268 /* Any desired hotkey functions go here, in the enum in onplay.h,
1269 and in the settings menu in settings_list.c. The order here
1270 is not important. */
1271 static struct hotkey_assignment hotkey_items[] = {
1272 { HOTKEY_VIEW_PLAYLIST, LANG_VIEW_DYNAMIC_PLAYLIST,
1273 HOTKEY_FUNC(NULL, NULL),
1274 ONPLAY_PLAYLIST },
1275 { HOTKEY_SHOW_TRACK_INFO, LANG_MENU_SHOW_ID3_INFO,
1276 HOTKEY_FUNC(browse_id3, NULL),
1277 ONPLAY_RELOAD_DIR },
1278 #ifdef HAVE_PITCHSCREEN
1279 { HOTKEY_PITCHSCREEN, LANG_PITCH,
1280 HOTKEY_FUNC(gui_syncpitchscreen_run, NULL),
1281 ONPLAY_RELOAD_DIR },
1282 #endif
1283 { HOTKEY_OPEN_WITH, LANG_ONPLAY_OPEN_WITH,
1284 HOTKEY_FUNC(open_with, NULL),
1285 ONPLAY_RELOAD_DIR },
1286 { HOTKEY_DELETE, LANG_DELETE,
1287 HOTKEY_FUNC(delete_item, NULL),
1288 ONPLAY_RELOAD_DIR },
1289 { HOTKEY_INSERT, LANG_INSERT,
1290 HOTKEY_FUNC(playlist_insert_func, (intptr_t*)PLAYLIST_INSERT),
1291 ONPLAY_RELOAD_DIR },
1292 { HOTKEY_INSERT_SHUFFLED, LANG_INSERT_SHUFFLED,
1293 HOTKEY_FUNC(playlist_insert_shuffled, NULL),
1294 ONPLAY_RELOAD_DIR },
1295 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1296 { HOTKEY_PICTUREFLOW, LANG_ONPLAY_PICTUREFLOW,
1297 HOTKEY_FUNC(NULL, NULL),
1298 ONPLAY_PICTUREFLOW },
1299 #endif
1302 /* Return the language ID for this action */
1303 int get_hotkey_lang_id(int action)
1305 int i = ARRAYLEN(hotkey_items);
1306 while (i--)
1308 if (hotkey_items[i].action == action)
1309 return hotkey_items[i].lang_id;
1312 return LANG_OFF;
1315 /* Execute the hotkey function, if listed */
1316 static int execute_hotkey(bool is_wps)
1318 int i = ARRAYLEN(hotkey_items);
1319 struct hotkey_assignment *this_item;
1320 const int action = (is_wps ? global_settings.hotkey_wps :
1321 global_settings.hotkey_tree);
1323 /* search assignment struct for a match for the hotkey setting */
1324 while (i--)
1326 this_item = &hotkey_items[i];
1327 if (this_item->action == action)
1329 /* run the associated function (with optional param), if any */
1330 const struct menu_func func = this_item->func;
1331 int func_return = ONPLAY_RELOAD_DIR;
1332 if (func.function != NULL)
1334 if (func.param != NULL)
1335 func_return = (*func.function_w_param)(func.param);
1336 else
1337 func_return = (*func.function)();
1339 /* return with the associated code */
1340 const int return_code = this_item->return_code;
1341 /* ONPLAY_OK here means to use the function return code */
1342 if (return_code == ONPLAY_OK)
1343 return func_return;
1344 return return_code;
1348 /* no valid hotkey set, ignore hotkey */
1349 return ONPLAY_RELOAD_DIR;
1351 #endif /* HOTKEY */
1353 int onplay(char* file, int attr, int from, bool hotkey)
1355 const struct menu_item_ex *menu;
1356 onplay_result = ONPLAY_OK;
1357 context = from;
1358 selected_file = file;
1359 selected_file_attr = attr;
1360 int menu_selection;
1361 #ifdef HAVE_HOTKEY
1362 if (hotkey)
1363 return execute_hotkey(context == CONTEXT_WPS);
1364 #else
1365 (void)hotkey;
1366 #endif
1368 push_current_activity(ACTIVITY_CONTEXTMENU);
1369 if (context == CONTEXT_WPS)
1370 menu = &wps_onplay_menu;
1371 else
1372 menu = &tree_onplay_menu;
1373 menu_selection = do_menu(menu, NULL, NULL, false);
1374 pop_current_activity();
1376 switch (menu_selection)
1378 case GO_TO_WPS:
1379 return ONPLAY_START_PLAY;
1380 case GO_TO_ROOT:
1381 case GO_TO_MAINMENU:
1382 return ONPLAY_MAINMENU;
1383 case GO_TO_PLAYLIST_VIEWER:
1384 return ONPLAY_PLAYLIST;
1385 #ifdef HAVE_PICTUREFLOW_INTEGRATION
1386 case GO_TO_PICTUREFLOW:
1387 return ONPLAY_PICTUREFLOW;
1388 #endif
1389 default:
1390 return onplay_result;