84f467d814974e78f0e6be6f73ec07b49e0877b5
[staging/HomeScreen.git] / WindowManager / src / windowmanager.cpp
1 /*
2  * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "windowmanager.hpp"
18
19
20 //////////////////////////////////////////
21 // THIS IS STILL UNDER HEAVY DEVELOPMENT!
22 // DO NOT JUDGE THE SOURCE CODE :)
23 //////////////////////////////////////////
24
25 // three layers will be defined. The HomeScreen will be placed
26 // full screen in the background.
27 // On top all applications in one layer.
28 // On top of that, the popup layer.
29 #define WINDOWMANAGER_LAYER_POPUP 100
30 #define WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY 101
31 #define WINDOWMANAGER_LAYER_APPLICATIONS 102
32 #define WINDOWMANAGER_LAYER_HOMESCREEN 103
33
34 #define WINDOWMANAGER_LAYER_NUM 4
35
36 #define WINDOWMANAGER_LAYER_ID_SHIFT 22
37
38 // the HomeScreen app has to have the surface id 1000
39 #define WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID 1000
40
41 void* WindowManager::myThis = 0;
42
43 WindowManager::WindowManager(QObject *parent) :
44     QObject(parent),
45     m_layouts(),
46     //    m_appSurfaces(),
47     mp_layoutAreaToSurfaceIdAssignment(0),
48     m_currentLayout(-1),
49     m_screenId(0), // use screen "0"
50     m_screenWidth(0),
51     m_screenHeight(0),
52     m_appLayers(),
53     m_pending_to_show(-1)
54 {
55     m_showLayers = new t_ilm_layer[WINDOWMANAGER_LAYER_NUM];
56
57     m_showLayers[0] = 0; /* POPUP is not shown by default */
58     m_showLayers[1] = 0; /* HOMESCREEN_OVERLAY is not shown by default */
59     m_showLayers[2] = 0; /* APPLICATIONS is not shown by default */
60     m_showLayers[3] = WINDOWMANAGER_LAYER_HOMESCREEN; /* HOMESCREEN is shwon by default */
61
62     qDebug("-=[WindowManager]=-");
63 }
64
65 void WindowManager::start()
66 {
67     qDebug("-=[start]=-");
68     mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
69 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
70     ilmErrorTypes err;
71
72     err = ilm_init();
73     qDebug("ilm_init = %d", err);
74     if(ILM_SUCCESS != err)
75     {
76         qDebug("failed! Exiting!");
77         exit(-1);
78     }
79
80     myThis = this;
81
82     ilm_getScreenResolution(m_screenId, &m_screenWidth, &m_screenHeight);
83
84     createNewLayer(WINDOWMANAGER_LAYER_POPUP);
85     createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
86 //  createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
87     createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
88
89     ilm_registerNotification(WindowManager::notificationFunc_static, this);
90 #endif
91
92     QDBusConnection dbus = QDBusConnection::sessionBus();
93     dbus.registerObject("/windowmanager", this);
94     dbus.registerService("org.agl.windowmanager");
95
96     // publish windowmanager interface
97     mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
98 }
99
100 WindowManager::~WindowManager()
101 {
102     qDebug("-=[~WindowManager]=-");
103     delete mp_windowManagerAdaptor;
104 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
105     ilm_destroy();
106 #endif
107     delete mp_layoutAreaToSurfaceIdAssignment;
108 }
109
110 int WindowManager::getLayerRenderOrder(t_ilm_layer id_array[])
111 {
112     int i, j;
113
114     for (i = WINDOWMANAGER_LAYER_NUM - 1, j = 0; i >= 0; i--, j++) {
115         if (m_showLayers[i] != 0) {
116             id_array[j] = m_showLayers[i];
117         }
118     }
119
120     return j;
121 }
122
123 void WindowManager::dumpScene()
124 {
125     qDebug("\n");
126     qDebug("current layout   : %d", m_currentLayout);
127     qDebug("available layouts: %d", m_layouts.size());
128     QList<Layout>::const_iterator i = m_layouts.begin();
129
130     while (i != m_layouts.constEnd())
131     {
132         qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
133         qDebug("  %d surface areas", i->layoutAreas.size());
134         for (int j = 0; j < i->layoutAreas.size(); ++j)
135         {
136             qDebug("  -area %d", j);
137             qDebug("    -x     : %d", i->layoutAreas.at(j).x);
138             qDebug("    -y     : %d", i->layoutAreas.at(j).y);
139             qDebug("    -width : %d", i->layoutAreas.at(j).width);
140             qDebug("    -height: %d", i->layoutAreas.at(j).height);
141         }
142
143         ++i;
144     }
145 }
146
147 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
148
149 void WindowManager::createNewLayer(int layerId)
150 {
151     qDebug("-=[createNewLayer]=-");
152     qDebug("  layerId %d", layerId);
153
154     t_ilm_layer newLayerId = layerId;
155     ilm_layerCreateWithDimension(&newLayerId, m_screenWidth, m_screenHeight);
156     ilm_layerSetOpacity(newLayerId, 1.0);
157     ilm_layerSetVisibility(newLayerId, ILM_TRUE);
158     ilm_layerSetSourceRectangle(newLayerId,
159                                     0,
160                                     0,
161                                     m_screenWidth,
162                                     m_screenHeight);
163     ilm_layerSetDestinationRectangle(newLayerId,
164                                     0,
165                                     0,
166                                     m_screenWidth,
167                                     m_screenHeight);
168
169     ilm_commitChanges();
170 }
171
172 t_ilm_layer WindowManager::getAppLayerID(pid_t pid)
173 {
174     t_ilm_layer layer_id;
175
176 //    layer_id = pid + (WINDOWMANAGER_LAYER_APPLICATIONS << WINDOWMANAGER_LAYER_ID_SHIFT);
177     layer_id = pid + (WINDOWMANAGER_LAYER_APPLICATIONS * 100000); /* for debug */
178
179     return layer_id;
180 }
181
182 void WindowManager::addSurfaceToAppLayer(int surfaceId)
183 {
184     struct ilmSurfaceProperties surfaceProperties;
185     t_ilm_layer layer_id;
186     int found = 0;
187     pid_t pid;
188
189     qDebug("-=[addSurfaceToAppLayer]=-");
190     qDebug("  surfaceId %d", surfaceId);
191
192     ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
193     pid = surfaceProperties.creatorPid;
194
195     if (pid < 0) {
196         /* No process */
197         qDebug("addSurfaceToAppLayer(%d) got pid == -1", surfaceId);
198         return;
199     }
200
201     QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(pid);
202     if (i == m_appLayers.end()) {
203         qDebug("No layer found, create new for app(pid=%d)", pid);
204
205         /* not found, create new one */
206         t_ilm_layer layer_id = getAppLayerID(pid);
207
208         createNewLayer(layer_id);
209         m_appLayers.insert(pid, layer_id);
210     }
211 }
212
213 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
214 {
215     qDebug("-=[addSurfaceToLayer]=-");
216     qDebug("  surfaceId %d", surfaceId);
217     qDebug("  layerId %d", layerId);
218
219     if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
220     {
221         struct ilmSurfaceProperties surfaceProperties;
222         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
223
224         // homescreen app always fullscreen in the back
225         ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, m_screenWidth, m_screenHeight);
226         //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, m_screenWidth, m_screenHeight);
227         ilm_surfaceSetOpacity(surfaceId, 1.0);
228         ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
229
230         ilm_layerAddSurface(layerId, surfaceId);
231     }
232 #if 0
233     if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
234     {
235         struct ilmSurfaceProperties surfaceProperties;
236         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
237
238         //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
239         //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
240         //ilm_surfaceSetOpacity(surfaceId, 0.0);
241         //ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
242
243         ilm_layerAddSurface(layerId, surfaceId);
244     }
245 #endif
246     if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
247     {
248         struct ilmSurfaceProperties surfaceProperties;
249         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
250
251         //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
252         //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
253         //ilm_surfaceSetOpacity(surfaceId, 0.5);
254         //ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
255
256         ilm_layerAddSurface(layerId, surfaceId);
257     }
258
259     if (layerId == WINDOWMANAGER_LAYER_POPUP)
260     {
261         struct ilmSurfaceProperties surfaceProperties;
262         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
263
264         //ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
265         //ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
266         //ilm_surfaceSetOpacity(surfaceId, 0.0);
267         //ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
268
269         ilm_layerAddSurface(layerId, surfaceId);
270     }
271
272     ilm_commitChanges();
273 }
274
275 #endif
276
277 void WindowManager::updateScreen()
278 {
279     qDebug("-=[updateScreen]=-");
280
281 #if 0
282 //#ifdef HAVE_IVI_LAYERMANAGEMENT_API
283     if (-1 != m_currentLayout)
284     {
285         // hide all surfaces
286         for (int i = 0; i < m_appSurfaces.size(); ++i)
287         {
288             ilm_layerRemoveSurface(WINDOWMANAGER_LAYER_APPLICATIONS, m_appSurfaces.at(i));
289             //ilm_surfaceSetVisibility(m_appSurfaces.at(i), ILM_FALSE);
290             //ilm_surfaceSetOpacity(m_appSurfaces.at(i), 0.0);
291             ilm_commitChanges();
292         }
293
294         // find the current used layout
295         QList<Layout>::const_iterator ci = m_layouts.begin();
296
297         Layout currentLayout;
298         while (ci != m_layouts.constEnd())
299         {
300             if (ci->id == m_currentLayout)
301             {
302                 currentLayout = *ci;
303             }
304
305             ++ci;
306         }
307
308         qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
309         for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
310         {
311             int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
312             qDebug("  surface no. %d: %d", j, surfaceToShow);
313
314             addSurfaceToLayer(surfaceToShow, WINDOWMANAGER_LAYER_APPLICATIONS);
315
316             ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
317             ilm_surfaceSetOpacity(surfaceToShow, 1.0);
318
319             qDebug("  layout area %d", j);
320             qDebug("    x: %d", currentLayout.layoutAreas[j].x);
321             qDebug("    y: %d", currentLayout.layoutAreas[j].y);
322             qDebug("    w: %d", currentLayout.layoutAreas[j].width);
323             qDebug("    h: %d", currentLayout.layoutAreas[j].height);
324
325             ilm_surfaceSetDestinationRectangle(surfaceToShow,
326                                              currentLayout.layoutAreas[j].x,
327                                              currentLayout.layoutAreas[j].y,
328                                              currentLayout.layoutAreas[j].width,
329                                              currentLayout.layoutAreas[j].height);
330             ilm_commitChanges();
331         }
332     }
333
334     // layer surface render order
335     t_ilm_int length;
336     t_ilm_surface* pArray;
337     ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN, &length, &pArray);
338     ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN, pArray, length);
339     ilm_commitChanges();
340     ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_APPLICATIONS, &length, &pArray);
341     ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_APPLICATIONS, pArray, length);
342     ilm_commitChanges();
343     ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, &length, &pArray);
344     ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, pArray, length);
345     ilm_commitChanges();
346     ilm_getSurfaceIDsOnLayer(WINDOWMANAGER_LAYER_POPUP, &length, &pArray);
347     ilm_layerSetRenderOrder(WINDOWMANAGER_LAYER_POPUP, pArray, length);
348     ilm_commitChanges();
349 #endif
350     if (m_pending_to_show != -1) {
351         showAppLayer(m_pending_to_show);
352     } else {
353         // display layer render order
354         t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
355         int num_layers = getLayerRenderOrder(renderOrder);
356         ilm_displaySetRenderOrder(m_screenId, renderOrder, num_layers);
357         ilm_commitChanges();
358     }
359 }
360
361 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
362 void WindowManager::notificationFunc_non_static(ilmObjectType object,
363                                     t_ilm_uint id,
364                                     t_ilm_bool created)
365 {
366     qDebug("-=[notificationFunc_non_static]=-");
367     qDebug("Notification from weston!");
368     if (ILM_SURFACE == object)
369     {
370         struct ilmSurfaceProperties surfaceProperties;
371
372         if (created)
373         {
374             qDebug("Surface created, ID: %d", id);
375             ilm_getPropertiesOfSurface(id, &surfaceProperties);
376             qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
377             qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
378
379             if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
380             {
381                 qDebug("HomeScreen app detected");
382                 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
383                 updateScreen();
384             }
385             else
386             {
387                 addSurfaceToAppLayer(id);
388                 //addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
389                 //m_appSurfaces.append(id);
390             }
391             ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
392
393             ilm_commitChanges();
394         }
395         else
396         {
397             qDebug("Surface destroyed, ID: %d", id);
398 #if 0
399             m_appSurfaces.removeAt(m_appSurfaces.indexOf(id));
400             ilm_surfaceRemoveNotification(id);
401
402             ilm_commitChanges();
403 #endif
404         }
405     }
406     if (ILM_LAYER == object)
407     {
408         //qDebug("Layer.. we don't care...");
409     }
410 }
411
412 void WindowManager::notificationFunc_static(ilmObjectType object,
413                                             t_ilm_uint id,
414                                             t_ilm_bool created,
415                                             void* user_data)
416 {
417     static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
418 }
419
420 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
421                                     struct ilmSurfaceProperties* surfaceProperties,
422                                     t_ilm_notification_mask mask)
423 {
424     qDebug("-=[surfaceCallbackFunction_non_static]=-");
425     qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
426     if (ILM_NOTIFICATION_VISIBILITY & mask)
427     {
428         qDebug("ILM_NOTIFICATION_VISIBILITY");
429         surfaceVisibilityChanged(surface, surfaceProperties->visibility);
430     }
431     if (ILM_NOTIFICATION_OPACITY & mask)
432     {
433         qDebug("ILM_NOTIFICATION_OPACITY");
434     }
435     if (ILM_NOTIFICATION_ORIENTATION & mask)
436     {
437         qDebug("ILM_NOTIFICATION_ORIENTATION");
438     }
439     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
440     {
441         qDebug("ILM_NOTIFICATION_SOURCE_RECT");
442     }
443     if (ILM_NOTIFICATION_DEST_RECT & mask)
444     {
445         qDebug("ILM_NOTIFICATION_DEST_RECT");
446     }
447     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
448     {
449         qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
450         /* add surface to layer for the application */
451
452         ilmErrorTypes result;
453         pid_t pid = surfaceProperties->creatorPid;
454
455         QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(pid);
456         if (i != m_appLayers.end()) {
457             t_ilm_layer layer_id = m_appLayers.value(pid);
458
459             result = ilm_layerAddSurface(layer_id, surface);
460
461             if (result != ILM_SUCCESS) {
462                 qDebug("ilm_layerAddSurface(%d,%d) failed.", layer_id, surface);
463             }
464
465             /* Dirty hack! cut & paste from HomeScreen/src/layouthandler.cpp */
466             const int SCREEN_WIDTH = 1080;
467             const int SCREEN_HEIGHT = 1920;
468
469             const int TOPAREA_HEIGHT = 218;
470             const int TOPAREA_WIDTH = SCREEN_WIDTH;
471             const int TOPAREA_X = 0;
472             const int TOPAREA_Y = 0;
473             const int MEDIAAREA_HEIGHT = 215;
474             const int MEDIAAREA_WIDTH = SCREEN_WIDTH;
475             const int MEDIAAREA_X = 0;
476             const int MEDIAAREA_Y = SCREEN_HEIGHT - MEDIAAREA_HEIGHT;
477
478             ilm_surfaceSetDestinationRectangle(surface,
479                                                0,
480                                                TOPAREA_HEIGHT,
481                                                SCREEN_WIDTH,
482                                                SCREEN_HEIGHT - TOPAREA_HEIGHT - MEDIAAREA_HEIGHT);
483
484             ilm_commitChanges();
485         } else {
486             qDebug("No layer for application(pid=%d)", surfaceProperties->creatorPid);
487         }
488     }
489     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
490     {
491         qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
492
493         /* application being down */
494         m_appLayers.remove(surfaceProperties->creatorPid);
495
496         updateScreen();
497     }
498     if (ILM_NOTIFICATION_CONFIGURED & mask)
499     {
500         qDebug("ILM_NOTIFICATION_CONFIGURED");
501         qDebug("  surfaceProperties %d", surface);
502         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
503         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
504
505         ilm_surfaceSetSourceRectangle(surface,
506                                       0,
507                                       0,
508                                       surfaceProperties->origSourceWidth,
509                                       surfaceProperties->origSourceHeight);
510
511         ilm_surfaceSetVisibility(surface, ILM_TRUE);
512
513         updateScreen();
514     }
515 }
516
517 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
518                                     struct ilmSurfaceProperties* surfaceProperties,
519                                     t_ilm_notification_mask mask)
520
521 {
522     static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
523 }
524 #endif
525
526 int WindowManager::layoutId() const
527 {
528     return m_currentLayout;
529 }
530
531 QString WindowManager::layoutName() const
532 {
533     QList<Layout>::const_iterator i = m_layouts.begin();
534
535     QString result = "not found";
536     while (i != m_layouts.constEnd())
537     {
538         if (i->id == m_currentLayout)
539         {
540             result = i->name;
541         }
542
543         ++i;
544     }
545
546     return result;
547 }
548
549
550 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
551 {
552     qDebug("-=[addLayout]=-");
553     m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
554
555     qDebug("addLayout %d %s, size %d",
556            layoutId,
557            layoutName.toStdString().c_str(),
558            surfaceAreas.size());
559
560     dumpScene();
561
562     return WINDOWMANAGER_NO_ERROR;
563 }
564
565 int WindowManager::deleteLayoutById(int layoutId)
566 {
567     qDebug("-=[deleteLayoutById]=-");
568     qDebug("layoutId: %d", layoutId);
569     int result = WINDOWMANAGER_NO_ERROR;
570
571     if (m_currentLayout == layoutId)
572     {
573         result = WINDOWMANAGER_ERROR_ID_IN_USE;
574     }
575     else
576     {
577         QList<Layout>::iterator i = m_layouts.begin();
578         result = WINDOWMANAGER_ERROR_ID_IN_USE;
579         while (i != m_layouts.constEnd())
580         {
581             if (i->id == layoutId)
582             {
583                 m_layouts.erase(i);
584                 result = WINDOWMANAGER_NO_ERROR;
585                 break;
586             }
587
588             ++i;
589         }
590     }
591
592     return result;
593 }
594
595
596 QList<Layout> WindowManager::getAllLayouts()
597 {
598     qDebug("-=[getAllLayouts]=-");
599
600     return m_layouts;
601 }
602
603 #if 0
604 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
605 {
606     QList<int> result;
607 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
608     struct ilmSurfaceProperties surfaceProperties;
609
610     for (int i = 0; i < m_appSurfaces.size(); ++i)
611     {
612         ilm_getPropertiesOfSurface(m_appSurfaces.at(i), &surfaceProperties);
613         if (pid == surfaceProperties.creatorPid)
614         {
615             result.append(m_appSurfaces.at(i));
616         }
617     }
618 #endif
619     return result;
620 }
621 #endif
622
623 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
624 {
625     qDebug("-=[getAvailableLayouts]=-");
626     QList<Layout>::const_iterator i = m_layouts.begin();
627
628     QList<int> result;
629     while (i != m_layouts.constEnd())
630     {
631         if (i->layoutAreas.size() == numberOfAppSurfaces)
632         {
633             result.append(i->id);
634         }
635
636         ++i;
637     }
638
639     return result;
640 }
641
642 #if 0
643 QList<int> WindowManager::getAvailableSurfaces()
644 {
645     qDebug("-=[getAvailableSurfaces]=-");
646
647     return m_appSurfaces;
648 }
649 #endif
650
651 QString WindowManager::getLayoutName(int layoutId)
652 {
653     qDebug("-=[getLayoutName]=-");
654     QList<Layout>::const_iterator i = m_layouts.begin();
655
656     QString result = "not found";
657     while (i != m_layouts.constEnd())
658     {
659         if (i->id == layoutId)
660         {
661             result = i->name;
662         }
663
664         ++i;
665     }
666
667     return result;
668 }
669
670 void WindowManager::hideLayer(int layer)
671 {
672     qDebug("-=[hideLayer]=-");
673     qDebug("layer %d", layer);
674
675 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
676     // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
677     if (layer >= 0 && layer < WINDOWMANAGER_LAYER_NUM) {
678         /* hide target layer */
679         m_showLayers[layer] = 0;
680
681         if (layer == WINDOWMANAGER_LAYER_APPLICATIONS) {
682             /* clear pending flag */
683             m_pending_to_show = -1;
684         } else if (m_pending_to_show != -1) {
685             /* there is a pending application to show */
686             showAppLayer(m_pending_to_show);
687             return;
688         }
689
690         t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
691         int num_layers = getLayerRenderOrder(renderOrder);
692         ilm_displaySetRenderOrder(m_screenId, renderOrder, num_layers);
693         ilm_commitChanges();
694     }
695 #endif
696 }
697
698 int WindowManager::setLayoutById(int layoutId)
699 {
700     qDebug("-=[setLayoutById]=-");
701     int result = WINDOWMANAGER_NO_ERROR;
702     m_currentLayout = layoutId;
703
704     mp_layoutAreaToSurfaceIdAssignment->clear();
705
706     dumpScene();
707
708     return result;
709 }
710
711 int WindowManager::setLayoutByName(const QString &layoutName)
712 {
713     qDebug("-=[setLayoutByName]=-");
714     int result = WINDOWMANAGER_NO_ERROR;
715
716     QList<Layout>::const_iterator i = m_layouts.begin();
717
718     while (i != m_layouts.constEnd())
719     {
720         if (i->name == layoutName)
721         {
722             m_currentLayout = i->id;
723
724             mp_layoutAreaToSurfaceIdAssignment->clear();
725
726             dumpScene();
727         }
728
729         ++i;
730     }
731
732     return result;
733 }
734
735 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
736 {
737     qDebug("-=[setSurfaceToLayoutArea]=-");
738     int result = WINDOWMANAGER_NO_ERROR;
739
740     qDebug("surfaceId %d", surfaceId);
741     qDebug("layoutAreaId %d", layoutAreaId);
742     mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
743
744     updateScreen();
745
746     dumpScene();
747
748     return result;
749 }
750
751 void WindowManager::showLayer(int layer)
752 {
753     qDebug("-=[showLayer]=-");
754     qDebug("layer %d", layer);
755
756 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
757     // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
758     if (layer >= 0 && layer < WINDOWMANAGER_LAYER_NUM) {
759         static const int layer_id_array[] = {
760             WINDOWMANAGER_LAYER_POPUP,
761             WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY,
762             WINDOWMANAGER_LAYER_APPLICATIONS,
763             WINDOWMANAGER_LAYER_HOMESCREEN,
764         };
765
766         m_showLayers[layer] = layer_id_array[layer];
767
768         t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
769         int num_layers = getLayerRenderOrder(renderOrder);
770         ilm_displaySetRenderOrder(m_screenId, renderOrder, num_layers);
771         ilm_commitChanges();
772     }
773 #endif
774 }
775
776 void WindowManager::showAppLayer(int pid)
777 {
778     qDebug("-=[showAppLayer]=-");
779     qDebug("pid %d", pid);
780
781     if (pid == -1) {
782         /* nothing to show */
783         return;
784     }
785
786     /* clear pending flag */
787     m_pending_to_show = -1;
788
789 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
790     /* search layer id for application to show */
791     QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(pid);
792
793     if (i != m_appLayers.end()) {
794         m_showLayers[2] = m_appLayers.value(pid);
795         qDebug("Found layer(%d) to show for app(pid=%d)", m_showLayers[2], pid);
796     } else {
797         /* Probably app layer hasn't been made yet */
798         m_pending_to_show = pid;
799         /* hide current app once, back to default screen */
800         m_showLayers[2] = 0;
801
802         qDebug("No layer to show for app(pid=%d)", pid);
803     }
804     t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
805
806     int num_layers = getLayerRenderOrder(renderOrder);
807     ilm_displaySetRenderOrder(m_screenId, renderOrder, num_layers);
808     ilm_commitChanges();
809 #endif
810 }