~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/tiling.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2009 Nikhil Marathe <nsm.nikhil@gmail.com>
 
6
 
 
7
This program is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
This program is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
*********************************************************************/
 
20
 
 
21
// all the tiling related code that is extensions to existing KWin classes
 
22
// Includes Workspace for now
 
23
 
 
24
#include "client.h"
 
25
#include "workspace.h"
 
26
#include "tile.h"
 
27
#include "tilinglayout.h"
 
28
#include "tilinglayoutfactory.h"
 
29
 
 
30
#include <knotification.h>
 
31
#include <klocale.h>
 
32
#include <kwindowinfo.h>
 
33
#include <kwindowsystem.h>
 
34
 
 
35
 
 
36
namespace KWin
 
37
{
 
38
 
 
39
bool Workspace::tilingEnabled() const
 
40
{
 
41
    return tilingEnabled_;
 
42
}
 
43
 
 
44
void Workspace::setTilingEnabled(bool tiling)
 
45
{
 
46
    if (tilingEnabled() == tiling) return;
 
47
 
 
48
    tilingEnabled_ = tiling;
 
49
 
 
50
    KSharedConfig::Ptr _config = KGlobal::config();
 
51
    KConfigGroup config(_config, "Windows");
 
52
    config.writeEntry("TilingOn", tilingEnabled_);
 
53
    config.sync();
 
54
    options->tilingOn = tilingEnabled_;
 
55
    options->tilingLayout = static_cast<TilingLayoutFactory::Layouts>(config.readEntry("TilingDefaultLayout", 0));
 
56
    options->tilingRaisePolicy = config.readEntry("TilingRaisePolicy", 0);
 
57
 
 
58
    if (tilingEnabled_) {
 
59
        tilingLayouts.resize(numberOfDesktops() + 1);
 
60
        foreach (Client * c, stackingOrder()) {
 
61
            createTile(c);
 
62
        }
 
63
    } else {
 
64
        qDeleteAll(tilingLayouts);
 
65
        tilingLayouts.clear();
 
66
    }
 
67
}
 
68
 
 
69
void Workspace::slotToggleTiling()
 
70
{
 
71
    if (tilingEnabled()) {
 
72
        setTilingEnabled(false);
 
73
        QString message = i18n("Tiling Disabled");
 
74
        KNotification::event("tilingdisabled", message, QPixmap(), NULL, KNotification::CloseOnTimeout, KComponentData("kwin"));
 
75
    } else {
 
76
        setTilingEnabled(true);
 
77
        QString message = i18n("Tiling Enabled");
 
78
        KNotification::event("tilingenabled", message, QPixmap(), NULL, KNotification::CloseOnTimeout, KComponentData("kwin"));
 
79
    }
 
80
}
 
81
 
 
82
void Workspace::createTile(Client *c)
 
83
{
 
84
    if (c == NULL)
 
85
        return;
 
86
 
 
87
    if (c->desktop() < 0 || c->desktop() >= tilingLayouts.size()) return;
 
88
 
 
89
    kDebug(1212) << "Now tiling " << c->caption();
 
90
    if (!tilingEnabled() || !tileable(c))
 
91
        return;
 
92
 
 
93
    Tile *t = new Tile(c, clientArea(PlacementArea, c));
 
94
    if (!tileable(c)) {
 
95
        kDebug(1212) << c->caption() << "is not tileable";
 
96
        t->floatTile();
 
97
    }
 
98
 
 
99
    if (!tilingLayouts.value(c->desktop())) {
 
100
        tilingLayouts[c->desktop()] = TilingLayoutFactory::createLayout(TilingLayoutFactory::DefaultLayout, this);
 
101
    }
 
102
    tilingLayouts[c->desktop()]->addTile(t);
 
103
    tilingLayouts[c->desktop()]->commit();
 
104
}
 
105
 
 
106
void Workspace::removeTile(Client *c)
 
107
{
 
108
    if (tilingLayouts[ c->desktop()])
 
109
        tilingLayouts[ c->desktop()]->removeTile(c);
 
110
}
 
111
 
 
112
bool Workspace::tileable(Client *c)
 
113
{
 
114
 
 
115
    kDebug(1212) << c->caption();
 
116
    KWindowInfo info = KWindowSystem::windowInfo(c->window(), -1U, NET::WM2WindowClass);
 
117
    kDebug(1212) << "WINDOW CLASS IS " << info.windowClassClass();
 
118
    if (info.windowClassClass() == "Plasma-desktop") {
 
119
        return false;
 
120
    }
 
121
    // TODO: if application specific settings
 
122
    // to ignore, put them here
 
123
 
 
124
    if (!c->isNormalWindow()) {
 
125
        return false;
 
126
    }
 
127
 
 
128
    // 0 means tile it, if we get 1 (floating), don't tile
 
129
    if (c->rules()->checkTilingOption(0) == 1) {
 
130
        return false;
 
131
    }
 
132
 
 
133
    kDebug() << "Tiling" << c;
 
134
    return true;
 
135
}
 
136
 
 
137
void Workspace::belowCursor()
 
138
{
 
139
    // TODO
 
140
}
 
141
 
 
142
Tile* Workspace::getNiceTile() const
 
143
{
 
144
    if (!tilingEnabled()) return NULL;
 
145
    if (!tilingLayouts.value(activeClient()->desktop())) return NULL;
 
146
 
 
147
    return tilingLayouts[ activeClient()->desktop()]->findTile(activeClient());
 
148
    // TODO
 
149
}
 
150
 
 
151
void Workspace::updateAllTiles()
 
152
{
 
153
    foreach (TilingLayout * t, tilingLayouts) {
 
154
        if (!t) continue;
 
155
        t->commit();
 
156
    }
 
157
}
 
158
 
 
159
/*
 
160
 * Resize the neighbouring clients to close any gaps
 
161
 */
 
162
void Workspace::notifyTilingWindowResize(Client *c, const QRect &moveResizeGeom, const QRect &orig)
 
163
{
 
164
    if (tilingLayouts.value(c->desktop()) == NULL)
 
165
        return;
 
166
    tilingLayouts[ c->desktop()]->clientResized(c, moveResizeGeom, orig);
 
167
}
 
168
 
 
169
void Workspace::notifyTilingWindowMove(Client *c, const QRect &moveResizeGeom, const QRect &orig)
 
170
{
 
171
    if (tilingLayouts.value(c->desktop()) == NULL) {
 
172
        return;
 
173
    }
 
174
    tilingLayouts[ c->desktop()]->clientMoved(c, moveResizeGeom, orig);
 
175
    updateAllTiles();
 
176
}
 
177
 
 
178
void Workspace::notifyTilingWindowResizeDone(Client *c, const QRect &moveResizeGeom, const QRect &orig, bool canceled)
 
179
{
 
180
    if (canceled)
 
181
        notifyTilingWindowResize(c, orig, moveResizeGeom);
 
182
    else
 
183
        notifyTilingWindowResize(c, moveResizeGeom, orig);
 
184
}
 
185
 
 
186
void Workspace::notifyTilingWindowMoveDone(Client *c, const QRect &moveResizeGeom, const QRect &orig, bool canceled)
 
187
{
 
188
    if (canceled)
 
189
        notifyTilingWindowMove(c, orig, moveResizeGeom);
 
190
    else
 
191
        notifyTilingWindowMove(c, moveResizeGeom, orig);
 
192
}
 
193
 
 
194
void Workspace::notifyTilingWindowDesktopChanged(Client *c, int old_desktop)
 
195
{
 
196
    if (c->desktop() < 1 || c->desktop() > numberOfDesktops())
 
197
        return;
 
198
 
 
199
    if (tilingLayouts.value(old_desktop)) {
 
200
        Tile *t = tilingLayouts[ old_desktop ]->findTile(c);
 
201
 
 
202
        // TODO: copied from createTile(), move this into separate method?
 
203
        if (!tilingLayouts.value(c->desktop())) {
 
204
            tilingLayouts[c->desktop()] = TilingLayoutFactory::createLayout(TilingLayoutFactory::DefaultLayout, this);
 
205
        }
 
206
 
 
207
        if (t)
 
208
            tilingLayouts[ c->desktop()]->addTile(t);
 
209
 
 
210
        tilingLayouts[ old_desktop ]->removeTile(c);
 
211
        tilingLayouts[ old_desktop ]->commit();
 
212
    }
 
213
}
 
214
 
 
215
/*
 
216
 * Implements the 3 raising modes in Window Behaviour -> Advanced
 
217
 */
 
218
void Workspace::notifyTilingWindowActivated(Client *c)
 
219
{
 
220
    if (c == NULL)
 
221
        return;
 
222
 
 
223
    if (options->tilingRaisePolicy == 1)   // individual raise/lowers
 
224
        return;
 
225
 
 
226
    if (tilingLayouts.value(c->desktop())) {
 
227
        QList<Tile *> tiles = tilingLayouts[ c->desktop()]->tiles();
 
228
 
 
229
        StackingUpdatesBlocker blocker(this);
 
230
 
 
231
        Tile *tile_to_raise = tilingLayouts[ c->desktop()]->findTile(c);
 
232
 
 
233
        if (!tile_to_raise) {
 
234
            return;
 
235
        }
 
236
 
 
237
        kDebug(1212) << "FOUND TILE";
 
238
        bool raise_floating = false;
 
239
        if (options->tilingRaisePolicy == 2)   // floating always on top
 
240
            raise_floating = true;
 
241
        else
 
242
            raise_floating = tile_to_raise->floating();
 
243
 
 
244
        foreach (Tile * t, tiles) {
 
245
            if (t->floating() == raise_floating && t != tile_to_raise)
 
246
                raiseClient(t->client());
 
247
        }
 
248
        // raise the current tile last so that it ends up on top
 
249
        // but only if it supposed to be raised, required to support tilingRaisePolicy
 
250
        kDebug(1212) << "Raise floating? " << raise_floating << "to raise is floating?" << tile_to_raise->floating();
 
251
        if (tile_to_raise->floating() == raise_floating)
 
252
            raiseClient(tile_to_raise->client());
 
253
    }
 
254
}
 
255
 
 
256
void Workspace::notifyTilingWindowMinimizeToggled(Client *c)
 
257
{
 
258
    if (tilingLayouts.value(c->desktop())) {
 
259
        tilingLayouts[ c->desktop()]->clientMinimizeToggled(c);
 
260
    }
 
261
}
 
262
 
 
263
void Workspace::notifyTilingWindowMaximized(Client *c, Options::WindowOperation op)
 
264
{
 
265
    if (tilingLayouts.value(c->desktop())) {
 
266
        Tile *t = tilingLayouts[ c->desktop()]->findTile(c);
 
267
        if (!t) {
 
268
            createTile(c);
 
269
            t = tilingLayouts[ c->desktop()]->findTile(c);
 
270
 
 
271
            // if still no tile, it couldn't be tiled
 
272
            // so ignore it
 
273
            if (!t)
 
274
                return;
 
275
        }
 
276
 
 
277
        // if window IS tiled and a maximize
 
278
        // is attempted, make the window float.
 
279
        // That is all we do since that can
 
280
        // mess up the layout.
 
281
        // In all other cases, don't do
 
282
        // anything, let the user manage toggling
 
283
        // using Meta+F
 
284
        if (!t->floating()
 
285
                && (op == Options::MaximizeOp
 
286
                    || op == Options::HMaximizeOp
 
287
                    || op == Options::VMaximizeOp)) {
 
288
            tilingLayouts[ c->desktop()]->toggleFloatTile(c);
 
289
        }
 
290
 
 
291
    }
 
292
}
 
293
 
 
294
Tile* Workspace::findAdjacentTile(Tile *ref, int d)
 
295
{
 
296
    QRect reference = ref->geometry();
 
297
    QPoint origin = reference.center();
 
298
 
 
299
    Tile *closest = NULL;
 
300
    int minDist = -1;
 
301
 
 
302
    QList<Tile *> tiles = tilingLayouts[ ref->client()->desktop()]->tiles();
 
303
 
 
304
    foreach (Tile * t, tiles) {
 
305
        if (t->client() == ref->client() || t->ignoreGeometry())
 
306
            continue;
 
307
 
 
308
        bool consider = false;
 
309
 
 
310
        QRect other = t->geometry();
 
311
        QPoint otherCenter = other.center();
 
312
 
 
313
        switch(d) {
 
314
        case Tile::Top:
 
315
            consider = otherCenter.y() < origin.y()
 
316
                       && other.bottom() < reference.top();
 
317
            break;
 
318
 
 
319
        case Tile::Right:
 
320
            consider = otherCenter.x() > origin.x()
 
321
                       && other.left() > reference.right();
 
322
            break;
 
323
 
 
324
        case Tile::Bottom:
 
325
            consider = otherCenter.y() > origin.y()
 
326
                       && other.top() > reference.bottom();
 
327
            break;
 
328
 
 
329
        case Tile::Left:
 
330
            consider = otherCenter.x() < origin.x()
 
331
                       && other.right() < reference.left();
 
332
            break;
 
333
 
 
334
        default:
 
335
            abort();
 
336
        }
 
337
 
 
338
        if (consider) {
 
339
            int dist = (otherCenter - origin).manhattanLength();
 
340
            if (minDist > dist || minDist < 0) {
 
341
                minDist = dist;
 
342
                closest = t;
 
343
            }
 
344
        }
 
345
    }
 
346
    return closest;
 
347
}
 
348
 
 
349
void Workspace::focusTile(int d)
 
350
{
 
351
    Tile *t = getNiceTile();
 
352
    if (t) {
 
353
        Tile *adj = findAdjacentTile(t, d);
 
354
        if (adj)
 
355
            activateClient(adj->client());
 
356
    }
 
357
}
 
358
 
 
359
void Workspace::moveTile(int d)
 
360
{
 
361
    Tile *t = getNiceTile();
 
362
    if (t) {
 
363
        Tile* adj = findAdjacentTile(t, d);
 
364
 
 
365
        tilingLayouts[ t->client()->desktop()]->swapTiles(t, adj);
 
366
    }
 
367
}
 
368
 
 
369
void Workspace::slotFocusTileLeft()
 
370
{
 
371
    focusTile(Tile::Left);
 
372
}
 
373
 
 
374
void Workspace::slotFocusTileRight()
 
375
{
 
376
    focusTile(Tile::Right);
 
377
}
 
378
 
 
379
void Workspace::slotFocusTileTop()
 
380
{
 
381
    focusTile(Tile::Top);
 
382
}
 
383
 
 
384
void Workspace::slotFocusTileBottom()
 
385
{
 
386
    focusTile(Tile::Bottom);
 
387
}
 
388
 
 
389
void Workspace::slotMoveTileLeft()
 
390
{
 
391
    moveTile(Tile::Left);
 
392
}
 
393
 
 
394
void Workspace::slotMoveTileRight()
 
395
{
 
396
    moveTile(Tile::Right);
 
397
}
 
398
 
 
399
void Workspace::slotMoveTileTop()
 
400
{
 
401
    moveTile(Tile::Top);
 
402
}
 
403
 
 
404
void Workspace::slotMoveTileBottom()
 
405
{
 
406
    moveTile(Tile::Bottom);
 
407
}
 
408
 
 
409
void Workspace::slotToggleFloating()
 
410
{
 
411
    Client *c = activeClient();
 
412
    if (tilingLayouts.value(c->desktop())) {
 
413
        tilingLayouts[ c->desktop()]->toggleFloatTile(c);
 
414
    }
 
415
}
 
416
 
 
417
void Workspace::slotNextTileLayout()
 
418
{
 
419
    if (tilingLayouts.value(currentDesktop())) {
 
420
 
 
421
        tilingLayouts.replace(currentDesktop(), TilingLayoutFactory::nextLayout(tilingLayouts[currentDesktop()]));
 
422
 
 
423
        tilingLayouts[currentDesktop()]->commit();
 
424
    }
 
425
}
 
426
 
 
427
void Workspace::slotPreviousTileLayout()
 
428
{
 
429
    if (tilingLayouts.value(currentDesktop())) {
 
430
 
 
431
        tilingLayouts.replace(currentDesktop(), TilingLayoutFactory::previousLayout(tilingLayouts[currentDesktop()]));
 
432
 
 
433
        tilingLayouts[currentDesktop()]->commit();
 
434
    }
 
435
}
 
436
 
 
437
KDecorationDefines::Position Workspace::supportedTilingResizeMode(Client *c, KDecorationDefines::Position currentMode)
 
438
{
 
439
    if (tilingLayouts.value(c->desktop())) {
 
440
        return tilingLayouts[c->desktop()]->resizeMode(c, currentMode);
 
441
    }
 
442
    return currentMode;
 
443
}
 
444
 
 
445
void Workspace::dumpTiles() const
 
446
{
 
447
    foreach (TilingLayout * t, tilingLayouts) {
 
448
        if (!t) {
 
449
            kDebug(1212) << "EMPTY DESKTOP";
 
450
            continue;
 
451
        }
 
452
        kDebug(1212) << "Desktop" << tilingLayouts.indexOf(t);
 
453
        foreach (Tile * tile, t->tiles()) {
 
454
            tile->dumpTile("--");
 
455
        }
 
456
    }
 
457
}
 
458
} // namespace
 
459