system.cpp
字号:
// SYSTEM.CPP
//
// Copyright (c) 1997-2002 Symbian Ltd. All rights reserved.
#include <opxapi.h>
#include <ES_SOCK.H>
#include <coeutils.h>
#include "OPXUTIL.H"
#include <BARSREAD.H>
#include <coemain.h>
#include <APGTASK.H>
#include <apgwgnam.h>
#include <eikapp.h>
#if !defined(__SERIES60__)
#include <eikcfdlg.h>
#endif
#if defined(USE_SYSTIMER)
#include <systimer.h>
#endif
#if defined(USE_ETEL)
#include <ETelBGSM.h> // for IMEI number access
#endif
#include <eikon.rsg>
#include <e32hal.h>
#include <hal.h>
#include <oplerr.h>
#include <f32file.h>
#include <OplDb.h>
#include <eikenv.h>
#include <eikdll.h>
#include <IR_SOCK.H>
#if defined(USE_PLP)
#include <plpsess.h>
#endif
#include <e32math.h>
//
#include <apgcli.h>
#include <apaid.h>
//
#include <s32file.h> // for CDictionaryFileStore
//
#include "System.h"
CSystemOpx::CSystemOpx()
:iThreadArray(4)
{
}
CSystemOpx::~CSystemOpx()
{
iThreadArray.ResetAndDestroy();
if (iConnectedToSystemTimerServer)
{
#if defined(USE_SYSTIMER)
iSystemTimer.Close();
#endif
}
}
// System timers are only supported by Crystal.
// Other platforms will leave with Not Supported.
void CSystemOpx::ConnectToSystemTimerServerL()
{
if (iConnectedToSystemTimerServer)
return;
#if defined(USE_SYSTIMER)
User::LeaveIfError(iSystemTimer.Connect());
#else
User::Leave(KOplErrNotSupported);
#endif
iConnectedToSystemTimerServer=ETrue;
}
void CSystemOpx::GetHAL(OplAPI& aOplAPI) const
{ //SyHAL&:(aAttribute&,BYREF aValue&)
TAny *pvalue=aOplAPI.PopPtrInt32();
TInt value=aOplAPI.GetLong(pvalue);
TInt32 attribute=aOplAPI.PopInt32();
TInt error=HAL::Get((HALData::TAttribute)attribute,value);
aOplAPI.PutLong(pvalue,value);
aOplAPI.Push(TInt32(error));
}
void CSystemOpx::SetHAL(OplAPI& aOplAPI) const
{ //SySetHAL&:(aAttribute&,aValue&)
TInt32 value=aOplAPI.PopInt32();
TInt attribute=aOplAPI.PopInt32();
TInt error=HAL::Set((HALData::TAttribute)attribute,value);
aOplAPI.Push(TInt32(error));
}
void CSystemOpx::GetTimer(OplAPI& aOplAPI)
{ //SyGetTimer:(aTimerId&,BYREF aDuration&)
TAny *pduration=aOplAPI.PopPtrInt32();
TTimeIntervalSeconds durationSeconds(aOplAPI.GetLong(pduration));
#if defined(USE_SYSTIMER)
TInt32 timerId=aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
iSystemTimer.GetTimerL(STATIC_CAST(TSysTimer,timerId),durationSeconds);
#else
aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
#endif
aOplAPI.PutLong(pduration,durationSeconds.Int());
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetTimer(OplAPI& aOplAPI)
{ //SySetTimer:(aTimerId&,aDuration&)
#if defined(USE_SYSTIMER)
TInt32 duration=aOplAPI.PopInt32();
TInt timerId=aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
iSystemTimer.SetTimerL(STATIC_CAST(TSysTimer,timerId),duration);
#else
aOplAPI.PopInt32();
aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
#endif
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::ResetTimer(OplAPI& aOplAPI)
{ //SyResetTimer:(aTimerId&)
#if defined(USE_SYSTIMER)
TInt timerId=aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
iSystemTimer.ResetTimerL(STATIC_CAST(TSysTimer,timerId));
#else
aOplAPI.PopInt32();
ConnectToSystemTimerServerL();
#endif
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetSoundEnabled(OplAPI& aOplAPI) const
{
// TInt32 state=
aOplAPI.PopInt32();
#pragma message("system.cpp: !!TODO Support SetSoundEnabled()")
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetSoundDriverEnabled(OplAPI& aOplAPI) const
{
// TInt32 state=
aOplAPI.PopInt32();
#pragma message("system.cpp: !!TODO Support SetSoundDriverEnabled()")
aOplAPI.Push(TReal64(0.0));
}
LOCAL_C void GetVol(TVolumeInfo& aVol, OplAPI& aOplAPI)
{
TInt32 drive=aOplAPI.PopInt32();
if (drive<0 || drive>25)
User::Leave(KOplErrInvalidArgs);
RFs& fs=aOplAPI.DbManager()->FsSession();
User::LeaveIfError(fs.Volume(aVol,drive));
}
void CSystemOpx::VolumeSize(OplAPI& aOplAPI) const
{
TVolumeInfo vol;
GetVol(vol, aOplAPI);
TInt64 ret(vol.iSize);
ret/=1024;
aOplAPI.Push(TInt32(ret.GetTInt()));
}
void CSystemOpx::VolumeSpaceFree(OplAPI& aOplAPI) const
{
TVolumeInfo vol;
GetVol(vol, aOplAPI);
TInt64 ret(vol.iFree);
ret/=1024;
aOplAPI.Push(TInt32(ret.GetTInt()));
}
void CSystemOpx::VolumeUniqueID(OplAPI& aOplAPI) const
{
TVolumeInfo vol;
GetVol(vol, aOplAPI);
aOplAPI.Push(TInt32(vol.iUniqueID));
}
void CSystemOpx::MediaType(OplAPI& aOplAPI) const
{
const TInt32 drive=aOplAPI.PopInt32();
if (drive<0 || drive>25)
User::Leave(KOplErrInvalidArgs);
TDriveInfo info;
RFs& fs=aOplAPI.DbManager()->FsSession();
User::LeaveIfError(fs.Drive(info,drive));
aOplAPI.Push(TInt32(info.iType));
}
void CSystemOpx::IsReadOnly(OplAPI& aOplAPI) const
{
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
TUint attVal;
fs.Att(file,attVal);
aOplAPI.Push(TInt32(attVal & KEntryAttReadOnly ? -1 : 0));
}
void CSystemOpx::IsHidden(OplAPI& aOplAPI) const
{
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
TUint attVal;
fs.Att(file,attVal);
aOplAPI.Push(TInt32(attVal & KEntryAttHidden ? -1 : 0));
}
void CSystemOpx::IsSystem(OplAPI& aOplAPI) const
{
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
TUint attVal;
fs.Att(file,attVal);
aOplAPI.Push(TInt32(attVal & KEntryAttSystem ? -1 : 0));
}
void CSystemOpx::SetReadOnly(OplAPI& aOplAPI) const
{ // SySetReadOnly:(file$,state&)
TInt32 state=aOplAPI.PopInt32();
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
if (state)
fs.SetAtt(file,KEntryAttReadOnly,0);
else
fs.SetAtt(file,0,KEntryAttReadOnly);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetHiddenFile(OplAPI& aOplAPI) const
{
TInt32 state=aOplAPI.PopInt32();
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
if (state)
fs.SetAtt(file,KEntryAttHidden,0);
else
fs.SetAtt(file,0,KEntryAttHidden);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetSystemFile(OplAPI& aOplAPI) const
{
TInt32 state=aOplAPI.PopInt32();
TPtrC file=aOplAPI.PopString();
RFs& fs=aOplAPI.DbManager()->FsSession();
if (state)
fs.SetAtt(file,KEntryAttSystem,0);
else
fs.SetAtt(file,0,KEntryAttSystem);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::DisplayTaskList(OplAPI& aOplAPI) const
{
aOplAPI.EikonEnv().DisplayTaskList();
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::SetComputeMode(OplAPI& aOplAPI) const
{
TInt32 state=aOplAPI.PopInt32();
if (state!=RWsSession::EPriorityControlDisabled && state!=RWsSession::EPriorityControlComputeOn&& state!=RWsSession::EPriorityControlComputeOff)
User::Leave(KOplErrInvalidArgs);
aOplAPI.WsSession().ComputeMode(RWsSession::TComputeMode(state));
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::LogonToThread(OplAPI& aOplAPI)
{
TInt32* statusW=aOplAPI.PopPtrInt32();
TInt tempId =aOplAPI.PopInt32();
TThreadId id= *((TThreadId*)&tempId);
RThreadHolder* thread=new(ELeave) RThreadHolder();
TRAPD(err,iThreadArray.AppendL(thread));
if (err)
{
delete thread;
User::Leave(err);
}
User::LeaveIfError(thread->Open(id));
TCallBack callBack(&(CSystemOpx::LogonToThreadCallBack),thread);
TRequestStatus& tr=aOplAPI.NewRequestL(statusW,EActivePriorityWsEvents+1,callBack);
thread->SetTReq(tr);
thread->SetTls(this);
thread->Logon(tr);
thread->Resume();
aOplAPI.Push(TReal64(0.0));
}
TInt CSystemOpx::LogonToThreadCallBack(TAny* aThread)
{
RThreadHolder* thread=(RThreadHolder*)aThread;
thread->Close();
TInt pos=thread->TheTls()->CheckThreadPointerL(thread);
thread->TheTls()->iThreadArray.Delete(pos);
delete thread;
return KErrNone;
}
void CSystemOpx::RunApp(OplAPI& aOplAPI) const
{
TInt32 cmd=aOplAPI.PopInt32();
TPtrC tail=aOplAPI.PopString();
TPtrC doc=aOplAPI.PopString();
TPtrC lib=aOplAPI.PopString();
if (cmd < EApaCommandOpen || cmd > EApaCommandRunWithoutViews) // min and max values.
User::Leave(KOplErrInvalidArgs);
CApaCommandLine* apaCommandLine=CApaCommandLine::NewLC();
apaCommandLine->SetLibraryNameL(lib);
apaCommandLine->SetDocumentNameL(doc);
TPtrC8 tail8bit((TText8*)tail.Ptr(),tail.Size());
apaCommandLine->SetTailEndL(tail8bit);
apaCommandLine->SetCommandL(TApaCommand(cmd));
TThreadId tId=EikDll::StartAppL(*apaCommandLine);
aOplAPI.Push(*((TInt32*)&tId));
CleanupStack::PopAndDestroy(); // apaCommandLine
}
void CSystemOpx::RunExe(OplAPI& aOplAPI) const
{
TPtrC name=aOplAPI.PopString();
TThreadId tId=EikDll::StartExeL(name);
aOplAPI.Push(*((TInt32*)&tId));
}
#ifdef __WINS__
typedef TInt (*ExeEntryPoint)();
LOCAL_C TInt ExeThreadStartFunction(TAny* aParam)
{
const TDesC& libraryName=(*(const TDesC*)aParam);
RLibrary lib;
TInt err=lib.Load(libraryName);
User::Free(aParam);
if (!err)
{
ExeEntryPoint exeFunc=(ExeEntryPoint)lib.Lookup(1);
if (!exeFunc)
err=KErrBadLibraryEntryPoint;
else
err=(*exeFunc)();
}
return(err);
}
#endif
void CSystemOpx::RunExeWithCommandLine(OplAPI& aOplAPI) const
{
TPtrC cmdLine=aOplAPI.PopString();
TPtrC exeName=aOplAPI.PopString();
// Leave if the EXE doesn't exist in the first place!
if (!ConeUtils::FileExists(exeName))
User::Leave(KOplErrNotExists);
TParse fileName;
TThreadId threadId;
#ifdef __WINS__
#pragma warning(disable:4238)
User::LeaveIfError(fileName.Set(exeName,&_L("Z:\\SYSTEM\\PROGRAMS\\.EXE"),NULL));
#pragma warning(default:4238)
RThread process;
TBuf<KMaxFileName> threadName;
TPtrC baseName=fileName.Name();
TInt err;
TInt num=0;
do
{
threadName.Format(_L("%S%02d"),&baseName,num++);
err=process.Create(threadName,
ExeThreadStartFunction,
KDefaultStackSize,
KDefaultMinHeapSize,
KDefaultMaxHeapSize,
NULL);
}
while(err==KErrAlreadyExists);
User::LeaveIfError(err);
RHeap* heap=process.Heap();
RHeap* originalHeap=User::SwitchHeap(heap);
HBufC* commandLine=fileName.FullName().Alloc();
User::SwitchHeap(originalHeap);
if (!commandLine)
{
process.Close();
User::LeaveNoMemory();
}
process.SetInitialParameter(commandLine);
threadId = process.Id();
#else
TPtrC tempDes=_L("Z:\\SYSTEM\\PROGRAMS\\.EXE");
User::LeaveIfError(fileName.Set(exeName,&tempDes,NULL));
RProcess process;
TInt err=process.Create(fileName.FullName(),cmdLine);
User::LeaveIfError(err);
// Get the threadId of the main thread in the exe's process
TFullName fullName(process.Name());
fullName.Append(_L("::Main"));
TFindThread fT(fullName);
fT.Next(fullName);
RThread thread;
thread.Open(fT);
threadId = thread.Id();
thread.Close();
#endif
process.Resume();
process.Close();
aOplAPI.Push(*((TInt32*)&threadId));
}
void CSystemOpx::RunDocument(OplAPI& aOplAPI) const
{
// It's illegal in GCC, but I'd like to do TBool aSwitchToIfRunning=((aOplAPI.PopInt16()==0) ? EFalse : ETrue);
TBool aSwitchToIfRunning=aOplAPI.PopInt16();
TFileName aDocFileName=aOplAPI.PopString();
if (!ConeUtils::FileExists(aDocFileName))
User::Leave(KOplErrNotExists);
//
// Code Compatible With ER5....
//
TThreadId threadId;
if (aSwitchToIfRunning)
{
TApaTaskList taskList(aOplAPI.WsSession());
TApaTask task=taskList.FindDoc(aDocFileName); // Check if doc is running
if (task.Exists())
{
threadId=task.ThreadId();
task.BringToForeground();
aOplAPI.Push(*((TInt32*)&threadId));
return;
}
}
RApaLsSession appList;
User::LeaveIfError(appList.Connect());
appList.GetAllApps();
TInt32 err = appList.StartDocument(aDocFileName,threadId);
appList.Close();
// -1 (KErrNotFound) maps to 'General Error' in OPL so
// this is hopefully more meaningful
if (err != KErrNone)
{
if (err == KErrNotFound)
User::Leave(KOplErrNotSupported);
else
User::Leave(err);
}
aOplAPI.Push(*((TInt32*)&threadId));
}
void CSystemOpx::KillCurrentProcess(OplAPI& aOplAPI) const
{
TInt32 reason=aOplAPI.PopInt32();
RProcess proc;
proc.Kill(reason);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::KillProcess(OplAPI& aOplAPI) const
{
TInt32 reason=aOplAPI.PopInt32();
TPtrC name=aOplAPI.PopString();
RProcess proc;
proc.Open(name);
proc.Kill(reason);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::TerminateCurrentProcess(OplAPI& aOplAPI) const
{
TInt32 reason=aOplAPI.PopInt32();
#ifdef __WINS__
// Terminating the current process on stops the emulator
// so kill the thread instead
RThread thread;
thread.Terminate(reason);
#else
RProcess proc;
proc.Terminate(reason);
#endif
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::TerminateProcess(OplAPI& aOplAPI) const
{ //SyTerminateProcess&:(processName$,reasonCode&)
TInt32 reason=aOplAPI.PopInt32();
TPtrC name=aOplAPI.PopString();
RProcess proc;
TInt error=proc.Open(name);
if (error==KErrNone)
{
proc.Terminate(reason);
}
aOplAPI.Push(TInt32(error));
}
void CSystemOpx::Mod(OplAPI& aOplAPI) const
{
TInt32 right=aOplAPI.PopInt32();
TInt32 left=aOplAPI.PopInt32();
if (right==0)
User::Leave(KErrDivideByZero);
aOplAPI.Push(left%right);
}
void CSystemOpx::XOR(OplAPI& aOplAPI) const
{
TInt32 right=aOplAPI.PopInt32();
TInt32 left=aOplAPI.PopInt32();
aOplAPI.Push(left^right);
}
void CSystemOpx::LoadRsc(OplAPI& aOplAPI) const
{
TPtrC file=aOplAPI.PopString();
aOplAPI.Push(TInt32(aOplAPI.EikonEnv().AddResourceFileL(file)));
}
void CSystemOpx::UnLoadRsc(OplAPI& aOplAPI) const
{
TInt file=aOplAPI.PopInt32();
aOplAPI.EikonEnv().DeleteResourceFile(file);
aOplAPI.Push(TReal64(0.0));
}
void CSystemOpx::ReadRsc(OplAPI& aOplAPI) const
{
TInt id=aOplAPI.PopInt32();
TBuf<256> des;
aOplAPI.EikonEnv().ReadResource(des,id);
aOplAPI.PushL(des);
}
void CSystemOpx::ReadRscLong(OplAPI& aOplAPI) const
{
TInt id=aOplAPI.PopInt32();
TResourceReader reader;
aOplAPI.EikonEnv().CreateResourceReaderLC(reader,id);
aOplAPI.Push(TInt32(reader.ReadInt32()));
}
void CSystemOpx::UidCheckSum(OplAPI& aOplAPI) const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -