|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "nativeapi.h" |
| 6 | + |
| 7 | +void on_application_event(const native_application_event_t* event) { |
| 8 | + switch (event->type) { |
| 9 | + case NATIVE_APPLICATION_EVENT_STARTED: |
| 10 | + printf("Application started event received\n"); |
| 11 | + break; |
| 12 | + case NATIVE_APPLICATION_EVENT_EXITING: |
| 13 | + printf("Application exiting event received with exit code: %d\n", event->exit_code); |
| 14 | + break; |
| 15 | + case NATIVE_APPLICATION_EVENT_ACTIVATED: |
| 16 | + printf("Application activated event received\n"); |
| 17 | + break; |
| 18 | + case NATIVE_APPLICATION_EVENT_DEACTIVATED: |
| 19 | + printf("Application deactivated event received\n"); |
| 20 | + break; |
| 21 | + case NATIVE_APPLICATION_EVENT_QUIT_REQUESTED: |
| 22 | + printf("Application quit requested event received\n"); |
| 23 | + break; |
| 24 | + default: |
| 25 | + printf("Unknown application event type: %d\n", event->type); |
| 26 | + break; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +int main() { |
| 31 | + printf("Application C API Example\n"); |
| 32 | + |
| 33 | + // Get the Application singleton |
| 34 | + native_application_t app = native_application_get_instance(); |
| 35 | + if (!app) { |
| 36 | + fprintf(stderr, "Failed to get application instance\n"); |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + // Initialize the application |
| 41 | + if (!native_application_initialize(app)) { |
| 42 | + fprintf(stderr, "Failed to initialize application\n"); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + printf("Application initialized successfully\n"); |
| 47 | + printf("Single instance: %s\n", native_application_is_single_instance(app) ? "Yes" : "No"); |
| 48 | + |
| 49 | + // Add event listener |
| 50 | + size_t listener_id = native_application_add_event_listener(app, on_application_event); |
| 51 | + if (listener_id == 0) { |
| 52 | + fprintf(stderr, "Failed to add event listener\n"); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + // Create a simple window |
| 57 | + native_window_options_t* window_options = native_window_options_create(); |
| 58 | + if (!window_options) { |
| 59 | + fprintf(stderr, "Failed to create window options\n"); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + native_window_options_set_title(window_options, "Application C Example Window"); |
| 64 | + native_window_options_set_size(window_options, 400.0, 300.0); |
| 65 | + |
| 66 | + native_window_t window = native_window_manager_create(window_options); |
| 67 | + if (!window) { |
| 68 | + fprintf(stderr, "Failed to create window\n"); |
| 69 | + native_window_options_destroy(window_options); |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + printf("Window created successfully\n"); |
| 74 | + printf("Window ID: %ld\n", native_window_get_id(window)); |
| 75 | + |
| 76 | + // Show the window |
| 77 | + native_window_show(window); |
| 78 | + |
| 79 | + printf("Starting application event loop...\n"); |
| 80 | + printf("Press Ctrl+C to quit\n"); |
| 81 | + |
| 82 | + // Run the application |
| 83 | + int exit_code = native_application_run(app); |
| 84 | + |
| 85 | + printf("Application exited with code: %d\n", exit_code); |
| 86 | + |
| 87 | + // Clean up |
| 88 | + native_application_remove_event_listener(app, listener_id); |
| 89 | + native_window_options_destroy(window_options); |
| 90 | + |
| 91 | + return exit_code; |
| 92 | +} |
0 commit comments