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