|
1 /* |
|
2 * Copyright (c) 2008 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: Implementation of haptics client's IVT-data cache. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "hwrmhapticsivtdatacache.h" |
|
20 |
|
21 // --------------------------------------------------------------------------- |
|
22 // Two-phase constructor. |
|
23 // --------------------------------------------------------------------------- |
|
24 // |
|
25 CHWRMHapticsIVTDataCache* CHWRMHapticsIVTDataCache::NewL() |
|
26 { |
|
27 CHWRMHapticsIVTDataCache* self = CHWRMHapticsIVTDataCache::NewLC(); |
|
28 CleanupStack::Pop( self ); |
|
29 |
|
30 return self; |
|
31 } |
|
32 |
|
33 // --------------------------------------------------------------------------- |
|
34 // Two-phase asynchronous constructor. |
|
35 // --------------------------------------------------------------------------- |
|
36 // |
|
37 CHWRMHapticsIVTDataCache* CHWRMHapticsIVTDataCache::NewLC() |
|
38 { |
|
39 CHWRMHapticsIVTDataCache* self = new ( ELeave ) CHWRMHapticsIVTDataCache(); |
|
40 CleanupStack::PushL( self ); |
|
41 |
|
42 self->ConstructL(); |
|
43 |
|
44 return self; |
|
45 } |
|
46 |
|
47 // --------------------------------------------------------------------------- |
|
48 // Destructor. |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 CHWRMHapticsIVTDataCache::~CHWRMHapticsIVTDataCache() |
|
52 { |
|
53 Reset(); |
|
54 if ( iIdle ) |
|
55 { |
|
56 iIdle->Cancel(); |
|
57 delete iIdle; |
|
58 } |
|
59 iCache.Close(); |
|
60 } |
|
61 |
|
62 // --------------------------------------------------------------------------- |
|
63 // Adds a new IVT-data item to the cache. |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 TInt CHWRMHapticsIVTDataCache::AddCacheItem( const TDesC8& aIVTData, |
|
67 TInt& aFileHandle ) |
|
68 { |
|
69 TInt err = KErrNoMemory; |
|
70 |
|
71 TCacheItem newItem; |
|
72 newItem.iIVTData = aIVTData.Alloc(); |
|
73 newItem.iClientFileHandle = ++iInternalHandle; |
|
74 newItem.iDeletionRequested = EFalse; |
|
75 newItem.iLoadObserver = NULL; |
|
76 |
|
77 aFileHandle = iInternalHandle; |
|
78 |
|
79 if ( newItem.iIVTData ) |
|
80 { |
|
81 err = iCache.Append( newItem ); |
|
82 } |
|
83 |
|
84 return err; |
|
85 } |
|
86 |
|
87 // --------------------------------------------------------------------------- |
|
88 // Adds a new IVT-data item to the cache. This overload is meant for |
|
89 // asynch calls. |
|
90 // --------------------------------------------------------------------------- |
|
91 // |
|
92 TBool CHWRMHapticsIVTDataCache::UpdateCacheItemListener( |
|
93 TInt aFileHandle, |
|
94 TRequestStatus& aClientStatus, |
|
95 RHWRMHapticsSession* aClientSession, |
|
96 const TIpcArgs& aArgs ) |
|
97 { |
|
98 TBool retVal( EFalse ); |
|
99 TInt itemIndex = FindItem( aFileHandle ); |
|
100 |
|
101 if ( itemIndex >= 0 ) |
|
102 { |
|
103 // New load observer listener is only instantiated if there isn't |
|
104 // already a listener for the cache item. Note that if there already |
|
105 // were one, this method will return EFalse, and the actual asynch |
|
106 // call will be made with the original client's status in Impl class. |
|
107 if ( !iCache[itemIndex].iLoadObserver ) |
|
108 { |
|
109 // If the below leaves this method will return EFalse, |
|
110 // and the actual asynch call will be made with the original |
|
111 // client's status. |
|
112 TRAPD(err, |
|
113 iCache[itemIndex].iLoadObserver = |
|
114 CHWRMHapticsIVTDataCacheAO::NewL( aFileHandle, |
|
115 this, |
|
116 aClientStatus ) ); |
|
117 if ( !err ) |
|
118 { |
|
119 iCache[itemIndex].iLoadObserver->PlayEffectAsynch |
|
120 ( aClientSession, aArgs ); |
|
121 retVal = ETrue; |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 return retVal; |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------------------------- |
|
130 // Removes a cache item identified by the given filehandle. |
|
131 // --------------------------------------------------------------------------- |
|
132 // |
|
133 TInt CHWRMHapticsIVTDataCache::RemoveCacheItem( TInt aFileHandle ) |
|
134 { |
|
135 TInt err = KErrNotFound; |
|
136 |
|
137 // search for the item in the cache |
|
138 TInt itemIndex = FindItem( aFileHandle ); |
|
139 |
|
140 if ( itemIndex >= 0 ) |
|
141 { |
|
142 // item was found, either mark it for deletion (if there's active |
|
143 // load observer active object waiting for response for previous |
|
144 // play with data request) or otherwise delete and remove it. |
|
145 if ( iCache[itemIndex].iLoadObserver && |
|
146 iCache[itemIndex].iLoadObserver->IsActive() ) |
|
147 { |
|
148 iCache[itemIndex].iDeletionRequested = ETrue; |
|
149 } |
|
150 else |
|
151 { |
|
152 delete iCache[itemIndex].iIVTData; |
|
153 iCache[itemIndex].iIVTData = NULL; |
|
154 delete iCache[itemIndex].iLoadObserver; |
|
155 iCache[itemIndex].iLoadObserver = NULL; |
|
156 iCache.Remove( itemIndex ); |
|
157 |
|
158 err = KErrNone; |
|
159 |
|
160 // update loaded item index, if needed |
|
161 if ( iLoadedItem == itemIndex ) |
|
162 { |
|
163 iLoadedItem = KErrNotFound; |
|
164 } |
|
165 else if ( iLoadedItem > itemIndex ) |
|
166 { |
|
167 --iLoadedItem; |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 return err; |
|
173 } |
|
174 |
|
175 // --------------------------------------------------------------------------- |
|
176 // Removes all items from the cache. |
|
177 // --------------------------------------------------------------------------- |
|
178 // |
|
179 void CHWRMHapticsIVTDataCache::Reset() |
|
180 { |
|
181 // delete and remove all items |
|
182 while ( iCache.Count() ) |
|
183 { |
|
184 delete iCache[0].iIVTData; |
|
185 iCache[0].iIVTData = NULL; |
|
186 delete iCache[0].iLoadObserver; |
|
187 iCache[0].iLoadObserver = NULL; |
|
188 iCache.Remove( 0 ); |
|
189 } |
|
190 |
|
191 iLoadedItem = KErrNotFound; |
|
192 } |
|
193 |
|
194 // --------------------------------------------------------------------------- |
|
195 // Returns a pointer to the IVT-data buffer, which corresponds to |
|
196 // the given filehandle. |
|
197 // --------------------------------------------------------------------------- |
|
198 // |
|
199 const HBufC8* CHWRMHapticsIVTDataCache::IVTData( TInt aFileHandle ) const |
|
200 { |
|
201 HBufC8* ivtData = NULL; |
|
202 |
|
203 // search for the item in the cache |
|
204 TInt itemIndex = FindItem( aFileHandle ); |
|
205 if ( itemIndex >= 0 ) |
|
206 { |
|
207 ivtData = iCache[itemIndex].iIVTData; |
|
208 } |
|
209 |
|
210 return ivtData; |
|
211 } |
|
212 |
|
213 // --------------------------------------------------------------------------- |
|
214 // Returns whether or not the IVT-data of the given filehandle |
|
215 // is currently loaded into the haptics system. |
|
216 // --------------------------------------------------------------------------- |
|
217 // |
|
218 TBool CHWRMHapticsIVTDataCache::IsLoaded( TInt aFileHandle ) const |
|
219 { |
|
220 TInt ret = EFalse; |
|
221 TInt index = FindItem( aFileHandle ); |
|
222 |
|
223 // if index was found and the index is the loaded item's index, |
|
224 // this data is already loaded |
|
225 if ( index >= 0 && index == iLoadedItem ) |
|
226 { |
|
227 ret = ETrue; |
|
228 } |
|
229 |
|
230 return ret; |
|
231 } |
|
232 |
|
233 // --------------------------------------------------------------------------- |
|
234 // Updates server side file handle |
|
235 // --------------------------------------------------------------------------- |
|
236 // |
|
237 void CHWRMHapticsIVTDataCache::UpdateCacheItem( TInt aClientFileHandle, |
|
238 TBool aSetLoaded ) |
|
239 { |
|
240 // find the item from cache with the given filehandle |
|
241 TInt itemIndex = FindItem( aClientFileHandle ); |
|
242 if ( itemIndex >= 0 ) |
|
243 { |
|
244 if ( aSetLoaded && !iCache[itemIndex].iDeletionRequested ) |
|
245 { |
|
246 iLoadedItem = itemIndex; |
|
247 } |
|
248 } |
|
249 } |
|
250 |
|
251 // --------------------------------------------------------------------------- |
|
252 // Returns client side file handle |
|
253 // --------------------------------------------------------------------------- |
|
254 // |
|
255 TInt CHWRMHapticsIVTDataCache::ClientFileHandle( const TDesC8& aIVTData ) |
|
256 { |
|
257 // find the item from cache with the given filehandle |
|
258 TInt clientFileHandle( KErrNotFound ); |
|
259 TInt count( iCache.Count() ); |
|
260 for ( TInt i( 0 ); i < count && KErrNotFound == clientFileHandle; ++i ) |
|
261 { |
|
262 TPtr8 cachedData = iCache[i].iIVTData->Des(); |
|
263 if ( cachedData == aIVTData ) |
|
264 { |
|
265 clientFileHandle = iCache[i].iClientFileHandle; |
|
266 } |
|
267 } |
|
268 |
|
269 return clientFileHandle; |
|
270 } |
|
271 |
|
272 // --------------------------------------------------------------------------- |
|
273 // Method for starting the idle object that then calls garbage collection |
|
274 // callback. |
|
275 // --------------------------------------------------------------------------- |
|
276 // |
|
277 void CHWRMHapticsIVTDataCache::RequestGarbageCollection() |
|
278 { |
|
279 if ( iIdle && !iIdle->IsActive() ) |
|
280 { |
|
281 iIdle->Start( TCallBack ( CollectGarbageIdle, this ) ); |
|
282 } |
|
283 } |
|
284 |
|
285 // --------------------------------------------------------------------------- |
|
286 // Garbage collection static callback. |
|
287 // --------------------------------------------------------------------------- |
|
288 // |
|
289 TInt CHWRMHapticsIVTDataCache::CollectGarbageIdle( TAny* aObjectPtr ) |
|
290 { |
|
291 CHWRMHapticsIVTDataCache* self = |
|
292 reinterpret_cast<CHWRMHapticsIVTDataCache*>( aObjectPtr ); |
|
293 if ( self ) |
|
294 { |
|
295 self->CollectGarbage(); |
|
296 } |
|
297 return KErrNone; |
|
298 } |
|
299 |
|
300 // --------------------------------------------------------------------------- |
|
301 // Garbage collection actual (object specific) callback. Cleans obsolete |
|
302 // load observers and whole TCacheItems if needed. |
|
303 // --------------------------------------------------------------------------- |
|
304 // |
|
305 void CHWRMHapticsIVTDataCache::CollectGarbage() |
|
306 { |
|
307 for ( TInt i = iCache.Count() - 1; i >= 0; --i ) |
|
308 { |
|
309 // This garbage collection is only interested in those entries for |
|
310 // which there has been load observer running, i.e., for which the |
|
311 // iLoadObserver is non-NULL, and for which the running has stopped, |
|
312 // i.e., the iLoadObserver.IsActive() check returns EFalse |
|
313 if ( iCache[i].iLoadObserver && !iCache[i].iLoadObserver->IsActive() ) |
|
314 { |
|
315 delete iCache[i].iLoadObserver; |
|
316 iCache[i].iLoadObserver = NULL; |
|
317 // If the entry was also marked for deletion, delete the whole |
|
318 // entry from TCacheItem array |
|
319 if ( iCache[i].iDeletionRequested ) |
|
320 { |
|
321 delete iCache[i].iIVTData; |
|
322 iCache[i].iIVTData = NULL; |
|
323 iCache.Remove( i ); |
|
324 |
|
325 // update loaded item index, if needed |
|
326 if ( iLoadedItem == i ) |
|
327 { |
|
328 iLoadedItem = KErrNotFound; |
|
329 } |
|
330 else if ( iLoadedItem > i ) |
|
331 { |
|
332 --iLoadedItem; |
|
333 } |
|
334 } |
|
335 } |
|
336 } |
|
337 } |
|
338 |
|
339 // --------------------------------------------------------------------------- |
|
340 // C++ constructor. |
|
341 // --------------------------------------------------------------------------- |
|
342 // |
|
343 CHWRMHapticsIVTDataCache::CHWRMHapticsIVTDataCache() |
|
344 : iLoadedItem( KErrNotFound ) |
|
345 { |
|
346 } |
|
347 |
|
348 // --------------------------------------------------------------------------- |
|
349 // Second phase construction. |
|
350 // --------------------------------------------------------------------------- |
|
351 // |
|
352 void CHWRMHapticsIVTDataCache::ConstructL() |
|
353 { |
|
354 iIdle = CIdle::NewL( CActive::EPriorityIdle ); |
|
355 } |
|
356 |
|
357 // --------------------------------------------------------------------------- |
|
358 // Searches the cache for an item with the given filehandle. |
|
359 // --------------------------------------------------------------------------- |
|
360 // |
|
361 TInt CHWRMHapticsIVTDataCache::FindItem( TInt aClientFileHandle ) const |
|
362 { |
|
363 TInt index = KErrNotFound; |
|
364 |
|
365 // find the item from cache with the given filehandle |
|
366 for ( TInt i = 0; i < iCache.Count() && index == KErrNotFound; ++i ) |
|
367 { |
|
368 if ( iCache[i].iClientFileHandle == aClientFileHandle ) |
|
369 { |
|
370 index = i; |
|
371 } |
|
372 } |
|
373 |
|
374 return index; |
|
375 } |
|
376 |
|
377 // End of File |