aboutsummaryrefslogtreecommitdiffstats
path: root/distrib/sdl-1.2.15/src/main/symbian/EKA2/SDL_main.cpp
blob: 3dc69d4aad1bf5994525c34c86d9e1837e49a137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/*
    SDL_Main.cpp
    Symbian OS services for SDL

    Markus Mertama
*/


#include "epoc_sdl.h"

#include"sdlepocapi.h"
#include <e32base.h>
#include <estlib.h>
#include <stdio.h>
#include <badesca.h>

#include "vectorbuffer.h"
#include <w32std.h>
#include <aknappui.h>
#include <aknapp.h>
#include "SDL_epocevents_c.h"
#include "SDL_keysym.h"
#include "dsa.h"


#ifdef SYMBIANC
#include <reent.h>
#endif

//Markus Mertama


extern SDLKey* KeyMap();
extern void ResetKeyMap();

class CCurrentAppUi;

//const TUid KSDLUid =  { 0xF01F3D69 };

NONSHARABLE_CLASS(EnvUtils)
	{
	public:
	static void DisableKeyBlocking();
	static TBool Rendezvous(RThread& aThread, TRequestStatus& aStatus);
	};

TInt Panic(TInt aErr, TInt aLine)
	{
	TBuf<64> b;
	b.Format(_L("Main at %d"), aLine);
	User::Panic(b, aErr);
	return 0;	
	}
	

NONSHARABLE_CLASS(CCurrentAppUi) : public CAknAppUi
	{
	public:
	static CCurrentAppUi* Cast(CEikAppUi* aUi);
	void DisableKeyBlocking();
	};
	
	
CCurrentAppUi* CCurrentAppUi::Cast(CEikAppUi* aUi)
	{
	return static_cast<CCurrentAppUi*>(aUi);	
	}
	
void CCurrentAppUi::DisableKeyBlocking()	
	{
	SetKeyBlockMode(ENoKeyBlock);	
	}


class CEventQueue : public CBase, public MEventQueue
    {
    public:
        static CEventQueue* NewL();
        ~CEventQueue();
    public:
        TInt Append(const TWsEvent& aEvent);
       	const TWsEvent& Shift();
       	void Lock();
       	void Unlock();
        TBool HasData();
    private:
        TVector<TWsEvent, 64> iVector;
        RCriticalSection iCS;
    };
    
 CEventQueue* CEventQueue::NewL()
    {
    CEventQueue* q = new (ELeave) CEventQueue();
    CleanupStack::PushL(q);
    User::LeaveIfError(q->iCS.CreateLocal());
    CleanupStack::Pop();
    return q;
    }
    
CEventQueue::~CEventQueue()
    {
    iCS.Close();
    }
    
TInt CEventQueue::Append(const TWsEvent& aEvent)
    {
    iCS.Wait();
   	const TInt err = iVector.Append(aEvent);
    iCS.Signal();
    return err;
    }
    
    
TBool CEventQueue::HasData()
    {
    return iVector.Size() > 0;
    }


void CEventQueue::Lock()
	{
    iCS.Wait();
	}
	
void CEventQueue::Unlock()
	{
	iCS.Signal();
	}

const TWsEvent& CEventQueue::Shift()
    {
    const TWsEvent& event =  iVector.Shift();
    return event;
    }


TSdlCleanupItem::TSdlCleanupItem(TSdlCleanupOperation aOperation, TAny* aItem) :
iOperation(aOperation), iItem(aItem), iThread(RThread().Id())
    {
    }

class CEikonEnv;
class CSdlAppServ;

    
NONSHARABLE_CLASS(EpocSdlEnvData)
    {
    public:
    void Free();
    CEventQueue*            iEventQueue;
    TMainFunc				iMain;
    TInt            		iEpocEnvFlags;
    int                     iArgc;
    char**                  iArgv;
    CDsa*                   iDsa;
    CSdlAppServ*            iAppSrv;
    TThreadId               iId;
    CArrayFix<TSdlCleanupItem>* iCleanupItems; 
    CEikAppUi*				iAppUi;
    CSDL*					iSdl;
    };
  
   
