| 1 |
#include "ukuidisplay_p.h" |
| 2 |
|
| 3 |
#include <QDebug> |
| 4 |
|
| 5 |
#include <QGuiApplication> |
| 6 |
|
| 7 |
#include <QtGui/qpa/qplatformnativeinterface.h> |
| 8 |
|
| 9 |
static UkuiDisplay *s_ukuiDisplay = nullptr; |
| 10 |
UkuiDisplay *ukuiDisplay() |
| 11 |
{ |
| 12 |
if (!s_ukuiDisplay) { |
| 13 |
s_ukuiDisplay = new UkuiDisplay(); |
| 14 |
} |
| 15 |
|
| 16 |
return s_ukuiDisplay; |
| 17 |
} |
| 18 |
|
| 19 |
UkuiDisplay::UkuiDisplay(QObject *parent) |
| 20 |
: QObject(parent) |
| 21 |
{ |
| 22 |
m_initialized = init(); |
| 23 |
} |
| 24 |
|
| 25 |
UkuiDisplay::~UkuiDisplay() |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
bool UkuiDisplay::init() |
| 30 |
{ |
| 31 |
if (QGuiApplication::platformName() != "wayland") { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
QPlatformNativeInterface *native = qApp->platformNativeInterface(); |
| 36 |
if (!native) { |
| 37 |
qWarning() << "Failed to get QPlatformNativeInterface"; |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
auto display = static_cast<wl_display *>(native->nativeResourceForIntegration("wl_display")); |
| 42 |
if (!display) { |
| 43 |
qWarning() << "Failed to get native display"; |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
m_display = std::make_unique<wayland::display_t>(display); |
| 48 |
if (!m_display) { |
| 49 |
qWarning() << "Failed to get wl_display"; |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
m_seat = wayland::seat_t(static_cast<wl_seat *>(native->nativeResourceForIntegration("wl_seat")), wayland::proxy_t::wrapper_type::foreign); |
| 54 |
if (!m_seat) { |
| 55 |
qWarning() << "Failed to get wl_seat"; |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
m_registry = m_display->get_registry(); |
| 60 |
if (!m_registry) { |
| 61 |
qWarning() << "Failed to get wl_registry"; |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
m_registry.on_global() = std::bind(&UkuiDisplay::handle_global, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); |
| 66 |
m_registry.on_global_remove() = std::bind(&UkuiDisplay::handle_global_remove, this, std::placeholders::_1); |
| 67 |
|
| 68 |
// roundtrip to get all global interfaces |
| 69 |
m_display->roundtrip(); |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
bool UkuiDisplay::isInitialized() const |
| 75 |
{ |
| 76 |
return m_initialized; |
| 77 |
} |
| 78 |
|
| 79 |
const wayland::display_t &UkuiDisplay::display() const |
| 80 |
{ |
| 81 |
return *m_display; |
| 82 |
} |
| 83 |
|
| 84 |
const wayland::seat_t &UkuiDisplay::seat() const |
| 85 |
{ |
| 86 |
return m_seat; |
| 87 |
} |
| 88 |
|
| 89 |
void UkuiDisplay::handle_global(uint32_t name, const std::string &interface, uint32_t version) |
| 90 |
{ |
| 91 |
m_globalInterfaces.append({name, interface, version}); |
| 92 |
} |
| 93 |
|
| 94 |
void UkuiDisplay::handle_global_remove(uint32_t name) |
| 95 |
{ |
| 96 |
for (int i = 0; i < m_globalInterfaces.size(); ++i) { |
| 97 |
if (m_globalInterfaces.at(i).name == name) { |
| 98 |
m_globalInterfaces.removeAt(i); |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
QList<UkuiDisplay::WaylandInterface> UkuiDisplay::globalInterfaces() |
| 105 |
{ |
| 106 |
return m_globalInterfaces; |
| 107 |
} |
| 108 |
|
| 109 |
wayland::proxy_t UkuiDisplay::bind(const std::string &name, wayland::proxy_t &proxy, uint32_t minVersion) |
| 110 |
{ |
| 111 |
if (QGuiApplication::platformName() != "wayland") { |
| 112 |
return nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
if (!isInitialized()) { |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
|
| 119 |
for (const WaylandInterface &interface : m_globalInterfaces) { |
| 120 |
if (interface.interface == name) { |
| 121 |
return m_registry.bind(interface.name, proxy, qMax(interface.version, minVersion)); |
| 122 |
} |
| 123 |
} |
| 124 |
return nullptr; |
| 125 |
} |