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 playlist_t *p_playlist;
154 input_thread_t *p_input;
164 int i_box_lines_total;
167 int i_box_plidx; /* Playlist index */
168 int b_box_plidx_follow;
169 int i_box_bidx; /* browser index */
173 msg_subscription_t* p_sub; /* message bank subscription */
175 char *psz_search_chain; /* for playlist searching */
176 char *psz_old_search; /* for searching next */
179 char *psz_open_chain;
180 char psz_partial_keys[7];
182 char *psz_current_dir;
184 struct dir_entry_t **pp_dir_entries;
185 vlc_bool_t b_show_hidden_files;
187 int i_current_view; /* playlist view */
188 struct pl_item_t **pp_plist;
190 vlc_bool_t b_need_update; /* for playlist view */
192 int i_verbose; /* stores verbosity level */
195 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title );
196 static void DrawLine( WINDOW *win, int y, int x, int w );
197 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
199 /*****************************************************************************
200 * Open: initialize and create window
201 *****************************************************************************/
202 static int Open( vlc_object_t *p_this )
204 intf_thread_t *p_intf = (intf_thread_t *)p_this;
208 /* Allocate instance and initialize some members */
209 p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
210 p_sys->p_playlist = NULL;
211 p_sys->p_input = NULL;
212 p_sys->f_slider = 0.0;
213 p_sys->f_slider_old = 0.0;
214 p_sys->i_box_type = BOX_PLAYLIST;
215 p_sys->i_box_lines = 0;
216 p_sys->i_box_start= 0;
217 p_sys->i_box_lines_total = 0;
218 p_sys->b_box_plidx_follow = VLC_TRUE;
219 p_sys->b_box_cleared = VLC_FALSE;
220 p_sys->i_box_plidx = 0;
221 p_sys->i_box_bidx = 0;
222 p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
223 memset( p_sys->psz_partial_keys, 0, sizeof( p_sys->psz_partial_keys ) );
225 /* Initialize the curses library */
226 p_sys->w = initscr();
227 keypad( p_sys->w, TRUE );
228 /* Don't do NL -> CR/NL */
230 /* Take input chars one at a time */
240 /* exported function */
241 p_intf->pf_run = Run;
243 /* Remember verbosity level */
244 var_Get( p_intf->p_libvlc, "verbose", &val );
245 p_sys->i_verbose = val.i_int;
248 var_Set( p_intf->p_libvlc, "verbose", val );
250 /* Set defaul playlist view */
251 p_sys->i_current_view = VIEW_CATEGORY;
252 p_sys->pp_plist = NULL;
253 p_sys->i_plist_entries = 0;
254 p_sys->b_need_update = VLC_FALSE;
256 /* Initialize search chain */
257 p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
258 p_sys->psz_old_search = NULL;
259 p_sys->i_before_search = 0;
261 /* Initialize open chain */
262 p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
264 /* Initialize browser options */
265 var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
266 var_Get( p_intf, "browse-dir", &val);
268 if( val.psz_string && *val.psz_string )
270 p_sys->psz_current_dir = strdup( val.psz_string );
271 free( val.psz_string );
275 p_sys->psz_current_dir = strdup( p_intf->p_libvlc->psz_homedir );
278 p_sys->i_dir_entries = 0;
279 p_sys->pp_dir_entries = NULL;
280 p_sys->b_show_hidden_files = VLC_FALSE;
286 /*****************************************************************************
287 * Close: destroy interface window
288 *****************************************************************************/
289 static void Close( vlc_object_t *p_this )
291 intf_thread_t *p_intf = (intf_thread_t *)p_this;
292 intf_sys_t *p_sys = p_intf->p_sys;
295 var_DelCallback( p_sys->p_playlist, "intf-change", PlaylistChanged,
297 var_DelCallback( p_sys->p_playlist, "item-append", PlaylistChanged,
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 if( p_sys->p_playlist )
322 vlc_object_release( p_sys->p_playlist );
325 /* Close the ncurses interface */
328 msg_Unsubscribe( p_intf, p_sys->p_sub );
330 /* Restores initial verbose setting */
332 val.i_int = p_sys->i_verbose;
333 var_Set( p_intf->p_libvlc, "verbose", val );
335 /* Destroy structure */
339 /*****************************************************************************
340 * Run: ncurses thread
341 *****************************************************************************/
342 static void Run( intf_thread_t *p_intf )
344 intf_sys_t *p_sys = p_intf->p_sys;
347 time_t t_last_refresh;
350 * force drawing the interface for the first time
352 t_last_refresh = ( time( 0 ) - 1);
354 while( !intf_ShouldDie( p_intf ) )
356 msleep( INTF_IDLE_SLEEP );
358 /* Update the input */
359 if( p_sys->p_playlist == NULL )
361 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
363 if( p_sys->p_playlist )
365 var_AddCallback( p_sys->p_playlist, "intf-change",
366 PlaylistChanged, p_intf );
367 var_AddCallback( p_sys->p_playlist, "item-append",
368 PlaylistChanged, p_intf );
371 if( p_sys->p_playlist )
373 vlc_mutex_lock( &p_sys->p_playlist->object_lock );
374 if( p_sys->p_input == NULL )
376 p_sys->p_input = p_sys->p_playlist->p_input;
379 if( !p_sys->p_input->b_dead )
381 vlc_object_yield( p_sys->p_input );
385 else if( p_sys->p_input->b_dead )
387 vlc_object_release( p_sys->p_input );
388 p_sys->p_input = NULL;
389 p_sys->f_slider = p_sys->f_slider_old = 0.0;
390 p_sys->b_box_cleared = VLC_FALSE;
392 vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
395 if( p_sys->b_box_plidx_follow && p_sys->p_playlist->status.p_item )
400 while( ( i_key = getch() ) != -1 )
403 * HandleKey returns 1 if the screen needs to be redrawn
405 if( HandleKey( p_intf, i_key ) )
407 Redraw( p_intf, &t_last_refresh );
411 if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
414 Redraw( p_intf, &t_last_refresh );
415 p_sys->b_box_cleared = VLC_TRUE;
419 * redraw the screen every second
421 if( (time(0) - t_last_refresh) >= 1 )
423 ManageSlider( p_intf );
424 Redraw( p_intf, &t_last_refresh );
429 /* following functions are local */
430 static char *KeyToUTF8( int i_key, char *psz_part )
433 int len = strlen( psz_part );
436 /* overflow error - should not happen */
437 memset( psz_part, 0, 6 );
441 psz_part[len] = (char)i_key;
444 psz_utf8 = strdup( psz_part );
446 psz_utf8 = FromLocaleDup( psz_part );
448 /* Ugly check for incomplete bytes sequences
449 * (in case of non-UTF8 multibyte local encoding) */
451 for( psz = psz_utf8; *psz; psz++ )
452 if( ( *psz == '?' ) && ( *psz_utf8 != '?' ) )
454 /* incomplete bytes sequence detected
455 * (VLC core inserted dummy question marks) */
460 /* Check for incomplete UTF8 bytes sequence */
461 if( EnsureUTF8( psz_utf8 ) == NULL )
468 memset( psz_part, 0, 6 );
472 static inline int RemoveLastUTF8Entity( char *psz, int len )
474 while( len && ( (psz[--len] & 0xc0) == 0x80 ) );
475 /* UTF8 continuation byte */
481 static int HandleKey( intf_thread_t *p_intf, int i_key )
483 intf_sys_t *p_sys = p_intf->p_sys;
486 if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
488 int b_ret = VLC_TRUE;
489 vlc_bool_t b_box_plidx_follow = VLC_FALSE;
494 /* Playlist Settings */
496 var_Get( p_sys->p_playlist, "random", &val );
497 val.b_bool = !val.b_bool;
498 var_Set( p_sys->p_playlist, "random", val );
501 var_Get( p_sys->p_playlist, "loop", &val );
502 val.b_bool = !val.b_bool;
503 var_Set( p_sys->p_playlist, "loop", val );
506 var_Get( p_sys->p_playlist, "repeat", &val );
507 val.b_bool = !val.b_bool;
508 var_Set( p_sys->p_playlist, "repeat", val );
513 playlist_RecursiveNodeSort( p_sys->p_playlist,
514 PlaylistGetRoot( p_intf ),
515 SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
516 p_sys->b_need_update = VLC_TRUE;
519 playlist_RecursiveNodeSort( p_sys->p_playlist,
520 PlaylistGetRoot( p_intf ),
521 SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
522 p_sys->b_need_update = VLC_TRUE;
527 switch( p_sys->i_current_view )
530 p_sys->i_current_view = VIEW_ONELEVEL;
533 p_sys->i_current_view = VIEW_CATEGORY;
535 //p_sys->b_need_update = VLC_TRUE;
536 PlaylistRebuild( p_intf );
539 /* Playlist navigation */
541 p_sys->i_box_plidx = 0;
544 p_sys->i_box_plidx = p_sys->p_playlist->items.i_size - 1;
547 p_sys->i_box_plidx--;
550 p_sys->i_box_plidx++;
553 p_sys->i_box_plidx -= p_sys->i_box_lines;
556 p_sys->i_box_plidx += p_sys->i_box_lines;
562 playlist_t *p_playlist = p_sys->p_playlist;
563 playlist_item_t *p_item;
565 vlc_mutex_lock( &p_playlist->object_lock );
566 p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
567 if( p_item->i_children == -1 )
569 playlist_DeleteFromInput( p_playlist,
570 p_item->p_input->i_id, VLC_TRUE );
574 playlist_NodeDelete( p_playlist, p_item,
575 VLC_TRUE , VLC_FALSE );
577 vlc_mutex_unlock( &p_playlist->object_lock );
578 PlaylistRebuild( p_intf );
584 if( !p_sys->pp_plist || !p_sys->pp_plist[p_sys->i_box_plidx] )
589 if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
592 playlist_item_t *p_item, *p_parent;
594 p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
597 p_parent = p_sys->p_playlist->p_root_onelevel;
598 while( p_parent->p_parent )
599 p_parent = p_parent->p_parent;
600 playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
601 VLC_TRUE, p_parent, p_item );
604 { /* FIXME doesn't work if the node is empty */
605 playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
607 p_sys->pp_plist[p_sys->i_box_plidx]->p_item,
610 b_box_plidx_follow = VLC_TRUE;
619 int i_max = p_sys->i_plist_entries;
620 if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
621 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
622 if( PlaylistIsPlaying( p_intf,
623 p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
624 b_box_plidx_follow = VLC_TRUE;
625 p_sys->b_box_plidx_follow = b_box_plidx_follow;
629 if( p_sys->i_box_type == BOX_BROWSE )
631 vlc_bool_t b_ret = VLC_TRUE;
632 /* Browser navigation */
636 p_sys->i_box_bidx = 0;
639 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
648 p_sys->i_box_bidx -= p_sys->i_box_lines;
651 p_sys->i_box_bidx += p_sys->i_box_lines;
653 case '.': /* Toggle show hidden files */
654 p_sys->b_show_hidden_files = ( p_sys->b_show_hidden_files ==
655 VLC_TRUE ? VLC_FALSE : VLC_TRUE );
662 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
664 int i_size_entry = strlen( p_sys->psz_current_dir ) +
665 strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
666 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
668 sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
670 playlist_item_t *p_parent = p_sys->p_playlist->status.p_node;
672 p_parent = p_sys->p_playlist->p_root_onelevel;
674 while( p_parent->p_parent )
675 p_parent = p_parent->p_parent;
677 playlist_Add( p_sys->p_playlist, psz_uri, NULL,
678 PLAYLIST_APPEND, PLAYLIST_END,
679 p_parent == p_sys->p_playlist->p_root_onelevel
681 p_sys->i_box_type = BOX_PLAYLIST;
686 int i_size_entry = strlen( p_sys->psz_current_dir ) +
687 strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
688 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
690 sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
692 p_sys->psz_current_dir = strdup( psz_uri );
703 if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
704 if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
708 else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO ||
709 p_sys->i_box_type == BOX_META )
714 p_sys->i_box_start = 0;
717 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
720 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
723 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
725 p_sys->i_box_start++;
729 p_sys->i_box_start -= p_sys->i_box_lines;
730 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
733 p_sys->i_box_start += p_sys->i_box_lines;
734 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
736 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
743 else if( p_sys->i_box_type == BOX_NONE )
749 ManageSlider( p_intf );
752 p_sys->f_slider = 99.9;
753 ManageSlider( p_intf );
756 p_sys->f_slider += 5.0;
757 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
758 ManageSlider( p_intf );
761 p_sys->f_slider -= 5.0;
762 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
763 ManageSlider( p_intf );
770 else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
773 i_chain_len = strlen( p_sys->psz_search_chain );
781 if( i_chain_len > 0 )
783 p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
785 else if( p_sys->psz_old_search )
787 SearchPlaylist( p_intf, p_sys->psz_old_search );
789 p_sys->i_box_type = BOX_PLAYLIST;
791 case 0x1b: /* Esc. */
792 p_sys->i_box_plidx = p_sys->i_before_search;
793 p_sys->i_box_type = BOX_PLAYLIST;
796 RemoveLastUTF8Entity( p_sys->psz_search_chain, i_chain_len );
800 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
802 if( psz_utf8 != NULL )
804 if( i_chain_len + strlen( psz_utf8 ) < SEARCH_CHAIN_SIZE )
806 strcpy( p_sys->psz_search_chain + i_chain_len,
814 if( p_sys->psz_old_search )
816 free( p_sys->psz_old_search );
817 p_sys->psz_old_search = NULL;
819 SearchPlaylist( p_intf, p_sys->psz_search_chain );
822 else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
824 int i_chain_len = strlen( p_sys->psz_open_chain );
825 playlist_t *p_playlist = p_sys->p_playlist;
834 if( p_playlist && i_chain_len > 0 )
836 playlist_item_t *p_parent = p_sys->p_playlist->status.p_node;
838 p_parent = p_sys->p_playlist->p_root_onelevel;
840 while( p_parent->p_parent )
841 p_parent = p_parent->p_parent;
843 playlist_Add( p_playlist, p_sys->psz_open_chain, NULL,
844 PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
845 p_parent == p_sys->p_playlist->p_root_onelevel
847 p_sys->b_box_plidx_follow = VLC_TRUE;
849 p_sys->i_box_type = BOX_PLAYLIST;
851 case 0x1b: /* Esc. */
852 p_sys->i_box_type = BOX_PLAYLIST;
855 RemoveLastUTF8Entity( p_sys->psz_open_chain, i_chain_len );
859 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
861 if( psz_utf8 != NULL )
863 if( i_chain_len + strlen( psz_utf8 ) < OPEN_CHAIN_SIZE )
865 strcpy( p_sys->psz_open_chain + i_chain_len,
883 vlc_object_kill( p_intf->p_libvlc );
888 if( p_sys->i_box_type == BOX_INFO )
889 p_sys->i_box_type = BOX_NONE;
891 p_sys->i_box_type = BOX_INFO;
892 p_sys->i_box_lines_total = 0;
895 if( p_sys->i_box_type == BOX_META )
896 p_sys->i_box_type = BOX_NONE;
898 p_sys->i_box_type = BOX_META;
899 p_sys->i_box_lines_total = 0;
902 if( p_sys->i_box_type == BOX_LOG )
903 p_sys->i_box_type = BOX_NONE;
905 p_sys->i_box_type = BOX_LOG;
908 if( p_sys->i_box_type == BOX_PLAYLIST )
909 p_sys->i_box_type = BOX_NONE;
911 p_sys->i_box_type = BOX_PLAYLIST;
914 if( p_sys->i_box_type == BOX_BROWSE )
915 p_sys->i_box_type = BOX_NONE;
917 p_sys->i_box_type = BOX_BROWSE;
921 if( p_sys->i_box_type == BOX_HELP )
922 p_sys->i_box_type = BOX_NONE;
924 p_sys->i_box_type = BOX_HELP;
925 p_sys->i_box_lines_total = 0;
928 if( p_sys->i_box_type != BOX_SEARCH )
930 if( p_sys->psz_search_chain == NULL )
934 p_sys->psz_search_chain[0] = '\0';
935 p_sys->b_box_plidx_follow = VLC_FALSE;
936 p_sys->i_before_search = p_sys->i_box_plidx;
937 p_sys->i_box_type = BOX_SEARCH;
941 if( p_sys->i_box_type != BOX_OPEN )
943 if( p_sys->psz_open_chain == NULL )
947 p_sys->psz_open_chain[0] = '\0';
948 p_sys->i_box_type = BOX_OPEN;
954 p_sys->f_slider += 1.0;
955 if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
956 ManageSlider( p_intf );
960 p_sys->f_slider -= 1.0;
961 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
962 ManageSlider( p_intf );
968 if( p_intf->p_sys->p_input )
970 vout_thread_t *p_vout;
971 p_vout = vlc_object_find( p_intf->p_sys->p_input,
972 VLC_OBJECT_VOUT, FIND_CHILD );
975 var_Get( p_vout, "fullscreen", &val );
976 val.b_bool = !val.b_bool;
977 var_Set( p_vout, "fullscreen", val );
978 vlc_object_release( p_vout );
982 playlist_t *p_playlist;
983 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
987 var_Get( p_playlist, "fullscreen", &val );
988 val.b_bool = !val.b_bool;
989 var_Set( p_playlist, "fullscreen", val );
990 vlc_object_release( p_playlist );
1002 if( p_intf->p_sys->p_playlist )
1004 playlist_Stop( p_intf->p_sys->p_playlist );
1013 if( p_sys->p_input )
1015 val.b_bool = VLC_TRUE;
1016 var_Set( p_sys->p_input, "prev-title", val );
1021 if( p_sys->p_input )
1023 val.b_bool = VLC_TRUE;
1024 var_Set( p_sys->p_input, "next-title", val );
1029 if( p_sys->p_input )
1031 val.b_bool = VLC_TRUE;
1032 var_Set( p_sys->p_input, "prev-chapter", val );
1037 if( p_sys->p_input )
1039 val.b_bool = VLC_TRUE;
1040 var_Set( p_sys->p_input, "next-chapter", val );
1045 if( p_intf->p_sys->p_playlist )
1047 playlist_Prev( p_intf->p_sys->p_playlist );
1053 if( p_intf->p_sys->p_playlist )
1055 playlist_Next( p_intf->p_sys->p_playlist );
1061 aout_VolumeUp( p_intf, 1, NULL );
1066 aout_VolumeDown( p_intf, 1, NULL );
1071 * ^l should clear and redraw the screen
1082 static void ManageSlider( intf_thread_t *p_intf )
1084 intf_sys_t *p_sys = p_intf->p_sys;
1085 input_thread_t *p_input = p_sys->p_input;
1088 if( p_input == NULL )
1092 var_Get( p_input, "state", &val );
1093 if( val.i_int != PLAYING_S )
1098 var_Get( p_input, "position", &val );
1099 if( p_sys->f_slider == p_sys->f_slider_old )
1102 p_sys->f_slider_old = 100 * val.f_float;
1106 p_sys->f_slider_old = p_sys->f_slider;
1108 val.f_float = p_sys->f_slider / 100.0;
1109 var_Set( p_input, "position", val );
1113 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
1118 intf_sys_t *p_sys = p_intf->p_sys;
1120 if( p_sys->i_before_search >= 0 )
1122 i_first = p_sys->i_before_search;
1125 if( ( ! psz_searchstring ) || strlen( psz_searchstring ) <= 0 )
1127 p_sys->i_box_plidx = p_sys->i_before_search;
1131 i_max = p_sys->i_plist_entries;
1133 i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
1136 i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1139 if( i_item < 0 || i_item >= i_max ) return;
1141 p_sys->i_box_plidx = i_item;
1144 static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
1145 int i_start, int i_stop )
1147 intf_sys_t *p_sys = p_intf->p_sys;
1150 for( i = i_start + 1; i < i_stop; i++ )
1152 if( strcasestr( p_sys->pp_plist[i]->psz_display,
1153 psz_searchstring ) != NULL )
1164 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
1170 va_start( vl_args, p_fmt );
1171 if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1175 if( ( p_buf == NULL ) || ( w <= 0 ) )
1178 i_len = strlen( p_buf );
1180 #ifdef HAVE_NCURSESW
1181 wchar_t psz_wide[i_len + 1];
1183 EnsureUTF8( p_buf );
1184 size_t i_char_len = mbstowcs( psz_wide, p_buf, i_len );
1186 size_t i_width; /* number of columns */
1188 if( i_char_len == (size_t)-1 )
1189 /* an invalid character was encountered */
1193 i_width = wcswidth( psz_wide, i_char_len );
1194 if( i_width == (size_t)-1 )
1195 /* a non printable character was encountered */
1198 if( i_width > (size_t)w )
1200 int i_total_width = 0;
1202 while( i_total_width < w )
1204 i_total_width += wcwidth( psz_wide[i] );
1205 if( w > 7 && i_total_width >= w/2 )
1208 psz_wide[i+1] = '.';
1209 i_total_width -= wcwidth( psz_wide[i] ) - 2;
1212 /* we require this check only if at least one character
1213 * 4 or more columns wide exists (which i doubt) */
1214 psz_wide[i-1] = '.';
1215 i_total_width -= wcwidth( psz_wide[i-1] ) - 1;
1218 /* find the widest string */
1219 int j, i_2nd_width = 0;
1220 for( j = i_char_len - 1; i_2nd_width < w - i_total_width; j-- )
1221 i_2nd_width += wcwidth( psz_wide[j] );
1223 /* we already have i_total_width columns filled, and we can't
1224 * have more than w columns */
1225 if( i_2nd_width > w - i_total_width )
1228 wmemmove( &psz_wide[i+2], &psz_wide[j+1], i_char_len - j - 1 );
1229 psz_wide[i + 2 + i_char_len - j - 1] = '\0';
1234 if( w <= 7 ) /* we don't add the '...' else we lose too much chars */
1237 size_t i_wlen = wcslen( psz_wide ) * 6 + 1; /* worst case */
1238 char psz_ellipsized[i_wlen];
1239 wcstombs( psz_ellipsized, psz_wide, i_wlen );
1240 mvprintw( y, x, "%s", psz_ellipsized );
1244 mvprintw( y, x, "%s", p_buf );
1245 mvhline( y, x + i_width, ' ', w - i_width );
1250 int i_cut = i_len - w;
1251 int x1 = i_len/2 - i_cut/2;
1252 int x2 = x1 + i_cut;
1256 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1265 char *psz_local = ToLocale( p_buf );
1266 mvprintw( y, x, "%s", psz_local );
1267 LocaleFree( p_buf );
1271 char *psz_local = ToLocale( p_buf );
1272 mvprintw( y, x, "%s", psz_local );
1273 LocaleFree( p_buf );
1274 mvhline( y, x + i_len, ' ', w - i_len );
1278 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1280 intf_sys_t *p_sys = p_intf->p_sys;
1285 if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1290 va_start( vl_args, p_fmt );
1291 if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1300 mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1303 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1305 intf_sys_t *p_sys = p_intf->p_sys;
1306 input_thread_t *p_input = p_sys->p_input;
1314 attrset( A_REVERSE );
1315 int i_len = strlen( "VLC media player [ h for help ]" );
1316 int mid = ( COLS - i_len ) / 2;
1319 int i_size = ( COLS > i_len + 1 ) ? COLS : i_len + 1;
1320 char psz_title[i_size];
1321 memset( psz_title, ' ', mid );
1322 snprintf( &psz_title[mid], i_size, "VLC media player [ h for help ]" );
1323 mvnprintw( y, 0, COLS, "%s", psz_title );
1324 attroff( A_REVERSE );
1330 /* strlen( "[Repeat] [Random] [Loop]" ) == 24, + '\0' */
1331 psz_state[0] = '\0';
1332 if( var_GetBool( p_sys->p_playlist, "repeat" ) )
1333 strcat( psz_state, "[Repeat] " );
1334 if( var_GetBool( p_sys->p_playlist, "random" ) )
1335 strcat( psz_state, "[Random] " );
1336 if( var_GetBool( p_sys->p_playlist, "loop" ) )
1337 strcat( psz_state, "[Loop]" );
1339 if( p_input && !p_input->b_dead )
1341 char buf1[MSTRTIME_MAX_SIZE];
1342 char buf2[MSTRTIME_MAX_SIZE];
1344 vlc_value_t val_list;
1347 char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
1348 mvnprintw( y++, 0, COLS, " Source : %s", psz_uri );
1352 var_Get( p_input, "state", &val );
1353 if( val.i_int == PLAYING_S )
1355 mvnprintw( y++, 0, COLS, " State : Playing %s", psz_state );
1357 else if( val.i_int == OPENING_S )
1359 mvnprintw( y++, 0, COLS, " State : Opening/Connecting %s", psz_state );
1361 else if( val.i_int == BUFFERING_S )
1363 mvnprintw( y++, 0, COLS, " State : Buffering %s", psz_state );
1365 else if( val.i_int == PAUSE_S )
1367 mvnprintw( y++, 0, COLS, " State : Paused %s", psz_state );
1370 if( val.i_int != INIT_S && val.i_int != END_S )
1372 audio_volume_t i_volume;
1375 var_Get( p_input, "time", &val );
1376 msecstotimestr( buf1, val.i_time / 1000 );
1378 var_Get( p_input, "length", &val );
1379 msecstotimestr( buf2, val.i_time / 1000 );
1381 mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1384 aout_VolumeGet( p_intf, &i_volume );
1385 mvnprintw( y++, 0, COLS, " Volume : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1388 if( !var_Get( p_input, "title", &val ) )
1390 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1391 if( val_list.p_list->i_count > 0 )
1393 mvnprintw( y++, 0, COLS, " Title : %d/%d", val.i_int, val_list.p_list->i_count );
1395 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1399 if( !var_Get( p_input, "chapter", &val ) )
1401 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1402 if( val_list.p_list->i_count > 0 )
1404 mvnprintw( y++, 0, COLS, " Chapter : %d/%d", val.i_int, val_list.p_list->i_count );
1406 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1416 mvnprintw( y++, 0, COLS, "Source: <no current item> %s", psz_state );
1417 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1418 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1419 DrawEmptyLine( p_sys->w, y++, 0, COLS );
1422 DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1423 DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1424 DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1427 p_sys->i_box_y = y + 1;
1428 p_sys->i_box_lines = LINES - y - 2;
1433 if( p_sys->i_box_type == BOX_HELP )
1437 DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1439 MainBoxWrite( p_intf, l++, 1, "[Display]" );
1440 MainBoxWrite( p_intf, l++, 1, " h,H Show/Hide help box" );
1441 MainBoxWrite( p_intf, l++, 1, " i Show/Hide info box" );
1442 MainBoxWrite( p_intf, l++, 1, " m Show/Hide metadata box" );
1443 MainBoxWrite( p_intf, l++, 1, " L Show/Hide messages box" );
1444 MainBoxWrite( p_intf, l++, 1, " P Show/Hide playlist box" );
1445 MainBoxWrite( p_intf, l++, 1, " B Show/Hide filebrowser" );
1446 MainBoxWrite( p_intf, l++, 1, "" );
1448 MainBoxWrite( p_intf, l++, 1, "[Global]" );
1449 MainBoxWrite( p_intf, l++, 1, " q, Q Quit" );
1450 MainBoxWrite( p_intf, l++, 1, " s Stop" );
1451 MainBoxWrite( p_intf, l++, 1, " <space> Pause/Play" );
1452 MainBoxWrite( p_intf, l++, 1, " f Toggle Fullscreen" );
1453 MainBoxWrite( p_intf, l++, 1, " n, p Next/Previous playlist item" );
1454 MainBoxWrite( p_intf, l++, 1, " [, ] Next/Previous title" );
1455 MainBoxWrite( p_intf, l++, 1, " <, > Next/Previous chapter" );
1456 MainBoxWrite( p_intf, l++, 1, " <right> Seek +1%%" );
1457 MainBoxWrite( p_intf, l++, 1, " <left> Seek -1%%" );
1458 MainBoxWrite( p_intf, l++, 1, " a Volume Up" );
1459 MainBoxWrite( p_intf, l++, 1, " z Volume Down" );
1460 MainBoxWrite( p_intf, l++, 1, "" );
1462 MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1463 MainBoxWrite( p_intf, l++, 1, " r Toggle Random playing" );
1464 MainBoxWrite( p_intf, l++, 1, " l Toggle Loop Playlist" );
1465 MainBoxWrite( p_intf, l++, 1, " R Toggle Repeat item" );
1466 MainBoxWrite( p_intf, l++, 1, " o Order Playlist by title" );
1467 MainBoxWrite( p_intf, l++, 1, " O Reverse order Playlist by title" );
1468 MainBoxWrite( p_intf, l++, 1, " / Look for an item" );
1469 MainBoxWrite( p_intf, l++, 1, " A Add an entry" );
1470 MainBoxWrite( p_intf, l++, 1, " D, <del> Delete an entry" );
1471 MainBoxWrite( p_intf, l++, 1, " <backspace> Delete an entry" );
1472 MainBoxWrite( p_intf, l++, 1, "" );
1474 MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1475 MainBoxWrite( p_intf, l++, 1, " <enter> Add the selected file to the playlist" );
1476 MainBoxWrite( p_intf, l++, 1, " <space> Add the selected directory to the playlist" );
1477 MainBoxWrite( p_intf, l++, 1, " . Show/Hide hidden files" );
1478 MainBoxWrite( p_intf, l++, 1, "" );
1480 MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1481 MainBoxWrite( p_intf, l++, 1, " <up>,<down> Navigate through the box line by line" );
1482 MainBoxWrite( p_intf, l++, 1, " <pgup>,<pgdown> Navigate through the box page by page" );
1483 MainBoxWrite( p_intf, l++, 1, "" );
1485 MainBoxWrite( p_intf, l++, 1, "[Player]" );
1486 MainBoxWrite( p_intf, l++, 1, " <up>,<down> Seek +/-5%%" );
1487 MainBoxWrite( p_intf, l++, 1, "" );
1489 MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1490 MainBoxWrite( p_intf, l++, 1, " Ctrl-l Refresh the screen" );
1492 p_sys->i_box_lines_total = l;
1493 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1495 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1498 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1500 y += l - p_sys->i_box_start;
1504 y += p_sys->i_box_lines;
1507 else if( p_sys->i_box_type == BOX_INFO )
1511 DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1516 vlc_mutex_lock( &input_GetItem(p_input)->lock );
1517 for( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
1519 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
1520 if( y >= y_end ) break;
1521 MainBoxWrite( p_intf, l++, 1, " [%s]", p_category->psz_name );
1522 for( j = 0; j < p_category->i_infos; j++ )
1524 info_t *p_info = p_category->pp_infos[j];
1525 if( y >= y_end ) break;
1526 MainBoxWrite( p_intf, l++, 1, " %s: %s", p_info->psz_name, p_info->psz_value );
1529 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1533 MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1535 p_sys->i_box_lines_total = l;
1536 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1538 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1541 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1543 y += l - p_sys->i_box_start;
1547 y += p_sys->i_box_lines;
1550 else if( p_sys->i_box_type == BOX_META )
1555 int i_len = strlen( VLC_META_INFO_CAT );
1556 char psz_title[i_len + 3];
1558 psz_title[1] = '\0';
1559 strcat( &psz_title[1], VLC_META_INFO_CAT );
1560 psz_title[i_len + 1] = ' ';
1561 psz_title[i_len + 2] = '\0';
1562 DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1567 input_item_t *p_item = input_GetItem( p_input );
1568 vlc_mutex_lock( &p_item->lock );
1569 for( i=0; i<VLC_META_TYPE_COUNT; i++ )
1571 if( y >= y_end ) break;
1572 char *psz_meta = p_item->p_meta->ppsz_meta[i];
1573 if( psz_meta && *psz_meta )
1575 const char *psz_meta_title;
1579 psz_meta_title = VLC_META_TITLE; break;
1581 psz_meta_title = VLC_META_ARTIST; break;
1583 psz_meta_title = VLC_META_GENRE ; break;
1585 psz_meta_title = VLC_META_COPYRIGHT; break;
1587 psz_meta_title = VLC_META_COLLECTION; break;
1589 psz_meta_title = VLC_META_SEQ_NUM; break;
1591 psz_meta_title = VLC_META_DESCRIPTION; break;
1593 psz_meta_title = VLC_META_RATING; break;
1595 psz_meta_title = VLC_META_DATE; break;
1597 psz_meta_title = VLC_META_SETTING; break;
1599 psz_meta_title = VLC_META_URL; break;
1601 psz_meta_title = VLC_META_LANGUAGE; break;
1603 psz_meta_title = VLC_META_NOW_PLAYING; break;
1605 psz_meta_title = VLC_META_PUBLISHER; break;
1607 psz_meta_title = VLC_META_ENCODED_BY; break;
1609 psz_meta_title = VLC_META_ART_URL; break;
1611 psz_meta_title = VLC_META_TRACKID; break;
1613 psz_meta_title = ""; break;
1615 MainBoxWrite( p_intf, l++, 1, " [%s]", psz_meta_title );
1616 MainBoxWrite( p_intf, l++, 1, " %s", psz_meta );
1619 vlc_mutex_unlock( &p_item->lock );
1623 MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1625 p_sys->i_box_lines_total = l;
1626 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1628 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1631 if( l - p_sys->i_box_start < p_sys->i_box_lines )
1633 y += l - p_sys->i_box_start;
1637 y += p_sys->i_box_lines;
1640 else if( p_sys->i_box_type == BOX_LOG )
1646 DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1648 i_start = p_intf->p_sys->p_sub->i_start;
1650 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1651 i_stop = *p_intf->p_sys->p_sub->pi_stop;
1652 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1656 static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1657 if( i_line >= h - 2 )
1663 if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1664 if( i_stop == i_start )
1668 mvnprintw( y + h-2-i_line, 1, COLS - 2, " [%s] %s",
1669 ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1670 p_sys->p_sub->p_msg[i_stop].psz_msg );
1673 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1674 p_intf->p_sys->p_sub->i_start = i_stop;
1675 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1678 else if( p_sys->i_box_type == BOX_BROWSE )
1680 /* Filebrowser box */
1681 int i_start, i_stop;
1683 DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1685 if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1686 if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1688 if( p_sys->i_box_bidx < (h - 2)/2 )
1693 else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1695 i_start = p_sys->i_box_bidx - (h - 2)/2;
1696 i_stop = i_start + h - 2;
1700 i_stop = p_sys->i_dir_entries;
1701 i_start = p_sys->i_dir_entries - (h - 2);
1707 if( i_stop > p_sys->i_dir_entries )
1709 i_stop = p_sys->i_dir_entries;
1712 for( i_item = i_start; i_item < i_stop; i_item++ )
1714 vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1716 if( y >= y_end ) break;
1719 attrset( A_REVERSE );
1721 mvnprintw( y++, 1, COLS - 2, " %c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? ' ' : '+',
1722 p_sys->pp_dir_entries[i_item]->psz_path );
1725 attroff( A_REVERSE );
1730 else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1731 p_sys->i_box_type == BOX_SEARCH ||
1732 p_sys->i_box_type == BOX_OPEN ) && p_sys->p_playlist )
1735 int i_start, i_stop, i_max = p_sys->i_plist_entries;
1739 switch( p_sys->i_current_view )
1742 psz_title = strdup( " Playlist (All, one level) " );
1745 psz_title = strdup( " Playlist (By category) " );
1748 psz_title = strdup( " Playlist (Manually added) " );
1751 DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1753 if( p_sys->b_need_update || p_sys->pp_plist == NULL )
1755 PlaylistRebuild( p_intf );
1757 if( p_sys->b_box_plidx_follow )
1759 FindIndex( p_intf );
1762 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1763 if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1764 if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
1766 if( p_sys->i_box_plidx < (h - 2)/2 )
1771 else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1773 i_start = p_sys->i_box_plidx - (h - 2)/2;
1774 i_stop = i_start + h - 2;
1779 i_start = i_max - (h - 2);
1785 if( i_stop > i_max )
1790 for( i_item = i_start; i_item < i_stop; i_item++ )
1792 vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1793 int c = ( PlaylistIsPlaying( p_intf,
1794 p_sys->pp_plist[i_item]->p_item ) ) ? '>' : ' ';
1796 if( y >= y_end ) break;
1799 attrset( A_REVERSE );
1801 mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1802 p_sys->pp_plist[i_item]->psz_display );
1805 attroff( A_REVERSE );
1814 if( p_sys->i_box_type == BOX_SEARCH )
1816 DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1817 if( p_sys->psz_search_chain )
1819 if( strlen( p_sys->psz_search_chain ) == 0 &&
1820 p_sys->psz_old_search != NULL )
1822 /* Searching next entry */
1823 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1827 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1831 if( p_sys->i_box_type == BOX_OPEN )
1833 if( p_sys->psz_open_chain )
1835 DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1836 mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1842 DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1847 *t_last_refresh = time( 0 );
1850 static playlist_item_t *PlaylistGetRoot( intf_thread_t *p_intf )
1852 intf_sys_t *p_sys = p_intf->p_sys;
1853 playlist_t *p_playlist = p_sys->p_playlist;
1855 if( p_playlist == NULL )
1860 switch( p_sys->i_current_view )
1863 return p_playlist->p_root_category;
1865 return p_playlist->p_root_onelevel;
1869 static void PlaylistRebuild( intf_thread_t *p_intf )
1871 intf_sys_t *p_sys = p_intf->p_sys;
1872 playlist_t *p_playlist = p_sys->p_playlist;
1874 if( p_playlist == NULL )
1879 vlc_mutex_lock( &p_playlist->object_lock );
1881 /* First clear the old one */
1882 PlaylistDestroy( p_intf );
1884 /* Build the new one */
1885 PlaylistAddNode( p_intf, PlaylistGetRoot( p_intf ), 0, "" );
1887 p_sys->b_need_update = VLC_FALSE;
1889 vlc_mutex_unlock( &p_playlist->object_lock );
1892 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1893 int i, const char *c )
1895 intf_sys_t *p_sys = p_intf->p_sys;
1896 playlist_item_t *p_child;
1900 psz_tmp = (char *)malloc( strlen( c ) + 4 );
1901 if( psz_tmp == NULL ) return;
1902 for( k = 0; k < p_node->i_children; k++ )
1904 struct pl_item_t *p_pl_item;
1908 p_child = p_node->pp_children[k];
1909 i_size = strlen( c ) + strlen( p_child->p_input->psz_name ) + 4;
1910 buff = (char *)malloc( sizeof( char ) * i_size );
1911 p_pl_item = (struct pl_item_t *)malloc( sizeof( struct pl_item_t ) );
1912 if( p_pl_item == NULL || buff == NULL ) return;
1916 sprintf( buff, "%s%c-%s", c, k == p_node->i_children - 1 ?
1917 '`' : '|', p_child->p_input->psz_name );
1921 sprintf( buff, " %s", p_child->p_input->psz_name );
1923 p_pl_item->psz_display = strdup( buff );
1924 p_pl_item->p_item = p_child;
1925 INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1926 p_sys->i_plist_entries, p_pl_item );
1930 if( p_child->i_children > 0 )
1932 sprintf( psz_tmp, "%s%c ", c,
1933 k == p_node->i_children - 1 ? ' ' : '|' );
1934 PlaylistAddNode( p_intf, p_child, i,
1935 strlen( c ) ? psz_tmp : " " );
1941 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1942 vlc_value_t oval, vlc_value_t nval, void *param )
1944 intf_thread_t *p_intf = (intf_thread_t *)param;
1945 p_intf->p_sys->b_need_update = VLC_TRUE;
1950 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *p_intf,
1951 playlist_item_t *p_item )
1953 playlist_item_t *p_played_item = p_intf->p_sys->p_playlist->status.p_item;
1954 return( p_item != NULL && p_played_item != NULL &&
1955 p_item->p_input != NULL && p_played_item->p_input != NULL &&
1956 p_item->p_input->i_id == p_played_item->p_input->i_id );
1959 static void FindIndex( intf_thread_t *p_intf )
1961 intf_sys_t *p_sys = p_intf->p_sys;
1964 if( p_sys->i_box_plidx < p_sys->i_plist_entries &&
1965 p_sys->i_box_plidx >= 0 &&
1966 PlaylistIsPlaying( p_intf,
1967 p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
1972 for( i = 0; i < p_sys->i_plist_entries; i++ )
1974 if( PlaylistIsPlaying( p_intf, p_sys->pp_plist[i]->p_item ) )
1976 p_sys->i_box_plidx = i;
1982 static void PlaylistDestroy( intf_thread_t *p_intf )
1984 intf_sys_t *p_sys = p_intf->p_sys;
1987 for( i = 0; i < p_sys->i_plist_entries; i++ )
1989 struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
1990 free( p_pl_item->psz_display );
1991 REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
1994 p_sys->pp_plist = NULL;
1995 p_sys->i_plist_entries = 0;
1998 static void Eject( intf_thread_t *p_intf )
2000 char *psz_device = NULL, *psz_parser, *psz_name;
2003 * Get the active input
2004 * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
2005 * If it's neither of these, then return
2008 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
2011 if( p_playlist == NULL )
2016 vlc_mutex_lock( &p_playlist->object_lock );
2018 if( p_playlist->status.p_item == NULL )
2020 vlc_mutex_unlock( &p_playlist->object_lock );
2021 vlc_object_release( p_playlist );
2025 psz_name = p_playlist->status.p_item->p_input->psz_name;
2029 if( !strncmp(psz_name, "dvd://", 4) )
2031 switch( psz_name[strlen("dvd://")] )
2035 psz_device = config_GetPsz( p_intf, "dvd" );
2038 /* Omit the first MRL-selector characters */
2039 psz_device = strdup( psz_name + strlen("dvd://" ) );
2043 else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
2045 switch( psz_name[strlen(VCD_MRL)] )
2049 psz_device = config_GetPsz( p_intf, VCD_MRL );
2052 /* Omit the beginning MRL-selector characters */
2053 psz_device = strdup( psz_name + strlen(VCD_MRL) );
2057 else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
2059 switch( psz_name[strlen(CDDA_MRL)] )
2063 psz_device = config_GetPsz( p_intf, "cd-audio" );
2066 /* Omit the beginning MRL-selector characters */
2067 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
2073 psz_device = strdup( psz_name );
2077 vlc_mutex_unlock( &p_playlist->object_lock );
2078 vlc_object_release( p_playlist );
2080 if( psz_device == NULL )
2085 /* Remove what we have after @ */
2086 psz_parser = psz_device;
2087 for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
2089 if( *psz_parser == '@' )
2096 /* If there's a stream playing, we aren't allowed to eject ! */
2097 if( p_intf->p_sys->p_input == NULL )
2099 msg_Dbg( p_intf, "ejecting %s", psz_device );
2101 intf_Eject( p_intf, psz_device );
2108 static int comp_dir_entries( const void *pp_dir_entry1,
2109 const void *pp_dir_entry2 )
2111 struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
2112 struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
2113 if ( p_dir_entry1->b_file == p_dir_entry2->b_file ) {
2114 return strcasecmp( p_dir_entry1->psz_path, p_dir_entry2->psz_path );
2118 return ( p_dir_entry1->b_file ? 1 : -1 );
2122 static void ReadDir( intf_thread_t *p_intf )
2124 intf_sys_t *p_sys = p_intf->p_sys;
2128 if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
2133 p_current_dir = utf8_opendir( p_sys->psz_current_dir );
2135 if( p_current_dir == NULL )
2137 /* something went bad, get out of here ! */
2138 msg_Warn( p_intf, "cannot open directory `%s' (%m)",
2139 p_sys->psz_current_dir );
2143 /* Clean the old shit */
2144 for( i = 0; i < p_sys->i_dir_entries; i++ )
2146 struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
2147 free( p_dir_entry->psz_path );
2148 REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
2149 free( p_dir_entry );
2151 p_sys->pp_dir_entries = NULL;
2152 p_sys->i_dir_entries = 0;
2154 /* while we still have entries in the directory */
2155 while( ( psz_entry = utf8_readdir( p_current_dir ) ) != NULL )
2157 #if defined( S_ISDIR )
2158 struct stat stat_data;
2160 struct dir_entry_t *p_dir_entry;
2161 int i_size_entry = strlen( p_sys->psz_current_dir ) +
2162 strlen( psz_entry ) + 2;
2165 if( p_sys->b_show_hidden_files == VLC_FALSE &&
2166 ( strlen( psz_entry ) && psz_entry[0] == '.' ) &&
2167 strcmp( psz_entry, ".." ) )
2173 psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
2174 sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, psz_entry );
2176 if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
2183 #if defined( S_ISDIR )
2184 if( !utf8_stat( psz_uri, &stat_data )
2185 && S_ISDIR(stat_data.st_mode) )
2186 /*#elif defined( DT_DIR )
2187 if( p_dir_content->d_type & DT_DIR )*/
2192 p_dir_entry->psz_path = strdup( psz_entry );
2193 p_dir_entry->b_file = VLC_FALSE;
2194 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2195 p_sys->i_dir_entries, p_dir_entry );
2199 p_dir_entry->psz_path = strdup( psz_entry );
2200 p_dir_entry->b_file = VLC_TRUE;
2201 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2202 p_sys->i_dir_entries, p_dir_entry );
2210 qsort( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2211 sizeof(struct dir_entry_t*), &comp_dir_entries );
2213 closedir( p_current_dir );
2218 msg_Dbg( p_intf, "no current dir set" );
2223 static void PlayPause( intf_thread_t *p_intf )
2225 input_thread_t *p_input = p_intf->p_sys->p_input;
2230 var_Get( p_input, "state", &val );
2231 if( val.i_int != PAUSE_S )
2233 val.i_int = PAUSE_S;
2237 val.i_int = PLAYING_S;
2239 var_Set( p_input, "state", val );
2241 else if( p_intf->p_sys->p_playlist )
2243 playlist_Play( p_intf->p_sys->p_playlist );
2247 /****************************************************************************
2249 ****************************************************************************/
2250 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title )
2255 if( w > 3 && h > 2 )
2257 if( title == NULL ) title = "";
2258 i_len = strlen( title );
2260 if( i_len > w - 2 ) i_len = w - 2;
2262 mvwaddch( win, y, x, ACS_ULCORNER );
2263 mvwhline( win, y, x+1, ACS_HLINE, ( w-i_len-2)/2 );
2264 mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
2265 mvwhline( win, y, x+(w-i_len)/2+i_len, ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
2266 mvwaddch( win, y, x+w-1,ACS_URCORNER );
2268 for( i = 0; i < h-2; i++ )
2270 mvwaddch( win, y+i+1, x, ACS_VLINE );
2271 mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
2274 mvwaddch( win, y+h-1, x, ACS_LLCORNER );
2275 mvwhline( win, y+h-1, x+1, ACS_HLINE, w - 2 );
2276 mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
2280 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
2284 mvhline( y, x, ' ', w );
2288 static void DrawLine( WINDOW *win, int y, int x, int w )
2292 attrset( A_REVERSE );
2293 mvhline( y, x, ' ', w );
2294 attroff( A_REVERSE );