ncurses.c 61.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * ncurses.c : NCurses plugin for vlc
 *****************************************************************************
4
 * Copyright (C) 2001-2004 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
hartman's avatar
hartman committed
9 10
 *          Yoann Peronneau <yoann@videolan.org>
 *          Derk-Jan Hartman <hartman at videolan dot org>
11
 *
12 13 14 15
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
16
 *
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>
#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <time.h>

#include <curses.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/vout.h>
hartman's avatar
hartman committed
41
#include <vlc/aout.h>
42

hartman's avatar
hartman committed
43 44 45 46 47 48 49 50
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#if (!defined( WIN32 ) || defined(__MINGW32__))
/* Mingw has its own version of dirent */
#   include <dirent.h>
#endif

51 52
#ifdef HAVE_CDDAX
#define CDDA_MRL "cddax://"
53
#else
54 55 56 57 58 59 60 61 62
#define CDDA_MRL "cdda://"
#endif

#ifdef HAVE_VCDX
#define VCD_MRL "vcdx://"
#else
#define VCD_MRL "vcdx://"
#endif

63 64 65
#define SEARCH_CHAIN_SIZE 20
#define OPEN_CHAIN_SIZE 50

66 67 68 69
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open           ( vlc_object_t * );
70
static void Close          ( vlc_object_t * );
71

72
static void Run            ( intf_thread_t * );
73
static void PlayPause      ( intf_thread_t * );
74 75 76
static void Eject          ( intf_thread_t * );

static int  HandleKey      ( intf_thread_t *, int );
77
static void Redraw         ( intf_thread_t *, time_t * );
78 79 80 81 82 83
static void PlaylistRebuild( intf_thread_t * );
static void PlaylistAddNode( intf_thread_t *, playlist_item_t *, int, char *);
static void PlaylistDestroy( intf_thread_t * );
static int  PlaylistChanged( vlc_object_t *, const char *, vlc_value_t,
                             vlc_value_t, void * );
static void FindIndex      ( intf_thread_t * );
84
static void SearchPlaylist ( intf_thread_t *, char * );
85
static int  SubSearchPlaylist( intf_thread_t *, char *, int, int );
86
static void ManageSlider   ( intf_thread_t * );
hartman's avatar
hartman committed
87
static void ReadDir        ( intf_thread_t * );
88 89 90 91

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
hartman's avatar
hartman committed
92 93 94

#define BROWSE_TEXT N_("Filebrowser starting point")
#define BROWSE_LONGTEXT N_( \
Felix Paul Kühne's avatar
Felix Paul Kühne committed
95
    "This option allows you to specify the directory the ncurses filebrowser " \
hartman's avatar
hartman committed
96 97
    "will show you initially.")

98
vlc_module_begin();
99
    set_shortname( "Ncurses" );
100
    set_description( _("Ncurses interface") );
101
    set_capability( "interface", 10 );
zorglub's avatar
zorglub committed
102 103
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_GENERAL );
104 105
    set_callbacks( Open, Close );
    add_shortcut( "curses" );
hartman's avatar
hartman committed
106
    add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );
107 108 109 110 111
vlc_module_end();

/*****************************************************************************
 * intf_sys_t: description and status of ncurses interface
 *****************************************************************************/
112 113 114 115 116 117
enum
{
    BOX_NONE,
    BOX_HELP,
    BOX_INFO,
    BOX_LOG,
118 119
    BOX_PLAYLIST,
    BOX_SEARCH,
hartman's avatar
hartman committed
120 121 122 123 124 125 126
    BOX_OPEN,
    BOX_BROWSE
};
struct dir_entry_t
{
    vlc_bool_t  b_file;
    char        *psz_path;
127
};
128 129 130 131 132
struct pl_item_t
{
    playlist_item_t *p_item;
    char            *psz_display;
};
133 134
struct intf_sys_t
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    playlist_t     *p_playlist;
    input_thread_t *p_input;

    float           f_slider;
    float           f_slider_old;

    WINDOW          *w;

    int             i_box_type;
    int             i_box_y;
    int             i_box_lines;
    int             i_box_lines_total;
    int             i_box_start;

