📄 ncurses.c
字号:
/***************************************************************************** * ncurses.c : NCurses plugin for vlc ***************************************************************************** * Copyright (C) 2001-2006 the VideoLAN team * $Id: ncurses.c 18329 2006-12-08 18:07:54Z hartman $ * * Authors: Sam Hocevar <sam@zoy.org> * Laurent Aimar <fenrir@via.ecp.fr> * Yoann Peronneau <yoann@videolan.org> * Derk-Jan Hartman <hartman at videolan dot org> * * 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. * * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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>#include <vlc/aout.h>#include "charset.h"#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#ifdef HAVE_CDDAX#define CDDA_MRL "cddax://"#else#define CDDA_MRL "cdda://"#endif#ifdef HAVE_VCDX#define VCD_MRL "vcdx://"#else#define VCD_MRL "vcdx://"#endif#define SEARCH_CHAIN_SIZE 20#define OPEN_CHAIN_SIZE 50/***************************************************************************** * Local prototypes. *****************************************************************************/static int Open ( vlc_object_t * );static void Close ( vlc_object_t * );static void Run ( intf_thread_t * );static void PlayPause ( intf_thread_t * );static void Eject ( intf_thread_t * );static int HandleKey ( intf_thread_t *, int );static void Redraw ( intf_thread_t *, time_t * );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 * );static void SearchPlaylist ( intf_thread_t *, char * );static int SubSearchPlaylist( intf_thread_t *, char *, int, int );static void ManageSlider ( intf_thread_t * );static void ReadDir ( intf_thread_t * );/***************************************************************************** * Module descriptor *****************************************************************************/#define BROWSE_TEXT N_("Filebrowser starting point")#define BROWSE_LONGTEXT N_( \ "This option allows you to specify the directory the ncurses filebrowser " \ "will show you initially.")vlc_module_begin(); set_shortname( "Ncurses" ); set_description( _("Ncurses interface") ); set_capability( "interface", 10 ); set_category( CAT_INTERFACE ); set_subcategory( SUBCAT_INTERFACE_MAIN ); set_callbacks( Open, Close ); add_shortcut( "curses" ); add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );vlc_module_end();/***************************************************************************** * intf_sys_t: description and status of ncurses interface *****************************************************************************/enum{ BOX_NONE, BOX_HELP, BOX_INFO, BOX_LOG, BOX_PLAYLIST, BOX_SEARCH, BOX_OPEN, BOX_BROWSE};struct dir_entry_t{ vlc_bool_t b_file; char *psz_path;};struct pl_item_t{ playlist_item_t *p_item; char *psz_display;};struct intf_sys_t{ 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; int i_box_plidx; /* Playlist index */ int b_box_plidx_follow; playlist_item_t *p_plnode; /* Playlist node */ int i_box_bidx; /* browser index */ int b_box_cleared; msg_subscription_t* p_sub; /* message bank subscription */ char *psz_search_chain; /* for playlist searching */ char *psz_old_search; /* for searching next */ int i_before_search; char *psz_open_chain; char psz_partial_keys[7]; char *psz_current_dir; int i_dir_entries; struct dir_entry_t **pp_dir_entries; vlc_bool_t b_show_hidden_files; 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 */};static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title );static void DrawLine( WINDOW *win, int y, int x, int w );static void DrawEmptyLine( WINDOW *win, int y, int x, int w );/***************************************************************************** * Open: initialize and create window *****************************************************************************/static int Open( vlc_object_t *p_this ){ intf_thread_t *p_intf = (intf_thread_t *)p_this; intf_sys_t *p_sys; vlc_value_t val; /* Allocate instance and initialize some members */ 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; p_sys->b_box_plidx_follow = VLC_TRUE; p_sys->b_box_cleared = VLC_FALSE; p_sys->i_box_plidx = 0; p_sys->p_plnode = NULL; p_sys->i_box_bidx = 0; p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL ); memset( p_sys->psz_partial_keys, 0, sizeof( p_sys->psz_partial_keys ) ); /* Initialize the curses library */ p_sys->w = initscr(); keypad( p_sys->w, TRUE ); /* 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(); /* exported function */ p_intf->pf_run = Run; /* Set quiet mode */ val.i_int = -1; var_Set( p_intf->p_vlc, "verbose", val ); /* Set defaul playlist view */ p_sys->i_current_view = VIEW_CATEGORY; p_sys->pp_plist = NULL; p_sys->i_plist_entries = 0; p_sys->b_need_update = VLC_FALSE; /* 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 ); /* Initialize browser options */ var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); var_Get( p_intf, "browse-dir", &val); 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 ); } p_sys->i_dir_entries = 0; p_sys->pp_dir_entries = NULL; p_sys->b_show_hidden_files = VLC_FALSE; ReadDir( p_intf ); return VLC_SUCCESS;}/***************************************************************************** * Close: destroy interface window *****************************************************************************/static void Close( vlc_object_t *p_this ){ intf_thread_t *p_intf = (intf_thread_t *)p_this; intf_sys_t *p_sys = p_intf->p_sys; int i; 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 ); 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; if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir ); 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 ); if( p_sys->p_input ) { vlc_object_release( p_sys->p_input ); } if( p_sys->p_playlist ) { vlc_object_release( p_sys->p_playlist ); } /* Close the ncurses interface */ endwin(); msg_Unsubscribe( p_intf, p_sys->p_sub ); /* Destroy structure */ free( p_sys );}/***************************************************************************** * Run: ncurses thread *****************************************************************************/static void Run( intf_thread_t *p_intf ){ intf_sys_t *p_sys = p_intf->p_sys; int i_key; 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 ); /* Update the input */ if( p_sys->p_playlist == NULL ) { 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 ); } } if( p_sys->p_playlist ) { vlc_mutex_lock( &p_sys->p_playlist->object_lock ); if( p_sys->p_input == NULL ) { 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 ); } } } else if( p_sys->p_input->b_dead ) { 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; } vlc_mutex_unlock( &p_sys->p_playlist->object_lock ); } if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 ) { FindIndex( p_intf ); } while( ( i_key = getch()) != -1 ) { /* * HandleKey returns 1 if the screen needs to be redrawn */ if( HandleKey( p_intf, i_key ) ) { Redraw( p_intf, &t_last_refresh ); } } /* 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; } /* * redraw the screen every second */ if( (time(0) - t_last_refresh) >= 1 ) { ManageSlider( p_intf ); Redraw( p_intf, &t_last_refresh ); } }}/* following functions are local */static char *KeyToUTF8( int i_key, char *psz_part ){ char *psz_utf8, *psz; int len = strlen( psz_part ); if( len == 6 ) { /* overflow error - should not happen */ memset( psz_part, 0, 6 ); return NULL; } psz_part[len] = (char)i_key; psz_utf8 = FromLocaleDup( psz_part );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -