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