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

📄 dde_ui.cpp

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

	Portions Copyright (C) 1998 EMusic.com

	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.
	
	$Id: DDE_UI.cpp,v 1.1 2000/04/17 19:41:41 elrod Exp $
____________________________________________________________________________*/

#include <windows.h>
#include <dde.h>
#include <stdio.h>
#include <math.h>

#include "DDE_UI.h"
#include "Playlist.h"
#include "event.h"
#include "eventdata.h"
#include "thread.h"

#define DDE_TIMEOUT	3000
#define STEP_SIZE	10
#define kAdviseFileName 	1
#define kAdviseSamplingFreq	2

HINSTANCE g_hInstance = NULL;

enum {
	kFastForward = 0,
	kFastReverse,
	kFileName,
	kNextTrack,
	kPause,
	kPlay,
	kPreviousTrack,
	kSamplingFreq,
	kShuffle,
	kStop
};

static const char *zCommands[11] = { "fast-forward", "fast-reverse", "fileName", "next", "pause",
	"play", "previous", "samplingFreq", "shuffle", "stop", NULL };

static long typeFromIndex[11] = {
	0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0
};

extern "C" DDE_UI *Initialize(FAContext *context)
{
    return new DDE_UI(context);
}


INT WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_being_called, LPVOID lpReserved)

{
	switch (ul_reason_being_called) {
		case DLL_PROCESS_ATTACH:
			g_hInstance = hInst;
	    	break;
		case DLL_THREAD_ATTACH:
		    break;
		case DLL_THREAD_DETACH:
		    break;
		case DLL_PROCESS_DETACH:
		    break;
	}
    return 1;                 
}


void DDE_UI::ParseArgs(int32 argc, char** argv)

{
/*    PlaylistManager	*playlist = plm;
    char         	*arg = NULL;
    bool         	shuffle = false;
    bool        	autoplay = false;
    int32         	count = 0;

    for(int32 i = 1;i < argc; i++)  {
	    arg = argv[i];

	    if (arg[0] == '-') {
	        switch (arg[1]) {
		        case 's':
                    shuffle = true;
		            break;
                case 'p':
                    autoplay = true;
		            break;
            }
        } else {
            playlist->AddItem(arg,0);
            count++;
	    }
    }

    playlist->SetCurrentIndex(0);

    if(shuffle) 
        playlist->SetShuffleMode(true);
*/
}


Error DDE_UI::Init(int32 startup_type) 

{ 
    ParseArgs(context->argc, context->argv);
    return kError_NoErr;
}


DDE_UI::DDE_UI(FAContext *theContext) 

{
	adviseList = NULL;
    context = theContext;
    plm = context->plm;
    target = context->target;
	winHwnd = NULL;
    uiThread = Thread::CreateThread();
    uiThread->Create(DDE_UI::UIThreadFunc,this);
}


DDE_UI::~DDE_UI() 

{
    if (uiThread) {
		uiThread->Destroy();
		delete uiThread;
		uiThread = NULL;
    }
}


void DDE_UI::CreateUI()

