main.cxx
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:14k
- /*
- * main.cxx
- *
- * PWLib application source file for pwtest
- *
- * Main program entry point.
- *
- * Copyright 98 Equivalence
- *
- * $Log: main.cxx,v $
- * Revision 1.20 2000/07/25 13:14:07 robertj
- * Got the video capture stuff going!
- *
- * Revision 1.19 2000/07/15 09:47:35 robertj
- * Added video I/O device classes.
- *
- * Revision 1.18 2000/04/29 08:11:06 robertj
- * Fixed problem with stream output width in PTimeInterval
- *
- * Revision 1.17 2000/04/29 04:51:07 robertj
- * Added microseconds to string output.
- * Fixed uninitialised microseconds on some constructors.
- *
- * Revision 1.16 1999/09/10 04:35:43 robertj
- * Added Windows version of PIPSocket::GetInterfaceTable() function.
- *
- * Revision 1.15 1999/08/07 07:11:14 robertj
- * Added test for menu text change
- *
- * Revision 1.14 1999/08/07 01:47:18 robertj
- * Fixed some trivial errors.
- *
- * Revision 1.13 1999/07/06 04:46:01 robertj
- * Fixed being able to case an unsigned to a PTimeInterval.
- * Improved resolution of PTimer::Tick() to be millisecond accurate.
- *
- * Revision 1.12 1999/07/03 03:12:59 robertj
- * #ifdef'ed test code for stuff not in general distrubution.
- *
- * Revision 1.11 1999/06/30 08:57:20 robertj
- * Fixed bug in encodeing sequence of constrained primitive type. Constraint not set.
- * Fixed bug in not emitting namespace use clause.
- * Added "normalisation" of separate sequence of <base type> to be single class.
- *
- * Revision 1.10 1999/06/24 14:00:09 robertj
- * Added URL parse test
- *
- * Revision 1.9 1999/06/14 07:59:39 robertj
- * Enhanced tracing again to add options to trace output (timestamps etc).
- *
- * Revision 1.8 1999/06/07 01:55:04 robertj
- * Added test for default driver function in sound classes.
- *
- * Revision 1.7 1999/05/28 14:07:18 robertj
- * Added function to get default audio device.
- *
- * Revision 1.6 1999/03/29 03:39:56 robertj
- * Changed semantics of PTitledWindow::OnClose() function.
- *
- * Revision 1.5 1999/02/23 07:11:29 robertj
- * Improved trace facility adding trace levels and #define to remove all trace code.
- *
- * Revision 1.4 1999/02/22 10:15:16 robertj
- * Sound driver interface implementation to Linux OSS specification.
- *
- * Revision 1.3 1999/01/31 00:59:27 robertj
- * Added IP Access Control List class to PTLib Components
- *
- * Revision 1.2 1998/12/23 01:17:29 robertj
- * PWCLib test
- *
- */
- #include <pwlib.h>
- #include <ptlib/videoio.h>
- #include <ptclib/ipacl.h>
- #include <ptclib/url.h>
- #include <ptclib/asner.h>
- #include "resources.h"
- class OpenSoundFileDialog : public POpenFileDialog
- {
- PCLASSINFO(OpenSoundFileDialog, POpenFileDialog)
- public:
- OpenSoundFileDialog(MainWindow * parent);
- virtual void OnInit();
- const PString & GetDriver() const { return driver; }
- BOOL GetWait() const { return waitFlag; }
- private:
- PString driver;
- BOOL waitFlag;
- };
- class SaveSoundFileDialog : public PSaveFileDialog
- {
- PCLASSINFO(SaveSoundFileDialog, PSaveFileDialog)
- public:
- SaveSoundFileDialog(MainWindow * parent);
- virtual void OnInit();
- const PString & GetDriver() const { return driver; }
- private:
- PString driver;
- };
- PCREATE_PROCESS(Pwtest);
- #define new PNEW
- Pwtest::Pwtest()
- : PApplication("Equivalence", "pwtest", 1, 0, AlphaCode, 1)
- {
- PTrace::SetLevel(1);
- PTrace::SetOptions(PTrace::Blocks);
- }
- #define TEST_TIME(t) PTRACE(1, t << " => " << PTime(t))
- void Pwtest::Main()
- {
- PTRACE_BLOCK("Pwtest::Pwtest()");
- PTRACE(2, GetName() << " v" << GetVersion(TRUE));
- PTime now;
- PTRACE(1, "Time is now " << now.AsString("h:m:s.u d/M/y"));
- PTRACE(1, "Time is now " << now.AsString("yyyy/MM/dd h:m:s.uuuu"));
- PTRACE(1, "Time is now " << now.AsString("MMM/d/yyyyy w h:m:sa"));
- PTRACE(1, "Time is now " << now.AsString("wwww d/M/yyy h:m:s.uu"));
- PTRACE(1, "Time is now " << now.AsString("www d/MMMM/yy h:m:s.uuu"));
- TEST_TIME("5/03/1999 12:34:56");
- TEST_TIME("15/06/1999 12:34:56");
- TEST_TIME("15/06/01 12:34:56 PST");
- TEST_TIME("5/06/02 12:34:56");
- TEST_TIME("5/23/1999 12:34am");
- TEST_TIME("5/23/00 12:34am");
- TEST_TIME("1999/23/04 12:34:56");
- TEST_TIME("Mar 3, 1999 12:34pm");
- TEST_TIME("3 Jul 2004 12:34pm");
- TEST_TIME("12:34:56 5 December 1999");
- TEST_TIME("10 minutes ago");
- TEST_TIME("2 weeks");
- PTRACE(1, "Testing timer resolution, reported as " << PTimer::Resolution() << "ms");
- time_t oldSec = time(NULL); // Wait for second boundary
- while (oldSec == time(NULL))
- ;
- oldSec++;
- PTimeInterval newTick = PTimer::Tick();
- PTimeInterval oldTick = newTick;
- unsigned count = 0;
- while (oldSec == time(NULL)) { // For one full second
- while (newTick == oldTick)
- newTick = PTimer::Tick();
- oldTick = newTick;
- count++; // Count the changes in tick
- } ;
- PTRACE(1, "Actual resolution is " << 1000000/count << "us");
- oldTick = 1234;
- PTRACE(1, "TimeInterval output: "" << setw(15) << newTick << '"');
- PTRACE(1, "TimeInterval output: "" << setw(15) << oldTick << '"');
- for (int p = 3; p < 20; p++)
- PTRACE(1, "TimeInterval output: " << p << " ""
- << setw(p) << setprecision(2) << oldTick << '"');
- PPER_Stream strm;
- PASN_ObjectId id;
- id = "0.0.8.2250.0.2";
- id.Encode(strm);
- strm.CompleteEncoding();
- PTRACE(1, "OID " << id << " -> " << strm);
- SetAboutDialogID(IDD_ABOUT);
- new MainWindow(GetArguments());
- PApplication::Main();
- }
- MainWindow::MainWindow(PArgList &)
- {
- PTRACE_BLOCK("Pwtest::Pwtest()");
- SetTitle(PResourceString(IDS_TITLE));
- SetIcon(PIcon(IDI_MAIN_WINDOW));
- myMenu = new MainMenu(this);
- SetMenu(myMenu);
- static const PButtonBar::ButtonID cmds[] = {
- { 1, "IP_ACL", IDM_TEST_ENABLED, IDM_TEST_DISABLED, IDS_TEST_BUTTON }
- };
- buttonBar = new PButtonBar(this, cmds, PARRAYSIZE(cmds));
- statusBar = new PStatusBar(this, 2);
- statusBar->SetSectionWidth(1, 20);
- new PMDIDocWindow(this, "Test");
- UpdateCommandSources();
- ShowAll();
- }
- void MainWindow::NewCmd(PMenuItem &, INT)
- {
- PMDIDocWindow * child = new PMDIDocWindow(this, "fred");
- child->Show();
- }
- void MainWindow::OpenCmd(PMenuItem &, INT)
- {
- POpenFileDialog dlg(this);
- if (dlg.RunModal()) {
- PTextFile file;
- if (file.Open(dlg.GetFile())) {
- PString str = file.ReadString(file.GetLength());
- }
- }
- }
- void MainWindow::CloseCmd(PMenuItem &, INT)
- {
- // Close document
- }
- void MainWindow::SaveCmd(PMenuItem & item, INT)
- {
- // Save current document
- PINDEX pos;
- pos = item.GetPosition();
- pos = item.GetMenu()->GetPosition();
- }
- void MainWindow::SaveAsCmd(PMenuItem &, INT)
- {
- PSaveFileDialog dlg(this);
- if (dlg.RunModal()) {
- // Save document to new name
- }
- }
- void MainWindow::PrintCmd(PMenuItem &, INT)
- {
- PPrintJobDialog dlg(this, printInfo);
- if (dlg.RunModal()) {
- printInfo = dlg.GetPrintInfo();
- PPrintCanvas canvas("pwtest", printInfo);
- // Add printing code here
- }
- }
- void MainWindow::PrinterSetupCmd(PMenuItem &, INT)
- {
- PPrinterSetupDialog dlg(this, printInfo);
- if (dlg.RunModal())
- printInfo = dlg.GetPrintInfo();
- }
- void MainWindow::ExitCmd(PMenuItem &, INT)
- // The Exit menu ... well ... exits.
- {
- owner->Terminate();
- }
- void MainWindow::CopyCmd()
- {
- PClipboard clip(this);
- // Do something with the clipboard
- }
- BOOL MainWindow::CanCopy()
- {
- // If want copy menu enabled
- return TRUE;
- }
- void MainWindow::PasteCmd()
- {
- PClipboard clip(this);
- // Do something with the clipboard
- }
- BOOL MainWindow::CanPaste()
- {
- // If want paste menu enabled, ie clipboard has right format
- return TRUE;
- }
- void MainWindow::OnRedraw(PCanvas & canvas)
- {
- PPattern::Bits pat = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 };
- canvas.SetFillPattern(PPattern(pat));
- canvas.FillRect(100, 100, 100, 100);
- canvas.FillRect(150, 150, 100, 100);
- }
- void MainWindow::OnResize(const PDim & newSize, ResizeType type)
- {
- if (type != Iconic) {
- PRect bounds(newSize);
- buttonBar->AutoAdjustBounds(bounds, AdjustTop);
- statusBar->AutoAdjustBounds(bounds, AdjustBottom);
- SetDocumentArea(bounds, PixelCoords);
- }
- }
- void MainWindow::OnClose()
- {
- PTopLevelWindow::OnClose();
- }
- void MainWindow::TestCmd()
- {
- myMenu->menuTestItem->SetString("Test Menu");
- }
- void MainWindow::IP_ACL_Cmd()
- {
- PIpAccessControlList list;
- list.LoadHostsAccess("ipop3d");
- list.Add("10.0.2.0/24");
- list.Add("192.168.");
- list.Add(".nr.equival.net");
- list.Add("-hadron.nr.equival.net");
- list.Add("-10.0.2.1");
- list.Add("203.41.8.86/255.255.255.248");
- list.Add("-ALL");
- PINDEX i;
- PStringStream str;
- for (i = 0; i < list.GetSize(); i++)
- str << list[i] << 'n';
- static const char * const addresses[] = {
- "10.0.1.1", "10.0.1.4", "10.0.2.4", "10.0.2.1",
- "129.168.1.1", "192.168.1.1",
- "203.41.8.86", "203.41.8.80", "203.41.8.88"
- };
- for (i = 0; i < PARRAYSIZE(addresses); i++) {
- PIPSocket::Address addr = addresses[i];
- str << 'n' << addr << ' ' << (list.IsAllowed(addr) ? "Allowed" : "Disallowed");
- }
- PSimpleDialog::Info(this, str);
- }
- void MainWindow::ListInterfacesCmd(PMenuItem &, INT)
- {
- PStringStream str;
- PIPSocket::InterfaceTable interfaces;
- if (PIPSocket::GetInterfaceTable(interfaces)) {
- str << "Interfaces:n";
- for (PINDEX i = 0; i < interfaces.GetSize(); i++)
- str << " "
- << interfaces[i].GetName() << ' '
- << interfaces[i].GetAddress() << ' '
- << interfaces[i].GetMACAddress() << 'n';
- }
- else
- str << "Error getting interfaces.";
- PSimpleDialog::Info(this, str);
- }
- #ifdef PARSE_URL_TEST
- void MainWindow::ParseURLCmd()
- {
- ParseURLDialog dlg(this);
- if (dlg.RunModal()) {
- PURL url = dlg.url;
- PStringStream str;
- str << "URL: " << url << "n"
- "Scheme: " << url.GetScheme() << "n"
- "Host: " << url.GetHostName() << "n"
- "Port: " << url.GetPort() << "n"
- "Username: " << url.GetUserName() << "n"
- "Password: " << url.GetPassword() << "n"
- "Fragment: " << url.GetFragment() << "n"
- "Params: " << url.GetParameters() << 'n';
- PINDEX i;
- for (i = 0; i < url.GetPath().GetSize(); i++)
- str << "Path[" << i << "]: " << url.GetPath()[i] << 'n';
- for (i = 0; i < url.GetQueryVars().GetSize(); i++)
- str << "Query[" << i << "]: "
- << url.GetQueryVars().GetKeyAt(i) << " = "
- << url.GetQueryVars().GetDataAt(i) << 'n';
- PSimpleDialog::Info(this, str);
- }
- }
- #endif
- void MainWindow::PlaySoundCmd()
- {
- OpenSoundFileDialog dlg(this);
- if (!dlg.RunModal())
- return;
- PSoundChannel player;
- if (!player.Open(dlg.GetDriver(), PSoundChannel::Player)) {
- PSimpleDialog::Error(this, "Could not open driver: "+dlg.GetDriver());
- return;
- }
- player.SetBuffers(65536, 8);
- if (!player.PlayFile(dlg.GetFile(), dlg.GetWait()))
- PSimpleDialog::Error(this, "Could not play file: "+dlg.GetFile());
- }
- OpenSoundFileDialog::OpenSoundFileDialog(MainWindow * parent)
- : POpenFileDialog(parent)
- {
- waitFlag = TRUE;
- AddFileType(".wav");
- }
- void OpenSoundFileDialog::OnInit()
- {
- POpenFileDialog::OnInit();
- PDim dim = GetDimensions(LocalCoords);
- driver = PSoundChannel::GetDefaultDevice(PSoundChannel::Player);
- PStringArray driverNames = PSoundChannel::GetDeviceNames(PSoundChannel::Player);
- PComboBox * drivers = new PComboBox(this);
- drivers->SetValuePointer(&driver);
- for (PINDEX i = 0; i < driverNames.GetSize(); i++) {
- drivers->AddString(driverNames[i]);
- if (driverNames[i] == driver)
- drivers->SetCurrent(i);
- }
- drivers->SetPosition(20, dim.Height());
- drivers->SetDimensions(120, 60, LocalCoords);
- drivers->Show();
- dim.AddHeight(20);
- if (dim.Width() <= 160)
- dim.SetWidth(160);
- PCheckBox * wait = new PCheckBox(this, "Wait", waitFlag);
- wait->SetValuePointer(&waitFlag);
- wait->SetPosition(20, dim.Height());
- wait->Show();
- PDim ctlDim = wait->GetDimensions(LocalCoords);
- dim.AddHeight(ctlDim.Height() + 10);
- if (dim.Width() <= ctlDim.Width()+40)
- dim.SetWidth(ctlDim.Width()+41);
- SetDimensions(dim, LocalCoords);
- }
- void MainWindow::RecordSoundCmd()
- {
- SaveSoundFileDialog dlg(this);
- if (!dlg.RunModal())
- return;
- PSoundChannel recorder;
- if (!recorder.Open(dlg.GetDriver(), PSoundChannel::Recorder)) {
- PSimpleDialog::Error(this, "Could not open driver: "+dlg.GetDriver());
- return;
- }
- recorder.SetBuffers(8*8000, 2); // 8 x 8 second buffers
- if (!recorder.RecordFile(dlg.GetFile()))
- PSimpleDialog::Error(this, "Could not record file: "+dlg.GetFile());
- }
- SaveSoundFileDialog::SaveSoundFileDialog(MainWindow * parent)
- : PSaveFileDialog(parent)
- {
- SetDefaultFileType(".wav");
- }
- void SaveSoundFileDialog::OnInit()
- {
- PSaveFileDialog::OnInit();
- PDim dim = GetDimensions(LocalCoords);
- driver = PSoundChannel::GetDefaultDevice(PSoundChannel::Recorder);
- PStringArray driverNames = PSoundChannel::GetDeviceNames(PSoundChannel::Recorder);
- PComboBox * drivers = new PComboBox(this);
- drivers->SetValuePointer(&driver);
- for (PINDEX i = 0; i < driverNames.GetSize(); i++) {
- drivers->AddString(driverNames[i]);
- if (driverNames[i] == driver)
- drivers->SetCurrent(i);
- }
- drivers->SetPosition(20, dim.Height());
- drivers->SetDimensions(120, 60, LocalCoords);
- drivers->Show();
- dim.AddHeight(20);
- if (dim.Width() <= 160)
- dim.SetWidth(160);
- SetDimensions(dim, LocalCoords);
- }
- void MainWindow::VideoCaptureCmd()
- {
- PVideoInputDevice grabber;
- if (grabber.Open(grabber.GetDeviceNames()[0])) {
- PBYTEArray buffer(grabber.GetMaxFrameBytes());
- PINDEX i;
- for (i = 0; i < 100; i++) {
- if (!grabber.GetFrameData(buffer.GetPointer()))
- break;
- }
- grabber.Stop();
- if (i < 100)
- PSimpleDialog::Error(this, "Error in grab.");
- else {
- PSimpleDialog::Info(this, "Grabbed 100 frames.");
- unsigned width, height;
- grabber.GetFrameSize(width, height);
- PPixelImage image(width, height, 24);
- memcpy(image->GetPixelDataPtr(), buffer, buffer.GetSize());
- PFile file("c:\capture.bmp", PFile::WriteOnly);
- image->Write(file);
- }
- }
- }
- // End of File ///////////////////////////////////////////////////////////////