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