EpocSdlEnvData* gEpocEnv;

#define MAINFUNC(x) EXPORT_C TMainFunc::TMainFunc(mainfunc##x aFunc){Mem::FillZ(iMainFunc, sizeof(iMainFunc)); iMainFunc[x - 1] = (void*) aFunc;}
    
MAINFUNC(1)
MAINFUNC(2)
MAINFUNC(3)
MAINFUNC(4)
MAINFUNC(5)
MAINFUNC(6)

EXPORT_C TMainFunc::TMainFunc() 
	{
	Mem::FillZ(iMainFunc, sizeof(iMainFunc));
	}
	

const void* TMainFunc::operator[](TInt aIndex) const
	{
	return iMainFunc[aIndex];
	}


NONSHARABLE_CLASS(CSdlAppServ) : public CActive
    {
    public:
        enum
            {
            EAppSrvNoop = CDsa::ELastDsaRequest,
            EAppSrvWindowWidth,
            EAppSrvWindowHeight,
            EAppSrvWindowDisplayMode,
            EAppSrvWindowPointerCursorMode,
            EAppSrvDsaStatus,
            EAppSrvStopThread,
            EAppSrvWaitDsa
            };
        CSdlAppServ();
        void ConstructL();
        ~CSdlAppServ();
        TInt Request(TInt aService);
        TInt RequestValue(TInt aService);
        void Init(); 
        void PanicMain(TInt aReason);
        void PanicMain(const TDesC& aInfo, TInt aReason);
        void SetObserver(MSDLObserver* aObserver);
        TInt ObserverEvent(TInt aEvent, TInt aParam);
        void SetParam(TInt aParam);
        void HandleObserverValue(TInt aService, TInt aReturnValue, TBool aMainThread);
        MSDLObserver* Observer();
    private:
        void RunL();
        void DoCancel();
    private:
        const TThreadId iMainId;
        RThread iAppThread;
        TInt iService;
        TInt iReturnValue;
        RSemaphore iSema;
        MSDLObserver* iObserver;
        TRequestStatus* iStatusPtr;
    };
    
CSdlAppServ::CSdlAppServ() : CActive(CActive::EPriorityHigh), iMainId(RThread().Id())
    {
    }
    
    
    
MSDLObserver* CSdlAppServ::Observer()
	{
	return iObserver;
	}
	
	
void CSdlAppServ::SetObserver(MSDLObserver* aObserver)
	{
	iObserver = aObserver;
	}	
	
TInt CSdlAppServ::ObserverEvent(TInt aEvent, TInt aParam)
	{
	if(iObserver != NULL)
		{
		if(RThread().Id() == gEpocEnv->iId)
			{
			return iObserver->SdlThreadEvent(aEvent, aParam);
			}
		else if(RThread().Id() == iMainId)
			{
			return iObserver->SdlEvent(aEvent, aParam);
			}
		PANIC(KErrNotSupported);
		}
	return 0;
	}
	
void CSdlAppServ::PanicMain(TInt aReason)    
    {
    iAppThread.Panic(RThread().Name(), aReason);
    }
    
void CSdlAppServ::PanicMain(const TDesC& aInfo, TInt aReason)    
    {
    iAppThread.Panic(aInfo, aReason);
    }    
    
void CSdlAppServ::ConstructL()
    {
    CActiveScheduler::Add(this);
    User::LeaveIfError(iSema.CreateLocal(1));
    iStatus = KRequestPending;
    iStatusPtr = &iStatus;
    SetActive();
    }
        
 CSdlAppServ::~CSdlAppServ()
    {
    Cancel();
    if(iSema.Handle() != NULL)
        iSema.Signal();
    iSema.Close();
    iAppThread.Close();
    }
    
