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