MauiMan

formfactormanager.cpp
1#include "formfactormanager.h"
2
3#include "settingsstore.h"
4#include "mauimanutils.h"
5
6#include <QDebug>
7#include <QSize>
8#include <QScreen>
9#include <QGuiApplication>
10
11#if !defined Q_OS_ANDROID
12#include <QDBusInterface>
13#endif
14
15#include <QInputDevice>
16
17using namespace MauiMan;
18
19#if !defined Q_OS_ANDROID
20static QString typeToString(QInputDevice::DeviceTypes type)
21{
22 qDebug() << type;
23 QStringList typeString;
24 if (type.testFlag(QInputDevice::DeviceType::Mouse))
25 typeString << QStringLiteral("Mouse");
26 if (type.testFlag(QInputDevice::DeviceType::TouchPad))
27 typeString << QStringLiteral("TouchPad");
28 if (type.testFlag(QInputDevice::DeviceType::TouchScreen))
29 typeString << QStringLiteral("TouchScreen");
30 if (type.testFlag(QInputDevice::DeviceType::Keyboard))
31 typeString << QStringLiteral("Keyboard");
32 if (typeString.isEmpty())
33 typeString << QStringLiteral("Unknown");
34 return typeString.join((QStringLiteral(", ")));
35}
36#endif
37
38void FormFactorManager::sync(const QString &key, const QVariant &value)
39{
40#if !defined Q_OS_ANDROID
41 if (m_interface && m_interface->isValid())
42 {
43 m_interface->call(key, value);
44 }
45#else
46 Q_UNUSED(key)
47 Q_UNUSED(value)
48#endif
49}
50
51void FormFactorManager::setConnections()
52{
53#if !defined Q_OS_ANDROID
54 if(m_interface)
55 {
56 m_interface->disconnect();
57 m_interface->deleteLater();
58 m_interface = nullptr;
59 }
60
61 m_interface = new QDBusInterface(QStringLiteral("org.mauiman.Manager"),
62 QStringLiteral("/FormFactor"),
63 QStringLiteral("org.mauiman.FormFactor"),
65
66 if (m_interface->isValid())
67 {
68 connect(m_interface, SIGNAL(preferredModeChanged(uint)), this, SLOT(onPreferredModeChanged(uint)));
69 connect(m_interface, SIGNAL(forceTouchScreenChanged(bool)), this, SLOT(onForceTouchScreenChanged(bool)));
70 }
71#endif
72}
73
74void FormFactorManager::loadSettings()
75{
76 m_settings->beginModule(QStringLiteral("FormFactor"));
77
78#if !defined Q_OS_ANDROID
79 if(m_interface && m_interface->isValid())
80 {
81 m_preferredMode = m_interface->property("preferredMode").toUInt();
82 m_forceTouchScreen = m_interface->property("forceTouchScreen").toBool();
83 return;
84 }
85#endif
86
87 m_preferredMode = m_settings->load(QStringLiteral("PreferredMode"), m_preferredMode).toUInt();
88 m_forceTouchScreen = m_settings->load(QStringLiteral("ForceTouchScreen"), m_forceTouchScreen).toBool();
89}
90
91FormFactorManager::FormFactorManager(QObject *parent) : MauiMan::FormFactorInfo(parent)
92 ,m_settings(new MauiMan::SettingsStore(this))
93 ,m_info(new MauiMan::FormFactorInfo(this))
94{
95 qDebug( " INIT FORMFACTOR MANAGER");
96
97#if !defined Q_OS_ANDROID
98 auto server = new MauiManUtils(this);
99 if(server->serverRunning())
100 {
101 this->setConnections();
102 }
103
104 connect(server, &MauiManUtils::serverRunningChanged, [this](bool state)
105 {
106 if(state)
107 {
108 this->setConnections();
109 }
110 });
111#endif
112 m_preferredMode = defaultMode();
113
114 loadSettings();
115}
116
118{
119 return m_preferredMode;
120}
121
122uint FormFactorInfo::bestMode() const
123{
124 return m_bestMode;
125}
126
128{
129 return m_defaultMode;
130}
131
133{
134 return m_hasKeyboard;
135}
136
138{
139 return m_hasTouchscreen;
140}
141
142bool FormFactorInfo::hasMouse() const
143{
144 return m_hasMouse;
145}
146
148{
149 return m_hasTouchpad;
150}
151
152void FormFactorManager::setPreferredMode(uint preferredMode)
153{
154 if (m_preferredMode == preferredMode)
155 return;
156
157 m_preferredMode = preferredMode;
158
159 sync(QStringLiteral("setPreferredMode"), m_preferredMode);
160 m_settings->save(QStringLiteral("PreferredMode"), m_preferredMode);
161
162 Q_EMIT preferredModeChanged(m_preferredMode);
163}
164
166{
167 return m_forceTouchScreen;
168}
169
170void FormFactorManager::setForceTouchScreen(bool newForceTouchScreen)
171{
172 if (m_forceTouchScreen == newForceTouchScreen)
173 return;
174
175 m_forceTouchScreen = newForceTouchScreen;
176
177 sync(QStringLiteral("forceTouchScreen"), m_forceTouchScreen);
178 m_settings->save(QStringLiteral("ForceTouchScreen"), m_forceTouchScreen);
179
180 Q_EMIT forceTouchScreenChanged(m_forceTouchScreen);
181}
182
183void FormFactorManager::onPreferredModeChanged(uint preferredMode)
184{
185 if (m_preferredMode == preferredMode)
186 return;
187
188 m_preferredMode = preferredMode;
189 Q_EMIT preferredModeChanged(m_preferredMode);
190}
191
192void FormFactorManager::onForceTouchScreenChanged(bool value)
193{
194 if (m_forceTouchScreen == value)
195 return;
196
197 m_forceTouchScreen = value;
198 Q_EMIT forceTouchScreenChanged(m_forceTouchScreen);
199}
200
201void FormFactorInfo::findBestMode()
202{
203 uint bestMode = m_defaultMode;
204 /*
205 * 0- desktop
206 * 1- tablet
207 * 2- phone
208 * */
209
210 if(m_hasTouchscreen)
211 {
212 if(m_screenSize.width() > 1500)
213 {
214 if(m_hasKeyboard || m_hasMouse || m_hasTouchpad)
215 {
216 bestMode = 0; //A big touch screen and with keyboard/mouse/trackpad
217 }else
218 {
219 bestMode = 1; //A big touch screen alone
220 }
221 }
222 else if(m_screenSize.width() > 500)
223 {
224 bestMode = 1; //A tablet size touch screen
225 }
226 else
227 {
228 bestMode = 2; //A mobile size touch screen
229 }
230
231 }else
232 {
233
234 if(m_screenSize.width() > 1500)
235 {
236 bestMode = 0; // A big screen
237
238 }
239 else if(m_screenSize.width() > 500)
240 {
241 if(m_hasTouchpad)
242 {
243 bestMode = 1; // A small screen with a trackpad
244 }else
245 {
246 bestMode = 0;
247 }
248 }
249 else
250 {
251 bestMode = 1; //A mobile size touch screen
252 }
253 }
254
255 m_bestMode = bestMode;
256 Q_EMIT bestModeChanged(m_bestMode);
257}
258
260{
261 QScreen *screen = qApp->primaryScreen();
262 return screen->geometry();
263}
264
266{
267 QScreen *screen = qApp->primaryScreen();
268 return screen->orientation();
269}
270
271void FormFactorInfo::checkInputs(const QList<const QInputDevice *> &devices)
272{
273
274for(const auto &dev : devices)
275 qDebug() << "DEVICE:::" << dev->type();
276
277 auto hasType = [devices](QInputDevice::DeviceType type) -> bool
278 {
279 qDebug() << "CHECXKING IF DEVICE HAS TYPE" << type;
280 auto res= std::find_if(devices.constBegin(), devices.constEnd(), [type](const QInputDevice *device)
281 {
282 return device->type() == type;
283 });
284
285 return res != std::end(devices);
286 };
287
288 m_hasKeyboard = QInputDevice::primaryKeyboard() ? true : false;
289 m_hasMouse = hasType(QInputDevice::DeviceType::Mouse);
290 m_hasTouchscreen = hasType(QInputDevice::DeviceType::TouchScreen);
291 m_hasTouchpad = hasType(QInputDevice::DeviceType::TouchPad);
292
293 qDebug() << "CHECXKING IF DEVICE HAS TYPE" << m_hasKeyboard;
294
295
296 Q_EMIT hasKeyboardChanged(m_hasKeyboard);
297 Q_EMIT hasMouseChanged(m_hasMouse);
298 Q_EMIT hasTouchscreenChanged(m_hasTouchscreen);
299 Q_EMIT hasTouchpadChanged(m_hasTouchpad);
300}
301
302FormFactorInfo::FormFactorInfo(QObject *parent) : QObject(parent)
303{
304 qDebug( "INIT FORMFACTOR INFO");
305
306#if !defined Q_OS_ANDROID
307 checkInputs(QInputDevice::devices());
308qDebug() << "HAS KEYBOARD?" << QInputDevice::primaryKeyboard();
309 findBestMode();
310#endif
311
312}
The MauiManUtils class.
The FormFactoInfo class contains information about the input devices available in the current system.
bool hasKeyboard
Whether the device has a physical keyboard.
bool hasTouchpad
Whether the device has a trackpad or touchpad.
uint defaultMode
The system preferred mode.
bool hasTouchscreen
Whether the device has a touch screen.
QRect screenSize
The size of the main screen.
bool hasMouse
Whether the device has a physical mouse.
uint bestMode
The best fitted mode according to the available input devices and the screen size.
Qt::ScreenOrientation screenOrientation
The current orientation of the main screen.
bool forceTouchScreen
If a device is not detected to have a touch screen , and still it has it, this property can be used t...
uint preferredMode
The preferred mode to display information.
The SettingsStore class Allows to store and read settings for MauiMan from the local conf file.
void save(const QString &key, const QVariant &value)
Save a conf value entry to the local file.
void beginModule(const QString &module)
Set up the module section to write to.
QVariant load(const QString &key, const QVariant &defaultValue)
Load the value of a conf entry, with a possible default value.
Type type(const QSqlDatabase &db)
The MauiMan name-space contains all of the available modules for configuring the Maui Applications an...
QDBusMessage call(QDBus::CallMode mode, const QString &method, Args &&... args)
bool isValid() const const
QDBusConnection sessionBus()
QList< const QInputDevice * > devices()
const QInputDevice * primaryKeyboard(const QString &seatName)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool disconnect(const QMetaObject::Connection &connection)
QVariant property(const char *name) const const
int width() const const
QString join(QChar separator) const const
ScreenOrientation
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
uint toUInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.