Additional parameter to WindowManager at application startup
[apps/homescreen.git] / homescreen / src / layouthandler.cpp
1 /*
2  * Copyright (C) 2016, 2017 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 "layouthandler.h"
18 #include <QTimerEvent>
19
20 LayoutHandler::LayoutHandler(QObject *parent) :
21     QObject(parent),
22     m_secondsTimerId(-1),
23     mp_dBusWindowManagerProxy(0),
24     mp_dBusPopupProxy(0),
25     m_visibleSurfaces(),
26     m_invisibleSurfaces(),
27     m_requestsToBeVisibleSurfaces()
28 {
29     qDBusRegisterMetaType<SimplePoint>();
30     qDBusRegisterMetaType<QList<SimplePoint> >();
31     qDBusRegisterMetaType<LayoutArea>();
32     qDBusRegisterMetaType<QList<LayoutArea> >();
33     qDBusRegisterMetaType<Layout>();
34     qDBusRegisterMetaType<QList<Layout> >();
35
36     qDebug("D-Bus: connect to org.agl.windowmanager /windowmanager");
37     mp_dBusWindowManagerProxy = new org::agl::windowmanager("org.agl.windowmanager",
38                                               "/windowmanager",
39                                               QDBusConnection::sessionBus(),
40                                               0);
41     qDebug("D-Bus: connect to org.agl.homescreen /Popup");
42     mp_dBusPopupProxy = new org::agl::popup("org.agl.homescreen",
43                                               "/Popup",
44                                               QDBusConnection::sessionBus(),
45                                               0);
46
47     QDBusConnection::sessionBus().connect("org.agl.windowmanager",
48                                        "/windowmanager",
49                                        "org.agl.windowmanager",
50                                        "surfaceVisibilityChanged",
51                                        this,
52                                        SIGNAL(surfaceVisibilityChanged(int,bool)));
53
54     QList<LayoutArea> surfaceAreas;
55     LayoutArea surfaceArea;
56
57     const int SCREEN_WIDTH = 1080;
58     const int SCREEN_HEIGHT = 1920;
59
60     const int TOPAREA_HEIGHT = 218;
61     const int TOPAREA_WIDTH = SCREEN_WIDTH;
62     const int TOPAREA_X = 0;
63     const int TOPAREA_Y = 0;
64     const int MEDIAAREA_HEIGHT = 215;
65     const int MEDIAAREA_WIDTH = SCREEN_WIDTH;
66     const int MEDIAAREA_X = 0;
67     const int MEDIAAREA_Y = SCREEN_HEIGHT - MEDIAAREA_HEIGHT;
68
69
70     // only one Layout for CES2017 needed
71     // layout 1:
72     // one app surface, statusbar, control bar
73     surfaceArea.x = 0;
74     surfaceArea.y = TOPAREA_HEIGHT;
75     surfaceArea.width = SCREEN_WIDTH;
76     surfaceArea.height = SCREEN_HEIGHT - TOPAREA_HEIGHT - MEDIAAREA_HEIGHT;
77
78     surfaceAreas.append(surfaceArea);
79
80     mp_dBusWindowManagerProxy->addLayout(1, "one app", surfaceAreas);
81 }
82
83 LayoutHandler::~LayoutHandler()
84 {
85     delete mp_dBusPopupProxy;
86     delete mp_dBusWindowManagerProxy;
87 }
88
89 void LayoutHandler::showAppLayer(const QString &app_id, int pid)
90 {
91     mp_dBusWindowManagerProxy->showAppLayer(app_id, pid);
92 }
93
94 void LayoutHandler::hideAppLayer()
95 {
96     // POPUP=0, HOMESCREEN_OVERLAY=1, APPS=2, HOMESCREEN=3
97     mp_dBusWindowManagerProxy->hideLayer(2); // TODO: enum
98 }
99
100 void LayoutHandler::makeMeVisible(int pid)
101 {
102     qDebug("makeMeVisible %d", pid);
103
104 #if 0
105     // if app does not request to be visible
106     if (-1 == m_requestsToBeVisiblePids.indexOf(pid))
107     {
108         m_requestsToBeVisiblePids.append(pid);
109
110         // callback every second
111         if (-1 != m_secondsTimerId)
112         {
113             killTimer(m_secondsTimerId);
114             m_secondsTimerId = -1;
115         }
116         m_secondsTimerId = startTimer(1000);
117     }
118     else
119     {
120         checkToDoQueue();
121     }
122 #endif
123 }
124
125 void LayoutHandler::checkToDoQueue()
126 {
127 #if 0
128     if ((-1 != m_secondsTimerId) && (0 == m_requestsToBeVisiblePids.size()))
129     {
130         killTimer(m_secondsTimerId);
131         m_secondsTimerId = -1;
132     }
133
134     if (0 != m_requestsToBeVisiblePids.size())
135     {
136         int pid = m_requestsToBeVisiblePids.at(0);
137         qDebug("pid %d wants to be visible", pid);
138
139         QList<int> allSurfaces;
140         allSurfaces = mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
141         if (0 == allSurfaces.size())
142         {
143             qDebug("no surfaces for pid %d. retrying!", pid);
144         }
145         else
146         {
147             m_requestsToBeVisiblePids.removeAt(0);
148             qSort(allSurfaces);
149
150             if (0 != allSurfaces.size())
151             {
152                 int firstSurface = allSurfaces.at(0);
153
154                 if (-1 != m_visibleSurfaces.indexOf(firstSurface))
155                 {
156                     qDebug("already visible");
157                 }
158                 else
159                 {
160                     if (-1 != m_invisibleSurfaces.indexOf(firstSurface))
161                     {
162                         m_invisibleSurfaces.removeAt(m_invisibleSurfaces.indexOf(firstSurface));
163                     }
164                     if (-1 == m_requestsToBeVisibleSurfaces.indexOf(firstSurface))
165                     {
166                         m_requestsToBeVisibleSurfaces.append(firstSurface);
167                     }
168
169                     qDebug("before");
170                     qDebug(" m_visibleSurfaces %d", m_visibleSurfaces.size());
171                     qDebug(" m_invisibleSurfaces %d", m_invisibleSurfaces.size());
172                     qDebug(" m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
173
174                     QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(1); // one app only for CES2017
175                     if (1 == availableLayouts.size())
176                     {
177                         qDebug("active layout: %d", availableLayouts.at(0));
178                         m_invisibleSurfaces.append(m_visibleSurfaces);
179                         m_visibleSurfaces.clear();
180                         m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
181                         m_requestsToBeVisibleSurfaces.clear();
182
183                         mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
184                         for (int i = 0; i < m_visibleSurfaces.size(); ++i)
185                         {
186                             mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
187                         }
188
189                         qDebug("after");
190                         qDebug(" m_visibleSurfaces %d", m_visibleSurfaces.size());
191                         qDebug(" m_invisibleSurfaces %d", m_invisibleSurfaces.size());
192                         qDebug(" m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
193                     }
194                     else
195                     {
196                         qDebug("this should not happen!?");
197                     }
198                 }
199             }
200         }
201     }
202 #endif
203 }
204
205 #if 0
206 QList<int> LayoutHandler::requestGetAllSurfacesOfProcess(int pid)
207 {
208     qDebug("requestGetAllSurfacesOfProcess %d", pid);
209
210     return mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
211 }
212 #endif
213
214 int LayoutHandler::requestGetSurfaceStatus(int surfaceId)
215 {
216     int result = -1;
217
218     if (-1 != m_visibleSurfaces.indexOf(surfaceId))
219     {
220         result = 0;
221     }
222     if (-1 != m_invisibleSurfaces.indexOf(surfaceId))
223     {
224         result = 1;
225     }
226     if (-1 != m_requestsToBeVisibleSurfaces.indexOf(surfaceId))
227     {
228         result = 1;
229     }
230
231     return result;
232 }
233
234 void LayoutHandler::requestRenderSurfaceToArea(int surfaceId, int layoutArea)
235 {
236     qDebug("requestRenderSurfaceToArea %d %d", surfaceId, layoutArea);
237     mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(surfaceId, layoutArea);
238 }
239
240 bool LayoutHandler::requestRenderSurfaceToAreaAllowed(int surfaceId, int layoutArea)
241 {
242     qDebug("requestRenderSurfaceToAreaAllowed %d %d", surfaceId, layoutArea);
243     bool result = true;
244     return result;
245 }
246
247 void LayoutHandler::requestSurfaceIdToFullScreen(int surfaceId)
248 {
249     qDebug("requestSurfaceIdToFullScreen %d", surfaceId);
250 }
251
252 void LayoutHandler::setLayoutByName(QString layoutName)
253 {
254     // switch to new layout
255     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
256     m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
257     m_requestsToBeVisibleSurfaces.clear();
258
259     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
260     for (int i = 0; i < m_visibleSurfaces.size(); ++i)
261     {
262         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
263     }
264 }
265
266 void LayoutHandler::requestSurfaceVisibilityChanged(int surfaceId, bool visible)
267 {
268     qDebug("requestSurfaceVisibilityChanged %d %s", surfaceId, visible ? "true" : "false");
269     emit surfaceVisibilityChanged(surfaceId, visible);
270 }
271
272 void LayoutHandler::timerEvent(QTimerEvent *e)
273 {
274     if (e->timerId() == m_secondsTimerId)
275     {
276         checkToDoQueue();
277     }
278 }
279