forked from linuxdeepin/dde-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginmanagerextension.cpp
More file actions
763 lines (640 loc) · 21.6 KB
/
pluginmanagerextension.cpp
File metadata and controls
763 lines (640 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "pluginmanagerextension_p.h"
#include "pluginmanagerintegration_p.h"
#include "constants.h"
#include <DGuiApplicationHelper>
#include <DPlatformTheme>
#include <cstdint>
#include <QtWaylandCompositor/QWaylandSurface>
#include <QtWaylandCompositor/QWaylandResource>
#include <QtWaylandCompositor/QWaylandCompositor>
#include <QtWaylandCompositor/QWaylandView>
#include <QtWaylandCompositor/QWaylandBufferRef>
#include <QJsonObject>
#include <QJsonParseError>
#include <QGuiApplication>
#include <QCursor>
#include <QPixmap>
#define protected public
#include <private/qwaylandcompositor_p.h>
#include <private/qwaylandsurface_p.h>
#undef protected
#include <qpa/qwindowsysteminterface_p.h>
#ifdef USE_DEEPIN_QT
#include <private/qwlqtkey_p.h>
#include <private/qwlqttouch_p.h>
#endif
DGUI_USE_NAMESPACE
PluginScaleManager::PluginScaleManager(QWaylandCompositor *compositor)
: QWaylandCompositorExtensionTemplate(compositor)
, m_compositor(compositor)
{
}
void PluginScaleManager::setPluginScale(const uint32_t &scale)
{
if (scale == m_scale)
return;
m_scale = scale;
if (!m_compositor)
return;
auto outputs = m_compositor->outputs();
std::for_each(outputs.begin(), outputs.end(), [this](auto *output) {
// 120 is base of fractional scale.
output->setScaleFactor(std::ceil(m_scale / 120.0));
});
Q_EMIT pluginScaleChanged(m_scale);
}
uint32_t PluginScaleManager::pluginScale()
{
return m_scale;
}
void PluginScaleManager::initialize()
{
QWaylandCompositorExtensionTemplate::initialize();
QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
Q_ASSERT(compositor);
init(compositor->display(), 1);
m_compositor = compositor;
connect(compositor, &QWaylandCompositor::outputAdded, this, [this](auto *output) {
output->setScaleFactor(std::ceil(m_scale / 120.0));
});
}
void PluginScaleManager::wp_fractional_scale_manager_v1_get_fractional_scale(Resource *resource, uint32_t id, struct ::wl_resource *surface)
{
QWaylandSurface *qwaylandSurface = QWaylandSurface::fromResource(surface);
QWaylandResource shellSurfaceResource(
wl_resource_create(resource->client(), &::wp_fractional_scale_v1_interface, wl_resource_get_version(resource->handle), id));
auto pluginScale = new PluginScale(this, qwaylandSurface, shellSurfaceResource);
pluginScale->send_preferred_scale(m_scale);
}
PluginScale::PluginScale(PluginScaleManager *manager, QWaylandSurface *surface, const QWaylandResource &resource)
{
setParent(manager);
init(resource.resource());
setExtensionContainer(surface);
QWaylandCompositorExtension::initialize();
connect(manager, &PluginScaleManager::pluginScaleChanged, this, [this](uint32_t scale) {
send_preferred_scale(scale);
});
}
void PluginScale::wp_fractional_scale_v1_destroy(Resource *resource)
{
Q_UNUSED(resource)
deleteLater();
}
PluginSurface::PluginSurface(PluginManager *manager,
const QString &pluginId,
const QString &itemKey,
const QString &displayName,
int pluginFlags,
int pluginType,
int sizePolicy,
QWaylandSurface *surface,
const QWaylandResource &resource)
: m_manager(manager)
, m_surface(surface)
, m_itemKey(itemKey)
, m_pluginId(pluginId)
, m_displayName(displayName)
, m_flags(pluginFlags)
, m_pluginType(pluginType)
, m_sizePolicy(sizePolicy)
, m_margins(0)
, m_height(1)
, m_width(1)
{
init(resource.resource());
setExtensionContainer(surface);
QWaylandCompositorExtension::initialize();
}
QWaylandQuickShellIntegration* PluginSurface::createIntegration(QWaylandQuickShellSurfaceItem *item)
{
return new PluginManagerIntegration(item);
}
QWaylandSurface* PluginSurface::surface() const
{
return m_surface;
}
QString PluginSurface::pluginId() const
{
return m_pluginId;
}
QString PluginSurface::itemKey() const
{
return m_itemKey;
}
QString PluginSurface::displayName() const
{
return m_displayName;
}
uint32_t PluginSurface::pluginType() const
{
return m_pluginType;
}
uint32_t PluginSurface::pluginFlags() const
{
return m_flags;
}
uint32_t PluginSurface::pluginSizePolicy () const
{
return m_sizePolicy;
}
int PluginSurface::height() const
{
return m_height;
}
int PluginSurface::width() const
{
return m_width;
}
QString PluginSurface::dccIcon() const
{
return m_dccIcon;
}
void PluginSurface::setItemActive(bool isActive)
{
if (m_isItemActive == isActive) {
return;
}
m_isItemActive = isActive;
Q_EMIT itemActiveChanged();
}
bool PluginSurface::isItemActive() const
{
return m_isItemActive;
}
void PluginSurface::updatePluginGeometry(const QRect &geometry)
{
send_geometry(geometry.x(), geometry.y(), geometry.width(), geometry.height());
}
void PluginSurface::plugin_mouse_event(QtWaylandServer::plugin::Resource *resource, int32_t type)
{
Q_UNUSED(resource)
qInfo() << "server plugin surface receive mouse event:" << type;
Q_EMIT recvMouseEvent((QEvent::Type)type);
}
void PluginSurface::plugin_dcc_icon(Resource *resource, const QString &icon)
{
Q_UNUSED(resource)
qInfo() << "dcc_icon:" << icon;
m_dccIcon = icon;
}
void PluginSurface::plugin_request_shutdown(Resource *resource, const QString &type)
{
Q_UNUSED(resource);
Q_EMIT m_manager->requestShutdown(type);
}
void PluginSurface::plugin_close_quick_panel(Resource *resource) {
Q_UNUSED(resource)
qInfo() << "close_quick_panel";
Q_EMIT m_manager->pluginCloseQuickPanelPopup();
}
void PluginSurface::plugin_destroy_resource(Resource *resource)
{
Q_UNUSED(resource);
Q_EMIT aboutToDestroy();
m_manager->removePluginSurface(this);
delete this;
}
void PluginSurface::plugin_destroy(Resource *resource)
{
wl_resource_destroy(resource->handle);
}
void PluginSurface::setGlobalPos(const QPoint &pos)
{
QRect g = qApp->primaryScreen() ? qApp->primaryScreen()->geometry() : QRect();
for (auto *screen : qApp->screens())
{
const QRect &sg = screen->geometry();
if (sg.contains(pos))
{
g = sg;
break;
}
}
auto p = g.topLeft() + (pos - g.topLeft()) * qApp->devicePixelRatio();
send_raw_global_pos(p.x(), p.y());
}
void PluginSurface::plugin_source_size(Resource *resource, int32_t width, int32_t height)
{
Q_UNUSED(resource);
if (width == 0 || height == 0)
return;
if (height != m_height) {
m_height = height;
Q_EMIT heightChanged();
}
if (width != m_width) {
m_width = width;
Q_EMIT widthChanged();
}
}
PluginPopup::PluginPopup(PluginManager *manager,
const QString &pluginId,
const QString &itemKey,
int x,
int y,
int popupType,
QWaylandSurface *surface,
const QWaylandResource &resource)
: m_manager(manager)
, m_surface(surface)
, m_itemKey(itemKey)
, m_pluginId(pluginId)
, m_popupType(popupType)
, m_height(1)
, m_width(1)
{
Q_UNUSED(x)
Q_UNUSED(y)
init(resource.resource());
setExtensionContainer(surface);
QWaylandCompositorExtension::initialize();
}
QWaylandSurface* PluginPopup::surface() const
{
return m_surface;
}
QWaylandQuickShellIntegration* PluginPopup::createIntegration(QWaylandQuickShellSurfaceItem *item)
{
return new PluginPopupIntegration(item);
}
void PluginPopup::updatePluginGeometry(const QRect &geometry)
{
send_geometry(geometry.x(), geometry.y(), geometry.width(), geometry.height());
}
void PluginPopup::setEmbedPanelMinHeight(int height)
{
m_manager->setEmbedPanelMinHeight(height);
}
QString PluginPopup::pluginId() const
{
return m_pluginId;
}
QString PluginPopup::itemKey() const
{
return m_itemKey;
}
int32_t PluginPopup::x() const
{
return m_x;
}
int32_t PluginPopup::y() const
{
return m_y;
}
void PluginPopup::setX(int32_t x)
{
m_x = x;
Q_EMIT xChanged();
}
void PluginPopup::setY(int32_t y)
{
m_y = y;
Q_EMIT yChanged();
}
int PluginPopup::height() const
{
return m_height;
}
int PluginPopup::width() const
{
return m_width;
}
int32_t PluginPopup::popupType() const
{
return m_popupType;
}
void PluginPopup::plugin_popup_set_position(Resource *resource, int32_t x, int32_t y)
{
Q_UNUSED(resource)
setX(x);
setY(y);
}
void PluginPopup::plugin_popup_destroy_resource(Resource *resource)
{
Q_UNUSED(resource);
// TODO why we get a same address with the object when new PluginPopup, if we delete the object.
Q_EMIT aboutToDestroy();
delete this;
}
void PluginPopup::plugin_popup_destroy(Resource *resource)
{
wl_resource_destroy(resource->handle);
}
void PluginPopup::plugin_popup_source_size(Resource *resource, int32_t width, int32_t height)
{
Q_UNUSED(resource);
if (width == 0 || height == 0)
return;
if (height != m_height) {
m_height = height;
Q_EMIT heightChanged();
}
if (width != m_width) {
m_width = width;
Q_EMIT widthChanged();
}
}
PluginManager::PluginManager(QWaylandCompositor *compositor)
: QWaylandCompositorExtensionTemplate(compositor)
{
auto theme = DGuiApplicationHelper::instance()->applicationTheme();
QObject::connect(theme, &DPlatformTheme::fontNameChanged, this, &PluginManager::onFontChanged);
QObject::connect(theme, &DPlatformTheme::fontPointSizeChanged, this, &PluginManager::onFontChanged);
QObject::connect(theme, &DPlatformTheme::activeColorChanged, this, &PluginManager::onActiveColorChanged);
QObject::connect(theme, &DPlatformTheme::darkActiveColorChanged, this, &PluginManager::onActiveColorChanged);
QObject::connect(theme, &DPlatformTheme::themeNameChanged, this, &PluginManager::onThemeChanged);
QObject::connect(theme, &DPlatformTheme::iconThemeNameChanged, this, &PluginManager::onThemeChanged);
}
void PluginManager::initialize()
{
QWaylandCompositorExtensionTemplate::initialize();
QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
// ###(zccrs): 在dde-shell中不要使用QWaylandCompositor的event handler,它会对key event进行
// 特殊处理,会丢弃掉原生事件的信息,仅根据 native scan key code 通过xkb生成原始key event向
// dde-shell 传递,这会导致需要状态切换后进行输入的字符丢失信息,比如外部环境打开NumLock后,小键盘
// 的数字输入会被event handler转成原始事件,丢失了NumLock的开关信息。
// dde-shell不是一个独立的合成器,所以不需要额外处理key event,需要遵守原始事件中的NumLock状态
// 确保输出数字时在dde-shell的wayland客户端中接收到的也是数字。
auto eventHandler = QWaylandCompositorPrivate::get(compositor)->eventHandler.get();
if (eventHandler == QWindowSystemInterfacePrivate::eventHandler) {
QWindowSystemInterfacePrivate::removeWindowSystemEventhandler(eventHandler);
}
// TODO: Use built-in protocol instead of qt
#ifdef USE_DEEPIN_QT
// 只创建就行
auto qtKey = new QtWayland::QtKeyExtensionGlobal(compositor);
qtKey->setParent(compositor);
auto qtTouch = new QtWayland::TouchExtensionGlobal(compositor);
qtTouch->setParent(compositor);
#endif
init(compositor->display(), 1);
// 设置鼠标焦点监听器
setupMouseFocusListener();
}
void PluginManager::updateDockOverflowState(int state)
{
QJsonObject obj;
obj[dock::MSG_TYPE] = dock::MSG_UPDATE_OVERFLOW_STATE;
obj[dock::MSG_DATA] = state;
sendEventMsg(toJson(obj));
}
void PluginManager::setPopupMinHeight(int height)
{
setEmbedPanelMinHeight(height);
}
uint32_t PluginManager::dockPosition() const
{
return m_dockPosition;
}
void PluginManager::setDockPosition(uint32_t dockPosition)
{
if (m_dockPosition == dockPosition)
return;
m_dockPosition = dockPosition;
foreach (PluginSurface *plugin, m_pluginSurfaces) {
Resource *target = resourceMap().value(plugin->surface()->waylandClient());
if (target) {
send_position_changed(target->handle, m_dockPosition);
}
}
}
uint32_t PluginManager::dockColorTheme() const
{
return m_dockColorTheme;
}
void PluginManager::setDockColorTheme(uint32_t type)
{
if (type == m_dockColorTheme)
return;
m_dockColorTheme = type;
foreach (PluginSurface *plugin, m_pluginSurfaces) {
Resource *target = resourceMap().value(plugin->surface()->waylandClient());
if (target) {
send_color_theme_changed(target->handle, m_dockColorTheme);
}
}
}
void PluginManager::setEmbedPanelMinHeight(int height)
{
if (m_popupMinHeight == height)
return;
m_popupMinHeight = height;
sendEventMsg(popupMinHeightMsg());
}
void PluginManager::plugin_manager_v1_request_message(Resource *resource, const QString &plugin_id, const QString &item_key, const QString &msg)
{
Q_UNUSED(resource)
qInfo() << "server pluginManager receive client:" << plugin_id << item_key << " msg:" << msg;
PluginSurface *dstPlugin = nullptr;
for (PluginSurface *plugin : m_pluginSurfaces) {
if (plugin->pluginId() == plugin_id && plugin->itemKey() == item_key) {
dstPlugin = plugin;
break;
}
}
if (!dstPlugin) {
return;
}
auto rootObj = getRootObj(msg);
if (rootObj.isEmpty()) {
return;
}
const auto &msgType = rootObj.value(dock::MSG_TYPE);
if (msgType == dock::MSG_SUPPORT_FLAG_CHANGED) {
// todo
} else if (msgType == dock::MSG_ITEM_ACTIVE_STATE) {
dstPlugin->setItemActive(rootObj.value(dock::MSG_DATA).toBool());
} else if (msgType == dock::MSG_UPDATE_TOOLTIPS_VISIBLE) {
}
}
void PluginManager::plugin_manager_v1_create_plugin(Resource *resource, const QString &pluginId, const QString &itemKey, const QString &display_name, int32_t plugin_flags, int32_t type, int32_t size_policy, struct ::wl_resource *surface, uint32_t id)
{
QWaylandSurface *qwaylandSurface = QWaylandSurface::fromResource(surface);
QWaylandResource shellSurfaceResource(wl_resource_create(resource->client(), &::plugin_interface,
wl_resource_get_version(resource->handle), id));
send_position_changed(resource->handle, m_dockPosition);
send_color_theme_changed(resource->handle, m_dockColorTheme);
auto theme = DGuiApplicationHelper::instance()->applicationTheme();
send_active_color_changed(resource->handle, theme->activeColor().name(), theme->darkActiveColor().name());
send_font_changed(resource->handle, theme->fontName(), theme->fontPointSize());
send_theme_changed(resource->handle, theme->themeName(), theme->iconThemeName());
auto plugin = new PluginSurface(this, pluginId, itemKey, display_name, plugin_flags, type, size_policy, qwaylandSurface, shellSurfaceResource);
m_pluginSurfaces << plugin;
Q_EMIT pluginSurfaceCreated(plugin);
sendEventMsg(resource, dockSizeMsg());
sendEventMsg(resource, popupMinHeightMsg());
}
void PluginManager::plugin_manager_v1_create_popup_at(Resource *resource, const QString &pluginId, const QString &itemKey, int32_t type, int32_t x, int32_t y, struct ::wl_resource *surface, uint32_t id)
{
QWaylandSurface *qwaylandSurface = QWaylandSurface::fromResource(surface);
QWaylandResource shellSurfaceResource(wl_resource_create(resource->client(), &::plugin_popup_interface,
wl_resource_get_version(resource->handle), id));
auto plugin = new PluginPopup(this, pluginId, itemKey, x, y, type, qwaylandSurface, shellSurfaceResource);
plugin->setX(x), plugin->setY(y);
Q_EMIT pluginPopupCreated(plugin);
}
QJsonObject PluginManager::getRootObj(const QString &jsonStr) {
QJsonParseError jsonParseError;
const QJsonDocument &resultDoc = QJsonDocument::fromJson(jsonStr.toLocal8Bit(), &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError || resultDoc.isEmpty()) {
qWarning() << "Result json parse error";
return QJsonObject();
}
return resultDoc.object();
}
QString PluginManager::toJson(const QJsonObject &jsonObj)
{
QJsonDocument doc;
doc.setObject(jsonObj);
return doc.toJson();
}
void PluginManager::sendEventMsg(const QString &msg)
{
foreach (PluginSurface *plugin, m_pluginSurfaces) {
Resource *target = resourceMap().value(plugin->surface()->waylandClient());
sendEventMsg(target, msg);
}
}
void PluginManager::sendEventMsg(Resource *target, const QString &msg)
{
if (target && !msg.isEmpty()) {
send_event_message(target->handle, msg);
}
}
int PluginSurface::margins() const
{
return m_margins;
}
void PluginSurface::setMargins(int newMargins)
{
if (m_margins == newMargins)
return;
m_margins = newMargins;
send_margin(m_margins);
emit marginsChanged();
}
QSize PluginManager::dockSize() const
{
return m_dockSize;
}
void PluginManager::setDockSize(const QSize &newDockSize)
{
if (m_dockSize == newDockSize)
return;
m_dockSize = newDockSize;
sendEventMsg(dockSizeMsg());
emit dockSizeChanged();
}
void PluginManager::removePluginSurface(PluginSurface *plugin)
{
Q_EMIT pluginSurfaceDestroyed(plugin);
m_pluginSurfaces.removeAll(plugin);
}
void PluginManager::onFontChanged()
{
foreachPluginSurface([this](Resource *source) {
auto theme = DGuiApplicationHelper::instance()->applicationTheme();
send_font_changed(source->handle, theme->fontName(), theme->fontPointSize());
});
}
void PluginManager::onActiveColorChanged()
{
foreachPluginSurface([this](Resource *source) {
auto theme = DGuiApplicationHelper::instance()->applicationTheme();
send_active_color_changed(source->handle, theme->activeColor().name(), theme->darkActiveColor().name());
});
}
void PluginManager::onThemeChanged()
{
foreachPluginSurface([this](Resource *source) {
auto theme = DGuiApplicationHelper::instance()->applicationTheme();
send_theme_changed(source->handle, theme->themeName(), theme->iconThemeName());
});
}
void PluginManager::foreachPluginSurface(PluginSurfaceCallback callback)
{
foreach (PluginSurface *plugin, m_pluginSurfaces) {
Resource *target = resourceMap().value(plugin->surface()->waylandClient());
if (target) {
callback(target);
}
}
}
QString PluginManager::dockSizeMsg() const
{
if (m_dockSize.isEmpty())
return QString();
QJsonObject sizeData;
sizeData["width"] = m_dockSize.width();
sizeData["height"] = m_dockSize.height();
QJsonObject obj;
obj[dock::MSG_TYPE] = dock::MSG_DOCK_PANEL_SIZE_CHANGED;
obj[dock::MSG_DATA] = sizeData;
return toJson(obj);
}
QString PluginManager::popupMinHeightMsg() const
{
if (m_popupMinHeight <= 0)
return QString();
QJsonObject obj;
obj[dock::MSG_TYPE] = dock::MSG_SET_APPLET_MIN_HEIGHT;
obj[dock::MSG_DATA] = m_popupMinHeight;
return toJson(obj);
}
void PluginManager::setupMouseFocusListener()
{
QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
if (!compositor)
return;
QWaylandSeat *seat = compositor->defaultSeat();
if (!seat)
return;
QObject::connect(seat, &QWaylandSeat::mouseFocusChanged, this,
[seat](QWaylandView *newFocus, QWaylandView *oldFocus) {
// Restore default cursor when mouse leaves all plugin areas
if (!newFocus && oldFocus) {
qApp->restoreOverrideCursor();
}
if (newFocus) {
if (auto surface = newFocus->surface()) {
seat->setKeyboardFocus(surface);
}
}
});
// Handle client cursor requests and apply cursor changes to the host window
QObject::connect(seat, &QWaylandSeat::cursorSurfaceRequested, this,
[this](QWaylandSurface *surface, int hotspotX, int hotspotY) {
// Disconnect previous connection
if (m_cursorSurfaceConn) {
QObject::disconnect(m_cursorSurfaceConn);
m_cursorSurfaceConn = {};
}
if (!surface) {
qApp->restoreOverrideCursor();
return;
}
auto updateCursor = [surface, hotspotX, hotspotY]() {
QWaylandSurfacePrivate *surf = QWaylandSurfacePrivate::get(surface);
QWaylandBufferRef buf = surf->bufferRef;
if (!buf.hasBuffer()) {
return;
}
QImage image = buf.image();
if (!image.isNull() && image.width() > 0 && image.height() > 0) {
QPixmap pixmap = QPixmap::fromImage(image);
QCursor cursor(QPixmap::fromImage(image), hotspotX, hotspotY);
if (qApp->overrideCursor()) {
qApp->changeOverrideCursor(cursor);
} else {
qApp->setOverrideCursor(cursor);
}
}
};
// Listen to surface redraw signal to grab cursor after buffer is updated
m_cursorSurfaceConn = QObject::connect(surface, &QWaylandSurface::redraw, updateCursor);
});
}