Changeset View
Changeset View
Standalone View
Standalone View
tabletmodemanager.cpp
Show All 17 Lines | |||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | 19 | * | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "tabletmodemanager.h" | 22 | #include "tabletmodemanager.h" | ||
23 | #include "input.h" | 23 | #include "input.h" | ||
24 | #include "input_event.h" | 24 | #include "input_event.h" | ||
25 | #include "input_event_spy.h" | 25 | #include "input_event_spy.h" | ||
26 | #include "settings.h" | ||||
26 | 27 | | |||
27 | #include "libinput/device.h" | 28 | #include "libinput/device.h" | ||
29 | #include "libinput/connection.h" | ||||
28 | 30 | | |||
31 | #include <QTimer> | ||||
29 | #include <QDBusConnection> | 32 | #include <QDBusConnection> | ||
30 | 33 | | |||
31 | using namespace KWin; | 34 | using namespace KWin; | ||
32 | 35 | | |||
33 | KWIN_SINGLETON_FACTORY_VARIABLE(TabletModeManager, s_manager) | 36 | KWIN_SINGLETON_FACTORY_VARIABLE(TabletModeManager, s_manager) | ||
34 | 37 | | |||
35 | class KWin::TabletModeInputEventSpy : public InputEventSpy | 38 | class TabletModeSwitchEventSpy : public QObject, public InputEventSpy | ||
36 | { | 39 | { | ||
37 | public: | 40 | public: | ||
38 | explicit TabletModeInputEventSpy(TabletModeManager *parent); | 41 | explicit TabletModeSwitchEventSpy(TabletModeManager *parent) | ||
39 | 42 | : QObject(parent) | |||
40 | void switchEvent(SwitchEvent *event) override; | 43 | , m_parent(parent) | ||
41 | private: | | |||
42 | TabletModeManager *m_parent; | | |||
43 | }; | | |||
44 | | ||||
45 | TabletModeInputEventSpy::TabletModeInputEventSpy(TabletModeManager *parent) | | |||
46 | : m_parent(parent) | | |||
47 | { | 44 | { | ||
48 | } | 45 | } | ||
49 | 46 | | |||
50 | void TabletModeInputEventSpy::switchEvent(SwitchEvent *event) | 47 | void switchEvent(SwitchEvent *event) override | ||
51 | { | 48 | { | ||
52 | if (!event->device()->isTabletModeSwitch()) { | 49 | if (!event->device()->isTabletModeSwitch()) { | ||
53 | return; | 50 | return; | ||
54 | } | 51 | } | ||
55 | 52 | | |||
56 | switch (event->state()) { | 53 | switch (event->state()) { | ||
57 | case SwitchEvent::State::Off: | 54 | case SwitchEvent::State::Off: | ||
58 | m_parent->setIsTablet(false); | 55 | m_parent->setIsTablet(false); | ||
59 | break; | 56 | break; | ||
60 | case SwitchEvent::State::On: | 57 | case SwitchEvent::State::On: | ||
61 | m_parent->setIsTablet(true); | 58 | m_parent->setIsTablet(true); | ||
62 | break; | 59 | break; | ||
63 | default: | 60 | default: | ||
64 | Q_UNREACHABLE(); | 61 | Q_UNREACHABLE(); | ||
65 | } | 62 | } | ||
66 | } | 63 | } | ||
67 | 64 | | |||
65 | private: | ||||
66 | TabletModeManager * const m_parent; | ||||
67 | }; | ||||
68 | 68 | | |||
69 | 69 | | |||
70 | class TabletModeTouchpadRemovedSpy : public QObject | ||||
71 | { | ||||
72 | public: | ||||
73 | explicit TabletModeTouchpadRemovedSpy(TabletModeManager *parent) | ||||
74 | : QObject(parent) | ||||
75 | , m_parent(parent) | ||||
76 | { | ||||
77 | auto c = LibInput::Connection::self(); | ||||
78 | connect(c, &LibInput::Connection::deviceAdded, this, &TabletModeTouchpadRemovedSpy::refresh); | ||||
79 | connect(c, &LibInput::Connection::deviceRemoved, this, &TabletModeTouchpadRemovedSpy::refresh); | ||||
80 | | ||||
81 | check(); | ||||
82 | } | ||||
83 | | ||||
84 | void refresh(LibInput::Device* d) { | ||||
85 | if (!d->isTouch() && !d->isPointer()) | ||||
86 | return; | ||||
87 | check(); | ||||
88 | } | ||||
89 | | ||||
90 | void check() { | ||||
91 | if (!LibInput::Connection::self()) { | ||||
92 | qDebug() << "no libinput :("; | ||||
93 | return; | ||||
94 | } | ||||
95 | | ||||
96 | const auto devices = LibInput::Connection::self()->devices(); | ||||
97 | const bool hasPointer = std::any_of(devices.constBegin(), devices.constEnd(), [](LibInput::Device* device){ return device->isPointer(); }); | ||||
98 | const bool hasTouch = std::any_of(devices.constBegin(), devices.constEnd(), [](LibInput::Device* device){ return device->isTouch(); }); | ||||
99 | m_parent->setIsTablet(hasTouch && !hasPointer); | ||||
100 | } | ||||
101 | | ||||
102 | private: | ||||
103 | TabletModeManager * const m_parent; | ||||
104 | }; | ||||
105 | | ||||
70 | TabletModeManager::TabletModeManager(QObject *parent) | 106 | TabletModeManager::TabletModeManager(QObject *parent) | ||
71 | : QObject(parent), | 107 | : QObject(parent) | ||
72 | m_spy(new TabletModeInputEventSpy(this)) | | |||
73 | { | 108 | { | ||
74 | input()->installInputEventSpy(m_spy); | 109 | if (input()->hasTabletModeSwitch()) { | ||
110 | input()->installInputEventSpy(new TabletModeSwitchEventSpy(this)); | ||||
111 | } else { | ||||
112 | // to enable: kwriteconfig5 --file kwin.cfg --group TabletMode --key TabletModeOnTouchPadRemoval true | ||||
davidedmundson: I'd prefer using kwin.kcfg
| |||||
113 | Settings settings(kwinApp()->config()); | ||||
114 | if (settings.tabletModeOnTouchPadRemoval()) { | ||||
maybe checking touchpad and mouse? ie maybe i detached the keyboard from my transformable laptop but iattached usb mouse and keyboard, i would expect in this case to go in desktop mode mart: maybe checking touchpad and mouse? ie maybe i detached the keyboard from my transformable… | |||||
115 | m_detecting = true; | ||||
116 | QTimer::singleShot(2000, this, [this] {new TabletModeTouchpadRemovedSpy(this);}); | ||||
If you do have tabletModeSwitch we're still detecting a tablet mode regardless of this setting. "SetTabletModeOnTouchPadRemoval" ? davidedmundson: If you do have tabletModeSwitch we're still detecting a tablet mode regardless of this setting. | |||||
117 | } | ||||
118 | } | ||||
75 | 119 | | |||
76 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin"), | 120 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin"), | ||
77 | QStringLiteral("org.kde.KWin.TabletModeManager"), | 121 | QStringLiteral("org.kde.KWin.TabletModeManager"), | ||
78 | this, | 122 | this, | ||
79 | QDBusConnection::ExportAllProperties | QDBusConnection::ExportAllSignals | 123 | QDBusConnection::ExportAllProperties | QDBusConnection::ExportAllSignals | ||
80 | ); | 124 | ); | ||
81 | 125 | | |||
82 | connect(input(), &InputRedirection::hasTabletModeSwitchChanged, this, &TabletModeManager::tabletModeAvailableChanged); | 126 | connect(input(), &InputRedirection::hasTabletModeSwitchChanged, this, &TabletModeManager::tabletModeAvailableChanged); | ||
83 | } | 127 | } | ||
84 | 128 | | |||
85 | bool TabletModeManager::isTabletModeAvailable() const | 129 | bool TabletModeManager::isTabletModeAvailable() const | ||
86 | { | 130 | { | ||
87 | return input()->hasTabletModeSwitch(); | 131 | return input()->hasTabletModeSwitch() || m_detecting; | ||
I don't really how this is meant to be used but should this be only true when there's a touch screen? davidedmundson: I don't really how this is meant to be used but should this be only true when there's a touch… | |||||
We could be a bit more specific, yes. But at this point, if it has requested through a config check to do the detection, I'd say it's fine to announce it as such. If you really want to, I can check that there's a touchscreen before initialising the Spy. apol: We could be a bit more specific, yes. But at this point, if it has requested through a config… | |||||
88 | } | 132 | } | ||
89 | 133 | | |||
90 | bool TabletModeManager::isTablet() const | 134 | bool TabletModeManager::isTablet() const | ||
91 | { | 135 | { | ||
92 | return m_isTabletMode; | 136 | return m_isTabletMode; | ||
93 | } | 137 | } | ||
94 | 138 | | |||
95 | void TabletModeManager::setIsTablet(bool tablet) | 139 | void TabletModeManager::setIsTablet(bool tablet) | ||
96 | { | 140 | { | ||
97 | if (m_isTabletMode == tablet) { | 141 | if (m_isTabletMode == tablet) { | ||
98 | return; | 142 | return; | ||
99 | } | 143 | } | ||
100 | 144 | | |||
101 | m_isTabletMode = tablet; | 145 | m_isTabletMode = tablet; | ||
102 | emit tabletModeChanged(tablet); | 146 | emit tabletModeChanged(tablet); | ||
103 | } | 147 | } |
I'd prefer using kwin.kcfg