TInt CSdlAppServ::Request(TInt aService)
    {
    if(RThread().Id() != iAppThread.Id())
    	{
    	iSema.Wait();
    	iService = aService;
    	iAppThread.RequestComplete(iStatusPtr, KErrNone); 
    	return KErrNone;
    	}
    return KErrBadHandle;
    }
    
TInt CSdlAppServ::RequestValue(TInt aService)
    {
    Request(aService);
    Request(EAppSrvNoop);
    return iReturnValue;
    }
   
void CSdlAppServ::Init()
    {
    PANIC_IF_ERROR(iAppThread.Open(iMainId));
    }

void CSdlAppServ::SetParam(TInt aParam)
	{
	iReturnValue = aParam;
	}
	
void CSdlAppServ::HandleObserverValue(TInt aService, TInt aReturnValue, TBool aMainThread)
	{
	if(iObserver != NULL && aMainThread)
		{
		switch(aService)
			{
			case MSDLObserver::EEventScreenSizeChanged:
			if(aReturnValue == MSDLObserver::EScreenSizeChangedDefaultPalette)
				EpocSdlEnv::LockPalette(EFalse);
			break;
			}
		}
	if(!aMainThread && aService == MSDLObserver::EEventSuspend)
		{
		if(iObserver == NULL || 
		(gEpocEnv->iDsa->Stopped() && aReturnValue != MSDLObserver::ESuspendNoSuspend))
			{
			EpocSdlEnv::Suspend();
			}
		}
	}

void CSdlAppServ::RunL()
    {
    if(iStatus == KErrNone)
        {
        switch(iService)
            {
            case CSdlAppServ::EAppSrvWaitDsa:
            	EpocSdlEnv::SetWaitDsa();
            	iReturnValue = EpocSdlEnv::IsDsaAvailable();
            //		}
            //	gEpocEnv->iDsa->Stop();
            //	gEpocEnv->iDsa->RestartL();
            	break;
           	 case CSdlAppServ::EAppSrvStopThread:
            	gEpocEnv->iDsa->SetSuspend();
            	break;
            case EpocSdlEnv::EDisableKeyBlocking:
                EnvUtils::DisableKeyBlocking();
                break;
          
            case EAppSrvWindowPointerCursorMode:
                iReturnValue = gEpocEnv->iDsa != NULL ?
                 gEpocEnv->iDsa->Session().PointerCursorMode() : KErrNotReady; 
                break;
            case EAppSrvDsaStatus:
            	gEpocEnv->iDsa->Stop();
                iReturnValue = KErrNone;
                break;
            case CDsa::ERequestUpdate:
            	gEpocEnv->iDsa->UnlockHWSurfaceRequestComplete();
            	break;
            case EAppSrvNoop:
                break;
            case MSDLObserver::EEventResume:
            case MSDLObserver::EEventSuspend:
            case MSDLObserver::EEventScreenSizeChanged:
            case MSDLObserver::EEventWindowReserved:
            case MSDLObserver::EEventKeyMapInit:
            case MSDLObserver::EEventWindowNotAvailable:
            case MSDLObserver::EEventMainExit:
            	iReturnValue = ObserverEvent(iService, iReturnValue);
            	HandleObserverValue(iService, iReturnValue, ETrue);
            	break;
            default:
                PANIC(KErrNotSupported);
            }
        iStatus = KRequestPending;
        iStatusPtr = &iStatus;
        SetActive();
        }
    iSema.Signal();
    }
    
void CSdlAppServ::DoCancel()
    {
    iSema.Wait();
    TRequestStatus* s = &iStatus;
    iAppThread.RequestComplete(s, KErrCancel); 
    }
 


MEventQueue& EpocSdlEnv::EventQueue()
    {
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    return *gEpocEnv->iEventQueue;
    }


TBool EpocSdlEnv::Flags(TInt aFlag)
    {
	const TInt flag = gEpocEnv->iEpocEnvFlags & aFlag;
	return flag == aFlag;
    }

TInt EpocSdlEnv::Argc()
    {
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    return gEpocEnv->iArgc;
    }
    
    
char** EpocSdlEnv::Argv()
    {
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    return gEpocEnv->iArgv;
    }
    
    
TBool EpocSdlEnv::IsDsaAvailable()
    {
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    return gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->IsDsaAvailable();
    }

  
void EpocSdlEnv::WaitDsaAvailable()
	{
	EpocSdlEnv::ObserverEvent(MSDLObserver::EEventWindowNotAvailable, 0);
	gEpocEnv->iAppSrv->Request(CSdlAppServ::EAppSrvStopThread);
	if(EpocSdlEnv::Flags(CSDL::EEnableFocusStop))
		{
		EpocSdlEnv::ObserverEvent(MSDLObserver::EEventSuspend, 0);
		}
	}
	
void EpocSdlEnv::Suspend()
	{
	if(gEpocEnv->iDsa->Stopped() || EpocSdlEnv::Flags(CSDL::EEnableFocusStop))
		{
	//	gEpocEnv->iDsa->ReleaseStop(); 
		gEpocEnv->iDsa->SetSuspend(); 
		RThread().Suspend();
		EpocSdlEnv::ObserverEvent(MSDLObserver::EEventResume, 0);
		}
	}
	
void EpocSdlEnv::SetWaitDsa()
	{
	if(!IsDsaAvailable())
		{
		RThread th;
		th.Open(gEpocEnv->iId);
		th.Suspend();
		th.Close();
		gEpocEnv->iDsa->SetSuspend(); 
		}
	}
	
void EpocSdlEnv::Resume()
	{
	gEpocEnv->iDsa->Resume();
	RThread th;
	th.Open(gEpocEnv->iId);
	th.Resume();
	th.Close();
	
	const TInt value = gEpocEnv->iAppSrv->ObserverEvent(MSDLObserver::EEventResume, 0);
	gEpocEnv->iAppSrv->HandleObserverValue(MSDLObserver::EEventResume, value, ETrue);
	}
    

TInt EpocSdlEnv::AllocSwSurface(const TSize& aSize, TDisplayMode aMode)
	{
	return gEpocEnv->iDsa->AllocSurface(EFalse, aSize, aMode);
	}
	
TInt EpocSdlEnv::AllocHwSurface(const TSize& aSize, TDisplayMode aMode)
	{
	return gEpocEnv->iDsa->AllocSurface(ETrue, aSize, aMode);
	}
		
	
void EpocSdlEnv::UnlockHwSurface()
	{
	gEpocEnv->iDsa->UnlockHwSurface();
	}
	
TUint8* EpocSdlEnv::LockHwSurface()
	{
	return gEpocEnv->iDsa->LockHwSurface();
	}


void EpocSdlEnv::UpdateSwSurface()
	{
	gEpocEnv->iDsa->UpdateSwSurface();
	}
	
TBool EpocSdlEnv::AddUpdateRect(TUint8* aAddress, const TRect& aUpdateRect, const TRect& aRect)
	{
	return gEpocEnv->iDsa->AddUpdateRect(aAddress, aUpdateRect, aRect);
	}
	
void EpocSdlEnv::Request(TInt aService)
    {
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    gEpocEnv->iAppSrv->Request(aService);
    }
    
    
TSize EpocSdlEnv::WindowSize(const TSize& aRequestedSize)
    { 
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    if(EpocSdlEnv::Flags(CSDL::EAllowImageResize) && gEpocEnv->iDsa->WindowSize() != aRequestedSize)
    	{
    	TRAP_IGNORE(gEpocEnv->iDsa->CreateZoomerL(aRequestedSize));
    	}
    return gEpocEnv->iDsa->WindowSize();
    }
    
 TSize EpocSdlEnv::WindowSize()
    { 
    __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
    return gEpocEnv->iDsa->WindowSize();
    }   
    
TDisplayMode EpocSdlEnv::DisplayMode()
    {
    return gEpocEnv->iDsa->DisplayMode();
    }
    
