📄 simple.cpp.svn-base
字号:
// ------------------------------------------------// File : simple.cpp// Date: 4-apr-2002// Author: giles// Desc: // Simple tray icon interface to PeerCast, mostly win32 related stuff.// // (c) 2002 peercast.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.// ------------------------------------------------#include <windows.h>#include "stdafx.h"#include "resource.h"#include "gui.h"#include "channel.h"#include "servent.h"#include "servmgr.h"#include "win32/wsys.h"#include "peercast.h"#include "simple.h"#define MAX_LOADSTRING 100#define PLAY_CMD 7000#define RELAY_CMD 8000#define INFO_CMD 9000#define URL_CMD 10000extern "C"{ void loadIcons(HINSTANCE hInstance, HWND hWnd);};// PeerCast globalsstatic int currNotify=0;String iniFileName;HWND guiWnd;HWND mainWnd;HMENU trayMenu,ltrayMenu;bool showGUI=true;bool allowMulti=false;bool killMe=false;bool allowTrayMenu=true;int seenNewVersionTime=0;HICON icon1,icon2;ChanInfo chanInfo;bool chanInfoIsRelayed;GnuID lastPlayID;// ---------------------------------Sys * APICALL MyPeercastInst::createSys(){ return new WSys(mainWnd);}// ---------------------------------const char * APICALL MyPeercastApp ::getIniFilename(){ return iniFileName.cstr();}// ---------------------------------const char *APICALL MyPeercastApp ::getClientTypeOS() { return PCX_OS_WIN32;}class NOTIFYICONDATA2{public: DWORD cbSize; // DWORD HWND hWnd; // HWND UINT uID; // UINT UINT uFlags; // UINT UINT uCallbackMessage; // UINT HICON hIcon; // HICON char szTip[128]; // char[128] DWORD dwState; // DWORD DWORD dwStateMask; // DWORD char szInfo[256]; // char[256] UINT uTimeoutOrVersion; // UINT char szInfoTitle[64]; // char[64] DWORD dwInfoFlags; // DWORD //GUID guidItem; > IE 6};NOTIFYICONDATA2 trayIcon;// Global Variables:HINSTANCE hInst; // current instanceTCHAR szTitle[MAX_LOADSTRING]; // The title bar textTCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text// Foward declarations of functions included in this code module:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);LRESULT CALLBACK ChanInfoProc(HWND, UINT, WPARAM, LPARAM);void setTrayIcon(int type, const char *,const char *,bool);void flipNotifyPopup(int id, ServMgr::NOTIFY_TYPE nt);HWND chWnd=NULL;int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ String tmpURL; tmpURL.clear(); char *localURL=NULL; iniFileName.set("peercast.ini"); // off by default now showGUI = false; if (strlen(lpCmdLine) > 0) { char *p; if ((p = strstr(lpCmdLine,"-inifile"))!=NULL) iniFileName.setFromString(p+8); if (strstr(lpCmdLine,"-zen")) showGUI = false; if (strstr(lpCmdLine,"-multi")) allowMulti = true; if (strstr(lpCmdLine,"-kill")) killMe = true; if ((p = strstr(lpCmdLine,"-url"))!=NULL) tmpURL.setFromString(p+4); } if (strstr(tmpURL.cstr(),"peercast://")) { localURL = tmpURL.cstr()+11; showGUI = false; } MSG msg; HACCEL hAccelTable; // Initialize global strings //LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); //LoadString(hInstance, IDC_APP_TITLE, szWindowClass, MAX_LOADSTRING); strcpy(szTitle,"PeerCast"); strcpy(szWindowClass,"PeerCast"); if (!allowMulti) { HANDLE mutex = CreateMutex(NULL,TRUE,szWindowClass); if (GetLastError() == ERROR_ALREADY_EXISTS) { HWND oldWin = FindWindow(szWindowClass,NULL); if (oldWin) { if (killMe) { SendMessage(oldWin,WM_DESTROY,0,0); return 0; } if (localURL) { if (strncmp(localURL,"pls/",4)==0) { COPYDATASTRUCT copy; copy.dwData = WM_PLAYCHANNEL; copy.cbData = strlen(localURL+4); copy.lpData = localURL+4; SendMessage(oldWin,WM_COPYDATA,NULL,(LPARAM)©); }else{ LRESULT r = SendMessage(oldWin,WM_GETPORTNUMBER,0,0); char cmd[512]; sprintf(cmd,"http://localhost:%d/%s",r,localURL); ShellExecute(NULL, NULL, cmd, NULL, NULL, SW_SHOWNORMAL); } }else{ if (showGUI) SendMessage(oldWin,WM_SHOWGUI,0,0); } } return 0; } } if (killMe) return 0; MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) return FALSE; peercastInst = new MyPeercastInst(); peercastApp = new MyPeercastApp(); peercastInst->init(); if (localURL) { if (strncmp(localURL,"pls/",4)==0) { GnuID id; id.fromStr(localURL+4); chanMgr->playChannel(id,false); }else sys->callLocalURL(localURL,servMgr->serverHost.port); } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SIMPLE); // setup menu notifes int mask = peercastInst->getNotifyMask(); if (mask & ServMgr::NT_PEERCAST) CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_PEERCAST,MF_CHECKED|MF_BYCOMMAND); if (mask & ServMgr::NT_BROADCASTERS) CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_BROADCASTERS,MF_CHECKED|MF_BYCOMMAND); if (mask & ServMgr::NT_TRACKINFO) CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_TRACKINFO,MF_CHECKED|MF_BYCOMMAND); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } Shell_NotifyIcon(NIM_DELETE, (NOTIFYICONDATA*)&trayIcon); servMgr->saveSettings(iniFileName.cstr()); return msg.wParam;}//// FUNCTION: MyRegisterClass()//// PURPOSE: Registers the window class.//// COMMENTS://// This function and its usage is only necessary if you want this code// to be compatible with Win32 systems prior to the 'RegisterClassEx'// function that was added to Windows 95. It is important to call this function// so that the application will get 'well formed' small icons associated// with it.//ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SIMPLE); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_SIMPLE; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);}//-----------------------------void loadIcons(HINSTANCE hInstance, HWND hWnd){ icon1 = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL); icon2 = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL2); trayIcon.cbSize = sizeof(trayIcon); trayIcon.hWnd = hWnd; trayIcon.uID = 100; trayIcon.uFlags = NIF_MESSAGE + NIF_ICON + NIF_TIP; trayIcon.uCallbackMessage = WM_TRAYICON; trayIcon.hIcon = icon1; strcpy(trayIcon.szTip, "PeerCast"); Shell_NotifyIcon(NIM_ADD, (NOTIFYICONDATA*)&trayIcon); //ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); trayMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_TRAYMENU)); ltrayMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_LTRAYMENU));}//-----------------------------//// FUNCTION: InitInstance(HANDLE, int)//// PURPOSE: Saves instance handle and creates main window//// COMMENTS://// In this function, we save the instance handle in a global variable and// create and display the main program window.//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } mainWnd = hWnd; loadIcons(hInstance,hWnd); return TRUE;}//-----------------------------static String trackTitle;//-----------------------------void channelPopup(const char *title, const char *msg){ String both; both.append(msg); both.append(" ("); both.append(title); both.append(")"); trayIcon.uFlags = NIF_ICON|NIF_TIP; strncpy(trayIcon.szTip, both.cstr(),sizeof(trayIcon.szTip)-1); trayIcon.szTip[sizeof(trayIcon.szTip)-1]=0; if (ServMgr::NT_TRACKINFO & peercastInst->getNotifyMask()) { trayIcon.uFlags |= 16; trayIcon.uTimeoutOrVersion = 10000; strcpy(trayIcon.szInfo,msg); strcpy(trayIcon.szInfoTitle,title); } Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon);}//-----------------------------void clearChannelPopup(){ trayIcon.uFlags = NIF_ICON|16; trayIcon.uTimeoutOrVersion = 10000; strcpy(trayIcon.szInfo,""); strcpy(trayIcon.szInfoTitle,""); Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon);}//-----------------------------void APICALL MyPeercastApp::channelStart(ChanInfo *info){ lastPlayID = info->id; clearChannelPopup();}//-----------------------------void APICALL MyPeercastApp::channelStop(ChanInfo *info){ if (info->id.isSame(lastPlayID)) { lastPlayID.clear(); clearChannelPopup(); }}//-----------------------------void APICALL MyPeercastApp::channelUpdate(ChanInfo *info){ if (info && info->id.isSame(lastPlayID)) { String tmp; tmp.append(info->track.artist); tmp.append(" "); tmp.append(info->track.title); if (!tmp.isSame(trackTitle)) { trackTitle=tmp; clearChannelPopup(); channelPopup(info->name.cstr(),trackTitle.cstr()); } }}//-----------------------------void APICALL MyPeercastApp::notifyMessage(ServMgr::NOTIFY_TYPE type, const char *msg){ currNotify = type; if (type == ServMgr::NT_UPGRADE) { trayIcon.uFlags = NIF_ICON; trayIcon.hIcon = icon2; }else{ trayIcon.uFlags = NIF_ICON; trayIcon.hIcon = icon1; } const char *title=""; switch(type) { case ServMgr::NT_UPGRADE: title = "Upgrade alert"; break; case ServMgr::NT_BROADCASTERS: title = "Message from broadcaster:"; break; case ServMgr::NT_PEERCAST: title = "Message from PeerCast:"; break; } if (type & peercastInst->getNotifyMask()) { trayIcon.uFlags |= 16; trayIcon.uTimeoutOrVersion = 10000; strcpy(trayIcon.szInfo,msg); strcpy(trayIcon.szInfoTitle,title); Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon); }}//-----------------------------// createGUI()//void createGUI(HWND hWnd){ if (!guiWnd) guiWnd = CreateDialog(hInst, (LPCTSTR)IDD_MAINWINDOW, hWnd, (DLGPROC)GUIProc); ShowWindow(guiWnd,SW_SHOWNORMAL);}// // addRelayedChannelsMenu(HMENU m)// //void addRelayedChannelsMenu(HMENU cm){ int cnt = GetMenuItemCount(cm); for(int i=0; i<cnt-3; i++) DeleteMenu(cm,0,MF_BYPOSITION); for(i=0; i<ChanMgr::MAX_CHANNELS; i++) { Channel *c = &chanMgr->channels[i]; if (c->isActive()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -