LayerManagerControl: fixed handling error in ILM calls
[profile/ivi/layer-management.git] / LayerManagerControl / src / control.cpp
1 /***************************************************************************
2  *
3  * Copyright 2012 BMW Car IT GmbH
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ****************************************************************************/
19
20 #include "ilm_client.h"
21 #include "LMControl.h"
22
23 #include <cstring>
24
25 #include <iostream>
26 using std::cout;
27 using std::cin;
28 using std::cerr;
29 using std::endl;
30
31
32 #include <iomanip>
33 using std::dec;
34 using std::hex;
35
36
37 #include <pthread.h>
38 #include <signal.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41
42
43 bool gBenchmark_running;
44
45 void benchmarkSigHandler(int sig)
46 {
47     (void) sig;
48     gBenchmark_running = false;
49 }
50
51 void getCommunicatorPerformance()
52 {
53     int runs = 0;
54     int runtimeInSec = 5;
55     unsigned int hwLayerCnt = 0;
56     cout << "running performance test for " << runtimeInSec << " seconds... ";
57     flush(cout);
58
59     signal(SIGALRM, benchmarkSigHandler);
60
61     gBenchmark_running = true;
62
63     alarm(runtimeInSec);
64
65     while (gBenchmark_running)
66     {
67         t_ilm_uint screenid = 0;
68
69         ilmErrorTypes callResult = ilm_getNumberOfHardwareLayers(screenid, &hwLayerCnt);
70         if (ILM_SUCCESS != callResult)
71         {
72             cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
73             cout << "Failed to get number of hardware layers for screen with ID " << screenid << "\n";
74             return;
75         }
76
77         ++runs;
78     }
79
80     signal(SIGALRM, SIG_DFL);
81
82     cout << (runs/runtimeInSec) << " transactions/second\n";
83 }
84
85 void setSurfaceKeyboardFocus(t_ilm_surface surface)
86 {
87     ilmErrorTypes callResult = ilm_SetKeyboardFocusOn(surface);
88     if (ILM_SUCCESS != callResult)
89     {
90         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
91         cout << "Failed to set keyboard focus at surface with ID " << surface << "\n";
92         return;
93     }
94 }
95
96 void getKeyboardFocus()
97 {
98     t_ilm_surface surfaceId;
99
100     ilmErrorTypes callResult = ilm_GetKeyboardFocusSurfaceId(&surfaceId);
101     if (ILM_SUCCESS == callResult)
102     {
103         cout << "keyboardFocusSurfaceId == " << surfaceId << endl;
104     }
105     else
106     {
107         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
108         cout << "Failed to get keyboard focus surface ID\n";
109         return;
110     }
111 }
112
113 void setSurfaceAcceptsInput(t_ilm_surface surfaceId, string kbdPointerTouch, t_ilm_bool acceptance)
114 {
115     char* str;
116     char* tok;
117
118     ilmInputDevice devices = (ilmInputDevice)0;
119
120     str = new char [kbdPointerTouch.size()+1];
121     strcpy (str, kbdPointerTouch.c_str());
122     tok = strtok(str, ":");
123     while (tok != NULL)
124     {
125         if (!strcmp(tok, "kbd"))
126         {
127             devices |= ILM_INPUT_DEVICE_KEYBOARD;
128         }
129         else if (!strcmp(tok, "pointer"))
130         {
131             devices |= ILM_INPUT_DEVICE_POINTER;
132         }
133         else if (!strcmp(tok, "touch"))
134         {
135             devices |= ILM_INPUT_DEVICE_TOUCH;
136         }
137         else
138         {
139             cerr << "Unknown devices specified." << endl;
140         }
141
142         tok = strtok(NULL, ":");
143     }
144
145     ilmErrorTypes callResult = ilm_UpdateInputEventAcceptanceOn(surfaceId, devices, acceptance);
146     if (ILM_SUCCESS != callResult)
147     {
148         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
149         cout << "Failed to update input event acceptance on surface with ID " << surfaceId << "\n";
150         return;
151     }
152
153     ilm_commitChanges();
154
155     delete[] str;
156 }
157
158 void layerNotificationCallback(t_ilm_layer layer, struct ilmLayerProperties* properties, t_ilm_notification_mask mask)
159 {
160     cout << "\nNotification: layer " << layer << " updated properties:\n";
161
162     if (ILM_NOTIFICATION_VISIBILITY & mask)
163     {
164         cout << "\tvisibility = " << properties->visibility << "\n";
165     }
166
167     if (ILM_NOTIFICATION_OPACITY & mask)
168     {
169         cout << "\topacity = " << properties->opacity << "\n";
170     }
171
172     if (ILM_NOTIFICATION_ORIENTATION & mask)
173     {
174         cout << "\torientation = " << properties->orientation << "\n";
175     }
176
177     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
178     {
179         cout << "\tsource rect = x:" << properties->sourceX
180              << ", y:" << properties->sourceY
181              << ", width:" << properties->sourceWidth
182              << ", height:" << properties->sourceHeight
183              << "\n";
184     }
185
186     if (ILM_NOTIFICATION_DEST_RECT & mask)
187     {
188         cout << "\tdest rect = x:" << properties->destX
189              << ", y:" << properties->destY
190              << ", width:" << properties->destWidth
191              << ", height:" << properties->destHeight
192              << "\n";
193     }
194 }
195
196 void testNotificationLayer(t_ilm_layer layerid)
197 {
198     cout << "Setup notification for layer " << layerid << " \n";
199
200     ilmErrorTypes callResult = ilm_layerAddNotification(layerid, layerNotificationCallback);
201     if (ILM_SUCCESS != callResult)
202     {
203         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
204         cout << "Failed to add notification callback to layer with ID " << layerid << "\n";
205         return;
206     }
207
208     for  (int i = 0; i < 2; ++i)
209     {
210         usleep(100 * 1000);
211         cout << "Set layer 1000 visbility to FALSE\n";
212         ilm_layerSetVisibility(layerid, ILM_FALSE);
213         ilm_commitChanges();
214
215         usleep(100 * 1000);
216         cout << "Set layer 1000 visbility to TRUE\n";
217         ilm_layerSetVisibility(layerid, ILM_TRUE);
218
219         cout << "Set layer 1000 opacity to 0.3\n";
220         ilm_layerSetOpacity(layerid, 0.3);
221         ilm_commitChanges();
222
223         usleep(100 * 1000);
224         cout << "Set layer 1000 opacity to 1.0\n";
225         ilm_layerSetOpacity(layerid, 1.0);
226         ilm_commitChanges();
227     }
228
229     ilm_commitChanges(); // make sure, app lives long enough to receive last notification
230 }
231
232 void watchLayer(unsigned int* layerids, unsigned int layeridCount)
233 {
234     for (unsigned int i = 0; i < layeridCount; ++i)
235     {
236         unsigned int layerid = layerids[i];
237         cout << "Setup notification for layer " << layerid << "\n";
238
239         ilmErrorTypes callResult = ilm_layerAddNotification(layerid, layerNotificationCallback);
240         if (ILM_SUCCESS != callResult)
241         {
242             cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
243             cout << "Failed to add notification callback to layer with ID " << layerid << "\n";
244             return;
245         }
246     }
247
248     cout << "Waiting for notifications...\n";
249     int block;
250     cin >> block;
251
252     for (unsigned int i = 0; i < layeridCount; ++i)
253     {
254         unsigned int layerid = layerids[i];
255         cout << "Removing notification for layer " << layerid << "\n";
256
257         ilmErrorTypes callResult = ilm_layerRemoveNotification(layerid);
258         if (ILM_SUCCESS != callResult)
259         {
260             cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
261             cout << "Failed to remove notification callback of layer with ID " << layerid << "\n";
262             return;
263         }
264     }
265
266     if (layerids)
267     {
268         delete[] layerids;
269     }
270 }
271
272 void surfaceNotificationCallback(t_ilm_layer surface, struct ilmSurfaceProperties* properties, t_ilm_notification_mask mask)
273 {
274     cout << "\nNotification: surface " << surface << " updated properties:\n";
275
276     if (ILM_NOTIFICATION_VISIBILITY & mask)
277     {
278         cout << "\tvisibility = " << properties->visibility << "\n";
279     }
280
281     if (ILM_NOTIFICATION_OPACITY & mask)
282     {
283         cout << "\topacity = " << properties->opacity << "\n";
284     }
285
286     if (ILM_NOTIFICATION_ORIENTATION & mask)
287     {
288         cout << "\torientation = " << properties->orientation << "\n";
289     }
290
291     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
292     {
293         cout << "\tsource rect = x:" << properties->sourceX
294              << ", y:" << properties->sourceY
295              << ", width:" << properties->sourceWidth
296              << ", height:" << properties->sourceHeight
297              << "\n";
298     }
299
300     if (ILM_NOTIFICATION_DEST_RECT & mask)
301     {
302         cout << "\tdest rect = x:" << properties->destX
303              << ", y:" << properties->destY
304              << ", width:" << properties->destWidth
305              << ", height:" << properties->destHeight
306              << "\n";
307     }
308 }
309
310 void watchSurface(unsigned int* surfaceids, unsigned int surfaceidCount)
311 {
312     for (unsigned int i = 0; i < surfaceidCount; ++i)
313     {
314         unsigned int surfaceid = surfaceids[i];
315         cout << "Setup notification for surface " << surfaceid << "\n";
316
317         ilmErrorTypes callResult = ilm_surfaceAddNotification(surfaceid, surfaceNotificationCallback);
318         if (ILM_SUCCESS != callResult)
319         {
320             cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
321             cout << "Failed to add notification callback to surface with ID " << surfaceid << "\n";
322             return;
323         }
324     }
325
326     cout << "Waiting for notifications...\n";
327     int block;
328     cin >> block;
329
330     for (unsigned int i = 0; i < surfaceidCount; ++i)
331     {
332         unsigned int surfaceid = surfaceids[i];
333         cout << "Removing notification for surface " << surfaceid << "\n";
334
335         ilmErrorTypes callResult = ilm_surfaceRemoveNotification(surfaceid);
336         if (ILM_SUCCESS != callResult)
337         {
338             cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
339             cout << "Failed to remove notification callback of surface with ID " << surfaceid << "\n";
340             return;
341         }
342     }
343
344     if (surfaceids)
345     {
346         delete[] surfaceids;
347     }
348 }
349
350 void setOptimization(t_ilm_uint id, t_ilm_uint mode)
351 {
352     ilmErrorTypes callResult = ilm_SetOptimizationMode((ilmOptimization)id,
353                                     (ilmOptimizationMode)mode);
354     if (ILM_SUCCESS != callResult)
355     {
356         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
357         cout << "Failed to set optimization with ID "<< id <<" mode " << mode << "\n";
358         return;
359     }
360
361     ilm_commitChanges();
362 }
363
364 void getOptimization(t_ilm_uint id)
365 {
366     ilmOptimization optimizationId = (ilmOptimization)id;
367     ilmOptimizationMode optimizationMode;
368
369     ilmErrorTypes callResult = ilm_GetOptimizationMode(optimizationId, &optimizationMode);
370     if (callResult == ILM_SUCCESS)
371     {
372         switch ( optimizationId )
373         {
374         case ILM_OPT_MULTITEXTURE :
375             cout << "Optimization " << (int)optimizationId << " (Multitexture Optimization)" << endl;
376             break;
377
378         case ILM_OPT_SKIP_CLEAR :
379             cout << "Optimization " << (int)optimizationId << " (Skip Clear)" << endl;
380             break;
381         default:
382             cout << "Optimization " << "unknown" << endl;
383             break;
384         }
385
386         switch (optimizationMode)
387         {
388         case ILM_OPT_MODE_FORCE_OFF :
389             cout << "Mode " << (int)optimizationMode << " (forced off)" << endl;
390             break;
391
392         case ILM_OPT_MODE_FORCE_ON :
393             cout << "Mode " << (int)optimizationMode << " (forced on)" << endl;
394             break;
395         case ILM_OPT_MODE_HEURISTIC :
396             cout << "Mode " << (int)optimizationMode << " (Heuristic / Render choose the optimization)" << endl;
397             break;
398         case ILM_OPT_MODE_TOGGLE :
399             cout << "Mode " << (int)optimizationMode << " (Toggle on/and off rapidly for debugging)" << endl;
400             break;
401
402         default:
403             cout <<"Mode " << "unknown" << endl ;
404             break;
405         }
406     }
407     else
408     {
409         cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n";
410         cout << "Failed to get mode for optimization with ID "<< optimizationId << "\n";
411         return;
412     }
413
414     ilm_commitChanges();
415 }