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