149 150
    int             i_box_plidx;    /* Playlist index */
    int             b_box_plidx_follow;
151 152
    playlist_item_t *p_plnode;      /* Playlist node */
    int             i_box_bidx;     /* browser index */
153

154
    int             b_box_cleared;
155

156
    msg_subscription_t* p_sub;                  /* message bank subscription */
157 158 159 160 161 162

    char            *psz_search_chain;          /* for playlist searching    */
    char            *psz_old_search;            /* for searching next        */
    int             i_before_search;

    char            *psz_open_chain;
163

hartman's avatar
hartman committed
164 165 166
    char            *psz_current_dir;
    int             i_dir_entries;
    struct dir_entry_t  **pp_dir_entries;
167
    vlc_bool_t      b_show_hidden_files;
168 169 170 171 172

    int             i_current_view;             /* playlist view             */
    struct pl_item_t    **pp_plist;
    int             i_plist_entries;
    vlc_bool_t      b_need_update;              /* for playlist view */
173 174
};

175 176 177 178
static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title );
static void DrawLine( WINDOW *win, int y, int x, int w );
static void DrawEmptyLine( WINDOW *win, int y, int x, int w );

179 180 181 182
/*****************************************************************************
 * Open: initialize and create window
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
183
{
184
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
185
    intf_sys_t    *p_sys;
186
    vlc_value_t    val;
187 188

    /* Allocate instance and initialize some members */
189 190 191 192 193 194 195 196 197
    p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    p_sys->p_playlist = NULL;
    p_sys->p_input = NULL;
    p_sys->f_slider = 0.0;
    p_sys->f_slider_old = 0.0;
    p_sys->i_box_type = BOX_PLAYLIST;
    p_sys->i_box_lines = 0;
    p_sys->i_box_start= 0;
    p_sys->i_box_lines_total = 0;
198
    p_sys->b_box_plidx_follow = VLC_TRUE;
199
    p_sys->b_box_cleared = VLC_FALSE;
200
    p_sys->i_box_plidx = 0;
201
    p_sys->p_plnode = NULL;
202
    p_sys->i_box_bidx = 0;
203
    p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
204 205

    /* Initialize the curses library */
206 207
    p_sys->w = initscr();
    keypad( p_sys->w, TRUE );
208 209 210 211 212 213 214 215 216 217 218 219
    /* Don't do NL -> CR/NL */
    nonl();
    /* Take input chars one at a time */
    cbreak();
    /* Don't echo */
    noecho();

    curs_set(0);
    timeout(0);

    clear();

220 221 222
    /* exported function */
    p_intf->pf_run = Run;

223 224 225 226
    /* Set quiet mode */
    val.i_int = -1;
    var_Set( p_intf->p_vlc, "verbose", val );

227
    /* Set defaul playlist view */
zorglub's avatar
zorglub committed
228
    p_sys->i_current_view = VIEW_CATEGORY;
229 230 231 232
    p_sys->pp_plist = NULL;
    p_sys->i_plist_entries = 0;
    p_sys->b_need_update = VLC_FALSE;

233 234 235 236 237 238 239
    /* Initialize search chain */
    p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
    p_sys->psz_old_search = NULL;
    p_sys->i_before_search = 0;

    /* Initialize open chain */
    p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
240

hartman's avatar
hartman committed
241 242 243
    /* Initialize browser options */
    var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_intf, "browse-dir", &val);
244

hartman's avatar
hartman committed
245 246 247 248 249 250 251 252 253
    if( val.psz_string && *val.psz_string )
    {
        p_sys->psz_current_dir = strdup( val.psz_string);
        free( val.psz_string );
    }
    else
    {
        p_sys->psz_current_dir = strdup( p_intf->p_vlc->psz_homedir );
    }
254

hartman's avatar
hartman committed
255
    p_sys->i_dir_entries = 0;
256
    p_sys->pp_dir_entries = NULL;
257
    p_sys->b_show_hidden_files = VLC_FALSE;
hartman's avatar
hartman committed
258
    ReadDir( p_intf );
259

260
    return VLC_SUCCESS;
261 262 263 264 265 266
}

