Changeset View
Changeset View
Standalone View
Standalone View
tabletmodemanager.cpp
Show All 19 Lines | |||||
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 | 26 | | |||
27 | #include "libinput/device.h" | 27 | #include "libinput/device.h" | ||
28 | #include "libinput/connection.h" | ||||
28 | 29 | | |||
30 | #include <QTimer> | ||||
29 | #include <QDBusConnection> | 31 | #include <QDBusConnection> | ||
30 | 32 | | |||
31 | using namespace KWin; | 33 | using namespace KWin; | ||
32 | 34 | | |||
33 | KWIN_SINGLETON_FACTORY_VARIABLE(TabletModeManager, s_manager) | 35 | KWIN_SINGLETON_FACTORY_VARIABLE(TabletModeManager, s_manager) | ||
34 | 36 | | |||
35 | class KWin::TabletModeInputEventSpy : public InputEventSpy | 37 | class TabletModeSwitchEventSpy : public QObject, public InputEventSpy | ||
36 | { | 38 | { | ||
37 | public: | 39 | public: | ||
38 | explicit TabletModeInputEventSpy(TabletModeManager *parent); | 40 | explicit TabletModeSwitchEventSpy(TabletModeManager *parent) | ||
39 | 41 | : QObject(parent) | |||
40 | void switchEvent(SwitchEvent *event) override; | 42 | , m_parent(parent) | ||
41 | private: | | |||
42 | TabletModeManager *m_parent; | | |||
43 | }; | | |||
44 | | ||||
45 | TabletModeInputEventSpy::TabletModeInputEventSpy(TabletModeManager *parent) | | |||
46 | : m_parent(parent) | | |||
47 | { | 43 | { | ||
48 | } | 44 | } | ||
49 | 45 | | |||
50 | void TabletModeInputEventSpy::switchEvent(SwitchEvent *event) | 46 | void switchEvent(SwitchEvent *event) override | ||
51 | { | 47 | { | ||
52 | if (!event->device()->isTabletModeSwitch()) { | 48 | if (!event->device()->isTabletModeSwitch()) { | ||
53 | return; | 49 | return; | ||
54 | } | 50 | } | ||
55 | 51 | | |||
56 | switch (event->state()) { | 52 | switch (event->state()) { | ||
57 | case SwitchEvent::State::Off: | 53 | case SwitchEvent::State::Off: | ||
58 | m_parent->setIsTablet(false); | 54 | m_parent->setIsTablet(false); | ||
59 | break; | 55 | break; | ||
60 | case SwitchEvent::State::On: | 56 | case SwitchEvent::State::On: | ||
61 | m_parent->setIsTablet(true); | 57 | m_parent->setIsTablet(true); | ||
62 | break; | 58 | break; | ||
63 | default: | 59 | default: | ||
64 | Q_UNREACHABLE(); | 60 | Q_UNREACHABLE(); | ||
65 | } | 61 | } | ||
66 | } | 62 | } | ||
67 | 63 | | |||
64 | private: | ||||
65 | TabletModeManager * const m_parent; | ||||
66 | }; | ||||
68 | 67 | | |||
69 | 68 | | |||
69 | class TabletModeTouchpadRemovedSpy : public QObject | ||||
70 | { | ||||
71 | public: | ||||
72 | explicit TabletModeTouchpadRemovedSpy(TabletModeManager *parent) | ||||
73 | : QObject(parent) | ||||
74 | , m_parent(parent) | ||||
75 | { | ||||
76 | auto c = LibInput::Connection::self(); | ||||
77 | connect(c, &LibInput::Connection::deviceAdded, this, &TabletModeTouchpadRemovedSpy::refresh); | ||||
78 | connect(c, &LibInput::Connection::deviceRemoved, this, &TabletModeTouchpadRemovedSpy::refresh); | ||||
79 | | ||||
80 | check(); | ||||
81 | } | ||||
82 | | ||||
83 | void refresh(LibInput::Device* d) { | ||||
84 | if (!d->isTouch() && !d->isPointer()) | ||||
85 | return; | ||||
86 | check(); | ||||
87 | } | ||||
88 | | ||||
89 | void check() { | ||||
90 | if (!LibInput::Connection::self()) { | ||||
91 | qDebug() << "no libinput :("; | ||||
92 | return; | ||||
93 | } | ||||
94 | | ||||
95 | const auto devices = LibInput::Connection::self()->devices(); | ||||
96 | const bool hasPointer = std::any_of(devices.constBegin(), devices.constEnd(), [](LibInput::Device* device){ return device->isPointer(); }); | ||||
97 | const bool hasTouch = std::any_of(devices.constBegin(), devices.constEnd(), [](LibInput::Device* device){ return device->isTouch(); }); | ||||
98 | m_parent->setIsTablet(hasTouch && !hasPointer); | ||||
99 | } | ||||
100 | | ||||
101 | private: | ||||
102 | TabletModeManager * const m_parent; | ||||
103 | }; | ||||
104 | | ||||
70 | TabletModeManager::TabletModeManager(QObject *parent) | 105 | TabletModeManager::TabletModeManager(QObject *parent) | ||
71 | : QObject(parent), | 106 | : QObject(parent) | ||
72 | m_spy(new TabletModeInputEventSpy(this)) | | |||
73 | { | 107 | { | ||
74 | input()->installInputEventSpy(m_spy); | 108 | if (input()->hasTabletModeSwitch()) { | ||
109 | input()->installInputEventSpy(new TabletModeSwitchEventSpy(this)); | ||||
110 | } else { | ||||
111 | auto config = KSharedConfig::openConfig("kwinrc"); | ||||
davidedmundson: I'd prefer using kwin.kcfg
| |||||
112 | KConfigGroup group(config, "TabletMode"); | ||||
113 | | ||||
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… | |||||
114 | // to enable: kwriteconfig5 --file kwinrc --group TabletMode --key DetectTabletMode true | ||||
115 | if (group.readEntry<bool>("DetectTabletMode", false)) { | ||||
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. | |||||
116 | m_detecting = true; | ||||
117 | QTimer::singleShot(2000, this, [this] {new TabletModeTouchpadRemovedSpy(this);}); | ||||
118 | } | ||||
119 | } | ||||
75 | 120 | | |||
76 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin"), | 121 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin"), | ||
77 | QStringLiteral("org.kde.KWin.TabletModeManager"), | 122 | QStringLiteral("org.kde.KWin.TabletModeManager"), | ||
78 | this, | 123 | this, | ||
79 | QDBusConnection::ExportAllProperties | QDBusConnection::ExportAllSignals | 124 | QDBusConnection::ExportAllProperties | QDBusConnection::ExportAllSignals | ||
80 | ); | 125 | ); | ||
81 | 126 | | |||
82 | connect(input(), &InputRedirection::hasTabletModeSwitchChanged, this, &TabletModeManager::tabletModeAvailableChanged); | 127 | connect(input(), &InputRedirection::hasTabletModeSwitchChanged, this, &TabletModeManager::tabletModeAvailableChanged); | ||
83 | } | 128 | } | ||
84 | 129 | | |||
85 | bool TabletModeManager::isTabletModeAvailable() const | 130 | bool TabletModeManager::isTabletModeAvailable() const | ||
86 | { | 131 | { | ||
87 | return input()->hasTabletModeSwitch(); | 132 | 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 | } | 133 | } | ||
89 | 134 | | |||
90 | bool TabletModeManager::isTablet() const | 135 | bool TabletModeManager::isTablet() const | ||
91 | { | 136 | { | ||
92 | return m_isTabletMode; | 137 | return m_isTabletMode; | ||
93 | } | 138 | } | ||
94 | 139 | | |||
95 | void TabletModeManager::setIsTablet(bool tablet) | 140 | void TabletModeManager::setIsTablet(bool tablet) | ||
96 | { | 141 | { | ||
97 | if (m_isTabletMode == tablet) { | 142 | if (m_isTabletMode == tablet) { | ||
98 | return; | 143 | return; | ||
99 | } | 144 | } | ||
100 | 145 | | |||
101 | m_isTabletMode = tablet; | 146 | m_isTabletMode = tablet; | ||
102 | emit tabletModeChanged(tablet); | 147 | emit tabletModeChanged(tablet); | ||
103 | } | 148 | } |
I'd prefer using kwin.kcfg