{
	WNDCLASSEX wndclass;

	wndclass.cbSize = sizeof(wndclass);
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = g_hInstance;
	wndclass.hIcon = NULL;
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = "FreeAmpDDE";
	wndclass.hIconSm = NULL;

	RegisterClassEx(&wndclass);

	winHwnd = CreateWindow("FreeAmpDDE", "FreeAmpDDE", WS_OVERLAPPEDWINDOW,
		                   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                           CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);

    if(winHwnd)
    {
        MSG msg;

        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}


void DDE_UI::UIThreadFunc(void *arg)

{
    DDE_UI	*ui = (DDE_UI*)arg;

    ui->CreateUI();
}


int DDE_UI::CommandSearch(const char *list[], int nCommands, char *name, int *key)

{
	int	i, j, k, cmp;

	i = 0;
	j = nCommands - 1;
	do {
		k = (i + j) >> 1;
		cmp = strcasecmp(list[k], name);
		if (cmp <= 0)
			i = k + 1;
		if (cmp >= 0)
			j = k - 1;
	} while (i <= j);
	if (strcasecmp(list[k], name) == 0) {
		*key = k;
		return(TRUE);
	} else {
		*key = -1;
		return(FALSE);
	}
}


long DDE_UI::CollectToSeparator(char **scanner, char separator, char *str)

{
        char    *lastChar;
        char    c;
        long    len = 0;

        lastChar = str;
        if (separator == ' ') {
                while ((**scanner == ' ') && (**scanner != '\n') && (**scanner != '\0'))
                        (*scanner)++;
        } else {
                while ((**scanner == ' ') && (**scanner != separator) &&
                         (**scanner != '\n') && (**scanner != '\0'))
                        (*scanner)++;
        }
        while ((**scanner != separator) && (**scanner != '\n') && (**scanner != '\0')) {
                c = *str++ = *(*scanner)++;
                if (c != ' ')
                        lastChar = str;
                len++;
        }
        *lastChar = '\0';
        if (**scanner == separator)
                (*scanner)++;
        return(len);
}


void DDE_UI::InterpretCommand(char *command)

{
	Event	*e;
	char	theResult[512];
	char	s[256];
	char 	*cmd;
	int32 	index;
	int32	cf;

	cmd = command;
	theResult[0] = '\0';
	CollectToSeparator(&cmd, ' ', s);
	if (CommandSearch(zCommands, 6, s, &index)) {
		switch (index) {
			case kFastForward:
				cf = (int32)((currSeconds + STEP_SIZE) / secondsPerFrame);
				if (cf > totalFrames)
					cf = totalFrames;
				currSeconds += STEP_SIZE;
				e = new ChangePositionEvent(cf);
				target->AcceptEvent(e);
				break;
			case kFastReverse:
				break;
			case kNextTrack:
				e = new Event(CMD_NextMediaPiece);
				target->AcceptEvent(e);
				break;
			case kPause:
				state = UIState_Paused;
				e = new Event(CMD_TogglePause);
				target->AcceptEvent(e);
				break;
			case kPlay:
				if (state == UIState_Paused) {
					e = new Event(CMD_TogglePause);
					target->AcceptEvent(e);
				} else {
					e = new Event(CMD_Play);
					target->AcceptEvent(e);
				}
				break;
			case kPreviousTrack:
				e = new Event(CMD_PrevMediaPiece);
				target->AcceptEvent(e);
				break;
			case kShuffle:
				if (plm) {
					plm->SetShuffleMode(true);
					plm->SetCurrentIndex(0);
				}
				e = new Event(CMD_Stop);
				target->AcceptEvent(e);
				e = new Event(CMD_Play);
				target->AcceptEvent(e);
				break;
			case kStop:
				e = new Event(CMD_Stop);
				target->AcceptEvent(e);
			default:
				break;
		}
		command[0] = '\0';
		if (theResult[0] != '\0') {
			strcpy(command, theResult);
			strcat(command, "\n");
		}
		strcat(command, "Ok.");	
	} else
		sprintf(command, "ERR> Command '%s' not recognized.", s);
}


FreeAmpAdvise *DDE_UI::FindAdvise(HWND theClient, long index)

{
	FreeAmpAdvise	*p;
	FreeAmpAdvise	*result = NULL;

	if (adviseList) {
		p = adviseList;
		while (p) {
			if (p->CheckTypeAndClient(theClient, index)) {
				result = p;
				break;
			}
		}
	}
	return(result);
}


void DDE_UI::RemoveAdvise(FreeAmpAdvise *theAdvise)

{
	FreeAmpAdvise	*p, *q;

	p = adviseList;
	q = NULL;
	while ((p) && (p != theAdvise))
		p = p->GetNext();
	if (p) {
		if (q)
			q->SetNext(theAdvise->GetNext());
		else
			adviseList = theAdvise->GetNext();
		delete theAdvise;
	}
}

BOOL DDE_UI::UpdateAdvises(HWND hwnd, short updateType, char *dataStr)

{
	FreeAmpAdvise	*p;
	HWND        	theClient;
	char         	*command;
	BOOL        	fDeferUpd;
	BOOL         	fAckReq;
	BOOL        	result = TRUE;

	switch (updateType) {
		case kAdviseFileName:
			command = "fileName";
			break;
		case kAdviseSamplingFreq:
			command = "sampleFreq";
			break;
		default:
			command = NULL;
			break;
	}
	p = adviseList;
	while ((p) && (result))  {
		if (p->GetType() == updateType) {
			theClient = p->GetClient(&fDeferUpd, &fAckReq);
			result = PostDataMessage(hwnd, theClient, command, dataStr, fDeferUpd, fAckReq, FALSE);
		}
		p = p->GetNext();
	}
	return(result);
}

BOOL DDE_UI::HandleAdvise(HWND hwnd, WORD wParam, DWORD lParam)

{
	HWND         	hwndClient;
	ATOM         	aItem;
	DDEACK      	ddeAck;
	GLOBALHANDLE	hDDEAdvise;
	DDEADVISE FAR	*lpDDEAdvise;
	FreeAmpAdvise  	*advise;
	short         	wStatus;
	short       	deferUpd, ackReq;
	short       	successful = FALSE;
	int         	index;
	char         	*cmd;
	char        	s[256];
	char        	command[256];

⌨️ 快捷键说明

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