sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "registeredsurfacemap.h" sl@0: #include "panics.h" sl@0: #include sl@0: #include sl@0: sl@0: /** sl@0: Destructor for the surface map, will unregister every surface in every client. sl@0: */ sl@0: CRegisteredSurfaceMap::~CRegisteredSurfaceMap() sl@0: { sl@0: TInt tempCount = iSessionSurfaces.Count(); sl@0: for (TInt ii = 0; ii < tempCount; ii++) sl@0: { sl@0: iSessionSurfaces[ii].RemoveAll(iScene); sl@0: } sl@0: iSessionSurfaces.Close(); sl@0: } sl@0: sl@0: /** sl@0: Constructor for the surface map. sl@0: */ sl@0: CRegisteredSurfaceMap::CRegisteredSurfaceMap(MWsScene& aScene):iScene(aScene) sl@0: {} sl@0: sl@0: /** sl@0: Basic compare function, looking at the address of each session. sl@0: @param aLeft Left session for comparison. sl@0: @param aRight Right session for comparison. sl@0: @return Negative if left is smaller than right, positive if left is greater than right, sl@0: zero if they are the same. sl@0: */ sl@0: TInt CRegisteredSurfaceMap::CompareDeviceSurfaces (const TSessionSurfaces& aLeft, const TSessionSurfaces& aRight) sl@0: { sl@0: return (TInt)&aLeft.Session()-(TInt)&aRight.Session(); sl@0: } sl@0: sl@0: /** sl@0: Function to register a surface on a client. Will go through a process to add the surface to a list sl@0: for the client to keep track of which surfaces are registered for which clients. sl@0: @param aClient The client for which to register the surface. sl@0: @param aSurfaceId The surface to register on the client. sl@0: @return KErrNone on success or a system-wide error code. sl@0: @see CWsClient::RegisterSurface sl@0: */ sl@0: TInt CRegisteredSurfaceMap::Add(const CWsClient& aClient, const TSurfaceId& aSurfaceId) sl@0: { sl@0: TInt err; sl@0: TInt tempPos; sl@0: TSessionSurfaces tempDeviceSurface(aClient); sl@0: sl@0: //find client sl@0: err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder(CompareDeviceSurfaces)); sl@0: sl@0: //if the client is found sl@0: if (err == KErrNone) sl@0: { sl@0: err = iSessionSurfaces[tempPos].AddSurfaceId(aSurfaceId, iScene); sl@0: } sl@0: else sl@0: { sl@0: if (err == KErrNotFound) sl@0: { sl@0: err = iSessionSurfaces.Insert(tempDeviceSurface, tempPos); sl@0: if (err == KErrNone) sl@0: { sl@0: err = iSessionSurfaces[tempPos].AddSurfaceId(aSurfaceId, iScene); sl@0: if ((err != KErrNone) && (err != KErrInUse)) //it didnt manage to register the surface id sl@0: { sl@0: iSessionSurfaces.Remove(tempPos); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Function to unregister a specific surface on a specific client. Goes through a process to check sl@0: that the surface is registered for the client before remove it from the list and unregistering it. sl@0: @param aClient The client for which to unregister the surface. sl@0: @param aSurfaceId The surface to unregister on the client. sl@0: @return KErrNone on success or a system-wide error code. sl@0: @see CWsClient::UnregisterSurface sl@0: */ sl@0: TInt CRegisteredSurfaceMap::Remove(CWsClient& aClient, const TSurfaceId& aSurfaceId) sl@0: { sl@0: TInt err; sl@0: TInt tempPos; sl@0: TSessionSurfaces tempDeviceSurface(aClient); sl@0: sl@0: //find client sl@0: err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder(CompareDeviceSurfaces)); sl@0: TInt surfaceAmountLeftOrError; sl@0: //if found sl@0: if (err == KErrNone) sl@0: { sl@0: surfaceAmountLeftOrError = iSessionSurfaces[tempPos].RemoveSurfaceId(aSurfaceId, iScene); sl@0: if (surfaceAmountLeftOrError == 0) //there are no more surfaces registered for the client sl@0: { sl@0: iSessionSurfaces.Remove(tempPos); sl@0: } sl@0: if (surfaceAmountLeftOrError < 0) //if an error was returned sl@0: { sl@0: err = surfaceAmountLeftOrError; sl@0: } sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Function that will unregister all surfaces for a specific client. sl@0: @param aClient The client for which to unregister all clients. sl@0: @return KErrNone on success or a system-wide error code. sl@0: @see CWsTop::ClearSurfaceMap sl@0: @see CWsClient::~CWsClient() sl@0: */ sl@0: TInt CRegisteredSurfaceMap::RemoveAll(CWsClient& aClient) sl@0: { sl@0: TInt err; sl@0: TInt tempPos; sl@0: TSessionSurfaces tempDeviceSurface(aClient); sl@0: sl@0: //find client sl@0: err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder(CompareDeviceSurfaces)); sl@0: sl@0: //if found sl@0: if (err == KErrNone) sl@0: { sl@0: err = iSessionSurfaces[tempPos].RemoveAll(iScene); sl@0: } sl@0: if (err == KErrNone || err == KErrInUse) sl@0: { sl@0: iSessionSurfaces.Remove(tempPos); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: TSessionSurfaces::TSessionSurfaces(const CWsClient& aSession):iSession(aSession) sl@0: {} sl@0: sl@0: TInt TSessionSurfaces::CompareIds(const TSurfaceId& aLeft, const TSurfaceId& aRight) sl@0: { sl@0: //Compare each internal id of the surface sl@0: TUint32 ll; sl@0: TUint32 rr; sl@0: TInt32 tempResult; sl@0: ll = aLeft.iInternal[0]; sl@0: rr = aRight.iInternal[0]; sl@0: tempResult = ll-rr; sl@0: if (tempResult == 0) sl@0: { sl@0: ll = aLeft.iInternal[1]; sl@0: rr = aRight.iInternal[1]; sl@0: tempResult = ll-rr; sl@0: if (tempResult == 0) sl@0: { sl@0: ll = aLeft.iInternal[2]; sl@0: rr = aRight.iInternal[2]; sl@0: tempResult = ll-rr; sl@0: if (tempResult == 0) sl@0: { sl@0: ll = aLeft.iInternal[3]; sl@0: rr = aRight.iInternal[3]; sl@0: tempResult = ll-rr; sl@0: } sl@0: } sl@0: } sl@0: if (tempResult != 0) //if they are different ids sl@0: { sl@0: if (ll < rr) sl@0: { sl@0: return -1; sl@0: } sl@0: else sl@0: { sl@0: return 1; sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: TInt TSessionSurfaces::AddSurfaceId(const TSurfaceId& aSurfaceId, MWsScene& aScene) sl@0: { sl@0: TInt err; sl@0: TInt tempPos; sl@0: //find surface id sl@0: err = iSurfaces.FindInOrder(aSurfaceId, tempPos, TLinearOrder(CompareIds)); sl@0: sl@0: if (err == KErrNotFound) sl@0: { sl@0: err = iSurfaces.Insert(aSurfaceId, tempPos); sl@0: if (err == KErrNone) //successfully added surface, can now register sl@0: { sl@0: err = aScene.RegisterSurface(aSurfaceId); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (err == KErrNone) sl@0: { sl@0: //shouldnt be registered more than once sl@0: err = KErrInUse; sl@0: } sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: TInt TSessionSurfaces::RemoveSurfaceId(const TSurfaceId& aSurfaceId, MWsScene& aScene) sl@0: { sl@0: TInt surfaceAmountLeftOrError; sl@0: TInt tempPos; sl@0: sl@0: //find surface id sl@0: surfaceAmountLeftOrError = iSurfaces.FindInOrder(aSurfaceId, tempPos, sl@0: TLinearOrder(CompareIds)); sl@0: if (surfaceAmountLeftOrError!=KErrNone) sl@0: surfaceAmountLeftOrError = iSurfaces.FindInOrder(aSurfaceId, tempPos, sl@0: TLinearOrder(CompareIds)); sl@0: //if found sl@0: if (surfaceAmountLeftOrError == KErrNone) sl@0: { sl@0: sl@0: surfaceAmountLeftOrError = aScene.UnregisterSurface(aSurfaceId); sl@0: if ((surfaceAmountLeftOrError == KErrNone) || (surfaceAmountLeftOrError == KErrInUse)) sl@0: { sl@0: iSurfaces.Remove(tempPos); sl@0: surfaceAmountLeftOrError = iSurfaces.Count(); sl@0: } sl@0: else sl@0: { sl@0: WS_ASSERT_DEBUG(EFalse,EWsPanicSurfaceMapError); //Unexpected error sl@0: } sl@0: } sl@0: return surfaceAmountLeftOrError; sl@0: } sl@0: sl@0: TInt TSessionSurfaces::RemoveAll(MWsScene& aScene) sl@0: { sl@0: TInt err = KErrNone; sl@0: TInt returnValue = KErrNone; sl@0: TInt tempSize = iSurfaces.Count(); sl@0: for (TInt ii = 0; ii < tempSize; ii++) sl@0: { sl@0: err = aScene.UnregisterSurface(iSurfaces[ii]); sl@0: WS_ASSERT_ALWAYS((err == KErrNone) || (err == KErrInUse), EWsPanicSurfaceMapError); sl@0: if (err != KErrNone && returnValue == KErrNone) sl@0: { //return first error code sl@0: returnValue = err; sl@0: } sl@0: } sl@0: iSurfaces.Close(); sl@0: return returnValue; sl@0: }