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