diff --git a/.gitignore b/.gitignore index 33389b6..485c2f2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ build-macos/ build-ohos/ build-windows/ cmake-build-debug/ +_codeql_build_dir/ +_codeql_detected_source_root # Generated test binaries and object files *.o diff --git a/CMakeLists.txt b/CMakeLists.txt index a954e2b..4a14471 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ add_subdirectory(src) # Add example programs subdirectory add_subdirectory(examples/application_example) add_subdirectory(examples/application_c_example) +add_subdirectory(examples/autostart_example) +add_subdirectory(examples/autostart_c_example) add_subdirectory(examples/display_example) add_subdirectory(examples/display_c_example) add_subdirectory(examples/id_allocator_example) diff --git a/examples/autostart_c_example/CMakeLists.txt b/examples/autostart_c_example/CMakeLists.txt new file mode 100644 index 0000000..0f5f853 --- /dev/null +++ b/examples/autostart_c_example/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.10) + +project(autostart_c_example) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# Add executable +add_executable(autostart_c_example main.c) + +# Link nativeapi library +target_link_libraries(autostart_c_example PRIVATE nativeapi) diff --git a/examples/autostart_c_example/main.c b/examples/autostart_c_example/main.c new file mode 100644 index 0000000..38df1a9 --- /dev/null +++ b/examples/autostart_c_example/main.c @@ -0,0 +1,111 @@ +#include +#include +#include + +int main() { + printf("AutoStart C API Example\n"); + printf("=======================\n\n"); + + // Check if auto-start is supported on this platform + if (!native_autostart_is_supported()) { + printf("AutoStart is not supported on this platform.\n"); + return 0; + } + + printf("AutoStart is supported on this platform.\n\n"); + + // Create an AutoStart manager with a custom identifier and display name + native_autostart_t autostart = + native_autostart_create_with_id_and_name("com.example.myapp", "MyApp"); + if (!autostart) { + printf("Failed to create AutoStart instance.\n"); + return 1; + } + + // Print info + char* id = native_autostart_get_id(autostart); + char* display_name = native_autostart_get_display_name(autostart); + char* executable = native_autostart_get_executable_path(autostart); + + printf("ID: %s\n", id ? id : "(null)"); + printf("Display name: %s\n", display_name ? display_name : "(null)"); + printf("Executable: %s\n", executable ? executable : "(null)"); + + char** arguments = NULL; + size_t arg_count = 0; + printf("Arguments: "); + if (native_autostart_get_arguments(autostart, &arguments, &arg_count) && arg_count > 0) { + for (size_t i = 0; i < arg_count; ++i) { + if (i > 0) + printf(", "); + printf("%s", arguments[i] ? arguments[i] : ""); + } + native_autostart_free_arguments(arguments, arg_count); + } else { + printf("(none)"); + } + printf("\n\n"); + + free_c_str(id); + free_c_str(display_name); + free_c_str(executable); + + // Check current state + printf("Currently enabled: %s\n", native_autostart_is_enabled(autostart) ? "yes" : "no"); + + // Enable auto-start + printf("\nEnabling auto-start...\n"); + if (native_autostart_enable(autostart)) { + printf(" -> Success\n"); + printf(" Is enabled now: %s\n", native_autostart_is_enabled(autostart) ? "yes" : "no"); + } else { + printf(" -> Failed to enable auto-start\n"); + } + + // Disable auto-start + printf("\nDisabling auto-start...\n"); + if (native_autostart_disable(autostart)) { + printf(" -> Success\n"); + printf(" Is enabled now: %s\n", native_autostart_is_enabled(autostart) ? "yes" : "no"); + } else { + printf(" -> Failed to disable auto-start\n"); + } + + // Demo: customize program and arguments + printf("\nSetting custom arguments...\n"); + char* current_exe = native_autostart_get_executable_path(autostart); + const char* custom_args[] = {"--minimized", "--silent"}; + native_autostart_set_program(autostart, current_exe ? current_exe : "", custom_args, 2); + free_c_str(current_exe); + + char* new_exe = native_autostart_get_executable_path(autostart); + printf(" Executable: %s\n", new_exe ? new_exe : "(null)"); + free_c_str(new_exe); + + char** new_arguments = NULL; + size_t new_arg_count = 0; + printf(" Arguments: "); + if (native_autostart_get_arguments(autostart, &new_arguments, &new_arg_count) && + new_arg_count > 0) { + for (size_t i = 0; i < new_arg_count; ++i) { + if (i > 0) + printf(", "); + printf("%s", new_arguments[i] ? new_arguments[i] : ""); + } + native_autostart_free_arguments(new_arguments, new_arg_count); + } else { + printf("(none)"); + } + printf("\n"); + + // Clean up + native_autostart_destroy(autostart); + + printf("\nDone!\n\n"); + printf("Platform notes:\n"); + printf(" - Windows: HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\n"); + printf(" - macOS: ~/Library/LaunchAgents/.plist\n"); + printf(" - Linux: ~/.config/autostart/.desktop\n"); + + return 0; +} diff --git a/examples/autostart_example/CMakeLists.txt b/examples/autostart_example/CMakeLists.txt new file mode 100644 index 0000000..4d6340a --- /dev/null +++ b/examples/autostart_example/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.10) + +project(autostart_example) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Add executable +add_executable(autostart_example main.cpp) + +# Link nativeapi library +target_link_libraries(autostart_example PRIVATE nativeapi) diff --git a/examples/autostart_example/main.cpp b/examples/autostart_example/main.cpp new file mode 100644 index 0000000..a50242b --- /dev/null +++ b/examples/autostart_example/main.cpp @@ -0,0 +1,88 @@ +#include +#include + +using namespace nativeapi; + +int main() { + std::cout << "AutoStart Example" << std::endl; + std::cout << "=================" << std::endl; + std::cout << std::endl; + + // Check if auto-start is supported on this platform + if (!AutoStart::IsSupported()) { + std::cout << "AutoStart is not supported on this platform." << std::endl; + return 0; + } + + std::cout << "AutoStart is supported on this platform." << std::endl; + std::cout << std::endl; + + // Create an AutoStart manager with a custom identifier and display name + AutoStart autostart("com.example.myapp", "MyApp"); + + std::cout << "ID: " << autostart.GetId() << std::endl; + std::cout << "Display name: " << autostart.GetDisplayName() << std::endl; + std::cout << "Executable: " << autostart.GetExecutablePath() << std::endl; + + auto args = autostart.GetArguments(); + std::cout << "Arguments: "; + if (args.empty()) { + std::cout << "(none)"; + } else { + for (size_t i = 0; i < args.size(); ++i) { + if (i > 0) + std::cout << ", "; + std::cout << args[i]; + } + } + std::cout << std::endl; + std::cout << std::endl; + + // Check current state + std::cout << "Currently enabled: " << (autostart.IsEnabled() ? "yes" : "no") << std::endl; + + // Enable auto-start + std::cout << std::endl; + std::cout << "Enabling auto-start..." << std::endl; + if (autostart.Enable()) { + std::cout << " -> Success" << std::endl; + std::cout << " Is enabled now: " << (autostart.IsEnabled() ? "yes" : "no") << std::endl; + } else { + std::cout << " -> Failed to enable auto-start" << std::endl; + } + + // Disable auto-start + std::cout << std::endl; + std::cout << "Disabling auto-start..." << std::endl; + if (autostart.Disable()) { + std::cout << " -> Success" << std::endl; + std::cout << " Is enabled now: " << (autostart.IsEnabled() ? "yes" : "no") << std::endl; + } else { + std::cout << " -> Failed to disable auto-start" << std::endl; + } + + // Demo: customize the program and arguments + std::cout << std::endl; + std::cout << "Setting custom program path and arguments..." << std::endl; + autostart.SetProgram(autostart.GetExecutablePath(), {"--minimized", "--silent"}); + std::cout << " Executable: " << autostart.GetExecutablePath() << std::endl; + + auto new_args = autostart.GetArguments(); + std::cout << " Arguments: "; + for (size_t i = 0; i < new_args.size(); ++i) { + if (i > 0) + std::cout << ", "; + std::cout << new_args[i]; + } + std::cout << std::endl; + + std::cout << std::endl; + std::cout << "Done!" << std::endl; + std::cout << std::endl; + std::cout << "Platform notes:" << std::endl; + std::cout << " - Windows: HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" << std::endl; + std::cout << " - macOS: ~/Library/LaunchAgents/.plist" << std::endl; + std::cout << " - Linux: ~/.config/autostart/.desktop" << std::endl; + + return 0; +} diff --git a/src/capi/autostart_c.cpp b/src/capi/autostart_c.cpp index 183b5ea..e139486 100644 --- a/src/capi/autostart_c.cpp +++ b/src/capi/autostart_c.cpp @@ -172,6 +172,15 @@ bool native_autostart_get_arguments(native_autostart_t autostart, } } +void native_autostart_free_arguments(char** arguments, size_t count) { + if (arguments) { + for (size_t i = 0; i < count; ++i) { + free_c_str(arguments[i]); + } + delete[] arguments; + } +} + bool native_autostart_enable(native_autostart_t autostart) { if (!autostart) { return false; diff --git a/src/capi/autostart_c.h b/src/capi/autostart_c.h index 2ba012d..66160b1 100644 --- a/src/capi/autostart_c.h +++ b/src/capi/autostart_c.h @@ -132,13 +132,21 @@ char* native_autostart_get_executable_path(native_autostart_t autostart); * @param autostart AutoStart handle. * @param out_arguments Output pointer to an array of newly allocated strings. * @param out_count Output pointer to receive the number of arguments. - * @return true on success; false on error. On success, free each string with - * free_c_str() and the array with delete[]. + * @return true on success; false on error. On success, free the result with + * native_autostart_free_arguments(). */ bool native_autostart_get_arguments(native_autostart_t autostart, char*** out_arguments, size_t* out_count); +/** + * Free an arguments array returned by native_autostart_get_arguments. + * + * @param arguments Array of strings returned by native_autostart_get_arguments. + * @param count Number of strings in the array (as set by native_autostart_get_arguments). + */ +void native_autostart_free_arguments(char** arguments, size_t count); + /** * Enable auto-start at user login for the configured program and arguments. *