TPointerCursorMode EpocSdlEnv::PointerMode()
    {
    return static_cast<TPointerCursorMode>
    (gEpocEnv->iAppSrv->RequestValue(CSdlAppServ::EAppSrvWindowPointerCursorMode));
    }
    
TInt EpocSdlEnv::SetPalette(TInt aFirstcolor, TInt aColorCount, TUint32* aPalette)  
	{
	return 	gEpocEnv->iDsa->SetPalette(aFirstcolor, aColorCount, aPalette);
	}

void EpocSdlEnv::PanicMain(TInt aErr)
    {
    gEpocEnv->iAppSrv->PanicMain(aErr);
    }
    
    
TInt EpocSdlEnv::AppendCleanupItem(const TSdlCleanupItem& aItem)
    {
    TRAPD(err, gEpocEnv->iCleanupItems->AppendL(aItem));
    return err;
    }
    
void EpocSdlEnv::RemoveCleanupItem(TAny* aItem)
    {
    for(TInt i = 0; i < gEpocEnv->iCleanupItems->Count(); i++)
        {
        if(gEpocEnv->iCleanupItems->At(i).iItem == aItem)
            gEpocEnv->iCleanupItems->Delete(i);
        }
    }
    
void EpocSdlEnv::CleanupItems()     
	{
	const TThreadId id = RThread().Id();
	TInt last = gEpocEnv->iCleanupItems->Count() - 1;
	TInt i;
	for(i = last; i >= 0 ; i--)
        {
        TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
        if(item.iThread == id)
        	{
        	item.iThread = TThreadId(0); 
        	item.iOperation(item.iItem);
        	}
        }
    last = gEpocEnv->iCleanupItems->Count() - 1;
	for(i = last; i >= 0 ; i--)
        {
        TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
        if(item.iThread == TThreadId(0))
        	{
        	gEpocEnv->iCleanupItems->Delete(i);
        	}
        }
	}
	
void EpocSdlEnv::FreeSurface()
	{
	Request(CSdlAppServ::EAppSrvDsaStatus);
	gEpocEnv->iDsa->Free();
	}
  
void EpocSdlEnv::LockPalette(TBool aLock)
	{
	gEpocEnv->iDsa->LockPalette(aLock);
	}
    
void EpocSdlEnv::ObserverEvent(TInt aService, TInt aParam)
	{
	const TBool sdlThread = RThread().Id() == gEpocEnv->iId;
	const TInt valuea = gEpocEnv->iAppSrv->ObserverEvent(aService, aParam);
	gEpocEnv->iAppSrv->HandleObserverValue(aService, valuea, !sdlThread);
	if(sdlThread)
		{
		gEpocEnv->iAppSrv->SetParam(aParam);
		const TInt valuet = gEpocEnv->iAppSrv->RequestValue(aService);
		gEpocEnv->iAppSrv->HandleObserverValue(aService, valuet, EFalse);	
		}
	}
			
    
TPoint EpocSdlEnv::WindowCoordinates(const TPoint& aPoint)    
    {
    return gEpocEnv->iDsa->WindowCoordinates(aPoint);	
    }
    
void EpocSdlEnv::PanicMain(const TDesC& aInfo, TInt aErr)
    {
    gEpocEnv->iAppSrv->PanicMain(aInfo, aErr);
    }
//Dsa is a low priority ao, it has to wait if its pending event, but ws
//event has been prioritized before it
//this is not called from app thread!
void EpocSdlEnv::WaitDeviceChange() 
    {
  	LockPalette(ETrue);
    gEpocEnv->iAppSrv->RequestValue(CSdlAppServ::EAppSrvWaitDsa);
    const TSize sz = WindowSize();
    const TInt param = reinterpret_cast<TInt>(&sz);
    ObserverEvent(MSDLObserver::EEventScreenSizeChanged, param);
    	
   // RThread().Suspend();
    }  
    
LOCAL_C TBool CheckSdl() 
    {
    TInt isExit = ETrue;
    RThread sdl;
    if(sdl.Open(gEpocEnv->iId) == KErrNone)
        {
        if(sdl.ExitType() == EExitPending)
            {
            isExit = EFalse;
            }
        sdl.Close();
        }
    return isExit;
    }
    
