// APSMAIN.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

//
//	The main startup of the AppArc server
//

#include <apsserv.h>
#include <fbs.h>

#include "apsstd.h"
#include "apasvst.h"

GLDEF_C void Fault(TSvFault aFault)
//
// Fault the server program.
//
	{
	_LIT(KApaServFault,"APSERV-fault");
	User::Panic(KApaServFault,aFault);
	}

GLDEF_C void CSvActiveScheduler::New()
//
// Create and install the active scheduler.
//
	{
	CSvActiveScheduler* pA=new CSvActiveScheduler;
	__ASSERT_ALWAYS(pA!=NULL,Fault(EMainApaCreateScheduler));
	CActiveScheduler::Install(pA);
	}

GLDEF_C void CSvActiveScheduler::Error(TInt) const
//
// Called if any Run() method leaves.
//
	{
	Fault(EMainApaActiveError);
	}

LOCAL_C void SignalServerStart()
//
//	Signal the global semaphore if it is present
//
	{

	RSemaphore globStartSignal;
	if (globStartSignal.OpenGlobal(NameApaServStartSemaphore())==KErrNone)
		globStartSignal.Signal();
	globStartSignal.Close();
	}

GLDEF_C TInt ApaServThreadStart(TAny* aPtr)
//
// The server thread.
//
	{
	__UHEAP_MARK;
	RThread thread;
	TInt error=thread.Rename(NameApaServServerThread());
	__ASSERT_ALWAYS(error==KErrNone || error==KErrAlreadyExists,Fault(EMainApaThreadRename));
	thread.SetNotifyIfDie(ETrue);
	thread.SetPriority(EPriorityAbsoluteForeground);
	thread.Close();
	CSvActiveScheduler::New();
	CTrapCleanup* trapCleanup=CTrapCleanup::New();
	SCommandLine* comLine=STATIC_CAST(SCommandLine*,aPtr);
	error=RFbsSession::Connect();
	__ASSERT_ALWAYS(error==KErrNone,Fault(EMainApaCreateScheduler));
	CApaAppListServer* appListServer=NULL;
	TRAPD(err,appListServer=CApaAppListServer::NewL(*(comLine->iAppStarter)));
	if (err!=KErrNone)
		return err;
#if defined(__WINS__)
	UserSvr::ServerStarted();
#endif
	SignalServerStart();
	CSvActiveScheduler::Start();
	delete appListServer;
	RFbsSession::Disconnect();
	delete CSvActiveScheduler::Current();
	delete trapCleanup;
	__UHEAP_MARKEND;
	return KErrNone;
	}


GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
//
//	DLL entry point
//
	{
	return(KErrNone);
	}