/*****************************************************************************
 * Close: destroy interface window
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
267
{
268
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
269
    intf_sys_t    *p_sys = p_intf->p_sys;
hartman's avatar
hartman committed
270
    int i;
271

272 273 274 275 276 277 278
    var_DelCallback( p_sys->p_playlist, "intf-change", PlaylistChanged,
                     p_intf );
    var_DelCallback( p_sys->p_playlist, "item-append", PlaylistChanged,
                     p_intf );

    PlaylistDestroy( p_intf );

hartman's avatar
hartman committed
279 280 281 282 283 284 285 286
    for( i = 0; i < p_sys->i_dir_entries; i++ )
    {
        struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
        if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
        REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
        if( p_dir_entry ) free( p_dir_entry );
    }
    p_sys->pp_dir_entries = NULL;
287

hartman's avatar
hartman committed
288
    if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
289 290 291 292
    if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
    if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
    if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );

293 294 295 296 297
    if( p_sys->p_input )
    {
        vlc_object_release( p_sys->p_input );
    }
    if( p_sys->p_playlist )
298
    {
299
        vlc_object_release( p_sys->p_playlist );
300 301 302 303 304
    }

    /* Close the ncurses interface */
    endwin();

305 306
    msg_Unsubscribe( p_intf, p_sys->p_sub );

307
    /* Destroy structure */
308
    free( p_sys );
309 310 311 312 313 314 315
}

/*****************************************************************************
 * Run: ncurses thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
316 317 318
    intf_sys_t    *p_sys = p_intf->p_sys;

    int i_key;
319 320 321 322 323 324 325 326 327 328 329
    time_t t_last_refresh;

    /*
     * force drawing the interface for the first time
     */
    t_last_refresh = ( time( 0 ) - 1);

    while( !p_intf->b_die )
    {
        msleep( INTF_IDLE_SLEEP );

330
        /* Update the input */
331
        if( p_sys->p_playlist == NULL )
332
        {
333 334 335 336 337 338 339 340 341
            p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                 FIND_ANYWHERE );
            if( p_sys->p_playlist )
            {
                var_AddCallback( p_sys->p_playlist, "intf-change",
                                 PlaylistChanged, p_intf );
                var_AddCallback( p_sys->p_playlist, "item-append",
                                 PlaylistChanged, p_intf );
            }
342
        }
343
        if( p_sys->p_playlist )
344
        {
345 346
            vlc_mutex_lock( &p_sys->p_playlist->object_lock );
            if( p_sys->p_input == NULL )
347
            {
348 349 350 351 352 353 354 355
                p_sys->p_input = p_sys->p_playlist->p_input;
                if( p_sys->p_input )
                {
                    if( !p_sys->p_input->b_dead )
                    {
                        vlc_object_yield( p_sys->p_input );
                    }
                }
356
            }
357
            else if( p_sys->p_input->b_dead )
358
            {
359 360 361 362
                vlc_object_release( p_sys->p_input );
                p_sys->p_input = NULL;
                p_sys->f_slider = p_sys->f_slider_old = 0.0;
                p_sys->b_box_cleared = VLC_FALSE;
363
            }
364
            vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
365
        }
366

367
        if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 )
368
        {
369
            FindIndex( p_intf );
370
        }
371

372
        while( ( i_key = getch()) != -1 )
373 374 375 376
        {
            /*
             * HandleKey returns 1 if the screen needs to be redrawn
             */
377
            if( HandleKey( p_intf, i_key ) )
378 379 380 381
            {
                Redraw( p_intf, &t_last_refresh );
            }
        }
382 383 384 385 386 387 388
        /* Hack */
        if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
        {
            clear();
            Redraw( p_intf, &t_last_refresh );
            p_sys->b_box_cleared = VLC_TRUE;
        }
389 390 391 392

        /*
         * redraw the screen every second
         */
393
        if( (time(0) - t_last_refresh) >= 1 )
394
        {
395
            ManageSlider( p_intf );
396 397 398 399 400 401 402 403 404
            Redraw( p_intf, &t_last_refresh );
        }
    }
}

/* following functions are local */

static int HandleKey( intf_thread_t *p_intf, int i_key )
{
405 406 407 408 409
    intf_sys_t *p_sys = p_intf->p_sys;
    vlc_value_t val;

    if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
    {
410 411
        int b_ret = VLC_TRUE;

412 413
        switch( i_key )
        {
hartman's avatar
hartman committed
414 415
            vlc_value_t val;
            /* Playlist Settings */
416
            case 'r':
hartman's avatar
hartman committed
417 418 419 420 421 422 423 424
                var_Get( p_sys->p_playlist, "random", &val );
                val.b_bool = !val.b_bool;
                var_Set( p_sys->p_playlist, "random", val );
                return 1;
            case 'l':
                var_Get( p_sys->p_playlist, "loop", &val );
                val.b_bool = !val.b_bool;
                var_Set( p_sys->p_playlist, "loop", val );
425
                return 1;
hartman's avatar
hartman committed
426 427 428 429 430 431 432
            case 'R':
                var_Get( p_sys->p_playlist, "repeat", &val );
                val.b_bool = !val.b_bool;
                var_Set( p_sys->p_playlist, "repeat", val );
                return 1;

            /* Playlist sort */
433
            case 'o':
434
                playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_NORMAL );
435 436
                return 1;
            case 'O':
437
                playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_REVERSE );
438 439
                return 1;

440 441 442 443 444 445 446 447
            /* Playlist view */
            case 'v':
                switch( p_sys->i_current_view )
                {
                    case VIEW_CATEGORY:
                        p_sys->i_current_view = VIEW_ALL;
                        break;
                    default:
zorglub's avatar
zorglub committed
448
                        p_sys->i_current_view = VIEW_CATEGORY;
449 450 451 452 453
                }
                PlaylistRebuild( p_intf );
                FindIndex( p_intf );
                return 1;

454 455
            /* Playlist navigation */
            case KEY_HOME:
456 457
                p_sys->i_box_plidx = 0;
                break;
458
            case KEY_END:
459 460
                p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
                break;
461
            case KEY_UP:
462 463
                p_sys->i_box_plidx--;
                break;
464
            case KEY_DOWN:
465 466
                p_sys->i_box_plidx++;
                break;
467
            case KEY_PPAGE:
468 469
                p_sys->i_box_plidx -= p_sys->i_box_lines;
                break;
470
            case KEY_NPAGE:
471 472
                p_sys->i_box_plidx += p_sys->i_box_lines;
                break;
hartman's avatar
hartman committed
473
            case 'D':
474 475 476 477
            case KEY_BACKSPACE:
            case KEY_DC:
            {
                int i_item = p_sys->p_playlist->i_index;
478

479 480 481
                playlist_LockDelete( p_sys->p_playlist, p_sys->i_box_plidx );
                if( i_item < p_sys->p_playlist->i_size &&
                    i_item != p_sys->p_playlist->i_index )
482 483 484
                {
                    playlist_Goto( p_sys->p_playlist, i_item );
                }
485
                break;
486 487
            }

488 489
            case KEY_ENTER:
            case 0x0d:
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                if( p_sys->i_current_view == VIEW_ALL )
                {
                    playlist_Goto( p_sys->p_playlist, p_sys->i_box_plidx );
                }
                else
                {
                    if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
                                == -1 )
                    {
                        playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
                            p_sys->pp_plist[p_sys->i_box_plidx]->p_item );
                    }
                    else
                    {
                        playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
                            p_sys->i_current_view,
                            p_sys->pp_plist[p_sys->i_box_plidx]->p_item,
                            NULL );
                    }
                }
                p_sys->b_box_plidx_follow = VLC_TRUE;
511
                break;
512
            default:
513
                b_ret = VLC_FALSE;
514 515
                break;
        }