void EpocSdlEnvData::Free()
    {
    if(RThread().Id() == gEpocEnv->iId)
    	{
    	iDsa->Free();
    	return;
    	}
   
    __ASSERT_ALWAYS(iArgv == NULL || CheckSdl(), PANIC(KErrNotReady));
        
    for(TInt i = 0; i < iArgc; i++)
        User::Free( iArgv[i] );
        
    User::Free(iArgv);	
     
    
    delete iEventQueue;
    
    if(iDsa != NULL)
    	iDsa->Free();
    
	delete iDsa;
	delete iAppSrv;
    }

_LIT(KSDLMain, "SDLMain");

LOCAL_C int MainL()
    {
    gEpocEnv->iCleanupItems = new (ELeave) CArrayFixFlat<TSdlCleanupItem>(8);
    
    char** envp=0;
     /* !! process exits here if there is "exit()" in main! */
    int ret = 0;
    for(TInt i = 0; i  < 6; i++)
        {
        void* f = (void*) gEpocEnv->iMain[i];
        if(f != NULL)
            {
            switch(i)
                {
                case 0:
                    ret = ((mainfunc1)f)(); 
                    return ret;
                case 3:
                    ((mainfunc1)f)(); 
                    return ret;
                case 1:
                    ret = ((mainfunc2)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv());
                    return ret;
                case 4:
                    ((mainfunc2)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv());
                    return ret;
                case 2:
                    ret = ((mainfunc3)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv(), envp);
                    return ret;
                case 5:
                    ((mainfunc3)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv(), envp);
                    return ret;
                }
            }
        }
    PANIC(KErrNotFound);
    return 0;
    }

LOCAL_C TInt DoMain(TAny* /*aParam*/)
    {
    
    
    CTrapCleanup* cleanup = CTrapCleanup::New();
      	
	TBool fbsconnected = EFalse;
	if(RFbsSession::GetSession() == NULL)
	    {
	    PANIC_IF_ERROR(RFbsSession::Connect());
	    fbsconnected = ETrue;
	    }
	
 	gEpocEnv->iAppSrv->Init();	

#ifdef SYMBIANC 
    // Create stdlib 
    _REENT;
#endif

    // Call stdlib main
    int ret = 0;
    
    //completes waiting rendesvous
    RThread::Rendezvous(KErrNone);
    
    TRAPD(err, err = MainL());
    
    EpocSdlEnv::ObserverEvent(MSDLObserver::EEventMainExit, err);
   
    // Free resources and return
    
  	EpocSdlEnv::CleanupItems();
        
    gEpocEnv->iCleanupItems->Reset();
    delete gEpocEnv->iCleanupItems;
    gEpocEnv->iCleanupItems = NULL;
    
    gEpocEnv->Free(); //free up in thread resources 
    
#ifdef SYMBIANC    
    _cleanup(); //this is normally called at exit, I call it here
#endif

    if(fbsconnected)
        RFbsSession::Disconnect();
    
#ifdef SYMBIANC     
    CloseSTDLIB();
#endif
       
 //   delete as;
   	delete cleanup;	

    return err == KErrNone ? ret : err;;
    }
    

    
EXPORT_C CSDL::~CSDL()
    {
   	gEpocEnv->Free();
    User::Free(gEpocEnv);
    gEpocEnv->iSdl = NULL;
    }

