2020-11-11 23:18:18 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "platform/linux/linux_wayland_integration.h"
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
#include "base/platform/linux/base_linux_wayland_utilities.h"
|
2020-11-11 23:18:18 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2022-06-03 16:09:17 +00:00
|
|
|
#include "base/qt_signal_producer.h"
|
|
|
|
#include "base/flat_map.h"
|
|
|
|
#include "qwayland-plasma-shell.h"
|
2020-11-11 23:18:18 +00:00
|
|
|
|
2022-06-03 16:09:17 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
#include <QtGui/QWindow>
|
2023-05-25 05:36:30 +00:00
|
|
|
#include <qpa/qplatformnativeinterface.h>
|
2023-05-13 19:31:20 +00:00
|
|
|
#include <qpa/qplatformwindow_p.h>
|
2022-06-03 16:09:17 +00:00
|
|
|
#include <wayland-client.h>
|
2020-11-11 23:18:18 +00:00
|
|
|
|
2023-05-13 19:31:20 +00:00
|
|
|
using namespace QNativeInterface;
|
|
|
|
using namespace QNativeInterface::Private;
|
2023-05-24 23:38:38 +00:00
|
|
|
using namespace base::Platform::Wayland;
|
2023-05-13 19:31:20 +00:00
|
|
|
|
2020-11-11 23:18:18 +00:00
|
|
|
namespace Platform {
|
|
|
|
namespace internal {
|
|
|
|
|
2021-07-25 21:30:56 +00:00
|
|
|
struct WaylandIntegration::Private {
|
2023-05-24 23:38:38 +00:00
|
|
|
QtWayland::org_kde_plasma_surface plasmaSurface(QWindow *window);
|
|
|
|
|
|
|
|
std::unique_ptr<wl_registry, RegistryDeleter> registry;
|
|
|
|
AutoDestroyer<QtWayland::org_kde_plasma_shell> plasmaShell;
|
2022-06-03 16:09:17 +00:00
|
|
|
uint32_t plasmaShellName = 0;
|
2023-05-24 23:38:38 +00:00
|
|
|
base::flat_map<
|
|
|
|
wl_surface*,
|
|
|
|
AutoDestroyer<QtWayland::org_kde_plasma_surface>
|
|
|
|
> plasmaSurfaces;
|
2022-06-03 16:09:17 +00:00
|
|
|
rpl::lifetime lifetime;
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
static const wl_registry_listener RegistryListener;
|
2021-01-02 01:06:11 +00:00
|
|
|
};
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
const wl_registry_listener WaylandIntegration::Private::RegistryListener = {
|
2022-06-03 16:09:17 +00:00
|
|
|
decltype(wl_registry_listener::global)(+[](
|
|
|
|
Private *data,
|
|
|
|
wl_registry *registry,
|
|
|
|
uint32_t name,
|
|
|
|
const char *interface,
|
|
|
|
uint32_t version) {
|
|
|
|
if (interface == qstr("org_kde_plasma_shell")) {
|
|
|
|
data->plasmaShell.init(registry, name, version);
|
|
|
|
data->plasmaShellName = name;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
decltype(wl_registry_listener::global_remove)(+[](
|
|
|
|
Private *data,
|
|
|
|
wl_registry *registry,
|
|
|
|
uint32_t name) {
|
|
|
|
if (name == data->plasmaShellName) {
|
2023-05-24 23:38:38 +00:00
|
|
|
data->plasmaShell = {};
|
2022-06-03 16:09:17 +00:00
|
|
|
data->plasmaShellName = 0;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
QtWayland::org_kde_plasma_surface WaylandIntegration::Private::plasmaSurface(
|
2022-06-03 16:09:17 +00:00
|
|
|
QWindow *window) {
|
|
|
|
if (!plasmaShell.isInitialized()) {
|
2023-05-24 23:38:38 +00:00
|
|
|
return {};
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 19:31:20 +00:00
|
|
|
const auto native = window->nativeInterface<QWaylandWindow>();
|
2022-06-03 16:09:17 +00:00
|
|
|
if (!native) {
|
2023-05-24 23:38:38 +00:00
|
|
|
return {};
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 19:31:20 +00:00
|
|
|
const auto surface = native->surface();
|
2022-06-03 16:09:17 +00:00
|
|
|
if (!surface) {
|
2023-05-24 23:38:38 +00:00
|
|
|
return {};
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto it = plasmaSurfaces.find(surface);
|
|
|
|
if (it != plasmaSurfaces.cend()) {
|
2023-05-24 23:38:38 +00:00
|
|
|
return it->second;
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
const auto plasmaSurface = plasmaShell.get_surface(surface);
|
|
|
|
if (!plasmaSurface) {
|
|
|
|
return {};
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
const auto result = plasmaSurfaces.emplace(surface, plasmaSurface);
|
2022-06-03 16:09:17 +00:00
|
|
|
|
|
|
|
base::qt_signal_producer(
|
2023-05-13 19:31:20 +00:00
|
|
|
native,
|
|
|
|
&QWaylandWindow::surfaceDestroyed
|
2022-06-03 16:09:17 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
auto it = plasmaSurfaces.find(surface);
|
|
|
|
if (it != plasmaSurfaces.cend()) {
|
|
|
|
plasmaSurfaces.erase(it);
|
|
|
|
}
|
|
|
|
}, lifetime);
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
return result.first->second;
|
2022-06-03 16:09:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:30:56 +00:00
|
|
|
WaylandIntegration::WaylandIntegration()
|
|
|
|
: _private(std::make_unique<Private>()) {
|
2023-05-13 19:31:20 +00:00
|
|
|
const auto native = qApp->nativeInterface<QWaylandApplication>();
|
2022-06-03 16:09:17 +00:00
|
|
|
if (!native) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:31:20 +00:00
|
|
|
const auto display = native->display();
|
2022-06-03 16:09:17 +00:00
|
|
|
if (!display) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_private->registry.reset(wl_display_get_registry(display));
|
|
|
|
wl_registry_add_listener(
|
|
|
|
_private->registry.get(),
|
|
|
|
&Private::RegistryListener,
|
|
|
|
_private.get());
|
|
|
|
|
2022-11-11 04:23:15 +00:00
|
|
|
wl_display_roundtrip(display);
|
2021-01-02 01:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WaylandIntegration::~WaylandIntegration() = default;
|
|
|
|
|
2020-11-11 23:18:18 +00:00
|
|
|
WaylandIntegration *WaylandIntegration::Instance() {
|
2020-11-13 16:05:54 +00:00
|
|
|
if (!IsWayland()) return nullptr;
|
2023-05-25 05:36:30 +00:00
|
|
|
static std::optional<WaylandIntegration> instance(std::in_place);
|
|
|
|
base::qt_signal_producer(
|
|
|
|
QGuiApplication::platformNativeInterface(),
|
|
|
|
&QObject::destroyed
|
|
|
|
) | rpl::start_with_next([&] {
|
|
|
|
instance = std::nullopt;
|
|
|
|
}, instance->_private->lifetime);
|
|
|
|
if (!instance) return nullptr;
|
|
|
|
return &*instance;
|
2020-11-11 23:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 02:38:30 +00:00
|
|
|
bool WaylandIntegration::skipTaskbarSupported() {
|
2022-06-03 16:09:17 +00:00
|
|
|
return _private->plasmaShell.isInitialized();
|
2021-05-13 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandIntegration::skipTaskbar(QWindow *window, bool skip) {
|
2022-06-03 16:09:17 +00:00
|
|
|
auto plasmaSurface = _private->plasmaSurface(window);
|
2023-05-24 23:38:38 +00:00
|
|
|
if (!plasmaSurface.isInitialized()) {
|
2021-05-13 02:38:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-24 23:38:38 +00:00
|
|
|
plasmaSurface.set_skip_taskbar(skip);
|
2021-05-13 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:18:18 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace Platform
|