Fix for pid and surface handling.
[staging/HomeScreen.git] / HomeScreen / src / layouthandler.cpp
1 #include "layouthandler.h"
2
3 LayoutHandler::LayoutHandler(QObject *parent) :
4     QObject(parent),
5     mp_dBusWindowManagerProxy(0),
6     mp_dBusPopupProxy(0),
7     m_visibleSurfaces(),
8     m_invisibleSurfaces(),
9     m_requestsToBeVisibleSurfaces()
10 {
11     qDBusRegisterMetaType<SimplePoint>();
12     qDBusRegisterMetaType<QList<SimplePoint> >();
13     qDBusRegisterMetaType<LayoutArea>();
14     qDBusRegisterMetaType<QList<LayoutArea> >();
15     qDBusRegisterMetaType<Layout>();
16     qDBusRegisterMetaType<QList<Layout> >();
17
18     qDebug("D-Bus: connect to org.agl.windowmanager /windowmanager");
19     mp_dBusWindowManagerProxy = new org::agl::windowmanager("org.agl.windowmanager",
20                                               "/windowmanager",
21                                               QDBusConnection::sessionBus(),
22                                               0);
23     qDebug("D-Bus: connect to org.agl.homescreen /Popup");
24     mp_dBusPopupProxy = new org::agl::popup("org.agl.homescreen",
25                                               "/Popup",
26                                               QDBusConnection::sessionBus(),
27                                               0);
28 }
29
30 LayoutHandler::~LayoutHandler()
31 {
32     delete mp_dBusPopupProxy;
33     delete mp_dBusWindowManagerProxy;
34 }
35
36 void LayoutHandler::setUpLayouts()
37 {
38     qDebug("setUpLayouts");
39     QList<LayoutArea> surfaceAreas;
40     LayoutArea surfaceArea;
41
42     const int SCREEN_WIDTH = 1080;
43     const int SCREEN_HEIGHT = 1920;
44
45     const int STATUSBAR_HEIGHT = 60;
46     const int STATUSBAR_WIDTH = SCREEN_WIDTH;
47     const int STATUSBAR_X = 0;
48     const int STATUSBAR_Y = 0;
49     const int CONTROLBAR_HEIGHT = 60;
50     const int CONTROLBAR_WIDTH = SCREEN_WIDTH;
51     const int CONTROLBAR_X = 0;
52     const int CONTROLBAR_Y = SCREEN_HEIGHT - CONTROLBAR_HEIGHT;
53
54
55     // layout 1:
56     // one app surface, statusbar, control bar
57     surfaceArea.x = 0;
58     surfaceArea.y = STATUSBAR_HEIGHT;
59     surfaceArea.width = SCREEN_WIDTH;
60     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
61
62     surfaceAreas.append(surfaceArea);
63
64     mp_dBusWindowManagerProxy->addLayout(1, "one app", surfaceAreas);
65
66
67     surfaceAreas.clear();
68
69     // layout 2:
70     // two app surfaces (one on top of the other), statusbar, control bar
71
72     // top surface
73     surfaceArea.x = 0;
74     surfaceArea.y = STATUSBAR_HEIGHT;
75     surfaceArea.width = SCREEN_WIDTH;
76     surfaceArea.height = (SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT) / 2;
77
78     surfaceAreas.append(surfaceArea);
79
80     // bottom surface
81     surfaceArea.x = 0;
82     surfaceArea.y = SCREEN_HEIGHT / 2;
83     surfaceArea.width = SCREEN_WIDTH;
84     surfaceArea.height = (SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT) / 2;
85
86     surfaceAreas.append(surfaceArea);
87
88     mp_dBusWindowManagerProxy->addLayout(2, "top on bottom", surfaceAreas);
89
90
91     surfaceAreas.clear();
92
93     // layout 3:
94     // two app surfaces (one besides the other), statusbar, control bar
95
96     // left surface
97     surfaceArea.x = 0;
98     surfaceArea.y = STATUSBAR_HEIGHT;
99     surfaceArea.width = SCREEN_WIDTH / 2;
100     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
101
102     surfaceAreas.append(surfaceArea);
103
104     // right surface
105     surfaceArea.x = SCREEN_WIDTH / 2;
106     surfaceArea.y = STATUSBAR_HEIGHT;
107     surfaceArea.width = SCREEN_WIDTH / 2;
108     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
109
110     surfaceAreas.append(surfaceArea);
111
112     mp_dBusWindowManagerProxy->addLayout(3, "side by side", surfaceAreas);
113 }
114
115 void LayoutHandler::makeMeVisible(int pid)
116 {
117     qDebug("makeMeVisible %d", pid);
118
119     QList<int> allSurfaces = mp_dBusWindowManagerProxy->getAllSurfacesOfProcess(pid);
120     qSort(allSurfaces);
121
122     if (0 != allSurfaces.size())
123     {
124         m_requestsToBeVisibleSurfaces.append(allSurfaces.at(0));
125
126         qDebug("m_visibleSurfaces %d", m_visibleSurfaces.size());
127         qDebug("m_invisibleSurfaces %d", m_invisibleSurfaces.size());
128         qDebug("m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
129
130         QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(m_visibleSurfaces.size() + m_requestsToBeVisibleSurfaces.size());
131         if (0 == availableLayouts.size())
132         {
133             // no layout fits the need!
134             // replace the last app
135             qDebug("no layout fits the need!");
136             qDebug("replace the last surface");
137
138             m_invisibleSurfaces.append(m_visibleSurfaces.last());
139             m_visibleSurfaces.removeLast();
140
141             m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
142             m_requestsToBeVisibleSurfaces.clear();
143
144             for (int i = 0; i < m_visibleSurfaces.size(); ++i)
145             {
146                 mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
147             }
148         }
149         if (1 == availableLayouts.size())
150         {
151             // switch to new layout
152             qDebug("switch to new layout %d", availableLayouts.at(0));
153             m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
154             m_requestsToBeVisibleSurfaces.clear();
155
156             mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
157             for (int i = 0; i < m_visibleSurfaces.size(); ++i)
158             {
159                 mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
160             }
161         }
162         if (1 < availableLayouts.size())
163         {
164             // more than one layout possible! Ask user.
165             qDebug("more than one layout possible! Ask user.");
166
167             QStringList choices;
168             for (int i = 0; i < availableLayouts.size(); ++i)
169             {
170                 choices.append(mp_dBusWindowManagerProxy->getLayoutName(availableLayouts.at(i)));
171             }
172
173             mp_dBusPopupProxy->showPopupComboBox("Select Layout", choices);
174         }
175     }
176 }
177
178 void LayoutHandler::requestRenderSurfaceToArea(int surfaceId, const QRect &renderArea)
179 {
180     qDebug("requestRenderSurfaceToArea %d %d,%d,%d,%d", surfaceId, renderArea.x(), renderArea.y(), renderArea.width(), renderArea.height());
181 }
182
183 void LayoutHandler::requestSurfaceIdToFullScreen(int surfaceId)
184 {
185     qDebug("requestSurfaceIdToFullScreen %d", surfaceId);
186 }
187
188 void LayoutHandler::setLayoutByName(QString layoutName)
189 {
190     // switch to new layout
191     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
192     m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
193     m_requestsToBeVisibleSurfaces.clear();
194
195     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
196     for (int i = 0; i < m_visibleSurfaces.size(); ++i)
197     {
198         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
199     }
200 }