2
* Copyright 2008 Aike J Sommer <dev@aikesommer.name>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2,
7
* or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details
14
* You should have received a copy of the GNU Library General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "outputscreens.h"
29
OutputScreens::OutputScreens(QObject * parent)
31
m_rebuildTimer(new QTimer(this))
33
m_rebuildTimer->setSingleShot(true);
34
connect(m_rebuildTimer, SIGNAL(timeout()), this, SLOT(rebuildTimeout()));
38
QList<Screen *> OutputScreens::screens() {
39
QList<Screen *> result;
40
foreach(OutputScreen * screen, m_screens) {
41
result.append(screen);
46
int OutputScreens::findId() {
47
for (int i = 0; i < m_screens.size(); ++i) {
48
if (! m_screens.contains(i)) {
52
return m_screens.size();
55
void OutputScreens::init() {
58
connect(Outputs::self(), SIGNAL(outputResized(Kephal::Output *, QSize, QSize)), this, SLOT(outputResized(Kephal::Output *, QSize, QSize)));
59
connect(Outputs::self(), SIGNAL(outputMoved(Kephal::Output *, QPoint, QPoint)), this, SLOT(outputMoved(Kephal::Output *, QPoint, QPoint)));
60
connect(Outputs::self(), SIGNAL(outputActivated(Kephal::Output *)), this, SLOT(outputActivated(Kephal::Output *)));
61
connect(Outputs::self(), SIGNAL(outputDeactivated(Kephal::Output *)), this, SLOT(outputDeactivated(Kephal::Output *)));
64
void OutputScreens::outputActivated(Kephal::Output * o) {
67
triggerRebuildScreens();
70
void OutputScreens::outputDeactivated(Kephal::Output * o) {
73
triggerRebuildScreens();
76
void OutputScreens::outputMoved(Kephal::Output * o, QPoint oldPosition, QPoint newPosition) {
81
triggerRebuildScreens();
84
void OutputScreens::outputResized(Kephal::Output * o, QSize oldSize, QSize newSize) {
89
triggerRebuildScreens();
92
void OutputScreens::buildScreens() {
93
foreach (Output * output, Outputs::self()->outputs()) {
94
// for each connected and active output,
95
if (! output->isConnected() || ! output->isActivated()) {
99
// look for any screen which intersects this output, and add it to the screen
101
foreach (OutputScreen * screen, m_screens) {
102
if (screen->geom().intersects(output->geom())) {
108
// if the output did not intersect, add it to the first empty screen found
110
foreach (OutputScreen * screen, m_screens) {
111
if (screen->outputs().empty()) {
118
// if no empty screen found, generate one and add this output to it.
120
OutputScreen * screen = new OutputScreen(this);
121
screen->_setId(findId());
123
m_screens.insert(screen->id(), screen);
127
// remove any remaining empty screens
128
for (QMap<int, OutputScreen *>::iterator i = m_screens.begin(); i != m_screens.end();) {
129
if (i.value()->outputs().empty()) {
130
i = m_screens.erase(i);
136
// remove any intersecting Screens and add removed Screens' Outputs to the intersected Screen
140
for (QMap<int, OutputScreen *>::iterator i = m_screens.begin(); i != m_screens.end(); ++i) {
141
bool deleted = false;
142
for (QMap<int, OutputScreen *>::iterator j = i + 1; j != m_screens.end(); ++j) {
143
if (i.value()->geom().intersects(j.value()->geom())) {
144
OutputScreen * to = i.value();
145
OutputScreen * from = j.value();
147
foreach (Output * output, from->outputs()) {
164
// I think this tries to renumber m_screens from 0, removing any gaps due to removed
165
// intersecting screens
166
for (int i = 0; i < m_screens.size(); ++i) {
167
if (! m_screens.contains(i)) {
168
// get the lowest numbered screen in m_screens
170
for (QMap<int, OutputScreen *>::iterator it = m_screens.begin(); it != m_screens.end(); ++it) {
171
if ((min == -1) || (it.key() < min)) {
175
OutputScreen * screen = m_screens.take(min);
177
m_screens.insert(i, screen);
182
void OutputScreens::triggerRebuildScreens() {
184
m_rebuildTimer->start(200);
187
void OutputScreens::rebuildTimeout() {
191
void OutputScreens::rebuildScreens() {
194
QMap<int, QRect> geoms;
195
for (QMap<int, OutputScreen *>::const_iterator i = m_screens.constBegin(); i != m_screens.constEnd(); ++i) {
196
geoms.insert(i.key(), i.value()->geom());
197
i.value()->clearOutputs();
200
prepareScreens(m_screens);
203
for (QMap<int, OutputScreen *>::const_iterator i = m_screens.constBegin(); i != m_screens.constEnd(); ++i) {
204
if (! geoms.contains(i.key())) {
205
emit screenAdded(i.value());
206
kDebug() << "emitted screenAdded " << i.key();
207
} else if (geoms[i.key()] != i.value()->geom()) {
208
if (geoms[i.key()].topLeft() != i.value()->geom().topLeft()) {
209
emit screenMoved(i.value(), geoms[i.key()].topLeft(), i.value()->geom().topLeft());
210
kDebug() << "emitted screenMoved " << i.key() << " - old " << geoms[i.key()] << " - new " << i.value()->geom();
212
if (geoms[i.key()].size() != i.value()->geom().size()) {
213
emit screenResized(i.value(), geoms[i.key()].size(), i.value()->geom().size());
214
kDebug() << "emitted screenResized " << i.key() << " - old " << geoms[i.key()] << " - new " << i.value()->geom();
219
for (QMap<int, QRect>::const_iterator i = geoms.constBegin(); i != geoms.constEnd(); ++i) {
220
if (! m_screens.contains(i.key())) {
221
emit screenRemoved(i.key());
222
kDebug() << "emitted screenRemoved " << i.key();
227
void OutputScreens::prepareScreens(QMap<int, OutputScreen *> & screens) {
233
OutputScreen::OutputScreen(QObject * parent)
234
: SimpleScreen(parent)
238
void OutputScreen::add(Output * output) {
239
m_outputs.append(output);
241
QRect geom = this->geom();
242
if (geom.isEmpty()) {
243
geom = output->geom();
245
geom = geom.unite(output->geom());
248
_setSize(geom.size());
249
_setPosition(geom.topLeft());
252
QList<Output *> OutputScreen::outputs() {
256
void OutputScreen::remove(Output * output) {
257
m_outputs.removeAll(output);
260
void OutputScreen::clearOutputs() {
262
_setSize(QSize(0, 0));
263
_setPosition(QPoint(0, 0));
269
#include "outputscreens.moc"