c3080ffe549504ce6c8645c80a97e4ecd5e429d8
[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(0, renderOrder, num_layers);
357         ilm_displaySetRenderOrder(1, renderOrder, num_layers);
358         ilm_commitChanges();
359     }
360 }
361
362 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
363 void WindowManager::notificationFunc_non_static(ilmObjectType object,
364                                     t_ilm_uint id,
365                                     t_ilm_bool created)
366 {
367     qDebug("-=[notificationFunc_non_static]=-");
368     qDebug("Notification from weston!");
369     if (ILM_SURFACE == object)
370     {
371         struct ilmSurfaceProperties surfaceProperties;
372
373         if (created)
374         {
375             qDebug("Surface created, ID: %d", id);
376             ilm_getPropertiesOfSurface(id, &surfaceProperties);
377             qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
378             qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
379
380             if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
381             {
382                 qDebug("HomeScreen app detected");
383                 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
384                 updateScreen();
385             }
386             else
387             {
388                 addSurfaceToAppLayer(id);
389                 //addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
390                 //m_appSurfaces.append(id);
391             }
392             ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
393
394             ilm_commitChanges();
395         }
396         else
397         {
398             qDebug("Surface destroyed, ID: %d", id);
399 #if 0
400             m_appSurfaces.removeAt(m_appSurfaces.indexOf(id));
401             ilm_surfaceRemoveNotification(id);
402
403             ilm_commitChanges();
404 #endif
405         }
406     }
407     if (ILM_LAYER == object)
408     {
409         //qDebug("Layer.. we don't care...");
410     }
411 }
412
413 void WindowManager::notificationFunc_static(ilmObjectType object,
414                                             t_ilm_uint id,
415                                             t_ilm_bool created,
416                                             void* user_data)
417 {
418     static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
419 }
420
421 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
422                                     struct ilmSurfaceProperties* surfaceProperties,
423                                     t_ilm_notification_mask mask)
424 {
425     qDebug("-=[surfaceCallbackFunction_non_static]=-");
426     qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
427     if (ILM_NOTIFICATION_VISIBILITY & mask)
428     {
429         qDebug("ILM_NOTIFICATION_VISIBILITY");
430         surfaceVisibilityChanged(surface, surfaceProperties->visibility);
431     }
432     if (ILM_NOTIFICATION_OPACITY & mask)
433     {
434         qDebug("ILM_NOTIFICATION_OPACITY");
435     }
436     if (ILM_NOTIFICATION_ORIENTATION & mask)
437     {
438         qDebug("ILM_NOTIFICATION_ORIENTATION");
439     }
440     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
441     {
442         qDebug("ILM_NOTIFICATION_SOURCE_RECT");
443     }
444     if (ILM_NOTIFICATION_DEST_RECT & mask)
445     {
446         qDebug("ILM_NOTIFICATION_DEST_RECT");
447     }
448     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
449     {
450         qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
451         /* add surface to layer for the application */
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(0, renderOrder, num_layers);
693         ilm_displaySetRenderOrder(1, renderOrder, num_layers);
694         ilm_commitChanges();
695     }
696 #endif
697 }
698
699 int WindowManager::setLayoutById(int layoutId)
700 {
701     qDebug("-=[setLayoutById]=-");
702     int result = WINDOWMANAGER_NO_ERROR;
703     m_currentLayout = layoutId;
704
705     mp_layoutAreaToSurfaceIdAssignment->clear();
706
707     dumpScene();
708
709     return result;
710 }
711
712 int WindowManager::setLayoutByName(const QString &layoutName)
713 {
714     qDebug("-=[setLayoutByName]=-");
715     int result = WINDOWMANAGER_NO_ERROR;
716
717     QList<Layout>::const_iterator i = m_layouts.begin();
718
719     while (i != m_layouts.constEnd())
720     {
721         if (i->name == layoutName)
722         {
723             m_currentLayout = i->id;
724
725             mp_layoutAreaToSurfaceIdAssignment->clear();
726
727             dumpScene();
728         }
729
730         ++i;
731     }
732
733     return result;
734 }
735
736 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
737 {
738     qDebug("-=[setSurfaceToLayoutArea]=-");
739     int result = WINDOWMANAGER_NO_ERROR;
740
741     qDebug("surfaceId %d", surfaceId);
742     qDebug("layoutAreaId %d", layoutAreaId);
743     mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
744
745     updateScreen();
746
747     dumpScene();
748
749     return result;
750 }
751
752 void WindowManager::showLayer(int layer)
753 {
754     qDebug("-=[showLayer]=-");
755     qDebug("layer %d", layer);
756
757 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
758     // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
759     if (layer >= 0 && layer < WINDOWMANAGER_LAYER_NUM) {
760         static const int layer_id_array[] = {
761             WINDOWMANAGER_LAYER_POPUP,
762             WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY,
763             WINDOWMANAGER_LAYER_APPLICATIONS,
764             WINDOWMANAGER_LAYER_HOMESCREEN,
765         };
766
767         m_showLayers[layer] = layer_id_array[layer];
768
769         t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
770         int num_layers = getLayerRenderOrder(renderOrder);
771         ilm_displaySetRenderOrder(0, renderOrder, num_layers);
772         ilm_displaySetRenderOrder(1, renderOrder, num_layers);
773         ilm_commitChanges();
774     }
775 #endif
776 }
777
778 void WindowManager::showAppLayer(int pid)
779 {
780     qDebug("-=[showAppLayer]=-");
781     qDebug("pid %d", pid);
782
783     if (pid == -1) {
784         /* nothing to show */
785         return;
786     }
787
788     /* clear pending flag */
789     m_pending_to_show = -1;
790
791 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
792     /* search layer id for application to show */
793     QMap<pid_t, t_ilm_layer>::const_iterator i = m_appLayers.find(pid);
794
795     if (i != m_appLayers.end()) {
796         m_showLayers[2] = m_appLayers.value(pid);
797         qDebug("Found layer(%d) to show for app(pid=%d)", m_showLayers[2], pid);
798     } else {
799         /* Probably app layer hasn't been made yet */
800         m_pending_to_show = pid;
801         /* hide current app once, back to default screen */
802         m_showLayers[2] = 0;
803
804         qDebug("No layer to show for app(pid=%d)", pid);
805     }
806     t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
807
808     int num_layers = getLayerRenderOrder(renderOrder);
809     ilm_displaySetRenderOrder(0, renderOrder, num_layers);
810     ilm_displaySetRenderOrder(1, renderOrder, num_layers);
811     ilm_commitChanges();
812 #endif
813 }