javauis/lcdui_akn/javalcdui/src/CMIDEnv.cpp
branchRCL_3
changeset 19 04becd199f91
child 60 6c158198356e
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <coemain.h>
       
    19 #include <eikenv.h>
       
    20 #include <hal.h>
       
    21 #include "CMIDEnv.h"
       
    22 #include "CMIDToolkit.h"
       
    23 #include "CMIDNotify.h"
       
    24 #include "CMIDKeyTranslator.h"
       
    25 #include "CMIDToLcduiObserver.h"
       
    26 
       
    27 #ifdef RD_JAVA_NGA_ENABLED
       
    28 #include <EGL/egl.h>
       
    29 #include <GLES/gl.h>
       
    30 #include <libc/string.h>
       
    31 #endif // RD_JAVA_NGA_ENABLED
       
    32 
       
    33 const TUint16 KCharComma = ',';
       
    34 
       
    35 CMIDEnv::CMIDEnv(CMIDToolkit* aToolkit, const TSize& aSize)
       
    36         : iToolkit(aToolkit)
       
    37         , iObservers(EGranularity)
       
    38         , iCanvasAssumedSize(aSize)
       
    39         , iToLcduiObserver(NULL)
       
    40 #ifdef RD_JAVA_NGA_ENABLED
       
    41         , iHardwareStatus(0)
       
    42 #endif
       
    43 {
       
    44     iCanvasData.iLock.CreateLocal();
       
    45 }
       
    46 
       
    47 void CMIDEnv::ConstructL()
       
    48 {
       
    49     iKeyTranslator = new(ELeave) CMIDKeyTranslator(*this);
       
    50     iKeyTranslator->ConstructL();
       
    51     iToLcduiObserver = new(ELeave) CMIDToLcduiObserver();
       
    52 
       
    53     // On a non-full-screen (normal) mode Canvas the background image must be initially shown,
       
    54     // if the value of the Nokia-UI-Enhancement attribute is "CanvasHasBackground". The
       
    55     // attribute may be placed in the JAD or the manifest.
       
    56     iCanvasData.iHasBackground = MidletAttributeContainsVal(
       
    57                                      LcduiMidletAttributes::KAttribUIEnhancement,
       
    58                                      LcduiMidletAttributeValues::KUIEnhCanvasBackground);
       
    59 #ifdef RD_JAVA_NGA_ENABLED
       
    60     InitHardwareStatusL();
       
    61 #endif // RD_JAVA_NGA_ENABLED
       
    62 }
       
    63 
       
    64 CMIDEnv::~CMIDEnv()
       
    65 {
       
    66     if (iToLcduiObserver)
       
    67     {
       
    68         delete iToLcduiObserver;
       
    69         iToLcduiObserver = NULL;
       
    70     }
       
    71     delete iKeyTranslator;
       
    72     iObservers.Close();
       
    73     iCanvasData.iLock.Close();
       
    74 }
       
    75 
       
    76 void CMIDEnv::SetUtils(MMIDUtils* aUtils)
       
    77 {
       
    78     iKeyTranslator->SetUtils(aUtils);
       
    79 }
       
    80 
       
    81 TPtrC CMIDEnv::MidletName() const
       
    82 {
       
    83     return iToolkit->MidletName();
       
    84 }
       
    85 
       
    86 TUid CMIDEnv::MidletUid() const
       
    87 {
       
    88     return iToolkit->MidletUid();
       
    89 }
       
    90 
       
    91 TPtrC CMIDEnv::MidletHome() const
       
    92 {
       
    93     return iToolkit->MidletHome();
       
    94 }
       
    95 
       
    96 #ifdef RD_SCALABLE_UI_V2
       
    97 TUid CMIDEnv::MidletSuiteUid()
       
    98 {
       
    99     if (!iMidletSuiteUid.iUid)
       
   100     {
       
   101         //Midlet suite uid is located inside brackets in the midlet home path.
       
   102         const TChar KUidStart = '[';
       
   103         const TChar KUidEnd = ']';
       
   104 
       
   105         const TPtrC midletHome = MidletHome();
       
   106 
       
   107         //Finding start and end positions
       
   108         TInt startPos = midletHome.Locate(KUidStart) + 1;
       
   109         TInt endPos = midletHome.LocateReverse(KUidEnd);
       
   110 
       
   111         if (startPos != KErrNotFound && endPos != KErrNotFound &&
       
   112                 startPos < endPos && endPos <= midletHome.Length())
       
   113         {
       
   114             //Creating new pointer descriptor representing the midlet suite uid
       
   115             const TPtrC uidDesc = midletHome.Mid(startPos, endPos - startPos);
       
   116 
       
   117             if (uidDesc.Length())
       
   118             {
       
   119                 //Creating unsigned integer out of the hex string.
       
   120                 TUint uint;
       
   121                 TLex lexer(uidDesc);
       
   122                 TInt err = lexer.Val(uint, EHex);
       
   123 
       
   124                 if (err == KErrNone)
       
   125                 {
       
   126                     iMidletSuiteUid = TUid::Uid(uint);
       
   127                 }
       
   128                 else
       
   129                 {
       
   130                     iMidletSuiteUid = TUid::Uid(KNullUidValue);
       
   131                 }
       
   132             }
       
   133             else
       
   134             {
       
   135                 iMidletSuiteUid = TUid::Uid(KNullUidValue);
       
   136             }
       
   137         }
       
   138         else
       
   139         {
       
   140             iMidletSuiteUid = TUid::Uid(KNullUidValue);
       
   141         }
       
   142     }
       
   143     return iMidletSuiteUid;
       
   144 }
       
   145 #endif // RD_SCALABLE_UI_V2
       
   146 
       
   147 
       
   148 TBool CMIDEnv::PostJavaEvent
       
   149 (
       
   150     MMIDComponent& aSource,
       
   151     TSourceType aSourceType,
       
   152     TEventType  aEventType,
       
   153     TInt        aEventData,
       
   154     TInt        aEventData1,
       
   155     TInt        aEventData2
       
   156 )
       
   157 {
       
   158 #ifdef TRACE_EVENTS
       
   159     RDebug::Print(_L("CMIDEnv::PostJavaEvent(%x,%d,%d,%d,%d,%d)"),
       
   160                   aSource,
       
   161                   aSourceType,
       
   162                   aEventType,
       
   163                   aEventData,aEventData1, aEventData2);
       
   164 #endif
       
   165     switch (aSourceType)
       
   166     {
       
   167     case EItem:
       
   168         return iToolkit->PostItemEvent(aSource, aEventType, aEventData, aEventData1, aEventData2);
       
   169     case EDisplayable:
       
   170         return iToolkit->PostDisplayableEvent(aSource, aEventType, aEventData, aEventData1);
       
   171     case ECanvasGraphicsItemPainterEvent:
       
   172         return iToolkit->PostCanvasGraphicsItemPainterEvent(aSource, aEventType, aEventData, aEventData1);
       
   173     default:
       
   174         ASSERT(EFalse);
       
   175     }
       
   176     return EFalse;
       
   177 }
       
   178 
       
   179 TBool CMIDEnv::PostJavaEvent(MMIDComponent& aSource,TSourceType aSourceType,TEventType aEventType,TInt aEventData)
       
   180 {
       
   181     return PostJavaEvent(aSource, aSourceType, aEventType, aEventData, 0, 0);
       
   182 }
       
   183 
       
   184 TBool CMIDEnv::PostJavaEvent(MMIDComponent& aSource,TSourceType aSourceType,TInt aEventData)
       
   185 {
       
   186     return PostJavaEvent(aSource, aSourceType, ENoType, aEventData, 0, 0);
       
   187 }
       
   188 
       
   189 TBool CMIDEnv::PostMidletEvent(TEventType aEventType)
       
   190 {
       
   191     return iToolkit->PostDisplayEvent(aEventType);
       
   192 }
       
   193 
       
   194 MMIDNotifyEvent* CMIDEnv::NewNotifyL(MMIDComponent& aSource)
       
   195 {
       
   196     return new(ELeave) CMIDNotifyEvent(iToolkit->ComponentPeer(aSource));
       
   197 }
       
   198 
       
   199 TBool   CMIDEnv::PostJavaNotify(MMIDNotifyEvent* aNotifyEvent)
       
   200 {
       
   201     CMIDNotifyEvent* notify = static_cast<CMIDNotifyEvent*>(aNotifyEvent);
       
   202     return iToolkit->PostEvent(notify, CJavaEventBase::ENotifyPriority);
       
   203 }
       
   204 
       
   205 TBool CMIDEnv::TranslateKeyL(TMIDKeyEvent& aEvent, const TKeyEvent& aWsEvent, TEventCode aType)
       
   206 {
       
   207     return iKeyTranslator->TranslateL(aEvent, aWsEvent, aType);
       
   208 }
       
   209 
       
   210 TBool CMIDEnv::PostKeyEvent(MMIDComponent& aSource, TMIDKeyEvent& aEvent)
       
   211 {
       
   212     return iKeyTranslator->PostKeyEvent(aSource, aEvent);
       
   213 }
       
   214 
       
   215 void CMIDEnv::ResetKeys()
       
   216 {
       
   217     iKeyTranslator->Reset();
       
   218 }
       
   219 
       
   220 MMIDDisplayable* CMIDEnv::Current()
       
   221 {
       
   222     return iToolkit->Current();
       
   223 }
       
   224 
       
   225 /**
       
   226  * Set the zoom size which may be queried by Canvas in the
       
   227  * plugin. This is a relic from when Canvas was implemented
       
   228  * in the framework. Should be deprecated.
       
   229  */
       
   230 void CMIDEnv::SetCanvasZoomSize(const TSize& aSize)
       
   231 {
       
   232     iCanvasZoomSize=aSize;
       
   233 }
       
   234 
       
   235 /**
       
   236  * Get the zoom size - used by the Canvas in the plugin.
       
   237  * Relic from when Canvas was implementd in the framework.
       
   238  * Should be deprecated.
       
   239  */
       
   240 TSize CMIDEnv::CanvasZoomSize()
       
   241 {
       
   242     return iCanvasZoomSize;
       
   243 }
       
   244 
       
   245 TSize CMIDEnv::CanvasAssumedSize()
       
   246 {
       
   247     return iCanvasAssumedSize;
       
   248 }
       
   249 
       
   250 TInt CMIDEnv::NumColors()
       
   251 {
       
   252     TDisplayMode mode = DisplayMode();
       
   253     return TDisplayModeUtils::NumDisplayModeColors(mode);
       
   254 }
       
   255 
       
   256 TDisplayMode CMIDEnv::DisplayMode()
       
   257 {
       
   258     MMIDGraphicsFactory& factory = iToolkit->GraphicsFactory();
       
   259     return factory.DisplayMode();
       
   260 }
       
   261 
       
   262 void CMIDEnv::AddObserverL(MMIDEnvObserver& aObserver)
       
   263 {
       
   264     ASSERT(iObservers.Find(&aObserver) == KErrNotFound);
       
   265     //
       
   266     User::LeaveIfError(iObservers.Append(&aObserver));
       
   267 }
       
   268 
       
   269 void CMIDEnv::RemoveObserver(MMIDEnvObserver& aObserver)
       
   270 {
       
   271     TInt index = iObservers.Find(&aObserver);
       
   272     if (index != KErrNotFound)
       
   273     {
       
   274         iObservers.Remove(index);
       
   275     }
       
   276 }
       
   277 
       
   278 void CMIDEnv::HandleSwitchOnL(TBool aSwitchOn)
       
   279 {
       
   280     for (TInt i = iObservers.Count(); i--;)
       
   281         iObservers[i]->HandleSwitchOnL(aSwitchOn);
       
   282 }
       
   283 
       
   284 void CMIDEnv::HandleForegroundL(TBool aForeground)
       
   285 {
       
   286     for (TInt i = iObservers.Count(); i--;)
       
   287     {
       
   288         iObservers[i]->HandleForegroundL(aForeground);
       
   289     }
       
   290     if (aForeground && iToolkit && iToolkit->Utils())
       
   291     {
       
   292         iToolkit->Utils()->HandleForegroundL(aForeground);
       
   293     }
       
   294 }
       
   295 
       
   296 void CMIDEnv::HandleResourceChangeL(TInt aType)
       
   297 {
       
   298     for (TInt i = iObservers.Count(); i--;)
       
   299     {
       
   300         iObservers[i]->HandleResourceChangeL(aType);
       
   301     }
       
   302     if (iToolkit && iToolkit->Utils())
       
   303     {
       
   304         iToolkit->Utils()->HandleResourceChangedL();
       
   305     }
       
   306 
       
   307 }
       
   308 
       
   309 TInt CMIDEnv::MidletAttribute(const TDesC& aAttributeName, TPtrC& aAttributeValue)
       
   310 {
       
   311     return iToolkit->MidletAttribute(aAttributeName,aAttributeValue);
       
   312 }
       
   313 
       
   314 TBool CMIDEnv::MidletAttributeIsSetToVal(const TDesC& aAttributeName,
       
   315         const TDesC& aAttributeValue)
       
   316 {
       
   317     TPtrC attributeValue;
       
   318 
       
   319     if (MidletAttribute(aAttributeName, attributeValue) == KErrNone &&
       
   320             attributeValue.CompareF(aAttributeValue) == 0)
       
   321     {
       
   322         return ETrue;
       
   323     }
       
   324     else
       
   325     {
       
   326         return EFalse;
       
   327     }
       
   328 }
       
   329 
       
   330 TBool CMIDEnv::MidletAttributeContainsVal(const TDesC& aAttributeName,
       
   331         const TDesC& aAttributeValue)
       
   332 {
       
   333     TPtrC attributeValue;
       
   334 
       
   335     if (MidletAttribute(aAttributeName, attributeValue) == KErrNone)
       
   336     {
       
   337         TInt startIdx = attributeValue.FindF(aAttributeValue);
       
   338         if (startIdx != KErrNotFound)
       
   339         {
       
   340             if (aAttributeName.FindF(LcduiMidletAttributes::KAttribUIEnhancement) == KErrNotFound)
       
   341             {
       
   342                 // If there is a preceding character before the value substring, it must be a comma
       
   343                 if (startIdx == 0 || attributeValue[ startIdx - 1 ] == KCharComma)
       
   344                 {
       
   345                     // Check what comes after the found value substring.
       
   346                     TInt endIdx = startIdx + aAttributeValue.Length();
       
   347                     if (endIdx < attributeValue.Length())
       
   348                     {
       
   349                         // only comma is ok
       
   350                         if (attributeValue[ endIdx ] == KCharComma)
       
   351                         {
       
   352                             return ETrue;
       
   353                         }
       
   354                     }
       
   355                 }
       
   356                 else // the found substring was right at the end of attributeValue string
       
   357                 {
       
   358                     return ETrue;
       
   359                 }
       
   360             }
       
   361             else // it is "Nokia-UI-Enhancement" - it is enough just to identify the substring in list of values
       
   362             {
       
   363                 return ETrue;
       
   364             }
       
   365         }
       
   366     }
       
   367 
       
   368     return EFalse;
       
   369 }
       
   370 
       
   371 void CMIDEnv::SetCanvasAssumedSize(const TSize& aSize)
       
   372 {
       
   373     iCanvasAssumedSize = aSize;
       
   374 }
       
   375 CFbsBitmap* CMIDEnv::ReserveCanvasFrameBufferL(MMIDCanvas& aCanvas, const TSize& aSz)
       
   376 {
       
   377     CMIDEnv::TMutexAutoLock lock(&iCanvasData.iLock);
       
   378     if (!aCanvas.IsGameCanvas())
       
   379     {
       
   380         if (!iCanvasData.iFrameBuffer)
       
   381         {
       
   382             iCanvasData.iFrameBuffer = CreateCanvasFrameBufferL(aCanvas, ETrue, aSz);
       
   383         }
       
   384         iCanvasData.iRefCount++;
       
   385         return iCanvasData.iFrameBuffer;
       
   386     }
       
   387     return CreateCanvasFrameBufferL(aCanvas, ETrue, aSz);
       
   388 }
       
   389 
       
   390 void CMIDEnv::ReleaseCanvasFrameBuffer(MMIDCanvas& aCanvas, CFbsBitmap*& aFrameBuffer)
       
   391 {
       
   392     CMIDEnv::TMutexAutoLock lock(&iCanvasData.iLock);
       
   393     if (!aCanvas.IsGameCanvas())
       
   394     {
       
   395         if (iCanvasData.iFrameBuffer)
       
   396         {
       
   397             iCanvasData.iRefCount--;
       
   398             if (iCanvasData.iRefCount <= 0)
       
   399             {
       
   400                 iCanvasData.Reset();
       
   401             }
       
   402         }
       
   403     }
       
   404     else
       
   405     {
       
   406         delete aFrameBuffer;
       
   407         aFrameBuffer = NULL;
       
   408     }
       
   409 }
       
   410 
       
   411 CFbsBitmap* CMIDEnv::CreateCanvasFrameBufferL(
       
   412     MMIDCanvas& /* aCanvas */, TBool aWsBitmap, const TSize& aSz)
       
   413 {
       
   414     CFbsBitmap* bitmap = new(ELeave) CFbsBitmap;
       
   415     CleanupStack::PushL(bitmap);
       
   416     TInt err = KErrGeneral;
       
   417     err = bitmap->CreateHardwareBitmap(aSz, DisplayMode(), MidletUid());
       
   418     if (KErrNone != err)
       
   419     {
       
   420         // Failed to create hardware bitmap. Fall back to normal software only bitmaps
       
   421         User::LeaveIfError(bitmap->Create(aSz, DisplayMode()));
       
   422     }
       
   423     if (!aWsBitmap)
       
   424     {
       
   425         CleanupStack::Pop();
       
   426         return bitmap;
       
   427     }
       
   428     CWsBitmap* wsBitmap =
       
   429         new(ELeave) CWsBitmap(CCoeEnv::Static()->WsSession());
       
   430     err = wsBitmap->Duplicate(bitmap->Handle());
       
   431     if (KErrNone != err)
       
   432     {
       
   433         delete wsBitmap;
       
   434         User::Leave(err);
       
   435     }
       
   436     CleanupStack::PopAndDestroy(bitmap);
       
   437     return wsBitmap;
       
   438 }
       
   439 
       
   440 TBool CMIDEnv::CanvasHasBackground(const MMIDCanvas& /* aCanvas */) const
       
   441 {
       
   442     return iCanvasData.iHasBackground;
       
   443 }
       
   444 
       
   445 #ifdef RD_JAVA_NGA_ENABLED
       
   446 // ---------------------------------------------------------------------------
       
   447 //
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 TBool CMIDEnv::IsHardwareAcceleratedL(MMIDEnv::THardwareType aHardwareType)
       
   451 {
       
   452     ASSERT(iHardwareStatus);
       
   453     return (iHardwareStatus & aHardwareType);
       
   454 }
       
   455 
       
   456 // ---------------------------------------------------------------------------
       
   457 // Checks from OpenGL ES API, if it is HW accelerated
       
   458 // ---------------------------------------------------------------------------
       
   459 //
       
   460 TInt CMIDEnv::InitHardwareStatusL()
       
   461 {
       
   462     iHardwareStatus = 1;
       
   463 
       
   464     EGLDisplay eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
       
   465     eglInitialize(eglDisplay, NULL, NULL);
       
   466 
       
   467     EGLConfig config;
       
   468     EGLint numOfConfigs = 0;
       
   469     // Choose suitable display config
       
   470     const EGLint attribs[] =
       
   471     {
       
   472         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
       
   473         EGL_BUFFER_SIZE, 32,
       
   474         EGL_DEPTH_SIZE, 16,
       
   475         EGL_NONE
       
   476     };
       
   477     eglChooseConfig(eglDisplay, attribs, &config, 1, &numOfConfigs);
       
   478     // Create a surface
       
   479     const EGLint pBuffAttribs[] =
       
   480     {
       
   481         EGL_WIDTH, 1,
       
   482         EGL_HEIGHT, 1,
       
   483         EGL_NONE
       
   484     };
       
   485     EGLSurface surface = eglCreatePbufferSurface(eglDisplay, config, pBuffAttribs);
       
   486     // Create a context into this thread.
       
   487     EGLContext context = eglCreateContext(eglDisplay, config, EGL_NO_CONTEXT, NULL);
       
   488     // Try to bind surface to context
       
   489     if (context != EGL_NO_CONTEXT && surface != EGL_NO_SURFACE &&
       
   490             eglMakeCurrent(eglDisplay, surface, surface, context))
       
   491     {
       
   492         if (strstr((const char *)glGetString(GL_RENDERER), "HW"))
       
   493         {
       
   494             iHardwareStatus |= MMIDEnv::EHardware3D;
       
   495         }
       
   496         eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
       
   497     }
       
   498 
       
   499     if (context != EGL_NO_CONTEXT)
       
   500     {
       
   501         eglDestroyContext(eglDisplay, context);
       
   502     }
       
   503 
       
   504     if (surface != EGL_NO_SURFACE)
       
   505     {
       
   506         eglDestroySurface(eglDisplay, surface);
       
   507     }
       
   508 
       
   509     eglTerminate(eglDisplay);
       
   510 
       
   511     return iHardwareStatus;
       
   512 }
       
   513 
       
   514 #endif // RD_JAVA_NGA_ENABLED
       
   515 
       
   516 void CMIDEnv::MappingDataForKey(TKeyEvent& aEvent, TEventCode aType)
       
   517 {
       
   518     iToolkit->Utils()->MappingDataForKey(aEvent, aType);
       
   519 }
       
   520 
       
   521 void CMIDEnv::SetLastKeyEvent(const TKeyEvent& aEvent)
       
   522 {
       
   523     iToolkit->Utils()->SetLastKeyEvent(aEvent);
       
   524 }
       
   525 
       
   526 
       
   527 // ---------------------------------------------------------------------------
       
   528 // CMIDEnv::ToLcduiObserver
       
   529 // Gets an instance of ToLcduiObserver.
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 MMIDToLcduiObserver& CMIDEnv::ToLcduiObserver()
       
   533 {
       
   534     return *iToLcduiObserver;
       
   535 }
       
   536 
       
   537 void CMIDEnv::DisplayableIsDestructed(const MMIDDisplayable* aDisplayable)
       
   538 {
       
   539     // Inform CMIDToolkit about deleting of displayble. This is needed
       
   540     // for prevereting of panic during changing to new Displayable
       
   541     iToolkit->DisplayableIsDestructed(aDisplayable);
       
   542 }