EXPORT_C CSDL* CSDL::NewL(TInt aFlags)
    {
    __ASSERT_ALWAYS(gEpocEnv == NULL, PANIC(KErrAlreadyExists));
    gEpocEnv = (EpocSdlEnvData*) User::AllocL(sizeof(EpocSdlEnvData));
    Mem::FillZ(gEpocEnv, sizeof(EpocSdlEnvData));
   
    gEpocEnv->iEpocEnvFlags = aFlags;
    gEpocEnv->iEventQueue = CEventQueue::NewL();
   
    gEpocEnv->iAppSrv = new (ELeave) CSdlAppServ();
    gEpocEnv->iAppSrv->ConstructL();
    
    CSDL* sdl = new (ELeave) CSDL();
    
    gEpocEnv->iSdl = sdl;
    
    return sdl;
    }
    
  /*  
EXPORT_C void CSDL::ReInitL(TFlags aFlags)
	{
	const TFlags prevFlags = gEpocEnv->iEpocEnvFlags;
	gEpocEnv->iEpocEnvFlags = aFlags;
	TInt err = KErrNone;
	if(((prevFlags & EDrawModeDSB) != (aFlags & EDrawModeDSB)) && gEpocEnv->iDsa)
		{
		delete gEpocEnv->iDsa;
		gEpocEnv->iDsa = NULL;
		gEpocEnv->iDsa = CDsa::RecreateL(EpocSdlEnv::Flags(CSDL::EDrawModeDSB));
		}
	}
 */


EXPORT_C void CSDL::SetContainerWindowL(RWindow& aWindow, RWsSession& aSession, CWsScreenDevice& aDevice)
    {
    if(gEpocEnv->iDsa == NULL)
    	gEpocEnv->iDsa = CDsa::CreateL(aSession);
    gEpocEnv->iDsa->ConstructL(aWindow, aDevice);
    }
        
   
EXPORT_C TThreadId CSDL::CallMainL(const TMainFunc& aFunc, TRequestStatus* const aStatus, const CDesC8Array* const aArg, TInt aFlags, TInt aStackSize)
    {
    ASSERT(gEpocEnv != NULL);
    gEpocEnv->iMain = aFunc;
    const TBool args = aArg != NULL;
    
    gEpocEnv->iArgc = aArg->Count() + 1;
    gEpocEnv->iArgv = (char**) User::AllocL(sizeof(char*) * (gEpocEnv->iArgc + 1));  
      
    TInt k = 0;
    const TFileName processName = RProcess().FileName();
    const TInt len = processName.Length();
    gEpocEnv->iArgv[k] = (char*) User::AllocL(len + 1);
    Mem::Copy(gEpocEnv->iArgv[k], processName.Ptr(), len);
    gEpocEnv->iArgv[k][len] = 0;
      
    for(TInt i =  0; args && (i < aArg->Count()); i++)
        {
        k++;
        const TInt len = aArg->MdcaPoint(i).Length();
        gEpocEnv->iArgv[k] = (char*) User::AllocL(len + 1);
        Mem::Copy(gEpocEnv->iArgv[k], aArg->MdcaPoint(i).Ptr(), len);
        gEpocEnv->iArgv[k][len] = 0;
        }
        
    gEpocEnv->iArgv[gEpocEnv->iArgc] = NULL;
         
    RThread thread;
    User::LeaveIfError(thread.Create(KSDLMain, DoMain, aStackSize, NULL, NULL));
    
    if(aStatus != NULL)
    	{
    	thread.Logon(*aStatus);
    	}
    	
    gEpocEnv->iId = thread.Id();
    thread.SetPriority(EPriorityLess);
    if((aFlags & CSDL::ERequestResume) == 0)
        {
        thread.Resume();
        }
    thread.Close();
    return gEpocEnv->iId;
    }
    
EXPORT_C TInt CSDL::AppendWsEvent(const TWsEvent& aEvent)
    {
    return EpocSdlEnv::EventQueue().Append(aEvent);
    }
    
EXPORT_C void CSDL::SDLPanic(const TDesC& aInfo, TInt aErr)
    {
    EpocSdlEnv::PanicMain(aInfo, aErr);
    }
    
EXPORT_C TInt CSDL::GetSDLCode(TInt aScanCode)
    {
    if(aScanCode < 0)
        return MAX_SCANCODE;
    if(aScanCode >= MAX_SCANCODE)
        return -1;
    return KeyMap()[aScanCode];
    }
    
