Enable automcatic startup of Homescreen app
[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 // the HomeScreen app has to have the surface id 1000
37 #define WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID 1000
38
39
40 void* WindowManager::myThis = 0;
41
42 WindowManager::WindowManager(QObject *parent) :
43     QObject(parent),
44     m_layouts(),
45     m_surfaces(),
46     mp_layoutAreaToSurfaceIdAssignment(0),
47     m_currentLayout(-1)
48 {
49     qDebug("-=[WindowManager]=-");
50 }
51
52 void WindowManager::start()
53 {
54     qDebug("-=[start]=-");
55     mp_layoutAreaToSurfaceIdAssignment = new QMap<int, unsigned int>;
56 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
57     ilmErrorTypes err;
58
59     err = ilm_init();
60     qDebug("ilm_init = %d", err);
61     if(ILM_SUCCESS != err)
62     {
63         qDebug("failed! Exiting!");
64         exit(-1);
65     }
66
67     myThis = this;
68     err =  ilm_registerNotification(WindowManager::notificationFunc_static, this);
69
70     createNewLayer(WINDOWMANAGER_LAYER_POPUP);
71     createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY);
72     createNewLayer(WINDOWMANAGER_LAYER_APPLICATIONS);
73     createNewLayer(WINDOWMANAGER_LAYER_HOMESCREEN);
74 #endif
75
76     QDBusConnection dbus = QDBusConnection::sessionBus();
77     dbus.registerObject("/windowmanager", this);
78     dbus.registerService("org.agl.windowmanager");
79
80     // publish windowmanager interface
81     mp_windowManagerAdaptor = new WindowmanagerAdaptor((QObject*)this);
82 }
83
84 WindowManager::~WindowManager()
85 {
86     qDebug("-=[~WindowManager]=-");
87     delete mp_windowManagerAdaptor;
88 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
89     ilm_destroy();
90 #endif
91     delete mp_layoutAreaToSurfaceIdAssignment;
92 }
93
94 void WindowManager::dumpScene()
95 {
96     qDebug("\n");
97     qDebug("current layout   : %d", m_currentLayout);
98     qDebug("available layouts: %d", m_layouts.size());
99     QList<Layout>::const_iterator i = m_layouts.begin();
100
101     while (i != m_layouts.constEnd())
102     {
103         qDebug("--[id: %d]--[%s]--", i->id, i->name.toStdString().c_str());
104         qDebug("  %d surface areas", i->layoutAreas.size());
105         for (int j = 0; j < i->layoutAreas.size(); ++j)
106         {
107             qDebug("  -area %d", j);
108             qDebug("    -x     : %d", i->layoutAreas.at(j).x);
109             qDebug("    -y     : %d", i->layoutAreas.at(j).y);
110             qDebug("    -width : %d", i->layoutAreas.at(j).width);
111             qDebug("    -height: %d", i->layoutAreas.at(j).height);
112         }
113
114         ++i;
115     }
116 }
117
118 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
119
120 void WindowManager::createNewLayer(int layerId)
121 {
122     qDebug("-=[createNewLayer]=-");
123     qDebug("layerId %d", layerId);
124
125     t_ilm_uint screenID = 0;
126     t_ilm_uint width;
127     t_ilm_uint height;
128
129     ilm_getScreenResolution(screenID, &width, &height);
130
131     t_ilm_layer newLayerId = layerId;
132     ilm_layerCreateWithDimension(&newLayerId, width, height);
133     ilm_layerSetOpacity(newLayerId, 1.0);
134     ilm_layerSetVisibility(newLayerId, ILM_TRUE);
135     ilm_layerSetSourceRectangle(newLayerId,
136                                     0,
137                                     0,
138                                     width,
139                                     height);
140     ilm_layerSetDestinationRectangle(newLayerId,
141                                     0,
142                                     0,
143                                     width,
144                                     height);
145
146     ilm_commitChanges();
147 }
148
149 void WindowManager::addSurfaceToLayer(int surfaceId, int layerId)
150 {
151     qDebug("-=[addSurfaceToLayer]=-");
152     qDebug("surfaceId %d", surfaceId);
153     qDebug("layerId %d", layerId);
154
155     if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN)
156     {
157         struct ilmSurfaceProperties surfaceProperties;
158         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
159
160         qDebug("sourceX %d", surfaceProperties.sourceX);
161         qDebug("sourceY %d", surfaceProperties.sourceY);
162         qDebug("sourceWidth %d", surfaceProperties.sourceWidth);
163         qDebug("sourceHeight %d", surfaceProperties.sourceHeight);
164
165         // homescreen app always fullscreen in the back
166         t_ilm_uint screenID = 0;
167         t_ilm_uint width;
168         t_ilm_uint height;
169
170         ilm_getScreenResolution(screenID, &width, &height);
171
172         ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, width, height);
173         ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, width, height);
174         ilm_surfaceSetOpacity(surfaceId, 1.0);
175         ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
176
177         ilm_layerAddSurface(layerId, surfaceId);
178     }
179
180     if (layerId == WINDOWMANAGER_LAYER_APPLICATIONS)
181     {
182         struct ilmSurfaceProperties surfaceProperties;
183         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
184
185         ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
186         ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
187         ilm_surfaceSetOpacity(surfaceId, 0.0);
188         ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
189
190         ilm_layerAddSurface(layerId, surfaceId);
191     }
192
193     if (layerId == WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY)
194     {
195         struct ilmSurfaceProperties surfaceProperties;
196         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
197
198         ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
199         ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
200         ilm_surfaceSetOpacity(surfaceId, 0.5);
201         ilm_surfaceSetVisibility(surfaceId, ILM_TRUE);
202
203         ilm_layerAddSurface(layerId, surfaceId);
204     }
205
206     if (layerId == WINDOWMANAGER_LAYER_POPUP)
207     {
208         struct ilmSurfaceProperties surfaceProperties;
209         ilm_getPropertiesOfSurface(surfaceId, &surfaceProperties);
210
211         ilm_surfaceSetDestinationRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
212         ilm_surfaceSetSourceRectangle(surfaceId, 0, 0, surfaceProperties.origSourceWidth, surfaceProperties.origSourceHeight);
213         ilm_surfaceSetOpacity(surfaceId, 0.0);
214         ilm_surfaceSetVisibility(surfaceId, ILM_FALSE);
215
216         ilm_layerAddSurface(layerId, surfaceId);
217     }
218
219     ilm_commitChanges();
220 }
221
222 #endif
223
224 void WindowManager::updateScreen()
225 {
226     qDebug("-=[updateScreen]=-");
227
228 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
229     if (-1 != m_currentLayout)
230     {
231
232         // hide all surfaces
233         for (int i = 0; i < m_surfaces.size(); ++i)
234         {
235             ilm_surfaceSetVisibility(m_surfaces.at(i), ILM_FALSE);
236             ilm_surfaceSetOpacity(m_surfaces.at(i), 0.0);
237         }
238
239         // find the current used layout
240         QList<Layout>::const_iterator ci = m_layouts.begin();
241
242         Layout currentLayout;
243         while (ci != m_layouts.constEnd())
244         {
245             if (ci->id == m_currentLayout)
246             {
247                 currentLayout = *ci;
248             }
249
250             ++ci;
251         }
252
253         qDebug("show %d apps", mp_layoutAreaToSurfaceIdAssignment->size());
254         for (int j = 0; j < mp_layoutAreaToSurfaceIdAssignment->size(); ++j)
255         {
256             int surfaceToShow = mp_layoutAreaToSurfaceIdAssignment->find(j).value();
257             qDebug("  surface no. %d: %d", j, surfaceToShow);
258
259             ilm_surfaceSetVisibility(surfaceToShow, ILM_TRUE);
260             ilm_surfaceSetOpacity(surfaceToShow, 1.0);
261
262             qDebug("  layout area %d", j);
263             qDebug("    x: %d", currentLayout.layoutAreas[j].x);
264             qDebug("    y: %d", currentLayout.layoutAreas[j].y);
265             qDebug("    w: %d", currentLayout.layoutAreas[j].width);
266             qDebug("    h: %d", currentLayout.layoutAreas[j].height);
267
268             ilm_surfaceSetDestinationRectangle(surfaceToShow,
269                                              currentLayout.layoutAreas[j].x,
270                                              currentLayout.layoutAreas[j].y,
271                                              currentLayout.layoutAreas[j].width,
272                                              currentLayout.layoutAreas[j].height);
273         }
274
275         ilm_commitChanges();
276     }
277
278     t_ilm_layer renderOrder[WINDOWMANAGER_LAYER_NUM];
279     renderOrder[0] = WINDOWMANAGER_LAYER_HOMESCREEN;
280     renderOrder[1] = WINDOWMANAGER_LAYER_APPLICATIONS;
281     renderOrder[2] = WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY;
282     renderOrder[3] = WINDOWMANAGER_LAYER_POPUP;
283
284     ilm_displaySetRenderOrder(0, renderOrder, WINDOWMANAGER_LAYER_NUM);
285
286     ilm_commitChanges();
287
288 #endif
289 }
290
291 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
292 void WindowManager::notificationFunc_non_static(ilmObjectType object,
293                                     t_ilm_uint id,
294                                     t_ilm_bool created)
295 {
296     qDebug("-=[notificationFunc_non_static]=-");
297     qDebug("Notification from weston!");
298     if (ILM_SURFACE == object)
299     {
300         struct ilmSurfaceProperties surfaceProperties;
301
302         if (created)
303         {
304             qDebug("Surface created, ID: %d", id);
305             ilm_getPropertiesOfSurface(id, &surfaceProperties);
306             qDebug("  origSourceWidth : %d", surfaceProperties.origSourceWidth);
307             qDebug("  origSourceHeight: %d", surfaceProperties.origSourceHeight);
308
309             if (WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID == id)
310             {
311                 qDebug("HomeScreen app detected");
312                 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_HOMESCREEN);
313                 updateScreen();
314             }
315             else
316             {
317                 addSurfaceToLayer(id, WINDOWMANAGER_LAYER_APPLICATIONS);
318
319                 m_surfaces.append(id);
320             }
321             ilm_surfaceAddNotification(id, surfaceCallbackFunction_static);
322
323             ilm_commitChanges();
324         }
325         else
326         {
327             qDebug("Surface destroyed, ID: %d", id);
328             m_surfaces.removeAt(m_surfaces.indexOf(id));
329             ilm_surfaceRemoveNotification(id);
330
331             ilm_commitChanges();
332         }
333     }
334     if (ILM_LAYER == object)
335     {
336         //qDebug("Layer.. we don't care...");
337     }
338 }
339
340 void WindowManager::notificationFunc_static(ilmObjectType object,
341                                             t_ilm_uint id,
342                                             t_ilm_bool created,
343                                             void* user_data)
344 {
345     static_cast<WindowManager*>(WindowManager::myThis)->notificationFunc_non_static(object, id, created);
346 }
347
348 void WindowManager::surfaceCallbackFunction_non_static(t_ilm_surface surface,
349                                     struct ilmSurfaceProperties* surfaceProperties,
350                                     t_ilm_notification_mask mask)
351 {
352     qDebug("-=[surfaceCallbackFunction_non_static]=-");
353     qDebug("surfaceCallbackFunction_non_static changes for surface %d", surface);
354     if (ILM_NOTIFICATION_VISIBILITY & mask)
355     {
356         qDebug("ILM_NOTIFICATION_VISIBILITY");
357         surfaceVisibilityChanged(surface, surfaceProperties->visibility);
358     }
359     if (ILM_NOTIFICATION_OPACITY & mask)
360     {
361         qDebug("ILM_NOTIFICATION_OPACITY");
362     }
363     if (ILM_NOTIFICATION_ORIENTATION & mask)
364     {
365         qDebug("ILM_NOTIFICATION_ORIENTATION");
366     }
367     if (ILM_NOTIFICATION_SOURCE_RECT & mask)
368     {
369         qDebug("ILM_NOTIFICATION_SOURCE_RECT");
370     }
371     if (ILM_NOTIFICATION_DEST_RECT & mask)
372     {
373         qDebug("ILM_NOTIFICATION_DEST_RECT");
374     }
375     if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
376     {
377         qDebug("ILM_NOTIFICATION_CONTENT_AVAILABLE");
378         updateScreen();
379     }
380     if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
381     {
382         qDebug("ILM_NOTIFICATION_CONTENT_REMOVED");
383     }
384     if (ILM_NOTIFICATION_CONFIGURED & mask)
385     {
386         qDebug("ILM_NOTIFICATION_CONFIGURED");
387         qDebug("  surfaceProperties %d", surface);
388         qDebug("    surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
389         qDebug("    surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
390
391         ilm_surfaceSetSourceRectangle(surface,
392                                       0,
393                                       0,
394                                       surfaceProperties->origSourceWidth,
395                                       surfaceProperties->origSourceHeight);
396
397         ilm_commitChanges();
398         updateScreen();
399     }
400 }
401
402 void WindowManager::surfaceCallbackFunction_static(t_ilm_surface surface,
403                                     struct ilmSurfaceProperties* surfaceProperties,
404                                     t_ilm_notification_mask mask)
405
406 {
407     static_cast<WindowManager*>(WindowManager::myThis)->surfaceCallbackFunction_non_static(surface, surfaceProperties, mask);
408 }
409 #endif
410
411 int WindowManager::layoutId() const
412 {
413     return m_currentLayout;
414 }
415
416 QString WindowManager::layoutName() const
417 {
418     QList<Layout>::const_iterator i = m_layouts.begin();
419
420     QString result = "not found";
421     while (i != m_layouts.constEnd())
422     {
423         if (i->id == m_currentLayout)
424         {
425             result = i->name;
426         }
427
428         ++i;
429     }
430
431     return result;
432 }
433
434
435 int WindowManager::addLayout(int layoutId, const QString &layoutName, const QList<LayoutArea> &surfaceAreas)
436 {
437     qDebug("-=[addLayout]=-");
438     m_layouts.append(Layout(layoutId, layoutName, surfaceAreas));
439
440     qDebug("addLayout %d %s, size %d",
441            layoutId,
442            layoutName.toStdString().c_str(),
443            surfaceAreas.size());
444
445     dumpScene();
446
447     return WINDOWMANAGER_NO_ERROR;
448 }
449
450 int WindowManager::deleteLayoutById(int layoutId)
451 {
452     qDebug("-=[deleteLayoutById]=-");
453     qDebug("layoutId: %d", layoutId);
454     int result = WINDOWMANAGER_NO_ERROR;
455
456     if (m_currentLayout == layoutId)
457     {
458         result = WINDOWMANAGER_ERROR_ID_IN_USE;
459     }
460     else
461     {
462         QList<Layout>::iterator i = m_layouts.begin();
463         result = WINDOWMANAGER_ERROR_ID_IN_USE;
464         while (i != m_layouts.constEnd())
465         {
466             if (i->id == layoutId)
467             {
468                 m_layouts.erase(i);
469                 result = WINDOWMANAGER_NO_ERROR;
470                 break;
471             }
472
473             ++i;
474         }
475     }
476
477     return result;
478 }
479
480
481 QList<Layout> WindowManager::getAllLayouts()
482 {
483     qDebug("-=[getAllLayouts]=-");
484
485     return m_layouts;
486 }
487
488 QList<int> WindowManager::getAllSurfacesOfProcess(int pid)
489 {
490     QList<int> result;
491 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
492     struct ilmSurfaceProperties surfaceProperties;
493
494     for (int i = 0; i < m_surfaces.size(); ++i)
495     {
496         ilm_getPropertiesOfSurface(m_surfaces.at(i), &surfaceProperties);
497         if (pid == surfaceProperties.creatorPid)
498         {
499             result.append(m_surfaces.at(i));
500         }
501     }
502 #endif
503     return result;
504 }
505
506 QList<int> WindowManager::getAvailableLayouts(int numberOfAppSurfaces)
507 {
508     qDebug("-=[getAvailableLayouts]=-");
509     QList<Layout>::const_iterator i = m_layouts.begin();
510
511     QList<int> result;
512     while (i != m_layouts.constEnd())
513     {
514         if (i->layoutAreas.size() == numberOfAppSurfaces)
515         {
516             result.append(i->id);
517         }
518
519         ++i;
520     }
521
522     return result;
523 }
524
525 QList<int> WindowManager::getAvailableSurfaces()
526 {
527     qDebug("-=[getAvailableSurfaces]=-");
528
529     return m_surfaces;
530 }
531
532 QString WindowManager::getLayoutName(int layoutId)
533 {
534     qDebug("-=[getLayoutName]=-");
535     QList<Layout>::const_iterator i = m_layouts.begin();
536
537     QString result = "not found";
538     while (i != m_layouts.constEnd())
539     {
540         if (i->id == layoutId)
541         {
542             result = i->name;
543         }
544
545         ++i;
546     }
547
548     return result;
549 }
550
551 void WindowManager::hideLayer(int layer)
552 {
553     qDebug("-=[hideLayer]=-");
554     qDebug("layer %d", layer);
555
556 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
557     if (0 == layer)
558     {
559         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_FALSE);
560     }
561     if (1 == layer)
562     {
563         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_FALSE);
564     }
565     if (2 == layer)
566     {
567         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_FALSE);
568     }
569     if (3 == layer)
570     {
571         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_FALSE);
572     }
573     ilm_commitChanges();
574 #endif
575 }
576
577 int WindowManager::setLayoutById(int layoutId)
578 {
579     qDebug("-=[setLayoutById]=-");
580     int result = WINDOWMANAGER_NO_ERROR;
581     m_currentLayout = layoutId;
582
583     mp_layoutAreaToSurfaceIdAssignment->clear();
584
585     dumpScene();
586
587     return result;
588 }
589
590 int WindowManager::setLayoutByName(const QString &layoutName)
591 {
592     qDebug("-=[setLayoutByName]=-");
593     int result = WINDOWMANAGER_NO_ERROR;
594
595     QList<Layout>::const_iterator i = m_layouts.begin();
596
597     while (i != m_layouts.constEnd())
598     {
599         if (i->name == layoutName)
600         {
601             m_currentLayout = i->id;
602
603             mp_layoutAreaToSurfaceIdAssignment->clear();
604
605             dumpScene();
606         }
607
608         ++i;
609     }
610
611     return result;
612 }
613
614 int WindowManager::setSurfaceToLayoutArea(int surfaceId, int layoutAreaId)
615 {
616     qDebug("-=[setSurfaceToLayoutArea]=-");
617     int result = WINDOWMANAGER_NO_ERROR;
618
619     qDebug("surfaceId %d", surfaceId);
620     qDebug("layoutAreaId %d", layoutAreaId);
621     mp_layoutAreaToSurfaceIdAssignment->insert(layoutAreaId, surfaceId);
622
623     updateScreen();
624
625     dumpScene();
626
627     return result;
628 }
629
630 void WindowManager::showLayer(int layer)
631 {
632     qDebug("-=[showLayer]=-");
633     qDebug("layer %d", layer);
634
635 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
636     if (0 == layer)
637     {
638         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_POPUP, ILM_TRUE);
639     }
640     if (1 == layer)
641     {
642         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN_OVERLAY, ILM_TRUE);
643     }
644     if (2 == layer)
645     {
646         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_APPLICATIONS, ILM_TRUE);
647     }
648     if (3 == layer)
649     {
650         ilm_layerSetVisibility(WINDOWMANAGER_LAYER_HOMESCREEN, ILM_TRUE);
651     }
652     ilm_commitChanges();
653 #endif
654 }