New layer management in WindowManager. Three layers are created. One for the HomeScre...
[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 surfaceId)
116 {
117     qDebug("makeMeVisible %d", surfaceId);
118     m_requestsToBeVisibleSurfaces.append(surfaceId);
119
120     qDebug("m_visibleSurfaces %d", m_visibleSurfaces.size());
121     qDebug("m_invisibleSurfaces %d", m_invisibleSurfaces.size());
122     qDebug("m_requestsToBeVisibleSurfaces %d", m_requestsToBeVisibleSurfaces.size());
123
124     QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(m_visibleSurfaces.size() + m_requestsToBeVisibleSurfaces.size());
125     if (0 == availableLayouts.size())
126     {
127         // no layout fits the need!
128         // replace the last app
129         qDebug("no layout fits the need!");
130         qDebug("replace the last surface");
131
132         m_invisibleSurfaces.append(m_visibleSurfaces.last());
133         m_visibleSurfaces.removeLast();
134
135         m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
136         m_requestsToBeVisibleSurfaces.clear();
137
138         for (int i = 0; i < m_visibleSurfaces.size(); ++i)
139         {
140             mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
141         }
142     }
143     if (1 == availableLayouts.size())
144     {
145         // switch to new layout
146         qDebug("switch to new layout %d", availableLayouts.at(0));
147         m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
148         m_requestsToBeVisibleSurfaces.clear();
149
150         mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
151         for (int i = 0; i < m_visibleSurfaces.size(); ++i)
152         {
153             mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(m_visibleSurfaces.at(i), i);
154         }
155     }
156     if (1 < availableLayouts.size())
157     {
158         // more than one layout possible! Ask user.
159         qDebug("more than one layout possible! Ask user.");
160
161         QStringList choices;
162         for (int i = 0; i < availableLayouts.size(); ++i)
163         {
164             choices.append(mp_dBusWindowManagerProxy->getLayoutName(availableLayouts.at(i)));
165         }
166
167         mp_dBusPopupProxy->showPopupComboBox("Select Layout", choices);
168
169     }
170 }
171
172 void LayoutHandler::toggleFullscreen()
173 {
174     qDebug("toggleFullscreen");
175 }
176
177 void LayoutHandler::setLayoutByName(QString layoutName)
178 {
179     // switch to new layout
180     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
181     m_visibleSurfaces.append(m_requestsToBeVisibleSurfaces);
182     m_requestsToBeVisibleSurfaces.clear();
183
184     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
185     for (int i = 0; i < m_visibleSurfaces.size(); ++i)
186     {
187         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
188     }
189 }