516 517 518

        if( b_ret )
        {
519 520 521 522
            int i_max = p_sys->i_plist_entries;
            if( p_sys->i_current_view == VIEW_ALL )
                i_max = p_sys->p_playlist->i_size;
            if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
523
            if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
524 525 526 527 528 529 530
            if( p_sys->i_current_view == VIEW_ALL )
            {
                if( p_sys->i_box_plidx == p_sys->p_playlist->i_index )
                    p_sys->b_box_plidx_follow = VLC_TRUE;
                else
                    p_sys->b_box_plidx_follow = VLC_FALSE;
            }
531
            else
532 533 534 535 536 537 538
            {
                if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item ==
                        p_sys->p_playlist->status.p_item )
                    p_sys->b_box_plidx_follow = VLC_TRUE;
                else
                    p_sys->b_box_plidx_follow = VLC_FALSE;
            }
539 540
            return 1;
        }
541
    }
hartman's avatar
hartman committed
542 543
    if( p_sys->i_box_type == BOX_BROWSE )
    {
544
        vlc_bool_t b_ret = VLC_TRUE;
hartman's avatar
hartman committed
545 546 547 548
        /* Browser navigation */
        switch( i_key )
        {
            case KEY_HOME:
549
                p_sys->i_box_bidx = 0;
hartman's avatar
hartman committed
550 551
                break;
            case KEY_END:
552
                p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
hartman's avatar
hartman committed
553 554
                break;
            case KEY_UP:
555
                p_sys->i_box_bidx--;
hartman's avatar
hartman committed
556 557
                break;
            case KEY_DOWN:
558
                p_sys->i_box_bidx++;
hartman's avatar
hartman committed
559 560
                break;
            case KEY_PPAGE:
561
                p_sys->i_box_bidx -= p_sys->i_box_lines;
hartman's avatar
hartman committed
562 563
                break;
            case KEY_NPAGE:
564
                p_sys->i_box_bidx += p_sys->i_box_lines;
hartman's avatar
hartman committed
565
                break;
566 567 568 569 570
            case '.': /* Toggle show hidden files */
                p_sys->b_show_hidden_files = ( p_sys->b_show_hidden_files ==
                    VLC_TRUE ? VLC_FALSE : VLC_TRUE );
                ReadDir( p_intf );
                break;
hartman's avatar
hartman committed
571 572 573

            case KEY_ENTER:
            case 0x0d:
574 575
            case ' ':
                if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
hartman's avatar
hartman committed
576 577
                {
                    int i_size_entry = strlen( p_sys->psz_current_dir ) +
578
                                       strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
hartman's avatar
hartman committed
579 580
                    char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);

581
                    sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
hartman's avatar
hartman committed
582 583 584 585 586 587 588 589 590
                    playlist_Add( p_sys->p_playlist, psz_uri,
                                  psz_uri,
                                  PLAYLIST_APPEND, PLAYLIST_END );
                    p_sys->i_box_type = BOX_PLAYLIST;
                    free( psz_uri );
                }
                else
                {
                    int i_size_entry = strlen( p_sys->psz_current_dir ) +
591
                                       strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
hartman's avatar
hartman committed
592 593
                    char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);

594
                    sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
595

hartman's avatar
hartman committed
596 597 598 599 600 601
                    p_sys->psz_current_dir = strdup( psz_uri );
                    ReadDir( p_intf );
                    free( psz_uri );
                }
                break;
            default:
602
                b_ret = VLC_FALSE;
hartman's avatar
hartman committed
603
                break;
