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