81bc1063809a3f9322b537764909df38a870e6f0
[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_visibleApps(),
8     m_invisibleApps(),
9     m_requestsToBeVisibleApps()
10 {
11     qDBusRegisterMetaType<SimplePoint>();
12     qDBusRegisterMetaType<QList<SimplePoint> >();
13     qDBusRegisterMetaType<SimpleRect>();
14     qDBusRegisterMetaType<QList<SimpleRect> >();
15
16     qDebug("D-Bus: connect to org.agl.windowmanager /windowmanager");
17     mp_dBusWindowManagerProxy = new org::agl::windowmanager("org.agl.windowmanager",
18                                               "/windowmanager",
19                                               QDBusConnection::sessionBus(),
20                                               0);
21     qDebug("D-Bus: connect to org.agl.homescreen /Popup");
22     mp_dBusPopupProxy = new org::agl::popup("org.agl.homescreen",
23                                               "/Popup",
24                                               QDBusConnection::sessionBus(),
25                                               0);
26 }
27
28 LayoutHandler::~LayoutHandler()
29 {
30     delete mp_dBusPopupProxy;
31     delete mp_dBusWindowManagerProxy;
32 }
33
34 void LayoutHandler::setUpLayouts()
35 {
36     qDebug("setUpLayouts");
37     QList<SimpleRect> surfaceAreas;
38     SimpleRect surfaceArea;
39     bool isFullScreen;
40     int associatedFullScreenLayout;
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     isFullScreen = false;
65     associatedFullScreenLayout = 4;
66
67     mp_dBusWindowManagerProxy->addLayout(1, "one app", isFullScreen, associatedFullScreenLayout, 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     isFullScreen = false;
92     associatedFullScreenLayout = -1;
93
94     mp_dBusWindowManagerProxy->addLayout(2, "top on bottom", isFullScreen, associatedFullScreenLayout, surfaceAreas);
95
96
97     surfaceAreas.clear();
98
99     // layout 3:
100     // two app surfaces (one besides the other), statusbar, control bar
101
102     // left surface
103     surfaceArea.x = 0;
104     surfaceArea.y = STATUSBAR_HEIGHT;
105     surfaceArea.width = SCREEN_WIDTH / 2;
106     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
107
108     surfaceAreas.append(surfaceArea);
109
110     // right surface
111     surfaceArea.x = SCREEN_WIDTH / 2;
112     surfaceArea.y = STATUSBAR_HEIGHT;
113     surfaceArea.width = SCREEN_WIDTH / 2;
114     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
115
116     surfaceAreas.append(surfaceArea);
117
118     isFullScreen = false;
119     associatedFullScreenLayout = -1;
120
121     mp_dBusWindowManagerProxy->addLayout(3, "side by side", isFullScreen, associatedFullScreenLayout, surfaceAreas);
122
123
124     surfaceAreas.clear();
125
126     // layout 4:
127     // one app surface full screen, no statusbar, no control bar
128     surfaceArea.x = 0;
129     surfaceArea.y = 0;
130     surfaceArea.width = SCREEN_WIDTH;
131     surfaceArea.height = SCREEN_HEIGHT;
132
133     surfaceAreas.append(surfaceArea);
134
135     isFullScreen = true;
136     associatedFullScreenLayout = 1;
137
138     mp_dBusWindowManagerProxy->addLayout(4, "one app full screen", isFullScreen, associatedFullScreenLayout, surfaceAreas);
139
140
141     surfaceAreas.clear();
142
143 }
144
145 void LayoutHandler::makeMeVisible(int pid)
146 {
147     qDebug("makeMeVisible %d", pid);
148     m_requestsToBeVisibleApps.append(pid);
149
150     qDebug("m_visibleApps %d", m_visibleApps.size());
151     qDebug("m_invisibleApps %d", m_invisibleApps.size());
152     qDebug("m_requestsToBeVisibleApps %d", m_requestsToBeVisibleApps.size());
153
154     QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(m_visibleApps.size() + m_requestsToBeVisibleApps.size());
155     if (0 == availableLayouts.size())
156     {
157         // no layout fits the need!
158         // replace the last app
159         qDebug("no layout fits the need!");
160         qDebug("replace the last app");
161
162         m_invisibleApps.append(m_visibleApps.last());
163         m_visibleApps.removeLast();
164
165         m_visibleApps.append(m_requestsToBeVisibleApps);
166         m_requestsToBeVisibleApps.clear();
167
168         for (int i = 0; i < m_visibleApps.size(); ++i)
169         {
170             mp_dBusWindowManagerProxy->setPidToLayoutArea(m_visibleApps.at(i), i);
171         }
172     }
173     if (1 == availableLayouts.size())
174     {
175         // switch to new layout
176         qDebug("switch to new layout %d", availableLayouts.at(0));
177         m_visibleApps.append(m_requestsToBeVisibleApps);
178         m_requestsToBeVisibleApps.clear();
179
180         mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
181         for (int i = 0; i < m_visibleApps.size(); ++i)
182         {
183             mp_dBusWindowManagerProxy->setPidToLayoutArea(m_visibleApps.at(i), i);
184         }
185     }
186     if (1 < availableLayouts.size())
187     {
188         // more than one layout possible! Ask user.
189         qDebug("more than one layout possible! Ask user.");
190
191         QStringList choices;
192         for (int i = 0; i < availableLayouts.size(); ++i)
193         {
194             choices.append(mp_dBusWindowManagerProxy->getLayoutName(availableLayouts.at(i)));
195         }
196
197         mp_dBusPopupProxy->showPopupComboBox("Select Layout", choices);
198
199     }
200 }
201
202 void LayoutHandler::toggleFullscreen()
203 {
204     qDebug("toggleFullscreen");
205     int currentLayout = mp_dBusWindowManagerProxy->getLayout();
206     int associatedFullScreenLayout = mp_dBusWindowManagerProxy->getAssociatedFullScreenLayout(currentLayout);
207     if (-1 != associatedFullScreenLayout)
208     {
209         mp_dBusWindowManagerProxy->setLayoutById(associatedFullScreenLayout);
210         for (int i = 0; i < m_visibleApps.size(); ++i)
211         {
212             mp_dBusWindowManagerProxy->setPidToLayoutArea(m_visibleApps.at(i), i);
213         }
214     }
215     else
216     {
217         qDebug("no associatedFullScreenLayout. Cannot switch to full screen.");
218     }
219 }
220
221 void LayoutHandler::setLayoutByName(QString layoutName)
222 {
223     // switch to new layout
224     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
225     m_visibleApps.append(m_requestsToBeVisibleApps);
226     m_requestsToBeVisibleApps.clear();
227
228     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
229     for (int i = 0; i < m_visibleApps.size(); ++i)
230     {
231         mp_dBusWindowManagerProxy->setPidToLayoutArea(i, i);
232     }
233 }