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