Changeset View
Changeset View
Standalone View
Standalone View
keyboard_layout_switching.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2017 Martin Gräßlin <mgraesslin@kde.org> | ||||
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 | #include "keyboard_layout_switching.h" | ||||
21 | #include "keyboard_layout.h" | ||||
22 | #include "virtualdesktops.h" | ||||
23 | #include "xkb.h" | ||||
24 | | ||||
25 | namespace KWin | ||||
26 | { | ||||
27 | | ||||
28 | namespace KeyboardLayoutSwitching | ||||
29 | { | ||||
30 | | ||||
31 | Policy::Policy(Xkb *xkb, KeyboardLayout *layout) | ||||
32 | : QObject(layout) | ||||
33 | , m_xkb(xkb) | ||||
34 | , m_layout(layout) | ||||
35 | { | ||||
36 | connect(m_layout, &KeyboardLayout::layoutsReconfigured, this, &Policy::clearCache); | ||||
37 | connect(m_layout, &KeyboardLayout::layoutChanged, this, &Policy::layoutChanged); | ||||
38 | } | ||||
39 | | ||||
40 | Policy::~Policy() = default; | ||||
41 | | ||||
42 | void Policy::setLayout(quint32 layout) | ||||
43 | { | ||||
44 | m_xkb->switchToLayout(layout); | ||||
45 | } | ||||
46 | | ||||
47 | quint32 Policy::layout() const | ||||
48 | { | ||||
49 | return m_xkb->currentLayout(); | ||||
50 | } | ||||
51 | | ||||
52 | Policy *Policy::create(Xkb *xkb, KeyboardLayout *layout, const QString &policy) | ||||
53 | { | ||||
54 | if (policy.toLower() == QStringLiteral("desktop")) { | ||||
55 | return new VirtualDesktopPolicy(xkb, layout); | ||||
56 | } | ||||
57 | return new GlobalPolicy(xkb, layout); | ||||
58 | } | ||||
59 | | ||||
60 | GlobalPolicy::GlobalPolicy(Xkb *xkb, KeyboardLayout *layout) | ||||
61 | : Policy(xkb, layout) | ||||
62 | { | ||||
63 | } | ||||
64 | | ||||
65 | GlobalPolicy::~GlobalPolicy() = default; | ||||
66 | | ||||
67 | VirtualDesktopPolicy::VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout) | ||||
68 | : Policy(xkb, layout) | ||||
69 | { | ||||
70 | connect(VirtualDesktopManager::self(), &VirtualDesktopManager::currentChanged, this, &VirtualDesktopPolicy::desktopChanged); | ||||
71 | } | ||||
72 | | ||||
73 | VirtualDesktopPolicy::~VirtualDesktopPolicy() = default; | ||||
74 | | ||||
75 | void VirtualDesktopPolicy::clearCache() | ||||
76 | { | ||||
77 | m_layouts.clear(); | ||||
78 | } | ||||
79 | | ||||
80 | void VirtualDesktopPolicy::desktopChanged() | ||||
81 | { | ||||
82 | auto d = VirtualDesktopManager::self()->currentDesktop(); | ||||
83 | if (!d) { | ||||
84 | return; | ||||
85 | } | ||||
86 | auto it = m_layouts.constFind(d); | ||||
87 | if (it == m_layouts.constEnd()) { | ||||
88 | // new desktop - go to default; | ||||
89 | setLayout(0); | ||||
90 | } else { | ||||
91 | setLayout(it.value()); | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | void VirtualDesktopPolicy::layoutChanged() | ||||
96 | { | ||||
97 | auto d = VirtualDesktopManager::self()->currentDesktop(); | ||||
98 | if (!d) { | ||||
99 | return; | ||||
100 | } | ||||
101 | auto it = m_layouts.find(d); | ||||
102 | const auto l = layout(); | ||||
103 | if (it == m_layouts.constEnd()) { | ||||
104 | m_layouts.insert(d, l); | ||||
105 | connect(d, &VirtualDesktop::aboutToBeDestroyed, this, | ||||
106 | [this, d] { | ||||
107 | m_layouts.remove(d); | ||||
108 | } | ||||
109 | ); | ||||
110 | } else { | ||||
111 | if (it.value() == l) { | ||||
112 | return; | ||||
113 | } | ||||
114 | it.value() = l; | ||||
115 | } | ||||
116 | } | ||||
117 | | ||||
118 | } | ||||
119 | } |