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