Debian Codegraph @

src/kernel/dregionmonitor.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dregionmonitor.h"
6 #include "private/dregionmonitor_p.h"
7
8 #include <QObject>
9 #include <QDebug>
10 #include <QGuiApplication>
11 #include <QtDBus/QtDBus>
12
13 DGUI_BEGIN_NAMESPACE
14
15 /*!
16 \class Dtk::Gui::DRegionMonitor
17 \inmodule dtkgui
18 \brief 一个在指定区域内监视鼠标键盘动作的类.
19 */
20
21 /*!
22 \enum DRegionMonitor::RegisterdFlag
23 DRegionMonitor::RegisterdFlag 定义了 DRegionMonitor 监听标志。
24
25 \var Motion
26 代表监听鼠标移动。
27
28 \var Button
29 代表监听鼠标按键。
30
31 \var Key
32 代表监听键盘按键。
33 */
34
35 DRegionMonitor::DRegionMonitor(QObject *parent)
36 : QObject(parent),
37 DObject(*new DRegionMonitorPrivate(this))
38 {
39 D_D(DRegionMonitor);
40
41 d->init();
42 }
43
44 bool DRegionMonitor::registered() const
45 {
46 D_DC(DRegionMonitor);
47
48 return !d->registerKey.isEmpty();
49 }
50
51 QRegion DRegionMonitor::watchedRegion() const
52 {
53 D_DC(DRegionMonitor);
54
55 return d->watchedRegion;
56 }
57
58 DRegionMonitor::RegisterdFlags DRegionMonitor::registerFlags() const
59 {
60 D_DC(DRegionMonitor);
61
62 return d->registerdFlags;
63 }
64
65 DRegionMonitor::CoordinateType DRegionMonitor::coordinateType() const
66 {
67 D_DC(DRegionMonitor);
68
69 return d->type;
70 }
71
72 void DRegionMonitor::registerRegion()
73 {
74 if (registered())
75 {
76 qWarning() << "region already registered!";
77 return;
78 }
79
80 D_D(DRegionMonitor);
81
82 d->registerMonitorRegion();
83 }
84
85 void DRegionMonitor::unregisterRegion()
86 {
87 D_D(DRegionMonitor);
88
89 d->unregisterMonitorRegion();
90 }
91
92 void DRegionMonitor::setWatchedRegion(const QRegion &region)
93 {
94 D_D(DRegionMonitor);
95
96 d->watchedRegion = region;
97 if (registered())
98 d->registerMonitorRegion();
99 }
100
101 /*!
102 \property DRegionMonitor::registerdFlags
103
104 \brief 监听模式属性.
105 \brief 监听模式,需要注意 DRegionMonitor::Motion 监听鼠标移动会影响性能,默认包含,如果
106 需要可通过此函数去掉 DRegionMonitor::Motion .
107
108 registerdFlagChanged 信号会在监听标志 registerdFlags 被改变的时候被触发.
109
110 \a flags 监听模式.
111
112 */
113 void DRegionMonitor::setRegisterFlags(RegisterdFlags flags)
114 {
115 D_D(DRegionMonitor);
116
117 if (d->registerdFlags == flags)
118 return;
119
120 d->registerdFlags = flags;
121 if (registered())
122 d->registerMonitorRegion();
123 Q_EMIT registerdFlagsChanged(flags);
124 }
125
126 void DRegionMonitor::setCoordinateType(DRegionMonitor::CoordinateType type)
127 {
128 D_D(DRegionMonitor);
129
130 d->type = type;
131 }
132
133 DRegionMonitorPrivate::DRegionMonitorPrivate(DRegionMonitor *q)
134 : DObjectPrivate(q)
135 {
136 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.deepin.dde.XEventMonitor1"))) {
137 eventInter = new XEventMonitor("org.deepin.dde.XEventMonitor1", "/org/deepin/dde/XEventMonitor1",
138 "org.deepin.dde.XEventMonitor1", q);
139 } else {
140 eventInter = new XEventMonitor("com.deepin.api.XEventMonitor", "/com/deepin/api/XEventMonitor",
141 "com.deepin.api.XEventMonitor", q);
142 }
143
144 }
145
146 DRegionMonitorPrivate::~DRegionMonitorPrivate()
147 {
148 if (registered())
149 unregisterMonitorRegion();
150
151 eventInter->deleteLater();
152 }
153
154 void DRegionMonitorPrivate::init()
155 {
156 D_Q(DRegionMonitor);
157
158 QObject::connect(eventInter, SIGNAL(ButtonPress(int,int,int,QString)), q, SLOT(_q_ButtonPress(const int, const int, const int, const QString&)));
159 QObject::connect(eventInter, SIGNAL(ButtonRelease(int,int,int,QString)), q, SLOT(_q_ButtonRelease(const int, const int, const int, const QString&)));
160 QObject::connect(eventInter, SIGNAL(CursorMove(int,int,QString)), q, SLOT(_q_CursorMove(const int, const int, const QString&)));
161 QObject::connect(eventInter, SIGNAL(CursorInto(int,int,QString)), q, SLOT(_q_CursorEnter(const int, const int, const QString&)));
162 QObject::connect(eventInter, SIGNAL(CursorOut(int,int,QString)), q, SLOT(_q_CursorLeave(const int, const int, const QString&)));
163 QObject::connect(eventInter, SIGNAL(KeyPress(QString,int,int,QString)), q, SLOT(_q_KeyPress(const QString&, const int, const int, const QString&)));
164 QObject::connect(eventInter, SIGNAL(KeyRelease(QString,int,int,QString)), q, SLOT(_q_KeyRelease(const QString&, const int, const int, const QString&)));
165 }
166
167 void DRegionMonitorPrivate::registerMonitorRegion()
168 {
169 if (registered())
170 unregisterMonitorRegion();
171
172 if (watchedRegion.isEmpty())
173 {
174 // 将监听区域设置为最大
175 registerKey = eventInter->RegisterArea(INT_MIN, INT_MIN, INT_MAX, INT_MAX, registerdFlags);
176 } else {
177 const QRect rect = watchedRegion.boundingRect();
178 const int x1 = rect.x();
179 const int y1 = rect.y();
180 const int x2 = x1 + rect.width();
181 const int y2 = y1 + rect.height();
182
183 registerKey = eventInter->RegisterArea(x1, y1, x2, y2, registerdFlags);
184 }
185 }
186
187 void DRegionMonitorPrivate::unregisterMonitorRegion()
188 {
189 if (registerKey.isEmpty())
190 return;
191
192 eventInter->UnregisterArea(registerKey);
193 registerKey.clear();
194 }
195
196 void DRegionMonitorPrivate::_q_ButtonPress(const int flag, const int x, const int y, const QString &key)
197 {
198 if (registerKey != key)
199 return;
200
201 D_Q(DRegionMonitor);
202
203 Q_EMIT q->buttonPress(deviceScaledCoordinate(QPoint(x, y), qApp->devicePixelRatio()), flag);
204 }
205
206 void DRegionMonitorPrivate::_q_ButtonRelease(const int flag, const int x, const int y, const QString &key)
207 {
208 if (registerKey != key)
209 return;
210
211 D_Q(DRegionMonitor);
212
213 Q_EMIT q->buttonRelease(deviceScaledCoordinate(QPoint(x, y), qApp->devicePixelRatio()), flag);
214 }
215
216 void DRegionMonitorPrivate::_q_CursorMove(const int x, const int y, const QString &key)
217 {
218 if (registerKey != key)
219 return;
220
221 D_Q(DRegionMonitor);
222
223 Q_EMIT q->cursorMove(deviceScaledCoordinate(QPoint(x, y), qApp->devicePixelRatio()));
224 }
225
226 void DRegionMonitorPrivate::_q_CursorEnter(const int x, const int y, const QString &key)
227 {
228 if (registerKey != key)
229 return;
230
231 D_Q(DRegionMonitor);
232
233 Q_EMIT q->cursorEnter(deviceScaledCoordinate(QPoint(x, y), qApp->devicePixelRatio()));
234 }
235
236 void DRegionMonitorPrivate::_q_CursorLeave(const int x, const int y, const QString &key)
237 {
238 if (registerKey != key)
239 return;
240
241 D_Q(DRegionMonitor);
242
243 Q_EMIT q->cursorLeave(deviceScaledCoordinate(QPoint(x, y), qApp->devicePixelRatio()));
244 }
245
246 void DRegionMonitorPrivate::_q_KeyPress(const QString &keyname, const int x, const int y, const QString &key)
247 {
248 if (registerKey != key)
249 return;
250
251 Q_UNUSED(x);
252 Q_UNUSED(y);
253
254 D_Q(DRegionMonitor);
255
256 Q_EMIT q->keyPress(keyname);
257 }
258
259 void DRegionMonitorPrivate::_q_KeyRelease(const QString &keyname, const int x, const int y, const QString &key)
260 {
261 if (registerKey != key)
262 return;
263
264 Q_UNUSED(x);
265 Q_UNUSED(y);
266
267 D_Q(DRegionMonitor);
268
269 Q_EMIT q->keyRelease(keyname);
270 }
271
272 const QPoint DRegionMonitorPrivate::deviceScaledCoordinate(const QPoint &p, const double ratio) const
273 {
274 D_QC(DRegionMonitor);
275
276 if (type == q->Original) {
277 return p;
278 }
279
280 for (const auto *s : qApp->screens())
281 {
282 const QRect &g(s->geometry());
283 const QRect realRect(g.topLeft(), g.size() * ratio);
284
285 if (realRect.contains(p))
286 return QPoint(realRect.topLeft() + (p - realRect.topLeft()) / ratio);
287 }
288
289 return p / ratio;
290 }
291
292 DGUI_END_NAMESPACE
293
294 #include "moc_dregionmonitor.cpp"