Implemented proximity interface.
[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
40     const int SCREEN_WIDTH = 1080;
41     const int SCREEN_HEIGHT = 1080;
42
43     const int STATUSBAR_HEIGHT = 60;
44     const int STATUSBAR_WIDTH = SCREEN_WIDTH;
45     const int STATUSBAR_X = 0;
46     const int STATUSBAR_Y = 0;
47     const int CONTROLBAR_HEIGHT = 60;
48     const int CONTROLBAR_WIDTH = SCREEN_WIDTH;
49     const int CONTROLBAR_X = 0;
50     const int CONTROLBAR_Y = SCREEN_HEIGHT - CONTROLBAR_HEIGHT;
51
52
53     // layout 1:
54     // one app surface, statusbar, control bar
55     surfaceArea.x = 0;
56     surfaceArea.y = STATUSBAR_HEIGHT;
57     surfaceArea.width = SCREEN_WIDTH;
58     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
59
60     surfaceAreas.append(surfaceArea);
61
62     mp_dBusWindowManagerProxy->addLayout(1, "one app", surfaceAreas);
63
64
65     surfaceAreas.clear();
66
67     // layout 2:
68     // two app surfaces (one on top of the other), statusbar, control bar
69
70     // top surface
71     surfaceArea.x = 0;
72     surfaceArea.y = STATUSBAR_HEIGHT;
73     surfaceArea.width = SCREEN_WIDTH;
74     surfaceArea.height = (SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT) / 2;
75
76     surfaceAreas.append(surfaceArea);
77
78     // bottom surface
79     surfaceArea.x = 0;
80     surfaceArea.y = STATUSBAR_HEIGHT / 2;
81     surfaceArea.width = SCREEN_WIDTH;
82     surfaceArea.height = (SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT) / 2;
83
84     surfaceAreas.append(surfaceArea);
85
86     mp_dBusWindowManagerProxy->addLayout(2, "top on bottom", surfaceAreas);
87
88
89     surfaceAreas.clear();
90
91     // layout 3:
92     // two app surfaces (one besides the other), statusbar, control bar
93
94     // left surface
95     surfaceArea.x = 0;
96     surfaceArea.y = STATUSBAR_HEIGHT;
97     surfaceArea.width = SCREEN_WIDTH / 2;
98     surfaceArea.height = SCREEN_HEIGHT - STATUSBAR_HEIGHT - CONTROLBAR_HEIGHT;
99
100     surfaceAreas.append(surfaceArea);
101
102     // right surface
103     surfaceArea.x = SCREEN_WIDTH / 2;
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     mp_dBusWindowManagerProxy->addLayout(3, "side by side", surfaceAreas);
111
112 }
113
114 void LayoutHandler::makeMeVisible(int pid)
115 {
116     qDebug("makeMeVisible %d", pid);
117     m_requestsToBeVisibleApps.append(pid);
118
119     qDebug("m_visibleApps %d", m_visibleApps.size());
120     qDebug("m_invisibleApps %d", m_invisibleApps.size());
121     qDebug("m_requestsToBeVisibleApps %d", m_requestsToBeVisibleApps.size());
122
123     QList<int> availableLayouts = mp_dBusWindowManagerProxy->getAvailableLayouts(m_visibleApps.size() + m_requestsToBeVisibleApps.size());
124     if (0 == availableLayouts.size())
125     {
126         // no layout fits the need!
127         // replace the last app
128         qDebug("no layout fits the need!");
129         qDebug("replace the last app");
130
131         m_invisibleApps.append(m_visibleApps.last());
132         m_visibleApps.removeLast();
133
134         for (int i = 0; i < m_visibleApps.size(); ++i)
135         {
136             mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
137         }
138     }
139     if (1 == availableLayouts.size())
140     {
141         // switch to new layout
142         qDebug("switch to new layout %d", availableLayouts.at(0));
143         m_visibleApps.append(m_requestsToBeVisibleApps);
144         m_requestsToBeVisibleApps.clear();
145
146         mp_dBusWindowManagerProxy->setLayoutById(availableLayouts.at(0));
147         for (int i = 0; i < m_visibleApps.size(); ++i)
148         {
149             mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
150         }
151     }
152     if (1 < availableLayouts.size())
153     {
154         // more than one layout possible! Ask user.
155         qDebug("more than one layout possible! Ask user.");
156
157         QStringList choices;
158         for (int i = 0; i < availableLayouts.size(); ++i)
159         {
160             choices.append(mp_dBusWindowManagerProxy->getLayoutName(availableLayouts.at(i)));
161         }
162
163         mp_dBusPopupProxy->showPopupComboBox("Select Layout", choices);
164
165     }
166 }
167
168 void LayoutHandler::setLayoutByName(QString layoutName)
169 {
170     // switch to new layout
171     qDebug("setLayout: switch to new layout %s", layoutName.toStdString().c_str());
172     m_visibleApps.append(m_requestsToBeVisibleApps);
173     m_requestsToBeVisibleApps.clear();
174
175     mp_dBusWindowManagerProxy->setLayoutByName(layoutName);
176     for (int i = 0; i < m_visibleApps.size(); ++i)
177     {
178         mp_dBusWindowManagerProxy->setSurfaceToLayoutArea(i, i);
179     }
180 }