⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ncursesui.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________
        
        FreeAmp - The Free MP3 Player

        Copyright (C) 1999 Stephan Auerhahn

        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., 675 Mass Ave, Cambridge, MA 02139, USA.
        
----------------------------------------------------------------------------
ncurses user interface by stephan auerhahn (palpatine@midi.net)

this is a hacked up version of cmdlineUI.cpp
____________________________________________________________________________*/

#include <iostream>
#include <stdio.h>
#include <unistd.h>

#include <sys/time.h>
#include <termios.h>
#include <signal.h>

#include "config.h"
#include "ncursesUI.h"
#include "event.h"
#include "playlist.h"
#include "thread.h"
#include "eventdata.h"

#define stdinfd 0

extern "C" {

UserInterface *Initialize(FAContext *context) {
    return new ncursesUI(context);
}

           }
	   
static struct termios normalTTY;
static struct termios rawTTY;

int getKey() {
    fd_set rdfs;

    FD_ZERO(&rdfs);
    FD_SET(stdinfd, &rdfs);
    if (select (stdinfd+1,&rdfs,NULL,NULL,NULL) == 1) {
        if (FD_ISSET(stdinfd, &rdfs)) return 1;
    }
    return 0;
}


ncursesUI::ncursesUI(FAContext *context) {
    m_context = context;
    m_plm = NULL;
    m_playerEQ = NULL;
    m_lastIndexPlayed = -1;
    m_id3InfoPrinted = false;
    helpwin = NULL;
    helpwin_open = false;
    playlistwin = NULL;
    playlistwin_open = false;
/* This stuff seems to mess up some events - palp
    m_mediaInfo_set = false;
    m_mediaInfo = NULL;
    m_mpegInfo_set = false;
    totalFrames = 0; */
    totalTime = 0;
    keyboardListenThread = NULL;
}

Error ncursesUI::Init(int32 startup_level) {
    cursesStarted = false;
    if ((m_startupLevel = startup_level) == PRIMARY_UI) {
        tcgetattr(stdinfd, &::normalTTY);
        ::rawTTY = ::normalTTY;
        ::rawTTY.c_lflag &= ~ICANON;
        ::rawTTY.c_lflag &= ~ECHO;
        tcsetattr(stdinfd, TCSANOW, &rawTTY);

        keyboardListenThread = Thread::CreateThread();
        keyboardListenThread->Create(ncursesUI::keyboardServiceFunction,this);

        /* curses initialization */
        initscr();
        cbreak();
        noecho();
        nonl();
        intrflush( stdscr, false );
        keypad( stdscr, true );
        start_color();
        cursesStarted = true;
        init_pair(1, COLOR_BLUE, COLOR_BLACK);
        init_pair(2, COLOR_GREEN, COLOR_BLACK);
        init_pair(3, COLOR_RED, COLOR_BLACK);
        init_pair(4, COLOR_CYAN, COLOR_BLACK);
        init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
        init_pair(6, COLOR_YELLOW, COLOR_BLACK);
        init_pair(7, COLOR_WHITE, COLOR_BLACK);
        init_pair(8, COLOR_BLACK, COLOR_BLACK);
        curs_set(0);

        showInfo();

        m_plm = m_context->plm;
        m_propManager = m_context->props;
        m_playerEQ = m_context->target;

        m_argc = m_context->argc;
        m_argv = m_context->argv;

        ProcessArgs();
    }
    return kError_NoErr;
}

ncursesUI::~ncursesUI() {
    if (cursesStarted) {
        curs_set(1);
        move(0,0);
        clear();
        refresh();
        endwin();
    }
    if (m_startupLevel == PRIMARY_UI) {
        tcsetattr(stdinfd, TCSANOW, &normalTTY);
    }
    //cout << "ncursesUI: begin deleted..." << endl;
    if (keyboardListenThread) {
        keyboardListenThread->Destroy();
        delete keyboardListenThread;
        keyboardListenThread = NULL;
    }
//    delete m_mediaInfo;
}

