1 /*****************************************************************************
2 * ncurses.c : NCurses interface for vlc
3 *****************************************************************************
4 * Copyright © 2001-2010 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Yoann Peronneau <yoann@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Rafaël Carré <funman@videolanorg>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /* UTF8 locale is required */
30 /*****************************************************************************
32 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
40 #define _XOPEN_SOURCE_EXTENDED 1
45 #include <vlc_interface.h>
48 #include <vlc_charset.h>
49 #include <vlc_input.h>
51 #include <vlc_playlist.h>
57 #ifdef HAVE_SYS_STAT_H
58 # include <sys/stat.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open (vlc_object_t *);
65 static void Close (vlc_object_t *);
67 /*****************************************************************************
69 *****************************************************************************/
71 #define BROWSE_TEXT N_("Filebrowser starting point")
72 #define BROWSE_LONGTEXT N_(\
73 "This option allows you to specify the directory the ncurses filebrowser " \
74 "will show you initially.")
77 set_shortname("Ncurses")
78 set_description(N_("Ncurses interface"))
79 set_capability("interface", 10)
80 set_category(CAT_INTERFACE)
81 set_subcategory(SUBCAT_INTERFACE_MAIN)
82 set_callbacks(Open, Close)
83 add_shortcut("curses")
84 add_directory("browse-dir", NULL, BROWSE_TEXT, BROWSE_LONGTEXT, false)
87 /*****************************************************************************
88 * intf_sys_t: description and status of ncurses interface
89 *****************************************************************************/
105 static const char *box_title[] = {
107 [BOX_HELP] = " Help ",
108 [BOX_INFO] = " Information ",
109 [BOX_LOG] = " Messages ",
110 [BOX_PLAYLIST] = " Playlist ",
111 [BOX_SEARCH] = " Playlist ",
112 [BOX_OPEN] = " Playlist ",
113 [BOX_BROWSE] = " Browse ",
114 [BOX_META] = " Meta-information ",
115 [BOX_OBJECTS] = " Objects ",
116 [BOX_STATS] = " Stats ",
134 /* XXX: new elements here ! */
139 /* Available colors: BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE */
140 static const struct { short f; short b; } color_pairs[] =
142 /* element */ /* foreground*/ /* background*/
143 [C_TITLE] = { COLOR_YELLOW, COLOR_BLACK },
145 /* jamaican playlist, for rastafari sisters & brothers! */
146 [C_PLAYLIST_1] = { COLOR_GREEN, COLOR_BLACK },
147 [C_PLAYLIST_2] = { COLOR_YELLOW, COLOR_BLACK },
148 [C_PLAYLIST_3] = { COLOR_RED, COLOR_BLACK },
150 /* used in DrawBox() */
151 [C_BOX] = { COLOR_CYAN, COLOR_BLACK },
152 /* Source: State, Position, Volume, Chapters, etc...*/
153 [C_STATUS] = { COLOR_BLUE, COLOR_BLACK },
155 /* VLC messages, keep the order from highest priority to lowest */
156 [C_INFO] = { COLOR_BLACK, COLOR_WHITE },
157 [C_ERROR] = { COLOR_RED, COLOR_BLACK },
158 [C_WARNING] = { COLOR_YELLOW, COLOR_BLACK },
159 [C_DEBUG] = { COLOR_WHITE, COLOR_BLACK },
161 /* Category title: help, info, metadata */
162 [C_CATEGORY] = { COLOR_MAGENTA, COLOR_BLACK },
163 /* Folder (BOX_BROWSE) */
164 [C_FOLDER] = { COLOR_RED, COLOR_BLACK },
175 playlist_item_t *p_item;
181 input_thread_t *p_input;
186 int i_box_y; // start of box content
188 int i_box_lines_total; // number of lines in the box
189 int i_box_start; // first line of box displayed
190 int i_box_idx; // selected line
192 msg_subscription_t *p_sub; // message bank subscription
193 msg_item_t *msgs[50]; // ring buffer
195 vlc_mutex_t msg_lock;
197 /* Search Box context */
198 char psz_search_chain[20];
199 char *psz_old_search;
202 /* Open Box Context */
203 char psz_open_chain[50];
205 /* File Browser context */
206 char *psz_current_dir;
208 struct dir_entry_t **pp_dir_entries;
209 bool b_show_hidden_files;
211 /* Playlist context */
213 struct pl_item_t **pp_plist;
217 playlist_item_t *p_node; /* current node */
226 /*****************************************************************************
228 *****************************************************************************/
230 static void DirsDestroy(intf_sys_t *p_sys)
232 while (p_sys->i_dir_entries)
234 struct dir_entry_t *p_dir_entry;
235 p_dir_entry = p_sys->pp_dir_entries[--p_sys->i_dir_entries];
236 free(p_dir_entry->psz_path);
239 free(p_sys->pp_dir_entries);
240 p_sys->pp_dir_entries = NULL;
243 static int comp_dir_entries(const void *pp_dir_entry1, const void *pp_dir_entry2)
245 struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
246 struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
248 if (p_dir_entry1->b_file == p_dir_entry2->b_file)
249 return strcasecmp(p_dir_entry1->psz_path, p_dir_entry2->psz_path);
251 return p_dir_entry1->b_file ? 1 : -1;
254 static bool IsFile(const char *current_dir, const char *entry)
261 if (asprintf(&uri, "%s/%s", current_dir, entry) != -1)
263 ret = vlc_stat(uri, &st) || !S_ISDIR(st.st_mode);
270 static void ReadDir(intf_thread_t *p_intf)
272 intf_sys_t *p_sys = p_intf->p_sys;
275 if (!p_sys->psz_current_dir || !*p_sys->psz_current_dir)
277 msg_Dbg(p_intf, "no current dir set");
284 p_current_dir = vlc_opendir(p_sys->psz_current_dir);
288 /* something went bad, get out of here ! */
289 msg_Warn(p_intf, "cannot open directory `%s' (%m)",
290 p_sys->psz_current_dir);
294 /* Clean the old shit */
297 /* while we still have entries in the directory */
298 while ((psz_entry = vlc_readdir(p_current_dir)))
300 struct dir_entry_t *p_dir_entry;
302 if (!p_sys->b_show_hidden_files)
303 if (*psz_entry == '.' && strcmp(psz_entry, ".."))
306 if (!(p_dir_entry = malloc(sizeof *p_dir_entry)))
309 p_dir_entry->b_file = IsFile(p_sys->psz_current_dir, psz_entry);
310 p_dir_entry->psz_path = psz_entry;
311 INSERT_ELEM(p_sys->pp_dir_entries, p_sys->i_dir_entries,
312 p_sys->i_dir_entries, p_dir_entry);
320 qsort(p_sys->pp_dir_entries, p_sys->i_dir_entries,
321 sizeof(struct dir_entry_t*), &comp_dir_entries);
323 closedir(p_current_dir);
326 /*****************************************************************************
327 * Adjust index position after a change (list navigation or item switching)
328 *****************************************************************************/
329 static void CheckIdx(intf_sys_t *p_sys)
331 int lines = p_sys->i_box_lines_total - 1;
332 int height = LINES - p_sys->i_box_y - 2;
333 if (height > lines - 1)
336 /* make sure the new index is within the box */
337 if (p_sys->i_box_idx <= 0)
339 p_sys->i_box_idx = 0;
340 p_sys->i_box_start = 0;
342 else if (p_sys->i_box_idx >= lines - 1 && lines > 0)
344 p_sys->i_box_idx = lines - 1;
345 p_sys->i_box_start = p_sys->i_box_idx - height;
348 /* Fix box start (1st line of the box displayed) */
349 if (p_sys->i_box_idx < p_sys->i_box_start ||
350 p_sys->i_box_idx > height + p_sys->i_box_start + 1)
352 p_sys->i_box_start = p_sys->i_box_idx - height/2;
353 if (p_sys->i_box_start < 0)
354 p_sys->i_box_start = 0;
356 else if (p_sys->i_box_idx == p_sys->i_box_start - 1)
357 p_sys->i_box_start--;
358 else if (p_sys->i_box_idx == height + p_sys->i_box_start + 1)
359 p_sys->i_box_start++;
362 /*****************************************************************************
364 *****************************************************************************/
365 static void PlaylistDestroy(intf_sys_t *p_sys)
367 while (p_sys->i_plist_entries)
369 struct pl_item_t *p_pl_item = p_sys->pp_plist[--p_sys->i_plist_entries];
370 free(p_pl_item->psz_display);
373 free(p_sys->pp_plist);
374 p_sys->pp_plist = NULL;
377 static inline playlist_item_t *PlaylistGetRoot(intf_thread_t *p_intf)
379 playlist_t *p_playlist = pl_Get(p_intf);
380 return p_intf->p_sys->category_view ?
381 p_playlist->p_root_category :
382 p_playlist->p_root_onelevel;
385 static bool PlaylistAddChild(intf_sys_t *p_sys, playlist_item_t *p_child,
386 const char *c, const char d)
389 char *psz_name = input_item_GetTitleFbName(p_child->p_input);
390 struct pl_item_t *p_pl_item = malloc(sizeof *p_pl_item);
392 if (!psz_name || !p_pl_item)
395 p_pl_item->p_item = p_child;
398 ret = asprintf(&p_pl_item->psz_display, "%s%c-%s", c, d, psz_name);
400 ret = asprintf(&p_pl_item->psz_display, " %s", psz_name);
408 INSERT_ELEM(p_sys->pp_plist, p_sys->i_plist_entries,
409 p_sys->i_plist_entries, p_pl_item);
419 static void PlaylistAddNode(intf_sys_t *p_sys, playlist_item_t *p_node,
422 for(int k = 0; k < p_node->i_children; k++)
424 playlist_item_t *p_child = p_node->pp_children[k];
425 char d = k == p_node->i_children - 1 ? '`' : '|';
426 if(!PlaylistAddChild(p_sys, p_child, c, d))
429 if (p_child->i_children <= 0)
435 if (asprintf(&psz_tmp, "%s%c ", c,
436 k == p_node->i_children - 1 ? ' ' : '|') == -1)
438 PlaylistAddNode(p_sys, p_child, psz_tmp);
442 PlaylistAddNode(p_sys, p_child, " ");
446 static void PlaylistRebuild(intf_thread_t *p_intf)
448 intf_sys_t *p_sys = p_intf->p_sys;
449 playlist_t *p_playlist = pl_Get(p_intf);
452 PlaylistDestroy(p_sys);
453 PlaylistAddNode(p_sys, PlaylistGetRoot(p_intf), "");
457 static int PlaylistChanged(vlc_object_t *p_this, const char *psz_variable,
458 vlc_value_t oval, vlc_value_t nval, void *param)
460 VLC_UNUSED(p_this); VLC_UNUSED(psz_variable);
461 VLC_UNUSED(oval); VLC_UNUSED(nval);
463 intf_thread_t *p_intf = (intf_thread_t *)param;
464 intf_sys_t *p_sys = p_intf->p_sys;
465 playlist_item_t *p_node = playlist_CurrentPlayingItem(pl_Get(p_intf));
467 p_sys->b_need_update = true;
468 p_sys->p_node = p_node ? p_node->p_parent : NULL;
474 /* This function have to be called with the playlist locked */
475 static inline bool PlaylistIsPlaying(playlist_t *p_playlist,
476 playlist_item_t *p_item)
478 playlist_item_t *p_played_item = playlist_CurrentPlayingItem(p_playlist);
479 return p_item && p_played_item
480 && p_item->p_input && p_played_item->p_input
481 && p_item->p_input->i_id == p_played_item->p_input->i_id;
484 static int SubSearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring,
485 int i_start, int i_stop)
487 for(int i = i_start + 1; i < i_stop; i++)
488 if (strcasestr(p_sys->pp_plist[i]->psz_display, psz_searchstring))
494 static void SearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring)
496 int i_item, i_first = p_sys->i_before_search;
501 if (!psz_searchstring || !*psz_searchstring)
503 p_sys->i_box_idx = p_sys->i_before_search;
507 i_item = SubSearchPlaylist(p_sys, psz_searchstring, i_first + 1,
508 p_sys->i_plist_entries);
510 i_item = SubSearchPlaylist(p_sys, psz_searchstring, 0, i_first);
514 p_sys->i_box_idx = i_item;
519 static inline bool IsIndex(intf_sys_t *p_sys, playlist_t *p_playlist, int i)
521 playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
522 return (p_item->i_children == 0 && p_item == p_sys->p_node) ||
523 PlaylistIsPlaying(p_playlist, p_item);
526 static void FindIndex(intf_sys_t *p_sys, playlist_t *p_playlist)
528 int plidx = p_sys->i_box_idx;
529 int max = p_sys->i_plist_entries;
533 if (!IsIndex(p_sys, p_playlist, plidx))
534 for(int i = 0; i < max; i++)
535 if (IsIndex(p_sys, p_playlist, i))
537 p_sys->i_box_idx = i;
544 p_sys->b_plidx_follow = true;
547 /****************************************************************************
549 ****************************************************************************/
551 static void start_color_and_pairs(intf_thread_t *p_intf)
555 p_intf->p_sys->b_color = false;
556 msg_Warn(p_intf, "Terminal doesn't support colors");
561 for(int i = C_DEFAULT + 1; i < C_MAX; i++)
562 init_pair(i, color_pairs[i].f, color_pairs[i].b);
564 /* untested, in all my terminals, !can_change_color() --funman */
565 if (can_change_color())
566 init_color(COLOR_YELLOW, 960, 500, 0); /* YELLOW -> ORANGE */
569 static void DrawBox(int y, int h, bool b_color, const char *title)
574 if (w <= 3 || h <= 0)
577 if (b_color) color_set(C_BOX, NULL);
579 if (!title) title = "";
580 i_len = strlen(title);
585 mvaddch(y, 0, ACS_ULCORNER);
586 mvhline(y, 1, ACS_HLINE, (w-i_len-2)/2);
587 mvprintw(y, 1+(w-i_len-2)/2, "%s", title);
588 mvhline(y, (w-i_len)/2+i_len, ACS_HLINE, w - 1 - ((w-i_len)/2+i_len));
589 mvaddch(y, w-1,ACS_URCORNER);
591 for(int i = 0; i < h; i++)
593 mvaddch(++y, 0, ACS_VLINE);
594 mvaddch(y, w-1, ACS_VLINE);
597 mvaddch(++y, 0, ACS_LLCORNER);
598 mvhline(y, 1, ACS_HLINE, w - 2);
599 mvaddch(y, w-1, ACS_LRCORNER);
600 if (b_color) color_set(C_DEFAULT, NULL);
603 static void DrawEmptyLine(int y, int x, int w)
607 mvhline(y, x, ' ', w);
610 static void DrawLine(int y, int x, int w)
615 mvhline(y, x, ' ', w);
619 static void mvnprintw(int y, int x, int w, const char *p_fmt, ...)
628 va_start(vl_args, p_fmt);
629 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
633 i_len = strlen(p_buf);
635 wchar_t psz_wide[i_len + 1];
638 size_t i_char_len = mbstowcs(psz_wide, p_buf, i_len);
640 size_t i_width; /* number of columns */
642 if (i_char_len == (size_t)-1) /* an invalid character was encountered */
648 i_width = wcswidth(psz_wide, i_char_len);
649 if (i_width == (size_t)-1)
651 /* a non printable character was encountered */
653 for(unsigned i = 0 ; i < i_char_len ; i++)
655 int i_cwidth = wcwidth(psz_wide[i]);
661 if (i_width <= (size_t)w)
663 mvprintw(y, x, "%s", p_buf);
664 mvhline(y, x + i_width, ' ', w - i_width);
669 int i_total_width = 0;
671 while (i_total_width < w)
673 i_total_width += wcwidth(psz_wide[i]);
674 if (w > 7 && i_total_width >= w/2)
678 i_total_width -= wcwidth(psz_wide[i]) - 2;
681 /* we require this check only if at least one character
682 * 4 or more columns wide exists (which i doubt) */
684 i_total_width -= wcwidth(psz_wide[i-1]) - 1;
687 /* find the widest string */
688 int j, i_2nd_width = 0;
689 for(j = i_char_len - 1; i_2nd_width < w - i_total_width; j--)
690 i_2nd_width += wcwidth(psz_wide[j]);
692 /* we already have i_total_width columns filled, and we can't
693 * have more than w columns */
694 if (i_2nd_width > w - i_total_width)
697 wmemmove(&psz_wide[i+2], &psz_wide[j+1], i_char_len - j - 1);
698 psz_wide[i + 2 + i_char_len - j - 1] = '\0';
703 if (w <= 7) /* we don't add the '...' else we lose too much chars */
706 size_t i_wlen = wcslen(psz_wide) * 6 + 1; /* worst case */
707 char psz_ellipsized[i_wlen];
708 wcstombs(psz_ellipsized, psz_wide, i_wlen);
709 mvprintw(y, x, "%s", psz_ellipsized);
714 static void MainBoxWrite(intf_sys_t *p_sys, int l, const char *p_fmt, ...)
718 bool b_selected = l == p_sys->i_box_idx;
720 if (l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_height)
723 va_start(vl_args, p_fmt);
724 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
728 if (b_selected) attron(A_REVERSE);
729 mvnprintw(p_sys->i_box_y + l - p_sys->i_box_start, 1, COLS - 2, "%s", p_buf);
730 if (b_selected) attroff(A_REVERSE);
735 static int SubDrawObject(intf_sys_t *p_sys, int l, vlc_object_t *p_obj, int i_level, const char *prefix)
737 int x = 2 * i_level++;
738 char *psz_name = vlc_object_get_name(p_obj);
741 memset(space, ' ', x);
746 MainBoxWrite(p_sys, l++, "%s%s%s \"%s\" (%p)", space, prefix,
747 p_obj->psz_object_type, psz_name, p_obj);
751 MainBoxWrite(p_sys, l++, "%s%s%s (%p)", space, prefix,
752 p_obj->psz_object_type, p_obj);
754 list = vlc_list_children(p_obj);
755 for(int i = 0; i < list->i_count ; i++)
757 l = SubDrawObject(p_sys, l, list->p_values[i].p_object, i_level,
758 (i == list->i_count - 1) ? "`-" : "|-" );
760 vlc_list_release(list);
764 static int DrawObjects(intf_thread_t *p_intf)
766 return SubDrawObject(p_intf->p_sys, 0, VLC_OBJECT(p_intf->p_libvlc), 0, "");
769 static int DrawMeta(intf_thread_t *p_intf)
771 intf_sys_t *p_sys = p_intf->p_sys;
772 input_thread_t *p_input = p_sys->p_input;
773 input_item_t *p_item;
779 p_item = input_GetItem(p_input);
780 vlc_mutex_lock(&p_item->lock);
781 for(int i=0; i<VLC_META_TYPE_COUNT; i++)
783 const char *psz_meta = vlc_meta_Get(p_item->p_meta, i);
784 if (!psz_meta || !*psz_meta)
787 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
788 MainBoxWrite(p_sys, l++, " [%s]",
789 vlc_meta_TypeToLocalizedString(i));
790 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
791 MainBoxWrite(p_sys, l++, " %s", psz_meta);
793 vlc_mutex_unlock(&p_item->lock);
798 static int DrawInfo(intf_thread_t *p_intf)
800 intf_sys_t *p_sys = p_intf->p_sys;
801 input_thread_t *p_input = p_sys->p_input;
802 input_item_t *p_item;
808 p_item = input_GetItem(p_input);
809 vlc_mutex_lock(&p_item->lock);
810 for(int i = 0; i < p_item->i_categories; i++)
812 info_category_t *p_category = p_item->pp_categories[i];
813 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
814 MainBoxWrite(p_sys, l++, _(" [%s]"), p_category->psz_name);
815 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
816 for(int j = 0; j < p_category->i_infos; j++)
818 info_t *p_info = p_category->pp_infos[j];
819 MainBoxWrite(p_sys, l++, _(" %s: %s"),
820 p_info->psz_name, p_info->psz_value);
823 vlc_mutex_unlock(&p_item->lock);
828 static int DrawStats(intf_thread_t *p_intf)
830 intf_sys_t *p_sys = p_intf->p_sys;
831 input_thread_t *p_input = p_sys->p_input;
832 input_item_t *p_item;
833 input_stats_t *p_stats;
834 int l = 0, i_audio = 0, i_video = 0;
839 p_item = input_GetItem(p_input);
842 vlc_mutex_lock(&p_item->lock);
843 p_stats = p_item->p_stats;
844 vlc_mutex_lock(&p_stats->lock);
846 for(int i = 0; i < p_item->i_es ; i++)
848 i_audio += (p_item->es[i]->i_cat == AUDIO_ES);
849 i_video += (p_item->es[i]->i_cat == VIDEO_ES);
853 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
854 MainBoxWrite(p_sys, l++, _(" [Incoming]"));
855 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
856 MainBoxWrite(p_sys, l++, _(" input bytes read : %8.0f KiB"),
857 (float)(p_stats->i_read_bytes)/1024);
858 MainBoxWrite(p_sys, l++, _(" input bitrate : %6.0f kb/s"),
859 p_stats->f_input_bitrate*8000);
860 MainBoxWrite(p_sys, l++, _(" demux bytes read : %8.0f KiB"),
861 (float)(p_stats->i_demux_read_bytes)/1024);
862 MainBoxWrite(p_sys, l++, _(" demux bitrate : %6.0f kb/s"),
863 p_stats->f_demux_bitrate*8000);
868 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
869 MainBoxWrite(p_sys, l++, _(" [Video Decoding]"));
870 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
871 MainBoxWrite(p_sys, l++, _(" video decoded : %"PRId64),
872 p_stats->i_decoded_video);
873 MainBoxWrite(p_sys, l++, _(" frames displayed : %"PRId64),
874 p_stats->i_displayed_pictures);
875 MainBoxWrite(p_sys, l++, _(" frames lost : %"PRId64),
876 p_stats->i_lost_pictures);
881 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
882 MainBoxWrite(p_sys, l++, _(" [Audio Decoding]"));
883 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
884 MainBoxWrite(p_sys, l++, _(" audio decoded : %"PRId64),
885 p_stats->i_decoded_audio);
886 MainBoxWrite(p_sys, l++, _(" buffers played : %"PRId64),
887 p_stats->i_played_abuffers);
888 MainBoxWrite(p_sys, l++, _(" buffers lost : %"PRId64),
889 p_stats->i_lost_abuffers);
892 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
893 MainBoxWrite(p_sys, l++, _(" [Streaming]"));
894 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
895 MainBoxWrite(p_sys, l++, _(" packets sent : %5i"), p_stats->i_sent_packets);
896 MainBoxWrite(p_sys, l++, _(" bytes sent : %8.0f KiB"),
897 (float)(p_stats->i_sent_bytes)/1025);
898 MainBoxWrite(p_sys, l++, _(" sending bitrate : %6.0f kb/s"),
899 p_stats->f_send_bitrate*8000);
900 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
902 vlc_mutex_unlock(&p_stats->lock);
903 vlc_mutex_unlock(&p_item->lock);
908 static int DrawHelp(intf_thread_t *p_intf)
910 intf_sys_t *p_sys = p_intf->p_sys;
913 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
914 MainBoxWrite(p_sys, l++, _("[Display]"));
915 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
916 MainBoxWrite(p_sys, l++, _(" h,H Show/Hide help box"));
917 MainBoxWrite(p_sys, l++, _(" i Show/Hide info box"));
918 MainBoxWrite(p_sys, l++, _(" m Show/Hide metadata box"));
919 MainBoxWrite(p_sys, l++, _(" L Show/Hide messages box"));
920 MainBoxWrite(p_sys, l++, _(" P Show/Hide playlist box"));
921 MainBoxWrite(p_sys, l++, _(" B Show/Hide filebrowser"));
922 MainBoxWrite(p_sys, l++, _(" x Show/Hide objects box"));
923 MainBoxWrite(p_sys, l++, _(" S Show/Hide statistics box"));
924 MainBoxWrite(p_sys, l++, _(" Esc Close Add/Search entry"));
925 MainBoxWrite(p_sys, l++, _(" Ctrl-l Refresh the screen"));
926 MainBoxWrite(p_sys, l++, "");
928 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
929 MainBoxWrite(p_sys, l++, _("[Global]"));
930 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
931 MainBoxWrite(p_sys, l++, _(" q, Q, Esc Quit"));
932 MainBoxWrite(p_sys, l++, _(" s Stop"));
933 MainBoxWrite(p_sys, l++, _(" <space> Pause/Play"));
934 MainBoxWrite(p_sys, l++, _(" f Toggle Fullscreen"));
935 MainBoxWrite(p_sys, l++, _(" n, p Next/Previous playlist item"));
936 MainBoxWrite(p_sys, l++, _(" [, ] Next/Previous title"));
937 MainBoxWrite(p_sys, l++, _(" <, > Next/Previous chapter"));
938 MainBoxWrite(p_sys, l++, _(" ←, → Seek -/+ 1%%"));
939 MainBoxWrite(p_sys, l++, _(" a, z Volume Up/Down"));
940 MainBoxWrite(p_sys, l++, "");
942 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
943 MainBoxWrite(p_sys, l++, _("[Playlist]"));
944 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
945 MainBoxWrite(p_sys, l++, _(" r Toggle Random playing"));
946 MainBoxWrite(p_sys, l++, _(" l Toggle Loop Playlist"));
947 MainBoxWrite(p_sys, l++, _(" R Toggle Repeat item"));
948 MainBoxWrite(p_sys, l++, _(" o Order Playlist by title"));
949 MainBoxWrite(p_sys, l++, _(" O Reverse order Playlist by title"));
950 MainBoxWrite(p_sys, l++, _(" g Go to the current playing item"));
951 MainBoxWrite(p_sys, l++, _(" / Look for an item"));
952 MainBoxWrite(p_sys, l++, _(" A Add an entry"));
953 MainBoxWrite(p_sys, l++, _(" D, ⌫, <del> Delete an entry"));
954 MainBoxWrite(p_sys, l++, _(" e Eject (if stopped)"));
955 MainBoxWrite(p_sys, l++, "");
957 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
958 MainBoxWrite(p_sys, l++, _("[Filebrowser]"));
959 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
960 MainBoxWrite(p_sys, l++, _(" <enter> Add the selected file to the playlist"));
961 MainBoxWrite(p_sys, l++, _(" <space> Add the selected directory to the playlist"));
962 MainBoxWrite(p_sys, l++, _(" . Show/Hide hidden files"));
963 MainBoxWrite(p_sys, l++, "");
965 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
966 MainBoxWrite(p_sys, l++, _("[Boxes]"));
967 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
968 MainBoxWrite(p_sys, l++, _(" ↑,↓ Navigate through the box line by line"));
969 MainBoxWrite(p_sys, l++, _(" ⇞,⇟ Navigate through the box page by page"));
970 MainBoxWrite(p_sys, l++, _(" ↖,↘ Navigate to start/end of box"));
971 MainBoxWrite(p_sys, l++, "");
973 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
974 MainBoxWrite(p_sys, l++, _("[Player]"));
975 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
976 MainBoxWrite(p_sys, l++, _(" ↑,↓ Seek +/-5%%"));
981 static int DrawBrowse(intf_thread_t *p_intf)
983 intf_sys_t *p_sys = p_intf->p_sys;
985 for(int i = 0; i < p_sys->i_dir_entries; i++)
987 struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
988 char type = p_dir_entry->b_file ? ' ' : '+';
991 color_set(p_dir_entry->b_file ? C_DEFAULT : C_FOLDER, NULL);
992 MainBoxWrite(p_sys, i, " %c %s", type, p_dir_entry->psz_path);
995 return p_sys->i_dir_entries;
998 static int DrawPlaylist(intf_thread_t *p_intf)
1000 intf_sys_t *p_sys = p_intf->p_sys;
1001 playlist_t *p_playlist = pl_Get(p_intf);
1003 if (p_sys->b_need_update)
1005 PlaylistRebuild(p_intf);
1006 p_sys->b_need_update = false;
1009 if (p_sys->b_plidx_follow)
1010 FindIndex(p_sys, p_playlist);
1012 for(int i = 0; i < p_sys->i_plist_entries; i++)
1015 playlist_item_t *p_current_item, *p_item, *p_node;
1016 input_thread_t *p_input2;
1018 p_item = p_sys->pp_plist[i]->p_item;
1019 p_node = p_sys->p_node;
1020 p_input2 = playlist_CurrentInput(p_playlist);
1024 p_current_item = playlist_CurrentPlayingItem(p_playlist);
1025 if ((p_node && p_item->p_input == p_node->p_input) ||
1026 (!p_node && p_input2 && p_current_item &&
1027 p_item->p_input == p_current_item->p_input))
1029 else if (p_item == p_node || (p_item != p_node &&
1030 PlaylistIsPlaying(p_playlist, p_item)))
1035 vlc_object_release(p_input2);
1037 if (p_sys->b_color) color_set(i%3 + C_PLAYLIST_1, NULL);
1038 MainBoxWrite(p_sys, i, "%c%s", c, p_sys->pp_plist[i]->psz_display);
1039 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
1042 return p_sys->i_plist_entries;
1045 static int DrawMessages(intf_thread_t *p_intf)
1047 intf_sys_t *p_sys = p_intf->p_sys;
1051 vlc_mutex_lock(&p_sys->msg_lock);
1055 msg_item_t *msg = p_sys->msgs[i];
1059 color_set(msg->i_type + C_INFO, NULL);
1060 MainBoxWrite(p_sys, l++, "[%s] %s", msg->psz_module, msg->psz_msg);
1063 if (++i == sizeof p_sys->msgs / sizeof *p_sys->msgs)
1066 if (i == p_sys->i_msgs) /* did we loop around the ring buffer ? */
1069 vlc_mutex_unlock(&p_sys->msg_lock);
1073 static int DrawStatus(intf_thread_t *p_intf)
1075 intf_sys_t *p_sys = p_intf->p_sys;
1076 input_thread_t *p_input = p_sys->p_input;
1077 playlist_t *p_playlist = pl_Get(p_intf);
1078 static const char name[] = "VLC media player "PACKAGE_VERSION;
1079 const size_t name_len = sizeof name - 1; /* without \0 termination */
1081 const char *repeat, *loop, *random;
1085 int padding = COLS - name_len; /* center title */
1090 if (p_sys->b_color) color_set(C_TITLE, NULL);
1091 DrawEmptyLine(y, 0, COLS);
1092 mvnprintw(y++, padding / 2, COLS, "%s", name);
1093 if (p_sys->b_color) color_set(C_STATUS, NULL);
1096 y++; /* leave a blank line */
1098 repeat = var_GetBool(p_playlist, "repeat") ? _("[Repeat] ") : "";
1099 random = var_GetBool(p_playlist, "random") ? _("[Random] ") : "";
1100 loop = var_GetBool(p_playlist, "loop") ? _("[Loop]") : "";
1102 if (p_input && !p_input->b_dead)
1106 char *psz_uri = input_item_GetURI(input_GetItem(p_input));
1107 mvnprintw(y++, 0, COLS, _(" Source : %s"), psz_uri);
1110 var_Get(p_input, "state", &val);
1113 static const char *input_state[] = {
1114 [PLAYING_S] = " State : Playing %s%s%s",
1115 [OPENING_S] = " State : Opening/Connecting %s%s%s",
1116 [PAUSE_S] = " State : Paused %s%s%s",
1118 char buf1[MSTRTIME_MAX_SIZE];
1119 char buf2[MSTRTIME_MAX_SIZE];
1120 audio_volume_t i_volume;
1130 mvnprintw(y++, 0, COLS, _(input_state[val.i_int]),
1131 repeat, random, loop);
1134 var_Get(p_input, "time", &val);
1135 secstotimestr(buf1, val.i_time / CLOCK_FREQ);
1136 var_Get(p_input, "length", &val);
1137 secstotimestr(buf2, val.i_time / CLOCK_FREQ);
1139 mvnprintw(y++, 0, COLS, _(" Position : %s/%s"), buf1, buf2);
1141 aout_VolumeGet(p_playlist, &i_volume);
1142 mvnprintw(y++, 0, COLS, _(" Volume : %i%%"),
1143 i_volume*200/AOUT_VOLUME_MAX);
1145 if (!var_Get(p_input, "title", &val))
1147 int i_title_count = var_CountChoices(p_input, "title");
1148 if (i_title_count > 0)
1149 mvnprintw(y++, 0, COLS, _(" Title : %"PRId64"/%d"),
1150 val.i_int, i_title_count);
1153 if (!var_Get(p_input, "chapter", &val))
1155 int i_chapter_count = var_CountChoices(p_input, "chapter");
1156 if (i_chapter_count > 0)
1157 mvnprintw(y++, 0, COLS, _(" Chapter : %"PRId64"/%d"),
1158 val.i_int, i_chapter_count);
1164 mvnprintw(y++, 0, COLS, _(" Source: <no current item> "));
1165 mvnprintw(y++, 0, COLS, " %s%s%s", repeat, random, loop);
1166 mvnprintw(y++, 0, COLS, _(" [ h for help ]"));
1167 DrawEmptyLine(y++, 0, COLS);
1170 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
1171 DrawBox(y++, 1, p_sys->b_color, ""); /* position slider */
1172 DrawEmptyLine(y, 1, COLS-2);
1174 DrawLine(y, 1, (int)((COLS-2) * var_GetFloat(p_input, "position")));
1176 y += 2; /* skip slider and box */
1181 static void FillTextBox(intf_sys_t *p_sys)
1183 int width = COLS - 2;
1184 const char *title = p_sys->i_box_type == BOX_OPEN ? "Open: %s" : "Find: %s";
1185 char *chain = p_sys->i_box_type == BOX_OPEN ? p_sys->psz_open_chain :
1186 p_sys->psz_old_search ? p_sys->psz_old_search :
1187 p_sys->psz_search_chain;
1189 DrawEmptyLine(7, 1, width);
1190 mvnprintw(7, 1, width, _(title), chain);
1193 static void FillBox(intf_thread_t *p_intf)
1195 intf_sys_t *p_sys = p_intf->p_sys;
1196 static int (* const draw[]) (intf_thread_t *) = {
1197 [BOX_HELP] = DrawHelp,
1198 [BOX_INFO] = DrawInfo,
1199 [BOX_META] = DrawMeta,
1200 [BOX_OBJECTS] = DrawObjects,
1201 [BOX_STATS] = DrawStats,
1202 [BOX_BROWSE] = DrawBrowse,
1203 [BOX_PLAYLIST] = DrawPlaylist,
1204 [BOX_SEARCH] = DrawPlaylist,
1205 [BOX_OPEN] = DrawPlaylist,
1206 [BOX_LOG] = DrawMessages,
1209 p_sys->i_box_lines_total = draw[p_sys->i_box_type](p_intf);
1211 if (p_sys->i_box_type == BOX_SEARCH || p_sys->i_box_type == BOX_OPEN)
1215 static void Redraw(intf_thread_t *p_intf, time_t *t_last_refresh)
1217 intf_sys_t *p_sys = p_intf->p_sys;
1218 int box = p_sys->i_box_type;
1219 int y = DrawStatus(p_intf);
1221 p_sys->i_box_height = LINES - y - 2;
1222 DrawBox(y++, p_sys->i_box_height, p_sys->b_color, _(box_title[box]));
1226 if (box != BOX_NONE)
1230 if (p_sys->i_box_start > p_sys->i_box_lines_total - 1)
1231 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1232 y += __MIN(p_sys->i_box_lines_total - p_sys->i_box_start,
1233 p_sys->i_box_height);
1236 while (y < LINES - 1)
1237 DrawEmptyLine(y++, 1, COLS - 2);
1240 *t_last_refresh = time(0);
1243 static void ChangePosition(intf_thread_t *p_intf, float increment)
1245 intf_sys_t *p_sys = p_intf->p_sys;
1246 input_thread_t *p_input = p_sys->p_input;
1249 if (!p_input || var_GetInteger(p_input, "state") != PLAYING_S)
1252 pos = var_GetFloat(p_input, "position") + increment;
1254 if (pos > 0.99) pos = 0.99;
1255 if (pos < 0.0) pos = 0.0;
1257 var_SetFloat(p_input, "position", pos);
1260 static inline void RemoveLastUTF8Entity(char *psz, int len)
1262 while (len && ((psz[--len] & 0xc0) == 0x80)) /* UTF8 continuation byte */
1267 static char *GetDiscDevice(intf_thread_t *p_intf, const char *name)
1269 static const struct { const char *s; size_t n; const char *v; } devs[] =
1271 { "cdda://", 7, "cd-audio", },
1272 { "dvd://", 6, "dvd", },
1273 { "vcd://", 6, "vcd", },
1277 for (unsigned i = 0; i < sizeof devs / sizeof *devs; i++)
1279 size_t n = devs[i].n;
1280 if (!strncmp(name, devs[i].s, n))
1285 return config_GetPsz(p_intf, devs[i].v);
1287 /* Omit the beginning MRL-selector characters */
1288 return strdup(name + n);
1292 device = strdup(name);
1294 if (device) /* Remove what we have after @ */
1295 device[strcspn(device, "@")] = '\0';
1300 static void Eject(intf_thread_t *p_intf)
1302 char *psz_device, *psz_name;
1303 playlist_t * p_playlist = pl_Get(p_intf);
1305 /* If there's a stream playing, we aren't allowed to eject ! */
1306 if (p_intf->p_sys->p_input)
1311 if (!playlist_CurrentPlayingItem(p_playlist))
1317 psz_name = playlist_CurrentPlayingItem(p_playlist)->p_input->psz_name;
1318 psz_device = psz_name ? GetDiscDevice(p_intf, psz_name) : NULL;
1324 intf_Eject(p_intf, psz_device);
1329 static void PlayPause(intf_thread_t *p_intf)
1331 input_thread_t *p_input = p_intf->p_sys->p_input;
1335 int64_t state = var_GetInteger( p_input, "state" );
1336 state = (state != PLAYING_S) ? PLAYING_S : PAUSE_S;
1337 var_SetInteger( p_input, "state", state );
1340 playlist_Play(pl_Get(p_intf));
1343 static inline void BoxSwitch(intf_sys_t *p_sys, int box)
1345 p_sys->i_box_type = (p_sys->i_box_type == box) ? BOX_NONE : box;
1346 p_sys->i_box_start = 0;
1347 p_sys->i_box_idx = 0;
1350 static bool HandlePlaylistKey(intf_thread_t *p_intf, int key)
1352 intf_sys_t *p_sys = p_intf->p_sys;
1353 playlist_t *p_playlist = pl_Get(p_intf);
1354 struct pl_item_t *p_pl_item;
1358 /* Playlist Settings */
1359 case 'r': var_ToggleBool(p_playlist, "random"); return true;
1360 case 'l': var_ToggleBool(p_playlist, "loop"); return true;
1361 case 'R': var_ToggleBool(p_playlist, "repeat"); return true;
1366 playlist_RecursiveNodeSort(p_playlist, PlaylistGetRoot(p_intf),
1367 SORT_TITLE_NODES_FIRST,
1368 (key == 'o')? ORDER_NORMAL : ORDER_REVERSE);
1369 p_sys->b_need_update = true;
1374 p_sys->category_view = !p_sys->category_view;
1375 p_sys->b_need_update = true;
1379 FindIndex(p_sys, p_playlist);
1388 playlist_item_t *p_item;
1391 p_item = p_sys->pp_plist[p_sys->i_box_idx]->p_item;
1392 if (p_item->i_children == -1)
1393 playlist_DeleteFromInput(p_playlist, p_item->p_input, pl_Locked);
1395 playlist_NodeDelete(p_playlist, p_item, true , false);
1397 p_sys->b_need_update = true;
1404 if (!(p_pl_item = p_sys->pp_plist[p_sys->i_box_idx]))
1407 if (p_pl_item->p_item->i_children)
1409 playlist_item_t *p_item, *p_parent = p_pl_item->p_item;
1410 if (p_parent->i_children == -1)
1414 while (p_parent->p_parent)
1415 p_parent = p_parent->p_parent;
1419 p_sys->p_node = p_parent;
1423 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked,
1427 { /* We only want to set the current node */
1428 playlist_Stop(p_playlist);
1429 p_sys->p_node = p_pl_item->p_item;
1432 p_sys->b_plidx_follow = true;
1439 static bool HandleBrowseKey(intf_thread_t *p_intf, int key)
1441 intf_sys_t *p_sys = p_intf->p_sys;
1442 struct dir_entry_t *dir_entry;
1447 p_sys->b_show_hidden_files = !p_sys->b_show_hidden_files;
1455 dir_entry = p_sys->pp_dir_entries[p_sys->i_box_idx];
1457 if (!dir_entry->b_file && key != ' ')
1459 char *current_dir = p_sys->psz_current_dir;
1460 if (asprintf(&p_sys->psz_current_dir, "%s/%s",
1461 p_sys->psz_current_dir, dir_entry->psz_path) != -1)
1467 p_sys->psz_current_dir = current_dir;
1469 p_sys->i_box_start = 0;
1470 p_sys->i_box_idx = 0;
1475 if (asprintf(&psz_uri, "%s://%s/%s",
1476 dir_entry->b_file ? "file" : "directory",
1477 p_sys->psz_current_dir, dir_entry->psz_path) == -1)
1482 playlist_t *p_playlist = pl_Get(p_intf);
1483 playlist_item_t *p_parent = p_sys->p_node;
1486 playlist_item_t *p_item;
1488 p_item = playlist_CurrentPlayingItem(p_playlist);
1489 p_parent = p_item ? p_item->p_parent : NULL;
1492 p_parent = p_playlist->p_local_onelevel;
1495 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1496 p_parent = p_parent->p_parent;
1498 input_item_t *p_input = p_playlist->p_local_onelevel->p_input;
1499 playlist_Add(p_playlist, psz_uri, NULL, PLAYLIST_APPEND,
1500 PLAYLIST_END, p_parent->p_input == p_input, false);
1502 p_sys->i_box_type = BOX_PLAYLIST;
1510 static bool HandleEditBoxKey(intf_thread_t *p_intf, int key, int box)
1512 intf_sys_t *p_sys = p_intf->p_sys;
1513 bool search = box == BOX_SEARCH;
1514 char *str = search ? p_sys->psz_search_chain: p_sys->psz_open_chain;
1515 size_t len = strlen(str);
1517 assert(box == BOX_SEARCH || box == BOX_OPEN);
1522 case KEY_CLEAR: clear(); return 1;
1530 p_sys->psz_old_search = strdup(p_sys->psz_search_chain);
1531 else if (p_sys->psz_old_search)
1532 SearchPlaylist(p_sys, p_sys->psz_old_search);
1536 playlist_t *p_playlist = pl_Get(p_intf);
1537 playlist_item_t *p_parent = p_sys->p_node, *p_current;
1542 p_current = playlist_CurrentPlayingItem(p_playlist);
1543 p_parent = p_current ? p_current->p_parent : NULL;
1545 p_parent = p_playlist->p_local_onelevel;
1548 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1549 p_parent = p_parent->p_parent;
1552 playlist_Add(p_playlist, p_sys->psz_open_chain, NULL,
1553 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
1554 p_parent->p_input == p_playlist->p_local_onelevel->p_input,
1557 p_sys->b_plidx_follow = true;
1559 p_sys->i_box_type = BOX_PLAYLIST;
1562 case 0x1b: /* ESC */
1563 /* Alt+key combinations return 2 keys in the terminal keyboard:
1564 * ESC, and the 2nd key.
1565 * If some other key is available immediately (where immediately
1566 * means after wgetch() 1 second delay), that means that the
1567 * ESC key was not pressed.
1569 * man 3X curs_getch says:
1571 * Use of the escape key by a programmer for a single
1572 * character function is discouraged, as it will cause a delay
1573 * of up to one second while the keypad code looks for a
1574 * following function-key sequence.
1580 if (search) p_sys->i_box_idx = p_sys->i_before_search;
1581 p_sys->i_box_type = BOX_PLAYLIST;
1586 RemoveLastUTF8Entity(str, len);
1590 if (len + 1 < search ? sizeof p_sys->psz_search_chain
1591 : sizeof p_sys->psz_open_chain)
1593 str[len + 0] = (char) key;
1594 str[len + 1] = '\0';
1600 free(p_sys->psz_old_search);
1601 p_sys->psz_old_search = NULL;
1602 SearchPlaylist(p_sys, str);
1607 static void InputNavigate(input_thread_t* p_input, const char *var)
1610 var_TriggerCallback(p_input, var);
1613 static bool HandleCommonKey(intf_thread_t *p_intf, int key)
1615 intf_sys_t *p_sys = p_intf->p_sys;
1616 playlist_t *p_playlist = pl_Get(p_intf);
1619 case 0x1b: /* ESC */
1626 libvlc_Quit(p_intf->p_libvlc);
1630 case 'H': BoxSwitch(p_sys, BOX_HELP); return true;
1631 case 'i': BoxSwitch(p_sys, BOX_INFO); return true;
1632 case 'm': BoxSwitch(p_sys, BOX_META); return true;
1633 case 'L': BoxSwitch(p_sys, BOX_LOG); return true;
1634 case 'P': BoxSwitch(p_sys, BOX_PLAYLIST); return true;
1635 case 'B': BoxSwitch(p_sys, BOX_BROWSE); return true;
1636 case 'x': BoxSwitch(p_sys, BOX_OBJECTS); return true;
1637 case 'S': BoxSwitch(p_sys, BOX_STATS); return true;
1639 case '/': /* Search */
1640 p_sys->psz_search_chain[0] = '\0';
1641 p_sys->b_plidx_follow = false;
1642 p_sys->i_before_search = p_sys->i_box_idx;
1643 p_sys->i_box_type = BOX_SEARCH;
1646 case 'A': /* Open */
1647 p_sys->psz_open_chain[0] = '\0';
1648 p_sys->i_box_type = BOX_OPEN;
1652 case KEY_RIGHT: ChangePosition(p_intf, +0.01); return true;
1653 case KEY_LEFT: ChangePosition(p_intf, -0.01); return true;
1655 /* Common control */
1659 vout_thread_t *p_vout = input_GetVout(p_sys->p_input);
1662 bool fs = var_ToggleBool(p_playlist, "fullscreen");
1663 var_SetBool(p_vout, "fullscreen", fs);
1664 vlc_object_release(p_vout);
1669 case ' ': PlayPause(p_intf); return true;
1670 case 's': playlist_Stop(p_playlist); return true;
1671 case 'e': Eject(p_intf); return true;
1673 case '[': InputNavigate(p_sys->p_input, "prev-title"); return true;
1674 case ']': InputNavigate(p_sys->p_input, "next-title"); return true;
1675 case '<': InputNavigate(p_sys->p_input, "prev-chapter"); return true;
1676 case '>': InputNavigate(p_sys->p_input, "next-chapter"); return true;
1678 case 'p': playlist_Prev(p_playlist); break;
1679 case 'n': playlist_Next(p_playlist); break;
1680 case 'a': aout_VolumeUp(p_playlist, 1, NULL); break;
1681 case 'z': aout_VolumeDown(p_playlist, 1, NULL); break;
1695 static bool HandleListKey(intf_thread_t *p_intf, int key)
1697 intf_sys_t *p_sys = p_intf->p_sys;
1698 playlist_t *p_playlist = pl_Get(p_intf);
1705 case KEY_END: p_sys->i_box_idx = p_sys->i_box_lines_total - 1; break;
1706 case KEY_HOME: p_sys->i_box_idx = 0; break;
1707 case KEY_UP: p_sys->i_box_idx--; break;
1708 case KEY_DOWN: p_sys->i_box_idx++; break;
1709 case KEY_PPAGE:p_sys->i_box_idx -= p_sys->i_box_height; break;
1710 case KEY_NPAGE:p_sys->i_box_idx += p_sys->i_box_height; break;
1717 if (p_sys->i_box_type == BOX_PLAYLIST)
1720 p_sys->b_plidx_follow = IsIndex(p_sys, p_playlist, p_sys->i_box_idx);
1727 static bool HandleKey(intf_thread_t *p_intf)
1729 intf_sys_t *p_sys = p_intf->p_sys;
1731 int box = p_sys->i_box_type;
1736 if (box == BOX_SEARCH || box == BOX_OPEN)
1737 return HandleEditBoxKey(p_intf, key, p_sys->i_box_type);
1739 if (box == BOX_NONE)
1745 case KEY_END: ChangePosition(p_intf, +.99); return true;
1746 case KEY_HOME: ChangePosition(p_intf, -1.0); return true;
1747 case KEY_UP: ChangePosition(p_intf, +0.05); return true;
1748 case KEY_DOWN: ChangePosition(p_intf, -0.05); return true;
1749 default: return HandleCommonKey(p_intf, key);
1752 if (box == BOX_BROWSE && HandleBrowseKey(p_intf, key))
1755 if (box == BOX_PLAYLIST && HandlePlaylistKey(p_intf, key))
1758 return HandleListKey(p_intf, key) || HandleCommonKey(p_intf, key);
1765 static void MsgCallback(msg_cb_data_t *data, msg_item_t *msg, unsigned i)
1767 intf_sys_t *p_sys = data->p_sys;
1768 (void)i; // what is this?
1769 int canc = vlc_savecancel();
1771 vlc_mutex_lock(&p_sys->msg_lock);
1773 if (p_sys->msgs[p_sys->i_msgs])
1774 msg_Release(p_sys->msgs[p_sys->i_msgs]);
1775 p_sys->msgs[p_sys->i_msgs++] = msg_Hold(msg);
1777 if (p_sys->i_msgs == (sizeof p_sys->msgs / sizeof *p_sys->msgs))
1780 vlc_mutex_unlock(&p_sys->msg_lock);
1782 vlc_restorecancel(canc);
1785 /*****************************************************************************
1786 * Run: ncurses thread
1787 *****************************************************************************/
1788 static void Run(intf_thread_t *p_intf)
1790 intf_sys_t *p_sys = p_intf->p_sys;
1791 playlist_t *p_playlist = pl_Get(p_intf);
1793 time_t t_last_refresh;
1794 int canc = vlc_savecancel();
1796 Redraw(p_intf, &t_last_refresh);
1798 var_AddCallback(p_playlist, "intf-change", PlaylistChanged, p_intf);
1799 var_AddCallback(p_playlist, "playlist-item-append", PlaylistChanged, p_intf);
1801 while (vlc_object_alive(p_intf))
1803 msleep(INTF_IDLE_SLEEP);
1805 if (!p_sys->p_input)
1807 p_sys->p_input = playlist_CurrentInput(p_playlist);
1809 Redraw(p_intf, &t_last_refresh);
1811 else if (p_sys->p_input->b_dead)
1813 vlc_object_release(p_sys->p_input);
1814 p_sys->p_input = NULL;
1817 while (HandleKey(p_intf))
1818 Redraw(p_intf, &t_last_refresh);
1820 if ((time(0) - t_last_refresh) >= 1)
1821 Redraw(p_intf, &t_last_refresh);
1823 var_DelCallback(p_playlist, "intf-change", PlaylistChanged, p_intf);
1824 var_DelCallback(p_playlist, "playlist-item-append", PlaylistChanged, p_intf);
1825 vlc_restorecancel(canc);
1828 /*****************************************************************************
1829 * Open: initialize and create window
1830 *****************************************************************************/
1831 static int Open(vlc_object_t *p_this)
1833 intf_thread_t *p_intf = (intf_thread_t *)p_this;
1834 intf_sys_t *p_sys = p_intf->p_sys = calloc(1, sizeof(intf_sys_t));
1835 struct msg_cb_data_t *msg_cb_data;
1840 msg_cb_data = malloc(sizeof *msg_cb_data);
1847 msg_cb_data->p_sys = p_sys;
1848 vlc_mutex_init(&p_sys->msg_lock);
1850 memset(p_sys->msgs, 0, sizeof p_sys->msgs);
1851 p_sys->p_sub = msg_Subscribe(p_intf->p_libvlc, MsgCallback, msg_cb_data);
1853 p_sys->i_box_type = BOX_PLAYLIST;
1854 p_sys->b_plidx_follow = true;
1855 p_sys->b_color = var_CreateGetBool(p_intf, "color");
1857 p_sys->category_view = true; //FIXME: switching back & forth is broken
1859 p_sys->psz_current_dir = var_CreateGetString(p_intf, "browse-dir");
1860 if (!p_sys->psz_current_dir || !*p_sys->psz_current_dir)
1862 free(p_sys->psz_current_dir);
1863 p_sys->psz_current_dir = config_GetUserDir(VLC_HOME_DIR);
1866 initscr(); /* Initialize the curses library */
1869 start_color_and_pairs(p_intf);
1871 keypad(stdscr, TRUE); /* Don't do NL -> CR/NL */
1872 nonl(); /* Take input chars one at a time */
1873 cbreak(); /* Don't echo */
1874 noecho(); /* Invisible cursor */
1875 curs_set(0); /* Non blocking wgetch() */
1879 /* Stop printing errors to the console */
1880 if(!freopen("/dev/null", "wb", stderr))
1881 msg_Err(p_intf, "Couldn't close stderr (%m)");
1884 PlaylistRebuild(p_intf),
1886 p_intf->pf_run = Run;
1890 /*****************************************************************************
1891 * Close: destroy interface window
1892 *****************************************************************************/
1893 static void Close(vlc_object_t *p_this)
1895 intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
1897 PlaylistDestroy(p_sys);
1900 free(p_sys->psz_current_dir);
1901 free(p_sys->psz_old_search);
1904 vlc_object_release(p_sys->p_input);
1906 endwin(); /* Close the ncurses interface */
1908 msg_Unsubscribe(p_sys->p_sub);
1909 vlc_mutex_destroy(&p_sys->msg_lock);
1910 for(unsigned i = 0; i < sizeof p_sys->msgs / sizeof *p_sys->msgs; i++)
1912 msg_Release(p_sys->msgs[i]);