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