| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik |
| | | 2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| | | 3 | | |
| | | 4 | | // Runtime-level Qt glue: native platform handles and the event-loop pump that |
| | | 5 | | // drains Rust's user-event queue into the sink installed by `run()` |
| | | 6 | | // (see src/ops/runtime.rs for the Rust half and src/qt_runtime.hpp for the |
| | | 7 | | // threading contract). |
| | | 8 | | |
| | | 9 | | #include "qt_runtime.hpp" |
| | | 10 | | |
| | | 11 | | #include "qt_window.hpp" // qt_window_winid: the widget-registry lookup |
| | | 12 | | |
| | | 13 | | // cxx-generated header of the bridge in `src/bridge/runtime.rs`; declares |
| | | 14 | | // `qt_on_pump_tick` and pulls in `qt_runtime.hpp`. |
| | | 15 | | #include "tauri-runtime-qt/src/bridge/runtime.rs.h" |
| | | 16 | | |
| | | 17 | | #include <QtCore/QByteArray> |
| | | 18 | | #include <QtCore/QCoreApplication> |
| | | 19 | | #include <QtCore/QMetaObject> |
| | | 20 | | #include <QtCore/QPointer> |
| | | 21 | | #include <QtCore/QSemaphore> |
| | | 22 | | #include <QtCore/QString> |
| | | 23 | | #include <QtCore/QThread> |
| | | 24 | | #include <QtCore/QTimer> |
| | | 25 | | #include <QtGui/QGuiApplication> |
| | | 26 | | #include <QtGui/qguiapplication_platform.h> |
| | | 27 | | #include <QtWidgets/QApplication> |
| | | 28 | | #include <QtWidgets/QWidget> |
| | | 29 | | |
| | | 30 | | #include <csignal> |
| | | 31 | | #include <cstddef> |
| | | 32 | | #include <cstdint> |
| | | 33 | | #include <cstdio> |
| | | 34 | | #include <memory> |
| | | 35 | | |
| | | 36 | | namespace { |
| | | 37 | | |
| | | 38 | | /// How long an off-GUI-thread read waits for the GUI thread before reporting |
| | | 39 | | /// failure (callers turn 0 into `HandleError::Unavailable`). The GUI thread |
| | | 40 | | /// runs the event loop, so it answers in microseconds unless the application is |
| | | 41 | | /// wedged. |
| | | 42 | | constexpr int kGuiThreadTimeoutMs = 5000; |
| | | 43 | | |
| | | 44 | | /// Pump cadence. Wake-ups posted by `qt_runtime_wake()` drain immediately; the |
| | | 45 | | /// timer is the safety net for events whose wake-up predates the |
| | | 46 | | /// `QCoreApplication` (they stay queued until the first tick). |
| | | 47 | | constexpr int kPumpIntervalMs = 16; |
| | | 48 | | |
| | | 49 | | QPointer<QTimer> g_pumpTimer; |
| | | 50 | | |
| | 40 | 51 | | QCoreApplication* application() { |
| | 40 | 52 | | return QCoreApplication::instance(); |
| | 40 | 53 | | } |
| | | 54 | | |
| | | 55 | | #ifdef TAURIQT_COVERAGE |
| | | 56 | | // Provided by the LLVM profile runtime rustc links for `-Cinstrument-coverage` |
| | | 57 | | // — the same runtime instance the clang-instrumented C++ translation units |
| | | 58 | | // resolve against (see build.rs), so one call flushes Rust and C++ alike. |
| | | 59 | | // Declared by hand: compiler-rt's InstrProfiling.h is not on the include path |
| | | 60 | | // of an ordinary Qt build. |
| | | 61 | | extern "C" int __llvm_profile_write_file(void); |
| | | 62 | | |
| | | 63 | | /// Flushes every instrumented translation unit and then dies of the signal with |
| | | 64 | | /// its default disposition, so the exit status still says "killed". |
| | | 65 | | /// |
| | | 66 | | /// Coverage runs are harness-driven: the harness stops the app (or the session |
| | | 67 | | /// it lives in) the moment it sees its verdict, and a signal taking the default |
| | | 68 | | /// action would skip the atexit hook the profile runtime installs, losing |
| | | 69 | | /// everything the run accumulated. Every signal that can end such a run is |
| | | 70 | | /// therefore armed with this flush: `SIGTERM`/`SIGINT` for an explicit stop, |
| | | 71 | | /// `SIGHUP`/`SIGQUIT`/`SIGPIPE` for a dying compositor or display connection, |
| | | 72 | | /// `SIGABRT` for a `qFatal()` in Qt. Coverage builds only. |
| | 0 | 73 | | void flushProfilesAndReraise(int signal_number) { |
| | 0 | 74 | | std::fprintf(stderr, "[tauri-runtime-qt] coverage: flushing the profile (signal %d)\n", |
| | 0 | 75 | | signal_number); |
| | 0 | 76 | | (void)__llvm_profile_write_file(); |
| | 0 | 77 | | std::signal(signal_number, SIG_DFL); |
| | 0 | 78 | | std::raise(signal_number); |
| | 0 | 79 | | } |
| | | 80 | | #endif |
| | | 81 | | |
| | | 82 | | /// `TAURIQT_EXIT_AFTER_MS=<n>`: leave the event loop through |
| | | 83 | | /// `QCoreApplication::exit(0)` n milliseconds after the pump starts, so a |
| | | 84 | | /// harness-driven run ends by itself and the profile runtime's atexit flush |
| | | 85 | | /// gets to run (`timeout`-killing the app would discard the profile). In a |
| | | 86 | | /// coverage build, arm the profile-flushing signal handlers as well. GUI thread |
| | | 87 | | /// only; unset or unparsable value: no timer, no handlers. |
| | 20 | 88 | | void installExitHooks(QCoreApplication* app) { |
| | 20 | 89 | | const QByteArray value = qgetenv("TAURIQT_EXIT_AFTER_MS"); |
| | 20 | 90 | | if (!value.isEmpty()) { |
| | 20 | 91 | | bool parsed = false; |
| | 20 | 92 | | const int after_ms = value.toInt(&parsed); |
| | 20 | 93 | | if (parsed && after_ms > 0) { |
| | 20 | 94 | | QTimer::singleShot(after_ms, app, []() { |
| | 1 | 95 | | std::fprintf(stderr, |
| | 1 | 96 | | "[tauri-runtime-qt] exit-after-ms: leaving the event loop\n"); |
| | 1 | 97 | | QCoreApplication::exit(0); |
| | 1 | 98 | | }); |
| | 20 | 99 | | } |
| | 20 | 100 | | } |
| | 20 | 101 | | #ifdef TAURIQT_COVERAGE |
| | 20 | 102 | | const int flush_signals[] = {SIGTERM, SIGINT, SIGHUP, SIGQUIT, SIGABRT, SIGPIPE}; |
| | 120 | 103 | | for (const int signal_number : flush_signals) { |
| | 120 | 104 | | std::signal(signal_number, flushProfilesAndReraise); |
| | 120 | 105 | | } |
| | 20 | 106 | | #endif |
| | 20 | 107 | | } |
| | | 108 | | |
| | | 109 | | #ifdef TAURIQT_COVERAGE |
| | | 110 | | /// Default period of the dump installed by `installPeriodicFlush`. |
| | | 111 | | constexpr int kProfileFlushMs = 1000; |
| | | 112 | | |
| | | 113 | | /// Dumps the profile of the running process every `TAURIQT_PROFILE_FLUSH_MS` |
| | | 114 | | /// (default `kProfileFlushMs`, `0` disables it). |
| | | 115 | | /// |
| | | 116 | | /// Not every run ends where it should: a harness can stop the app's session |
| | | 117 | | /// under it, and a process killed with `SIGKILL` — or reached before one of the |
| | | 118 | | /// handlers above — never runs the atexit hook the profile runtime installs, so |
| | | 119 | | /// nothing would be written at all. The dump is cumulative and merges into |
| | | 120 | | /// `LLVM_PROFILE_FILE`, so the last dump before the kill is what gets reported; |
| | | 121 | | /// the interval therefore bounds how much of a killed run can be missing. |
| | | 122 | | /// GUI thread only (called from `installPumpTimer`). |
| | 20 | 123 | | void installPeriodicFlush(QCoreApplication* app) { |
| | 20 | 124 | | const QByteArray value = qgetenv("TAURIQT_PROFILE_FLUSH_MS"); |
| | 20 | 125 | | bool parsed = false; |
| | 20 | 126 | | const int interval_ms = value.isEmpty() ? kProfileFlushMs : value.toInt(&parsed); |
| | 20 | 127 | | if ((!value.isEmpty() && !parsed) || interval_ms <= 0) { |
| | 0 | 128 | | return; |
| | 0 | 129 | | } |
| | 20 | 130 | | auto* timer = new QTimer(app); |
| | 20 | 131 | | timer->setTimerType(Qt::CoarseTimer); |
| | 20 | 132 | | timer->setInterval(interval_ms); |
| | 209 | 133 | | QObject::connect(timer, &QTimer::timeout, app, []() { (void)__llvm_profile_write_file(); }); |
| | 20 | 134 | | timer->start(); |
| | 20 | 135 | | } |
| | | 136 | | #endif |
| | | 137 | | |
| | | 138 | | /// Installs the drain timer; GUI thread only. |
| | 20 | 139 | | void installPumpTimer() { |
| | 20 | 140 | | QCoreApplication* app = application(); |
| | 20 | 141 | | if (app == nullptr || !g_pumpTimer.isNull()) { |
| | 0 | 142 | | return; |
| | 0 | 143 | | } |
| | 20 | 144 | | installExitHooks(app); |
| | 20 | 145 | | #ifdef TAURIQT_COVERAGE |
| | 20 | 146 | | installPeriodicFlush(app); |
| | 20 | 147 | | #endif |
| | 20 | 148 | | auto* timer = new QTimer(app); |
| | 20 | 149 | | timer->setTimerType(Qt::CoarseTimer); |
| | 20 | 150 | | timer->setInterval(kPumpIntervalMs); |
| | 13351 | 151 | | QObject::connect(timer, &QTimer::timeout, app, []() { qt_on_pump_tick(); }); |
| | 20 | 152 | | timer->start(); |
| | 20 | 153 | | g_pumpTimer = timer; |
| | 20 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// Runs `fn` on the GUI thread and waits for it; false when the GUI thread did |
| | | 157 | | /// not answer in time. The semaphore is kept alive by the shared pointer even |
| | | 158 | | /// when the wait times out, so a late call is still safe. |
| | | 159 | | template <typename Fn> |
| | 0 | 160 | | bool runOnGuiThreadAndWait(QCoreApplication* app, Fn fn) { |
| | 0 | 161 | | if (QThread::currentThread() == app->thread()) { |
| | 0 | 162 | | fn(); |
| | 0 | 163 | | return true; |
| | 0 | 164 | | } |
| | 0 | 165 | | const auto done = std::make_shared<QSemaphore>(); |
| | 0 | 166 | | QMetaObject::invokeMethod(app, [done, fn]() mutable { |
| | 0 | 167 | | fn(); |
| | 0 | 168 | | done->release(); |
| | 0 | 169 | | }, Qt::QueuedConnection); |
| | 0 | 170 | | return done->tryAcquire(1, kGuiThreadTimeoutMs); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// The platform connection Qt opened: `kind` matches `qt_runtime_platform_kind()`, |
| | | 174 | | /// `pointer` is the `xcb_connection_t*` / `Display*` / `wl_display*`. |
| | | 175 | | struct PlatformConnection { |
| | | 176 | | std::int32_t kind = 0; |
| | | 177 | | std::uint64_t pointer = 0; |
| | | 178 | | }; |
| | | 179 | | |
| | | 180 | | /// Reads the platform connection Qt opened. |
| | | 181 | | /// |
| | | 182 | | /// The plugin name decides whether the handles are real: only the `xcb` |
| | | 183 | | /// (X11) and `wayland` plugins own a window system connection whose windows |
| | | 184 | | /// can be described as raw handles. Other plugins answer the same native |
| | | 185 | | /// interfaces with unrelated objects — the `offscreen` plugin, for instance, |
| | | 186 | | /// implements `QX11Application` with the private `Display*` it opens for GLX |
| | | 187 | | /// and returns counter values (1, 2, …) from `QWidget::winId()` — so they |
| | | 188 | | /// report "unknown" instead of handing out values that do not describe a |
| | | 189 | | /// window. |
| | | 190 | | /// |
| | | 191 | | /// The platform integration and the connection it returns are created once by |
| | | 192 | | /// the `QGuiApplication` constructor and never replaced, so this only reads |
| | | 193 | | /// immutable state: `resolveInterface()` (which backs `nativeInterface<T>()`) |
| | | 194 | | /// is a `dynamic_cast` against `platformNativeInterface()`. No widget or |
| | | 195 | | /// event-loop state is touched, so the read is safe from any thread, including |
| | | 196 | | /// before the event loop starts. |
| | 0 | 197 | | PlatformConnection platformConnection() { |
| | 0 | 198 | | PlatformConnection connection; |
| | 0 | 199 | | auto* app = qobject_cast<QGuiApplication*>(application()); |
| | 0 | 200 | | if (app == nullptr) { |
| | 0 | 201 | | return connection; |
| | 0 | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | const QString platformName = QGuiApplication::platformName(); |
| | 0 | 205 | | if (platformName == QStringLiteral("wayland")) { |
| | 0 | 206 | | #if QT_CONFIG(wayland) |
| | 0 | 207 | | if (auto* wayland = app->nativeInterface<QNativeInterface::QWaylandApplication>()) { |
| | 0 | 208 | | if (wayland->display() != nullptr) { |
| | 0 | 209 | | connection.kind = 3; |
| | 0 | 210 | | connection.pointer = reinterpret_cast<std::uint64_t>(wayland->display()); |
| | 0 | 211 | | } |
| | 0 | 212 | | } |
| | 0 | 213 | | #endif |
| | 0 | 214 | | return connection; |
| | 0 | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (platformName == QStringLiteral("xcb")) { |
| | 0 | 218 | | #if QT_CONFIG(xcb) |
| | 0 | 219 | | if (auto* x11 = app->nativeInterface<QNativeInterface::QX11Application>()) { |
| | 0 | 220 | | if (x11->connection() != nullptr) { |
| | 0 | 221 | | connection.kind = 1; |
| | 0 | 222 | | connection.pointer = reinterpret_cast<std::uint64_t>(x11->connection()); |
| | 0 | 223 | | } else if (x11->display() != nullptr) { |
| | 0 | 224 | | connection.kind = 2; |
| | 0 | 225 | | connection.pointer = reinterpret_cast<std::uint64_t>(x11->display()); |
| | 0 | 226 | | } |
| | 0 | 227 | | } |
| | 0 | 228 | | #endif |
| | 0 | 229 | | return connection; |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | // Any other plugin (offscreen, minimal, vnc, eglfs, linuxfb, …): no |
| | | 233 | | // window-system handles. |
| | 0 | 234 | | return connection; |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | } // namespace |
| | | 238 | | |
| | 0 | 239 | | std::int32_t qt_runtime_platform_kind() { |
| | 0 | 240 | | return platformConnection().kind; |
| | 0 | 241 | | } |
| | | 242 | | |
| | 20 | 243 | | rust::String qt_runtime_platform_name() { |
| | 20 | 244 | | if (QGuiApplication::instance() == nullptr) { |
| | 0 | 245 | | return rust::String(); |
| | 0 | 246 | | } |
| | 20 | 247 | | return rust::String(QGuiApplication::platformName().toUtf8().constData()); |
| | 20 | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | std::uint64_t qt_runtime_display_pointer() { |
| | 0 | 251 | | return platformConnection().pointer; |
| | 0 | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | std::uint64_t qt_runtime_window_winid(std::uint64_t id) { |
| | 0 | 255 | | QCoreApplication* app = application(); |
| | 0 | 256 | | if (app == nullptr) { |
| | 0 | 257 | | return 0; |
| | 0 | 258 | | } |
| | 0 | 259 | | const auto winid = std::make_shared<std::uint64_t>(0); |
| | 0 | 260 | | const bool answered = runOnGuiThreadAndWait(app, [id, winid]() { |
| | 0 | 261 | | *winid = qt_window_winid(id); |
| | 0 | 262 | | }); |
| | 0 | 263 | | return answered ? *winid : 0; |
| | 0 | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | bool qt_runtime_has_application() { |
| | 0 | 267 | | return application() != nullptr; |
| | 0 | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | void qt_runtime_wake() { |
| | 0 | 271 | | QCoreApplication* app = application(); |
| | 0 | 272 | | if (app == nullptr) { |
| | 0 | 273 | | return; // no event loop to wake; the first pump tick drains the queue |
| | 0 | 274 | | } |
| | 0 | 275 | | QMetaObject::invokeMethod(app, []() { qt_on_pump_tick(); }, Qt::QueuedConnection); |
| | 0 | 276 | | } |
| | | 277 | | |
| | 20 | 278 | | bool qt_runtime_start_pump() { |
| | 20 | 279 | | QCoreApplication* app = application(); |
| | 20 | 280 | | if (app == nullptr) { |
| | 0 | 281 | | return false; |
| | 0 | 282 | | } |
| | 20 | 283 | | if (QThread::currentThread() == app->thread()) { |
| | 20 | 284 | | installPumpTimer(); |
| | 20 | 285 | | } else { |
| | 0 | 286 | | QMetaObject::invokeMethod(app, []() { installPumpTimer(); }, Qt::QueuedConnection); |
| | 0 | 287 | | } |
| | 20 | 288 | | return true; |
| | 20 | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | void qt_runtime_exit(std::int32_t code) { |
| | 0 | 292 | | QCoreApplication* app = application(); |
| | 0 | 293 | | if (app == nullptr) { |
| | 0 | 294 | | return; |
| | 0 | 295 | | } |
| | 0 | 296 | | if (QThread::currentThread() == app->thread()) { |
| | 0 | 297 | | QCoreApplication::exit(code); |
| | 0 | 298 | | return; |
| | 0 | 299 | | } |
| | 0 | 300 | | QMetaObject::invokeMethod(app, [code]() { QCoreApplication::exit(code); }, |
| | 0 | 301 | | Qt::QueuedConnection); |
| | 0 | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | void qt_runtime_process_events() { |
| | 0 | 305 | | QCoreApplication* app = application(); |
| | 0 | 306 | | if (app == nullptr) { |
| | 0 | 307 | | return; |
| | 0 | 308 | | } |
| | 0 | 309 | | if (QThread::currentThread() == app->thread()) { |
| | 0 | 310 | | QCoreApplication::processEvents(); |
| | 0 | 311 | | return; |
| | 0 | 312 | | } |
| | 0 | 313 | | QMetaObject::invokeMethod(app, []() { QCoreApplication::processEvents(); }, |
| | 0 | 314 | | Qt::QueuedConnection); |
| | 0 | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | rust::String qt_runtime_debug_native_state() { |
| | 0 | 318 | | const PlatformConnection connection = platformConnection(); |
| | | 319 | | |
| | 0 | 320 | | const QString platformName = |
| | 0 | 321 | | qGuiApp != nullptr ? QGuiApplication::platformName() : QStringLiteral("<no application>"); |
| | 0 | 322 | | QString text = QStringLiteral("platform=%1 kind=%2 display=0x%3") |
| | 0 | 323 | | .arg(platformName) |
| | 0 | 324 | | .arg(connection.kind) |
| | 0 | 325 | | .arg(connection.pointer, 0, 16); |
| | | 326 | | |
| | 0 | 327 | | if (qobject_cast<QApplication*>(application()) != nullptr) { |
| | 0 | 328 | | const QWidgetList windows = QApplication::topLevelWidgets(); |
| | 0 | 329 | | text += QStringLiteral(" topLevelWidgets=%1").arg(windows.size()); |
| | 0 | 330 | | for (QWidget* window : windows) { |
| | 0 | 331 | | text += QStringLiteral(" [winId=0x%1 visible=%2 title=\"%3\"]") |
| | 0 | 332 | | .arg(static_cast<quintptr>(window->winId()), 0, 16) |
| | 0 | 333 | | .arg(window->isVisible() ? 1 : 0) |
| | 0 | 334 | | .arg(window->windowTitle()); |
| | 0 | 335 | | } |
| | 0 | 336 | | } else { |
| | 0 | 337 | | text += QStringLiteral(" (no QApplication)"); |
| | 0 | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | const QByteArray utf8 = text.toUtf8(); |
| | 0 | 341 | | return rust::String(utf8.constData(), static_cast<std::size_t>(utf8.size())); |
| | 0 | 342 | | } |