EXPORT_C TInt CSDL::SDLCodesCount() const
	{
	return MAX_SCANCODE;
	}
	
EXPORT_C void CSDL::ResetSDLCodes()
	{
	ResetKeyMap();
	}
    
EXPORT_C void CSDL::SetOrientation(TOrientationMode aMode)
	{
	gEpocEnv->iDsa->SetOrientation(aMode);
	}
    
EXPORT_C TInt CSDL::SetSDLCode(TInt aScanCode, TInt aSDLCode)
    {
    const TInt current = GetSDLCode(aScanCode);
    if(aScanCode >= 0 && aScanCode < MAX_SCANCODE)
        KeyMap()[aScanCode] = static_cast<SDLKey>(aSDLCode);
    return current;
    }


EXPORT_C MSDLObserver* CSDL::Observer()
	{
	return gEpocEnv->iAppSrv->Observer();
	}    
    
EXPORT_C void CSDL::SetObserver(MSDLObserver* aObserver)
	{
	gEpocEnv->iAppSrv->SetObserver(aObserver);
	}
	
EXPORT_C void CSDL::Resume()
	{
	EpocSdlEnv::Resume();
	}
	
EXPORT_C void CSDL::Suspend()
	{
	gEpocEnv->iDsa->DoStop();
	}
	
EXPORT_C CSDL::CSDL()
    {
    }

EXPORT_C void CSDL::DisableKeyBlocking(CAknAppUi& aAppUi) const
	{
	gEpocEnv->iAppUi = &aAppUi;
	EnvUtils::DisableKeyBlocking();
	}

EXPORT_C TInt CSDL::SetBlitter(MBlitter* aBlitter)
	{
	if(gEpocEnv && gEpocEnv->iDsa)
		{
		gEpocEnv->iDsa->SetBlitter(aBlitter);
		return KErrNone;
		}
	return KErrNotReady;
	}
		
	
EXPORT_C TInt CSDL::AppendOverlay(MOverlay& aOverlay, TInt aPriority)
	{
	if(gEpocEnv && gEpocEnv->iDsa)
		{
		return gEpocEnv->iDsa->AppendOverlay(aOverlay, aPriority);
		}
	return KErrNotReady;
	}

EXPORT_C TInt CSDL::RemoveOverlay(MOverlay& aOverlay)	
	{
	if(gEpocEnv && gEpocEnv->iDsa)
		{
		return gEpocEnv->iDsa->RemoveOverlay(aOverlay);
		}
	return KErrNotReady;
	}

EXPORT_C TInt CSDL::RedrawRequest()
	{
	if(gEpocEnv && gEpocEnv->iDsa)
		{
		return gEpocEnv->iDsa->RedrawRequest();
		}
	return KErrNotReady;
	}
	
/*
EXPORT_C CSDL* CSDL::Current()
    {
    return gEpocEnv != NULL ? gEpocEnv->iSdl : NULL;
    }

    
EXPORT_C TInt CSDL::SetVolume(TInt aVolume)
    {
    return EpocSdlEnv::SetVolume(aVolume);
    } 
    
EXPORT_C TInt CSDL::Volume() const
    {
    return EpocSdlEnv::Volume();
    }     
	
EXPORT_C TInt CSDL::MaxVolume() const
    {
    return EpocSdlEnv::MaxVolume();
    } 	
*/
			
void EnvUtils::DisableKeyBlocking()
	{
	if(gEpocEnv->iAppUi != NULL)
		return CCurrentAppUi::Cast(gEpocEnv->iAppUi)->DisableKeyBlocking();
	}
	
TBool EnvUtils::Rendezvous(RThread& aThread, TRequestStatus& aStatus)
	{
	if(gEpocEnv->iId != TThreadId(0) &&
    		 	aThread.Open(gEpocEnv->iId) &&
    		  	aThread.ExitType() == EExitPending)
    			{
    			aThread.Rendezvous(aStatus);
    			return ETrue;
    			}
    return EFalse;
	}