604 605 606
        }
        if( b_ret )
        {
607 608
            if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
            if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
hartman's avatar
hartman committed
609 610 611
            return 1;
        }
    }
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
    {
        switch( i_key )
        {
            case KEY_HOME:
                p_sys->i_box_start = 0;
                return 1;
            case KEY_END:
                p_sys->i_box_start = p_sys->i_box_lines_total - 1;
                return 1;
            case KEY_UP:
                if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
                return 1;
            case KEY_DOWN:
                if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
                {
                    p_sys->i_box_start++;
                }
                return 1;
            case KEY_PPAGE:
                p_sys->i_box_start -= p_sys->i_box_lines;
                if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
                return 1;
            case KEY_NPAGE:
                p_sys->i_box_start += p_sys->i_box_lines;
                if( p_sys->i_box_start >= p_sys->i_box_lines_total )
                {
                    p_sys->i_box_start = p_sys->i_box_lines_total - 1;
                }
                return 1;
            default:
                break;
        }
    }
    else if( p_sys->i_box_type == BOX_NONE )
    {
        switch( i_key )
        {
            case KEY_HOME:
                p_sys->f_slider = 0;
652
                ManageSlider( p_intf );
653 654 655
                return 1;
            case KEY_END:
                p_sys->f_slider = 99.9;
656
                ManageSlider( p_intf );
657 658
                return 1;
            case KEY_UP:
659
                p_sys->f_slider += 5.0;
660
                if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
661
                ManageSlider( p_intf );
662 663
                return 1;
            case KEY_DOWN:
664
                p_sys->f_slider -= 5.0;
665
                if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
666
                ManageSlider( p_intf );
667 668 669 670 671 672
                return 1;

            default:
                break;
        }
    }
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
    {
        int i_chain_len;
        i_chain_len = strlen( p_sys->psz_search_chain );
        switch( i_key )
        {
            case 0x0c:      /* ^l */
                clear();
                return 1;
            case KEY_ENTER:
            case 0x0d:
                if( i_chain_len > 0 )
                {
                    p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
                }
                else if( p_sys->psz_old_search )
                {
                    SearchPlaylist( p_intf, p_sys->psz_old_search );
                }
                p_sys->i_box_type = BOX_PLAYLIST;
                return 1;
            case 0x1b:      /* Esc. */
                p_sys->i_box_plidx = p_sys->i_before_search;
                p_sys->i_box_type = BOX_PLAYLIST;
                return 1;
            case KEY_BACKSPACE:
                if( i_chain_len > 0 )
                {
                    p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
                }
                break;
            default:
                if( i_chain_len < SEARCH_CHAIN_SIZE )
                {
                    p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
                    p_sys->psz_search_chain[ i_chain_len ] = 0;
                }
                break;
        }
        if( p_sys->psz_old_search )
        {
            free( p_sys->psz_old_search );
            p_sys->psz_old_search = NULL;
        }
        SearchPlaylist( p_intf, p_sys->psz_search_chain );
        return 1;
    }
    else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
    {
722
        int i_chain_len = strlen( p_sys->psz_open_chain );
723 724 725 726 727 728 729 730 731
        playlist_t *p_playlist = p_sys->p_playlist;

        switch( i_key )
        {
            case 0x0c:      /* ^l */
                clear();
                return 1;
            case KEY_ENTER:
            case 0x0d:
732
                if( p_playlist && i_chain_len > 0 )
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
                {
                    playlist_Add( p_playlist, p_sys->psz_open_chain,
                                  p_sys->psz_open_chain,
                                  PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
                    p_sys->b_box_plidx_follow = VLC_TRUE;
                }
                p_sys->i_box_type = BOX_PLAYLIST;
                return 1;
            case 0x1b:      /* Esc. */
                p_sys->i_box_type = BOX_PLAYLIST;
                return 1;
            case KEY_BACKSPACE:
                if( i_chain_len > 0 )
                {
                    p_sys->psz_open_chain[ i_chain_len - 1 ] = '\0';
                }
                break;
            default:
                if( i_chain_len < OPEN_CHAIN_SIZE )
                {
                    p_sys->psz_open_chain[ i_chain_len++ ] = i_key;
                    p_sys->psz_open_chain[ i_chain_len ] = 0;
                }
                break;
        }
        return 1;
    }

761 762

    /* Common keys */
763 764 765 766
    switch( i_key )
    {
        case 'q':
        case 'Q':
767
        case 0x1b:  /* Esc */
hartman's avatar
hartman committed
768
            p_intf->p_vlc->b_die = VLC_TRUE;
769 770
            return 0;

771 772 773 774 775 776 777 778
        /* Box switching */
        case 'i':
            if( p_sys->i_box_type == BOX_INFO )
                p_sys->i_box_type = BOX_NONE;
            else
                p_sys->i_box_type = BOX_INFO;
            p_sys->i_box_lines_total = 0;
            return 1;
hartman's avatar
hartman committed
779
        case 'L':
780 781 782 783 784 785 786 787 788 789 790
            if( p_sys->i_box_type == BOX_LOG )
                p_sys->i_box_type = BOX_NONE;
            else
                p_sys->i_box_type = BOX_LOG;
            return 1;
        case 'P':
            if( p_sys->i_box_type == BOX_PLAYLIST )
                p_sys->i_box_type = BOX_NONE;
            else
                p_sys->i_box_type = BOX_PLAYLIST;
            return 1;
hartman's avatar
hartman committed
791 792 793 794 795 796
        case 'B':
            if( p_sys->i_box_type == BOX_BROWSE )
                p_sys->i_box_type = BOX_NONE;
            else
                p_sys->i_box_type = BOX_BROWSE;
            return 1;
797 798 799 800 801 802 803
        case 'h':
        case 'H':
            if( p_sys->i_box_type == BOX_HELP )
                p_sys->i_box_type = BOX_NONE;
            else
                p_sys->i_box_type = BOX_HELP;
            p_sys->i_box_lines_total = 0;
804
            return 1;
805 806 807 808 809 810 811 812 813 814 815 816 817
        case '/':
            if( p_sys->i_box_type != BOX_SEARCH )
            {
                if( p_sys->psz_search_chain == NULL )
                {
                    return 1;
                }
                p_sys->psz_search_chain[0] = '\0';
                p_sys->b_box_plidx_follow = VLC_FALSE;
                p_sys->i_before_search = p_sys->i_box_plidx;
                p_sys->i_box_type = BOX_SEARCH;
            }
            return 1;
hartman's avatar
hartman committed
818
        case 'A': /* Open */
819 820 821 822 823 824 825 826 827 828
            if( p_sys->i_box_type != BOX_OPEN )
            {
                if( p_sys->psz_open_chain == NULL )
                {
                    return 1;
                }
                p_sys->psz_open_chain[0] = '\0';
                p_sys->i_box_type = BOX_OPEN;
            }
            return 1;
829

830 831
        /* Navigation */
        case KEY_RIGHT:
832
            p_sys->f_slider += 1.0;
833
            if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
834
            ManageSlider( p_intf );
835 836
            return 1;

837
        case KEY_LEFT:
838
            p_sys->f_slider -= 1.0;
839
            if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
840
            ManageSlider( p_intf );
841 842
            return 1;

843 844 845
        /* Common control */
        case 'f':
        {
sigmunau's avatar
sigmunau committed
846
            if( p_intf->p_sys->p_input )
847
            {
848
                vout_thread_t *p_vout;
sigmunau's avatar
sigmunau committed
849 850 851 852
                p_vout = vlc_object_find( p_intf->p_sys->p_input,
                                          VLC_OBJECT_VOUT, FIND_CHILD );
                if( p_vout )
                {
853 854 855
                    var_Get( p_vout, "fullscreen", &val );
                    val.b_bool = !val.b_bool;
                    var_Set( p_vout, "fullscreen", val );
sigmunau's avatar
sigmunau committed
856 857
                    vlc_object_release( p_vout );
                }
858 859 860 861 862 863 864 865 866 867 868 869 870
                else
                {
                    playlist_t *p_playlist;
                    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                  FIND_ANYWHERE );
                    if( p_playlist )
                    {
                        var_Get( p_playlist, "fullscreen", &val );
                        val.b_bool = !val.b_bool;
                        var_Set( p_playlist, "fullscreen", val );
                        vlc_object_release( p_playlist );
                    }
                }
871 872 873 874 875 876
            }
            return 0;
        }

        case ' ':
            PlayPause( p_intf );
877 878
            return 1;

879 880 881 882 883
        case 's':
            if( p_intf->p_sys->p_playlist )
            {
                playlist_Stop( p_intf->p_sys->p_playlist );
            }
884 885 886 887 888 889 890
            return 1;

        case 'e':
            Eject( p_intf );
            return 1;

        case '[':
891 892 893 894 895
            if( p_sys->p_input )
            {
                val.b_bool = VLC_TRUE;
                var_Set( p_sys->p_input, "prev-title", val );
            }
896
            return 1;
897 898

        case ']':
899 900 901 902 903
            if( p_sys->p_input )
            {
                val.b_bool = VLC_TRUE;
                var_Set( p_sys->p_input, "next-title", val );
            }
904
            return 1;
905 906

        case '<':
907 908 909 910 911
            if( p_sys->p_input )
            {
                val.b_bool = VLC_TRUE;
                var_Set( p_sys->p_input, "prev-chapter", val );
            }
912
            return 1;
913 914

        case '>':
915 916 917 918 919
            if( p_sys->p_input )
            {
                val.b_bool = VLC_TRUE;
                var_Set( p_sys->p_input, "next-chapter", val );
            }
920
            return 1;
921

922 923 924 925 926 927 928
        case 'p':
            if( p_intf->p_sys->p_playlist )
            {
                playlist_Prev( p_intf->p_sys->p_playlist );
            }
            clear();
            return 1;
hartman's avatar
hartman committed
929

930 931 932 933 934 935 936
        case 'n':
            if( p_intf->p_sys->p_playlist )
            {
                playlist_Next( p_intf->p_sys->p_playlist );
            }
            clear();
            return 1;
937

hartman's avatar
hartman committed
938 939 940 941 942 943 944 945 946 947
        case 'a':
            aout_VolumeUp( p_intf, 1, NULL );
            clear();
            return 1;

        case 'z':
            aout_VolumeDown( p_intf, 1, NULL );
            clear();
            return 1;

948 949 950 951 952 953 954 955
        /*
         * ^l should clear and redraw the screen
         */
        case 0x0c:
            clear();
            return 1;

        default:
956 957 958 959
            return 0;
    }
}

