Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build-macos/
build-ohos/
build-windows/
cmake-build-debug/
_codeql_detected_source_root

# Generated test binaries and object files
*.o
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions examples/autostart_c_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 with the native API library
target_link_libraries(autostart_c_example nativeapi)

# Set include directories
target_include_directories(autostart_c_example PRIVATE ../../include)
107 changes: 107 additions & 0 deletions examples/autostart_c_example/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <nativeapi.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
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.c", "My C Example App");
if (!autostart) {
printf("Failed to create AutoStart instance.\n");
return 1;
}

/* Display current configuration */
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("AutoStart configuration:\n");
printf(" ID: %s\n", id ? id : "(null)");
printf(" Display name: %s\n", display_name ? display_name : "(null)");
printf(" Executable: %s\n\n", executable ? executable : "(null)");

free_c_str(id);
free_c_str(display_name);

/* Set a custom program path and arguments */
const char* arguments[] = {"--minimized", "--autostart"};
native_autostart_set_program(autostart, executable ? executable : "", arguments, 2);
free_c_str(executable);

/* Retrieve and display the updated executable path */
char* exec_path = native_autostart_get_executable_path(autostart);
printf("After SetProgram:\n");
printf(" Executable: %s\n", exec_path ? exec_path : "(null)");
printf(" Arguments: --minimized --autostart\n\n");
free_c_str(exec_path);

/* Check current state before enabling */
printf("Is enabled (before Enable): %s\n",
native_autostart_is_enabled(autostart) ? "yes" : "no");

/* Enable auto-start */
printf("Enabling auto-start...\n");
if (native_autostart_enable(autostart)) {
printf("Auto-start enabled successfully.\n");
} else {
printf("Failed to enable auto-start.\n");
native_autostart_destroy(autostart);
return 1;
}

/* Verify it is now enabled */
printf("Is enabled (after Enable): %s\n\n",
native_autostart_is_enabled(autostart) ? "yes" : "no");

/* Update the display name and re-enable to update the stored entry */
native_autostart_set_display_name(autostart, "My C Example App (Updated)");
char* updated_name = native_autostart_get_display_name(autostart);
printf("Updated display name to: %s\n", updated_name ? updated_name : "(null)");
free_c_str(updated_name);
native_autostart_enable(autostart);

/* Disable auto-start */
printf("\nDisabling auto-start...\n");
if (native_autostart_disable(autostart)) {
printf("Auto-start disabled successfully.\n");
} else {
printf("Failed to disable auto-start.\n");
native_autostart_destroy(autostart);
return 1;
}

/* Verify it is now disabled */
printf("Is enabled (after Disable): %s\n\n",
native_autostart_is_enabled(autostart) ? "yes" : "no");

/* Clean up */
native_autostart_destroy(autostart);

printf("Example completed successfully!\n\n");
printf("This example demonstrated:\n");
printf(" * native_autostart_is_supported() - Check platform support\n");
printf(" * native_autostart_create_with_id_and_name() - Create with ID and name\n");
printf(" * native_autostart_get_id() - Get identifier\n");
printf(" * native_autostart_get_display_name() - Get display name\n");
printf(" * native_autostart_set_display_name() - Update display name\n");
printf(" * native_autostart_set_program() - Set executable and arguments\n");
printf(" * native_autostart_get_executable_path() - Get configured executable\n");
printf(" * native_autostart_enable() - Register auto-start with the OS\n");
printf(" * native_autostart_disable() - Remove auto-start from the OS\n");
printf(" * native_autostart_is_enabled() - Query current registration state\n");
printf(" * native_autostart_destroy() - Free resources\n");

return 0;
}
16 changes: 16 additions & 0 deletions examples/autostart_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.10)

project(autostart_example)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Add executable
add_executable(autostart_example main.cpp)

# Link with the native API library
target_link_libraries(autostart_example nativeapi)

# Set include directories
target_include_directories(autostart_example PRIVATE ../../include)
89 changes: 89 additions & 0 deletions examples/autostart_example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <string>
#include <vector>

#include "nativeapi.h"

using namespace nativeapi;

int main() {
std::cout << "AutoStart Example\n";
std::cout << "=================\n\n";

// Check if auto-start is supported on this platform
if (!AutoStart::IsSupported()) {
std::cout << "AutoStart is not supported on this platform.\n";
return 0;
}

std::cout << "AutoStart is supported on this platform.\n\n";

// Create an AutoStart manager with a custom identifier and display name
AutoStart autostart("com.example.myapp", "My Example App");

// Display current configuration
std::cout << "AutoStart configuration:\n";
std::cout << " ID: " << autostart.GetId() << "\n";
std::cout << " Display name: " << autostart.GetDisplayName() << "\n";
std::cout << " Executable: " << autostart.GetExecutablePath() << "\n\n";

// Set a custom program path and arguments
autostart.SetProgram(autostart.GetExecutablePath(), {"--minimized", "--autostart"});

std::cout << "After SetProgram:\n";
std::cout << " Executable: " << autostart.GetExecutablePath() << "\n";
auto args = autostart.GetArguments();
std::cout << " Arguments: ";
for (const auto& arg : args) {
std::cout << arg << " ";
}
std::cout << "\n\n";

// Check current state before enabling
std::cout << "Is enabled (before Enable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n";

// Enable auto-start
std::cout << "Enabling auto-start...\n";
if (autostart.Enable()) {
std::cout << "Auto-start enabled successfully.\n";
} else {
std::cout << "Failed to enable auto-start.\n";
return 1;
}

// Verify it is now enabled
std::cout << "Is enabled (after Enable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n\n";

// Update the display name and re-enable to update the stored entry
autostart.SetDisplayName("My Example App (Updated)");
std::cout << "Updated display name to: " << autostart.GetDisplayName() << "\n";
autostart.Enable();

// Disable auto-start
std::cout << "\nDisabling auto-start...\n";
if (autostart.Disable()) {
std::cout << "Auto-start disabled successfully.\n";
} else {
std::cout << "Failed to disable auto-start.\n";
return 1;
}

// Verify it is now disabled
std::cout << "Is enabled (after Disable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n\n";

std::cout << "Example completed successfully!\n\n";
std::cout << "This example demonstrated:\n";
std::cout << " * AutoStart::IsSupported() - Check platform support\n";
std::cout << " * AutoStart(id, name) - Construct with identifier and display name\n";
std::cout << " * AutoStart::GetId() - Get identifier\n";
std::cout << " * AutoStart::GetDisplayName() - Get display name\n";
std::cout << " * AutoStart::SetDisplayName() - Update display name\n";
std::cout << " * AutoStart::SetProgram() - Set executable path and arguments\n";
std::cout << " * AutoStart::GetExecutablePath() - Get configured executable\n";
std::cout << " * AutoStart::GetArguments() - Get configured arguments\n";
std::cout << " * AutoStart::Enable() - Register auto-start with the OS\n";
std::cout << " * AutoStart::Disable() - Remove auto-start from the OS\n";
std::cout << " * AutoStart::IsEnabled() - Query current registration state\n";

return 0;
}