1 /*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 1999, 2000 Matthias Ettrich <ettrich@kde.org>
6 SPDX-FileCopyrightText: 2003 Lubos Lunak <l.lunak@kde.org>
7 SPDX-FileCopyrightText: 2009 Martin Gräßlin <mgraesslin@kde.org>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 //#define QT_CLEAN_NAMESPACE
13 // own
14 #include "tabbox.h"
15 // tabbox
16 #include "tabbox/clientmodel.h"
17 #include "tabbox/desktopmodel.h"
18 #include "tabbox/tabboxconfig.h"
19 #include "tabbox/desktopchain.h"
20 #include "tabbox/tabbox_logging.h"
21 #include "tabbox/x11_filter.h"
22 // kwin
23 #ifdef KWIN_BUILD_ACTIVITIES
24 #include "activities.h"
25 #endif
26 #include "composite.h"
27 #include "x11client.h"
28 #include "effects.h"
29 #include "input.h"
30 #include "keyboard_input.h"
31 #include "pointer_input.h"
32 #include "focuschain.h"
33 #include "platform.h"
34 #include "screenedge.h"
35 #include "screens.h"
36 #include "unmanaged.h"
37 #include "virtualdesktops.h"
38 #include "workspace.h"
39 #include "xcbutils.h"
40 // Qt
41 #include <QAction>
42 #include <QKeyEvent>
43 // KDE
44 #include <KConfig>
45 #include <KConfigGroup>
46 #include <KGlobalAccel>
47 #include <KLocalizedString>
48 #include <kkeyserver.h>
49 // X11
50 #include <X11/keysym.h>
51 #include <X11/keysymdef.h>
52 // xcb
53 #include <xcb/xcb_keysyms.h>
54
55 // specify externals before namespace
56
57 namespace KWin
58 {
59
60 namespace TabBox
61 {
62
TabBoxHandlerImpl(TabBox * tabBox)63 TabBoxHandlerImpl::TabBoxHandlerImpl(TabBox* tabBox)
64 : TabBoxHandler(tabBox)
65 , m_tabBox(tabBox)
66 , m_desktopFocusChain(new DesktopChainManager(this))
67 {
68 // connects for DesktopFocusChainManager
69 VirtualDesktopManager *vds = VirtualDesktopManager::self();
70 connect(vds, &VirtualDesktopManager::countChanged, m_desktopFocusChain, &DesktopChainManager::resize);
71 connect(vds, &VirtualDesktopManager::currentChanged, m_desktopFocusChain, &DesktopChainManager::addDesktop);
72 #ifdef KWIN_BUILD_ACTIVITIES
73 if (Activities::self()) {
74 connect(Activities::self(), &Activities::currentChanged, m_desktopFocusChain, &DesktopChainManager::useChain);
75 }
76 #endif
77 }
78
~TabBoxHandlerImpl()79 TabBoxHandlerImpl::~TabBoxHandlerImpl()
80 {
81 }
82
activeScreen() const83 int TabBoxHandlerImpl::activeScreen() const
84 {
85 return kwinApp()->platform()->enabledOutputs().indexOf(workspace()->activeOutput());
86 }
87
currentDesktop() const88 int TabBoxHandlerImpl::currentDesktop() const
89 {
90 return VirtualDesktopManager::self()->current();
91 }
92
desktopName(TabBoxClient * client) const93 QString TabBoxHandlerImpl::desktopName(TabBoxClient* client) const
94 {
95 if (TabBoxClientImpl* c = static_cast< TabBoxClientImpl* >(client)) {
96 if (!c->client()->isOnAllDesktops())
97 return desktopName(c->client()->desktop());
98 }
99 return desktopName(VirtualDesktopManager::self()->current());
100 }
101
desktopName(int desktop) const102 QString TabBoxHandlerImpl::desktopName(int desktop) const
103 {
104 const VirtualDesktop *vd = VirtualDesktopManager::self()->desktopForX11Id(desktop);
105 return vd ? vd->name() : QString();
106 }
107
nextClientFocusChain(TabBoxClient * client) const108 QWeakPointer<TabBoxClient> TabBoxHandlerImpl::nextClientFocusChain(TabBoxClient* client) const
109 {
110 if (TabBoxClientImpl* c = static_cast< TabBoxClientImpl* >(client)) {
111 auto next = FocusChain::self()->nextMostRecentlyUsed(c->client());
112 if (next)
113 return next->tabBoxClient();
114 }
115 return QWeakPointer<TabBoxClient>();
116 }
117
firstClientFocusChain() const118 QWeakPointer< TabBoxClient > TabBoxHandlerImpl::firstClientFocusChain() const
119 {
120 if (auto c = FocusChain::self()->firstMostRecentlyUsed()) {
121 return QWeakPointer<TabBoxClient>(c->tabBoxClient());
122 } else {
123 return QWeakPointer<TabBoxClient>();
124 }
125 }
126
isInFocusChain(TabBoxClient * client) const127 bool TabBoxHandlerImpl::isInFocusChain(TabBoxClient *client) const
128 {
129 if (TabBoxClientImpl *c = static_cast<TabBoxClientImpl*>(client)) {
130 return FocusChain::self()->contains(c->client());
131 }
132 return false;
133 }
134
nextDesktopFocusChain(int desktop) const135 int TabBoxHandlerImpl::nextDesktopFocusChain(int desktop) const
136 {
137 return m_desktopFocusChain->next(desktop);
138 }
139
numberOfDesktops() const140 int TabBoxHandlerImpl::numberOfDesktops() const
141 {
142 return VirtualDesktopManager::self()->count();
143 }
144
activeClient() const145 QWeakPointer<TabBoxClient> TabBoxHandlerImpl::activeClient() const
146 {
147 if (Workspace::self()->activeClient())
148 return Workspace::self()->activeClient()->tabBoxClient();
149 else
150 return QWeakPointer<TabBoxClient>();
151 }
152
checkDesktop(TabBoxClient * client,int desktop) const153 bool TabBoxHandlerImpl::checkDesktop(TabBoxClient* client, int desktop) const
154 {
155 auto current = (static_cast< TabBoxClientImpl* >(client))->client();
156
157 switch (config().clientDesktopMode()) {
158 case TabBoxConfig::AllDesktopsClients:
159 return true;
160 case TabBoxConfig::ExcludeCurrentDesktopClients:
161 return !current->isOnDesktop(desktop);
162 default: // TabBoxConfig::OnlyCurrentDesktopClients
163 return current->isOnDesktop(desktop);
164 }
165 }
166
checkActivity(TabBoxClient * client) const167 bool TabBoxHandlerImpl::checkActivity(TabBoxClient* client) const
168 {
169 auto current = (static_cast< TabBoxClientImpl* >(client))->client();
170
171 switch (config().clientActivitiesMode()) {
172 case TabBoxConfig::AllActivitiesClients:
173 return true;
174 case TabBoxConfig::ExcludeCurrentActivityClients:
175 return !current->isOnCurrentActivity();
176 default: // TabBoxConfig::OnlyCurrentActivityClients
177 return current->isOnCurrentActivity();
178 }
179 }
180
checkApplications(TabBoxClient * client) const181 bool TabBoxHandlerImpl::checkApplications(TabBoxClient* client) const
182 {
183 auto current = (static_cast< TabBoxClientImpl* >(client))->client();
184 TabBoxClientImpl* c;
185 QListIterator< QWeakPointer<TabBoxClient> > i(clientList());
186
187 switch (config().clientApplicationsMode()) {
188 case TabBoxConfig::OneWindowPerApplication:
189 // check if the list already contains an entry of this application
190 while (i.hasNext()) {
191 QSharedPointer<TabBoxClient> client = i.next().toStrongRef();
192 if (!client) {
193 continue;
194 }
195 if ((c = dynamic_cast< TabBoxClientImpl* >(client.data()))) {
196 if (AbstractClient::belongToSameApplication(c->client(), current, AbstractClient::SameApplicationCheck::AllowCrossProcesses)) {
197 return false;
198 }
199 }
200 }
201 return true;
202 case TabBoxConfig::AllWindowsCurrentApplication: {
203 QSharedPointer<TabBoxClient> pointer = tabBox->activeClient().toStrongRef();
204 if (!pointer) {
205 return false;
206 }
207 if ((c = dynamic_cast< TabBoxClientImpl* >(pointer.data()))) {
208 if (AbstractClient::belongToSameApplication(c->client(), current, AbstractClient::SameApplicationCheck::AllowCrossProcesses)) {
209 return true;
210 }
211 }
212 return false;
213 }
214 default: // TabBoxConfig::AllWindowsAllApplications
215 return true;
216 }
217 }
218
checkMinimized(TabBoxClient * client) const219 bool TabBoxHandlerImpl::checkMinimized(TabBoxClient* client) const
220 {
221 switch (config().clientMinimizedMode()) {
222 case TabBoxConfig::ExcludeMinimizedClients:
223 return !client->isMinimized();
224 case TabBoxConfig::OnlyMinimizedClients:
225 return client->isMinimized();
226 default: // TabBoxConfig::IgnoreMinimizedStatus
227 return true;
228 }
229 }
230
checkMultiScreen(TabBoxClient * client) const231 bool TabBoxHandlerImpl::checkMultiScreen(TabBoxClient* client) const
232 {
233 auto current = (static_cast< TabBoxClientImpl* >(client))->client();
234
235 switch (config().clientMultiScreenMode()) {
236 case TabBoxConfig::IgnoreMultiScreen:
237 return true;
238 case TabBoxConfig::ExcludeCurrentScreenClients:
239 return current->output() != workspace()->activeOutput();
240 default: // TabBoxConfig::OnlyCurrentScreenClients
241 return current->output() == workspace()->activeOutput();
242 }
243 }
244
clientToAddToList(TabBoxClient * client,int desktop) const245 QWeakPointer<TabBoxClient> TabBoxHandlerImpl::clientToAddToList(TabBoxClient* client, int desktop) const
246 {
247 if (!client) {
248 return QWeakPointer<TabBoxClient>();
249 }
250 AbstractClient* ret = nullptr;
251 AbstractClient* current = (static_cast< TabBoxClientImpl* >(client))->client();
252
253 bool addClient = checkDesktop(client, desktop)
254 && checkActivity(client)
255 && checkApplications(client)
256 && checkMinimized(client)
257 && checkMultiScreen(client);
258 addClient = addClient && current->wantsTabFocus() && !current->skipSwitcher();
259 if (addClient) {
260 // don't add windows that have modal dialogs
261 AbstractClient* modal = current->findModal();
262 if (modal == nullptr || modal == current)
263 ret = current;
264 else if (!clientList().contains(modal->tabBoxClient()))
265 ret = modal;
266 else {
267 // nothing
268 }
269 }
270 if (ret)
271 return ret->tabBoxClient();
272 else
273 return QWeakPointer<TabBoxClient>();
274 }
275
stackingOrder() const276 TabBoxClientList TabBoxHandlerImpl::stackingOrder() const
277 {
278 QList<Toplevel *> stacking = Workspace::self()->stackingOrder();
279 TabBoxClientList ret;
280 Q_FOREACH (Toplevel *toplevel, stacking) {
281 if (auto client = qobject_cast<AbstractClient*>(toplevel)) {
282 ret.append(client->tabBoxClient());
283 }
284 }
285 return ret;
286 }
287
isKWinCompositing() const288 bool TabBoxHandlerImpl::isKWinCompositing() const
289 {
290 return Compositor::compositing();
291 }
292
raiseClient(TabBoxClient * c) const293 void TabBoxHandlerImpl::raiseClient(TabBoxClient* c) const
294 {
295 Workspace::self()->raiseClient(static_cast<TabBoxClientImpl*>(c)->client());
296 }
297
restack(TabBoxClient * c,TabBoxClient * under)298 void TabBoxHandlerImpl::restack(TabBoxClient *c, TabBoxClient *under)
299 {
300 Workspace::self()->restack(static_cast<TabBoxClientImpl*>(c)->client(),
301 static_cast<TabBoxClientImpl*>(under)->client(), true);
302 }
303
elevateClient(TabBoxClient * c,QWindow * tabbox,bool b) const304 void TabBoxHandlerImpl::elevateClient(TabBoxClient *c, QWindow *tabbox, bool b) const
305 {
306 auto cl = static_cast<TabBoxClientImpl*>(c)->client();
307 cl->elevate(b);
308 if (Toplevel *w = Workspace::self()->findInternal(tabbox))
309 w->elevate(b);
310 }
311
shadeClient(TabBoxClient * c,bool b) const312 void TabBoxHandlerImpl::shadeClient(TabBoxClient *c, bool b) const
313 {
314 AbstractClient *client = static_cast<TabBoxClientImpl *>(c)->client();
315 client->cancelShadeHoverTimer(); // stop core shading action
316 if (!b && client->shadeMode() == ShadeNormal)
317 client->setShade(ShadeHover);
318 else if (b && client->shadeMode() == ShadeHover)
319 client->setShade(ShadeNormal);
320 }
321
desktopClient() const322 QWeakPointer<TabBoxClient> TabBoxHandlerImpl::desktopClient() const
323 {
324 Q_FOREACH (Toplevel *toplevel, Workspace::self()->stackingOrder()) {
325 auto client = qobject_cast<AbstractClient*>(toplevel);
326 if (client && client->isDesktop() && client->isOnCurrentDesktop() && client->output() == workspace()->activeOutput()) {
327 return client->tabBoxClient();
328 }
329 }
330 return QWeakPointer<TabBoxClient>();
331 }
332
activateAndClose()333 void TabBoxHandlerImpl::activateAndClose()
334 {
335 m_tabBox->accept();
336 }
337
highlightWindows(TabBoxClient * window,QWindow * controller)338 void TabBoxHandlerImpl::highlightWindows(TabBoxClient *window, QWindow *controller)
339 {
340 if (!effects) {
341 return;
342 }
343 QVector<EffectWindow*> windows;
344 if (window) {
345 windows << static_cast<TabBoxClientImpl*>(window)->client()->effectWindow();
346 }
347 if (Toplevel *t = workspace()->findInternal(controller)) {
348 windows << t->effectWindow();
349 }
350 static_cast<EffectsHandlerImpl*>(effects)->highlightWindows(windows);
351 }
352
noModifierGrab() const353 bool TabBoxHandlerImpl::noModifierGrab() const
354 {
355 return m_tabBox->noModifierGrab();
356 }
357
358 /*********************************************************
359 * TabBoxClientImpl
360 *********************************************************/
361
TabBoxClientImpl(AbstractClient * client)362 TabBoxClientImpl::TabBoxClientImpl(AbstractClient *client)
363 : TabBoxClient()
364 , m_client(client)
365 {
366 }
367
~TabBoxClientImpl()368 TabBoxClientImpl::~TabBoxClientImpl()
369 {
370 }
371
caption() const372 QString TabBoxClientImpl::caption() const
373 {
374 if (m_client->isDesktop())
375 return i18nc("Special entry in alt+tab list for minimizing all windows",
376 "Show Desktop");
377 return m_client->caption();
378 }
379
icon() const380 QIcon TabBoxClientImpl::icon() const
381 {
382 if (m_client->isDesktop()) {
383 return QIcon::fromTheme(QStringLiteral("user-desktop"));
384 }
385 return m_client->icon();
386 }
387
isMinimized() const388 bool TabBoxClientImpl::isMinimized() const
389 {
390 return m_client->isMinimized();
391 }
392
x() const393 int TabBoxClientImpl::x() const
394 {
395 return m_client->x();
396 }
397
y() const398 int TabBoxClientImpl::y() const
399 {
400 return m_client->y();
401 }
402
width() const403 int TabBoxClientImpl::width() const
404 {
405 return m_client->width();
406 }
407
height() const408 int TabBoxClientImpl::height() const
409 {
410 return m_client->height();
411 }
412
isCloseable() const413 bool TabBoxClientImpl::isCloseable() const
414 {
415 return m_client->isCloseable();
416 }
417
close()418 void TabBoxClientImpl::close()
419 {
420 m_client->closeWindow();
421 }
422
isFirstInTabBox() const423 bool TabBoxClientImpl::isFirstInTabBox() const
424 {
425 return m_client->isFirstInTabBox();
426 }
427
internalId() const428 QUuid TabBoxClientImpl::internalId() const
429 {
430 return m_client->internalId();
431 }
432
433 /*********************************************************
434 * TabBox
435 *********************************************************/
436 TabBox *TabBox::s_self = nullptr;
437
create(QObject * parent)438 TabBox *TabBox::create(QObject *parent)
439 {
440 Q_ASSERT(!s_self);
441 s_self = new TabBox(parent);
442 return s_self;
443 }
444
TabBox(QObject * parent)445 TabBox::TabBox(QObject *parent)
446 : QObject(parent)
447 , m_displayRefcount(0)
448 , m_desktopGrab(false)
449 , m_tabGrab(false)
450 , m_noModifierGrab(false)
451 , m_forcedGlobalMouseGrab(false)
452 , m_ready(false)
453 {
454 m_isShown = false;
455 m_defaultConfig = TabBoxConfig();
456 m_defaultConfig.setTabBoxMode(TabBoxConfig::ClientTabBox);
457 m_defaultConfig.setClientDesktopMode(TabBoxConfig::OnlyCurrentDesktopClients);
458 m_defaultConfig.setClientActivitiesMode(TabBoxConfig::OnlyCurrentActivityClients);
459 m_defaultConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsAllApplications);
460 m_defaultConfig.setClientMinimizedMode(TabBoxConfig::IgnoreMinimizedStatus);
461 m_defaultConfig.setShowDesktopMode(TabBoxConfig::DoNotShowDesktopClient);
462 m_defaultConfig.setClientMultiScreenMode(TabBoxConfig::IgnoreMultiScreen);
463 m_defaultConfig.setClientSwitchingMode(TabBoxConfig::FocusChainSwitching);
464
465 m_alternativeConfig = TabBoxConfig();
466 m_alternativeConfig.setTabBoxMode(TabBoxConfig::ClientTabBox);
467 m_alternativeConfig.setClientDesktopMode(TabBoxConfig::AllDesktopsClients);
468 m_alternativeConfig.setClientActivitiesMode(TabBoxConfig::OnlyCurrentActivityClients);
469 m_alternativeConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsAllApplications);
470 m_alternativeConfig.setClientMinimizedMode(TabBoxConfig::IgnoreMinimizedStatus);
471 m_alternativeConfig.setShowDesktopMode(TabBoxConfig::DoNotShowDesktopClient);
472 m_alternativeConfig.setClientMultiScreenMode(TabBoxConfig::IgnoreMultiScreen);
473 m_alternativeConfig.setClientSwitchingMode(TabBoxConfig::FocusChainSwitching);
474
475 m_defaultCurrentApplicationConfig = m_defaultConfig;
476 m_defaultCurrentApplicationConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsCurrentApplication);
477
478 m_alternativeCurrentApplicationConfig = m_alternativeConfig;
479 m_alternativeCurrentApplicationConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsCurrentApplication);
480
481 m_desktopConfig = TabBoxConfig();
482 m_desktopConfig.setTabBoxMode(TabBoxConfig::DesktopTabBox);
483 m_desktopConfig.setShowTabBox(true);
484 m_desktopConfig.setShowDesktopMode(TabBoxConfig::DoNotShowDesktopClient);
485 m_desktopConfig.setDesktopSwitchingMode(TabBoxConfig::MostRecentlyUsedDesktopSwitching);
486
487 m_desktopListConfig = TabBoxConfig();
488 m_desktopListConfig.setTabBoxMode(TabBoxConfig::DesktopTabBox);
489 m_desktopListConfig.setShowTabBox(true);
490 m_desktopListConfig.setShowDesktopMode(TabBoxConfig::DoNotShowDesktopClient);
491 m_desktopListConfig.setDesktopSwitchingMode(TabBoxConfig::StaticDesktopSwitching);
492 m_tabBox = new TabBoxHandlerImpl(this);
493 QTimer::singleShot(0, this, &TabBox::handlerReady);
494
495 m_tabBoxMode = TabBoxDesktopMode; // init variables
496 connect(&m_delayedShowTimer, &QTimer::timeout, this, &TabBox::show);
497 connect(Workspace::self(), &Workspace::configChanged, this, &TabBox::reconfigure);
498 }
499
~TabBox()500 TabBox::~TabBox()
501 {
502 s_self = nullptr;
503 }
504
handlerReady()505 void TabBox::handlerReady()
506 {
507 m_tabBox->setConfig(m_defaultConfig);
508 reconfigure();
509 m_ready = true;
510 }
511
512 template <typename Slot>
key(const char * actionName,Slot slot,const QKeySequence & shortcut)513 void TabBox::key(const char *actionName, Slot slot, const QKeySequence &shortcut)
514 {
515 QAction *a = new QAction(this);
516 a->setProperty("componentName", QStringLiteral(KWIN_NAME));
517 a->setObjectName(QString::fromUtf8(actionName));
518 a->setText(i18n(actionName));
519 KGlobalAccel::self()->setGlobalShortcut(a, QList<QKeySequence>() << shortcut);
520 input()->registerShortcut(shortcut, a, TabBox::self(), slot);
521 auto cuts = KGlobalAccel::self()->shortcut(a);
522 globalShortcutChanged(a, cuts.isEmpty() ? QKeySequence() : cuts.first());
523 }
524
525 static const char s_windows[] = I18N_NOOP("Walk Through Windows");
526 static const char s_windowsRev[] = I18N_NOOP("Walk Through Windows (Reverse)");
527 static const char s_windowsAlt[] = I18N_NOOP("Walk Through Windows Alternative");
528 static const char s_windowsAltRev[] = I18N_NOOP("Walk Through Windows Alternative (Reverse)");
529 static const char s_app[] = I18N_NOOP("Walk Through Windows of Current Application");
530 static const char s_appRev[] = I18N_NOOP("Walk Through Windows of Current Application (Reverse)");
531 static const char s_appAlt[] = I18N_NOOP("Walk Through Windows of Current Application Alternative");
532 static const char s_appAltRev[] = I18N_NOOP("Walk Through Windows of Current Application Alternative (Reverse)");
533 static const char s_desktops[] = I18N_NOOP("Walk Through Desktops");
534 static const char s_desktopsRev[] = I18N_NOOP("Walk Through Desktops (Reverse)");
535 static const char s_desktopList[] = I18N_NOOP("Walk Through Desktop List");
536 static const char s_desktopListRev[] = I18N_NOOP("Walk Through Desktop List (Reverse)");
537
initShortcuts()538 void TabBox::initShortcuts()
539 {
540 key(s_windows, &TabBox::slotWalkThroughWindows, Qt::ALT + Qt::Key_Tab);
541 key(s_windowsRev, &TabBox::slotWalkBackThroughWindows, Qt::ALT + Qt::SHIFT + Qt::Key_Backtab);
542 key(s_app, &TabBox::slotWalkThroughCurrentAppWindows, Qt::ALT + Qt::Key_QuoteLeft);
543 key(s_appRev, &TabBox::slotWalkBackThroughCurrentAppWindows, Qt::ALT + Qt::Key_AsciiTilde);
544 key(s_windowsAlt, &TabBox::slotWalkThroughWindowsAlternative);
545 key(s_windowsAltRev, &TabBox::slotWalkBackThroughWindowsAlternative);
546 key(s_appAlt, &TabBox::slotWalkThroughCurrentAppWindowsAlternative);
547 key(s_appAltRev, &TabBox::slotWalkBackThroughCurrentAppWindowsAlternative);
548 key(s_desktops, &TabBox::slotWalkThroughDesktops);
549 key(s_desktopsRev, &TabBox::slotWalkBackThroughDesktops);
550 key(s_desktopList, &TabBox::slotWalkThroughDesktopList);
551 key(s_desktopListRev, &TabBox::slotWalkBackThroughDesktopList);
552
553 connect(KGlobalAccel::self(), &KGlobalAccel::globalShortcutChanged, this, &TabBox::globalShortcutChanged);
554 }
555
globalShortcutChanged(QAction * action,const QKeySequence & seq)556 void TabBox::globalShortcutChanged(QAction *action, const QKeySequence &seq)
557 {
558 if (qstrcmp(qPrintable(action->objectName()), s_windows) == 0) {
559 m_cutWalkThroughWindows = seq;
560 } else if (qstrcmp(qPrintable(action->objectName()), s_windowsRev) == 0) {
561 m_cutWalkThroughWindowsReverse = seq;
562 } else if (qstrcmp(qPrintable(action->objectName()), s_app) == 0) {
563 m_cutWalkThroughCurrentAppWindows = seq;
564 } else if (qstrcmp(qPrintable(action->objectName()), s_appRev) == 0) {
565 m_cutWalkThroughCurrentAppWindowsReverse = seq;
566 } else if (qstrcmp(qPrintable(action->objectName()), s_windowsAlt) == 0) {
567 m_cutWalkThroughWindowsAlternative = seq;
568 } else if (qstrcmp(qPrintable(action->objectName()), s_windowsAltRev) == 0) {
569 m_cutWalkThroughWindowsAlternativeReverse = seq;
570 } else if (qstrcmp(qPrintable(action->objectName()), s_appAlt) == 0) {
571 m_cutWalkThroughCurrentAppWindowsAlternative = seq;
572 } else if (qstrcmp(qPrintable(action->objectName()), s_appAltRev) == 0) {
573 m_cutWalkThroughCurrentAppWindowsAlternativeReverse = seq;
574 } else if (qstrcmp(qPrintable(action->objectName()), s_desktops) == 0) {
575 m_cutWalkThroughDesktops = seq;
576 } else if (qstrcmp(qPrintable(action->objectName()), s_desktopsRev) == 0) {
577 m_cutWalkThroughDesktopsReverse = seq;
578 } else if (qstrcmp(qPrintable(action->objectName()), s_desktopList) == 0) {
579 m_cutWalkThroughDesktopList = seq;
580 } else if (qstrcmp(qPrintable(action->objectName()), s_desktopListRev) == 0) {
581 m_cutWalkThroughDesktopListReverse = seq;
582 }
583 }
584
setMode(TabBoxMode mode)585 void TabBox::setMode(TabBoxMode mode)
586 {
587 m_tabBoxMode = mode;
588 switch(mode) {
589 case TabBoxWindowsMode:
590 m_tabBox->setConfig(m_defaultConfig);
591 break;
592 case TabBoxWindowsAlternativeMode:
593 m_tabBox->setConfig(m_alternativeConfig);
594 break;
595 case TabBoxCurrentAppWindowsMode:
596 m_tabBox->setConfig(m_defaultCurrentApplicationConfig);
597 break;
598 case TabBoxCurrentAppWindowsAlternativeMode:
599 m_tabBox->setConfig(m_alternativeCurrentApplicationConfig);
600 break;
601 case TabBoxDesktopMode:
602 m_tabBox->setConfig(m_desktopConfig);
603 break;
604 case TabBoxDesktopListMode:
605 m_tabBox->setConfig(m_desktopListConfig);
606 break;
607 }
608 }
609
reset(bool partial_reset)610 void TabBox::reset(bool partial_reset)
611 {
612 switch(m_tabBox->config().tabBoxMode()) {
613 case TabBoxConfig::ClientTabBox:
614 m_tabBox->createModel(partial_reset);
615 if (!partial_reset) {
616 if (Workspace::self()->activeClient())
617 setCurrentClient(Workspace::self()->activeClient());
618 // it's possible that the active client is not part of the model
619 // in that case the index is invalid
620 if (!m_tabBox->currentIndex().isValid())
621 setCurrentIndex(m_tabBox->first());
622 } else {
623 if (!m_tabBox->currentIndex().isValid() || !m_tabBox->client(m_tabBox->currentIndex()))
624 setCurrentIndex(m_tabBox->first());
625 }
626 break;
627 case TabBoxConfig::DesktopTabBox:
628 m_tabBox->createModel();
629
630 if (!partial_reset)
631 setCurrentDesktop(VirtualDesktopManager::self()->current());
632 break;
633 }
634
635 Q_EMIT tabBoxUpdated();
636 }
637
nextPrev(bool next)638 void TabBox::nextPrev(bool next)
639 {
640 setCurrentIndex(m_tabBox->nextPrev(next), false);
641 Q_EMIT tabBoxUpdated();
642 }
643
currentClient()644 AbstractClient* TabBox::currentClient()
645 {
646 if (TabBoxClientImpl* client = static_cast< TabBoxClientImpl* >(m_tabBox->client(m_tabBox->currentIndex()))) {
647 if (!Workspace::self()->hasClient(client->client()))
648 return nullptr;
649 return client->client();
650 } else
651 return nullptr;
652 }
653
currentClientList()654 QList<AbstractClient*> TabBox::currentClientList()
655 {
656 TabBoxClientList list = m_tabBox->clientList();
657 QList<AbstractClient*> ret;
658 Q_FOREACH (const QWeakPointer<TabBoxClient> &clientPointer, list) {
659 QSharedPointer<TabBoxClient> client = clientPointer.toStrongRef();
660 if (!client)
661 continue;
662 if (const TabBoxClientImpl* c = static_cast< const TabBoxClientImpl* >(client.data()))
663 ret.append(c->client());
664 }
665 return ret;
666 }
667
currentDesktop()668 int TabBox::currentDesktop()
669 {
670 return m_tabBox->desktop(m_tabBox->currentIndex());
671 }
672
currentDesktopList()673 QList< int > TabBox::currentDesktopList()
674 {
675 return m_tabBox->desktopList();
676 }
677
setCurrentClient(AbstractClient * newClient)678 void TabBox::setCurrentClient(AbstractClient *newClient)
679 {
680 setCurrentIndex(m_tabBox->index(newClient->tabBoxClient()));
681 }
682
setCurrentDesktop(int newDesktop)683 void TabBox::setCurrentDesktop(int newDesktop)
684 {
685 setCurrentIndex(m_tabBox->desktopIndex(newDesktop));
686 }
687
setCurrentIndex(QModelIndex index,bool notifyEffects)688 void TabBox::setCurrentIndex(QModelIndex index, bool notifyEffects)
689 {
690 if (!index.isValid())
691 return;
692 m_tabBox->setCurrentIndex(index);
693 if (notifyEffects) {
694 Q_EMIT tabBoxUpdated();
695 }
696 }
697
show()698 void TabBox::show()
699 {
700 Q_EMIT tabBoxAdded(m_tabBoxMode);
701 if (isDisplayed()) {
702 m_isShown = false;
703 return;
704 }
705 workspace()->setShowingDesktop(false);
706 reference();
707 m_isShown = true;
708 m_tabBox->show();
709 }
710
hide(bool abort)711 void TabBox::hide(bool abort)
712 {
713 m_delayedShowTimer.stop();
714 if (m_isShown) {
715 m_isShown = false;
716 unreference();
717 }
718 Q_EMIT tabBoxClosed();
719 if (isDisplayed())
720 qCDebug(KWIN_TABBOX) << "Tab box was not properly closed by an effect";
721 m_tabBox->hide(abort);
722 if (kwinApp()->x11Connection()) {
723 Xcb::sync();
724 }
725 }
726
reconfigure()727 void TabBox::reconfigure()
728 {
729 KSharedConfigPtr c = kwinApp()->config();
730 KConfigGroup config = c->group("TabBox");
731
732 loadConfig(c->group("TabBox"), m_defaultConfig);
733 loadConfig(c->group("TabBoxAlternative"), m_alternativeConfig);
734
735 m_defaultCurrentApplicationConfig = m_defaultConfig;
736 m_defaultCurrentApplicationConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsCurrentApplication);
737 m_alternativeCurrentApplicationConfig = m_alternativeConfig;
738 m_alternativeCurrentApplicationConfig.setClientApplicationsMode(TabBoxConfig::AllWindowsCurrentApplication);
739
740 m_tabBox->setConfig(m_defaultConfig);
741
742 m_delayShow = config.readEntry<bool>("ShowDelay", true);
743 m_delayShowTime = config.readEntry<int>("DelayTime", 90);
744
745 const QString defaultDesktopLayout = QStringLiteral("org.kde.breeze.desktop");
746 m_desktopConfig.setLayoutName(config.readEntry("DesktopLayout", defaultDesktopLayout));
747 m_desktopListConfig.setLayoutName(config.readEntry("DesktopListLayout", defaultDesktopLayout));
748
749 QList<ElectricBorder> *borders = &m_borderActivate;
750 QString borderConfig = QStringLiteral("BorderActivate");
751 for (int i = 0; i < 2; ++i) {
752 Q_FOREACH (ElectricBorder border, *borders) {
753 ScreenEdges::self()->unreserve(border, this);
754 }
755 borders->clear();
756 QStringList list = config.readEntry(borderConfig, QStringList());
757 Q_FOREACH (const QString &s, list) {
758 bool ok;
759 const int i = s.toInt(&ok);
760 if (!ok)
761 continue;
762 borders->append(ElectricBorder(i));
763 ScreenEdges::self()->reserve(ElectricBorder(i), this, "toggle");
764 }
765 borders = &m_borderAlternativeActivate;
766 borderConfig = QStringLiteral("BorderAlternativeActivate");
767 }
768
769 auto touchConfig = [this, config] (const QString &key, QHash<ElectricBorder, QAction *> &actions, TabBoxMode mode, const QStringList &defaults = QStringList{}) {
770 // fist erase old config
771 for (auto it = actions.begin(); it != actions.end(); ) {
772 delete it.value();
773 it = actions.erase(it);
774 }
775 // now new config
776 const QStringList list = config.readEntry(key, defaults);
777 for (const auto &s : list) {
778 bool ok;
779 const int i = s.toInt(&ok);
780 if (!ok) {
781 continue;
782 }
783 QAction *a = new QAction(this);
784 connect(a, &QAction::triggered, this, std::bind(&TabBox::toggleMode, this, mode));
785 ScreenEdges::self()->reserveTouch(ElectricBorder(i), a);
786 actions.insert(ElectricBorder(i), a);
787 }
788 };
789 touchConfig(QStringLiteral("TouchBorderActivate"), m_touchActivate, TabBoxWindowsMode);
790 touchConfig(QStringLiteral("TouchBorderAlternativeActivate"), m_touchAlternativeActivate, TabBoxWindowsAlternativeMode);
791 }
792
loadConfig(const KConfigGroup & config,TabBoxConfig & tabBoxConfig)793 void TabBox::loadConfig(const KConfigGroup& config, TabBoxConfig& tabBoxConfig)
794 {
795 tabBoxConfig.setClientDesktopMode(TabBoxConfig::ClientDesktopMode(
796 config.readEntry<int>("DesktopMode", TabBoxConfig::defaultDesktopMode())));
797 tabBoxConfig.setClientActivitiesMode(TabBoxConfig::ClientActivitiesMode(
798 config.readEntry<int>("ActivitiesMode", TabBoxConfig::defaultActivitiesMode())));
799 tabBoxConfig.setClientApplicationsMode(TabBoxConfig::ClientApplicationsMode(
800 config.readEntry<int>("ApplicationsMode", TabBoxConfig::defaultApplicationsMode())));
801 tabBoxConfig.setClientMinimizedMode(TabBoxConfig::ClientMinimizedMode(
802 config.readEntry<int>("MinimizedMode", TabBoxConfig::defaultMinimizedMode())));
803 tabBoxConfig.setShowDesktopMode(TabBoxConfig::ShowDesktopMode(
804 config.readEntry<int>("ShowDesktopMode", TabBoxConfig::defaultShowDesktopMode())));
805 tabBoxConfig.setClientMultiScreenMode(TabBoxConfig::ClientMultiScreenMode(
806 config.readEntry<int>("MultiScreenMode", TabBoxConfig::defaultMultiScreenMode())));
807 tabBoxConfig.setClientSwitchingMode(TabBoxConfig::ClientSwitchingMode(
808 config.readEntry<int>("SwitchingMode", TabBoxConfig::defaultSwitchingMode())));
809
810 tabBoxConfig.setShowTabBox(config.readEntry<bool>("ShowTabBox",
811 TabBoxConfig::defaultShowTabBox()));
812 tabBoxConfig.setHighlightWindows(config.readEntry<bool>("HighlightWindows",
813 TabBoxConfig::defaultHighlightWindow()));
814
815 tabBoxConfig.setLayoutName(config.readEntry<QString>("LayoutName", TabBoxConfig::defaultLayoutName()));
816 }
817
delayedShow()818 void TabBox::delayedShow()
819 {
820 if (isDisplayed() || m_delayedShowTimer.isActive())
821 // already called show - no need to call it twice
822 return;
823
824 if (!m_delayShowTime) {
825 show();
826 return;
827 }
828
829 m_delayedShowTimer.setSingleShot(true);
830 m_delayedShowTimer.start(m_delayShowTime);
831 }
832
handleMouseEvent(QMouseEvent * event)833 bool TabBox::handleMouseEvent(QMouseEvent *event)
834 {
835 if (!m_isShown && isDisplayed()) {
836 // tabbox has been replaced, check effects
837 if (effects && static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowEvent(event)) {
838 return true;
839 }
840 }
841 switch (event->type()) {
842 case QEvent::MouseMove:
843 if (!m_tabBox->containsPos(event->globalPos())) {
844 // filter out all events which are not on the TabBox window.
845 // We don't want windows to react on the mouse events
846 return true;
847 }
848 return false;
849 case QEvent::MouseButtonPress:
850 if ((!m_isShown && isDisplayed()) || !m_tabBox->containsPos(event->globalPos())) {
851 close(); // click outside closes tab
852 return true;
853 }
854 // fall through
855 case QEvent::MouseButtonRelease:
856 default:
857 // we do not filter it out, the intenal filter takes care
858 return false;
859 }
860 return false;
861 }
862
handleWheelEvent(QWheelEvent * event)863 bool TabBox::handleWheelEvent(QWheelEvent *event)
864 {
865 if (!m_isShown && isDisplayed()) {
866 // tabbox has been replaced, check effects
867 if (effects && static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowEvent(event)) {
868 return true;
869 }
870 }
871 if (event->angleDelta().y() == 0) {
872 return false;
873 }
874 const QModelIndex index = m_tabBox->nextPrev(event->angleDelta().y() > 0);
875 if (index.isValid()) {
876 setCurrentIndex(index);
877 }
878 return true;
879 }
880
grabbedKeyEvent(QKeyEvent * event)881 void TabBox::grabbedKeyEvent(QKeyEvent* event)
882 {
883 Q_EMIT tabBoxKeyEvent(event);
884 if (!m_isShown && isDisplayed()) {
885 // tabbox has been replaced, check effects
886 return;
887 }
888 if (m_noModifierGrab) {
889 if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return || event->key() == Qt::Key_Space) {
890 accept();
891 return;
892 }
893 }
894 m_tabBox->grabbedKeyEvent(event);
895 }
896
897 struct KeySymbolsDeleter
898 {
cleanupKWin::TabBox::KeySymbolsDeleter899 static inline void cleanup(xcb_key_symbols_t *symbols)
900 {
901 xcb_key_symbols_free(symbols);
902 }
903 };
904
905 /**
906 * Handles alt-tab / control-tab
907 */
areKeySymXsDepressed(const uint keySyms[],int nKeySyms)908 static bool areKeySymXsDepressed(const uint keySyms[], int nKeySyms)
909 {
910 Xcb::QueryKeymap keys;
911
912 QScopedPointer<xcb_key_symbols_t, KeySymbolsDeleter> symbols(xcb_key_symbols_alloc(connection()));
913 if (symbols.isNull() || !keys) {
914 return false;
915 }
916 const auto keymap = keys->keys;
917
918 bool depressed = false;
919 for (int iKeySym = 0; iKeySym < nKeySyms; iKeySym++) {
920 uint keySymX = keySyms[ iKeySym ];
921 xcb_keycode_t *keyCodes = xcb_key_symbols_get_keycode(symbols.data(), keySymX);
922 if (!keyCodes) {
923 continue;
924 }
925
926 int j = 0;
927 while (keyCodes[j] != XCB_NO_SYMBOL) {
928 const xcb_keycode_t keyCodeX = keyCodes[j++];
929 int i = keyCodeX / 8;
930 char mask = 1 << (keyCodeX - (i * 8));
931
932 if (i < 0 || i >= 32) {
933 continue;
934 }
935
936 qCDebug(KWIN_TABBOX) << iKeySym << ": keySymX=0x" << QString::number(keySymX, 16)
937 << " i=" << i << " mask=0x" << QString::number(mask, 16)
938 << " keymap[i]=0x" << QString::number(keymap[i], 16);
939
940 if (keymap[i] & mask) {
941 depressed = true;
942 break;
943 }
944 }
945
946 free(keyCodes);
947 }
948
949 return depressed;
950 }
951
areModKeysDepressedX11(const QKeySequence & seq)952 static bool areModKeysDepressedX11(const QKeySequence &seq)
953 {
954 uint rgKeySyms[10];
955 int nKeySyms = 0;
956 int mod = seq[seq.count()-1] & Qt::KeyboardModifierMask;
957
958 if (mod & Qt::SHIFT) {
959 rgKeySyms[nKeySyms++] = XK_Shift_L;
960 rgKeySyms[nKeySyms++] = XK_Shift_R;
961 }
962 if (mod & Qt::CTRL) {
963 rgKeySyms[nKeySyms++] = XK_Control_L;
964 rgKeySyms[nKeySyms++] = XK_Control_R;
965 }
966 if (mod & Qt::ALT) {
967 rgKeySyms[nKeySyms++] = XK_Alt_L;
968 rgKeySyms[nKeySyms++] = XK_Alt_R;
969 }
970 if (mod & Qt::META) {
971 // It would take some code to determine whether the Win key
972 // is associated with Super or Meta, so check for both.
973 // See bug #140023 for details.
974 rgKeySyms[nKeySyms++] = XK_Super_L;
975 rgKeySyms[nKeySyms++] = XK_Super_R;
976 rgKeySyms[nKeySyms++] = XK_Meta_L;
977 rgKeySyms[nKeySyms++] = XK_Meta_R;
978 }
979
980 return areKeySymXsDepressed(rgKeySyms, nKeySyms);
981 }
982
areModKeysDepressedWayland(const QKeySequence & seq)983 static bool areModKeysDepressedWayland(const QKeySequence &seq)
984 {
985 const int mod = seq[seq.count()-1] & Qt::KeyboardModifierMask;
986 const Qt::KeyboardModifiers mods = input()->modifiersRelevantForGlobalShortcuts();
987 if ((mod & Qt::SHIFT) && mods.testFlag(Qt::ShiftModifier)) {
988 return true;
989 }
990 if ((mod & Qt::CTRL) && mods.testFlag(Qt::ControlModifier)) {
991 return true;
992 }
993 if ((mod & Qt::ALT) && mods.testFlag(Qt::AltModifier)) {
994 return true;
995 }
996 if ((mod & Qt::META) && mods.testFlag(Qt::MetaModifier)) {
997 return true;
998 }
999 return false;
1000 }
1001
areModKeysDepressed(const QKeySequence & seq)1002 static bool areModKeysDepressed(const QKeySequence& seq) {
1003 if (seq.isEmpty())
1004 return false;
1005 if (kwinApp()->shouldUseWaylandForCompositing()) {
1006 return areModKeysDepressedWayland(seq);
1007 } else {
1008 return areModKeysDepressedX11(seq);
1009 }
1010 }
1011
navigatingThroughWindows(bool forward,const QKeySequence & shortcut,TabBoxMode mode)1012 void TabBox::navigatingThroughWindows(bool forward, const QKeySequence &shortcut, TabBoxMode mode)
1013 {
1014 if (!m_ready || isGrabbed() || !Workspace::self()->isOnCurrentHead()) {
1015 return;
1016 }
1017 if (!options->focusPolicyIsReasonable()) {
1018 //ungrabXKeyboard(); // need that because of accelerator raw mode
1019 // CDE style raise / lower
1020 CDEWalkThroughWindows(forward);
1021 } else {
1022 workspace()->forEachAbstractClient([](Toplevel *toplevel) {
1023 if (toplevel->isPopupWindow())
1024 toplevel->popupDone();
1025 });
1026 if (areModKeysDepressed(shortcut)) {
1027 if (startKDEWalkThroughWindows(mode))
1028 KDEWalkThroughWindows(forward);
1029 } else
1030 // if the shortcut has no modifiers, don't show the tabbox,
1031 // don't grab, but simply go to the next window
1032 KDEOneStepThroughWindows(forward, mode);
1033 }
1034 }
1035
slotWalkThroughWindows()1036 void TabBox::slotWalkThroughWindows()
1037 {
1038 navigatingThroughWindows(true, m_cutWalkThroughWindows, TabBoxWindowsMode);
1039 }
1040
slotWalkBackThroughWindows()1041 void TabBox::slotWalkBackThroughWindows()
1042 {
1043 navigatingThroughWindows(false, m_cutWalkThroughWindowsReverse, TabBoxWindowsMode);
1044 }
1045
slotWalkThroughWindowsAlternative()1046 void TabBox::slotWalkThroughWindowsAlternative()
1047 {
1048 navigatingThroughWindows(true, m_cutWalkThroughWindowsAlternative, TabBoxWindowsAlternativeMode);
1049 }
1050
slotWalkBackThroughWindowsAlternative()1051 void TabBox::slotWalkBackThroughWindowsAlternative()
1052 {
1053 navigatingThroughWindows(false, m_cutWalkThroughWindowsAlternativeReverse, TabBoxWindowsAlternativeMode);
1054 }
1055
slotWalkThroughCurrentAppWindows()1056 void TabBox::slotWalkThroughCurrentAppWindows()
1057 {
1058 navigatingThroughWindows(true, m_cutWalkThroughCurrentAppWindows, TabBoxCurrentAppWindowsMode);
1059 }
1060
slotWalkBackThroughCurrentAppWindows()1061 void TabBox::slotWalkBackThroughCurrentAppWindows()
1062 {
1063 navigatingThroughWindows(false, m_cutWalkThroughCurrentAppWindowsReverse, TabBoxCurrentAppWindowsMode);
1064 }
1065
slotWalkThroughCurrentAppWindowsAlternative()1066 void TabBox::slotWalkThroughCurrentAppWindowsAlternative()
1067 {
1068 navigatingThroughWindows(true, m_cutWalkThroughCurrentAppWindowsAlternative, TabBoxCurrentAppWindowsAlternativeMode);
1069 }
1070
slotWalkBackThroughCurrentAppWindowsAlternative()1071 void TabBox::slotWalkBackThroughCurrentAppWindowsAlternative()
1072 {
1073 navigatingThroughWindows(false, m_cutWalkThroughCurrentAppWindowsAlternativeReverse, TabBoxCurrentAppWindowsAlternativeMode);
1074 }
1075
slotWalkThroughDesktops()1076 void TabBox::slotWalkThroughDesktops()
1077 {
1078 if (!m_ready || isGrabbed() || !Workspace::self()->isOnCurrentHead()) {
1079 return;
1080 }
1081 if (areModKeysDepressed(m_cutWalkThroughDesktops)) {
1082 if (startWalkThroughDesktops())
1083 walkThroughDesktops(true);
1084 } else {
1085 oneStepThroughDesktops(true);
1086 }
1087 }
1088
slotWalkBackThroughDesktops()1089 void TabBox::slotWalkBackThroughDesktops()
1090 {
1091 if (!m_ready || isGrabbed() || !Workspace::self()->isOnCurrentHead()) {
1092 return;
1093 }
1094 if (areModKeysDepressed(m_cutWalkThroughDesktopsReverse)) {
1095 if (startWalkThroughDesktops())
1096 walkThroughDesktops(false);
1097 } else {
1098 oneStepThroughDesktops(false);
1099 }
1100 }
1101
slotWalkThroughDesktopList()1102 void TabBox::slotWalkThroughDesktopList()
1103 {
1104 if (!m_ready || isGrabbed() || !Workspace::self()->isOnCurrentHead()) {
1105 return;
1106 }
1107 if (areModKeysDepressed(m_cutWalkThroughDesktopList)) {
1108 if (startWalkThroughDesktopList())
1109 walkThroughDesktops(true);
1110 } else {
1111 oneStepThroughDesktopList(true);
1112 }
1113 }
1114
slotWalkBackThroughDesktopList()1115 void TabBox::slotWalkBackThroughDesktopList()
1116 {
1117 if (!m_ready || isGrabbed() || !Workspace::self()->isOnCurrentHead()) {
1118 return;
1119 }
1120 if (areModKeysDepressed(m_cutWalkThroughDesktopListReverse)) {
1121 if (startWalkThroughDesktopList())
1122 walkThroughDesktops(false);
1123 } else {
1124 oneStepThroughDesktopList(false);
1125 }
1126 }
1127
shadeActivate(AbstractClient * c)1128 void TabBox::shadeActivate(AbstractClient *c)
1129 {
1130 if ((c->shadeMode() == ShadeNormal || c->shadeMode() == ShadeHover) && options->isShadeHover())
1131 c->setShade(ShadeActivated);
1132 }
1133
toggle(ElectricBorder eb)1134 bool TabBox::toggle(ElectricBorder eb)
1135 {
1136 if (m_borderAlternativeActivate.contains(eb)) {
1137 return toggleMode(TabBoxWindowsAlternativeMode);
1138 } else {
1139 return toggleMode(TabBoxWindowsMode);
1140 }
1141 }
1142
toggleMode(TabBoxMode mode)1143 bool TabBox::toggleMode(TabBoxMode mode)
1144 {
1145 if (!options->focusPolicyIsReasonable())
1146 return false; // not supported.
1147 if (isDisplayed()) {
1148 accept();
1149 return true;
1150 }
1151 if (!establishTabBoxGrab())
1152 return false;
1153 m_noModifierGrab = m_tabGrab = true;
1154 setMode(mode);
1155 reset();
1156 show();
1157 return true;
1158 }
1159
startKDEWalkThroughWindows(TabBoxMode mode)1160 bool TabBox::startKDEWalkThroughWindows(TabBoxMode mode)
1161 {
1162 if (!establishTabBoxGrab())
1163 return false;
1164 m_tabGrab = true;
1165 m_noModifierGrab = false;
1166 setMode(mode);
1167 reset();
1168 return true;
1169 }
1170
startWalkThroughDesktops(TabBoxMode mode)1171 bool TabBox::startWalkThroughDesktops(TabBoxMode mode)
1172 {
1173 if (!establishTabBoxGrab())
1174 return false;
1175 m_desktopGrab = true;
1176 m_noModifierGrab = false;
1177 setMode(mode);
1178 reset();
1179 return true;
1180 }
1181
startWalkThroughDesktops()1182 bool TabBox::startWalkThroughDesktops()
1183 {
1184 return startWalkThroughDesktops(TabBoxDesktopMode);
1185 }
1186
startWalkThroughDesktopList()1187 bool TabBox::startWalkThroughDesktopList()
1188 {
1189 return startWalkThroughDesktops(TabBoxDesktopListMode);
1190 }
1191
KDEWalkThroughWindows(bool forward)1192 void TabBox::KDEWalkThroughWindows(bool forward)
1193 {
1194 nextPrev(forward);
1195 delayedShow();
1196 }
1197
walkThroughDesktops(bool forward)1198 void TabBox::walkThroughDesktops(bool forward)
1199 {
1200 nextPrev(forward);
1201 delayedShow();
1202 }
1203
CDEWalkThroughWindows(bool forward)1204 void TabBox::CDEWalkThroughWindows(bool forward)
1205 {
1206 AbstractClient* c = nullptr;
1207 // this function find the first suitable client for unreasonable focus
1208 // policies - the topmost one, with some exceptions (can't be keepabove/below,
1209 // otherwise it gets stuck on them)
1210 // Q_ASSERT(Workspace::self()->block_stacking_updates == 0);
1211 for (int i = Workspace::self()->stackingOrder().size() - 1;
1212 i >= 0 ;
1213 --i) {
1214 auto it = qobject_cast<AbstractClient*>(Workspace::self()->stackingOrder().at(i));
1215 if (it && it->isOnCurrentActivity() && it->isOnCurrentDesktop() && !it->isSpecialWindow()
1216 && it->isShown(false) && it->wantsTabFocus()
1217 && !it->keepAbove() && !it->keepBelow()) {
1218 c = it;
1219 break;
1220 }
1221 }
1222 AbstractClient* nc = c;
1223 bool options_traverse_all;
1224 {
1225 KConfigGroup group(kwinApp()->config(), "TabBox");
1226 options_traverse_all = group.readEntry("TraverseAll", false);
1227 }
1228
1229 AbstractClient* firstClient = nullptr;
1230 do {
1231 nc = forward ? nextClientStatic(nc) : previousClientStatic(nc);
1232 if (!firstClient) {
1233 // When we see our first client for the second time,
1234 // it's time to stop.
1235 firstClient = nc;
1236 } else if (nc == firstClient) {
1237 // No candidates found.
1238 nc = nullptr;
1239 break;
1240 }
1241 } while (nc && nc != c &&
1242 ((!options_traverse_all && !nc->isOnDesktop(currentDesktop())) ||
1243 nc->isMinimized() || !nc->wantsTabFocus() || nc->keepAbove() || nc->keepBelow() || !nc->isOnCurrentActivity()));
1244 if (nc) {
1245 if (c && c != nc)
1246 Workspace::self()->lowerClient(c);
1247 if (options->focusPolicyIsReasonable()) {
1248 Workspace::self()->activateClient(nc);
1249 shadeActivate(nc);
1250 } else {
1251 if (!nc->isOnDesktop(currentDesktop()))
1252 setCurrentDesktop(nc->desktop());
1253 Workspace::self()->raiseClient(nc);
1254 }
1255 }
1256 }
1257
KDEOneStepThroughWindows(bool forward,TabBoxMode mode)1258 void TabBox::KDEOneStepThroughWindows(bool forward, TabBoxMode mode)
1259 {
1260 setMode(mode);
1261 reset();
1262 nextPrev(forward);
1263 if (AbstractClient* c = currentClient()) {
1264 Workspace::self()->activateClient(c);
1265 shadeActivate(c);
1266 }
1267 }
1268
oneStepThroughDesktops(bool forward,TabBoxMode mode)1269 void TabBox::oneStepThroughDesktops(bool forward, TabBoxMode mode)
1270 {
1271 setMode(mode);
1272 reset();
1273 nextPrev(forward);
1274 if (currentDesktop() != -1)
1275 setCurrentDesktop(currentDesktop());
1276 }
1277
oneStepThroughDesktops(bool forward)1278 void TabBox::oneStepThroughDesktops(bool forward)
1279 {
1280 oneStepThroughDesktops(forward, TabBoxDesktopMode);
1281 }
1282
oneStepThroughDesktopList(bool forward)1283 void TabBox::oneStepThroughDesktopList(bool forward)
1284 {
1285 oneStepThroughDesktops(forward, TabBoxDesktopListMode);
1286 }
1287
keyPress(int keyQt)1288 void TabBox::keyPress(int keyQt)
1289 {
1290 enum Direction { Backward = -1, Steady = 0, Forward = 1 };
1291 Direction direction(Steady);
1292
1293 auto contains = [](const QKeySequence &shortcut, int key) -> bool {
1294 for (int i = 0; i < shortcut.count(); ++i) {
1295 if (shortcut[i] == key) {
1296 return true;
1297 }
1298 }
1299 return false;
1300 };
1301
1302 // tests whether a shortcut matches and handles pitfalls on ShiftKey invocation
1303 auto directionFor = [keyQt, contains](const QKeySequence &forward, const QKeySequence &backward) -> Direction {
1304 if (contains(forward, keyQt))
1305 return Forward;
1306 if (contains(backward, keyQt))
1307 return Backward;
1308 if (!(keyQt & Qt::ShiftModifier))
1309 return Steady;
1310
1311 // Before testing the unshifted key (Ctrl+A vs. Ctrl+Shift+a etc.), see whether this is +Shift+Tab
1312 // and check that against +Shift+Backtab (as well)
1313 Qt::KeyboardModifiers mods = Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier|Qt::MetaModifier|Qt::KeypadModifier|Qt::GroupSwitchModifier;
1314 mods &= keyQt;
1315 if ((keyQt & ~mods) == Qt::Key_Tab) {
1316 if (contains(forward, mods | Qt::Key_Backtab))
1317 return Forward;
1318 if (contains(backward, mods | Qt::Key_Backtab))
1319 return Backward;
1320 }
1321
1322 // if the shortcuts do not match, try matching again after filtering the shift key from keyQt
1323 // it is needed to handle correctly the ALT+~ shorcut for example as it is coded as ALT+SHIFT+~ in keyQt
1324 if (contains(forward, keyQt & ~Qt::ShiftModifier))
1325 return Forward;
1326 if (contains(backward, keyQt & ~Qt::ShiftModifier))
1327 return Backward;
1328
1329 return Steady;
1330 };
1331
1332 if (m_tabGrab) {
1333 static const int ModeCount = 4;
1334 static const TabBoxMode modes[ModeCount] = {
1335 TabBoxWindowsMode, TabBoxWindowsAlternativeMode,
1336 TabBoxCurrentAppWindowsMode, TabBoxCurrentAppWindowsAlternativeMode
1337 };
1338 const QKeySequence cuts[2*ModeCount] = {
1339 // forward
1340 m_cutWalkThroughWindows, m_cutWalkThroughWindowsAlternative,
1341 m_cutWalkThroughCurrentAppWindows, m_cutWalkThroughCurrentAppWindowsAlternative,
1342 // backward
1343 m_cutWalkThroughWindowsReverse, m_cutWalkThroughWindowsAlternativeReverse,
1344 m_cutWalkThroughCurrentAppWindowsReverse, m_cutWalkThroughCurrentAppWindowsAlternativeReverse
1345 };
1346 bool testedCurrent = false; // in case of collision, prefer to stay in the current mode
1347 int i = 0, j = 0;
1348 while (true) {
1349 if (!testedCurrent && modes[i] != mode()) {
1350 ++j;
1351 i = (i+1) % ModeCount;
1352 continue;
1353 }
1354 if (testedCurrent && modes[i] == mode()) {
1355 break;
1356 }
1357 testedCurrent = true;
1358 direction = directionFor(cuts[i], cuts[i+ModeCount]);
1359 if (direction != Steady) {
1360 if (modes[i] != mode()) {
1361 accept(false);
1362 setMode(modes[i]);
1363 auto replayWithChangedTabboxMode = [this, direction]() {
1364 reset();
1365 nextPrev(direction == Forward);
1366 };
1367 QTimer::singleShot(50, this, replayWithChangedTabboxMode);
1368 }
1369 break;
1370 } else if (++j > 2*ModeCount) { // guarding counter for invalid modes
1371 qCDebug(KWIN_TABBOX) << "Invalid TabBoxMode";
1372 return;
1373 }
1374 i = (i+1) % ModeCount;
1375 }
1376 if (direction != Steady) {
1377 qCDebug(KWIN_TABBOX) << "== " << cuts[i].toString() << " or " << cuts[i+ModeCount].toString();
1378 KDEWalkThroughWindows(direction == Forward);
1379 }
1380 } else if (m_desktopGrab) {
1381 direction = directionFor(m_cutWalkThroughDesktops, m_cutWalkThroughDesktopsReverse);
1382 if (direction == Steady)
1383 direction = directionFor(m_cutWalkThroughDesktopList, m_cutWalkThroughDesktopListReverse);
1384 if (direction != Steady)
1385 walkThroughDesktops(direction == Forward);
1386 }
1387
1388 if (m_desktopGrab || m_tabGrab) {
1389 if (((keyQt & ~Qt::KeyboardModifierMask) == Qt::Key_Escape) && direction == Steady) {
1390 // if Escape is part of the shortcut, don't cancel
1391 close(true);
1392 } else if (direction == Steady) {
1393 QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, keyQt & ~Qt::KeyboardModifierMask, Qt::NoModifier);
1394 grabbedKeyEvent(event);
1395 }
1396 }
1397 }
1398
close(bool abort)1399 void TabBox::close(bool abort)
1400 {
1401 if (isGrabbed()) {
1402 removeTabBoxGrab();
1403 }
1404 hide(abort);
1405 input()->pointer()->setEnableConstraints(true);
1406 m_tabGrab = false;
1407 m_desktopGrab = false;
1408 m_noModifierGrab = false;
1409 }
1410
accept(bool closeTabBox)1411 void TabBox::accept(bool closeTabBox)
1412 {
1413 AbstractClient *c = currentClient();
1414 if (closeTabBox)
1415 close();
1416 if (c) {
1417 Workspace::self()->activateClient(c);
1418 shadeActivate(c);
1419 if (c->isDesktop())
1420 Workspace::self()->setShowingDesktop(!Workspace::self()->showingDesktop());
1421 }
1422 }
1423
modifiersReleased()1424 void TabBox::modifiersReleased()
1425 {
1426 if (m_noModifierGrab) {
1427 return;
1428 }
1429 if (m_tabGrab) {
1430 bool old_control_grab = m_desktopGrab;
1431 accept();
1432 m_desktopGrab = old_control_grab;
1433 }
1434 if (m_desktopGrab) {
1435 bool old_tab_grab = m_tabGrab;
1436 int desktop = currentDesktop();
1437 close();
1438 m_tabGrab = old_tab_grab;
1439 if (desktop != -1) {
1440 setCurrentDesktop(desktop);
1441 VirtualDesktopManager::self()->setCurrent(desktop);
1442 }
1443 }
1444 }
1445
nextDesktopStatic(int iDesktop) const1446 int TabBox::nextDesktopStatic(int iDesktop) const
1447 {
1448 DesktopNext functor;
1449 return functor(iDesktop, true);
1450 }
1451
previousDesktopStatic(int iDesktop) const1452 int TabBox::previousDesktopStatic(int iDesktop) const
1453 {
1454 DesktopPrevious functor;
1455 return functor(iDesktop, true);
1456 }
1457
1458 /**
1459 * Auxiliary functions to travers all clients according to the static
1460 * order. Useful for the CDE-style Alt-tab feature.
1461 */
nextClientStatic(AbstractClient * c) const1462 AbstractClient* TabBox::nextClientStatic(AbstractClient* c) const
1463 {
1464 const auto &list = Workspace::self()->allClientList();
1465 if (!c || list.isEmpty())
1466 return nullptr;
1467 int pos = list.indexOf(c);
1468 if (pos == -1)
1469 return list.first();
1470 ++pos;
1471 if (pos == list.count())
1472 return list.first();
1473 return list.at(pos);
1474 }
1475
1476 /**
1477 * Auxiliary functions to travers all clients according to the static
1478 * order. Useful for the CDE-style Alt-tab feature.
1479 */
previousClientStatic(AbstractClient * c) const1480 AbstractClient* TabBox::previousClientStatic(AbstractClient* c) const
1481 {
1482 const auto &list = Workspace::self()->allClientList();
1483 if (!c || list.isEmpty())
1484 return nullptr;
1485 int pos = list.indexOf(c);
1486 if (pos == -1)
1487 return list.last();
1488 if (pos == 0)
1489 return list.last();
1490 --pos;
1491 return list.at(pos);
1492 }
1493
establishTabBoxGrab()1494 bool TabBox::establishTabBoxGrab()
1495 {
1496 if (kwinApp()->shouldUseWaylandForCompositing()) {
1497 m_forcedGlobalMouseGrab = true;
1498 return true;
1499 }
1500 updateXTime();
1501 if (!grabXKeyboard())
1502 return false;
1503 // Don't try to establish a global mouse grab using XGrabPointer, as that would prevent
1504 // using Alt+Tab while DND (#44972). However force passive grabs on all windows
1505 // in order to catch MouseRelease events and close the tabbox (#67416).
1506 // All clients already have passive grabs in their wrapper windows, so check only
1507 // the active client, which may not have it.
1508 Q_ASSERT(!m_forcedGlobalMouseGrab);
1509 m_forcedGlobalMouseGrab = true;
1510 if (Workspace::self()->activeClient() != nullptr)
1511 Workspace::self()->activeClient()->updateMouseGrab();
1512 m_x11EventFilter.reset(new X11Filter);
1513 return true;
1514 }
1515
removeTabBoxGrab()1516 void TabBox::removeTabBoxGrab()
1517 {
1518 if (kwinApp()->shouldUseWaylandForCompositing()) {
1519 m_forcedGlobalMouseGrab = false;
1520 return;
1521 }
1522 updateXTime();
1523 ungrabXKeyboard();
1524 Q_ASSERT(m_forcedGlobalMouseGrab);
1525 m_forcedGlobalMouseGrab = false;
1526 if (Workspace::self()->activeClient() != nullptr)
1527 Workspace::self()->activeClient()->updateMouseGrab();
1528 m_x11EventFilter.reset();
1529 }
1530 } // namespace TabBox
1531 } // namespace
1532
1533