960
static void ManageSlider( intf_thread_t *p_intf )
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
{
    intf_sys_t     *p_sys = p_intf->p_sys;
    input_thread_t *p_input = p_sys->p_input;
    vlc_value_t     val;

    if( p_input == NULL )
    {
        return;
    }
    var_Get( p_input, "state", &val );
    if( val.i_int != PLAYING_S )
    {
        return;
    }

    var_Get( p_input, "position", &val );
    if( p_sys->f_slider == p_sys->f_slider_old )
    {
        p_sys->f_slider =
        p_sys->f_slider_old = 100 * val.f_float;
981
    }
982 983 984
    else
    {
        p_sys->f_slider_old = p_sys->f_slider;
985

986 987 988
        val.f_float = p_sys->f_slider / 100.0;
        var_Set( p_input, "position", val );
    }
989 990
}

991 992
static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
{
993
    int i_max;
994 995 996 997 998 999
    int i_first = 0 ;
    int i_item = -1;
    intf_sys_t *p_sys = p_intf->p_sys;
    playlist_t *p_playlist = p_sys->p_playlist;

    if( p_sys->i_before_search >= 0 )
1000
    {
1001
        i_first = p_sys->i_before_search;
1002
    }
1003 1004 1005 1006 1007 1008 1009

    if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
    {
        p_sys->i_box_plidx = p_sys->i_before_search;
        return;
    }

1010 1011 1012 1013 1014
    i_max = p_sys->i_current_view == VIEW_ALL ?
                p_playlist->i_size : p_sys->i_plist_entries;

    i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
    if( i_item < 0 )
1015
    {
1016
        i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1017
    }
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

    if( i_item < 0 || i_item >= i_max ) return;

    p_sys->i_box_plidx = i_item;
}

static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
                              int i_start, int i_stop )
{
    intf_sys_t *p_sys = p_intf->p_sys;
    playlist_t *p_playlist = p_sys->p_playlist;
    int i, i_item = -1;

    if( p_sys->i_current_view == VIEW_ALL )
1032
    {
1033
        for( i = i_start + 1; i < i_stop; i++ )
1034
        {
1035
            if( strcasestr( p_playlist->pp_items[i]->input.psz_name,
1036
                            psz_searchstring ) != NULL
1037
                || strcasestr( p_playlist->pp_items[i]->input.psz_uri,
1038 1039
                               psz_searchstring ) != NULL )
            {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
                i_item = i;
                break;
            }
        }
    }
    else
    {
        for( i = i_start + 1; i < i_stop; i++ )
        {
            if( strcasestr( p_sys->pp_plist[i]->psz_display,
                            psz_searchstring ) != NULL )
            {
                i_item = i;
1053 1054 1055 1056 1057