]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
Remembers verbosity level when closing ncurses intf
[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 <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
1129     va_start( vl_args, p_fmt );
1130     vasprintf( &p_buf, p_fmt, vl_args );
1131     va_end( vl_args );
1132
1133     if( p_buf == NULL )
1134     {
1135         return;
1136     }
1137     if(  w > 0 )
1138     {
1139         if( ( i_len = strlen( p_buf ) ) > w )
1140         {
1141             char *psz_local;
1142             int i_cut = i_len - w;
1143             int x1 = i_len/2 - i_cut/2;
1144             int x2 = x1 + i_cut;
1145
1146             if( i_len > x2 )
1147             {
1148                 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1149             }
1150             p_buf[w] = '\0';
1151             if( w > 7 )
1152             {
1153                 p_buf[w/2-1] = '.';
1154                 p_buf[w/2  ] = '.';
1155                 p_buf[w/2+1] = '.';
1156             }
1157             psz_local = ToLocale( p_buf );
1158             mvprintw( y, x, "%s", psz_local );
1159             LocaleFree( p_buf );
1160         }
1161         else
1162         {
1163             char *psz_local = ToLocale( p_buf );
1164             mvprintw( y, x, "%s", psz_local );
1165             LocaleFree( p_buf );
1166             mvhline( y, x + i_len, ' ', w - i_len );
1167         }
1168     }
1169 }
1170 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1171 {
1172     intf_sys_t     *p_sys = p_intf->p_sys;
1173
1174     va_list  vl_args;
1175     char    *p_buf = NULL;
1176
1177     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1178     {
1179         return;
1180     }
1181
1182     va_start( vl_args, p_fmt );
1183     vasprintf( &p_buf, p_fmt, vl_args );
1184     va_end( vl_args );
1185
1186     if( p_buf == NULL )
1187     {
1188         return;
1189     }
1190
1191     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1192 }
1193
1194 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1195 {
1196     intf_sys_t     *p_sys = p_intf->p_sys;
1197     input_thread_t *p_input = p_sys->p_input;
1198     int y = 0;
1199     int h;
1200     int y_end;
1201
1202     clear();
1203
1204     /* Title */
1205     attrset( A_REVERSE );
1206     mvnprintw( y, 0, COLS, "VLC media player" " (ncurses interface) [ h for help ]" );
1207     attroff( A_REVERSE );
1208     y += 2;
1209
1210     /* Infos */
1211     if( p_input && !p_input->b_dead )
1212     {
1213         char buf1[MSTRTIME_MAX_SIZE];
1214         char buf2[MSTRTIME_MAX_SIZE];
1215         vlc_value_t val;
1216         vlc_value_t val_list;
1217
1218         /* Source */
1219         char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
1220         mvnprintw( y++, 0, COLS, " Source   : %s", psz_uri );
1221         free( psz_uri );
1222
1223         /* State */
1224         var_Get( p_input, "state", &val );
1225         if( val.i_int == PLAYING_S )
1226         {
1227             mvnprintw( y++, 0, COLS, " State    : Playing" );
1228         }
1229         else if( val.i_int == OPENING_S )
1230         {
1231             mvnprintw( y++, 0, COLS, " State    : Openning/Connecting" );
1232         }
1233         else if( val.i_int == BUFFERING_S )
1234         {
1235             mvnprintw( y++, 0, COLS, " State    : Buffering" );
1236         }
1237         else if( val.i_int == PAUSE_S )
1238         {
1239             mvnprintw( y++, 0, COLS, " State    : Paused" );
1240         }
1241         else
1242         {
1243             y++;
1244         }
1245         if( val.i_int != INIT_S && val.i_int != END_S )
1246         {
1247             audio_volume_t i_volume;
1248
1249             /* Position */
1250             var_Get( p_input, "time", &val );
1251             msecstotimestr( buf1, val.i_time / 1000 );
1252
1253             var_Get( p_input, "length", &val );
1254             msecstotimestr( buf2, val.i_time / 1000 );
1255
1256             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1257
1258             /* Volume */
1259             aout_VolumeGet( p_intf, &i_volume );
1260             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1261
1262             /* Title */
1263             if( !var_Get( p_input, "title", &val ) )
1264             {
1265                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1266                 if( val_list.p_list->i_count > 0 )
1267                 {
1268                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1269                 }
1270                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1271             }
1272
1273             /* Chapter */
1274             if( !var_Get( p_input, "chapter", &val ) )
1275             {
1276                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1277                 if( val_list.p_list->i_count > 0 )
1278                 {
1279                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1280                 }
1281                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1282             }
1283         }
1284         else
1285         {
1286             y += 2;
1287         }
1288     }
1289     else
1290     {
1291         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1292         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1293         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1294         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1295     }
1296
1297     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1298     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1299     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1300     y += 3;
1301
1302     p_sys->i_box_y = y + 1;
1303     p_sys->i_box_lines = LINES - y - 2;
1304
1305     h = LINES - y;
1306     y_end = y + h - 1;
1307
1308     if( p_sys->i_box_type == BOX_HELP )
1309     {
1310         /* Help box */
1311         int l = 0;
1312         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1313
1314         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1315         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1316         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1317         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1318         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1319         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1320         MainBoxWrite( p_intf, l++, 1, "" );
1321
1322         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1323         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1324         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1325         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1326         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1327         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1328         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1329         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1330         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1331         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1332         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1333         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1334         MainBoxWrite( p_intf, l++, 1, "" );
1335
1336         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1337         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1338         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1339         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1340         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1341         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1342         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1343         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1344         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1345         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1346         MainBoxWrite( p_intf, l++, 1, "" );
1347
1348         MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1349         MainBoxWrite( p_intf, l++, 1, "     <enter>     Add the selected file to the playlist" );
1350         MainBoxWrite( p_intf, l++, 1, "     <space>     Add the selected directory to the playlist" );
1351         MainBoxWrite( p_intf, l++, 1, "     .           Show/Hide hidden files" );
1352         MainBoxWrite( p_intf, l++, 1, "" );
1353
1354         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1355         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1356         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1357         MainBoxWrite( p_intf, l++, 1, "" );
1358
1359         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1360         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1361         MainBoxWrite( p_intf, l++, 1, "" );
1362
1363         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1364         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1365
1366         p_sys->i_box_lines_total = l;
1367         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1368         {
1369             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1370         }
1371
1372         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1373         {
1374             y += l - p_sys->i_box_start;
1375         }
1376         else
1377         {
1378             y += p_sys->i_box_lines;
1379         }
1380     }
1381     else if( p_sys->i_box_type == BOX_INFO )
1382     {
1383         /* Info box */
1384         int l = 0;
1385         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1386
1387         if( p_input )
1388         {
1389             int i,j;
1390             vlc_mutex_lock( &input_GetItem(p_input)->lock );
1391             for( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
1392             {
1393                 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
1394                 if( y >= y_end ) break;
1395                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1396                 for( j = 0; j < p_category->i_infos; j++ )
1397                 {
1398                     info_t *p_info = p_category->pp_infos[j];
1399                     if( y >= y_end ) break;
1400                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1401                 }
1402             }
1403             vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1404         }
1405         else
1406         {
1407             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1408         }
1409         p_sys->i_box_lines_total = l;
1410         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1411         {
1412             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1413         }
1414
1415         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1416         {
1417             y += l - p_sys->i_box_start;
1418         }
1419         else
1420         {
1421             y += p_sys->i_box_lines;
1422         }
1423     }
1424     else if( p_sys->i_box_type == BOX_LOG )
1425     {
1426         int i_line = 0;
1427         int i_stop;
1428         int i_start;
1429
1430         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1431
1432         i_start = p_intf->p_sys->p_sub->i_start;
1433
1434         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1435         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1436         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1437
1438         for( ;; )
1439         {
1440             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1441             if( i_line >= h - 2 )
1442             {
1443                 break;
1444             }
1445             i_stop--;
1446             i_line++;
1447             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1448             if( i_stop == i_start )
1449             {
1450                 break;
1451             }
1452             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1453                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1454                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1455         }
1456
1457         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1458         p_intf->p_sys->p_sub->i_start = i_stop;
1459         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1460         y = y_end;
1461     }
1462     else if( p_sys->i_box_type == BOX_BROWSE )
1463     {
1464         /* Filebrowser box */
1465         int        i_start, i_stop;
1466         int        i_item;
1467         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1468
1469         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1470         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1471
1472         if( p_sys->i_box_bidx < (h - 2)/2 )
1473         {
1474             i_start = 0;
1475             i_stop = h - 2;
1476         }
1477         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1478         {
1479             i_start = p_sys->i_box_bidx - (h - 2)/2;
1480             i_stop = i_start + h - 2;
1481         }
1482         else
1483         {
1484             i_stop = p_sys->i_dir_entries;
1485             i_start = p_sys->i_dir_entries - (h - 2);
1486         }
1487         if( i_start < 0 )
1488         {
1489             i_start = 0;
1490         }
1491         if( i_stop > p_sys->i_dir_entries )
1492         {
1493             i_stop = p_sys->i_dir_entries;
1494         }
1495
1496         for( i_item = i_start; i_item < i_stop; i_item++ )
1497         {
1498             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1499
1500             if( y >= y_end ) break;
1501             if( b_selected )
1502             {
1503                 attrset( A_REVERSE );
1504             }
1505             mvnprintw( y++, 1, COLS - 2, " %c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? ' ' : '+',
1506                             p_sys->pp_dir_entries[i_item]->psz_path );
1507             if( b_selected )
1508             {
1509                 attroff( A_REVERSE );
1510             }
1511         }
1512
1513     }
1514     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1515                p_sys->i_box_type == BOX_SEARCH ||
1516                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1517     {
1518         /* Playlist box */
1519         int        i_start, i_stop, i_max = p_sys->i_plist_entries;
1520         int        i_item;
1521         char       *psz_title;
1522
1523         switch( p_sys->i_current_view )
1524         {
1525             case VIEW_ONELEVEL:
1526                 psz_title = strdup( " Playlist (All, one level) " );
1527                 break;
1528             case VIEW_CATEGORY:
1529                 psz_title = strdup( " Playlist (By category) " );
1530                 break;
1531             default:
1532                 psz_title = strdup( " Playlist (Manually added) " );
1533         }
1534
1535         DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1536
1537         if( p_sys->b_need_update || p_sys->pp_plist == NULL )
1538         {
1539             PlaylistRebuild( p_intf );
1540         }
1541         if( p_sys->b_box_plidx_follow )
1542         {
1543             FindIndex( p_intf );
1544         }
1545
1546         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1547         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1548         if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
1549
1550         if( p_sys->i_box_plidx < (h - 2)/2 )
1551         {
1552             i_start = 0;
1553             i_stop = h - 2;
1554         }
1555         else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1556         {
1557             i_start = p_sys->i_box_plidx - (h - 2)/2;
1558             i_stop = i_start + h - 2;
1559         }
1560         else
1561         {
1562             i_stop = i_max;
1563             i_start = i_max - (h - 2);
1564         }
1565         if( i_start < 0 )
1566         {
1567             i_start = 0;
1568         }
1569         if( i_stop > i_max )
1570         {
1571             i_stop = i_max;
1572         }
1573
1574         for( i_item = i_start; i_item < i_stop; i_item++ )
1575         {
1576             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1577             int c = ( PlaylistIsPlaying( p_intf,
1578                           p_sys->pp_plist[i_item]->p_item ) ) ? '>' : ' ';
1579
1580             if( y >= y_end ) break;
1581             if( b_selected )
1582             {
1583                 attrset( A_REVERSE );
1584             }
1585             mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1586                        p_sys->pp_plist[i_item]->psz_display );
1587             if( b_selected )
1588             {
1589                 attroff( A_REVERSE );
1590             }
1591         }
1592
1593     }
1594     else
1595     {
1596         y++;
1597     }
1598     if( p_sys->i_box_type == BOX_SEARCH )
1599     {
1600         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1601         if( p_sys->psz_search_chain )
1602         {
1603             if( strlen( p_sys->psz_search_chain ) == 0 &&
1604                 p_sys->psz_old_search != NULL )
1605             {
1606                 /* Searching next entry */
1607                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1608             }
1609             else
1610             {
1611                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1612             }
1613         }
1614     }
1615     if( p_sys->i_box_type == BOX_OPEN )
1616     {
1617         if( p_sys->psz_open_chain )
1618         {
1619             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1620             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1621         }
1622     }
1623
1624     while( y < y_end )
1625     {
1626         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1627     }
1628
1629     refresh();
1630
1631     *t_last_refresh = time( 0 );
1632 }
1633
1634 static playlist_item_t *PlaylistGetRoot( intf_thread_t *p_intf )
1635 {
1636     intf_sys_t *p_sys = p_intf->p_sys;
1637     playlist_t *p_playlist = p_sys->p_playlist;
1638
1639     if( p_playlist == NULL )
1640     {
1641         return NULL;
1642     }
1643
1644     switch( p_sys->i_current_view )
1645     {
1646         case VIEW_CATEGORY:
1647             return p_playlist->p_root_category;
1648         default:
1649             return p_playlist->p_root_onelevel;
1650     }
1651 }
1652
1653 static void PlaylistRebuild( intf_thread_t *p_intf )
1654 {
1655     intf_sys_t *p_sys = p_intf->p_sys;
1656     playlist_t *p_playlist = p_sys->p_playlist;
1657
1658     if( p_playlist == NULL )
1659     {
1660         return;
1661     }
1662
1663     vlc_mutex_lock( &p_playlist->object_lock );
1664
1665     /* First clear the old one */
1666     PlaylistDestroy( p_intf );
1667
1668     /* Build the new one */
1669     PlaylistAddNode( p_intf, PlaylistGetRoot( p_intf ), 0, "" );
1670
1671     p_sys->b_need_update = VLC_FALSE;
1672
1673     vlc_mutex_unlock( &p_playlist->object_lock );
1674 }
1675
1676 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1677                              int i, const char *c )
1678 {
1679     intf_sys_t *p_sys = p_intf->p_sys;
1680     playlist_item_t *p_child;
1681     char *psz_tmp;
1682     int k;
1683
1684     psz_tmp = (char *)malloc( strlen( c ) + 4 );
1685     if( psz_tmp == NULL ) return;
1686     for( k = 0; k < p_node->i_children; k++ )
1687     {
1688         struct pl_item_t *p_pl_item;
1689         char *buff;
1690         int i_size;
1691
1692         p_child = p_node->pp_children[k];
1693         i_size = strlen( c ) + strlen( p_child->p_input->psz_name ) + 4;
1694         buff = (char *)malloc( sizeof( char ) * i_size );
1695         p_pl_item = (struct pl_item_t *)malloc( sizeof( struct pl_item_t ) );
1696         if( p_pl_item == NULL || buff == NULL ) return;
1697
1698         if( strlen( c ) )
1699         {
1700             sprintf( buff, "%s%c-%s", c, k == p_node->i_children - 1 ?
1701                      '`' : '|', p_child->p_input->psz_name );
1702         }
1703         else
1704         {
1705             sprintf( buff, " %s", p_child->p_input->psz_name );
1706         }
1707         p_pl_item->psz_display = strdup( buff );
1708         p_pl_item->p_item = p_child;
1709         INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1710                      p_sys->i_plist_entries, p_pl_item );
1711         free( buff );
1712         i++;
1713
1714         if( p_child->i_children > 0 )
1715         {
1716             sprintf( psz_tmp, "%s%c ", c,
1717                      k == p_node->i_children - 1 ? ' ' : '|' );
1718             PlaylistAddNode( p_intf, p_child, i,
1719                              strlen( c ) ? psz_tmp : " " );
1720         }
1721     }
1722     free( psz_tmp );
1723 }
1724
1725 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1726                             vlc_value_t oval, vlc_value_t nval, void *param )
1727 {
1728     intf_thread_t *p_intf = (intf_thread_t *)param;
1729     p_intf->p_sys->b_need_update = VLC_TRUE;
1730     return VLC_SUCCESS;
1731 }
1732
1733 /* Playlist suxx */
1734 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *p_intf,
1735                                             playlist_item_t *p_item )
1736 {
1737     playlist_item_t *p_played_item = p_intf->p_sys->p_playlist->status.p_item;
1738     return( p_item != NULL && p_played_item != NULL &&
1739             p_item->p_input != NULL && p_played_item->p_input != NULL &&
1740             p_item->p_input->i_id == p_played_item->p_input->i_id );
1741 }
1742
1743 static void FindIndex( intf_thread_t *p_intf )
1744 {
1745     intf_sys_t *p_sys = p_intf->p_sys;
1746     int i;
1747
1748     if( p_sys->i_box_plidx < p_sys->i_plist_entries &&
1749         p_sys->i_box_plidx >= 0 &&
1750         PlaylistIsPlaying( p_intf,
1751                            p_sys->pp_plist[p_sys->i_box_plidx]->p_item ) )
1752     {
1753         return;
1754     }
1755
1756     for( i = 0; i < p_sys->i_plist_entries; i++ )
1757     {
1758         if( PlaylistIsPlaying( p_intf, p_sys->pp_plist[i]->p_item ) )
1759         {
1760             p_sys->i_box_plidx = i;
1761             break;
1762         }
1763     }
1764 }
1765
1766 static void PlaylistDestroy( intf_thread_t *p_intf )
1767 {
1768     intf_sys_t *p_sys = p_intf->p_sys;
1769     int i;
1770
1771     for( i = 0; i < p_sys->i_plist_entries; i++ )
1772     {
1773         struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
1774         free( p_pl_item->psz_display );
1775         REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
1776         free( p_pl_item );
1777     }
1778     p_sys->pp_plist = NULL;
1779     p_sys->i_plist_entries = 0;
1780 }
1781
1782 static void Eject( intf_thread_t *p_intf )
1783 {
1784     char *psz_device = NULL, *psz_parser, *psz_name;
1785
1786     /*
1787      * Get the active input
1788      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1789      * If it's neither of these, then return
1790      */
1791
1792     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1793                                                        FIND_ANYWHERE );
1794
1795     if( p_playlist == NULL )
1796     {
1797         return;
1798     }
1799
1800     vlc_mutex_lock( &p_playlist->object_lock );
1801
1802     if( p_playlist->status.p_item == NULL )
1803     {
1804         vlc_mutex_unlock( &p_playlist->object_lock );
1805         vlc_object_release( p_playlist );
1806         return;
1807     }
1808
1809     psz_name = p_playlist->status.p_item->p_input->psz_name;
1810
1811     if( psz_name )
1812     {
1813         if( !strncmp(psz_name, "dvd://", 4) )
1814         {
1815             switch( psz_name[strlen("dvd://")] )
1816             {
1817             case '\0':
1818             case '@':
1819                 psz_device = config_GetPsz( p_intf, "dvd" );
1820                 break;
1821             default:
1822                 /* Omit the first MRL-selector characters */
1823                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1824                 break;
1825             }
1826         }
1827         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1828         {
1829             switch( psz_name[strlen(VCD_MRL)] )
1830             {
1831             case '\0':
1832             case '@':
1833                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1834                 break;
1835             default:
1836                 /* Omit the beginning MRL-selector characters */
1837                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1838                 break;
1839             }
1840         }
1841         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1842         {
1843             switch( psz_name[strlen(CDDA_MRL)] )
1844             {
1845             case '\0':
1846             case '@':
1847                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1848                 break;
1849             default:
1850                 /* Omit the beginning MRL-selector characters */
1851                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1852                 break;
1853             }
1854         }
1855         else
1856         {
1857             psz_device = strdup( psz_name );
1858         }
1859     }
1860
1861     vlc_mutex_unlock( &p_playlist->object_lock );
1862     vlc_object_release( p_playlist );
1863
1864     if( psz_device == NULL )
1865     {
1866         return;
1867     }
1868
1869     /* Remove what we have after @ */
1870     psz_parser = psz_device;
1871     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1872     {
1873         if( *psz_parser == '@' )
1874         {
1875             *psz_parser = '\0';
1876             break;
1877         }
1878     }
1879
1880     /* If there's a stream playing, we aren't allowed to eject ! */
1881     if( p_intf->p_sys->p_input == NULL )
1882     {
1883         msg_Dbg( p_intf, "ejecting %s", psz_device );
1884
1885         intf_Eject( p_intf, psz_device );
1886     }
1887
1888     free(psz_device);
1889     return;
1890 }
1891
1892 static int comp_dir_entries( const void *pp_dir_entry1,
1893                              const void *pp_dir_entry2 )
1894 {
1895     struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
1896     struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
1897     if ( p_dir_entry1->b_file == p_dir_entry2->b_file ) {
1898         return strcasecmp( p_dir_entry1->psz_path, p_dir_entry2->psz_path );
1899     }
1900     else
1901     {
1902         return ( p_dir_entry1->b_file ? 1 : -1 );
1903     }
1904 }
1905
1906 static void ReadDir( intf_thread_t *p_intf )
1907 {
1908     intf_sys_t *p_sys = p_intf->p_sys;
1909     DIR *p_current_dir;
1910     int i;
1911
1912     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1913     {
1914         char *psz_entry;
1915
1916         /* Open the dir */
1917         p_current_dir = utf8_opendir( p_sys->psz_current_dir );
1918
1919         if( p_current_dir == NULL )
1920         {
1921             /* something went bad, get out of here ! */
1922 #ifdef HAVE_ERRNO_H
1923             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1924                       p_sys->psz_current_dir, strerror(errno));
1925 #else
1926             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1927 #endif
1928             return;
1929         }
1930
1931         /* Clean the old shit */
1932         for( i = 0; i < p_sys->i_dir_entries; i++ )
1933         {
1934             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1935             free( p_dir_entry->psz_path );
1936             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1937             free( p_dir_entry );
1938         }
1939         p_sys->pp_dir_entries = NULL;
1940         p_sys->i_dir_entries = 0;
1941
1942         /* while we still have entries in the directory */
1943         while( ( psz_entry = utf8_readdir( p_current_dir ) ) != NULL )
1944         {
1945 #if defined( S_ISDIR )
1946             struct stat stat_data;
1947 #endif
1948             struct dir_entry_t *p_dir_entry;
1949             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1950                                strlen( psz_entry ) + 2;
1951             char *psz_uri;
1952
1953             if( p_sys->b_show_hidden_files == VLC_FALSE &&
1954                 ( strlen( psz_entry ) && psz_entry[0] == '.' ) &&
1955                 strcmp( psz_entry, ".." ) )
1956             {
1957                 free( psz_entry );
1958                 continue;
1959             }
1960
1961             psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1962             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, psz_entry );
1963
1964             if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
1965             {
1966                 free( psz_uri );
1967                 free( psz_entry );
1968                 continue;
1969             }
1970
1971 #if defined( S_ISDIR )
1972             if( !utf8_stat( psz_uri, &stat_data )
1973              && S_ISDIR(stat_data.st_mode) )
1974 /*#elif defined( DT_DIR )
1975             if( p_dir_content->d_type & DT_DIR )*/
1976 #else
1977             if( 0 )
1978 #endif
1979             {
1980                 p_dir_entry->psz_path = strdup( psz_entry );
1981                 p_dir_entry->b_file = VLC_FALSE;
1982                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1983                      p_sys->i_dir_entries, p_dir_entry );
1984             }
1985             else
1986             {
1987                 p_dir_entry->psz_path = strdup( psz_entry );
1988                 p_dir_entry->b_file = VLC_TRUE;
1989                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1990                      p_sys->i_dir_entries, p_dir_entry );
1991             }
1992
1993             free( psz_uri );
1994             free( psz_entry );
1995         }
1996
1997         /* Sort */
1998         qsort( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1999                sizeof(struct dir_entry_t*), &comp_dir_entries );
2000
2001         closedir( p_current_dir );
2002         return;
2003     }
2004     else
2005     {
2006         msg_Dbg( p_intf, "no current dir set" );
2007         return;
2008     }
2009 }
2010
2011 static void PlayPause( intf_thread_t *p_intf )
2012 {
2013     input_thread_t *p_input = p_intf->p_sys->p_input;
2014     vlc_value_t val;
2015
2016     if( p_input )
2017     {
2018         var_Get( p_input, "state", &val );
2019         if( val.i_int != PAUSE_S )
2020         {
2021             val.i_int = PAUSE_S;
2022         }
2023         else
2024         {
2025             val.i_int = PLAYING_S;
2026         }
2027         var_Set( p_input, "state", val );
2028     }
2029     else if( p_intf->p_sys->p_playlist )
2030     {
2031         playlist_Play( p_intf->p_sys->p_playlist );
2032     }
2033 }
2034
2035 /****************************************************************************
2036  *
2037  ****************************************************************************/
2038 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title )
2039 {
2040     int i;
2041     int i_len;
2042
2043     if( w > 3 && h > 2 )
2044     {
2045         if( title == NULL ) title = "";
2046         i_len = strlen( title );
2047
2048         if( i_len > w - 2 ) i_len = w - 2;
2049
2050         mvwaddch( win, y, x,    ACS_ULCORNER );
2051         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
2052         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
2053         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
2054         mvwaddch( win, y, x+w-1,ACS_URCORNER );
2055
2056         for( i = 0; i < h-2; i++ )
2057         {
2058             mvwaddch( win, y+i+1, x,     ACS_VLINE );
2059             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
2060         }
2061
2062         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
2063         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
2064         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
2065     }
2066 }
2067
2068 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
2069 {
2070     if( w > 0 )
2071     {
2072         mvhline( y, x, ' ', w );
2073     }
2074 }
2075
2076 static void DrawLine( WINDOW *win, int y, int x, int w )
2077 {
2078     if( w > 0 )
2079     {
2080         attrset( A_REVERSE );
2081         mvhline( y, x, ' ', w );
2082         attroff( A_REVERSE );
2083     }
2084 }