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