|
| 1 | +#include <nativeapi.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +int main(void) { |
| 6 | + printf("AutoStart C API Example\n"); |
| 7 | + printf("=======================\n\n"); |
| 8 | + |
| 9 | + /* Check if auto-start is supported on this platform */ |
| 10 | + if (!native_autostart_is_supported()) { |
| 11 | + printf("AutoStart is not supported on this platform.\n"); |
| 12 | + return 0; |
| 13 | + } |
| 14 | + |
| 15 | + printf("AutoStart is supported on this platform.\n\n"); |
| 16 | + |
| 17 | + /* Create an AutoStart manager with a custom identifier and display name */ |
| 18 | + native_autostart_t autostart = |
| 19 | + native_autostart_create_with_id_and_name("com.example.myapp.c", "My C Example App"); |
| 20 | + if (!autostart) { |
| 21 | + printf("Failed to create AutoStart instance.\n"); |
| 22 | + return 1; |
| 23 | + } |
| 24 | + |
| 25 | + /* Display current configuration */ |
| 26 | + char* id = native_autostart_get_id(autostart); |
| 27 | + char* display_name = native_autostart_get_display_name(autostart); |
| 28 | + char* executable = native_autostart_get_executable_path(autostart); |
| 29 | + |
| 30 | + printf("AutoStart configuration:\n"); |
| 31 | + printf(" ID: %s\n", id ? id : "(null)"); |
| 32 | + printf(" Display name: %s\n", display_name ? display_name : "(null)"); |
| 33 | + printf(" Executable: %s\n\n", executable ? executable : "(null)"); |
| 34 | + |
| 35 | + free_c_str(id); |
| 36 | + free_c_str(display_name); |
| 37 | + |
| 38 | + /* Set a custom program path and arguments */ |
| 39 | + const char* arguments[] = {"--minimized", "--autostart"}; |
| 40 | + native_autostart_set_program(autostart, executable ? executable : "", arguments, 2); |
| 41 | + free_c_str(executable); |
| 42 | + |
| 43 | + /* Retrieve and display the updated executable path */ |
| 44 | + char* exec_path = native_autostart_get_executable_path(autostart); |
| 45 | + printf("After SetProgram:\n"); |
| 46 | + printf(" Executable: %s\n", exec_path ? exec_path : "(null)"); |
| 47 | + printf(" Arguments: --minimized --autostart\n\n"); |
| 48 | + free_c_str(exec_path); |
| 49 | + |
| 50 | + /* Check current state before enabling */ |
| 51 | + printf("Is enabled (before Enable): %s\n", |
| 52 | + native_autostart_is_enabled(autostart) ? "yes" : "no"); |
| 53 | + |
| 54 | + /* Enable auto-start */ |
| 55 | + printf("Enabling auto-start...\n"); |
| 56 | + if (native_autostart_enable(autostart)) { |
| 57 | + printf("Auto-start enabled successfully.\n"); |
| 58 | + } else { |
| 59 | + printf("Failed to enable auto-start.\n"); |
| 60 | + native_autostart_destroy(autostart); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + /* Verify it is now enabled */ |
| 65 | + printf("Is enabled (after Enable): %s\n\n", |
| 66 | + native_autostart_is_enabled(autostart) ? "yes" : "no"); |
| 67 | + |
| 68 | + /* Update the display name and re-enable to update the stored entry */ |
| 69 | + native_autostart_set_display_name(autostart, "My C Example App (Updated)"); |
| 70 | + char* updated_name = native_autostart_get_display_name(autostart); |
| 71 | + printf("Updated display name to: %s\n", updated_name ? updated_name : "(null)"); |
| 72 | + free_c_str(updated_name); |
| 73 | + native_autostart_enable(autostart); |
| 74 | + |
| 75 | + /* Disable auto-start */ |
| 76 | + printf("\nDisabling auto-start...\n"); |
| 77 | + if (native_autostart_disable(autostart)) { |
| 78 | + printf("Auto-start disabled successfully.\n"); |
| 79 | + } else { |
| 80 | + printf("Failed to disable auto-start.\n"); |
| 81 | + native_autostart_destroy(autostart); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + /* Verify it is now disabled */ |
| 86 | + printf("Is enabled (after Disable): %s\n\n", |
| 87 | + native_autostart_is_enabled(autostart) ? "yes" : "no"); |
| 88 | + |
| 89 | + /* Clean up */ |
| 90 | + native_autostart_destroy(autostart); |
| 91 | + |
| 92 | + printf("Example completed successfully!\n\n"); |
| 93 | + printf("This example demonstrated:\n"); |
| 94 | + printf(" * native_autostart_is_supported() - Check platform support\n"); |
| 95 | + printf(" * native_autostart_create_with_id_and_name() - Create with ID and name\n"); |
| 96 | + printf(" * native_autostart_get_id() - Get identifier\n"); |
| 97 | + printf(" * native_autostart_get_display_name() - Get display name\n"); |
| 98 | + printf(" * native_autostart_set_display_name() - Update display name\n"); |
| 99 | + printf(" * native_autostart_set_program() - Set executable and arguments\n"); |
| 100 | + printf(" * native_autostart_get_executable_path() - Get configured executable\n"); |
| 101 | + printf(" * native_autostart_enable() - Register auto-start with the OS\n"); |
| 102 | + printf(" * native_autostart_disable() - Remove auto-start from the OS\n"); |
| 103 | + printf(" * native_autostart_is_enabled() - Query current registration state\n"); |
| 104 | + printf(" * native_autostart_destroy() - Free resources\n"); |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
0 commit comments