1 /*****************************************************************************
2 * ncurses.c : NCurses interface for vlc
3 *****************************************************************************
4 * Copyright © 2001-2011 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 #define _XOPEN_SOURCE_EXTENDED 1
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
48 #include <vlc_interface.h>
50 #include <vlc_aout_intf.h>
51 #include <vlc_charset.h>
52 #include <vlc_input.h>
54 #include <vlc_playlist.h>
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
65 /*****************************************************************************
67 *****************************************************************************/
69 #define BROWSE_TEXT N_("Filebrowser starting point")
70 #define BROWSE_LONGTEXT N_(\
71 "This option allows you to specify the directory the ncurses filebrowser " \
72 "will show you initially.")
75 set_shortname("Ncurses")
76 set_description(N_("Ncurses interface"))
77 set_capability("interface", 10)
78 set_category(CAT_INTERFACE)
79 set_subcategory(SUBCAT_INTERFACE_MAIN)
80 set_callbacks(Open, Close)
81 add_shortcut("curses")
82 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[][19] = {
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 *item;
181 input_thread_t *p_input;
187 int box_y; // start of box content
189 int box_lines_total; // number of lines in the box
190 int box_start; // first line of box displayed
191 int box_idx; // selected line
193 msg_subscription_t *sub; // message bank subscription
199 } msgs[50]; // ring buffer
202 vlc_mutex_t msg_lock;
204 /* Search Box context */
205 char search_chain[20];
207 /* Open Box Context */
210 /* File Browser context */
213 struct dir_entry_t **dir_entries;
214 bool show_hidden_files;
216 /* Playlist context */
217 struct pl_item_t **plist;
222 playlist_item_t *node; /* current node */
226 /*****************************************************************************
228 *****************************************************************************/
230 static void DirsDestroy(intf_sys_t *sys)
232 while (sys->n_dir_entries) {
233 struct dir_entry_t *dir_entry = sys->dir_entries[--sys->n_dir_entries];
234 free(dir_entry->path);
237 free(sys->dir_entries);
238 sys->dir_entries = NULL;
241 static int comdir_entries(const void *a, const void *b)
243 struct dir_entry_t *dir_entry1 = *(struct dir_entry_t**)a;
244 struct dir_entry_t *dir_entry2 = *(struct dir_entry_t**)b;
246 if (dir_entry1->file == dir_entry2->file)
247 return strcasecmp(dir_entry1->path, dir_entry2->path);
249 return dir_entry1->file ? 1 : -1;
252 static bool IsFile(const char *current_dir, const char *entry)
257 if (asprintf(&uri, "%s" DIR_SEP "%s", current_dir, entry) != -1) {
259 ret = vlc_stat(uri, &st) || !S_ISDIR(st.st_mode);
266 static void ReadDir(intf_thread_t *intf)
268 intf_sys_t *sys = intf->p_sys;
270 if (!sys->current_dir || !*sys->current_dir) {
271 msg_Dbg(intf, "no current dir set");
275 DIR *current_dir = vlc_opendir(sys->current_dir);
277 msg_Warn(intf, "cannot open directory `%s' (%m)", sys->current_dir);
284 while ((entry = vlc_readdir(current_dir))) {
285 if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, ".."))
288 struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry);
292 dir_entry->file = IsFile(sys->current_dir, entry);
293 dir_entry->path = entry;
294 INSERT_ELEM(sys->dir_entries, sys->n_dir_entries,
295 sys->n_dir_entries, dir_entry);
302 qsort(sys->dir_entries, sys->n_dir_entries,
303 sizeof(struct dir_entry_t*), &comdir_entries);
305 closedir(current_dir);
308 /*****************************************************************************
309 * Adjust index position after a change (list navigation or item switching)
310 *****************************************************************************/
311 static void CheckIdx(intf_sys_t *sys)
313 int lines = sys->box_lines_total;
314 int height = LINES - sys->box_y - 2;
315 if (height > lines - 1)
318 /* make sure the new index is within the box */
319 if (sys->box_idx <= 0) {
322 } else if (sys->box_idx >= lines - 1 && lines > 0) {
323 sys->box_idx = lines - 1;
324 sys->box_start = sys->box_idx - height;
327 /* Fix box start (1st line of the box displayed) */
328 if (sys->box_idx < sys->box_start ||
329 sys->box_idx > height + sys->box_start + 1) {
330 sys->box_start = sys->box_idx - height/2;
331 if (sys->box_start < 0)
333 } else if (sys->box_idx == sys->box_start - 1) {
335 } else if (sys->box_idx == height + sys->box_start + 1) {
340 /*****************************************************************************
342 *****************************************************************************/
343 static void PlaylistDestroy(intf_sys_t *sys)
345 while (sys->plist_entries) {
346 struct pl_item_t *p_pl_item = sys->plist[--sys->plist_entries];
347 free(p_pl_item->display);
354 static bool PlaylistAddChild(intf_sys_t *sys, playlist_item_t *p_child,
355 const char *c, const char d)
358 char *name = input_item_GetTitleFbName(p_child->p_input);
359 struct pl_item_t *p_pl_item = malloc(sizeof *p_pl_item);
361 if (!name || !p_pl_item)
364 p_pl_item->item = p_child;
367 ret = asprintf(&p_pl_item->display, "%s%c-%s", c, d, name);
369 ret = asprintf(&p_pl_item->display, " %s", name);
377 INSERT_ELEM(sys->plist, sys->plist_entries,
378 sys->plist_entries, p_pl_item);
388 static void PlaylistAddNode(intf_sys_t *sys, playlist_item_t *node,
391 for (int k = 0; k < node->i_children; k++) {
392 bool last = k == node->i_children - 1;
393 playlist_item_t *p_child = node->pp_children[k];
394 if (!PlaylistAddChild(sys, p_child, c, last ? '`' : '|'))
397 if (p_child->i_children <= 0)
402 if (asprintf(&tmp, "%s%c ", c, last ? ' ' : '|') == -1)
404 PlaylistAddNode(sys, p_child, tmp);
407 PlaylistAddNode(sys, p_child, " ");
412 static void PlaylistRebuild(intf_thread_t *intf)
414 intf_sys_t *sys = intf->p_sys;
415 playlist_t *p_playlist = pl_Get(intf);
417 PlaylistDestroy(sys);
418 PlaylistAddNode(sys, p_playlist->p_root_onelevel, "");
421 static int ItemChanged(vlc_object_t *p_this, const char *variable,
422 vlc_value_t oval, vlc_value_t nval, void *param)
424 VLC_UNUSED(p_this); VLC_UNUSED(variable);
425 VLC_UNUSED(oval); VLC_UNUSED(nval);
427 intf_sys_t *sys = ((intf_thread_t *)param)->p_sys;
429 vlc_mutex_lock(&sys->pl_lock);
430 sys->need_update = true;
431 vlc_mutex_unlock(&sys->pl_lock);
436 static int PlaylistChanged(vlc_object_t *p_this, const char *variable,
437 vlc_value_t oval, vlc_value_t nval, void *param)
439 VLC_UNUSED(p_this); VLC_UNUSED(variable);
440 VLC_UNUSED(oval); VLC_UNUSED(nval);
441 intf_thread_t *intf = (intf_thread_t *)param;
442 intf_sys_t *sys = intf->p_sys;
443 playlist_item_t *node = playlist_CurrentPlayingItem(pl_Get(intf));
445 vlc_mutex_lock(&sys->pl_lock);
446 sys->need_update = true;
447 sys->node = node ? node->p_parent : NULL;
448 vlc_mutex_unlock(&sys->pl_lock);
454 static int SubSearchPlaylist(intf_sys_t *sys, char *searchstring,
455 int i_start, int i_stop)
457 for (int i = i_start + 1; i < i_stop; i++)
458 if (strcasestr(sys->plist[i]->display, searchstring))
464 static void SearchPlaylist(intf_sys_t *sys)
466 char *str = sys->search_chain;
467 int i_first = sys->box_idx;
474 int i_item = SubSearchPlaylist(sys, str, i_first + 1, sys->plist_entries);
476 i_item = SubSearchPlaylist(sys, str, 0, i_first);
479 sys->box_idx = i_item;
484 static inline bool IsIndex(intf_sys_t *sys, playlist_t *p_playlist, int i)
486 playlist_item_t *item = sys->plist[i]->item;
490 vlc_mutex_lock(&sys->pl_lock);
491 if (item->i_children == 0 && item == sys->node) {
492 vlc_mutex_unlock(&sys->pl_lock);
495 vlc_mutex_unlock(&sys->pl_lock);
497 playlist_item_t *p_played_item = playlist_CurrentPlayingItem(p_playlist);
498 if (p_played_item && item->p_input && p_played_item->p_input)
499 return item->p_input->i_id == p_played_item->p_input->i_id;
504 static void FindIndex(intf_sys_t *sys, playlist_t *p_playlist)
506 int plidx = sys->box_idx;
507 int max = sys->plist_entries;
511 if (!IsIndex(sys, p_playlist, plidx))
512 for (int i = 0; i < max; i++)
513 if (IsIndex(sys, p_playlist, i)) {
521 sys->plidx_follow = true;
524 /****************************************************************************
526 ****************************************************************************/
528 static void start_color_and_pairs(intf_thread_t *intf)
531 intf->p_sys->color = false;
532 msg_Warn(intf, "Terminal doesn't support colors");
537 for (int i = C_DEFAULT + 1; i < C_MAX; i++)
538 init_pair(i, color_pairs[i].f, color_pairs[i].b);
540 /* untested, in all my terminals, !can_change_color() --funman */
541 if (can_change_color())
542 init_color(COLOR_YELLOW, 960, 500, 0); /* YELLOW -> ORANGE */
545 static void DrawBox(int y, int h, bool color, const char *title)
548 if (w <= 3 || h <= 0)
551 if (color) color_set(C_BOX, NULL);
553 if (!title) title = "";
554 int len = strlen(title);
559 mvaddch(y, 0, ACS_ULCORNER);
560 mvhline(y, 1, ACS_HLINE, (w-len-2)/2);
561 mvprintw(y, 1+(w-len-2)/2, "%s", title);
562 mvhline(y, (w-len)/2+len, ACS_HLINE, w - 1 - ((w-len)/2+len));
563 mvaddch(y, w-1,ACS_URCORNER);
565 for (int i = 0; i < h; i++) {
566 mvaddch(++y, 0, ACS_VLINE);
567 mvaddch(y, w-1, ACS_VLINE);
570 mvaddch(++y, 0, ACS_LLCORNER);
571 mvhline(y, 1, ACS_HLINE, w - 2);
572 mvaddch(y, w-1, ACS_LRCORNER);
573 if (color) color_set(C_DEFAULT, NULL);
576 static void DrawEmptyLine(int y, int x, int w)
580 mvhline(y, x, ' ', w);
583 static void DrawLine(int y, int x, int w)
588 mvhline(y, x, ' ', w);
592 static void mvnprintw(int y, int x, int w, const char *p_fmt, ...)
601 va_start(vl_args, p_fmt);
602 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
608 wchar_t wide[len + 1];
611 size_t i_char_len = mbstowcs(wide, p_buf, len);
613 size_t i_width; /* number of columns */
615 if (i_char_len == (size_t)-1) /* an invalid character was encountered */ {
620 i_width = wcswidth(wide, i_char_len);
621 if (i_width == (size_t)-1) {
622 /* a non printable character was encountered */
624 for (unsigned i = 0 ; i < i_char_len ; i++) {
625 int i_cwidth = wcwidth(wide[i]);
631 if (i_width <= (size_t)w) {
632 mvprintw(y, x, "%s", p_buf);
633 mvhline(y, x + i_width, ' ', w - i_width);
638 int i_total_width = 0;
640 while (i_total_width < w) {
641 i_total_width += wcwidth(wide[i]);
642 if (w > 7 && i_total_width >= w/2) {
645 i_total_width -= wcwidth(wide[i]) - 2;
647 /* we require this check only if at least one character
648 * 4 or more columns wide exists (which i doubt) */
650 i_total_width -= wcwidth(wide[i-1]) - 1;
653 /* find the widest string */
654 int j, i_2nd_width = 0;
655 for (j = i_char_len - 1; i_2nd_width < w - i_total_width; j--)
656 i_2nd_width += wcwidth(wide[j]);
658 /* we already have i_total_width columns filled, and we can't
659 * have more than w columns */
660 if (i_2nd_width > w - i_total_width)
663 wmemmove(&wide[i+2], &wide[j+1], i_char_len - j - 1);
664 wide[i + 2 + i_char_len - j - 1] = '\0';
669 if (w <= 7) /* we don't add the '...' else we lose too much chars */
672 size_t i_wlen = wcslen(wide) * 6 + 1; /* worst case */
673 char ellipsized[i_wlen];
674 wcstombs(ellipsized, wide, i_wlen);
675 mvprintw(y, x, "%s", ellipsized);
680 static void MainBoxWrite(intf_sys_t *sys, int l, const char *p_fmt, ...)
684 bool b_selected = l == sys->box_idx;
686 if (l < sys->box_start || l - sys->box_start >= sys->box_height)
689 va_start(vl_args, p_fmt);
690 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
694 if (b_selected) attron(A_REVERSE);
695 mvnprintw(sys->box_y + l - sys->box_start, 1, COLS - 2, "%s", p_buf);
696 if (b_selected) attroff(A_REVERSE);
701 static int SubDrawObject(intf_sys_t *sys, int l, vlc_object_t *p_obj, int i_level, const char *prefix)
703 char *name = vlc_object_get_name(p_obj);
704 MainBoxWrite(sys, l++, "%*s%s%s \"%s\" (%p)", 2 * i_level++, "", prefix,
705 p_obj->psz_object_type, name ? name : "", p_obj);
708 vlc_list_t *list = vlc_list_children(p_obj);
709 for (int i = 0; i < list->i_count ; i++) {
710 l = SubDrawObject(sys, l, list->p_values[i].p_object, i_level,
711 (i == list->i_count - 1) ? "`-" : "|-" );
713 vlc_list_release(list);
717 static int DrawObjects(intf_thread_t *intf)
719 return SubDrawObject(intf->p_sys, 0, VLC_OBJECT(intf->p_libvlc), 0, "");
722 static int DrawMeta(intf_thread_t *intf)
724 intf_sys_t *sys = intf->p_sys;
725 input_thread_t *p_input = sys->p_input;
732 item = input_GetItem(p_input);
733 vlc_mutex_lock(&item->lock);
734 for (int i=0; i<VLC_META_TYPE_COUNT; i++) {
735 const char *meta = vlc_meta_Get(item->p_meta, i);
739 if (sys->color) color_set(C_CATEGORY, NULL);
740 MainBoxWrite(sys, l++, " [%s]", vlc_meta_TypeToLocalizedString(i));
741 if (sys->color) color_set(C_DEFAULT, NULL);
742 MainBoxWrite(sys, l++, " %s", meta);
744 vlc_mutex_unlock(&item->lock);
749 static int DrawInfo(intf_thread_t *intf)
751 intf_sys_t *sys = intf->p_sys;
752 input_thread_t *p_input = sys->p_input;
759 item = input_GetItem(p_input);
760 vlc_mutex_lock(&item->lock);
761 for (int i = 0; i < item->i_categories; i++) {
762 info_category_t *p_category = item->pp_categories[i];
763 if (sys->color) color_set(C_CATEGORY, NULL);
764 MainBoxWrite(sys, l++, _(" [%s]"), p_category->psz_name);
765 if (sys->color) color_set(C_DEFAULT, NULL);
766 for (int j = 0; j < p_category->i_infos; j++) {
767 info_t *p_info = p_category->pp_infos[j];
768 MainBoxWrite(sys, l++, _(" %s: %s"),
769 p_info->psz_name, p_info->psz_value);
772 vlc_mutex_unlock(&item->lock);
777 static int DrawStats(intf_thread_t *intf)
779 intf_sys_t *sys = intf->p_sys;
780 input_thread_t *p_input = sys->p_input;
782 input_stats_t *p_stats;
783 int l = 0, i_audio = 0, i_video = 0;
788 item = input_GetItem(p_input);
791 vlc_mutex_lock(&item->lock);
792 p_stats = item->p_stats;
793 vlc_mutex_lock(&p_stats->lock);
795 for (int i = 0; i < item->i_es ; i++) {
796 i_audio += (item->es[i]->i_cat == AUDIO_ES);
797 i_video += (item->es[i]->i_cat == VIDEO_ES);
801 if (sys->color) color_set(C_CATEGORY, NULL);
802 MainBoxWrite(sys, l++, _("+-[Incoming]"));
803 if (sys->color) color_set(C_DEFAULT, NULL);
804 MainBoxWrite(sys, l++, _("| input bytes read : %8.0f KiB"),
805 (float)(p_stats->i_read_bytes)/1024);
806 MainBoxWrite(sys, l++, _("| input bitrate : %6.0f kb/s"),
807 p_stats->f_input_bitrate*8000);
808 MainBoxWrite(sys, l++, _("| demux bytes read : %8.0f KiB"),
809 (float)(p_stats->i_demux_read_bytes)/1024);
810 MainBoxWrite(sys, l++, _("| demux bitrate : %6.0f kb/s"),
811 p_stats->f_demux_bitrate*8000);
815 if (sys->color) color_set(C_CATEGORY, NULL);
816 MainBoxWrite(sys, l++, _("+-[Video Decoding]"));
817 if (sys->color) color_set(C_DEFAULT, NULL);
818 MainBoxWrite(sys, l++, _("| video decoded : %5"PRIi64),
819 p_stats->i_decoded_video);
820 MainBoxWrite(sys, l++, _("| frames displayed : %5"PRIi64),
821 p_stats->i_displayed_pictures);
822 MainBoxWrite(sys, l++, _("| frames lost : %5"PRIi64),
823 p_stats->i_lost_pictures);
827 if (sys->color) color_set(C_CATEGORY, NULL);
828 MainBoxWrite(sys, l++, _("+-[Audio Decoding]"));
829 if (sys->color) color_set(C_DEFAULT, NULL);
830 MainBoxWrite(sys, l++, _("| audio decoded : %5"PRIi64),
831 p_stats->i_decoded_audio);
832 MainBoxWrite(sys, l++, _("| buffers played : %5"PRIi64),
833 p_stats->i_played_abuffers);
834 MainBoxWrite(sys, l++, _("| buffers lost : %5"PRIi64),
835 p_stats->i_lost_abuffers);
838 if (sys->color) color_set(C_CATEGORY, NULL);
839 MainBoxWrite(sys, l++, _("+-[Streaming]"));
840 if (sys->color) color_set(C_DEFAULT, NULL);
841 MainBoxWrite(sys, l++, _("| packets sent : %5"PRIi64), p_stats->i_sent_packets);
842 MainBoxWrite(sys, l++, _("| bytes sent : %8.0f KiB"),
843 (float)(p_stats->i_sent_bytes)/1025);
844 MainBoxWrite(sys, l++, _("| sending bitrate : %6.0f kb/s"),
845 p_stats->f_send_bitrate*8000);
846 if (sys->color) color_set(C_DEFAULT, NULL);
848 vlc_mutex_unlock(&p_stats->lock);
849 vlc_mutex_unlock(&item->lock);
854 static int DrawHelp(intf_thread_t *intf)
856 intf_sys_t *sys = intf->p_sys;
859 #define H(a) MainBoxWrite(sys, l++, a)
861 if (sys->color) color_set(C_CATEGORY, NULL);
863 if (sys->color) color_set(C_DEFAULT, NULL);
864 H(_(" h,H Show/Hide help box"));
865 H(_(" i Show/Hide info box"));
866 H(_(" m Show/Hide metadata box"));
867 H(_(" L Show/Hide messages box"));
868 H(_(" P Show/Hide playlist box"));
869 H(_(" B Show/Hide filebrowser"));
870 H(_(" x Show/Hide objects box"));
871 H(_(" S Show/Hide statistics box"));
872 H(_(" Esc Close Add/Search entry"));
873 H(_(" Ctrl-l Refresh the screen"));
876 if (sys->color) color_set(C_CATEGORY, NULL);
878 if (sys->color) color_set(C_DEFAULT, NULL);
879 H(_(" q, Q, Esc Quit"));
881 H(_(" <space> Pause/Play"));
882 H(_(" f Toggle Fullscreen"));
883 H(_(" n, p Next/Previous playlist item"));
884 H(_(" [, ] Next/Previous title"));
885 H(_(" <, > Next/Previous chapter"));
886 /* xgettext: You can use ← and → characters */
887 H(_(" <left>,<right> Seek -/+ 1%%"));
888 H(_(" a, z Volume Up/Down"));
889 /* xgettext: You can use ↑ and ↓ characters */
890 H(_(" <up>,<down> Navigate through the box line by line"));
891 /* xgettext: You can use ⇞ and ⇟ characters */
892 H(_(" <pageup>,<pagedown> Navigate through the box page by page"));
893 /* xgettext: You can use ↖ and ↘ characters */
894 H(_(" <start>,<end> Navigate to start/end of box"));
897 if (sys->color) color_set(C_CATEGORY, NULL);
899 if (sys->color) color_set(C_DEFAULT, NULL);
900 H(_(" r Toggle Random playing"));
901 H(_(" l Toggle Loop Playlist"));
902 H(_(" R Toggle Repeat item"));
903 H(_(" o Order Playlist by title"));
904 H(_(" O Reverse order Playlist by title"));
905 H(_(" g Go to the current playing item"));
906 H(_(" / Look for an item"));
907 H(_(" ; Look for the next item"));
908 H(_(" A Add an entry"));
909 /* xgettext: You can use ⌫ character to translate <backspace> */
910 H(_(" D, <backspace>, <del> Delete an entry"));
911 H(_(" e Eject (if stopped)"));
914 if (sys->color) color_set(C_CATEGORY, NULL);
915 H(_("[Filebrowser]"));
916 if (sys->color) color_set(C_DEFAULT, NULL);
917 H(_(" <enter> Add the selected file to the playlist"));
918 H(_(" <space> Add the selected directory to the playlist"));
919 H(_(" . Show/Hide hidden files"));
922 if (sys->color) color_set(C_CATEGORY, NULL);
924 if (sys->color) color_set(C_DEFAULT, NULL);
925 /* xgettext: You can use ↑ and ↓ characters */
926 H(_(" <up>,<down> Seek +/-5%%"));
932 static int DrawBrowse(intf_thread_t *intf)
934 intf_sys_t *sys = intf->p_sys;
936 for (int i = 0; i < sys->n_dir_entries; i++) {
937 struct dir_entry_t *dir_entry = sys->dir_entries[i];
938 char type = dir_entry->file ? ' ' : '+';
941 color_set(dir_entry->file ? C_DEFAULT : C_FOLDER, NULL);
942 MainBoxWrite(sys, i, " %c %s", type, dir_entry->path);
945 return sys->n_dir_entries;
948 static int DrawPlaylist(intf_thread_t *intf)
950 intf_sys_t *sys = intf->p_sys;
951 playlist_t *p_playlist = pl_Get(intf);
954 vlc_mutex_lock(&sys->pl_lock);
955 if (sys->need_update) {
956 PlaylistRebuild(intf);
957 sys->need_update = false;
959 vlc_mutex_unlock(&sys->pl_lock);
962 if (sys->plidx_follow)
963 FindIndex(sys, p_playlist);
965 for (int i = 0; i < sys->plist_entries; i++) {
967 playlist_item_t *current_item;
968 playlist_item_t *item = sys->plist[i]->item;
969 vlc_mutex_lock(&sys->pl_lock);
970 playlist_item_t *node = sys->node;
971 vlc_mutex_unlock(&sys->pl_lock);
975 current_item = playlist_CurrentPlayingItem(p_playlist);
976 if ((node && item->p_input == node->p_input) ||
977 (!node && current_item && item->p_input == current_item->p_input))
979 else if (item == node || current_item == item)
985 if (sys->color) color_set(i%3 + C_PLAYLIST_1, NULL);
986 MainBoxWrite(sys, i, "%c%s", c, sys->plist[i]->display);
987 if (sys->color) color_set(C_DEFAULT, NULL);
990 return sys->plist_entries;
993 static int DrawMessages(intf_thread_t *intf)
995 intf_sys_t *sys = intf->p_sys;
998 vlc_mutex_lock(&sys->msg_lock);
1001 msg_item_t *msg = sys->msgs[i].item;
1004 color_set(sys->msgs[i].type + C_INFO, NULL);
1005 MainBoxWrite(sys, l++, "[%s] %s", msg->psz_module, sys->msgs[i].msg);
1008 if (++i == sizeof sys->msgs / sizeof *sys->msgs)
1011 if (i == sys->i_msgs) /* did we loop around the ring buffer ? */
1015 vlc_mutex_unlock(&sys->msg_lock);
1017 color_set(C_DEFAULT, NULL);
1021 static int DrawStatus(intf_thread_t *intf)
1023 intf_sys_t *sys = intf->p_sys;
1024 input_thread_t *p_input = sys->p_input;
1025 playlist_t *p_playlist = pl_Get(intf);
1026 static const char name[] = "VLC media player "PACKAGE_VERSION;
1027 const size_t name_len = sizeof name - 1; /* without \0 termination */
1029 const char *repeat, *loop, *random;
1033 int padding = COLS - name_len; /* center title */
1038 if (sys->color) color_set(C_TITLE, NULL);
1039 DrawEmptyLine(y, 0, COLS);
1040 mvnprintw(y++, padding / 2, COLS, "%s", name);
1041 if (sys->color) color_set(C_STATUS, NULL);
1044 y++; /* leave a blank line */
1046 repeat = var_GetBool(p_playlist, "repeat") ? _("[Repeat] ") : "";
1047 random = var_GetBool(p_playlist, "random") ? _("[Random] ") : "";
1048 loop = var_GetBool(p_playlist, "loop") ? _("[Loop]") : "";
1050 if (p_input && !p_input->b_dead) {
1054 uri = input_item_GetURI(input_GetItem(p_input));
1055 path = make_path(uri);
1057 mvnprintw(y++, 0, COLS, _(" Source : %s"), path?path:uri);
1061 var_Get(p_input, "state", &val);
1064 static const char *input_state[] = {
1065 [PLAYING_S] = " State : Playing %s%s%s",
1066 [OPENING_S] = " State : Opening/Connecting %s%s%s",
1067 [PAUSE_S] = " State : Paused %s%s%s",
1069 char buf1[MSTRTIME_MAX_SIZE];
1070 char buf2[MSTRTIME_MAX_SIZE];
1081 mvnprintw(y++, 0, COLS, _(input_state[val.i_int]),
1082 repeat, random, loop);
1085 var_Get(p_input, "time", &val);
1086 secstotimestr(buf1, val.i_time / CLOCK_FREQ);
1087 var_Get(p_input, "length", &val);
1088 secstotimestr(buf2, val.i_time / CLOCK_FREQ);
1090 mvnprintw(y++, 0, COLS, _(" Position : %s/%s"), buf1, buf2);
1092 i_volume = aout_VolumeGet(p_playlist);
1093 mvnprintw(y++, 0, COLS, _(" Volume : %u%%"),
1094 i_volume*100/AOUT_VOLUME_DEFAULT);
1096 if (!var_Get(p_input, "title", &val)) {
1097 int i_title_count = var_CountChoices(p_input, "title");
1098 if (i_title_count > 0)
1099 mvnprintw(y++, 0, COLS, _(" Title : %"PRId64"/%d"),
1100 val.i_int, i_title_count);
1103 if (!var_Get(p_input, "chapter", &val)) {
1104 int i_chapter_count = var_CountChoices(p_input, "chapter");
1105 if (i_chapter_count > 0) mvnprintw(y++, 0, COLS, _(" Chapter : %"PRId64"/%d"),
1106 val.i_int, i_chapter_count);
1110 mvnprintw(y++, 0, COLS, _(" Source: <no current item> "));
1111 mvnprintw(y++, 0, COLS, " %s%s%s", repeat, random, loop);
1112 mvnprintw(y++, 0, COLS, _(" [ h for help ]"));
1113 DrawEmptyLine(y++, 0, COLS);
1116 if (sys->color) color_set(C_DEFAULT, NULL);
1117 DrawBox(y++, 1, sys->color, ""); /* position slider */
1118 DrawEmptyLine(y, 1, COLS-2);
1120 DrawLine(y, 1, (int)((COLS-2) * var_GetFloat(p_input, "position")));
1122 y += 2; /* skip slider and box */
1127 static void FillTextBox(intf_sys_t *sys)
1129 int width = COLS - 2;
1131 DrawEmptyLine(7, 1, width);
1132 if (sys->box_type == BOX_OPEN)
1133 mvnprintw(7, 1, width, _("Open: %s"), sys->open_chain);
1135 mvnprintw(7, 1, width, _("Find: %s"), sys->search_chain);
1138 static void FillBox(intf_thread_t *intf)
1140 intf_sys_t *sys = intf->p_sys;
1141 static int (* const draw[]) (intf_thread_t *) = {
1142 [BOX_HELP] = DrawHelp,
1143 [BOX_INFO] = DrawInfo,
1144 [BOX_META] = DrawMeta,
1145 [BOX_OBJECTS] = DrawObjects,
1146 [BOX_STATS] = DrawStats,
1147 [BOX_BROWSE] = DrawBrowse,
1148 [BOX_PLAYLIST] = DrawPlaylist,
1149 [BOX_SEARCH] = DrawPlaylist,
1150 [BOX_OPEN] = DrawPlaylist,
1151 [BOX_LOG] = DrawMessages,
1154 sys->box_lines_total = draw[sys->box_type](intf);
1156 if (sys->box_type == BOX_SEARCH || sys->box_type == BOX_OPEN)
1160 static void Redraw(intf_thread_t *intf)
1162 intf_sys_t *sys = intf->p_sys;
1163 int box = sys->box_type;
1164 int y = DrawStatus(intf);
1166 sys->box_height = LINES - y - 2;
1167 DrawBox(y++, sys->box_height, sys->color, _(box_title[box]));
1171 if (box != BOX_NONE) {
1174 if (sys->box_lines_total == 0)
1176 else if (sys->box_start > sys->box_lines_total - 1)
1177 sys->box_start = sys->box_lines_total - 1;
1178 y += __MIN(sys->box_lines_total - sys->box_start,
1182 while (y < LINES - 1)
1183 DrawEmptyLine(y++, 1, COLS - 2);
1188 static void ChangePosition(intf_thread_t *intf, float increment)
1190 intf_sys_t *sys = intf->p_sys;
1191 input_thread_t *p_input = sys->p_input;
1194 if (!p_input || var_GetInteger(p_input, "state") != PLAYING_S)
1197 pos = var_GetFloat(p_input, "position") + increment;
1199 if (pos > 0.99) pos = 0.99;
1200 if (pos < 0.0) pos = 0.0;
1202 var_SetFloat(p_input, "position", pos);
1205 static inline void RemoveLastUTF8Entity(char *psz, int len)
1207 while (len && ((psz[--len] & 0xc0) == 0x80)) /* UTF8 continuation byte */
1212 static char *GetDiscDevice(intf_thread_t *intf, const char *name)
1214 static const struct { const char *s; size_t n; const char *v; } devs[] =
1216 { "cdda://", 7, "cd-audio", },
1217 { "dvd://", 6, "dvd", },
1218 { "vcd://", 6, "vcd", },
1222 for (unsigned i = 0; i < sizeof devs / sizeof *devs; i++) {
1223 size_t n = devs[i].n;
1224 if (!strncmp(name, devs[i].s, n)) {
1225 if (name[n] == '@' || name[n] == '\0')
1226 return config_GetPsz(intf, devs[i].v);
1227 /* Omit the beginning MRL-selector characters */
1228 return strdup(name + n);
1232 device = strdup(name);
1234 if (device) /* Remove what we have after @ */
1235 device[strcspn(device, "@")] = '\0';
1240 static void Eject(intf_thread_t *intf)
1242 char *device, *name;
1243 playlist_t * p_playlist = pl_Get(intf);
1245 /* If there's a stream playing, we aren't allowed to eject ! */
1246 if (intf->p_sys->p_input)
1251 if (!playlist_CurrentPlayingItem(p_playlist)) {
1256 name = playlist_CurrentPlayingItem(p_playlist)->p_input->psz_name;
1257 device = name ? GetDiscDevice(intf, name) : NULL;
1262 intf_Eject(intf, device);
1267 static void PlayPause(intf_thread_t *intf)
1269 input_thread_t *p_input = intf->p_sys->p_input;
1272 int64_t state = var_GetInteger( p_input, "state" );
1273 state = (state != PLAYING_S) ? PLAYING_S : PAUSE_S;
1274 var_SetInteger( p_input, "state", state );
1276 playlist_Play(pl_Get(intf));
1279 static inline void BoxSwitch(intf_sys_t *sys, int box)
1281 sys->box_type = (sys->box_type == box) ? BOX_NONE : box;
1286 static bool HandlePlaylistKey(intf_thread_t *intf, int key)
1288 intf_sys_t *sys = intf->p_sys;
1289 playlist_t *p_playlist = pl_Get(intf);
1290 struct pl_item_t *p_pl_item;
1294 /* Playlist Settings */
1295 case 'r': var_ToggleBool(p_playlist, "random"); return true;
1296 case 'l': var_ToggleBool(p_playlist, "loop"); return true;
1297 case 'R': var_ToggleBool(p_playlist, "repeat"); return true;
1302 playlist_RecursiveNodeSort(p_playlist, p_playlist->p_root_onelevel,
1303 SORT_TITLE_NODES_FIRST,
1304 (key == 'o')? ORDER_NORMAL : ORDER_REVERSE);
1305 vlc_mutex_lock(&sys->pl_lock);
1306 sys->need_update = true;
1307 vlc_mutex_unlock(&sys->pl_lock);
1311 SearchPlaylist(sys);
1315 FindIndex(sys, p_playlist);
1324 playlist_item_t *item;
1327 item = sys->plist[sys->box_idx]->item;
1328 if (item->i_children == -1)
1329 playlist_DeleteFromInput(p_playlist, item->p_input, pl_Locked);
1331 playlist_NodeDelete(p_playlist, item, true , false);
1333 vlc_mutex_lock(&sys->pl_lock);
1334 sys->need_update = true;
1335 vlc_mutex_unlock(&sys->pl_lock);
1342 if (!(p_pl_item = sys->plist[sys->box_idx]))
1345 if (p_pl_item->item->i_children) {
1346 playlist_item_t *item, *p_parent = p_pl_item->item;
1347 if (p_parent->i_children == -1) {
1350 while (p_parent->p_parent)
1351 p_parent = p_parent->p_parent;
1353 vlc_mutex_lock(&sys->pl_lock);
1354 sys->node = p_parent;
1355 vlc_mutex_unlock(&sys->pl_lock);
1359 playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Unlocked,
1361 } else { /* We only want to set the current node */
1362 playlist_Stop(p_playlist);
1363 vlc_mutex_lock(&sys->pl_lock);
1364 sys->node = p_pl_item->item;
1365 vlc_mutex_unlock(&sys->pl_lock);
1368 sys->plidx_follow = true;
1375 static bool HandleBrowseKey(intf_thread_t *intf, int key)
1377 intf_sys_t *sys = intf->p_sys;
1378 struct dir_entry_t *dir_entry;
1383 sys->show_hidden_files = !sys->show_hidden_files;
1391 dir_entry = sys->dir_entries[sys->box_idx];
1393 if (asprintf(&path, "%s" DIR_SEP "%s", sys->current_dir,
1394 dir_entry->path) == -1)
1397 if (!dir_entry->file && key != ' ') {
1398 free(sys->current_dir);
1399 sys->current_dir = path;
1407 char *uri = make_URI(path, dir_entry->file ? "file"
1413 playlist_t *p_playlist = pl_Get(intf);
1414 vlc_mutex_lock(&sys->pl_lock);
1415 playlist_item_t *p_parent = sys->node;
1416 vlc_mutex_unlock(&sys->pl_lock);
1418 playlist_item_t *item;
1420 item = playlist_CurrentPlayingItem(p_playlist);
1421 p_parent = item ? item->p_parent : NULL;
1424 p_parent = p_playlist->p_local_onelevel;
1427 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1428 p_parent = p_parent->p_parent;
1430 input_item_t *p_input = p_playlist->p_local_onelevel->p_input;
1431 playlist_Add(p_playlist, uri, NULL, PLAYLIST_APPEND,
1432 PLAYLIST_END, p_parent->p_input == p_input, false);
1434 BoxSwitch(sys, BOX_PLAYLIST);
1442 static void OpenSelection(intf_thread_t *intf)
1444 intf_sys_t *sys = intf->p_sys;
1445 char *uri = make_URI(sys->open_chain, NULL);
1449 playlist_t *p_playlist = pl_Get(intf);
1450 vlc_mutex_lock(&sys->pl_lock);
1451 playlist_item_t *p_parent = sys->node;
1452 vlc_mutex_unlock(&sys->pl_lock);
1456 playlist_item_t *current;
1457 current= playlist_CurrentPlayingItem(p_playlist);
1458 p_parent = current ? current->p_parent : NULL;
1460 p_parent = p_playlist->p_local_onelevel;
1463 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1464 p_parent = p_parent->p_parent;
1467 playlist_Add(p_playlist, uri, NULL,
1468 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
1469 p_parent->p_input == p_playlist->p_local_onelevel->p_input,
1472 sys->plidx_follow = true;
1476 static void HandleEditBoxKey(intf_thread_t *intf, int key, int box)
1478 intf_sys_t *sys = intf->p_sys;
1479 bool search = box == BOX_SEARCH;
1480 char *str = search ? sys->search_chain: sys->open_chain;
1481 size_t len = strlen(str);
1483 assert(box == BOX_SEARCH || box == BOX_OPEN);
1488 case KEY_CLEAR: clear(); return;
1494 SearchPlaylist(sys);
1496 OpenSelection(intf);
1498 sys->box_type = BOX_PLAYLIST;
1501 case 0x1b: /* ESC */
1502 /* Alt+key combinations return 2 keys in the terminal keyboard:
1503 * ESC, and the 2nd key.
1504 * If some other key is available immediately (where immediately
1505 * means after getch() 1 second delay), that means that the
1506 * ESC key was not pressed.
1508 * man 3X curs_getch says:
1510 * Use of the escape key by a programmer for a single
1511 * character function is discouraged, as it will cause a delay
1512 * of up to one second while the keypad code looks for a
1513 * following function-key sequence.
1517 sys->box_type = BOX_PLAYLIST;
1522 RemoveLastUTF8Entity(str, len);
1526 if (len + 1 < (search ? sizeof sys->search_chain
1527 : sizeof sys->open_chain)) {
1529 str[len + 1] = '\0';
1534 SearchPlaylist(sys);
1537 static void InputNavigate(input_thread_t* p_input, const char *var)
1540 var_TriggerCallback(p_input, var);
1543 static void HandleCommonKey(intf_thread_t *intf, int key)
1545 intf_sys_t *sys = intf->p_sys;
1546 playlist_t *p_playlist = pl_Get(intf);
1549 case 0x1b: /* ESC */
1556 libvlc_Quit(intf->p_libvlc);
1557 sys->exit = true; // terminate the main loop
1561 case 'H': BoxSwitch(sys, BOX_HELP); return;
1562 case 'i': BoxSwitch(sys, BOX_INFO); return;
1563 case 'm': BoxSwitch(sys, BOX_META); return;
1564 case 'L': BoxSwitch(sys, BOX_LOG); return;
1565 case 'P': BoxSwitch(sys, BOX_PLAYLIST); return;
1566 case 'B': BoxSwitch(sys, BOX_BROWSE); return;
1567 case 'x': BoxSwitch(sys, BOX_OBJECTS); return;
1568 case 'S': BoxSwitch(sys, BOX_STATS); return;
1570 case '/': /* Search */
1571 sys->plidx_follow = false;
1572 BoxSwitch(sys, BOX_SEARCH);
1575 case 'A': /* Open */
1576 sys->open_chain[0] = '\0';
1577 BoxSwitch(sys, BOX_OPEN);
1581 case KEY_RIGHT: ChangePosition(intf, +0.01); return;
1582 case KEY_LEFT: ChangePosition(intf, -0.01); return;
1584 /* Common control */
1587 vout_thread_t *p_vout = input_GetVout(sys->p_input);
1589 bool fs = var_ToggleBool(p_playlist, "fullscreen");
1590 var_SetBool(p_vout, "fullscreen", fs);
1591 vlc_object_release(p_vout);
1596 case ' ': PlayPause(intf); return;
1597 case 's': playlist_Stop(p_playlist); return;
1598 case 'e': Eject(intf); return;
1600 case '[': InputNavigate(sys->p_input, "prev-title"); return;
1601 case ']': InputNavigate(sys->p_input, "next-title"); return;
1602 case '<': InputNavigate(sys->p_input, "prev-chapter"); return;
1603 case '>': InputNavigate(sys->p_input, "next-chapter"); return;
1605 case 'p': playlist_Prev(p_playlist); break;
1606 case 'n': playlist_Next(p_playlist); break;
1607 case 'a': aout_VolumeUp(p_playlist, 1, NULL); break;
1608 case 'z': aout_VolumeDown(p_playlist, 1, NULL); break;
1622 static bool HandleListKey(intf_thread_t *intf, int key)
1624 intf_sys_t *sys = intf->p_sys;
1625 playlist_t *p_playlist = pl_Get(intf);
1630 /* workaround for FreeBSD + xterm:
1631 * see http://www.nabble.com/curses-vs.-xterm-key-mismatch-t3574377.html */
1634 case KEY_END: sys->box_idx = sys->box_lines_total - 1; break;
1635 case KEY_HOME: sys->box_idx = 0; break;
1636 case KEY_UP: sys->box_idx--; break;
1637 case KEY_DOWN: sys->box_idx++; break;
1638 case KEY_PPAGE:sys->box_idx -= sys->box_height; break;
1639 case KEY_NPAGE:sys->box_idx += sys->box_height; break;
1646 if (sys->box_type == BOX_PLAYLIST) {
1648 sys->plidx_follow = IsIndex(sys, p_playlist, sys->box_idx);
1655 static void HandleKey(intf_thread_t *intf)
1657 intf_sys_t *sys = intf->p_sys;
1659 int box = sys->box_type;
1664 if (box == BOX_SEARCH || box == BOX_OPEN) {
1665 HandleEditBoxKey(intf, key, sys->box_type);
1669 if (box == BOX_NONE)
1675 case KEY_END: ChangePosition(intf, +.99); return;
1676 case KEY_HOME: ChangePosition(intf, -1.0); return;
1677 case KEY_UP: ChangePosition(intf, +0.05); return;
1678 case KEY_DOWN: ChangePosition(intf, -0.05); return;
1679 default: HandleCommonKey(intf, key); return;
1682 if (box == BOX_BROWSE && HandleBrowseKey(intf, key))
1685 if (box == BOX_PLAYLIST && HandlePlaylistKey(intf, key))
1688 if (HandleListKey(intf, key))
1691 HandleCommonKey(intf, key);
1697 static msg_item_t *msg_Copy (const msg_item_t *msg)
1699 msg_item_t *copy = (msg_item_t *)xmalloc (sizeof (*copy));
1700 copy->i_object_id = msg->i_object_id;
1701 copy->psz_object_type = msg->psz_object_type;
1702 copy->psz_module = strdup (msg->psz_module);
1703 copy->psz_header = msg->psz_header ? strdup (msg->psz_header) : NULL;
1707 static void msg_Free (msg_item_t *msg)
1709 free ((char *)msg->psz_module);
1710 free ((char *)msg->psz_header);
1714 static void MsgCallback(void *data, int type, const msg_item_t *msg,
1715 const char *format, va_list ap)
1717 intf_sys_t *sys = data;
1720 if (sys->verbosity < 0
1721 || sys->verbosity < (type - VLC_MSG_ERR)
1722 || vasprintf(&text, format, ap) == -1)
1725 vlc_mutex_lock(&sys->msg_lock);
1727 sys->msgs[sys->i_msgs].type = type;
1728 if (sys->msgs[sys->i_msgs].item != NULL)
1729 msg_Free(sys->msgs[sys->i_msgs].item);
1730 sys->msgs[sys->i_msgs].item = msg_Copy(msg);
1731 free(sys->msgs[sys->i_msgs].msg);
1732 sys->msgs[sys->i_msgs].msg = text;
1734 if (++sys->i_msgs == (sizeof sys->msgs / sizeof *sys->msgs))
1737 vlc_mutex_unlock(&sys->msg_lock);
1740 static inline void UpdateInput(intf_sys_t *sys, playlist_t *p_playlist)
1742 if (!sys->p_input) {
1743 sys->p_input = playlist_CurrentInput(p_playlist);
1744 } else if (sys->p_input->b_dead) {
1745 vlc_object_release(sys->p_input);
1746 sys->p_input = NULL;
1750 /*****************************************************************************
1751 * Run: ncurses thread
1752 *****************************************************************************/
1753 static void Run(intf_thread_t *intf)
1755 intf_sys_t *sys = intf->p_sys;
1756 playlist_t *p_playlist = pl_Get(intf);
1758 int canc = vlc_savecancel();
1760 var_AddCallback(p_playlist, "intf-change", PlaylistChanged, intf);
1761 var_AddCallback(p_playlist, "item-change", ItemChanged, intf);
1762 var_AddCallback(p_playlist, "playlist-item-append", PlaylistChanged, intf);
1764 while (vlc_object_alive(intf) && !sys->exit) {
1765 UpdateInput(sys, p_playlist);
1770 var_DelCallback(p_playlist, "intf-change", PlaylistChanged, intf);
1771 var_DelCallback(p_playlist, "item-change", ItemChanged, intf);
1772 var_DelCallback(p_playlist, "playlist-item-append", PlaylistChanged, intf);
1773 vlc_restorecancel(canc);
1776 /*****************************************************************************
1777 * Open: initialize and create window
1778 *****************************************************************************/
1779 static int Open(vlc_object_t *p_this)
1781 intf_thread_t *intf = (intf_thread_t *)p_this;
1782 intf_sys_t *sys = intf->p_sys = calloc(1, sizeof(intf_sys_t));
1783 playlist_t *p_playlist = pl_Get(p_this);
1788 vlc_mutex_init(&sys->msg_lock);
1789 vlc_mutex_init(&sys->pl_lock);
1791 sys->verbosity = var_InheritInteger(intf, "verbose");
1792 sys->sub = vlc_Subscribe(MsgCallback, sys);
1794 sys->box_type = BOX_PLAYLIST;
1795 sys->plidx_follow = true;
1796 sys->color = var_CreateGetBool(intf, "color");
1798 sys->current_dir = var_CreateGetNonEmptyString(intf, "browse-dir");
1799 if (!sys->current_dir)
1800 sys->current_dir = config_GetUserDir(VLC_HOME_DIR);
1802 initscr(); /* Initialize the curses library */
1805 start_color_and_pairs(intf);
1807 keypad(stdscr, TRUE);
1808 nonl(); /* Don't do NL -> CR/NL */
1809 cbreak(); /* Take input chars one at a time */
1810 noecho(); /* Don't echo */
1811 curs_set(0); /* Invisible cursor */
1812 timeout(1000); /* blocking getch() */
1815 /* Stop printing errors to the console */
1816 if (!freopen("/dev/null", "wb", stderr))
1817 msg_Err(intf, "Couldn't close stderr (%m)");
1821 PlaylistRebuild(intf),
1828 /*****************************************************************************
1829 * Close: destroy interface window
1830 *****************************************************************************/
1831 static void Close(vlc_object_t *p_this)
1833 intf_sys_t *sys = ((intf_thread_t*)p_this)->p_sys;
1835 PlaylistDestroy(sys);
1838 free(sys->current_dir);
1841 vlc_object_release(sys->p_input);
1843 endwin(); /* Close the ncurses interface */
1845 vlc_Unsubscribe(sys->sub);
1846 vlc_mutex_destroy(&sys->msg_lock);
1847 vlc_mutex_destroy(&sys->pl_lock);
1848 for(unsigned i = 0; i < sizeof sys->msgs / sizeof *sys->msgs; i++) {
1849 if (sys->msgs[i].item)
1850 msg_Free(sys->msgs[i].item);
1851 free(sys->msgs[i].msg);