void ncursesUI::keyboardServiceFunction(void *pclcio) {
    ncursesUI *pMe = (ncursesUI *)pclcio;
    //char *pkey = new char[1];
    char chr;
    //size_t rtn;
    //int fn = STDIN_FILENO;
    for (;;) {
        ::getKey();
        read(stdinfd,&chr,1);
        switch (chr) {
            case 'p':
            case 'P': {
                Event *e = new Event(CMD_TogglePause);
                pMe->m_playerEQ->AcceptEvent(e);
                break;
            }
            case '-': {
                Event *e = new Event(CMD_PrevMediaPiece);
                pMe->m_playerEQ->AcceptEvent(e);
                break;
            }
            case '=':
            case '+':
            case 'n':
            case 'N': {
                Event *e = new Event(CMD_NextMediaPiece);
                pMe->m_playerEQ->AcceptEvent(e);
                break; }
            case 'q':
            case 'Q': {
                Event *e = new Event(CMD_QuitPlayer);
                pMe->m_playerEQ->AcceptEvent(e);
                clear();
                move(0,0);
                endwin();
                break; }
            case 's':
            case 'S': {
                if (pMe->m_plm) {
                    pMe->m_plm->SetShuffleMode(true);
                    pMe->m_plm->SetCurrentIndex(0);
                }
                Event *e = new Event(CMD_Stop);
                pMe->m_playerEQ->AcceptEvent(e);
                e = new Event(CMD_Play);
                pMe->m_playerEQ->AcceptEvent(e);
                break;}
            case 'h':
            case 'H': {
                pMe->help();
                break;}
//            case '/': {
//                pMe->playlist();
//                break;}
//          case 'f':{
//              Event *e = new Event(CMD_ChangePosition,(void *)200);
//              pMe->m_playerEQ->AcceptEvent(pMe->m_playerEQ,e);
//          }
            default:
                break;
        }
    }

}

Error ncursesUI::AcceptEvent(Event *e) {
    if (e) {
        switch (e->Type()) {
            case INFO_PlaylistDonePlay: {
                Event *e = new Event(CMD_QuitPlayer);
                m_playerEQ->AcceptEvent(e);
                break; }
            case CMD_Cleanup: {
                if (m_startupLevel == PRIMARY_UI) 
                   tcsetattr(stdinfd, TCSANOW, &normalTTY);
                Event *e = new Event(INFO_ReadyToDieUI);
                m_playerEQ->AcceptEvent(e);
                break; }
            case INFO_MPEGInfo: {
                MpegInfoEvent *mie = (MpegInfoEvent *)e;
                if (mie) {
                    char buf[1024];
                    sprintf(buf, "%d kbps", mie->GetBitRate() / 1000);
                    move(LINES - 6, COLS - strlen(buf));
                    addstr(buf);
                    sprintf(buf, "%d kHz %s", mie->GetSampleRate() / 1000,
                                              mie->GetStereo() < 4 ? "stereo" : "mono");
                    move(LINES - 5, COLS - strlen(buf));
                    addstr(buf);
                    sprintf(buf, "MPEG-%s layer %d",
                                 mie->GetMpegVersion() == 1 ? "1" :
                                 mie->GetMpegVersion() == 2 ? "2" : "2.5",
                                 mie->GetLayer());
                    move(LINES - 4, COLS - strlen(buf));
                    addstr(buf);
                    refresh();
                }
                break;
            }
            case INFO_MediaInfo: {
                MediaInfoEvent *pmvi = (MediaInfoEvent *)e;
                if (pmvi)
                {
//                    m_mediaInfo = new MediaInfoEvent(*pmvi);
                    totalTime = pmvi->m_totalSeconds;
//                    m_mediaInfo_set = true;
                }
                if (pmvi && m_lastIndexPlayed != pmvi->m_indexOfSong) {
                    const PlaylistItem *pItem;
                    MetaData md;
                    char buf[1024];                    
                    m_lastIndexPlayed = pmvi->m_indexOfSong;
//                    m_id3InfoPrinted = false;
                    pItem = m_plm->GetCurrentItem();
                    if (!pItem)
                       break;
                    md = pItem->GetMetaData();

                    clear();
                    move(0,0);
                    showInfo();
                    move(2, 0);
                    addstr("Title  : ");
                    if (md.Title().c_str()[0] != '\0')
		        addstr((char *)md.Title().c_str());
		    else
                        addstr(pmvi->m_filename);
                    addstr("\nArtist : ");
                    addstr((char *)md.Artist().c_str());
                    addstr("\nAlbum  : ");
                    addstr((char *)md.Album().c_str());
                    addstr("\nYear   : ");
                    if (md.Year() != 0)
		    {
			sprintf(buf, "%d", md.Year());
		    	addstr(buf);
                    }
                    addstr("\nGenre  : ");
                    addstr((char *)md.Genre().c_str());
                    addstr("\nTrack  : ");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -