First integration of new HMI design
[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     m_requestsToBeVisiblePids.append(pid);
90
91     // callback every second
92     if (-1 != m_secondsTimerId)
93     {
94         killTimer(m_secondsTimerId);
95         m_secondsTimerId = -1;
96     }
97     m_secondsTimerId = startTimer(1000);
98 }
99
100 void LayoutHandler::checkToDoQueue()
101 {
102     if ((-1 != m_secondsTimerId) && (0 == m_requestsToBeVisiblePids.size()))
103     {
104         killTimer(m_secondsTimerId);
105         m_secondsTimerId = -1;
106     }
107
108     if (0 != m_requestsToBeVisiblePids.size())
109     {
110         int pid = m_requestsToBeVisiblePids.at(0);
111         qDebug("pid %d wants to be visible", pid);
112
113         QList<int> allSurfaces;
114         allSurfaces = mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
115         if (0 == allSurfaces.size())
116         {
117             qDebug("no surfaces for pid %d. retrying!", pid);
118         }
119         else
120         {
121             m_requestsToBeVisiblePids.removeAt(0);
122             qSort(allSurfaces);
123
124             if (0 != allSurfaces.size())
125             {
126                 if (-1 == m_visibleSurfaces.indexOf(allSurfaces.at(0)))
127                 {
128                     qDebug("already visible");
129                 }
130                 if (-1 == m_invisibleSurfaces.indexOf(allSurfaces.at(0)))
131                 {
132                     m_invisibleSurfaces.removeAt(m_invisibleSurfaces.indexOf(allSurfaces.at(0)));
133                 }
134                 if (-1 == m_requestsToBeVisibleSurfaces.indexOf(allSurfaces.at(0)))
135                 {
136                     m_requestsToBeVisibleSurfaces.append(allSurfaces.at(0));
137                 }
138
139                 qDebug("m_visibleSurfaces %d", m_visibleSurfaces.size());
140                 qDebug("m_invisibleSurfaces %d", m_invisibleSurfaces.size());
141                 qDebug("m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
142
143                 QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(1); // one app only for CES2017
144                 if (1 == availableLayouts.size())
145                 {
146                     qDebug("active layout: %d", availableLayouts.at(0));
147                     m_invisibleSurfaces.append(m_visibleSurfaces);
148                     m_visibleSurfaces.clear();
149                     m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
150                     m_requestsToBeVisibleSurfaces.clear();
151
152                     mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
153                     for (int i = 0; i < m_visibleSurfaces.size(); ++i)
154                     {
155                         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
156                     }
157                 }
158                 else
159                 {
160                     qDebug("this should not happen!?");
161                 }
162             }
163         }
164     }
165 }
166
167 QList<int> LayoutHandler::requestGetAllSurfacesOfProcess(int pid)
168 {
169     qDebug("requestGetAllSurfacesOfProcess %d", pid);
170
171     return mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
172 }
173
174 int LayoutHandler::requestGetSurfaceStatus(int surfaceId)
175 {
176     int result = -1;
177
178     if (-1 != m_visibleSurfaces.indexOf(surfaceId))
179     {
180         result = 0;
181     }
182     if (-1 != m_invisibleSurfaces.indexOf(surfaceId))
183     {
184         result = 1;
185     }
186     if (-1 != m_requestsToBeVisibleSurfaces.indexOf(surfaceId))
187     {
188         result = 1;
189     }
190
191     return result;
192 }
193
194 void LayoutHandler::requestRenderSurfaceToArea(int surfaceId, int layoutArea)
195 {
196     qDebug("requestRenderSurfaceToArea %d %d", surfaceId, layoutArea);
197 }
198
199 bool LayoutHandler::requestRenderSurfaceToAreaAllowed(int surfaceId, int layoutArea)
200 {
201     qDebug("requestRenderSurfaceToAreaAllowed %d %d", surfaceId, layoutArea);
202     bool result = true;
203     return result;
204 }
205
206 void LayoutHandler::requestSurfaceIdToFullScreen(int surfaceId)
207 {
208     qDebug("requestSurfaceIdToFullScreen %d", surfaceId);
209 }
210
211 void LayoutHandler::setLayoutByName(QString layoutName)
212 {
213     // switch to new layout
214     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
215     m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
216     m_requestsToBeVisibleSurfaces.clear();
217
218     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
219     for (int i = 0; i < m_visibleSurfaces.size(); ++i)
220     {
221         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
222     }
223 }
224
225 void LayoutHandler::requestSurfaceVisibilityChanged(int surfaceId, bool visible)
226 {
227     qDebug("requestSurfaceVisibilityChanged %d %s", surfaceId, visible ? "true" : "false");
228     emit surfaceVisibilityChanged(surfaceId, visible);
229 }
230
231 void LayoutHandler::timerEvent(QTimerEvent *e)
232 {
233     if (e->timerId() == m_secondsTimerId)
234     {
235         checkToDoQueue();
236     }
237 }
238