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 #include <vlc_common.h>
38 #include <vlc_plugin.h>
40 #define _XOPEN_SOURCE_EXTENDED 1
45 #include <vlc_interface.h>
47 #include <vlc_aout_intf.h>
48 #include <vlc_charset.h>
49 #include <vlc_input.h>
51 #include <vlc_playlist.h>
58 #ifdef HAVE_SYS_STAT_H
59 # include <sys/stat.h>
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Open (vlc_object_t *);
66 static void Close (vlc_object_t *);
68 /*****************************************************************************
70 *****************************************************************************/
72 #define BROWSE_TEXT N_("Filebrowser starting point")
73 #define BROWSE_LONGTEXT N_(\
74 "This option allows you to specify the directory the ncurses filebrowser " \
75 "will show you initially.")
78 set_shortname("Ncurses")
79 set_description(N_("Ncurses interface"))
80 set_capability("interface", 10)
81 set_category(CAT_INTERFACE)
82 set_subcategory(SUBCAT_INTERFACE_MAIN)
83 set_callbacks(Open, Close)
84 add_shortcut("curses")
85 add_directory("browse-dir", NULL, BROWSE_TEXT, BROWSE_LONGTEXT, false)
88 /*****************************************************************************
89 * intf_sys_t: description and status of ncurses interface
90 *****************************************************************************/
106 static const char box_title[][19] = {
108 [BOX_HELP] = " Help ",
109 [BOX_INFO] = " Information ",
110 [BOX_LOG] = " Messages ",
111 [BOX_PLAYLIST] = " Playlist ",
112 [BOX_SEARCH] = " Playlist ",
113 [BOX_OPEN] = " Playlist ",
114 [BOX_BROWSE] = " Browse ",
115 [BOX_META] = " Meta-information ",
116 [BOX_OBJECTS] = " Objects ",
117 [BOX_STATS] = " Stats ",
135 /* XXX: new elements here ! */
140 /* Available colors: BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE */
141 static const struct { short f; short b; } color_pairs[] =
143 /* element */ /* foreground*/ /* background*/
144 [C_TITLE] = { COLOR_YELLOW, COLOR_BLACK },
146 /* jamaican playlist, for rastafari sisters & brothers! */
147 [C_PLAYLIST_1] = { COLOR_GREEN, COLOR_BLACK },
148 [C_PLAYLIST_2] = { COLOR_YELLOW, COLOR_BLACK },
149 [C_PLAYLIST_3] = { COLOR_RED, COLOR_BLACK },
151 /* used in DrawBox() */
152 [C_BOX] = { COLOR_CYAN, COLOR_BLACK },
153 /* Source: State, Position, Volume, Chapters, etc...*/
154 [C_STATUS] = { COLOR_BLUE, COLOR_BLACK },
156 /* VLC messages, keep the order from highest priority to lowest */
157 [C_INFO] = { COLOR_BLACK, COLOR_WHITE },
158 [C_ERROR] = { COLOR_RED, COLOR_BLACK },
159 [C_WARNING] = { COLOR_YELLOW, COLOR_BLACK },
160 [C_DEBUG] = { COLOR_WHITE, COLOR_BLACK },
162 /* Category title: help, info, metadata */
163 [C_CATEGORY] = { COLOR_MAGENTA, COLOR_BLACK },
164 /* Folder (BOX_BROWSE) */
165 [C_FOLDER] = { COLOR_RED, COLOR_BLACK },
176 playlist_item_t *item;
182 input_thread_t *p_input;
188 int box_y; // start of box content
190 int box_lines_total; // number of lines in the box
191 int box_start; // first line of box displayed
192 int box_idx; // selected line
194 msg_subscription_t *sub; // message bank subscription
200 } msgs[50]; // ring buffer
203 vlc_mutex_t msg_lock;
205 /* Search Box context */
206 char search_chain[20];
210 /* Open Box Context */
213 /* File Browser context */
216 struct dir_entry_t **dir_entries;
217 bool show_hidden_files;
219 /* Playlist context */
220 struct pl_item_t **plist;
225 playlist_item_t *node; /* current node */
229 /*****************************************************************************
231 *****************************************************************************/
233 static void DirsDestroy(intf_sys_t *sys)
235 while (sys->n_dir_entries) {
236 struct dir_entry_t *dir_entry = sys->dir_entries[--sys->n_dir_entries];
237 free(dir_entry->path);
240 free(sys->dir_entries);
241 sys->dir_entries = NULL;
244 static int comdir_entries(const void *a, const void *b)
246 struct dir_entry_t *dir_entry1 = *(struct dir_entry_t**)a;
247 struct dir_entry_t *dir_entry2 = *(struct dir_entry_t**)b;
249 if (dir_entry1->file == dir_entry2->file)
250 return strcasecmp(dir_entry1->path, dir_entry2->path);
252 return dir_entry1->file ? 1 : -1;
255 static bool IsFile(const char *current_dir, const char *entry)
260 if (asprintf(&uri, "%s" DIR_SEP "%s", current_dir, entry) != -1) {
262 ret = vlc_stat(uri, &st) || !S_ISDIR(st.st_mode);
269 static void ReadDir(intf_thread_t *intf)
271 intf_sys_t *sys = intf->p_sys;
273 if (!sys->current_dir || !*sys->current_dir) {
274 msg_Dbg(intf, "no current dir set");
278 DIR *current_dir = vlc_opendir(sys->current_dir);
280 msg_Warn(intf, "cannot open directory `%s' (%m)", sys->current_dir);
287 while ((entry = vlc_readdir(current_dir))) {
288 if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, ".."))
291 struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry);
295 dir_entry->file = IsFile(sys->current_dir, entry);
296 dir_entry->path = entry;
297 INSERT_ELEM(sys->dir_entries, sys->n_dir_entries,
298 sys->n_dir_entries, dir_entry);
305 qsort(sys->dir_entries, sys->n_dir_entries,
306 sizeof(struct dir_entry_t*), &comdir_entries);
308 closedir(current_dir);
311 /*****************************************************************************
312 * Adjust index position after a change (list navigation or item switching)
313 *****************************************************************************/
314 static void CheckIdx(intf_sys_t *sys)
316 int lines = sys->box_lines_total;
317 int height = LINES - sys->box_y - 2;
318 if (height > lines - 1)
321 /* make sure the new index is within the box */
322 if (sys->box_idx <= 0) {
325 } else if (sys->box_idx >= lines - 1 && lines > 0) {
326 sys->box_idx = lines - 1;
327 sys->box_start = sys->box_idx - height;
330 /* Fix box start (1st line of the box displayed) */
331 if (sys->box_idx < sys->box_start ||
332 sys->box_idx > height + sys->box_start + 1) {
333 sys->box_start = sys->box_idx - height/2;
334 if (sys->box_start < 0)
336 } else if (sys->box_idx == sys->box_start - 1) {
338 } else if (sys->box_idx == height + sys->box_start + 1) {
343 /*****************************************************************************
345 *****************************************************************************/
346 static void PlaylistDestroy(intf_sys_t *sys)
348 while (sys->plist_entries) {
349 struct pl_item_t *p_pl_item = sys->plist[--sys->plist_entries];
350 free(p_pl_item->display);
357 static bool PlaylistAddChild(intf_sys_t *sys, playlist_item_t *p_child,
358 const char *c, const char d)
361 char *name = input_item_GetTitleFbName(p_child->p_input);
362 struct pl_item_t *p_pl_item = malloc(sizeof *p_pl_item);
364 if (!name || !p_pl_item)
367 p_pl_item->item = p_child;
370 ret = asprintf(&p_pl_item->display, "%s%c-%s", c, d, name);
372 ret = asprintf(&p_pl_item->display, " %s", name);
380 INSERT_ELEM(sys->plist, sys->plist_entries,
381 sys->plist_entries, p_pl_item);
391 static void PlaylistAddNode(intf_sys_t *sys, playlist_item_t *node,
394 for (int k = 0; k < node->i_children; k++) {
395 bool last = k == node->i_children - 1;
396 playlist_item_t *p_child = node->pp_children[k];
397 if (!PlaylistAddChild(sys, p_child, c, last ? '`' : '|'))
400 if (p_child->i_children <= 0)
405 if (asprintf(&tmp, "%s%c ", c, last ? ' ' : '|') == -1)
407 PlaylistAddNode(sys, p_child, tmp);
410 PlaylistAddNode(sys, p_child, " ");
415 static void PlaylistRebuild(intf_thread_t *intf)
417 intf_sys_t *sys = intf->p_sys;
418 playlist_t *p_playlist = pl_Get(intf);
421 PlaylistDestroy(sys);
422 PlaylistAddNode(sys, p_playlist->p_root_onelevel, "");
426 static int ItemChanged(vlc_object_t *p_this, const char *variable,
427 vlc_value_t oval, vlc_value_t nval, void *param)
429 VLC_UNUSED(p_this); VLC_UNUSED(variable);
430 VLC_UNUSED(oval); VLC_UNUSED(nval);
432 intf_sys_t *sys = ((intf_thread_t *)param)->p_sys;
434 vlc_mutex_lock(&sys->pl_lock);
435 sys->need_update = true;
436 vlc_mutex_unlock(&sys->pl_lock);
441 static int PlaylistChanged(vlc_object_t *p_this, const char *variable,
442 vlc_value_t oval, vlc_value_t nval, void *param)
444 VLC_UNUSED(p_this); VLC_UNUSED(variable);
445 VLC_UNUSED(oval); VLC_UNUSED(nval);
446 intf_thread_t *intf = (intf_thread_t *)param;
447 intf_sys_t *sys = intf->p_sys;
448 playlist_item_t *node = playlist_CurrentPlayingItem(pl_Get(intf));
450 vlc_mutex_lock(&sys->pl_lock);
451 sys->need_update = true;
452 sys->node = node ? node->p_parent : NULL;
453 vlc_mutex_unlock(&sys->pl_lock);
459 static int SubSearchPlaylist(intf_sys_t *sys, char *searchstring,
460 int i_start, int i_stop)
462 for (int i = i_start + 1; i < i_stop; i++)
463 if (strcasestr(sys->plist[i]->display, searchstring))
469 static void SearchPlaylist(intf_sys_t *sys, char *str)
471 int i_first = sys->before_search;
476 sys->box_idx = sys->before_search;
480 int i_item = SubSearchPlaylist(sys, str, i_first + 1, sys->plist_entries);
482 i_item = SubSearchPlaylist(sys, str, 0, i_first);
485 sys->box_idx = i_item;
490 static inline bool IsIndex(intf_sys_t *sys, playlist_t *p_playlist, int i)
492 playlist_item_t *item = sys->plist[i]->item;
496 vlc_mutex_lock(&sys->pl_lock);
497 if (item->i_children == 0 && item == sys->node) {
498 vlc_mutex_unlock(&sys->pl_lock);
501 vlc_mutex_unlock(&sys->pl_lock);
503 playlist_item_t *p_played_item = playlist_CurrentPlayingItem(p_playlist);
504 if (p_played_item && item->p_input && p_played_item->p_input)
505 return item->p_input->i_id == p_played_item->p_input->i_id;
510 static void FindIndex(intf_sys_t *sys, playlist_t *p_playlist)
512 int plidx = sys->box_idx;
513 int max = sys->plist_entries;
517 if (!IsIndex(sys, p_playlist, plidx))
518 for (int i = 0; i < max; i++)
519 if (IsIndex(sys, p_playlist, i)) {
527 sys->plidx_follow = true;
530 /****************************************************************************
532 ****************************************************************************/
534 static void start_color_and_pairs(intf_thread_t *intf)
537 intf->p_sys->color = false;
538 msg_Warn(intf, "Terminal doesn't support colors");
543 for (int i = C_DEFAULT + 1; i < C_MAX; i++)
544 init_pair(i, color_pairs[i].f, color_pairs[i].b);
546 /* untested, in all my terminals, !can_change_color() --funman */
547 if (can_change_color())
548 init_color(COLOR_YELLOW, 960, 500, 0); /* YELLOW -> ORANGE */
551 static void DrawBox(int y, int h, bool color, const char *title)
554 if (w <= 3 || h <= 0)
557 if (color) color_set(C_BOX, NULL);
559 if (!title) title = "";
560 int len = strlen(title);
565 mvaddch(y, 0, ACS_ULCORNER);
566 mvhline(y, 1, ACS_HLINE, (w-len-2)/2);
567 mvprintw(y, 1+(w-len-2)/2, "%s", title);
568 mvhline(y, (w-len)/2+len, ACS_HLINE, w - 1 - ((w-len)/2+len));
569 mvaddch(y, w-1,ACS_URCORNER);
571 for (int i = 0; i < h; i++) {
572 mvaddch(++y, 0, ACS_VLINE);
573 mvaddch(y, w-1, ACS_VLINE);
576 mvaddch(++y, 0, ACS_LLCORNER);
577 mvhline(y, 1, ACS_HLINE, w - 2);
578 mvaddch(y, w-1, ACS_LRCORNER);
579 if (color) color_set(C_DEFAULT, NULL);
582 static void DrawEmptyLine(int y, int x, int w)
586 mvhline(y, x, ' ', w);
589 static void DrawLine(int y, int x, int w)
594 mvhline(y, x, ' ', w);
598 static void mvnprintw(int y, int x, int w, const char *p_fmt, ...)
607 va_start(vl_args, p_fmt);
608 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
614 wchar_t wide[len + 1];
617 size_t i_char_len = mbstowcs(wide, p_buf, len);
619 size_t i_width; /* number of columns */
621 if (i_char_len == (size_t)-1) /* an invalid character was encountered */ {
626 i_width = wcswidth(wide, i_char_len);
627 if (i_width == (size_t)-1) {
628 /* a non printable character was encountered */
630 for (unsigned i = 0 ; i < i_char_len ; i++) {
631 int i_cwidth = wcwidth(wide[i]);
637 if (i_width <= (size_t)w) {
638 mvprintw(y, x, "%s", p_buf);
639 mvhline(y, x + i_width, ' ', w - i_width);
644 int i_total_width = 0;
646 while (i_total_width < w) {
647 i_total_width += wcwidth(wide[i]);
648 if (w > 7 && i_total_width >= w/2) {
651 i_total_width -= wcwidth(wide[i]) - 2;
653 /* we require this check only if at least one character
654 * 4 or more columns wide exists (which i doubt) */
656 i_total_width -= wcwidth(wide[i-1]) - 1;
659 /* find the widest string */
660 int j, i_2nd_width = 0;
661 for (j = i_char_len - 1; i_2nd_width < w - i_total_width; j--)
662 i_2nd_width += wcwidth(wide[j]);
664 /* we already have i_total_width columns filled, and we can't
665 * have more than w columns */
666 if (i_2nd_width > w - i_total_width)
669 wmemmove(&wide[i+2], &wide[j+1], i_char_len - j - 1);
670 wide[i + 2 + i_char_len - j - 1] = '\0';
675 if (w <= 7) /* we don't add the '...' else we lose too much chars */
678 size_t i_wlen = wcslen(wide) * 6 + 1; /* worst case */
679 char ellipsized[i_wlen];
680 wcstombs(ellipsized, wide, i_wlen);
681 mvprintw(y, x, "%s", ellipsized);
686 static void MainBoxWrite(intf_sys_t *sys, int l, const char *p_fmt, ...)
690 bool b_selected = l == sys->box_idx;
692 if (l < sys->box_start || l - sys->box_start >= sys->box_height)
695 va_start(vl_args, p_fmt);
696 if (vasprintf(&p_buf, p_fmt, vl_args) == -1)
700 if (b_selected) attron(A_REVERSE);
701 mvnprintw(sys->box_y + l - sys->box_start, 1, COLS - 2, "%s", p_buf);
702 if (b_selected) attroff(A_REVERSE);
707 static int SubDrawObject(intf_sys_t *sys, int l, vlc_object_t *p_obj, int i_level, const char *prefix)
709 char *name = vlc_object_get_name(p_obj);
710 MainBoxWrite(sys, l++, "%*s%s%s \"%s\" (%p)", 2 * i_level++, "", prefix,
711 p_obj->psz_object_type, name ? name : "", p_obj);
714 vlc_list_t *list = vlc_list_children(p_obj);
715 for (int i = 0; i < list->i_count ; i++) {
716 l = SubDrawObject(sys, l, list->p_values[i].p_object, i_level,
717 (i == list->i_count - 1) ? "`-" : "|-" );
719 vlc_list_release(list);
723 static int DrawObjects(intf_thread_t *intf)
725 return SubDrawObject(intf->p_sys, 0, VLC_OBJECT(intf->p_libvlc), 0, "");
728 static int DrawMeta(intf_thread_t *intf)
730 intf_sys_t *sys = intf->p_sys;
731 input_thread_t *p_input = sys->p_input;
738 item = input_GetItem(p_input);
739 vlc_mutex_lock(&item->lock);
740 for (int i=0; i<VLC_META_TYPE_COUNT; i++) {
741 const char *meta = vlc_meta_Get(item->p_meta, i);
745 if (sys->color) color_set(C_CATEGORY, NULL);
746 MainBoxWrite(sys, l++, " [%s]", vlc_meta_TypeToLocalizedString(i));
747 if (sys->color) color_set(C_DEFAULT, NULL);
748 MainBoxWrite(sys, l++, " %s", meta);
750 vlc_mutex_unlock(&item->lock);
755 static int DrawInfo(intf_thread_t *intf)
757 intf_sys_t *sys = intf->p_sys;
758 input_thread_t *p_input = sys->p_input;
765 item = input_GetItem(p_input);
766 vlc_mutex_lock(&item->lock);
767 for (int i = 0; i < item->i_categories; i++) {
768 info_category_t *p_category = item->pp_categories[i];
769 if (sys->color) color_set(C_CATEGORY, NULL);
770 MainBoxWrite(sys, l++, _(" [%s]"), p_category->psz_name);
771 if (sys->color) color_set(C_DEFAULT, NULL);
772 for (int j = 0; j < p_category->i_infos; j++) {
773 info_t *p_info = p_category->pp_infos[j];
774 MainBoxWrite(sys, l++, _(" %s: %s"),
775 p_info->psz_name, p_info->psz_value);
778 vlc_mutex_unlock(&item->lock);
783 static int DrawStats(intf_thread_t *intf)
785 intf_sys_t *sys = intf->p_sys;
786 input_thread_t *p_input = sys->p_input;
788 input_stats_t *p_stats;
789 int l = 0, i_audio = 0, i_video = 0;
794 item = input_GetItem(p_input);
797 vlc_mutex_lock(&item->lock);
798 p_stats = item->p_stats;
799 vlc_mutex_lock(&p_stats->lock);
801 for (int i = 0; i < item->i_es ; i++) {
802 i_audio += (item->es[i]->i_cat == AUDIO_ES);
803 i_video += (item->es[i]->i_cat == VIDEO_ES);
807 if (sys->color) color_set(C_CATEGORY, NULL);
808 MainBoxWrite(sys, l++, _(" [Incoming]"));
809 if (sys->color) color_set(C_DEFAULT, NULL);
810 MainBoxWrite(sys, l++, _(" input bytes read : %8.0f KiB"),
811 (float)(p_stats->i_read_bytes)/1024);
812 MainBoxWrite(sys, l++, _(" input bitrate : %6.0f kb/s"),
813 p_stats->f_input_bitrate*8000);
814 MainBoxWrite(sys, l++, _(" demux bytes read : %8.0f KiB"),
815 (float)(p_stats->i_demux_read_bytes)/1024);
816 MainBoxWrite(sys, l++, _(" demux bitrate : %6.0f kb/s"),
817 p_stats->f_demux_bitrate*8000);
821 if (sys->color) color_set(C_CATEGORY, NULL);
822 MainBoxWrite(sys, l++, _(" [Video Decoding]"));
823 if (sys->color) color_set(C_DEFAULT, NULL);
824 MainBoxWrite(sys, l++, _(" video decoded : %"PRId64),
825 p_stats->i_decoded_video);
826 MainBoxWrite(sys, l++, _(" frames displayed : %"PRId64),
827 p_stats->i_displayed_pictures);
828 MainBoxWrite(sys, l++, _(" frames lost : %"PRId64),
829 p_stats->i_lost_pictures);
833 if (sys->color) color_set(C_CATEGORY, NULL);
834 MainBoxWrite(sys, l++, _(" [Audio Decoding]"));
835 if (sys->color) color_set(C_DEFAULT, NULL);
836 MainBoxWrite(sys, l++, _(" audio decoded : %"PRId64),
837 p_stats->i_decoded_audio);
838 MainBoxWrite(sys, l++, _(" buffers played : %"PRId64),
839 p_stats->i_played_abuffers);
840 MainBoxWrite(sys, l++, _(" buffers lost : %"PRId64),
841 p_stats->i_lost_abuffers);
844 if (sys->color) color_set(C_CATEGORY, NULL);
845 MainBoxWrite(sys, l++, _(" [Streaming]"));
846 if (sys->color) color_set(C_DEFAULT, NULL);
847 MainBoxWrite(sys, l++, _(" packets sent : %5i"), p_stats->i_sent_packets);
848 MainBoxWrite(sys, l++, _(" bytes sent : %8.0f KiB"),
849 (float)(p_stats->i_sent_bytes)/1025);
850 MainBoxWrite(sys, l++, _(" sending bitrate : %6.0f kb/s"),
851 p_stats->f_send_bitrate*8000);
852 if (sys->color) color_set(C_DEFAULT, NULL);
854 vlc_mutex_unlock(&p_stats->lock);
855 vlc_mutex_unlock(&item->lock);
860 static int DrawHelp(intf_thread_t *intf)
862 intf_sys_t *sys = intf->p_sys;
865 #define H(a) MainBoxWrite(sys, l++, a)
867 if (sys->color) color_set(C_CATEGORY, NULL);
869 if (sys->color) color_set(C_DEFAULT, NULL);
870 H(_(" h,H Show/Hide help box"));
871 H(_(" i Show/Hide info box"));
872 H(_(" m Show/Hide metadata box"));
873 H(_(" L Show/Hide messages box"));
874 H(_(" P Show/Hide playlist box"));
875 H(_(" B Show/Hide filebrowser"));
876 H(_(" x Show/Hide objects box"));
877 H(_(" S Show/Hide statistics box"));
878 H(_(" Esc Close Add/Search entry"));
879 H(_(" Ctrl-l Refresh the screen"));
882 if (sys->color) color_set(C_CATEGORY, NULL);
884 if (sys->color) color_set(C_DEFAULT, NULL);
885 H(_(" q, Q, Esc Quit"));
887 H(_(" <space> Pause/Play"));
888 H(_(" f Toggle Fullscreen"));
889 H(_(" n, p Next/Previous playlist item"));
890 H(_(" [, ] Next/Previous title"));
891 H(_(" <, > Next/Previous chapter"));
892 /* xgettext: You can use ← and → characters */
893 H(_(" <left>,<right> Seek -/+ 1%%"));
894 H(_(" a, z Volume Up/Down"));
895 /* xgettext: You can use ↑ and ↓ characters */
896 H(_(" <up>,<down> Navigate through the box line by line"));
897 /* xgettext: You can use ⇞ and ⇟ characters */
898 H(_(" <pageup>,<pagedown> Navigate through the box page by page"));
899 /* xgettext: You can use ↖ and ↘ characters */
900 H(_(" <start>,<end> Navigate to start/end of box"));
903 if (sys->color) color_set(C_CATEGORY, NULL);
905 if (sys->color) color_set(C_DEFAULT, NULL);
906 H(_(" r Toggle Random playing"));
907 H(_(" l Toggle Loop Playlist"));
908 H(_(" R Toggle Repeat item"));
909 H(_(" o Order Playlist by title"));
910 H(_(" O Reverse order Playlist by title"));
911 H(_(" g Go to the current playing item"));
912 H(_(" / Look for an item"));
913 H(_(" A Add an entry"));
914 /* xgettext: You can use ⌫ character to translate <backspace> */
915 H(_(" D, <backspace>, <del> Delete an entry"));
916 H(_(" e Eject (if stopped)"));
919 if (sys->color) color_set(C_CATEGORY, NULL);
920 H(_("[Filebrowser]"));
921 if (sys->color) color_set(C_DEFAULT, NULL);
922 H(_(" <enter> Add the selected file to the playlist"));
923 H(_(" <space> Add the selected directory to the playlist"));
924 H(_(" . Show/Hide hidden files"));
927 if (sys->color) color_set(C_CATEGORY, NULL);
929 if (sys->color) color_set(C_DEFAULT, NULL);
930 /* xgettext: You can use ↑ and ↓ characters */
931 H(_(" <up>,<down> Seek +/-5%%"));
937 static int DrawBrowse(intf_thread_t *intf)
939 intf_sys_t *sys = intf->p_sys;
941 for (int i = 0; i < sys->n_dir_entries; i++) {
942 struct dir_entry_t *dir_entry = sys->dir_entries[i];
943 char type = dir_entry->file ? ' ' : '+';
946 color_set(dir_entry->file ? C_DEFAULT : C_FOLDER, NULL);
947 MainBoxWrite(sys, i, " %c %s", type, dir_entry->path);
950 return sys->n_dir_entries;
953 static int DrawPlaylist(intf_thread_t *intf)
955 intf_sys_t *sys = intf->p_sys;
956 playlist_t *p_playlist = pl_Get(intf);
958 vlc_mutex_lock(&sys->pl_lock);
959 if (sys->need_update) {
960 PlaylistRebuild(intf);
961 sys->need_update = false;
963 vlc_mutex_unlock(&sys->pl_lock);
965 if (sys->plidx_follow)
966 FindIndex(sys, p_playlist);
968 for (int i = 0; i < sys->plist_entries; i++) {
970 playlist_item_t *current_item;
971 playlist_item_t *item = sys->plist[i]->item;
972 vlc_mutex_lock(&sys->pl_lock);
973 playlist_item_t *node = sys->node;
974 vlc_mutex_unlock(&sys->pl_lock);
978 current_item = playlist_CurrentPlayingItem(p_playlist);
979 if ((node && item->p_input == node->p_input) ||
980 (!node && current_item && item->p_input == current_item->p_input))
982 else if (item == node || current_item == item)
988 if (sys->color) color_set(i%3 + C_PLAYLIST_1, NULL);
989 MainBoxWrite(sys, i, "%c%s", c, sys->plist[i]->display);
990 if (sys->color) color_set(C_DEFAULT, NULL);
993 return sys->plist_entries;
996 static int DrawMessages(intf_thread_t *intf)
998 intf_sys_t *sys = intf->p_sys;
1001 vlc_mutex_lock(&sys->msg_lock);
1002 int i = sys->i_msgs;
1004 msg_item_t *msg = sys->msgs[i].item;
1007 color_set(sys->msgs[i].type + C_INFO, NULL);
1008 MainBoxWrite(sys, l++, "[%s] %s", msg->psz_module, sys->msgs[i].msg);
1011 if (++i == sizeof sys->msgs / sizeof *sys->msgs)
1014 if (i == sys->i_msgs) /* did we loop around the ring buffer ? */
1018 vlc_mutex_unlock(&sys->msg_lock);
1020 color_set(C_DEFAULT, NULL);
1024 static int DrawStatus(intf_thread_t *intf)
1026 intf_sys_t *sys = intf->p_sys;
1027 input_thread_t *p_input = sys->p_input;
1028 playlist_t *p_playlist = pl_Get(intf);
1029 static const char name[] = "VLC media player "PACKAGE_VERSION;
1030 const size_t name_len = sizeof name - 1; /* without \0 termination */
1032 const char *repeat, *loop, *random;
1036 int padding = COLS - name_len; /* center title */
1041 if (sys->color) color_set(C_TITLE, NULL);
1042 DrawEmptyLine(y, 0, COLS);
1043 mvnprintw(y++, padding / 2, COLS, "%s", name);
1044 if (sys->color) color_set(C_STATUS, NULL);
1047 y++; /* leave a blank line */
1049 repeat = var_GetBool(p_playlist, "repeat") ? _("[Repeat] ") : "";
1050 random = var_GetBool(p_playlist, "random") ? _("[Random] ") : "";
1051 loop = var_GetBool(p_playlist, "loop") ? _("[Loop]") : "";
1053 if (p_input && !p_input->b_dead) {
1057 uri = input_item_GetURI(input_GetItem(p_input));
1058 path = make_path(uri);
1060 mvnprintw(y++, 0, COLS, _(" Source : %s"), path?path:uri);
1064 var_Get(p_input, "state", &val);
1067 static const char *input_state[] = {
1068 [PLAYING_S] = " State : Playing %s%s%s",
1069 [OPENING_S] = " State : Opening/Connecting %s%s%s",
1070 [PAUSE_S] = " State : Paused %s%s%s",
1072 char buf1[MSTRTIME_MAX_SIZE];
1073 char buf2[MSTRTIME_MAX_SIZE];
1084 mvnprintw(y++, 0, COLS, _(input_state[val.i_int]),
1085 repeat, random, loop);
1088 var_Get(p_input, "time", &val);
1089 secstotimestr(buf1, val.i_time / CLOCK_FREQ);
1090 var_Get(p_input, "length", &val);
1091 secstotimestr(buf2, val.i_time / CLOCK_FREQ);
1093 mvnprintw(y++, 0, COLS, _(" Position : %s/%s"), buf1, buf2);
1095 i_volume = aout_VolumeGet(p_playlist);
1096 mvnprintw(y++, 0, COLS, _(" Volume : %u%%"),
1097 i_volume*100/AOUT_VOLUME_DEFAULT);
1099 if (!var_Get(p_input, "title", &val)) {
1100 int i_title_count = var_CountChoices(p_input, "title");
1101 if (i_title_count > 0)
1102 mvnprintw(y++, 0, COLS, _(" Title : %"PRId64"/%d"),
1103 val.i_int, i_title_count);
1106 if (!var_Get(p_input, "chapter", &val)) {
1107 int i_chapter_count = var_CountChoices(p_input, "chapter");
1108 if (i_chapter_count > 0) mvnprintw(y++, 0, COLS, _(" Chapter : %"PRId64"/%d"),
1109 val.i_int, i_chapter_count);
1113 mvnprintw(y++, 0, COLS, _(" Source: <no current item> "));
1114 mvnprintw(y++, 0, COLS, " %s%s%s", repeat, random, loop);
1115 mvnprintw(y++, 0, COLS, _(" [ h for help ]"));
1116 DrawEmptyLine(y++, 0, COLS);
1119 if (sys->color) color_set(C_DEFAULT, NULL);
1120 DrawBox(y++, 1, sys->color, ""); /* position slider */
1121 DrawEmptyLine(y, 1, COLS-2);
1123 DrawLine(y, 1, (int)((COLS-2) * var_GetFloat(p_input, "position")));
1125 y += 2; /* skip slider and box */
1130 static void FillTextBox(intf_sys_t *sys)
1132 int width = COLS - 2;
1133 const char *title = sys->box_type == BOX_OPEN ? "Open: %s" : "Find: %s";
1134 char *chain = sys->box_type == BOX_OPEN ? sys->open_chain :
1135 sys->old_search ? sys->old_search :
1138 DrawEmptyLine(7, 1, width);
1139 mvnprintw(7, 1, width, _(title), chain);
1142 static void FillBox(intf_thread_t *intf)
1144 intf_sys_t *sys = intf->p_sys;
1145 static int (* const draw[]) (intf_thread_t *) = {
1146 [BOX_HELP] = DrawHelp,
1147 [BOX_INFO] = DrawInfo,
1148 [BOX_META] = DrawMeta,
1149 [BOX_OBJECTS] = DrawObjects,
1150 [BOX_STATS] = DrawStats,
1151 [BOX_BROWSE] = DrawBrowse,
1152 [BOX_PLAYLIST] = DrawPlaylist,
1153 [BOX_SEARCH] = DrawPlaylist,
1154 [BOX_OPEN] = DrawPlaylist,
1155 [BOX_LOG] = DrawMessages,
1158 sys->box_lines_total = draw[sys->box_type](intf);
1160 if (sys->box_type == BOX_SEARCH || sys->box_type == BOX_OPEN)
1164 static void Redraw(intf_thread_t *intf)
1166 intf_sys_t *sys = intf->p_sys;
1167 int box = sys->box_type;
1168 int y = DrawStatus(intf);
1170 sys->box_height = LINES - y - 2;
1171 DrawBox(y++, sys->box_height, sys->color, _(box_title[box]));
1175 if (box != BOX_NONE) {
1178 if (sys->box_lines_total == 0)
1180 else if (sys->box_start > sys->box_lines_total - 1)
1181 sys->box_start = sys->box_lines_total - 1;
1182 y += __MIN(sys->box_lines_total - sys->box_start,
1186 while (y < LINES - 1)
1187 DrawEmptyLine(y++, 1, COLS - 2);
1192 static void ChangePosition(intf_thread_t *intf, float increment)
1194 intf_sys_t *sys = intf->p_sys;
1195 input_thread_t *p_input = sys->p_input;
1198 if (!p_input || var_GetInteger(p_input, "state") != PLAYING_S)
1201 pos = var_GetFloat(p_input, "position") + increment;
1203 if (pos > 0.99) pos = 0.99;
1204 if (pos < 0.0) pos = 0.0;
1206 var_SetFloat(p_input, "position", pos);
1209 static inline void RemoveLastUTF8Entity(char *psz, int len)
1211 while (len && ((psz[--len] & 0xc0) == 0x80)) /* UTF8 continuation byte */
1216 static char *GetDiscDevice(intf_thread_t *intf, const char *name)
1218 static const struct { const char *s; size_t n; const char *v; } devs[] =
1220 { "cdda://", 7, "cd-audio", },
1221 { "dvd://", 6, "dvd", },
1222 { "vcd://", 6, "vcd", },
1226 for (unsigned i = 0; i < sizeof devs / sizeof *devs; i++) {
1227 size_t n = devs[i].n;
1228 if (!strncmp(name, devs[i].s, n)) {
1229 if (name[n] == '@' || name[n] == '\0')
1230 return config_GetPsz(intf, devs[i].v);
1231 /* Omit the beginning MRL-selector characters */
1232 return strdup(name + n);
1236 device = strdup(name);
1238 if (device) /* Remove what we have after @ */
1239 device[strcspn(device, "@")] = '\0';
1244 static void Eject(intf_thread_t *intf)
1246 char *device, *name;
1247 playlist_t * p_playlist = pl_Get(intf);
1249 /* If there's a stream playing, we aren't allowed to eject ! */
1250 if (intf->p_sys->p_input)
1255 if (!playlist_CurrentPlayingItem(p_playlist)) {
1260 name = playlist_CurrentPlayingItem(p_playlist)->p_input->psz_name;
1261 device = name ? GetDiscDevice(intf, name) : NULL;
1266 intf_Eject(intf, device);
1271 static void PlayPause(intf_thread_t *intf)
1273 input_thread_t *p_input = intf->p_sys->p_input;
1276 int64_t state = var_GetInteger( p_input, "state" );
1277 state = (state != PLAYING_S) ? PLAYING_S : PAUSE_S;
1278 var_SetInteger( p_input, "state", state );
1280 playlist_Play(pl_Get(intf));
1283 static inline void BoxSwitch(intf_sys_t *sys, int box)
1285 sys->box_type = (sys->box_type == box) ? BOX_NONE : box;
1290 static bool HandlePlaylistKey(intf_thread_t *intf, int key)
1292 intf_sys_t *sys = intf->p_sys;
1293 playlist_t *p_playlist = pl_Get(intf);
1294 struct pl_item_t *p_pl_item;
1298 /* Playlist Settings */
1299 case 'r': var_ToggleBool(p_playlist, "random"); return true;
1300 case 'l': var_ToggleBool(p_playlist, "loop"); return true;
1301 case 'R': var_ToggleBool(p_playlist, "repeat"); return true;
1306 playlist_RecursiveNodeSort(p_playlist, p_playlist->p_root_onelevel,
1307 SORT_TITLE_NODES_FIRST,
1308 (key == 'o')? ORDER_NORMAL : ORDER_REVERSE);
1309 vlc_mutex_lock(&sys->pl_lock);
1310 sys->need_update = true;
1311 vlc_mutex_unlock(&sys->pl_lock);
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 HandleEditBoxKey(intf_thread_t *intf, int key, int box)
1444 intf_sys_t *sys = intf->p_sys;
1445 bool search = box == BOX_SEARCH;
1446 char *str = search ? sys->search_chain: sys->open_chain;
1447 size_t len = strlen(str);
1449 assert(box == BOX_SEARCH || box == BOX_OPEN);
1454 case KEY_CLEAR: clear(); return;
1461 sys->old_search = strdup(sys->search_chain);
1462 else if (sys->old_search)
1463 SearchPlaylist(sys, sys->old_search);
1465 char *uri = make_URI(sys->open_chain, NULL);
1467 sys->box_type = BOX_PLAYLIST;
1471 playlist_t *p_playlist = pl_Get(intf);
1472 vlc_mutex_lock(&sys->pl_lock);
1473 playlist_item_t *p_parent = sys->node;
1474 vlc_mutex_unlock(&sys->pl_lock);
1478 playlist_item_t *current;
1479 current= playlist_CurrentPlayingItem(p_playlist);
1480 p_parent = current ? current->p_parent : NULL;
1482 p_parent = p_playlist->p_local_onelevel;
1485 while (p_parent->p_parent && p_parent->p_parent->p_parent)
1486 p_parent = p_parent->p_parent;
1489 playlist_Add(p_playlist, uri, NULL,
1490 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
1491 p_parent->p_input == p_playlist->p_local_onelevel->p_input,
1495 sys->plidx_follow = true;
1497 sys->box_type = BOX_PLAYLIST;
1500 case 0x1b: /* ESC */
1501 /* Alt+key combinations return 2 keys in the terminal keyboard:
1502 * ESC, and the 2nd key.
1503 * If some other key is available immediately (where immediately
1504 * means after getch() 1 second delay), that means that the
1505 * ESC key was not pressed.
1507 * man 3X curs_getch says:
1509 * Use of the escape key by a programmer for a single
1510 * character function is discouraged, as it will cause a delay
1511 * of up to one second while the keypad code looks for a
1512 * following function-key sequence.
1518 if (search) sys->box_idx = sys->before_search;
1519 sys->box_type = BOX_PLAYLIST;
1524 RemoveLastUTF8Entity(str, len);
1528 if (len + 1 < (search ? sizeof sys->search_chain
1529 : sizeof sys->open_chain)) {
1531 str[len + 1] = '\0';
1536 free(sys->old_search);
1537 sys->old_search = NULL;
1538 SearchPlaylist(sys, str);
1542 static void InputNavigate(input_thread_t* p_input, const char *var)
1545 var_TriggerCallback(p_input, var);
1548 static void HandleCommonKey(intf_thread_t *intf, int key)
1550 intf_sys_t *sys = intf->p_sys;
1551 playlist_t *p_playlist = pl_Get(intf);
1554 case 0x1b: /* ESC */
1561 libvlc_Quit(intf->p_libvlc);
1562 sys->exit = true; // terminate the main loop
1566 case 'H': BoxSwitch(sys, BOX_HELP); return;
1567 case 'i': BoxSwitch(sys, BOX_INFO); return;
1568 case 'm': BoxSwitch(sys, BOX_META); return;
1569 case 'L': BoxSwitch(sys, BOX_LOG); return;
1570 case 'P': BoxSwitch(sys, BOX_PLAYLIST); return;
1571 case 'B': BoxSwitch(sys, BOX_BROWSE); return;
1572 case 'x': BoxSwitch(sys, BOX_OBJECTS); return;
1573 case 'S': BoxSwitch(sys, BOX_STATS); return;
1575 case '/': /* Search */
1576 sys->search_chain[0] = '\0';
1577 sys->plidx_follow = false;
1578 if (sys->box_type == BOX_PLAYLIST) {
1579 sys->before_search = sys->box_idx;
1580 sys->box_type = BOX_SEARCH;
1582 sys->before_search = 0;
1583 BoxSwitch(sys, BOX_SEARCH);
1587 case 'A': /* Open */
1588 sys->open_chain[0] = '\0';
1589 if (sys->box_type == BOX_PLAYLIST)
1590 sys->box_type = BOX_OPEN;
1592 BoxSwitch(sys, BOX_OPEN);
1596 case KEY_RIGHT: ChangePosition(intf, +0.01); return;
1597 case KEY_LEFT: ChangePosition(intf, -0.01); return;
1599 /* Common control */
1602 vout_thread_t *p_vout = input_GetVout(sys->p_input);
1604 bool fs = var_ToggleBool(p_playlist, "fullscreen");
1605 var_SetBool(p_vout, "fullscreen", fs);
1606 vlc_object_release(p_vout);
1611 case ' ': PlayPause(intf); return;
1612 case 's': playlist_Stop(p_playlist); return;
1613 case 'e': Eject(intf); return;
1615 case '[': InputNavigate(sys->p_input, "prev-title"); return;
1616 case ']': InputNavigate(sys->p_input, "next-title"); return;
1617 case '<': InputNavigate(sys->p_input, "prev-chapter"); return;
1618 case '>': InputNavigate(sys->p_input, "next-chapter"); return;
1620 case 'p': playlist_Prev(p_playlist); break;
1621 case 'n': playlist_Next(p_playlist); break;
1622 case 'a': aout_VolumeUp(p_playlist, 1, NULL); break;
1623 case 'z': aout_VolumeDown(p_playlist, 1, NULL); break;
1637 static bool HandleListKey(intf_thread_t *intf, int key)
1639 intf_sys_t *sys = intf->p_sys;
1640 playlist_t *p_playlist = pl_Get(intf);
1645 /* workaround for FreeBSD + xterm:
1646 * see http://www.nabble.com/curses-vs.-xterm-key-mismatch-t3574377.html */
1649 case KEY_END: sys->box_idx = sys->box_lines_total - 1; break;
1650 case KEY_HOME: sys->box_idx = 0; break;
1651 case KEY_UP: sys->box_idx--; break;
1652 case KEY_DOWN: sys->box_idx++; break;
1653 case KEY_PPAGE:sys->box_idx -= sys->box_height; break;
1654 case KEY_NPAGE:sys->box_idx += sys->box_height; break;
1661 if (sys->box_type == BOX_PLAYLIST) {
1663 sys->plidx_follow = IsIndex(sys, p_playlist, sys->box_idx);
1670 static void HandleKey(intf_thread_t *intf)
1672 intf_sys_t *sys = intf->p_sys;
1674 int box = sys->box_type;
1679 if (box == BOX_SEARCH || box == BOX_OPEN) {
1680 HandleEditBoxKey(intf, key, sys->box_type);
1684 if (box == BOX_NONE)
1690 case KEY_END: ChangePosition(intf, +.99); return;
1691 case KEY_HOME: ChangePosition(intf, -1.0); return;
1692 case KEY_UP: ChangePosition(intf, +0.05); return;
1693 case KEY_DOWN: ChangePosition(intf, -0.05); return;
1694 default: HandleCommonKey(intf, key); return;
1697 if (box == BOX_BROWSE && HandleBrowseKey(intf, key))
1700 if (box == BOX_PLAYLIST && HandlePlaylistKey(intf, key))
1703 if (HandleListKey(intf, key))
1706 HandleCommonKey(intf, key);
1712 static msg_item_t *msg_Copy (const msg_item_t *msg)
1714 msg_item_t *copy = (msg_item_t *)xmalloc (sizeof (*copy));
1715 copy->i_object_id = msg->i_object_id;
1716 copy->psz_object_type = msg->psz_object_type;
1717 copy->psz_module = strdup (msg->psz_module);
1718 copy->psz_header = msg->psz_header ? strdup (msg->psz_header) : NULL;
1722 static void msg_Free (msg_item_t *msg)
1724 free ((char *)msg->psz_module);
1725 free ((char *)msg->psz_header);
1729 static void MsgCallback(void *data, int type, const msg_item_t *msg,
1730 const char *format, va_list ap)
1732 intf_sys_t *sys = data;
1735 if (sys->verbosity < 0
1736 || sys->verbosity < (type - VLC_MSG_ERR)
1737 || vasprintf(&text, format, ap) == -1)
1740 vlc_mutex_lock(&sys->msg_lock);
1742 sys->msgs[sys->i_msgs].type = type;
1743 if (sys->msgs[sys->i_msgs].item != NULL)
1744 msg_Free(sys->msgs[sys->i_msgs].item);
1745 sys->msgs[sys->i_msgs].item = msg_Copy(msg);
1746 free(sys->msgs[sys->i_msgs].msg);
1747 sys->msgs[sys->i_msgs].msg = text;
1749 if (++sys->i_msgs == (sizeof sys->msgs / sizeof *sys->msgs))
1752 vlc_mutex_unlock(&sys->msg_lock);
1755 static inline void UpdateInput(intf_sys_t *sys, playlist_t *p_playlist)
1757 if (!sys->p_input) {
1758 sys->p_input = playlist_CurrentInput(p_playlist);
1759 } else if (sys->p_input->b_dead) {
1760 vlc_object_release(sys->p_input);
1761 sys->p_input = NULL;
1765 /*****************************************************************************
1766 * Run: ncurses thread
1767 *****************************************************************************/
1768 static void Run(intf_thread_t *intf)
1770 intf_sys_t *sys = intf->p_sys;
1771 playlist_t *p_playlist = pl_Get(intf);
1773 int canc = vlc_savecancel();
1775 var_AddCallback(p_playlist, "intf-change", PlaylistChanged, intf);
1776 var_AddCallback(p_playlist, "item-change", ItemChanged, intf);
1777 var_AddCallback(p_playlist, "playlist-item-append", PlaylistChanged, intf);
1779 while (vlc_object_alive(intf) && !sys->exit) {
1780 UpdateInput(sys, p_playlist);
1785 var_DelCallback(p_playlist, "intf-change", PlaylistChanged, intf);
1786 var_DelCallback(p_playlist, "item-change", ItemChanged, intf);
1787 var_DelCallback(p_playlist, "playlist-item-append", PlaylistChanged, intf);
1788 vlc_restorecancel(canc);
1791 /*****************************************************************************
1792 * Open: initialize and create window
1793 *****************************************************************************/
1794 static int Open(vlc_object_t *p_this)
1796 intf_thread_t *intf = (intf_thread_t *)p_this;
1797 intf_sys_t *sys = intf->p_sys = calloc(1, sizeof(intf_sys_t));
1802 vlc_mutex_init(&sys->msg_lock);
1803 vlc_mutex_init(&sys->pl_lock);
1805 sys->verbosity = var_InheritInteger(intf, "verbose");
1806 sys->sub = vlc_Subscribe(MsgCallback, sys);
1808 sys->box_type = BOX_PLAYLIST;
1809 sys->plidx_follow = true;
1810 sys->color = var_CreateGetBool(intf, "color");
1812 sys->current_dir = var_CreateGetNonEmptyString(intf, "browse-dir");
1813 if (!sys->current_dir)
1814 sys->current_dir = config_GetUserDir(VLC_HOME_DIR);
1816 initscr(); /* Initialize the curses library */
1819 start_color_and_pairs(intf);
1821 keypad(stdscr, TRUE);
1822 nonl(); /* Don't do NL -> CR/NL */
1823 cbreak(); /* Take input chars one at a time */
1824 noecho(); /* Don't echo */
1825 curs_set(0); /* Invisible cursor */
1826 timeout(1000); /* blocking getch() */
1829 /* Stop printing errors to the console */
1830 if (!freopen("/dev/null", "wb", stderr))
1831 msg_Err(intf, "Couldn't close stderr (%m)");
1834 PlaylistRebuild(intf),
1840 /*****************************************************************************
1841 * Close: destroy interface window
1842 *****************************************************************************/
1843 static void Close(vlc_object_t *p_this)
1845 intf_sys_t *sys = ((intf_thread_t*)p_this)->p_sys;
1847 PlaylistDestroy(sys);
1850 free(sys->current_dir);
1851 free(sys->old_search);
1854 vlc_object_release(sys->p_input);
1856 endwin(); /* Close the ncurses interface */
1858 vlc_Unsubscribe(sys->sub);
1859 vlc_mutex_destroy(&sys->msg_lock);
1860 vlc_mutex_destroy(&sys->pl_lock);
1861 for(unsigned i = 0; i < sizeof sys->msgs / sizeof *sys->msgs; i++) {
1862 if (sys->msgs[i].item)
1863 msg_Free(sys->msgs[i].item);
1864 free(sys->msgs[i].msg);