1 /*****************************************************************************
2 * ncurses.c : NCurses interface for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 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 /*****************************************************************************
30 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
37 # define _XOPEN_SOURCE_EXTENDED 1
42 #include <vlc_interface.h>
45 #include <vlc_charset.h>
46 #include <vlc_input.h>
47 #include <vlc_playlist.h>
50 #ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h>
53 #if (!defined( WIN32 ) || defined(__MINGW32__))
54 /* Mingw has its own version of dirent */
59 #define CDDA_MRL "cddax://"
61 #define CDDA_MRL "cdda://"
65 #define VCD_MRL "vcdx://"
67 #define VCD_MRL "vcd://"
70 #define SEARCH_CHAIN_SIZE 20
71 #define OPEN_CHAIN_SIZE 50
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close ( vlc_object_t * );
79 static void Run ( intf_thread_t * );
80 static void PlayPause ( intf_thread_t * );
81 static void Eject ( intf_thread_t * );
83 static int HandleKey ( intf_thread_t *, int );
84 static void Redraw ( intf_thread_t *, time_t * );
86 static playlist_item_t *PlaylistGetRoot( intf_thread_t * );
87 static void PlaylistRebuild( intf_thread_t * );
88 static void PlaylistAddNode( intf_thread_t *, playlist_item_t *, int, const char *);
89 static void PlaylistDestroy( intf_thread_t * );
90 static int PlaylistChanged( vlc_object_t *, const char *, vlc_value_t,
91 vlc_value_t, void * );
92 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *,
94 static void FindIndex ( intf_thread_t * );
95 static void SearchPlaylist ( intf_thread_t *, char * );
96 static int SubSearchPlaylist( intf_thread_t *, char *, int, int );
98 static void ManageSlider ( intf_thread_t * );
99 static void ReadDir ( intf_thread_t * );
101 /*****************************************************************************
103 *****************************************************************************/
105 #define BROWSE_TEXT N_("Filebrowser starting point")
106 #define BROWSE_LONGTEXT N_( \
107 "This option allows you to specify the directory the ncurses filebrowser " \
108 "will show you initially.")
111 set_shortname( "Ncurses" );
112 set_description( _("Ncurses interface") );
113 set_capability( "interface", 10 );
114 set_category( CAT_INTERFACE );
115 set_subcategory( SUBCAT_INTERFACE_MAIN );
116 set_callbacks( Open, Close );
117 add_shortcut( "curses" );
118 add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );
121 /*****************************************************************************
122 * intf_sys_t: description and status of ncurses interface
123 *****************************************************************************/
148 playlist_item_t *p_item;
153 input_thread_t *p_input;
163 int i_box_lines_total;
166 int i_box_plidx; /* Playlist index */
167 int b_box_plidx_follow;
168 int i_box_bidx; /* browser index */
170 playlist_item_t *p_node; /* current node */
174 msg_subscription_t* p_sub; /* message bank subscription */
176 char *psz_search_chain; /* for playlist searching */
177 char *psz_old_search; /* for searching next */
180 char *psz_open_chain;
181 char psz_partial_keys[7];
183 char *psz_current_dir;
185 struct dir_entry_t **pp_dir_entries;
186 vlc_bool_t b_show_hidden_files;
188 int i_current_view; /* playlist view */
189 struct pl_item_t **pp_plist;
191 vlc_bool_t b_need_update; /* for playlist view */
193 int i_verbose; /* stores verbosity level */
196 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title );
197 static void DrawLine( WINDOW *win, int y, int x, int w );
198 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
200 /*****************************************************************************
201 * Open: initialize and create window
202 *****************************************************************************/
203 static int Open( vlc_object_t *p_this )
205 intf_thread_t *p_intf = (intf_thread_t *)p_this;
209 /* Allocate instance and initialize some members */
210 p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
211 p_sys->p_node = NULL;
212 p_sys->p_input = NULL;
213 p_sys->f_slider = 0.0;
214 p_sys->f_slider_old = 0.0;
215 p_sys->i_box_type = BOX_PLAYLIST;
216 p_sys->i_box_lines = 0;
217 p_sys->i_box_start= 0;
218 p_sys->i_box_lines_total = 0;
219 p_sys->b_box_plidx_follow = VLC_TRUE;
220 p_sys->b_box_cleared = VLC_FALSE;
221 p_sys->i_box_plidx = 0;
222 p_sys->i_box_bidx = 0;
223 p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
224 memset( p_sys->psz_partial_keys, 0, sizeof( p_sys->psz_partial_keys ) );
226 /* Initialize the curses library */
227 p_sys->w = initscr();
228 keypad( p_sys->w, TRUE );
229 /* Don't do NL -> CR/NL */
231 /* Take input chars one at a time */
241 /* exported function */
242 p_intf->pf_run = Run;
244 /* Remember verbosity level */
245 var_Get( p_intf->p_libvlc, "verbose", &val );
246 p_sys->i_verbose = val.i_int;
249 var_Set( p_intf->p_libvlc, "verbose", val );
251 /* Set defaul playlist view */
252 p_sys->i_current_view = VIEW_CATEGORY;
253 p_sys->pp_plist = NULL;
254 p_sys->i_plist_entries = 0;
255 p_sys->b_need_update = VLC_FALSE;
257 /* Initialize search chain */
258 p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
259 p_sys->psz_old_search = NULL;
260 p_sys->i_before_search = 0;
262 /* Initialize open chain */
263 p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
265 /* Initialize browser options */
266 var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
267 var_Get( p_intf, "browse-dir", &val);
269 if( val.psz_string && *val.psz_string )
271 p_sys->psz_current_dir = strdup( val.psz_string );
272 free( val.psz_string );
276 p_sys->psz_current_dir = strdup( p_intf->p_libvlc->psz_homedir );
279 p_sys->i_dir_entries = 0;
280 p_sys->pp_dir_entries = NULL;
281 p_sys->b_show_hidden_files = VLC_FALSE;
287 /*****************************************************************************
288 * Close: destroy interface window
289 *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
292 intf_thread_t *p_intf = (intf_thread_t *)p_this;
293 intf_sys_t *p_sys = p_intf->p_sys;
294 playlist_t *p_playlist = pl_Get( p_intf );
297 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, p_intf );
298 var_DelCallback( p_playlist, "item-append", PlaylistChanged, p_intf );
300 PlaylistDestroy( p_intf );
302 for( i = 0; i < p_sys->i_dir_entries; i++ )
304 struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
305 if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
306 REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
307 if( p_dir_entry ) free( p_dir_entry );
309 p_sys->pp_dir_entries = NULL;
311 if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
312 if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
313 if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
314 if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
318 vlc_object_release( p_sys->p_input );
320 pl_Release( p_intf );
322 /* Close the ncurses interface */
325 msg_Unsubscribe( p_intf, p_sys->p_sub );
327 /* Restores initial verbose setting */
329 val.i_int = p_sys->i_verbose;
330 var_Set( p_intf->p_libvlc, "verbose", val );
332 /* Destroy structure */
336 /*****************************************************************************
337 * Run: ncurses thread
338 *****************************************************************************/
339 static void Run( intf_thread_t *p_intf )
341 intf_sys_t *p_sys = p_intf->p_sys;
342 playlist_t *p_playlist = pl_Yield( p_intf );
345 time_t t_last_refresh;
348 * force drawing the interface for the first time
350 t_last_refresh = ( time( 0 ) - 1);
352 * force building of the playlist array
354 PlaylistRebuild( p_intf );
356 while( !intf_ShouldDie( p_intf ) )
358 msleep( INTF_IDLE_SLEEP );
360 /* Update the input */
361 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, p_intf );
362 var_AddCallback( p_playlist, "item-append", PlaylistChanged, p_intf );
365 if( p_sys->p_input == NULL )
367 p_sys->p_input = p_playlist->p_input;
370 if( !p_sys->p_input->b_dead )
372 vlc_object_yield( p_sys->p_input );
376 else if( p_sys->p_input->b_dead )
378 vlc_object_release( p_sys->p_input );
379 p_sys->p_input = NULL;
380 p_sys->f_slider = p_sys->f_slider_old = 0.0;
381 p_sys->b_box_cleared = VLC_FALSE;
385 if( p_sys->b_box_plidx_follow && p_playlist->status.p_item )
390 while( ( i_key = getch() ) != -1 )
393 * HandleKey returns 1 if the screen needs to be redrawn
395 if( HandleKey( p_intf, i_key ) )
397 Redraw( p_intf, &t_last_refresh );
401 if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
404 Redraw( p_intf, &t_last_refresh );
405 p_sys->b_box_cleared = VLC_TRUE;
409 * redraw the screen every second
411 if( (time(0) - t_last_refresh) >= 1 )
413 ManageSlider( p_intf );
414 Redraw( p_intf, &t_last_refresh );
419 /* following functions are local */
420 static char *KeyToUTF8( int i_key, char *psz_part )
423 int len = strlen( psz_part );
426 /* overflow error - should not happen */
427 memset( psz_part, 0, 6 );
431 psz_part[len] = (char)i_key;
434 psz_utf8 = strdup( psz_part );
436 psz_utf8 = FromLocaleDup( psz_part );
438 /* Ugly check for incomplete bytes sequences
439 * (in case of non-UTF8 multibyte local encoding) */
441 for( psz = psz_utf8; *psz; psz++ )
442 if( ( *psz == '?' ) && ( *psz_utf8 != '?' ) )
444 /* incomplete bytes sequence detected
445 * (VLC core inserted dummy question marks) */
450 /* Check for incomplete UTF8 bytes sequence */
451 if( EnsureUTF8( psz_utf8 ) == NULL )
458 memset( psz_part, 0, 6 );
462 static inline int RemoveLastUTF8Entity( char *psz, int len )
464 while( len && ( (psz[--len] & 0xc0) == 0x80 ) );
465 /* UTF8 continuation byte */
471 static int HandleKey( intf_thread_t *p_intf, int i_key )
473 intf_sys_t *p_sys = p_intf->p_sys;
475 playlist_t *p_playlist = pl_Get( p_intf );
477 if( p_sys->i_box_type == BOX_PLAYLIST )
479 int b_ret = VLC_TRUE;
480 vlc_bool_t b_box_plidx_follow = VLC_FALSE;
485 /* Playlist Settings */
487 var_Get( p_playlist, "random", &val );
488 val.b_bool = !val.b_bool;
489 var_Set( p_playlist, "random", val );
492 var_Get( p_playlist, "loop", &val );
493 val.b_bool = !val.b_bool;
494 var_Set( p_playlist, "loop", val );
497 var_Get( p_playlist, "repeat", &val );
498 val.b_bool = !val.b_bool;
499 var_Set( p_playlist, "repeat", val );
504 playlist_RecursiveNodeSort( p_playlist,
505 PlaylistGetRoot( p_intf ),
506 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
507 p_sys->b_need_update = VLC_TRUE;
510 playlist_RecursiveNodeSort( p_playlist,
511 PlaylistGetRoot( p_intf ),
512 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
513 p_sys->b_need_update = VLC_TRUE;
518 switch( p_sys->i_current_view )
521 p_sys->i_current_view = VIEW_ONELEVEL;
524 p_sys->i_current_view = VIEW_CATEGORY;
526 //p_sys->b_need_update = VLC_TRUE;
527 PlaylistRebuild( p_intf );
530 /* Playlist navigation */
535 p_sys->i_box_plidx = 0;
538 /* workaround for FreeBSD + xterm:
539 * see http://www.nabble.com/curses-vs.-xterm-key-mismatch-t3574377.html */
543 p_sys->i_box_plidx = p_playlist->items.i_size - 1;
546 p_sys->i_box_plidx--;
549 p_sys->i_box_plidx++;
552 p_sys->i_box_plidx -= p_sys->i_box_lines;
555 p_sys->i_box_plidx += p_sys->i_box_lines;
561 playlist_item_t *p_item;
564 p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
565 if( p_item->i_children == -1 )
567 playlist_DeleteFromInput( p_playlist,
568 p_item->p_input->i_id, VLC_TRUE );
572 playlist_NodeDelete( p_playlist, p_item,
573 VLC_TRUE , VLC_FALSE );
576 PlaylistRebuild( p_intf );
582 if( !p_sys->pp_plist[p_sys->i_box_plidx] )
587 if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
590 playlist_item_t *p_item, *p_parent;
592 p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
595 p_parent = p_playlist->p_root_onelevel;
596 while( p_parent->p_parent )
597 p_parent = p_parent->p_parent;
598 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
599 VLC_TRUE, p_parent, p_item );
601 else if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
603 { /* We only want to set the current node */
604 playlist_Stop( p_playlist );
605 p_sys->p_node = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
609 p_sys->p_node = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
610 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
611 p_sys->pp_plist[p_sys->i_box_plidx]->p_item, NULL );
613 b_box_plidx_follow = VLC_TRUE;
622 int i_max = p_sys->i_plist_entries;
623 if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
624 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
625 if( PlaylistIsPlaying( p_intf,
626 p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
627 b_box_plidx_follow = VLC_TRUE;
628 p_sys->b_box_plidx_follow = b_box_plidx_follow;
632 if( p_sys->i_box_type == BOX_BROWSE )
634 vlc_bool_t b_ret = VLC_TRUE;
635 /* Browser navigation */
639 p_sys->i_box_bidx = 0;
645 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
654 p_sys->i_box_bidx -= p_sys->i_box_lines;
657 p_sys->i_box_bidx += p_sys->i_box_lines;
659 case '.': /* Toggle show hidden files */
660 p_sys->b_show_hidden_files = ( p_sys->b_show_hidden_files ==
661 VLC_TRUE ? VLC_FALSE : VLC_TRUE );
668 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
670 int i_size_entry = strlen( "directory://" ) +
671 strlen( p_sys->psz_current_dir ) +
672 strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
673 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
675 sprintf( psz_uri, "directory://%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
677 playlist_item_t *p_parent = p_sys->p_node;
679 p_parent = p_playlist->status.p_node;
681 p_parent = p_playlist->p_local_onelevel;
683 while( p_parent->p_parent && p_parent->p_parent->p_parent )
684 p_parent = p_parent->p_parent;
686 playlist_Add( p_playlist, psz_uri, NULL, PLAYLIST_APPEND,
689 p_playlist->p_local_onelevel->p_input
692 p_sys->i_box_type = BOX_PLAYLIST;
697 int i_size_entry = strlen( p_sys->psz_current_dir ) +
698 strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
699 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
701 sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
703 p_sys->psz_current_dir = strdup( psz_uri );
714 if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
715 if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
719 else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO ||
720 p_sys->i_box_type == BOX_META )
725 p_sys->i_box_start = 0;
731 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
734 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
737 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
739 p_sys->i_box_start++;
743 p_sys->i_box_start -= p_sys->i_box_lines;
744 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
747 p_sys->i_box_start += p_sys->i_box_lines;
748 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
750 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
757 else if( p_sys->i_box_type == BOX_NONE )
763 ManageSlider( p_intf );
769 p_sys->f_slider = 99.9;
770 ManageSlider( p_intf );
773 p_sys->f_slider += 5.0;
774 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
775 ManageSlider( p_intf );
778 p_sys->f_slider -= 5.0;
779 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
780 ManageSlider( p_intf );
787 else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
790 i_chain_len = strlen( p_sys->psz_search_chain );
798 if( i_chain_len > 0 )
800 p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
802 else if( p_sys->psz_old_search )
804 SearchPlaylist( p_intf, p_sys->psz_old_search );
806 p_sys->i_box_type = BOX_PLAYLIST;
808 case 0x1b: /* Esc. */
809 p_sys->i_box_plidx = p_sys->i_before_search;
810 p_sys->i_box_type = BOX_PLAYLIST;
813 RemoveLastUTF8Entity( p_sys->psz_search_chain, i_chain_len );
817 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
819 if( psz_utf8 != NULL )
821 if( i_chain_len + strlen( psz_utf8 ) < SEARCH_CHAIN_SIZE )
823 strcpy( p_sys->psz_search_chain + i_chain_len,
831 if( p_sys->psz_old_search )
833 free( p_sys->psz_old_search );
834 p_sys->psz_old_search = NULL;
836 SearchPlaylist( p_intf, p_sys->psz_search_chain );
839 else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
841 int i_chain_len = strlen( p_sys->psz_open_chain );
850 if( i_chain_len > 0 )
852 playlist_item_t *p_parent = p_sys->p_node;
855 p_parent = p_playlist->status.p_node;
857 p_parent = p_playlist->p_local_onelevel;
859 while( p_parent->p_parent && p_parent->p_parent->p_parent )
860 p_parent = p_parent->p_parent;
862 playlist_Add( p_playlist, p_sys->psz_open_chain, NULL,
863 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
865 p_playlist->p_local_onelevel->p_input
868 p_sys->b_box_plidx_follow = VLC_TRUE;
870 p_sys->i_box_type = BOX_PLAYLIST;
872 case 0x1b: /* Esc. */
873 p_sys->i_box_type = BOX_PLAYLIST;
876 RemoveLastUTF8Entity( p_sys->psz_open_chain, i_chain_len );
880 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
882 if( psz_utf8 != NULL )
884 if( i_chain_len + strlen( psz_utf8 ) < OPEN_CHAIN_SIZE )
886 strcpy( p_sys->psz_open_chain + i_chain_len,
904 vlc_object_kill( p_intf->p_libvlc );
909 if( p_sys->i_box_type == BOX_INFO )
910 p_sys->i_box_type = BOX_NONE;
912 p_sys->i_box_type = BOX_INFO;
913 p_sys->i_box_lines_total = 0;
916 if( p_sys->i_box_type == BOX_META )
917 p_sys->i_box_type = BOX_NONE;
919 p_sys->i_box_type = BOX_META;
920 p_sys->i_box_lines_total = 0;
923 if( p_sys->i_box_type == BOX_LOG )
924 p_sys->i_box_type = BOX_NONE;
926 p_sys->i_box_type = BOX_LOG;
929 if( p_sys->i_box_type == BOX_PLAYLIST )
930 p_sys->i_box_type = BOX_NONE;
932 p_sys->i_box_type = BOX_PLAYLIST;
935 if( p_sys->i_box_type == BOX_BROWSE )
936 p_sys->i_box_type = BOX_NONE;
938 p_sys->i_box_type = BOX_BROWSE;
942 if( p_sys->i_box_type == BOX_HELP )
943 p_sys->i_box_type = BOX_NONE;
945 p_sys->i_box_type = BOX_HELP;
946 p_sys->i_box_lines_total = 0;
949 if( p_sys->i_box_type != BOX_SEARCH )
951 if( p_sys->psz_search_chain == NULL )
955 p_sys->psz_search_chain[0] = '\0';
956 p_sys->b_box_plidx_follow = VLC_FALSE;
957 p_sys->i_before_search = p_sys->i_box_plidx;
958 p_sys->i_box_type = BOX_SEARCH;
962 if( p_sys->i_box_type != BOX_OPEN )
964 if( p_sys->psz_open_chain == NULL )
968 p_sys->psz_open_chain[0] = '\0';
969 p_sys->i_box_type = BOX_OPEN;
975 p_sys->f_slider += 1.0;
976 if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
977 ManageSlider( p_intf );
981 p_sys->f_slider -= 1.0;
982 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
983 ManageSlider( p_intf );
989 if( p_intf->p_sys->p_input )
991 vout_thread_t *p_vout;
992 p_vout = vlc_object_find( p_intf->p_sys->p_input,
993 VLC_OBJECT_VOUT, FIND_CHILD );
996 var_Get( p_vout, "fullscreen", &val );
997 val.b_bool = !val.b_bool;
998 var_Set( p_vout, "fullscreen", val );
999 vlc_object_release( p_vout );
1003 var_Get( p_playlist, "fullscreen", &val );
1004 val.b_bool = !val.b_bool;
1005 var_Set( p_playlist, "fullscreen", val );
1012 PlayPause( p_intf );
1016 playlist_Stop( p_playlist );
1024 if( p_sys->p_input )
1026 val.b_bool = VLC_TRUE;
1027 var_Set( p_sys->p_input, "prev-title", val );
1032 if( p_sys->p_input )
1034 val.b_bool = VLC_TRUE;
1035 var_Set( p_sys->p_input, "next-title", val );
1040 if( p_sys->p_input )
1042 val.b_bool = VLC_TRUE;
1043 var_Set( p_sys->p_input, "prev-chapter", val );
1048 if( p_sys->p_input )
1050 val.b_bool = VLC_TRUE;
1051 var_Set( p_sys->p_input, "next-chapter", val );
1056 playlist_Prev( p_playlist );
1061 playlist_Next( p_playlist );
1066 aout_VolumeUp( p_intf, 1, NULL );
1071 aout_VolumeDown( p_intf, 1, NULL );
1076 * ^l should clear and redraw the screen
1087 static void ManageSlider( intf_thread_t *p_intf )
1089 intf_sys_t *p_sys = p_intf->p_sys;
1090 input_thread_t *p_input = p_sys->p_input;
1093 if( p_input == NULL )
1097 var_Get( p_input, "state", &val );
1098 if( val.i_int != PLAYING_S )
1103 var_Get( p_input, "position", &val );
1104 if( p_sys->f_slider == p_sys->f_slider_old )
1107 p_sys->f_slider_old = 100 * val.f_float;
1111 p_sys->f_slider_old = p_sys->f_slider;
1113 val.f_float = p_sys->f_slider / 100.0;
1114 var_Set( p_input, "position", val );
1118 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
1123 intf_sys_t *p_sys = p_intf->p_sys;
1125 if( p_sys->i_before_search >= 0 )
1127 i_first = p_sys->i_before_search;
1130 if( ( ! psz_searchstring ) || strlen( psz_searchstring ) <= 0 )
1132 p_sys->i_box_plidx = p_sys->i_before_search;
1136 i_max = p_sys->i_plist_entries;
1138 i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
1141 i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1144 if( i_item < 0 || i_item >= i_max ) return;
1146 p_sys->i_box_plidx = i_item;
1149 static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
1150 int i_start, int i_stop )
1152 intf_sys_t *p_sys = p_intf->p_sys;
1155 for( i = i_start + 1; i < i_stop; i++ )
1157 if( strcasestr( p_sys->pp_plist[i]->psz_display,
1158 psz_searchstring ) != NULL )
1169 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
1175 va_start( vl_args, p_fmt );
1176 if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1180 if( ( p_buf == NULL ) || ( w <= 0 ) )
1183 i_len = strlen( p_buf );
1185 #ifdef HAVE_NCURSESW
1186 wchar_t psz_wide[i_len + 1];
1188 EnsureUTF8( p_buf );
1189 size_t i_char_len = mbstowcs( psz_wide, p_buf, i_len );
1191 size_t i_width; /* number of columns */
1193 if( i_char_len == (size_t)-1 )
1194 /* an invalid character was encountered */
1198 i_width = wcswidth( psz_wide, i_char_len );
1199 if( i_width == (size_t)-1 )
1201 /* a non printable character was encountered */
1205 for( i = 0 ; i < i_char_len ; i++ )
1207 i_cwidth = wcwidth( psz_wide[i] );
1208 if( i_cwidth != -1 )
1209 i_width += i_cwidth;
1213 if( i_width > (size_t)w )
1215 int i_total_width = 0;
1217 while( i_total_width < w )
1219 i_total_width += wcwidth( psz_wide[i] );
1220 if( w > 7 && i_total_width >= w/2 )
1223 psz_wide[i+1] = '.';
1224 i_total_width -= wcwidth( psz_wide[i] ) - 2;
1227 /* we require this check only if at least one character
1228 * 4 or more columns wide exists (which i doubt) */
1229 psz_wide[i-1] = '.';
1230 i_total_width -= wcwidth( psz_wide[i-1] ) - 1;
1233 /* find the widest string */
1234 int j, i_2nd_width = 0;
1235 for( j = i_char_len - 1; i_2nd_width < w - i_total_width; j-- )
1236 i_2nd_width += wcwidth( psz_wide[j] );
1238 /* we already have i_total_width columns filled, and we can't
1239 * have more than w columns */
1240 if( i_2nd_width > w - i_total_width )
1243 wmemmove( &psz_wide[i+2], &psz_wide[j+1], i_char_len - j - 1 );
1244 psz_wide[i + 2 + i_char_len - j - 1] = '\0';
1249 if( w <= 7 ) /* we don't add the '...' else we lose too much chars */
1252 size_t i_wlen = wcslen( psz_wide ) * 6 + 1; /* worst case */
1253 char psz_ellipsized[i_wlen];
1254 wcstombs( psz_ellipsized, psz_wide, i_wlen );
1255 mvprintw( y, x, "%s", psz_ellipsized );
1259 mvprintw( y, x, "%s", p_buf );
1260 mvhline( y, x + i_width, ' ', w - i_width );
1265 int i_cut = i_len - w;
1266 int x1 = i_len/2 - i_cut/2;
1267 int x2 = x1 + i_cut;
1271 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1280 char *psz_local = ToLocale( p_buf );
1281 mvprintw( y, x, "%s", psz_local );
1282 LocaleFree( p_buf );
1286 char *psz_local = ToLocale( p_buf );
1287 mvprintw( y, x, "%s", psz_local );
1288 LocaleFree( p_buf );
1289 mvhline( y, x + i_len, ' ', w - i_len );
1293 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1295 intf_sys_t *p_sys = p_intf->p_sys;
1300 if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1305 va_start( vl_args, p_fmt );
1306 if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1315 mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1318 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1320 intf_sys_t *p_sys = p_intf->p_sys;
1321 input_thread_t *p_input = p_sys->p_input;
1322 playlist_t *p_playlist = pl_Get( p_intf );
1330 attrset( A_REVERSE );
1331 int i_len = strlen( "VLC media player [ h for help ]" );
1332 int mid = ( COLS - i_len ) / 2;
1335 int i_size = ( COLS > i_len + 1 ) ? COLS : i_len + 1;
1336 char psz_title[i_size];
1337 memset( psz_title, ' ', mid );
1338 snprintf( &psz_title[mid], i_size, "VLC media player [ h for help ]" );
1339 mvnprintw( y, 0, COLS, "%s", psz_title );
1340 attroff( A_REVERSE );
1346 /* strlen( "[Repeat] [Random] [Loop]" ) == 24, + '\0' */
1347 psz_state[0] = '\0';
1348 if( var_GetBool( p_playlist, "repeat" ) )
1349 strcat( psz_state, "[Repeat] " );
1350 if( var_GetBool( p_playlist, "random" ) )
1351 strcat( psz_state, "[Random] " );
1352 if( var_GetBool( p_playlist, "loop" ) )
1353 strcat( psz_state, "[Loop]" );
1355 if( p_input && !p_input->b_dead )
1357 char buf1[MSTRTIME_MAX_SIZE];
1358 char buf2[MSTRTIME_MAX_SIZE];
1360 vlc_value_t val_list;
1363 char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
1364 mvnprintw( y++, 0, COLS, " Source : %s", psz_uri );
1368 var_Get( p_input, "state", &val );
1369 if( val.i_int == PLAYING_S )
1371 mvnprintw( y++, 0, COLS, " State : Playing %s", psz_state );
1373 else if( val.i_int == OPENING_S )
1375 mvnprintw( y++, 0, COLS, " State : Opening/Connecting %s", psz_state );
1377 else if( val.i_int == BUFFERING_S )
1379 mvnprintw( y++, 0, COLS, " State : Buffering %s", psz_state );
1381 else if( val.i_int == PAUSE_S )
1383 mvnprintw( y++, 0, COLS, " State : Paused %s", psz_state );
1386 if( val.i_int != INIT_S && val.i_int != END_S )
1388 audio_volume_t i_volume;
1391 var_Get( p_input, "time", &val );
1392 msecstotimestr( buf1, val.i_time / 1000 );
1394 var_Get( p_input, "length", &val );
1395 msecstotimestr( buf2, val.i_time / 1000 );
1397 mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1400 aout_VolumeGet( p_intf, &i_volume );
1401 mvnprintw( y++, 0, COLS, " Volume : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1404 if( !var_Get( p_input, "title", &val ) )
1406 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1407 if( val_list.p_list->i_count > 0 )
1409 mvnprintw( y++, 0, COLS, " Title : %d/%d", val.i_int, val_list.p_list->i_count );
1411 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1415 if( !var_Get( p_input, "chapter", &val ) )
1417 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1418 if( val_list.p_list->i_count > 0 )
1420 mvnprintw( y++, 0, COLS, " Chapter : %d/%d", val.i_int, val_list.p_list->i_count );
1422 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1432 mvnprintw( y++, 0, COLS, "Source: <no current item> %s", psz_state );
1433 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1434 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1435 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1438 DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1439 DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1440 DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1443 p_sys->i_box_y = y + 1;
1444 p_sys->i_box_lines = LINES - y - 2;
1449 if( p_sys->i_box_type == BOX_HELP )
1453 DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1455 MainBoxWrite( p_intf, l++, 1, "[Display]" );
1456 MainBoxWrite( p_intf, l++, 1, " h,H Show/Hide help box" );
1457 MainBoxWrite( p_intf, l++, 1, " i Show/Hide info box" );
1458 MainBoxWrite( p_intf, l++, 1, " m Show/Hide metadata box" );
1459 MainBoxWrite( p_intf, l++, 1, " L Show/Hide messages box" );
1460 MainBoxWrite( p_intf, l++, 1, " P Show/Hide playlist box" );
1461 MainBoxWrite( p_intf, l++, 1, " B Show/Hide filebrowser" );
1462 MainBoxWrite( p_intf, l++, 1, "" );
1464 MainBoxWrite( p_intf, l++, 1, "[Global]" );
1465 MainBoxWrite( p_intf, l++, 1, " q, Q Quit" );
1466 MainBoxWrite( p_intf, l++, 1, " s Stop" );
1467 MainBoxWrite( p_intf, l++, 1, " <space> Pause/Play" );
1468 MainBoxWrite( p_intf, l++, 1, " f Toggle Fullscreen" );
1469 MainBoxWrite( p_intf, l++, 1, " n, p Next/Previous playlist item" );
1470 MainBoxWrite( p_intf, l++, 1, " [, ] Next/Previous title" );
1471 MainBoxWrite( p_intf, l++, 1, " <, > Next/Previous chapter" );
1472 MainBoxWrite( p_intf, l++, 1, " <right> Seek +1%%" );
1473 MainBoxWrite( p_intf, l++, 1, " <left> Seek -1%%" );
1474 MainBoxWrite( p_intf, l++, 1, " a Volume Up" );
1475 MainBoxWrite( p_intf, l++, 1, " z Volume Down" );
1476 MainBoxWrite( p_intf, l++, 1, "" );
1478 MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1479 MainBoxWrite( p_intf, l++, 1, " r Toggle Random playing" );
1480 MainBoxWrite( p_intf, l++, 1, " l Toggle Loop Playlist" );
1481 MainBoxWrite( p_intf, l++, 1, " R Toggle Repeat item" );
1482 MainBoxWrite( p_intf, l++, 1, " o Order Playlist by title" );
1483 MainBoxWrite( p_intf, l++, 1, " O Reverse order Playlist by title" );
1484 MainBoxWrite( p_intf, l++, 1, " g Go to the current playing item" );
1485 MainBoxWrite( p_intf, l++, 1, " / Look for an item" );
1486 MainBoxWrite( p_intf, l++, 1, " A Add an entry" );
1487 MainBoxWrite( p_intf, l++, 1, " D, <del> Delete an entry" );
1488 MainBoxWrite( p_intf, l++, 1, " <backspace> Delete an entry" );
1489 MainBoxWrite( p_intf, l++, 1, "" );
1491 MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1492 MainBoxWrite( p_intf, l++, 1, " <enter> Add the selected file to the playlist" );
1493 MainBoxWrite( p_intf, l++, 1, " <space> Add the selected directory to the playlist" );
1494 MainBoxWrite( p_intf, l++, 1, " . Show/Hide hidden files" );
1495 MainBoxWrite( p_intf, l++, 1, "" );
1497 MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1498 MainBoxWrite( p_intf, l++, 1, " <up>,<down> Navigate through the box line by line" );
1499 MainBoxWrite( p_intf, l++, 1, " <pgup>,<pgdown> Navigate through the box page by page" );
1500 MainBoxWrite( p_intf, l++, 1, "" );
1502 MainBoxWrite( p_intf, l++, 1, "[Player]" );
1503 MainBoxWrite( p_intf, l++, 1, " <up>,<down> Seek +/-5%%" );
1504 MainBoxWrite( p_intf, l++, 1, "" );
1506 MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1507 MainBoxWrite( p_intf, l++, 1, " Ctrl-l Refresh the screen" );
1509 p_sys->i_box_lines_total = l;
1510 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1512 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1515 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1517 y += l - p_sys->i_box_start;
1521 y += p_sys->i_box_lines;
1524 else if( p_sys->i_box_type == BOX_INFO )
1528 DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1533 vlc_mutex_lock( &input_GetItem(p_input)->lock );
1534 for( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
1536 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
1537 if( y >= y_end ) break;
1538 MainBoxWrite( p_intf, l++, 1, " [%s]", p_category->psz_name );
1539 for( j = 0; j < p_category->i_infos; j++ )
1541 info_t *p_info = p_category->pp_infos[j];
1542 if( y >= y_end ) break;
1543 MainBoxWrite( p_intf, l++, 1, " %s: %s", p_info->psz_name, p_info->psz_value );
1546 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1550 MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1552 p_sys->i_box_lines_total = l;
1553 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1555 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1558 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1560 y += l - p_sys->i_box_start;
1564 y += p_sys->i_box_lines;
1567 else if( p_sys->i_box_type == BOX_META )
1572 int i_len = strlen( VLC_META_INFO_CAT );
1573 char psz_title[i_len + 3];
1575 psz_title[1] = '\0';
1576 strcat( &psz_title[1], VLC_META_INFO_CAT );
1577 psz_title[i_len + 1] = ' ';
1578 psz_title[i_len + 2] = '\0';
1579 DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1584 input_item_t *p_item = input_GetItem( p_input );
1585 vlc_mutex_lock( &p_item->lock );
1586 for( i=0; i<VLC_META_TYPE_COUNT; i++ )
1588 if( y >= y_end ) break;
1589 char *psz_meta = p_item->p_meta->ppsz_meta[i];
1590 if( psz_meta && *psz_meta )
1592 const char *psz_meta_title;
1596 psz_meta_title = VLC_META_TITLE; break;
1598 psz_meta_title = VLC_META_ARTIST; break;
1600 psz_meta_title = VLC_META_GENRE ; break;
1602 psz_meta_title = VLC_META_COPYRIGHT; break;
1604 psz_meta_title = VLC_META_COLLECTION; break;
1606 psz_meta_title = VLC_META_SEQ_NUM; break;
1608 psz_meta_title = VLC_META_DESCRIPTION; break;
1610 psz_meta_title = VLC_META_RATING; break;
1612 psz_meta_title = VLC_META_DATE; break;
1614 psz_meta_title = VLC_META_SETTING; break;
1616 psz_meta_title = VLC_META_URL; break;
1618 psz_meta_title = VLC_META_LANGUAGE; break;
1620 psz_meta_title = VLC_META_NOW_PLAYING; break;
1622 psz_meta_title = VLC_META_PUBLISHER; break;
1624 psz_meta_title = VLC_META_ENCODED_BY; break;
1626 psz_meta_title = VLC_META_ART_URL; break;
1628 psz_meta_title = VLC_META_TRACKID; break;
1630 psz_meta_title = ""; break;
1632 MainBoxWrite( p_intf, l++, 1, " [%s]", psz_meta_title );
1633 MainBoxWrite( p_intf, l++, 1, " %s", psz_meta );
1636 vlc_mutex_unlock( &p_item->lock );
1640 MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1642 p_sys->i_box_lines_total = l;
1643 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1645 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1648 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1650 y += l - p_sys->i_box_start;
1654 y += p_sys->i_box_lines;
1657 else if( p_sys->i_box_type == BOX_LOG )
1663 DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1665 i_start = p_intf->p_sys->p_sub->i_start;
1667 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1668 i_stop = *p_intf->p_sys->p_sub->pi_stop;
1669 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1673 static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1674 if( i_line >= h - 2 )
1680 if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1681 if( i_stop == i_start )
1685 mvnprintw( y + h-2-i_line, 1, COLS - 2, " [%s] %s",
1686 ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1687 p_sys->p_sub->p_msg[i_stop].psz_msg );
1690 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1691 p_intf->p_sys->p_sub->i_start = i_stop;
1692 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1695 else if( p_sys->i_box_type == BOX_BROWSE )
1697 /* Filebrowser box */
1698 int i_start, i_stop;
1700 DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1702 if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1703 if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1705 if( p_sys->i_box_bidx < (h - 2)/2 )
1710 else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1712 i_start = p_sys->i_box_bidx - (h - 2)/2;
1713 i_stop = i_start + h - 2;
1717 i_stop = p_sys->i_dir_entries;
1718 i_start = p_sys->i_dir_entries - (h - 2);
1724 if( i_stop > p_sys->i_dir_entries )
1726 i_stop = p_sys->i_dir_entries;
1729 for( i_item = i_start; i_item < i_stop; i_item++ )
1731 vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1733 if( y >= y_end ) break;
1736 attrset( A_REVERSE );
1738 mvnprintw( y++, 1, COLS - 2, " %c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? ' ' : '+',
1739 p_sys->pp_dir_entries[i_item]->psz_path );
1742 attroff( A_REVERSE );
1747 else if( p_sys->i_box_type == BOX_PLAYLIST ||
1748 p_sys->i_box_type == BOX_SEARCH ||
1749 p_sys->i_box_type == BOX_OPEN )
1752 int i_start, i_stop, i_max = p_sys->i_plist_entries;
1756 switch( p_sys->i_current_view )
1759 psz_title = strdup( " Playlist (All, one level) " );
1762 psz_title = strdup( " Playlist (By category) " );
1765 psz_title = strdup( " Playlist (Manually added) " );
1768 DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1770 if( p_sys->b_need_update || p_sys->pp_plist == NULL )
1772 PlaylistRebuild( p_intf );
1774 if( p_sys->b_box_plidx_follow )
1776 FindIndex( p_intf );
1779 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1780 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1781 if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
1783 if( p_sys->i_box_plidx < (h - 2)/2 )
1788 else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1790 i_start = p_sys->i_box_plidx - (h - 2)/2;
1791 i_stop = i_start + h - 2;
1796 i_start = i_max - (h - 2);
1802 if( i_stop > i_max )
1807 for( i_item = i_start; i_item < i_stop; i_item++ )
1809 vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1810 playlist_item_t *p_item = p_sys->pp_plist[i_item]->p_item;
1811 playlist_item_t *p_node = p_sys->p_node;
1813 if( ( p_node && p_item->p_input == p_node->p_input ) ||
1814 ( !p_node && p_item->p_input ==
1815 p_playlist->status.p_node->p_input ) )
1817 else if( p_item == p_node || ( p_item != p_node &&
1818 PlaylistIsPlaying( p_intf, p_item ) ) )
1821 if( y >= y_end ) break;
1824 attrset( A_REVERSE );
1826 mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1827 p_sys->pp_plist[i_item]->psz_display );
1830 attroff( A_REVERSE );
1839 if( p_sys->i_box_type == BOX_SEARCH )
1841 DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1842 if( p_sys->psz_search_chain )
1844 if( strlen( p_sys->psz_search_chain ) == 0 &&
1845 p_sys->psz_old_search != NULL )
1847 /* Searching next entry */
1848 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1852 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1856 if( p_sys->i_box_type == BOX_OPEN )
1858 if( p_sys->psz_open_chain )
1860 DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1861 mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1867 DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1872 *t_last_refresh = time( 0 );
1875 static playlist_item_t *PlaylistGetRoot( intf_thread_t *p_intf )
1877 intf_sys_t *p_sys = p_intf->p_sys;
1878 playlist_t *p_playlist = pl_Get( p_intf );
1880 switch( p_sys->i_current_view )
1883 return p_playlist->p_root_category;
1885 return p_playlist->p_root_onelevel;
1889 static void PlaylistRebuild( intf_thread_t *p_intf )
1891 intf_sys_t *p_sys = p_intf->p_sys;
1892 playlist_t *p_playlist = pl_Get( p_intf );
1896 /* First clear the old one */
1897 PlaylistDestroy( p_intf );
1899 /* Build the new one */
1900 PlaylistAddNode( p_intf, PlaylistGetRoot( p_intf ), 0, "" );
1902 p_sys->b_need_update = VLC_FALSE;
1907 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1908 int i, const char *c )
1910 intf_sys_t *p_sys = p_intf->p_sys;
1911 playlist_item_t *p_child;
1914 for( k = 0; k < p_node->i_children; k++ )
1917 p_child = p_node->pp_children[k];
1918 char *psz_name = input_item_GetTitle( p_child->p_input );
1919 if( !psz_name || !*psz_name )
1922 psz_name = input_item_GetName( p_child->p_input );
1927 if( asprintf( &psz_display, "%s%c-%s", c,
1928 k == p_node->i_children - 1 ? '`' : '|', psz_name ) == -1 )
1933 if( asprintf( &psz_display, " %s", psz_name ) == -1 )
1937 struct pl_item_t *p_pl_item = malloc( sizeof( struct pl_item_t ) );
1940 p_pl_item->psz_display = psz_display;
1941 p_pl_item->p_item = p_child;
1942 INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1943 p_sys->i_plist_entries, p_pl_item );
1946 if( p_child->i_children > 0 )
1949 if( asprintf( &psz_tmp, "%s%c ", c,
1950 k == p_node->i_children - 1 ? ' ' : '|' ) == -1 )
1952 PlaylistAddNode( p_intf, p_child, i,
1953 strlen( c ) ? psz_tmp : " " );
1959 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1960 vlc_value_t oval, vlc_value_t nval, void *param )
1962 intf_thread_t *p_intf = (intf_thread_t *)param;
1963 playlist_t *p_playlist = pl_Get( p_intf );
1964 p_intf->p_sys->b_need_update = VLC_TRUE;
1965 p_intf->p_sys->p_node = p_playlist->status.p_node;
1970 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *p_intf,
1971 playlist_item_t *p_item )
1973 playlist_t *p_playlist = pl_Get( p_intf );
1974 playlist_item_t *p_played_item = p_playlist->status.p_item;
1975 return( p_item != NULL && p_played_item != NULL &&
1976 p_item->p_input != NULL && p_played_item->p_input != NULL &&
1977 p_item->p_input->i_id == p_played_item->p_input->i_id );
1980 static void FindIndex( intf_thread_t *p_intf )
1982 intf_sys_t *p_sys = p_intf->p_sys;
1985 if( p_sys->i_box_plidx < p_sys->i_plist_entries && p_sys->i_box_plidx >= 0 )
1987 playlist_item_t *p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
1988 if( ( p_item->i_children == 0 && p_item == p_sys->p_node ) ||
1989 PlaylistIsPlaying( p_intf, p_item ) )
1993 for( i = 0; i < p_sys->i_plist_entries; i++ )
1995 playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
1996 if( ( p_item->i_children == 0 && p_item == p_sys->p_node ) ||
1997 PlaylistIsPlaying( p_intf, p_sys->pp_plist[i]->p_item ) )
1999 p_sys->i_box_plidx = i;
2005 static void PlaylistDestroy( intf_thread_t *p_intf )
2007 intf_sys_t *p_sys = p_intf->p_sys;
2010 for( i = 0; i < p_sys->i_plist_entries; i++ )
2012 struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
2013 free( p_pl_item->psz_display );
2014 REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
2017 p_sys->pp_plist = NULL;
2018 p_sys->i_plist_entries = 0;
2021 static void Eject( intf_thread_t *p_intf )
2023 char *psz_device = NULL, *psz_parser, *psz_name;
2026 * Get the active input
2027 * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
2028 * If it's neither of these, then return
2031 playlist_t * p_playlist = pl_Get( p_intf );
2034 if( p_playlist->status.p_item == NULL )
2040 psz_name = p_playlist->status.p_item->p_input->psz_name;
2044 if( !strncmp(psz_name, "dvd://", 4) )
2046 switch( psz_name[strlen("dvd://")] )
2050 psz_device = config_GetPsz( p_intf, "dvd" );
2053 /* Omit the first MRL-selector characters */
2054 psz_device = strdup( psz_name + strlen("dvd://" ) );
2058 else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
2060 switch( psz_name[strlen(VCD_MRL)] )
2064 psz_device = config_GetPsz( p_intf, VCD_MRL );
2067 /* Omit the beginning MRL-selector characters */
2068 psz_device = strdup( psz_name + strlen(VCD_MRL) );
2072 else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
2074 switch( psz_name[strlen(CDDA_MRL)] )
2078 psz_device = config_GetPsz( p_intf, "cd-audio" );
2081 /* Omit the beginning MRL-selector characters */
2082 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
2088 psz_device = strdup( psz_name );
2094 if( psz_device == NULL )
2099 /* Remove what we have after @ */
2100 psz_parser = psz_device;
2101 for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
2103 if( *psz_parser == '@' )
2110 /* If there's a stream playing, we aren't allowed to eject ! */
2111 if( p_intf->p_sys->p_input == NULL )
2113 msg_Dbg( p_intf, "ejecting %s", psz_device );
2115 intf_Eject( p_intf, psz_device );
2122 static int comp_dir_entries( const void *pp_dir_entry1,
2123 const void *pp_dir_entry2 )
2125 struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
2126 struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
2127 if ( p_dir_entry1->b_file == p_dir_entry2->b_file ) {
2128 return strcasecmp( p_dir_entry1->psz_path, p_dir_entry2->psz_path );
2132 return ( p_dir_entry1->b_file ? 1 : -1 );
2136 static void ReadDir( intf_thread_t *p_intf )
2138 intf_sys_t *p_sys = p_intf->p_sys;
2142 if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
2147 p_current_dir = utf8_opendir( p_sys->psz_current_dir );
2149 if( p_current_dir == NULL )
2151 /* something went bad, get out of here ! */
2152 msg_Warn( p_intf, "cannot open directory `%s' (%m)",
2153 p_sys->psz_current_dir );
2157 /* Clean the old shit */
2158 for( i = 0; i < p_sys->i_dir_entries; i++ )
2160 struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
2161 free( p_dir_entry->psz_path );
2162 REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
2163 free( p_dir_entry );
2165 p_sys->pp_dir_entries = NULL;
2166 p_sys->i_dir_entries = 0;
2168 /* while we still have entries in the directory */
2169 while( ( psz_entry = utf8_readdir( p_current_dir ) ) != NULL )
2171 #if defined( S_ISDIR )
2172 struct stat stat_data;
2174 struct dir_entry_t *p_dir_entry;
2175 int i_size_entry = strlen( p_sys->psz_current_dir ) +
2176 strlen( psz_entry ) + 2;
2179 if( p_sys->b_show_hidden_files == VLC_FALSE &&
2180 ( strlen( psz_entry ) && psz_entry[0] == '.' ) &&
2181 strcmp( psz_entry, ".." ) )
2187 psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
2188 sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, psz_entry );
2190 if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
2197 #if defined( S_ISDIR )
2198 if( !utf8_stat( psz_uri, &stat_data )
2199 && S_ISDIR(stat_data.st_mode) )
2200 /*#elif defined( DT_DIR )
2201 if( p_dir_content->d_type & DT_DIR )*/
2206 p_dir_entry->psz_path = strdup( psz_entry );
2207 p_dir_entry->b_file = VLC_FALSE;
2208 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2209 p_sys->i_dir_entries, p_dir_entry );
2213 p_dir_entry->psz_path = strdup( psz_entry );
2214 p_dir_entry->b_file = VLC_TRUE;
2215 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2216 p_sys->i_dir_entries, p_dir_entry );
2224 qsort( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2225 sizeof(struct dir_entry_t*), &comp_dir_entries );
2227 closedir( p_current_dir );
2232 msg_Dbg( p_intf, "no current dir set" );
2237 static void PlayPause( intf_thread_t *p_intf )
2239 input_thread_t *p_input = p_intf->p_sys->p_input;
2240 playlist_t *p_playlist = pl_Get( p_intf );
2245 var_Get( p_input, "state", &val );
2246 if( val.i_int != PAUSE_S )
2248 val.i_int = PAUSE_S;
2252 val.i_int = PLAYING_S;
2254 var_Set( p_input, "state", val );
2257 playlist_Play( p_playlist );
2260 /****************************************************************************
2262 ****************************************************************************/
2263 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title )
2268 if( w > 3 && h > 2 )
2270 if( title == NULL ) title = "";
2271 i_len = strlen( title );
2273 if( i_len > w - 2 ) i_len = w - 2;
2275 mvwaddch( win, y, x, ACS_ULCORNER );
2276 mvwhline( win, y, x+1, ACS_HLINE, ( w-i_len-2)/2 );
2277 mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
2278 mvwhline( win, y, x+(w-i_len)/2+i_len, ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
2279 mvwaddch( win, y, x+w-1,ACS_URCORNER );
2281 for( i = 0; i < h-2; i++ )
2283 mvwaddch( win, y+i+1, x, ACS_VLINE );
2284 mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
2287 mvwaddch( win, y+h-1, x, ACS_LLCORNER );
2288 mvwhline( win, y+h-1, x+1, ACS_HLINE, w - 2 );
2289 mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
2293 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
2297 mvhline( y, x, ' ', w );
2301 static void DrawLine( WINDOW *win, int y, int x, int w )
2305 attrset( A_REVERSE );
2306 mvhline( y, x, ' ', w );
2307 attroff( A_REVERSE );