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;
187 int i_box_y; // start of box content
189 int i_box_lines_total; // number of lines in the box
190 int i_box_start; // first line of box displayed
191 int i_box_idx; // selected line
193 msg_subscription_t *p_sub; // message bank subscription
194 msg_item_t *msgs[50]; // ring buffer
196 vlc_mutex_t msg_lock;
198 /* Search Box context */
199 char psz_search_chain[20];
200 char *psz_old_search;
203 /* Open Box Context */
204 char psz_open_chain[50];
206 /* File Browser context */
207 char *psz_current_dir;
209 struct dir_entry_t **pp_dir_entries;
210 bool b_show_hidden_files;
212 /* 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;
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 bool PlaylistAddChild(intf_sys_t *p_sys, playlist_item_t *p_child,
378 const char *c, const char d)
381 char *psz_name = input_item_GetTitleFbName(p_child->p_input);
382 struct pl_item_t *p_pl_item = malloc(sizeof *p_pl_item);
384 if (!psz_name || !p_pl_item)
387 p_pl_item->p_item = p_child;
390 ret = asprintf(&p_pl_item->psz_display, "%s%c-%s", c, d, psz_name);
392 ret = asprintf(&p_pl_item->psz_display, " %s", psz_name);
400 INSERT_ELEM(p_sys->pp_plist, p_sys->i_plist_entries,
401 p_sys->i_plist_entries, p_pl_item);
411 static void PlaylistAddNode(intf_sys_t *p_sys, playlist_item_t *p_node,
414 for(int k = 0; k < p_node->i_children; k++)
416 playlist_item_t *p_child = p_node->pp_children[k];
417 char d = k == p_node->i_children - 1 ? '`' : '|';
418 if(!PlaylistAddChild(p_sys, p_child, c, d))
421 if (p_child->i_children <= 0)
427 if (asprintf(&psz_tmp, "%s%c ", c,
428 k == p_node->i_children - 1 ? ' ' : '|') == -1)
430 PlaylistAddNode(p_sys, p_child, psz_tmp);
434 PlaylistAddNode(p_sys, p_child, " ");
438 static void PlaylistRebuild(intf_thread_t *p_intf)
440 intf_sys_t *p_sys = p_intf->p_sys;
441 playlist_t *p_playlist = pl_Get(p_intf);
444 PlaylistDestroy(p_sys);
445 PlaylistAddNode(p_sys, p_playlist->p_root_onelevel, "");
449 static int PlaylistChanged(vlc_object_t *p_this, const char *psz_variable,
450 vlc_value_t oval, vlc_value_t nval, void *param)
452 VLC_UNUSED(p_this); VLC_UNUSED(psz_variable);
453 VLC_UNUSED(oval); VLC_UNUSED(nval);
455 intf_thread_t *p_intf = (intf_thread_t *)param;
456 intf_sys_t *p_sys = p_intf->p_sys;
457 playlist_item_t *p_node = playlist_CurrentPlayingItem(pl_Get(p_intf));
459 p_sys->b_need_update = true;
460 p_sys->p_node = p_node ? p_node->p_parent : NULL;
466 static int SubSearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring,
467 int i_start, int i_stop)
469 for(int i = i_start + 1; i < i_stop; i++)
470 if (strcasestr(p_sys->pp_plist[i]->psz_display, psz_searchstring))
476 static void SearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring)
478 int i_item, i_first = p_sys->i_before_search;
483 if (!psz_searchstring || !*psz_searchstring)
485 p_sys->i_box_idx = p_sys->i_before_search;
489 i_item = SubSearchPlaylist(p_sys, psz_searchstring, i_first + 1,
490 p_sys->i_plist_entries);
492 i_item = SubSearchPlaylist(p_sys, psz_searchstring, 0, i_first);
496 p_sys->i_box_idx = i_item;
501 static inline bool IsIndex(intf_sys_t *p_sys, playlist_t *p_playlist, int i)
503 playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
504 playlist_item_t *p_played_item;
508 if (p_item->i_children == 0 && p_item == p_sys->p_node)
511 p_played_item = playlist_CurrentPlayingItem(p_playlist);
512 if (p_played_item && p_item->p_input && p_played_item->p_input)
513 return p_item->p_input->i_id == p_played_item->p_input->i_id;
518 static void FindIndex(intf_sys_t *p_sys, playlist_t *p_playlist)
520 int plidx = p_sys->i_box_idx;
521 int max = p_sys->i_plist_entries;
525 if (!IsIndex(p_sys, p_playlist, plidx))
526 for(int i = 0; i < max; i++)
527 if (IsIndex(p_sys, p_playlist, i))
529 p_sys->i_box_idx = i;
536 p_sys->b_plidx_follow = true;
539 /****************************************************************************
541 ****************************************************************************/
543 static void start_color_and_pairs(intf_thread_t *p_intf)
547 p_intf->p_sys->b_color = false;
548 msg_Warn(p_intf, "Terminal doesn't support colors");
553 for(int i = C_DEFAULT + 1; i < C_MAX; i++)
554 init_pair(i, color_pairs[i].f, color_pairs[i].b);
556 /* untested, in all my terminals, !can_change_color() --funman */
557 if (can_change_color())
558 init_color(COLOR_YELLOW, 960, 500, 0); /* YELLOW -> ORANGE */
561 static void DrawBox(int y, int h, bool b_color, const char *title)
566 if (w <= 3 || h <= 0)
569 if (b_color) color_set(C_BOX, NULL);
571 if (!title) title = "";
572 i_len = strlen(title);
577 mvaddch(y, 0, ACS_ULCORNER);
578 mvhline(y, 1, ACS_HLINE, (w-i_len-2)/2);
579 mvprintw(y, 1+(w-i_len-2)/2, "%s", title);
580 mvhline(y, (w-i_len)/2+i_len, ACS_HLINE, w - 1 - ((w-i_len)/2+i_len));
581 mvaddch(y, w-1,ACS_URCORNER);
583 for(int i = 0; i < h; i++)
585 mvaddch(++y, 0, ACS_VLINE);
586 mvaddch(y, w-1, ACS_VLINE);
589 mvaddch(++y, 0, ACS_LLCORNER);
590 mvhline(y, 1, ACS_HLINE, w - 2);
591 mvaddch(y, w-1, ACS_LRCORNER);
592 if (b_color) color_set(C_DEFAULT, NULL);
595 static void DrawEmptyLine(int y, int x, int w)
599 mvhline(y, x, ' ', w);
602 static void DrawLine(int y, int x, int w)
607 mvhline(y, x, ' ', w);
611 static void mvnprintw(int y, int x, int w, const char *p_fmt, ...)
620 va_start(vl_args, p_fmt);
621 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
625 i_len = strlen(p_buf);
627 wchar_t psz_wide[i_len + 1];
630 size_t i_char_len = mbstowcs(psz_wide, p_buf, i_len);
632 size_t i_width; /* number of columns */
634 if (i_char_len == (size_t)-1) /* an invalid character was encountered */
640 i_width = wcswidth(psz_wide, i_char_len);
641 if (i_width == (size_t)-1)
643 /* a non printable character was encountered */
645 for(unsigned i = 0 ; i < i_char_len ; i++)
647 int i_cwidth = wcwidth(psz_wide[i]);
653 if (i_width <= (size_t)w)
655 mvprintw(y, x, "%s", p_buf);
656 mvhline(y, x + i_width, ' ', w - i_width);
661 int i_total_width = 0;
663 while (i_total_width < w)
665 i_total_width += wcwidth(psz_wide[i]);
666 if (w > 7 && i_total_width >= w/2)
670 i_total_width -= wcwidth(psz_wide[i]) - 2;
673 /* we require this check only if at least one character
674 * 4 or more columns wide exists (which i doubt) */
676 i_total_width -= wcwidth(psz_wide[i-1]) - 1;
679 /* find the widest string */
680 int j, i_2nd_width = 0;
681 for(j = i_char_len - 1; i_2nd_width < w - i_total_width; j--)
682 i_2nd_width += wcwidth(psz_wide[j]);
684 /* we already have i_total_width columns filled, and we can't
685 * have more than w columns */
686 if (i_2nd_width > w - i_total_width)
689 wmemmove(&psz_wide[i+2], &psz_wide[j+1], i_char_len - j - 1);
690 psz_wide[i + 2 + i_char_len - j - 1] = '\0';
695 if (w <= 7) /* we don't add the '...' else we lose too much chars */
698 size_t i_wlen = wcslen(psz_wide) * 6 + 1; /* worst case */
699 char psz_ellipsized[i_wlen];
700 wcstombs(psz_ellipsized, psz_wide, i_wlen);
701 mvprintw(y, x, "%s", psz_ellipsized);
706 static void MainBoxWrite(intf_sys_t *p_sys, int l, const char *p_fmt, ...)
710 bool b_selected = l == p_sys->i_box_idx;
712 if (l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_height)
715 va_start(vl_args, p_fmt);
716 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
720 if (b_selected) attron(A_REVERSE);
721 mvnprintw(p_sys->i_box_y + l - p_sys->i_box_start, 1, COLS - 2, "%s", p_buf);
722 if (b_selected) attroff(A_REVERSE);
727 static int SubDrawObject(intf_sys_t *p_sys, int l, vlc_object_t *p_obj, int i_level, const char *prefix)
729 int x = 2 * i_level++;
730 char *psz_name = vlc_object_get_name(p_obj);
733 memset(space, ' ', x);
738 MainBoxWrite(p_sys, l++, "%s%s%s \"%s\" (%p)", space, prefix,
739 p_obj->psz_object_type, psz_name, p_obj);
743 MainBoxWrite(p_sys, l++, "%s%s%s (%p)", space, prefix,
744 p_obj->psz_object_type, p_obj);
746 list = vlc_list_children(p_obj);
747 for(int i = 0; i < list->i_count ; i++)
749 l = SubDrawObject(p_sys, l, list->p_values[i].p_object, i_level,
750 (i == list->i_count - 1) ? "`-" : "|-" );
752 vlc_list_release(list);
756 static int DrawObjects(intf_thread_t *p_intf)
758 return SubDrawObject(p_intf->p_sys, 0, VLC_OBJECT(p_intf->p_libvlc), 0, "");
761 static int DrawMeta(intf_thread_t *p_intf)
763 intf_sys_t *p_sys = p_intf->p_sys;
764 input_thread_t *p_input = p_sys->p_input;
765 input_item_t *p_item;
771 p_item = input_GetItem(p_input);
772 vlc_mutex_lock(&p_item->lock);
773 for(int i=0; i<VLC_META_TYPE_COUNT; i++)
775 const char *psz_meta = vlc_meta_Get(p_item->p_meta, i);
776 if (!psz_meta || !*psz_meta)
779 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
780 MainBoxWrite(p_sys, l++, " [%s]",
781 vlc_meta_TypeToLocalizedString(i));
782 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
783 MainBoxWrite(p_sys, l++, " %s", psz_meta);
785 vlc_mutex_unlock(&p_item->lock);
790 static int DrawInfo(intf_thread_t *p_intf)
792 intf_sys_t *p_sys = p_intf->p_sys;
793 input_thread_t *p_input = p_sys->p_input;
794 input_item_t *p_item;
800 p_item = input_GetItem(p_input);
801 vlc_mutex_lock(&p_item->lock);
802 for(int i = 0; i < p_item->i_categories; i++)
804 info_category_t *p_category = p_item->pp_categories[i];
805 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
806 MainBoxWrite(p_sys, l++, _(" [%s]"), p_category->psz_name);
807 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
808 for(int j = 0; j < p_category->i_infos; j++)
810 info_t *p_info = p_category->pp_infos[j];
811 MainBoxWrite(p_sys, l++, _(" %s: %s"),
812 p_info->psz_name, p_info->psz_value);
815 vlc_mutex_unlock(&p_item->lock);
820 static int DrawStats(intf_thread_t *p_intf)
822 intf_sys_t *p_sys = p_intf->p_sys;
823 input_thread_t *p_input = p_sys->p_input;
824 input_item_t *p_item;
825 input_stats_t *p_stats;
826 int l = 0, i_audio = 0, i_video = 0;
831 p_item = input_GetItem(p_input);
834 vlc_mutex_lock(&p_item->lock);
835 p_stats = p_item->p_stats;
836 vlc_mutex_lock(&p_stats->lock);
838 for(int i = 0; i < p_item->i_es ; i++)
840 i_audio += (p_item->es[i]->i_cat == AUDIO_ES);
841 i_video += (p_item->es[i]->i_cat == VIDEO_ES);
845 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
846 MainBoxWrite(p_sys, l++, _(" [Incoming]"));
847 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
848 MainBoxWrite(p_sys, l++, _(" input bytes read : %8.0f KiB"),
849 (float)(p_stats->i_read_bytes)/1024);
850 MainBoxWrite(p_sys, l++, _(" input bitrate : %6.0f kb/s"),
851 p_stats->f_input_bitrate*8000);
852 MainBoxWrite(p_sys, l++, _(" demux bytes read : %8.0f KiB"),
853 (float)(p_stats->i_demux_read_bytes)/1024);
854 MainBoxWrite(p_sys, l++, _(" demux bitrate : %6.0f kb/s"),
855 p_stats->f_demux_bitrate*8000);
860 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
861 MainBoxWrite(p_sys, l++, _(" [Video Decoding]"));
862 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
863 MainBoxWrite(p_sys, l++, _(" video decoded : %"PRId64),
864 p_stats->i_decoded_video);
865 MainBoxWrite(p_sys, l++, _(" frames displayed : %"PRId64),
866 p_stats->i_displayed_pictures);
867 MainBoxWrite(p_sys, l++, _(" frames lost : %"PRId64),
868 p_stats->i_lost_pictures);
873 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
874 MainBoxWrite(p_sys, l++, _(" [Audio Decoding]"));
875 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
876 MainBoxWrite(p_sys, l++, _(" audio decoded : %"PRId64),
877 p_stats->i_decoded_audio);
878 MainBoxWrite(p_sys, l++, _(" buffers played : %"PRId64),
879 p_stats->i_played_abuffers);
880 MainBoxWrite(p_sys, l++, _(" buffers lost : %"PRId64),
881 p_stats->i_lost_abuffers);
884 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
885 MainBoxWrite(p_sys, l++, _(" [Streaming]"));
886 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
887 MainBoxWrite(p_sys, l++, _(" packets sent : %5i"), p_stats->i_sent_packets);
888 MainBoxWrite(p_sys, l++, _(" bytes sent : %8.0f KiB"),
889 (float)(p_stats->i_sent_bytes)/1025);
890 MainBoxWrite(p_sys, l++, _(" sending bitrate : %6.0f kb/s"),
891 p_stats->f_send_bitrate*8000);
892 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
894 vlc_mutex_unlock(&p_stats->lock);
895 vlc_mutex_unlock(&p_item->lock);
900 static int DrawHelp(intf_thread_t *p_intf)
902 intf_sys_t *p_sys = p_intf->p_sys;
905 #define H(a) MainBoxWrite(p_sys, l++, a)
907 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
909 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
910 H(_(" h,H Show/Hide help box"));
911 H(_(" i Show/Hide info box"));
912 H(_(" m Show/Hide metadata box"));
913 H(_(" L Show/Hide messages box"));
914 H(_(" P Show/Hide playlist box"));
915 H(_(" B Show/Hide filebrowser"));
916 H(_(" x Show/Hide objects box"));
917 H(_(" S Show/Hide statistics box"));
918 H(_(" Esc Close Add/Search entry"));
919 H(_(" Ctrl-l Refresh the screen"));
922 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
924 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
925 H(_(" q, Q, Esc Quit"));
927 H(_(" <space> Pause/Play"));
928 H(_(" f Toggle Fullscreen"));
929 H(_(" n, p Next/Previous playlist item"));
930 H(_(" [, ] Next/Previous title"));
931 H(_(" <, > Next/Previous chapter"));
932 /* xgettext: You can use ← and → characters */
933 H(_(" <left>,<right> Seek -/+ 1%%"));
934 H(_(" a, z Volume Up/Down"));
935 /* xgettext: You can use ↑ and ↓ characters */
936 H(_(" <up>,<down> Navigate through the box line by line"));
937 /* xgettext: You can use ⇞ and ⇟ characters */
938 H(_(" <pageup>,<pagedown> Navigate through the box page by page"));
939 /* xgettext: You can use ↖ and ↘ characters */
940 H(_(" <start>,<end> Navigate to start/end of box"));
943 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
945 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
946 H(_(" r Toggle Random playing"));
947 H(_(" l Toggle Loop Playlist"));
948 H(_(" R Toggle Repeat item"));
949 H(_(" o Order Playlist by title"));
950 H(_(" O Reverse order Playlist by title"));
951 H(_(" g Go to the current playing item"));
952 H(_(" / Look for an item"));
953 H(_(" A Add an entry"));
954 /* xgettext: You can use ⌫ character to translate <backspace> */
955 H(_(" D, <backspace>, <del> Delete an entry"));
956 H(_(" e Eject (if stopped)"));
959 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
960 H(_("[Filebrowser]"));
961 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
962 H(_(" <enter> Add the selected file to the playlist"));
963 H(_(" <space> Add the selected directory to the playlist"));
964 H(_(" . Show/Hide hidden files"));
967 if (p_sys->b_color) color_set(C_CATEGORY, NULL);
969 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
970 /* xgettext: You can use ↑ and ↓ characters */
971 H(_(" <up>,<down> Seek +/-5%%"));
977 static int DrawBrowse(intf_thread_t *p_intf)
979 intf_sys_t *p_sys = p_intf->p_sys;
981 for(int i = 0; i < p_sys->i_dir_entries; i++)
983 struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
984 char type = p_dir_entry->b_file ? ' ' : '+';
987 color_set(p_dir_entry->b_file ? C_DEFAULT : C_FOLDER, NULL);
988 MainBoxWrite(p_sys, i, " %c %s", type, p_dir_entry->psz_path);
991 return p_sys->i_dir_entries;
994 static int DrawPlaylist(intf_thread_t *p_intf)
996 intf_sys_t *p_sys = p_intf->p_sys;
997 playlist_t *p_playlist = pl_Get(p_intf);
999 if (p_sys->b_need_update)
1001 PlaylistRebuild(p_intf);
1002 p_sys->b_need_update = false;
1005 if (p_sys->b_plidx_follow)
1006 FindIndex(p_sys, p_playlist);
1008 for(int i = 0; i < p_sys->i_plist_entries; i++)
1011 playlist_item_t *p_current_item;
1012 playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
1013 playlist_item_t *p_node = p_sys->p_node;
1017 p_current_item = playlist_CurrentPlayingItem(p_playlist);
1018 if ((p_node && p_item->p_input == p_node->p_input) ||
1019 (!p_node && p_current_item && p_item->p_input == p_current_item->p_input))
1021 else if (p_item == p_node || p_current_item == p_item)
1027 if (p_sys->b_color) color_set(i%3 + C_PLAYLIST_1, NULL);
1028 MainBoxWrite(p_sys, i, "%c%s", c, p_sys->pp_plist[i]->psz_display);
1029 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
1032 return p_sys->i_plist_entries;
1035 static int DrawMessages(intf_thread_t *p_intf)
1037 intf_sys_t *p_sys = p_intf->p_sys;
1041 vlc_mutex_lock(&p_sys->msg_lock);
1045 msg_item_t *msg = p_sys->msgs[i];
1049 color_set(msg->i_type + C_INFO, NULL);
1050 MainBoxWrite(p_sys, l++, "[%s] %s", msg->psz_module, msg->psz_msg);
1053 if (++i == sizeof p_sys->msgs / sizeof *p_sys->msgs)
1056 if (i == p_sys->i_msgs) /* did we loop around the ring buffer ? */
1060 vlc_mutex_unlock(&p_sys->msg_lock);
1062 color_set(C_DEFAULT, NULL);
1066 static int DrawStatus(intf_thread_t *p_intf)
1068 intf_sys_t *p_sys = p_intf->p_sys;
1069 input_thread_t *p_input = p_sys->p_input;
1070 playlist_t *p_playlist = pl_Get(p_intf);
1071 static const char name[] = "VLC media player "PACKAGE_VERSION;
1072 const size_t name_len = sizeof name - 1; /* without \0 termination */
1074 const char *repeat, *loop, *random;
1078 int padding = COLS - name_len; /* center title */
1083 if (p_sys->b_color) color_set(C_TITLE, NULL);
1084 DrawEmptyLine(y, 0, COLS);
1085 mvnprintw(y++, padding / 2, COLS, "%s", name);
1086 if (p_sys->b_color) color_set(C_STATUS, NULL);
1089 y++; /* leave a blank line */
1091 repeat = var_GetBool(p_playlist, "repeat") ? _("[Repeat] ") : "";
1092 random = var_GetBool(p_playlist, "random") ? _("[Random] ") : "";
1093 loop = var_GetBool(p_playlist, "loop") ? _("[Loop]") : "";
1095 if (p_input && !p_input->b_dead)
1099 char *psz_uri = input_item_GetURI(input_GetItem(p_input));
1100 mvnprintw(y++, 0, COLS, _(" Source : %s"), psz_uri);
1103 var_Get(p_input, "state", &val);
1106 static const char *input_state[] = {
1107 [PLAYING_S] = " State : Playing %s%s%s",
1108 [OPENING_S] = " State : Opening/Connecting %s%s%s",
1109 [PAUSE_S] = " State : Paused %s%s%s",
1111 char buf1[MSTRTIME_MAX_SIZE];
1112 char buf2[MSTRTIME_MAX_SIZE];
1113 audio_volume_t i_volume;
1123 mvnprintw(y++, 0, COLS, _(input_state[val.i_int]),
1124 repeat, random, loop);
1127 var_Get(p_input, "time", &val);
1128 secstotimestr(buf1, val.i_time / CLOCK_FREQ);
1129 var_Get(p_input, "length", &val);
1130 secstotimestr(buf2, val.i_time / CLOCK_FREQ);
1132 mvnprintw(y++, 0, COLS, _(" Position : %s/%s"), buf1, buf2);
1134 aout_VolumeGet(p_playlist, &i_volume);
1135 mvnprintw(y++, 0, COLS, _(" Volume : %i%%"),
1136 i_volume*200/AOUT_VOLUME_MAX);
1138 if (!var_Get(p_input, "title", &val))
1140 int i_title_count = var_CountChoices(p_input, "title");
1141 if (i_title_count > 0)
1142 mvnprintw(y++, 0, COLS, _(" Title : %"PRId64"/%d"),
1143 val.i_int, i_title_count);
1146 if (!var_Get(p_input, "chapter", &val))
1148 int i_chapter_count = var_CountChoices(p_input, "chapter");
1149 if (i_chapter_count > 0)
1150 mvnprintw(y++, 0, COLS, _(" Chapter : %"PRId64"/%d"),
1151 val.i_int, i_chapter_count);
1157 mvnprintw(y++, 0, COLS, _(" Source: <no current item> "));
1158 mvnprintw(y++, 0, COLS, " %s%s%s", repeat, random, loop);
1159 mvnprintw(y++, 0, COLS, _(" [ h for help ]"));
1160 DrawEmptyLine(y++, 0, COLS);
1163 if (p_sys->b_color) color_set(C_DEFAULT, NULL);
1164 DrawBox(y++, 1, p_sys->b_color, ""); /* position slider */
1165 DrawEmptyLine(y, 1, COLS-2);
1167 DrawLine(y, 1, (int)((COLS-2) * var_GetFloat(p_input, "position")));
1169 y += 2; /* skip slider and box */
1174 static void FillTextBox(intf_sys_t *p_sys)
1176 int width = COLS - 2;
1177 const char *title = p_sys->i_box_type == BOX_OPEN ? "Open: %s" : "Find: %s";
1178 char *chain = p_sys->i_box_type == BOX_OPEN ? p_sys->psz_open_chain :
1179 p_sys->psz_old_search ? p_sys->psz_old_search :
1180 p_sys->psz_search_chain;
1182 DrawEmptyLine(7, 1, width);
1183 mvnprintw(7, 1, width, _(title), chain);
1186 static void FillBox(intf_thread_t *p_intf)
1188 intf_sys_t *p_sys = p_intf->p_sys;
1189 static int (* const draw[]) (intf_thread_t *) = {
1190 [BOX_HELP] = DrawHelp,
1191 [BOX_INFO] = DrawInfo,
1192 [BOX_META] = DrawMeta,
1193 [BOX_OBJECTS] = DrawObjects,
1194 [BOX_STATS] = DrawStats,
1195 [BOX_BROWSE] = DrawBrowse,
1196 [BOX_PLAYLIST] = DrawPlaylist,
1197 [BOX_SEARCH] = DrawPlaylist,
1198 [BOX_OPEN] = DrawPlaylist,
1199 [BOX_LOG] = DrawMessages,
1202 p_sys->i_box_lines_total = draw[p_sys->i_box_type](p_intf);
1204 if (p_sys->i_box_type == BOX_SEARCH || p_sys->i_box_type == BOX_OPEN)
1208 static void Redraw(intf_thread_t *p_intf)
1210 intf_sys_t *p_sys = p_intf->p_sys;
1211 int box = p_sys->i_box_type;
1212 int y = DrawStatus(p_intf);
1214 p_sys->i_box_height = LINES - y - 2;
1215 DrawBox(y++, p_sys->i_box_height, p_sys->b_color, _(box_title[box]));
1219 if (box != BOX_NONE)
1223 if (p_sys->i_box_start > p_sys->i_box_lines_total - 1)
1224 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1225 y += __MIN(p_sys->i_box_lines_total - p_sys->i_box_start,
1226 p_sys->i_box_height);
1229 while (y < LINES - 1)
1230 DrawEmptyLine(y++, 1, COLS - 2);
1235 static void ChangePosition(intf_thread_t *p_intf, float increment)
1237 intf_sys_t *p_sys = p_intf->p_sys;
1238 input_thread_t *p_input = p_sys->p_input;
1241 if (!p_input || var_GetInteger(p_input, "state") != PLAYING_S)
1244 pos = var_GetFloat(p_input, "position") + increment;
1246 if (pos > 0.99) pos = 0.99;
1247 if (pos < 0.0) pos = 0.0;
1249 var_SetFloat(p_input, "position", pos);
1252 static inline void RemoveLastUTF8Entity(char *psz, int len)
1254 while (len && ((psz[--len] & 0xc0) == 0x80)) /* UTF8 continuation byte */
1259 static char *GetDiscDevice(intf_thread_t *p_intf, const char *name)
1261 static const struct { const char *s; size_t n; const char *v; } devs[] =
1263 { "cdda://", 7, "cd-audio", },
1264 { "dvd://", 6, "dvd", },
1265 { "vcd://", 6, "vcd", },
1269 for (unsigned i = 0; i < sizeof devs / sizeof *devs; i++)
1271 size_t n = devs[i].n;
1272 if (!strncmp(name, devs[i].s, n))
1277 return config_GetPsz(p_intf, devs[i].v);
1279 /* Omit the beginning MRL-selector characters */
1280 return strdup(name + n);
1284 device = strdup(name);
1286 if (device) /* Remove what we have after @ */
1287 device[strcspn(device, "@")] = '\0';
1292 static void Eject(intf_thread_t *p_intf)
1294 char *psz_device, *psz_name;
1295 playlist_t * p_playlist = pl_Get(p_intf);
1297 /* If there's a stream playing, we aren't allowed to eject ! */
1298 if (p_intf->p_sys->p_input)
1303 if (!playlist_CurrentPlayingItem(p_playlist))
1309 psz_name = playlist_CurrentPlayingItem(p_playlist)->p_input->psz_name;
1310 psz_device = psz_name ? GetDiscDevice(p_intf, psz_name) : NULL;
1316 intf_Eject(p_intf, psz_device);
1321 static void PlayPause(intf_thread_t *p_intf)
1323 input_thread_t *p_input = p_intf->p_sys->p_input;
1327 int64_t state = var_GetInteger( p_input, "state" );
1328 state = (state != PLAYING_S) ? PLAYING_S : PAUSE_S;
1329 var_SetInteger( p_input, "state", state );
1332 playlist_Play(pl_Get(p_intf));
1335 static inline void BoxSwitch(intf_sys_t *p_sys, int box)
1337 p_sys->i_box_type = (p_sys->i_box_type == box) ? BOX_NONE : box;
1338 p_sys->i_box_start = 0;
1339 p_sys->i_box_idx = 0;
1342 static bool HandlePlaylistKey(intf_thread_t *p_intf, int key)
1344 intf_sys_t *p_sys = p_intf->p_sys;
1345 playlist_t *p_playlist = pl_Get(p_intf);
1346 struct pl_item_t *p_pl_item;
1350 /* Playlist Settings */
1351 case 'r': var_ToggleBool(p_playlist, "random"); return true;
1352 case 'l': var_ToggleBool(p_playlist, "loop"); return true;
1353 case 'R': var_ToggleBool(p_playlist, "repeat"); return true;
1358 playlist_RecursiveNodeSort(p_playlist, p_playlist->p_root_onelevel,
1359 SORT_TITLE_NODES_FIRST,
1360 (key == 'o')? ORDER_NORMAL : ORDER_REVERSE);
1361 p_sys->b_need_update = true;
1365 FindIndex(p_sys, p_playlist);
1374 playlist_item_t *p_item;
1377 p_item = p_sys->pp_plist[p_sys->i_box_idx]->p_item;
1378 if (p_item->i_children == -1)
1379 playlist_DeleteFromInput(p_playlist, p_item->p_input, pl_Locked);
1381 playlist_NodeDelete(p_playlist, p_item, true , false);
1383 p_sys->b_need_update = true;
1390 if (!(p_pl_item = p_sys->pp_plist[p_sys->i_box_idx]))
1393 if (p_pl_item->p_item->i_children)
1395 playlist_item_t *p_item, *p_parent = p_pl_item->p_item;
1396 if (p_parent->i_children == -1)
1400 while (p_parent->p_parent)
1401 p_parent = p_parent->p_parent;
1405 p_sys->p_node = p_parent;
1409 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked,
1413 { /* We only want to set the current node */
1414 playlist_Stop(p_playlist);
1415 p_sys->p_node = p_pl_item->p_item;
1418 p_sys->b_plidx_follow = true;
1425 static bool HandleBrowseKey(intf_thread_t *p_intf, int key)
1427 intf_sys_t *p_sys = p_intf->p_sys;
1428 struct dir_entry_t *dir_entry;
1433 p_sys->b_show_hidden_files = !p_sys->b_show_hidden_files;
1441 dir_entry = p_sys->pp_dir_entries[p_sys->i_box_idx];
1443 if (!dir_entry->b_file && key != ' ')
1445 char *current_dir = p_sys->psz_current_dir;
1446 if (asprintf(&p_sys->psz_current_dir, "%s/%s",
1447 p_sys->psz_current_dir, dir_entry->psz_path) != -1)
1453 p_sys->psz_current_dir = current_dir;
1455 p_sys->i_box_start = 0;
1456 p_sys->i_box_idx = 0;
1461 if (asprintf(&psz_uri, "%s://%s/%s",
1462 dir_entry->b_file ? "file" : "directory",
1463 p_sys->psz_current_dir, dir_entry->psz_path) == -1)
1468 playlist_t *p_playlist = pl_Get(p_intf);
1469 playlist_item_t *p_parent = p_sys->p_node;
1472 playlist_item_t *p_item;
1474 p_item = playlist_CurrentPlayingItem(p_playlist);
1475 p_parent = p_item ? p_item->p_parent : NULL;
1478 p_parent = p_playlist->p_local_onelevel;
1481 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1482 p_parent = p_parent->p_parent;
1484 input_item_t *p_input = p_playlist->p_local_onelevel->p_input;
1485 playlist_Add(p_playlist, psz_uri, NULL, PLAYLIST_APPEND,
1486 PLAYLIST_END, p_parent->p_input == p_input, false);
1488 p_sys->i_box_type = BOX_PLAYLIST;
1496 static void HandleEditBoxKey(intf_thread_t *p_intf, int key, int box)
1498 intf_sys_t *p_sys = p_intf->p_sys;
1499 bool search = box == BOX_SEARCH;
1500 char *str = search ? p_sys->psz_search_chain: p_sys->psz_open_chain;
1501 size_t len = strlen(str);
1503 assert(box == BOX_SEARCH || box == BOX_OPEN);
1508 case KEY_CLEAR: clear(); return;
1516 p_sys->psz_old_search = strdup(p_sys->psz_search_chain);
1517 else if (p_sys->psz_old_search)
1518 SearchPlaylist(p_sys, p_sys->psz_old_search);
1522 playlist_t *p_playlist = pl_Get(p_intf);
1523 playlist_item_t *p_parent = p_sys->p_node, *p_current;
1528 p_current = playlist_CurrentPlayingItem(p_playlist);
1529 p_parent = p_current ? p_current->p_parent : NULL;
1531 p_parent = p_playlist->p_local_onelevel;
1534 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1535 p_parent = p_parent->p_parent;
1538 playlist_Add(p_playlist, p_sys->psz_open_chain, NULL,
1539 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
1540 p_parent->p_input == p_playlist->p_local_onelevel->p_input,
1543 p_sys->b_plidx_follow = true;
1545 p_sys->i_box_type = BOX_PLAYLIST;
1548 case 0x1b: /* ESC */
1549 /* Alt+key combinations return 2 keys in the terminal keyboard:
1550 * ESC, and the 2nd key.
1551 * If some other key is available immediately (where immediately
1552 * means after getch() 1 second delay), that means that the
1553 * ESC key was not pressed.
1555 * man 3X curs_getch says:
1557 * Use of the escape key by a programmer for a single
1558 * character function is discouraged, as it will cause a delay
1559 * of up to one second while the keypad code looks for a
1560 * following function-key sequence.
1566 if (search) p_sys->i_box_idx = p_sys->i_before_search;
1567 p_sys->i_box_type = BOX_PLAYLIST;
1572 RemoveLastUTF8Entity(str, len);
1576 if (len + 1 < search ? sizeof p_sys->psz_search_chain
1577 : sizeof p_sys->psz_open_chain)
1579 str[len + 0] = (char) key;
1580 str[len + 1] = '\0';
1586 free(p_sys->psz_old_search);
1587 p_sys->psz_old_search = NULL;
1588 SearchPlaylist(p_sys, str);
1592 static void InputNavigate(input_thread_t* p_input, const char *var)
1595 var_TriggerCallback(p_input, var);
1598 static void HandleCommonKey(intf_thread_t *p_intf, int key)
1600 intf_sys_t *p_sys = p_intf->p_sys;
1601 playlist_t *p_playlist = pl_Get(p_intf);
1604 case 0x1b: /* ESC */
1611 libvlc_Quit(p_intf->p_libvlc);
1612 p_sys->b_exit = true; // terminate the main loop
1616 case 'H': BoxSwitch(p_sys, BOX_HELP); return;
1617 case 'i': BoxSwitch(p_sys, BOX_INFO); return;
1618 case 'm': BoxSwitch(p_sys, BOX_META); return;
1619 case 'L': BoxSwitch(p_sys, BOX_LOG); return;
1620 case 'P': BoxSwitch(p_sys, BOX_PLAYLIST); return;
1621 case 'B': BoxSwitch(p_sys, BOX_BROWSE); return;
1622 case 'x': BoxSwitch(p_sys, BOX_OBJECTS); return;
1623 case 'S': BoxSwitch(p_sys, BOX_STATS); return;
1625 case '/': /* Search */
1626 p_sys->psz_search_chain[0] = '\0';
1627 p_sys->b_plidx_follow = false;
1628 p_sys->i_before_search = p_sys->i_box_idx;
1629 p_sys->i_box_type = BOX_SEARCH;
1632 case 'A': /* Open */
1633 p_sys->psz_open_chain[0] = '\0';
1634 p_sys->i_box_type = BOX_OPEN;
1638 case KEY_RIGHT: ChangePosition(p_intf, +0.01); return;
1639 case KEY_LEFT: ChangePosition(p_intf, -0.01); return;
1641 /* Common control */
1645 vout_thread_t *p_vout = input_GetVout(p_sys->p_input);
1648 bool fs = var_ToggleBool(p_playlist, "fullscreen");
1649 var_SetBool(p_vout, "fullscreen", fs);
1650 vlc_object_release(p_vout);
1655 case ' ': PlayPause(p_intf); return;
1656 case 's': playlist_Stop(p_playlist); return;
1657 case 'e': Eject(p_intf); return;
1659 case '[': InputNavigate(p_sys->p_input, "prev-title"); return;
1660 case ']': InputNavigate(p_sys->p_input, "next-title"); return;
1661 case '<': InputNavigate(p_sys->p_input, "prev-chapter"); return;
1662 case '>': InputNavigate(p_sys->p_input, "next-chapter"); return;
1664 case 'p': playlist_Prev(p_playlist); break;
1665 case 'n': playlist_Next(p_playlist); break;
1666 case 'a': aout_VolumeUp(p_playlist, 1, NULL); break;
1667 case 'z': aout_VolumeDown(p_playlist, 1, NULL); break;
1681 static bool HandleListKey(intf_thread_t *p_intf, int key)
1683 intf_sys_t *p_sys = p_intf->p_sys;
1684 playlist_t *p_playlist = pl_Get(p_intf);
1689 /* workaround for FreeBSD + xterm:
1690 * see http://www.nabble.com/curses-vs.-xterm-key-mismatch-t3574377.html */
1693 case KEY_END: p_sys->i_box_idx = p_sys->i_box_lines_total - 1; break;
1694 case KEY_HOME: p_sys->i_box_idx = 0; break;
1695 case KEY_UP: p_sys->i_box_idx--; break;
1696 case KEY_DOWN: p_sys->i_box_idx++; break;
1697 case KEY_PPAGE:p_sys->i_box_idx -= p_sys->i_box_height; break;
1698 case KEY_NPAGE:p_sys->i_box_idx += p_sys->i_box_height; break;
1705 if (p_sys->i_box_type == BOX_PLAYLIST)
1708 p_sys->b_plidx_follow = IsIndex(p_sys, p_playlist, p_sys->i_box_idx);
1715 static void HandleKey(intf_thread_t *p_intf)
1717 intf_sys_t *p_sys = p_intf->p_sys;
1719 int box = p_sys->i_box_type;
1724 if (box == BOX_SEARCH || box == BOX_OPEN)
1726 HandleEditBoxKey(p_intf, key, p_sys->i_box_type);
1730 if (box == BOX_NONE)
1736 case KEY_END: ChangePosition(p_intf, +.99); return;
1737 case KEY_HOME: ChangePosition(p_intf, -1.0); return;
1738 case KEY_UP: ChangePosition(p_intf, +0.05); return;
1739 case KEY_DOWN: ChangePosition(p_intf, -0.05); return;
1740 default: HandleCommonKey(p_intf, key); return;
1743 if (box == BOX_BROWSE && HandleBrowseKey(p_intf, key))
1746 if (box == BOX_PLAYLIST && HandlePlaylistKey(p_intf, key))
1749 if (HandleListKey(p_intf, key))
1752 HandleCommonKey(p_intf, key);
1759 static void MsgCallback(msg_cb_data_t *data, msg_item_t *msg, unsigned i)
1761 intf_sys_t *p_sys = data->p_sys;
1762 (void)i; // what is this?
1763 int canc = vlc_savecancel();
1765 vlc_mutex_lock(&p_sys->msg_lock);
1767 if (p_sys->msgs[p_sys->i_msgs])
1768 msg_Release(p_sys->msgs[p_sys->i_msgs]);
1769 p_sys->msgs[p_sys->i_msgs++] = msg_Hold(msg);
1771 if (p_sys->i_msgs == (sizeof p_sys->msgs / sizeof *p_sys->msgs))
1774 vlc_mutex_unlock(&p_sys->msg_lock);
1776 vlc_restorecancel(canc);
1779 static inline void UpdateInput(intf_sys_t *p_sys, playlist_t *p_playlist)
1781 if (!p_sys->p_input)
1783 p_sys->p_input = playlist_CurrentInput(p_playlist);
1785 else if (p_sys->p_input->b_dead)
1787 vlc_object_release(p_sys->p_input);
1788 p_sys->p_input = NULL;
1792 /*****************************************************************************
1793 * Run: ncurses thread
1794 *****************************************************************************/
1795 static void Run(intf_thread_t *p_intf)
1797 intf_sys_t *p_sys = p_intf->p_sys;
1798 playlist_t *p_playlist = pl_Get(p_intf);
1800 int canc = vlc_savecancel();
1802 var_AddCallback(p_playlist, "intf-change", PlaylistChanged, p_intf);
1803 var_AddCallback(p_playlist, "playlist-item-append", PlaylistChanged, p_intf);
1805 while (vlc_object_alive(p_intf) && !p_sys->b_exit)
1807 UpdateInput(p_sys, p_playlist);
1812 var_DelCallback(p_playlist, "intf-change", PlaylistChanged, p_intf);
1813 var_DelCallback(p_playlist, "playlist-item-append", PlaylistChanged, p_intf);
1814 vlc_restorecancel(canc);
1817 /*****************************************************************************
1818 * Open: initialize and create window
1819 *****************************************************************************/
1820 static int Open(vlc_object_t *p_this)
1822 intf_thread_t *p_intf = (intf_thread_t *)p_this;
1823 intf_sys_t *p_sys = p_intf->p_sys = calloc(1, sizeof(intf_sys_t));
1824 struct msg_cb_data_t *msg_cb_data;
1829 msg_cb_data = malloc(sizeof *msg_cb_data);
1836 msg_cb_data->p_sys = p_sys;
1837 vlc_mutex_init(&p_sys->msg_lock);
1839 memset(p_sys->msgs, 0, sizeof p_sys->msgs);
1840 p_sys->p_sub = msg_Subscribe(p_intf->p_libvlc, MsgCallback, msg_cb_data);
1841 msg_SubscriptionSetVerbosity(p_sys->p_sub,
1842 var_GetInteger(p_intf->p_libvlc, "verbose"));
1844 p_sys->i_box_type = BOX_PLAYLIST;
1845 p_sys->b_plidx_follow = true;
1846 p_sys->b_color = var_CreateGetBool(p_intf, "color");
1848 p_sys->psz_current_dir = var_CreateGetString(p_intf, "browse-dir");
1849 if (!p_sys->psz_current_dir || !*p_sys->psz_current_dir)
1851 free(p_sys->psz_current_dir);
1852 p_sys->psz_current_dir = config_GetUserDir(VLC_HOME_DIR);
1855 initscr(); /* Initialize the curses library */
1858 start_color_and_pairs(p_intf);
1860 keypad(stdscr, TRUE); /* Don't do NL -> CR/NL */
1861 nonl(); /* Take input chars one at a time */
1862 cbreak(); /* Don't echo */
1863 noecho(); /* Invisible cursor */
1864 curs_set(0); /* Non blocking getch() */
1868 /* Stop printing errors to the console */
1869 if(!freopen("/dev/null", "wb", stderr))
1870 msg_Err(p_intf, "Couldn't close stderr (%m)");
1873 PlaylistRebuild(p_intf),
1875 p_intf->pf_run = Run;
1879 /*****************************************************************************
1880 * Close: destroy interface window
1881 *****************************************************************************/
1882 static void Close(vlc_object_t *p_this)
1884 intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
1886 PlaylistDestroy(p_sys);
1889 free(p_sys->psz_current_dir);
1890 free(p_sys->psz_old_search);
1893 vlc_object_release(p_sys->p_input);
1895 endwin(); /* Close the ncurses interface */
1897 msg_Unsubscribe(p_sys->p_sub);
1898 vlc_mutex_destroy(&p_sys->msg_lock);
1899 for(unsigned i = 0; i < sizeof p_sys->msgs / sizeof *p_sys->msgs; i++)
1901 msg_Release(p_sys->msgs[i]);