|
| 1 | +#include "app_runner_c.h" |
| 2 | +#include <atomic> |
| 3 | +#include <memory> |
| 4 | + |
| 5 | +#include "../app_runner.h" |
| 6 | + |
| 7 | +using namespace nativeapi; |
| 8 | + |
| 9 | +// Global state for the app runner |
| 10 | +static std::atomic<bool> g_is_running{false}; |
| 11 | +static std::atomic<int> g_exit_code{0}; |
| 12 | + |
| 13 | +// Helper to convert native_window_t back to shared_ptr<Window> |
| 14 | +static std::shared_ptr<Window> GetWindowFromHandle( |
| 15 | + native_window_t window_handle) { |
| 16 | + if (!window_handle) { |
| 17 | + return nullptr; |
| 18 | + } |
| 19 | + |
| 20 | + // Assuming native_window_handle structure from window_c.cpp |
| 21 | + struct native_window_handle { |
| 22 | + std::shared_ptr<Window> window; |
| 23 | + }; |
| 24 | + |
| 25 | + auto* handle = reinterpret_cast<native_window_handle*>(window_handle); |
| 26 | + return handle->window; |
| 27 | +} |
| 28 | + |
| 29 | +extern "C" { |
| 30 | + |
| 31 | +int native_app_runner_run(native_window_t window) { |
| 32 | + // Validate input |
| 33 | + if (!window) { |
| 34 | + return NATIVE_APP_EXIT_INVALID_WINDOW; |
| 35 | + } |
| 36 | + |
| 37 | + auto window_ptr = GetWindowFromHandle(window); |
| 38 | + if (!window_ptr) { |
| 39 | + return NATIVE_APP_EXIT_INVALID_WINDOW; |
| 40 | + } |
| 41 | + |
| 42 | + // Mark as running |
| 43 | + g_is_running.store(true); |
| 44 | + g_exit_code.store(0); |
| 45 | + |
| 46 | + try { |
| 47 | + // Get the AppRunner instance and run the application |
| 48 | + AppRunner& runner = AppRunner::GetInstance(); |
| 49 | + |
| 50 | + // Run the main event loop |
| 51 | + int exit_code = runner.Run(window_ptr); |
| 52 | + |
| 53 | + // Mark as no longer running |
| 54 | + g_is_running.store(false); |
| 55 | + |
| 56 | + // Use the stored exit code if terminate was called, otherwise use the |
| 57 | + // returned code |
| 58 | + int final_exit_code = g_exit_code.load(); |
| 59 | + if (final_exit_code == 0) { |
| 60 | + final_exit_code = exit_code; |
| 61 | + } |
| 62 | + |
| 63 | + return final_exit_code; |
| 64 | + |
| 65 | + } catch (const std::exception& e) { |
| 66 | + // Handle any exceptions that might occur |
| 67 | + g_is_running.store(false); |
| 68 | + |
| 69 | + return NATIVE_APP_EXIT_FAILURE; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +bool native_app_runner_is_running(void) { |
| 74 | + return g_is_running.load(); |
| 75 | +} |
| 76 | + |
| 77 | +} // extern "C" |
0 commit comments