]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
ncurses: Use ncursesw to correctly display wide characters on UTF-8 locale
[vlc] / modules / gui / ncurses.c
1 /*****************************************************************************
2  * ncurses.c : NCurses plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
5  * $Id$
6  *
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  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <time.h>
34
35 #include <ncursesw/curses.h>
36
37 #include <vlc_interface.h>
38 #include <vlc_vout.h>
39 #include <vlc_aout.h>
40 #include <vlc_charset.h>
41 #include <vlc_input.h>
42 #include <vlc_playlist.h>
43
44 #ifdef HAVE_SYS_STAT_H
45 #   include <sys/stat.h>
46 #endif
47 #if (!defined( WIN32 ) || defined(__MINGW32__))
48 /* Mingw has its own version of dirent */
49 #   include <dirent.h>
50 #endif
51
52 #ifdef HAVE_CDDAX
53 #define CDDA_MRL "cddax://"
54 #else
55 #define CDDA_MRL "cdda://"
56 #endif
57
58 #ifdef HAVE_VCDX
59 #define VCD_MRL "vcdx://"
60 #else
61 #define VCD_MRL "vcdx://"
62 #endif
63
64 #define SEARCH_CHAIN_SIZE 20
65 #define OPEN_CHAIN_SIZE 50
66
67 /*****************************************************************************
68  * Local prototypes.
69  *****************************************************************************/
70 static int  Open           ( vlc_object_t * );
71 static void Close          ( vlc_object_t * );
72
73 static void Run            ( intf_thread_t * );
74 static void PlayPause      ( intf_thread_t * );
75 static void Eject          ( intf_thread_t * );
76
77 static int  HandleKey      ( intf_thread_t *, int );
78 static void Redraw         ( intf_thread_t *, time_t * );
79
80 static playlist_item_t *PlaylistGetRoot( intf_thread_t * );
81 static void PlaylistRebuild( intf_thread_t * );
82 static void PlaylistAddNode( intf_thread_t *, playlist_item_t *, int, const char *);
83 static void PlaylistDestroy( intf_thread_t * );
84 static int  PlaylistChanged( vlc_object_t *, const char *, vlc_value_t,
85                              vlc_value_t, void * );
86 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *,
87                                             playlist_item_t * );
88 static void FindIndex      ( intf_thread_t * );
89 static void SearchPlaylist ( intf_thread_t *, char * );
90 static int  SubSearchPlaylist( intf_thread_t *, char *, int, int );
91
92 static void ManageSlider   ( intf_thread_t * );
93 static void ReadDir        ( intf_thread_t * );
94
95 /*****************************************************************************
96  * Module descriptor
97  *****************************************************************************/
98
99 #define BROWSE_TEXT N_("Filebrowser starting point")
100 #define BROWSE_LONGTEXT N_( \
101     "This option allows you to specify the directory the ncurses filebrowser " \
102     "will show you initially.")
103
104 vlc_module_begin();
105     set_shortname( "Ncurses" );
106     set_description( _("Ncurses interface") );
107     set_capability( "interface", 10 );
108     set_category( CAT_INTERFACE );
109     set_subcategory( SUBCAT_INTERFACE_MAIN );
110     set_callbacks( Open, Close );
111     add_shortcut( "curses" );
112     add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );
113 vlc_module_end();
114
115 /*****************************************************************************
116  * intf_sys_t: description and status of ncurses interface
117  *****************************************************************************/
118 enum
119 {
120     BOX_NONE,
121     BOX_HELP,
122     BOX_INFO,
123     BOX_LOG,
124     BOX_PLAYLIST,
125     BOX_SEARCH,
126     BOX_OPEN,
127     BOX_BROWSE
128 };
129 enum
130 {
131     VIEW_CATEGORY,
132     VIEW_ONELEVEL
133 };
134 struct dir_entry_t
135 {
136     vlc_bool_t  b_file;
137     char        *psz_path;
138 };
139 struct pl_item_t
140 {
141     playlist_item_t *p_item;
142     char            *psz_display;
143 };
144 struct intf_sys_t
145 {
146     playlist_t     *p_playlist;
147     input_thread_t *p_input;
148
149     float           f_slider;
150     float           f_slider_old;
151
152     WINDOW          *w;
153
154     int             i_box_type;
155     int             i_box_y;
156     int             i_box_lines;
157     int             i_box_lines_total;
158     int             i_box_start;
159
160     int             i_box_plidx;    /* Playlist index */
161     int             b_box_plidx_follow;
162     playlist_item_t *p_plnode;      /* Playlist node */
163     int             i_box_bidx;     /* browser index */
164
165     int             b_box_cleared;
166
167     msg_subscription_t* p_sub;                  /* message bank subscription */
168
169     char            *psz_search_chain;          /* for playlist searching    */
170     char            *psz_old_search;            /* for searching next        */
171     int             i_before_search;
172
173     char            *psz_open_chain;
174     char             psz_partial_keys[7];
175
176     char            *psz_current_dir;
177     int             i_dir_entries;
178     struct dir_entry_t  **pp_dir_entries;
179     vlc_bool_t      b_show_hidden_files;
180
181     int             i_current_view;             /* playlist view             */
182     struct pl_item_t    **pp_plist;
183     int             i_plist_entries;
184     vlc_bool_t      b_need_update;              /* for playlist view         */
185
186     int             i_verbose;                  /* stores verbosity level    */
187 };
188
189 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title );
190 static void DrawLine( WINDOW *win, int y, int x, int w );
191 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
192
193 /*****************************************************************************
194  * Open: initialize and create window
195  *****************************************************************************/
196 static int Open( vlc_object_t *p_this )
197 {
198     intf_thread_t *p_intf = (intf_thread_t *)p_this;
199     intf_sys_t    *p_sys;
200     vlc_value_t    val;
201
202     /* Allocate instance and initialize some members */
203     p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
204     p_sys->p_playlist = NULL;
205     p_sys->p_input = NULL;
206     p_sys->f_slider = 0.0;
207     p_sys->f_slider_old = 0.0;
208     p_sys->i_box_type = BOX_PLAYLIST;
209     p_sys->i_box_lines = 0;
210     p_sys->i_box_start= 0;
211     p_sys->i_box_lines_total = 0;
212     p_sys->b_box_plidx_follow = VLC_TRUE;
213     p_sys->b_box_cleared = VLC_FALSE;
214     p_sys->i_box_plidx = 0;
215     p_sys->p_plnode = NULL;
216     p_sys->i_box_bidx = 0;
217     p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
218     memset( p_sys->psz_partial_keys, 0, sizeof( p_sys->psz_partial_keys ) );
219
220     /* Initialize the curses library */
221     p_sys->w = initscr();
222     keypad( p_sys->w, TRUE );
223     /* Don't do NL -> CR/NL */
224     nonl();
225     /* Take input chars one at a time */
226     cbreak();
227     /* Don't echo */
228     noecho();
229
230     curs_set(0);
231     timeout(0);
232
233     clear();
234
235     /* exported function */
236     p_intf->pf_run = Run;
237
238     /* Remember verbosity level */
239     var_Get( p_intf->p_libvlc, "verbose", &val );
240     p_sys->i_verbose = val.i_int;
241     /* Set quiet mode */
242     val.i_int = -1;
243     var_Set( p_intf->p_libvlc, "verbose", val );
244
245     /* Set defaul playlist view */
246     p_sys->i_current_view = VIEW_CATEGORY;
247     p_sys->pp_plist = NULL;
248     p_sys->i_plist_entries = 0;
249     p_sys->b_need_update = VLC_FALSE;
250
251     /* Initialize search chain */
252     p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
253     p_sys->psz_old_search = NULL;
254     p_sys->i_before_search = 0;
255
256     /* Initialize open chain */
257     p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
258
259     /* Initialize browser options */
260     var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
261     var_Get( p_intf, "browse-dir", &val);
262
263     if( val.psz_string && *val.psz_string )
264     {
265         p_sys->psz_current_dir = strdup( val.psz_string );
266         free( val.psz_string );
267     }
268     else
269     {
270         p_sys->psz_current_dir = strdup( p_intf->p_libvlc->psz_homedir );
271     }
272
273     p_sys->i_dir_entries = 0;
274     p_sys->pp_dir_entries = NULL;
275     p_sys->b_show_hidden_files = VLC_FALSE;
276     ReadDir( p_intf );
277
278     return VLC_SUCCESS;
279 }
280
281 /*****************************************************************************
282  * Close: destroy interface window
283  *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
285 {
286     intf_thread_t *p_intf = (intf_thread_t *)p_this;
287     intf_sys_t    *p_sys = p_intf->p_sys;
288     int i;
289
290     var_DelCallback( p_sys->p_playlist, "intf-change", PlaylistChanged,
291                      p_intf );
292     var_DelCallback( p_sys->p_playlist, "item-append", PlaylistChanged,
293                      p_intf );
294
295     PlaylistDestroy( p_intf );
296
297     for( i = 0; i < p_sys->i_dir_entries; i++ )
298     {
299         struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
300         if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
301         REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
302         if( p_dir_entry ) free( p_dir_entry );
303     }
304     p_sys->pp_dir_entries = NULL;
305
306     if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
307     if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
308     if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
309     if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
310
311     if( p_sys->p_input )
312     {
313         vlc_object_release( p_sys->p_input );
314     }
315     if( p_sys->p_playlist )
316     {
317         vlc_object_release( p_sys->p_playlist );
318     }
319
320     /* Close the ncurses interface */
321     endwin();
322
323     msg_Unsubscribe( p_intf, p_sys->p_sub );
324
325     /* Restores initial verbose setting */
326     vlc_value_t val;
327     val.i_int = p_sys->i_verbose;
328     var_Set( p_intf->p_libvlc, "verbose", val );
329
330     /* Destroy structure */
331     free( p_sys );
332 }
333
334 /*****************************************************************************
335  * Run: ncurses thread
336  *****************************************************************************/
337 static void Run( intf_thread_t *p_intf )
338 {
339     intf_sys_t    *p_sys = p_intf->p_sys;
340
341     int i_key;
342     time_t t_last_refresh;
343
344     /*
345      * force drawing the interface for the first time
346      */
347     t_last_refresh = ( time( 0 ) - 1);
348
349     while( !intf_ShouldDie( p_intf ) )
350     {
351         msleep( INTF_IDLE_SLEEP );
352
353         /* Update the input */
354         if( p_sys->p_playlist == NULL )
355         {
356             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
357                                                  FIND_ANYWHERE );
358             if( p_sys->p_playlist )
359             {
360                 var_AddCallback( p_sys->p_playlist, "intf-change",
361                                  PlaylistChanged, p_intf );
362                 var_AddCallback( p_sys->p_playlist, "item-append",
363                                  PlaylistChanged, p_intf );
364             }
365         }
366         if( p_sys->p_playlist )
367         {
368             vlc_mutex_lock( &p_sys->p_playlist->object_lock );
369             if( p_sys->p_input == NULL )
370             {
371                 p_sys->p_input = p_sys->p_playlist->p_input;
372                 if( p_sys->p_input )
373                 {
374                     if( !p_sys->p_input->b_dead )
375                     {
376                         vlc_object_yield( p_sys->p_input );
377                     }
378                 }
379             }
380             else if( p_sys->p_input->b_dead )
381             {
382                 vlc_object_release( p_sys->p_input );
383                 p_sys->p_input = NULL;
384                 p_sys->f_slider = p_sys->f_slider_old = 0.0;
385                 p_sys->b_box_cleared = VLC_FALSE;
386             }
387             vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
388         }
389
390         if( p_sys->b_box_plidx_follow && p_sys->p_playlist->status.p_item )
391         {
392             FindIndex( p_intf );
393         }
394
395         while( ( i_key = getch() ) != -1 )
396         {
397             /*
398              * HandleKey returns 1 if the screen needs to be redrawn
399              */
400             if( HandleKey( p_intf, i_key ) )
401             {
402                 Redraw( p_intf, &t_last_refresh );
403             }
404         }
405         /* Hack */
406         if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
407         {
408             clear();
409             Redraw( p_intf, &t_last_refresh );
410             p_sys->b_box_cleared = VLC_TRUE;
411         }
412
413         /*
414          * redraw the screen every second
415          */
416         if( (time(0) - t_last_refresh) >= 1 )
417         {
418             ManageSlider( p_intf );
419             Redraw( p_intf, &t_last_refresh );
420         }
421     }
422 }
423
424 /* following functions are local */
425
426 static char *KeyToUTF8( int i_key, char *psz_part )
427 {
428     char *psz_utf8, *psz;
429     int len = strlen( psz_part );
430     if( len == 6 )
431     {
432         /* overflow error - should not happen */
433         memset( psz_part, 0, 6 );
434         return NULL;
435     }
436
437     psz_part[len] = (char)i_key;
438
439     psz_utf8 = FromLocaleDup( psz_part );
440
441     /* Ugly check for incomplete bytes sequences
442      * (in case of non-UTF8 multibyte local encoding) */
443     for( psz = psz_utf8; *psz; psz++ )
444         if( ( *psz == '?' ) && ( *psz_utf8 != '?' ) )
445         {
446             /* incomplete bytes sequence detected
447              * (VLC core inserted dummy question marks) */
448             free( psz_utf8 );
449             return NULL;
450         }
451
452     /* Check for incomplete UTF8 bytes sequence */
453     if( EnsureUTF8( psz_utf8 ) == NULL )
454     {
455         free( psz_utf8 );
456         return NULL;
457     }
458
459     memset( psz_part, 0, 6 );
460     return psz_utf8;
461 }
462
463 static inline int RemoveLastUTF8Entity( char *psz, int len )
464 {
465     while( len && ( (psz[--len] & 0xc0) == 0x80 ) );
466                        /* UTF8 continuation byte */
467
468     psz[len] = '\0';
469     return len;
470 }
471
472 static int HandleKey( intf_thread_t *p_intf, int i_key )
473 {
474     intf_sys_t *p_sys = p_intf->p_sys;
475     vlc_value_t val;
476
477     if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
478     {
479         int b_ret = VLC_TRUE;
480         vlc_bool_t b_box_plidx_follow = VLC_FALSE;
481
482         switch( i_key )
483         {
484             vlc_value_t val;
485             /* Playlist Settings */
486             case 'r':
487                 var_Get( p_sys->p_playlist, "random", &val );
488                 val.b_bool = !val.b_bool;
489                 var_Set( p_sys->p_playlist, "random", val );
490                 return 1;
491             case 'l':
492                 var_Get( p_sys->p_playlist, "loop", &val );
493                 val.b_bool = !val.b_bool;
494                 var_Set( p_sys->p_playlist, "loop", val );
495                 return 1;
496             case 'R':
497                 var_Get( p_sys->p_playlist, "repeat", &val );
498                 val.b_bool = !val.b_bool;
499                 var_Set( p_sys->p_playlist, "repeat", val );
500                 return 1;
501
502             /* Playlist sort */
503             case 'o':
504                 playlist_RecursiveNodeSort( p_sys->p_playlist,
505                                             PlaylistGetRoot( p_intf ),
506                                             SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
507                 p_sys->b_need_update = VLC_TRUE;
508                 return 1;
509             case 'O':
510                 playlist_RecursiveNodeSort( p_sys->p_playlist,
511                                             PlaylistGetRoot( p_intf ),
512                                             SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
513                 p_sys->b_need_update = VLC_TRUE;
514                 return 1;
515
516             /* Playlist view */
517             case 'v':
518                 switch( p_sys->i_current_view )
519                 {
520                     case VIEW_CATEGORY:
521                         p_sys->i_current_view = VIEW_ONELEVEL;
522                         break;
523                     default:
524                         p_sys->i_current_view = VIEW_CATEGORY;
525                 }
526                 //p_sys->b_need_update = VLC_TRUE;
527                 PlaylistRebuild( p_intf );
528                 return 1;
529
530             /* Playlist navigation */
531             case KEY_HOME:
532                 p_sys->i_box_plidx = 0;
533                 break;
534             case KEY_END:
535                 p_sys->i_box_plidx = p_sys->p_playlist->items.i_size - 1;
536                 break;
537             case KEY_UP:
538                 p_sys->i_box_plidx--;
539                 break;
540             case KEY_DOWN:
541                 p_sys->i_box_plidx++;
542                 break;
543             case KEY_PPAGE:
544                 p_sys->i_box_plidx -= p_sys->i_box_lines;
545                 break;
546             case KEY_NPAGE:
547                 p_sys->i_box_plidx += p_sys->i_box_lines;
548                 break;
549             case 'D':
550             case KEY_BACKSPACE:
551             case KEY_DC:
552             {
553                 playlist_t *p_playlist = p_sys->p_playlist;
554                 playlist_item_t *p_item;
555
556                 vlc_mutex_lock( &p_playlist->object_lock );
557                 p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
558                 if( p_item->i_children == -1 )
559                 {
560                     playlist_DeleteFromInput( p_playlist,
561                                               p_item->p_input->i_id, VLC_TRUE );
562                 }
563                 else
564                 {
565                     playlist_NodeDelete( p_playlist, p_item,
566                                          VLC_TRUE , VLC_FALSE );
567                 }
568                 vlc_mutex_unlock( &p_playlist->object_lock );
569                 p_sys->b_need_update = VLC_TRUE;
570
571                 break;
572             }
573
574             case KEY_ENTER:
575             case 0x0d:
576                 if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
577                         == -1 )
578                 {
579                     playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
580                                       VLC_TRUE, NULL,
581                         p_sys->pp_plist[p_sys->i_box_plidx]->p_item );
582                 }
583                 else
584                 {
585                     playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
586                         VLC_TRUE,
587                         p_sys->pp_plist[p_sys->i_box_plidx]->p_item,
588                         NULL );
589                 }
590                 b_box_plidx_follow = VLC_TRUE;
591                 break;
592             default:
593                 b_ret = VLC_FALSE;
594                 break;
595         }
596
597         if( b_ret )
598         {
599             int i_max = p_sys->i_plist_entries;
600             if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
601             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
602             if( PlaylistIsPlaying( p_intf,
603                     p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
604                 b_box_plidx_follow = VLC_TRUE;
605             p_sys->b_box_plidx_follow = b_box_plidx_follow;
606             return 1;
607         }
608     }
609     if( p_sys->i_box_type == BOX_BROWSE )
610     {
611         vlc_bool_t b_ret = VLC_TRUE;
612         /* Browser navigation */
613         switch( i_key )
614         {
615             case KEY_HOME:
616                 p_sys->i_box_bidx = 0;
617                 break;
618             case KEY_END:
619                 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
620                 break;
621             case KEY_UP:
622                 p_sys->i_box_bidx--;
623                 break;
624             case KEY_DOWN:
625                 p_sys->i_box_bidx++;
626                 break;
627             case KEY_PPAGE:
628                 p_sys->i_box_bidx -= p_sys->i_box_lines;
629                 break;
630             case KEY_NPAGE:
631                 p_sys->i_box_bidx += p_sys->i_box_lines;
632                 break;
633             case '.': /* Toggle show hidden files */
634                 p_sys->b_show_hidden_files = ( p_sys->b_show_hidden_files ==
635                     VLC_TRUE ? VLC_FALSE : VLC_TRUE );
636                 ReadDir( p_intf );
637                 break;
638
639             case KEY_ENTER:
640             case 0x0d:
641             case ' ':
642                 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
643                 {
644                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
645                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
646                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
647
648                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
649                     /* XXX
650                     playlist_Add( p_sys->p_playlist, psz_uri,
651                                   psz_uri,
652                                   PLAYLIST_APPEND, PLAYLIST_END );
653                     */
654                     p_sys->i_box_type = BOX_PLAYLIST;
655                     free( psz_uri );
656                 }
657                 else
658                 {
659                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
660                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
661                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
662
663                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
664
665                     p_sys->psz_current_dir = strdup( psz_uri );
666                     ReadDir( p_intf );
667                     free( psz_uri );
668                 }
669                 break;
670             default:
671                 b_ret = VLC_FALSE;
672                 break;
673         }
674         if( b_ret )
675         {
676             if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
677             if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
678             return 1;
679         }
680     }
681     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
682     {
683         switch( i_key )
684         {
685             case KEY_HOME:
686                 p_sys->i_box_start = 0;
687                 return 1;
688             case KEY_END:
689                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
690                 return 1;
691             case KEY_UP:
692                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
693                 return 1;
694             case KEY_DOWN:
695                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
696                 {
697                     p_sys->i_box_start++;
698                 }
699                 return 1;
700             case KEY_PPAGE:
701                 p_sys->i_box_start -= p_sys->i_box_lines;
702                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
703                 return 1;
704             case KEY_NPAGE:
705                 p_sys->i_box_start += p_sys->i_box_lines;
706                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
707                 {
708                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
709                 }
710                 return 1;
711             default:
712                 break;
713         }
714     }
715     else if( p_sys->i_box_type == BOX_NONE )
716     {
717         switch( i_key )
718         {
719             case KEY_HOME:
720                 p_sys->f_slider = 0;
721                 ManageSlider( p_intf );
722                 return 1;
723             case KEY_END:
724                 p_sys->f_slider = 99.9;
725                 ManageSlider( p_intf );
726                 return 1;
727             case KEY_UP:
728                 p_sys->f_slider += 5.0;
729                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
730                 ManageSlider( p_intf );
731                 return 1;
732             case KEY_DOWN:
733                 p_sys->f_slider -= 5.0;
734                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
735                 ManageSlider( p_intf );
736                 return 1;
737
738             default:
739                 break;
740         }
741     }
742     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
743     {
744         int i_chain_len;
745         i_chain_len = strlen( p_sys->psz_search_chain );
746         switch( i_key )
747         {
748             case 0x0c:      /* ^l */
749                 clear();
750                 return 1;
751             case KEY_ENTER:
752             case 0x0d:
753                 if( i_chain_len > 0 )
754                 {
755                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
756                 }
757                 else if( p_sys->psz_old_search )
758                 {
759                     SearchPlaylist( p_intf, p_sys->psz_old_search );
760                 }
761                 p_sys->i_box_type = BOX_PLAYLIST;
762                 return 1;
763             case 0x1b:      /* Esc. */
764                 p_sys->i_box_plidx = p_sys->i_before_search;
765                 p_sys->i_box_type = BOX_PLAYLIST;
766                 return 1;
767             case KEY_BACKSPACE:
768                 RemoveLastUTF8Entity( p_sys->psz_search_chain, i_chain_len );
769                 break;
770             default:
771             {
772                 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
773
774                 if( psz_utf8 != NULL )
775                 {
776                     if( i_chain_len + strlen( psz_utf8 ) < SEARCH_CHAIN_SIZE )
777                     {
778                         strcpy( p_sys->psz_search_chain + i_chain_len,
779                                 psz_utf8 );
780                     }
781                     free( psz_utf8 );
782                 }
783                 break;
784             }
785         }
786         if( p_sys->psz_old_search )
787         {
788             free( p_sys->psz_old_search );
789             p_sys->psz_old_search = NULL;
790         }
791         SearchPlaylist( p_intf, p_sys->psz_search_chain );
792         return 1;
793     }
794     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
795     {
796         int i_chain_len = strlen( p_sys->psz_open_chain );
797         playlist_t *p_playlist = p_sys->p_playlist;
798
799         switch( i_key )
800         {
801             case 0x0c:      /* ^l */
802                 clear();
803                 return 1;
804             case KEY_ENTER:
805             case 0x0d:
806                 if( p_playlist && i_chain_len > 0 )
807                 {
808                     /*
809                     playlist_Add( p_playlist, p_sys->psz_open_chain,
810                                   p_sys->psz_open_chain,
811                                   PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
812                     */
813                     p_sys->b_box_plidx_follow = VLC_TRUE;
814                 }
815                 p_sys->i_box_type = BOX_PLAYLIST;
816                 return 1;
817             case 0x1b:      /* Esc. */
818                 p_sys->i_box_type = BOX_PLAYLIST;
819                 return 1;
820             case KEY_BACKSPACE:
821                 RemoveLastUTF8Entity( p_sys->psz_open_chain, i_chain_len );
822                 break;
823             default:
824             {
825                 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
826
827                 if( psz_utf8 != NULL )
828                 {
829                     if( i_chain_len + strlen( psz_utf8 ) < OPEN_CHAIN_SIZE )
830                     {
831                         strcpy( p_sys->psz_open_chain + i_chain_len,
832                                 psz_utf8 );
833                     }
834                     free( psz_utf8 );
835                 }
836                 break;
837             }
838         }
839         return 1;
840     }
841
842
843     /* Common keys */
844     switch( i_key )
845     {
846         case 'q':
847         case 'Q':
848         case 0x1b:  /* Esc */
849             vlc_object_kill( p_intf->p_libvlc );
850             return 0;
851
852         /* Box switching */
853         case 'i':
854             if( p_sys->i_box_type == BOX_INFO )
855                 p_sys->i_box_type = BOX_NONE;
856             else
857                 p_sys->i_box_type = BOX_INFO;
858             p_sys->i_box_lines_total = 0;
859             return 1;
860         case 'L':
861             if( p_sys->i_box_type == BOX_LOG )
862                 p_sys->i_box_type = BOX_NONE;
863             else
864                 p_sys->i_box_type = BOX_LOG;
865             return 1;
866         case 'P':
867             if( p_sys->i_box_type == BOX_PLAYLIST )
868                 p_sys->i_box_type = BOX_NONE;
869             else
870                 p_sys->i_box_type = BOX_PLAYLIST;
871             return 1;
872         case 'B':
873             if( p_sys->i_box_type == BOX_BROWSE )
874                 p_sys->i_box_type = BOX_NONE;
875             else
876                 p_sys->i_box_type = BOX_BROWSE;
877             return 1;
878         case 'h':
879         case 'H':
880             if( p_sys->i_box_type == BOX_HELP )
881                 p_sys->i_box_type = BOX_NONE;
882             else
883                 p_sys->i_box_type = BOX_HELP;
884             p_sys->i_box_lines_total = 0;
885             return 1;
886         case '/':
887             if( p_sys->i_box_type != BOX_SEARCH )
888             {
889                 if( p_sys->psz_search_chain == NULL )
890                 {
891                     return 1;
892                 }
893                 p_sys->psz_search_chain[0] = '\0';
894                 p_sys->b_box_plidx_follow = VLC_FALSE;
895                 p_sys->i_before_search = p_sys->i_box_plidx;
896                 p_sys->i_box_type = BOX_SEARCH;
897             }
898             return 1;
899         case 'A': /* Open */
900             if( p_sys->i_box_type != BOX_OPEN )
901             {
902                 if( p_sys->psz_open_chain == NULL )
903                 {
904                     return 1;
905                 }
906                 p_sys->psz_open_chain[0] = '\0';
907                 p_sys->i_box_type = BOX_OPEN;
908             }
909             return 1;
910
911         /* Navigation */
912         case KEY_RIGHT:
913             p_sys->f_slider += 1.0;
914             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
915             ManageSlider( p_intf );
916             return 1;
917
918         case KEY_LEFT:
919             p_sys->f_slider -= 1.0;
920             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
921             ManageSlider( p_intf );
922             return 1;
923
924         /* Common control */
925         case 'f':
926         {
927             if( p_intf->p_sys->p_input )
928             {
929                 vout_thread_t *p_vout;
930                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
931                                           VLC_OBJECT_VOUT, FIND_CHILD );
932                 if( p_vout )
933                 {
934                     var_Get( p_vout, "fullscreen", &val );
935                     val.b_bool = !val.b_bool;
936                     var_Set( p_vout, "fullscreen", val );
937                     vlc_object_release( p_vout );
938                 }
939                 else
940                 {
941                     playlist_t *p_playlist;
942                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
943                                                   FIND_ANYWHERE );
944                     if( p_playlist )
945                     {
946                         var_Get( p_playlist, "fullscreen", &val );
947                         val.b_bool = !val.b_bool;
948                         var_Set( p_playlist, "fullscreen", val );
949                         vlc_object_release( p_playlist );
950                     }
951                 }
952             }
953             return 0;
954         }
955
956         case ' ':
957             PlayPause( p_intf );
958             return 1;
959
960         case 's':
961             if( p_intf->p_sys->p_playlist )
962             {
963                 playlist_Stop( p_intf->p_sys->p_playlist );
964             }
965             return 1;
966
967         case 'e':
968             Eject( p_intf );
969             return 1;
970
971         case '[':
972             if( p_sys->p_input )
973             {
974                 val.b_bool = VLC_TRUE;
975                 var_Set( p_sys->p_input, "prev-title", val );
976             }
977             return 1;
978
979         case ']':
980             if( p_sys->p_input )
981             {
982                 val.b_bool = VLC_TRUE;
983                 var_Set( p_sys->p_input, "next-title", val );
984             }
985             return 1;
986
987         case '<':
988             if( p_sys->p_input )
989             {
990                 val.b_bool = VLC_TRUE;
991                 var_Set( p_sys->p_input, "prev-chapter", val );
992             }
993             return 1;
994
995         case '>':
996             if( p_sys->p_input )
997             {
998                 val.b_bool = VLC_TRUE;
999                 var_Set( p_sys->p_input, "next-chapter", val );
1000             }
1001             return 1;
1002
1003         case 'p':
1004             if( p_intf->p_sys->p_playlist )
1005             {
1006                 playlist_Prev( p_intf->p_sys->p_playlist );
1007             }
1008             clear();
1009             return 1;
1010
1011         case 'n':
1012             if( p_intf->p_sys->p_playlist )
1013             {
1014                 playlist_Next( p_intf->p_sys->p_playlist );
1015             }
1016             clear();
1017             return 1;
1018
1019         case 'a':
1020             aout_VolumeUp( p_intf, 1, NULL );
1021             clear();
1022             return 1;
1023
1024         case 'z':
1025             aout_VolumeDown( p_intf, 1, NULL );
1026             clear();
1027             return 1;
1028
1029         /*
1030          * ^l should clear and redraw the screen
1031          */
1032         case 0x0c:
1033             clear();
1034             return 1;
1035
1036         default:
1037             return 0;
1038     }
1039 }
1040
1041 static void ManageSlider( intf_thread_t *p_intf )
1042 {
1043     intf_sys_t     *p_sys = p_intf->p_sys;
1044     input_thread_t *p_input = p_sys->p_input;
1045     vlc_value_t     val;
1046
1047     if( p_input == NULL )
1048     {
1049         return;
1050     }
1051     var_Get( p_input, "state", &val );
1052     if( val.i_int != PLAYING_S )
1053     {
1054         return;
1055     }
1056
1057     var_Get( p_input, "position", &val );
1058     if( p_sys->f_slider == p_sys->f_slider_old )
1059     {
1060         p_sys->f_slider =
1061         p_sys->f_slider_old = 100 * val.f_float;
1062     }
1063     else
1064     {
1065         p_sys->f_slider_old = p_sys->f_slider;
1066
1067         val.f_float = p_sys->f_slider / 100.0;
1068         var_Set( p_input, "position", val );
1069     }
1070 }
1071
1072 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
1073 {
1074     int i_max;
1075     int i_first = 0 ;
1076     int i_item = -1;
1077     intf_sys_t *p_sys = p_intf->p_sys;
1078
1079     if( p_sys->i_before_search >= 0 )
1080     {
1081         i_first = p_sys->i_before_search;
1082     }
1083
1084     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
1085     {
1086         p_sys->i_box_plidx = p_sys->i_before_search;
1087         return;
1088     }
1089
1090     i_max = p_sys->i_plist_entries;
1091
1092     i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
1093     if( i_item < 0 )
1094     {
1095         i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1096     }
1097
1098     if( i_item < 0 || i_item >= i_max ) return;
1099
1100     p_sys->i_box_plidx = i_item;
1101 }
1102
1103 static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
1104                               int i_start, int i_stop )
1105 {
1106     intf_sys_t *p_sys = p_intf->p_sys;
1107     int i, i_item = -1;
1108
1109     for( i = i_start + 1; i < i_stop; i++ )
1110     {
1111         if( strcasestr( p_sys->pp_plist[i]->psz_display,
1112                         psz_searchstring ) != NULL )
1113         {
1114             i_item = i;
1115             break;
1116         }
1117     }
1118
1119     return i_item;
1120 }
1121
1122
1123 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
1124 {
1125     va_list  vl_args;
1126     char    *p_buf = NULL;
1127     int      i_len;
1128     size_t   i_char_len;    /* UCS character length */
1129     size_t   i_width;       /* Display width */
1130     wchar_t *psz_wide;      /* wchar_t representation of p_buf */
1131
1132     va_start( vl_args, p_fmt );
1133     vasprintf( &p_buf, p_fmt, vl_args );
1134     va_end( vl_args );
1135
1136     if( ( p_buf == NULL ) || ( w <= 0 ) )
1137         return;
1138
1139     i_len = strlen( p_buf );
1140     psz_wide = (wchar_t *) malloc( sizeof( wchar_t ) * ( i_len + 1 ) );
1141
1142     i_char_len = mbstowcs( psz_wide, p_buf, i_len );
1143
1144     if( i_char_len == -1 ) /* an invalid character was encountered */
1145         i_width = i_len;
1146     else
1147     {
1148         i_width = wcswidth( psz_wide, i_char_len );
1149         if( i_width == -1 ) /* a non printable character was encountered */
1150             i_width = i_len;
1151     }
1152
1153     if( i_width > w )
1154     { /* FIXME: ellipsize psz_wide while keeping the width in mind */
1155         char *psz_local;
1156         int i_cut = i_len - w;
1157         int x1 = i_len/2 - i_cut/2;
1158         int x2 = x1 + i_cut;
1159
1160         if( i_len > x2 )
1161         {
1162             memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1163         }
1164         p_buf[w] = '\0';
1165         if( w > 7 )
1166         {
1167             p_buf[w/2-1] = '.';
1168             p_buf[w/2  ] = '.';
1169             p_buf[w/2+1] = '.';
1170         }
1171         psz_local = ToLocale( p_buf );
1172         mvprintw( y, x, "%s", psz_local );
1173         LocaleFree( p_buf );
1174     }
1175     else
1176     {
1177         char *psz_local = ToLocale( p_buf );
1178         mvprintw( y, x, "%s", psz_local );
1179         LocaleFree( p_buf );
1180         mvhline( y, x + i_width, ' ', w - i_width );
1181     }
1182 }
1183 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1184 {
1185     intf_sys_t     *p_sys = p_intf->p_sys;
1186
1187     va_list  vl_args;
1188     char    *p_buf = NULL;
1189
1190     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1191     {
1192         return;
1193     }
1194
1195     va_start( vl_args, p_fmt );
1196     vasprintf( &p_buf, p_fmt, vl_args );
1197     va_end( vl_args );
1198
1199     if( p_buf == NULL )
1200     {
1201         return;
1202     }
1203
1204     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1205 }
1206
1207 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1208 {
1209     intf_sys_t     *p_sys = p_intf->p_sys;
1210     input_thread_t *p_input = p_sys->p_input;
1211     int y = 0;
1212     int h;
1213     int y_end;
1214
1215     clear();
1216
1217     /* Title */
1218     attrset( A_REVERSE );
1219     mvnprintw( y, 0, COLS, "VLC media player" " (ncurses interface) [ h for help ]" );
1220     attroff( A_REVERSE );
1221     y += 2;
1222
1223     /* Infos */
1224     if( p_input && !p_input->b_dead )
1225     {
1226         char buf1[MSTRTIME_MAX_SIZE];
1227         char buf2[MSTRTIME_MAX_SIZE];
1228         vlc_value_t val;
1229         vlc_value_t val_list;
1230
1231         /* Source */
1232         char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
1233         mvnprintw( y++, 0, COLS, " Source   : %s", psz_uri );
1234         free( psz_uri );
1235
1236         /* State */
1237         var_Get( p_input, "state", &val );
1238         if( val.i_int == PLAYING_S )
1239         {
1240             mvnprintw( y++, 0, COLS, " State    : Playing" );
1241         }
1242         else if( val.i_int == OPENING_S )
1243         {
1244             mvnprintw( y++, 0, COLS, " State    : Openning/Connecting" );
1245         }
1246         else if( val.i_int == BUFFERING_S )
1247         {
1248             mvnprintw( y++, 0, COLS, " State    : Buffering" );
1249         }
1250         else if( val.i_int == PAUSE_S )
1251         {
1252             mvnprintw( y++, 0, COLS, " State    : Paused" );
1253         }
1254         else
1255         {
1256             y++;
1257         }
1258         if( val.i_int != INIT_S && val.i_int != END_S )
1259         {
1260             audio_volume_t i_volume;
1261
1262             /* Position */
1263             var_Get( p_input, "time", &val );
1264             msecstotimestr( buf1, val.i_time / 1000 );
1265
1266             var_Get( p_input, "length", &val );
1267             msecstotimestr( buf2, val.i_time / 1000 );
1268
1269             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1270
1271             /* Volume */
1272             aout_VolumeGet( p_intf, &i_volume );
1273             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1274
1275             /* Title */
1276             if( !var_Get( p_input, "title", &val ) )
1277             {
1278                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1279                 if( val_list.p_list->i_count > 0 )
1280                 {
1281                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1282                 }
1283                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1284             }
1285
1286             /* Chapter */
1287             if( !var_Get( p_input, "chapter", &val ) )
1288             {
1289                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1290                 if( val_list.p_list->i_count > 0 )
1291                 {
1292                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1293                 }
1294                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1295             }
1296         }
1297         else
1298         {
1299             y += 2;
1300         }
1301     }
1302     else
1303     {
1304         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1305         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1306         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1307         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1308     }
1309
1310     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1311     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1312     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1313     y += 3;
1314
1315     p_sys->i_box_y = y + 1;
1316     p_sys->i_box_lines = LINES - y - 2;
1317
1318     h = LINES - y;
1319     y_end = y + h - 1;
1320
1321     if( p_sys->i_box_type == BOX_HELP )
1322     {
1323         /* Help box */
1324         int l = 0;
1325         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1326
1327         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1328         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1329         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1330         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1331         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1332         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1333         MainBoxWrite( p_intf, l++, 1, "" );
1334
1335         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1336         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1337         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1338         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1339         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1340         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1341         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1342         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1343         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1344         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1345         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1346         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1347         MainBoxWrite( p_intf, l++, 1, "" );
1348
1349         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1350         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1351         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1352         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1353         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1354         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1355         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1356         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1357         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1358         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1359         MainBoxWrite( p_intf, l++, 1, "" );
1360
1361         MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1362         MainBoxWrite( p_intf, l++, 1, "     <enter>     Add the selected file to the playlist" );
1363         MainBoxWrite( p_intf, l++, 1, "     <space>     Add the selected directory to the playlist" );
1364         MainBoxWrite( p_intf, l++, 1, "     .           Show/Hide hidden files" );
1365         MainBoxWrite( p_intf, l++, 1, "" );
1366
1367         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1368         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1369         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1370         MainBoxWrite( p_intf, l++, 1, "" );
1371
1372         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1373         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1374         MainBoxWrite( p_intf, l++, 1, "" );
1375
1376         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1377         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1378
1379         p_sys->i_box_lines_total = l;
1380         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1381         {
1382             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1383         }
1384
1385         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1386         {
1387             y += l - p_sys->i_box_start;
1388         }
1389         else
1390         {
1391             y += p_sys->i_box_lines;
1392         }
1393     }
1394     else if( p_sys->i_box_type == BOX_INFO )
1395     {
1396         /* Info box */
1397         int l = 0;
1398         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1399
1400         if( p_input )
1401         {
1402             int i,j;
1403             vlc_mutex_lock( &input_GetItem(p_input)->lock );
1404             for( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
1405             {
1406                 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
1407                 if( y >= y_end ) break;
1408                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1409                 for( j = 0; j < p_category->i_infos; j++ )
1410                 {
1411                     info_t *p_info = p_category->pp_infos[j];
1412                     if( y >= y_end ) break;
1413                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1414                 }
1415             }
1416             vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1417         }
1418         else
1419         {
1420             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1421         }
1422         p_sys->i_box_lines_total = l;
1423         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1424         {
1425             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1426         }
1427
1428         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1429         {
1430             y += l - p_sys->i_box_start;
1431         }
1432         else
1433         {
1434             y += p_sys->i_box_lines;
1435         }
1436     }
1437     else if( p_sys->i_box_type == BOX_LOG )
1438     {
1439         int i_line = 0;
1440         int i_stop;
1441         int i_start;
1442
1443         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1444
1445         i_start = p_intf->p_sys->p_sub->i_start;
1446
1447         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1448         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1449         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1450
1451         for( ;; )
1452         {
1453             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1454             if( i_line >= h - 2 )
1455             {
1456                 break;
1457             }
1458             i_stop--;
1459             i_line++;
1460             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1461             if( i_stop == i_start )
1462             {
1463                 break;
1464             }
1465             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1466                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1467                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1468         }
1469
1470         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1471         p_intf->p_sys->p_sub->i_start = i_stop;
1472         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1473         y = y_end;
1474     }
1475     else if( p_sys->i_box_type == BOX_BROWSE )
1476     {
1477         /* Filebrowser box */
1478         int        i_start, i_stop;
1479         int        i_item;
1480         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1481
1482         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1483         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1484
1485         if( p_sys->i_box_bidx < (h - 2)/2 )
1486         {
1487             i_start = 0;
1488             i_stop = h - 2;
1489         }
1490         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1491         {
1492             i_start = p_sys->i_box_bidx - (h - 2)/2;
1493             i_stop = i_start + h - 2;
1494         }
1495         else
1496         {
1497             i_stop = p_sys->i_dir_entries;
1498             i_start = p_sys->i_dir_entries - (h - 2);
1499         }
1500         if( i_start < 0 )
1501         {
1502             i_start = 0;
1503         }
1504         if( i_stop > p_sys->i_dir_entries )
1505         {
1506             i_stop = p_sys->i_dir_entries;
1507         }
1508
1509         for( i_item = i_start; i_item < i_stop; i_item++ )
1510         {
1511             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1512
1513             if( y >= y_end ) break;
1514             if( b_selected )
1515             {
1516                 attrset( A_REVERSE );
1517             }
1518             mvnprintw( y++, 1, COLS - 2, " %c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? ' ' : '+',
1519                             p_sys->pp_dir_entries[i_item]->psz_path );
1520             if( b_selected )
1521             {
1522                 attroff( A_REVERSE );
1523             }
1524         }
1525
1526     }
1527     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1528                p_sys->i_box_type == BOX_SEARCH ||
1529                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1530     {
1531         /* Playlist box */
1532         int        i_start, i_stop, i_max = p_sys->i_plist_entries;
1533         int        i_item;
1534         char       *psz_title;
1535
1536         switch( p_sys->i_current_view )
1537         {
1538             case VIEW_ONELEVEL:
1539                 psz_title = strdup( " Playlist (All, one level) " );
1540                 break;
1541             case VIEW_CATEGORY:
1542                 psz_title = strdup( " Playlist (By category) " );
1543                 break;
1544             default:
1545                 psz_title = strdup( " Playlist (Manually added) " );
1546         }
1547
1548         DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1549
1550         if( p_sys->b_need_update || p_sys->pp_plist == NULL )
1551         {
1552             PlaylistRebuild( p_intf );
1553         }
1554         if( p_sys->b_box_plidx_follow )
1555         {
1556             FindIndex( p_intf );
1557         }
1558
1559         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1560         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1561         if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
1562
1563         if( p_sys->i_box_plidx < (h - 2)/2 )
1564         {
1565             i_start = 0;
1566             i_stop = h - 2;
1567         }
1568         else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1569         {
1570             i_start = p_sys->i_box_plidx - (h - 2)/2;
1571             i_stop = i_start + h - 2;
1572         }
1573         else
1574         {
1575             i_stop = i_max;
1576             i_start = i_max - (h - 2);
1577         }
1578         if( i_start < 0 )
1579         {
1580             i_start = 0;
1581         }
1582         if( i_stop > i_max )
1583         {
1584             i_stop = i_max;
1585         }
1586
1587         for( i_item = i_start; i_item < i_stop; i_item++ )
1588         {
1589             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1590             int c = ( PlaylistIsPlaying( p_intf,
1591                           p_sys->pp_plist[i_item]->p_item ) ) ? '>' : ' ';
1592
1593             if( y >= y_end ) break;
1594             if( b_selected )
1595             {
1596                 attrset( A_REVERSE );
1597             }
1598             mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1599                        p_sys->pp_plist[i_item]->psz_display );
1600             if( b_selected )
1601             {
1602                 attroff( A_REVERSE );
1603             }
1604         }
1605
1606     }
1607     else
1608     {
1609         y++;
1610     }
1611     if( p_sys->i_box_type == BOX_SEARCH )
1612     {
1613         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1614         if( p_sys->psz_search_chain )
1615         {
1616             if( strlen( p_sys->psz_search_chain ) == 0 &&
1617                 p_sys->psz_old_search != NULL )
1618             {
1619                 /* Searching next entry */
1620                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1621             }
1622             else
1623             {
1624                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1625             }
1626         }
1627     }
1628     if( p_sys->i_box_type == BOX_OPEN )
1629     {
1630         if( p_sys->psz_open_chain )
1631         {
1632             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1633             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1634         }
1635     }
1636
1637     while( y < y_end )
1638     {
1639         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1640     }
1641
1642     refresh();
1643
1644     *t_last_refresh = time( 0 );
1645 }
1646
1647 static playlist_item_t *PlaylistGetRoot( intf_thread_t *p_intf )
1648 {
1649     intf_sys_t *p_sys = p_intf->p_sys;
1650     playlist_t *p_playlist = p_sys->p_playlist;
1651
1652     if( p_playlist == NULL )
1653     {
1654         return NULL;
1655     }
1656
1657     switch( p_sys->i_current_view )
1658     {
1659         case VIEW_CATEGORY:
1660             return p_playlist->p_root_category;
1661         default:
1662             return p_playlist->p_root_onelevel;
1663     }
1664 }
1665
1666 static void PlaylistRebuild( intf_thread_t *p_intf )
1667 {
1668     intf_sys_t *p_sys = p_intf->p_sys;
1669     playlist_t *p_playlist = p_sys->p_playlist;
1670
1671     if( p_playlist == NULL )
1672     {
1673         return;
1674     }
1675
1676     vlc_mutex_lock( &p_playlist->object_lock );
1677
1678     /* First clear the old one */
1679     PlaylistDestroy( p_intf );
1680
1681     /* Build the new one */
1682     PlaylistAddNode( p_intf, PlaylistGetRoot( p_intf ), 0, "" );
1683
1684     p_sys->b_need_update = VLC_FALSE;
1685
1686     vlc_mutex_unlock( &p_playlist->object_lock );
1687 }
1688
1689 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1690                              int i, const char *c )
1691 {
1692     intf_sys_t *p_sys = p_intf->p_sys;
1693     playlist_item_t *p_child;
1694     char *psz_tmp;
1695     int k;
1696
1697     psz_tmp = (char *)malloc( strlen( c ) + 4 );
1698     if( psz_tmp == NULL ) return;
1699     for( k = 0; k < p_node->i_children; k++ )
1700     {
1701         struct pl_item_t *p_pl_item;
1702         char *buff;
1703         int i_size;
1704
1705         p_child = p_node->pp_children[k];
1706         i_size = strlen( c ) + strlen( p_child->p_input->psz_name ) + 4;
1707         buff = (char *)malloc( sizeof( char ) * i_size );
1708         p_pl_item = (struct pl_item_t *)malloc( sizeof( struct pl_item_t ) );
1709         if( p_pl_item == NULL || buff == NULL ) return;
1710
1711         if( strlen( c ) )
1712         {
1713             sprintf( buff, "%s%c-%s", c, k == p_node->i_children - 1 ?
1714                      '`' : '|', p_child->p_input->psz_name );
1715         }
1716         else
1717         {
1718             sprintf( buff, " %s", p_child->p_input->psz_name );
1719         }
1720         p_pl_item->psz_display = strdup( buff );
1721         p_pl_item->p_item = p_child;
1722         INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1723                      p_sys->i_plist_entries, p_pl_item );
1724         free( buff );
1725         i++;
1726
1727         if( p_child->i_children > 0 )
1728         {
1729             sprintf( psz_tmp, "%s%c ", c,
1730                      k == p_node->i_children - 1 ? ' ' : '|' );
1731             PlaylistAddNode( p_intf, p_child, i,
1732                              strlen( c ) ? psz_tmp : " " );
1733         }
1734     }
1735     free( psz_tmp );
1736 }
1737
1738 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1739                             vlc_value_t oval, vlc_value_t nval, void *param )
1740 {
1741     intf_thread_t *p_intf = (intf_thread_t *)param;
1742     p_intf->p_sys->b_need_update = VLC_TRUE;
1743     return VLC_SUCCESS;
1744 }
1745
1746 /* Playlist suxx */
1747 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *p_intf,
1748                                             playlist_item_t *p_item )
1749 {
1750     playlist_item_t *p_played_item = p_intf->p_sys->p_playlist->status.p_item;
1751     return( p_item != NULL && p_played_item != NULL &&
1752             p_item->p_input != NULL && p_played_item->p_input != NULL &&
1753             p_item->p_input->i_id == p_played_item->p_input->i_id );
1754 }
1755
1756 static void FindIndex( intf_thread_t *p_intf )
1757 {
1758     intf_sys_t *p_sys = p_intf->p_sys;
1759     int i;
1760
1761     if( p_sys->i_box_plidx < p_sys->i_plist_entries &&
1762         p_sys->i_box_plidx >= 0 &&
1763         PlaylistIsPlaying( p_intf,
1764                            p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
1765     {
1766         return;
1767     }
1768
1769     for( i = 0; i < p_sys->i_plist_entries; i++ )
1770     {
1771         if( PlaylistIsPlaying( p_intf, p_sys->pp_plist[i]->p_item ) )
1772         {
1773             p_sys->i_box_plidx = i;
1774             break;
1775         }
1776     }
1777 }
1778
1779 static void PlaylistDestroy( intf_thread_t *p_intf )
1780 {
1781     intf_sys_t *p_sys = p_intf->p_sys;
1782     int i;
1783
1784     for( i = 0; i < p_sys->i_plist_entries; i++ )
1785     {
1786         struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
1787         free( p_pl_item->psz_display );
1788         REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
1789         free( p_pl_item );
1790     }
1791     p_sys->pp_plist = NULL;
1792     p_sys->i_plist_entries = 0;
1793 }
1794
1795 static void Eject( intf_thread_t *p_intf )
1796 {
1797     char *psz_device = NULL, *psz_parser, *psz_name;
1798
1799     /*
1800      * Get the active input
1801      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1802      * If it's neither of these, then return
1803      */
1804
1805     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1806                                                        FIND_ANYWHERE );
1807
1808     if( p_playlist == NULL )
1809     {
1810         return;
1811     }
1812
1813     vlc_mutex_lock( &p_playlist->object_lock );
1814
1815     if( p_playlist->status.p_item == NULL )
1816     {
1817         vlc_mutex_unlock( &p_playlist->object_lock );
1818         vlc_object_release( p_playlist );
1819         return;
1820     }
1821
1822     psz_name = p_playlist->status.p_item->p_input->psz_name;
1823
1824     if( psz_name )
1825     {
1826         if( !strncmp(psz_name, "dvd://", 4) )
1827         {
1828             switch( psz_name[strlen("dvd://")] )
1829             {
1830             case '\0':
1831             case '@':
1832                 psz_device = config_GetPsz( p_intf, "dvd" );
1833                 break;
1834             default:
1835                 /* Omit the first MRL-selector characters */
1836                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1837                 break;
1838             }
1839         }
1840         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1841         {
1842             switch( psz_name[strlen(VCD_MRL)] )
1843             {
1844             case '\0':
1845             case '@':
1846                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1847                 break;
1848             default:
1849                 /* Omit the beginning MRL-selector characters */
1850                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1851                 break;
1852             }
1853         }
1854         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1855         {
1856             switch( psz_name[strlen(CDDA_MRL)] )
1857             {
1858             case '\0':
1859             case '@':
1860                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1861                 break;
1862             default:
1863                 /* Omit the beginning MRL-selector characters */
1864                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1865                 break;
1866             }
1867         }
1868         else
1869         {
1870             psz_device = strdup( psz_name );
1871         }
1872     }
1873
1874     vlc_mutex_unlock( &p_playlist->object_lock );
1875     vlc_object_release( p_playlist );
1876
1877     if( psz_device == NULL )
1878     {
1879         return;
1880     }
1881
1882     /* Remove what we have after @ */
1883     psz_parser = psz_device;
1884     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1885     {
1886         if( *psz_parser == '@' )
1887         {
1888             *psz_parser = '\0';
1889             break;
1890         }
1891     }
1892
1893     /* If there's a stream playing, we aren't allowed to eject ! */
1894     if( p_intf->p_sys->p_input == NULL )
1895     {
1896         msg_Dbg( p_intf, "ejecting %s", psz_device );
1897
1898         intf_Eject( p_intf, psz_device );
1899     }
1900
1901     free(psz_device);
1902     return;
1903 }
1904
1905 static int comp_dir_entries( const void *pp_dir_entry1,
1906                              const void *pp_dir_entry2 )
1907 {
1908     struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
1909     struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
1910     if ( p_dir_entry1->b_file == p_dir_entry2->b_file ) {
1911         return strcasecmp( p_dir_entry1->psz_path, p_dir_entry2->psz_path );
1912     }
1913     else
1914     {
1915         return ( p_dir_entry1->b_file ? 1 : -1 );
1916     }
1917 }
1918
1919 static void ReadDir( intf_thread_t *p_intf )
1920 {
1921     intf_sys_t *p_sys = p_intf->p_sys;
1922     DIR *p_current_dir;
1923     int i;
1924
1925     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1926     {
1927         char *psz_entry;
1928
1929         /* Open the dir */
1930         p_current_dir = utf8_opendir( p_sys->psz_current_dir );
1931
1932         if( p_current_dir == NULL )
1933         {
1934             /* something went bad, get out of here ! */
1935 #ifdef HAVE_ERRNO_H
1936             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1937                       p_sys->psz_current_dir, strerror(errno));
1938 #else
1939             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1940 #endif
1941             return;
1942         }
1943
1944         /* Clean the old shit */
1945         for( i = 0; i < p_sys->i_dir_entries; i++ )
1946         {
1947             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1948             free( p_dir_entry->psz_path );
1949             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1950             free( p_dir_entry );
1951         }
1952         p_sys->pp_dir_entries = NULL;
1953         p_sys->i_dir_entries = 0;
1954
1955         /* while we still have entries in the directory */
1956         while( ( psz_entry = utf8_readdir( p_current_dir ) ) != NULL )
1957         {
1958 #if defined( S_ISDIR )
1959             struct stat stat_data;
1960 #endif
1961             struct dir_entry_t *p_dir_entry;
1962             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1963                                strlen( psz_entry ) + 2;
1964             char *psz_uri;
1965
1966             if( p_sys->b_show_hidden_files == VLC_FALSE &&
1967                 ( strlen( psz_entry ) && psz_entry[0] == '.' ) &&
1968                 strcmp( psz_entry, ".." ) )
1969             {
1970                 free( psz_entry );
1971                 continue;
1972             }
1973
1974             psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1975             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, psz_entry );
1976
1977             if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
1978             {
1979                 free( psz_uri );
1980                 free( psz_entry );
1981                 continue;
1982             }
1983
1984 #if defined( S_ISDIR )
1985             if( !utf8_stat( psz_uri, &stat_data )
1986              && S_ISDIR(stat_data.st_mode) )
1987 /*#elif defined( DT_DIR )
1988             if( p_dir_content->d_type & DT_DIR )*/
1989 #else
1990             if( 0 )
1991 #endif
1992             {
1993                 p_dir_entry->psz_path = strdup( psz_entry );
1994                 p_dir_entry->b_file = VLC_FALSE;
1995                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1996                      p_sys->i_dir_entries, p_dir_entry );
1997             }
1998             else
1999             {
2000                 p_dir_entry->psz_path = strdup( psz_entry );
2001                 p_dir_entry->b_file = VLC_TRUE;
2002                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2003                      p_sys->i_dir_entries, p_dir_entry );
2004             }
2005
2006             free( psz_uri );
2007             free( psz_entry );
2008         }
2009
2010         /* Sort */
2011         qsort( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2012                sizeof(struct dir_entry_t*), &comp_dir_entries );
2013
2014         closedir( p_current_dir );
2015         return;
2016     }
2017     else
2018     {
2019         msg_Dbg( p_intf, "no current dir set" );
2020         return;
2021     }
2022 }
2023
2024 static void PlayPause( intf_thread_t *p_intf )
2025 {
2026     input_thread_t *p_input = p_intf->p_sys->p_input;
2027     vlc_value_t val;
2028
2029     if( p_input )
2030     {
2031         var_Get( p_input, "state", &val );
2032         if( val.i_int != PAUSE_S )
2033         {
2034             val.i_int = PAUSE_S;
2035         }
2036         else
2037         {
2038             val.i_int = PLAYING_S;
2039         }
2040         var_Set( p_input, "state", val );
2041     }
2042     else if( p_intf->p_sys->p_playlist )
2043     {
2044         playlist_Play( p_intf->p_sys->p_playlist );
2045     }
2046 }
2047
2048 /****************************************************************************
2049  *
2050  ****************************************************************************/
2051 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title )
2052 {
2053     int i;
2054     int i_len;
2055
2056     if( w > 3 && h > 2 )
2057     {
2058         if( title == NULL ) title = "";
2059         i_len = strlen( title );
2060
2061         if( i_len > w - 2 ) i_len = w - 2;
2062
2063         mvwaddch( win, y, x,    ACS_ULCORNER );
2064         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
2065         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
2066         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
2067         mvwaddch( win, y, x+w-1,ACS_URCORNER );
2068
2069         for( i = 0; i < h-2; i++ )
2070         {
2071             mvwaddch( win, y+i+1, x,     ACS_VLINE );
2072             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
2073         }
2074
2075         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
2076         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
2077         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
2078     }
2079 }
2080
2081 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
2082 {
2083     if( w > 0 )
2084     {
2085         mvhline( y, x, ' ', w );
2086     }
2087 }
2088
2089 static void DrawLine( WINDOW *win, int y, int x, int w )
2090 {
2091     if( w > 0 )
2092     {
2093         attrset( A_REVERSE );
2094         mvhline( y, x, ' ', w );
2095         attroff( A_REVERSE );
2096     }
2097 }