Skip to content

Commit 4ad2ebf

Browse files
Copilotlijy91
andauthored
Add AutoStart example programs for macOS (#41)
* Initial plan * Add autostart_example and autostart_c_example Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com> * Remove _codeql_detected_source_root from tracking, add to .gitignore Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lijy91 <3889523+lijy91@users.noreply.github.com>
1 parent bc2aaad commit 4ad2ebf

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ build-macos/
99
build-ohos/
1010
build-windows/
1111
cmake-build-debug/
12+
_codeql_detected_source_root
1213

1314
# Generated test binaries and object files
1415
*.o

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ add_subdirectory(src)
1010
# Add example programs subdirectory
1111
add_subdirectory(examples/application_example)
1212
add_subdirectory(examples/application_c_example)
13+
add_subdirectory(examples/autostart_example)
14+
add_subdirectory(examples/autostart_c_example)
1315
add_subdirectory(examples/display_example)
1416
add_subdirectory(examples/display_c_example)
1517
add_subdirectory(examples/id_allocator_example)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(autostart_c_example)
4+
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_C_STANDARD_REQUIRED ON)
7+
8+
# Add executable
9+
add_executable(autostart_c_example main.c)
10+
11+
# Link with the native API library
12+
target_link_libraries(autostart_c_example nativeapi)
13+
14+
# Set include directories
15+
target_include_directories(autostart_c_example PRIVATE ../../include)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(autostart_example)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add executable
10+
add_executable(autostart_example main.cpp)
11+
12+
# Link with the native API library
13+
target_link_libraries(autostart_example nativeapi)
14+
15+
# Set include directories
16+
target_include_directories(autostart_example PRIVATE ../../include)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "nativeapi.h"
6+
7+
using namespace nativeapi;
8+
9+
int main() {
10+
std::cout << "AutoStart Example\n";
11+
std::cout << "=================\n\n";
12+
13+
// Check if auto-start is supported on this platform
14+
if (!AutoStart::IsSupported()) {
15+
std::cout << "AutoStart is not supported on this platform.\n";
16+
return 0;
17+
}
18+
19+
std::cout << "AutoStart is supported on this platform.\n\n";
20+
21+
// Create an AutoStart manager with a custom identifier and display name
22+
AutoStart autostart("com.example.myapp", "My Example App");
23+
24+
// Display current configuration
25+
std::cout << "AutoStart configuration:\n";
26+
std::cout << " ID: " << autostart.GetId() << "\n";
27+
std::cout << " Display name: " << autostart.GetDisplayName() << "\n";
28+
std::cout << " Executable: " << autostart.GetExecutablePath() << "\n\n";
29+
30+
// Set a custom program path and arguments
31+
autostart.SetProgram(autostart.GetExecutablePath(), {"--minimized", "--autostart"});
32+
33+
std::cout << "After SetProgram:\n";
34+
std::cout << " Executable: " << autostart.GetExecutablePath() << "\n";
35+
auto args = autostart.GetArguments();
36+
std::cout << " Arguments: ";
37+
for (const auto& arg : args) {
38+
std::cout << arg << " ";
39+
}
40+
std::cout << "\n\n";
41+
42+
// Check current state before enabling
43+
std::cout << "Is enabled (before Enable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n";
44+
45+
// Enable auto-start
46+
std::cout << "Enabling auto-start...\n";
47+
if (autostart.Enable()) {
48+
std::cout << "Auto-start enabled successfully.\n";
49+
} else {
50+
std::cout << "Failed to enable auto-start.\n";
51+
return 1;
52+
}
53+
54+
// Verify it is now enabled
55+
std::cout << "Is enabled (after Enable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n\n";
56+
57+
// Update the display name and re-enable to update the stored entry
58+
autostart.SetDisplayName("My Example App (Updated)");
59+
std::cout << "Updated display name to: " << autostart.GetDisplayName() << "\n";
60+
autostart.Enable();
61+
62+
// Disable auto-start
63+
std::cout << "\nDisabling auto-start...\n";
64+
if (autostart.Disable()) {
65+
std::cout << "Auto-start disabled successfully.\n";
66+
} else {
67+
std::cout << "Failed to disable auto-start.\n";
68+
return 1;
69+
}
70+
71+
// Verify it is now disabled
72+
std::cout << "Is enabled (after Disable): " << (autostart.IsEnabled() ? "yes" : "no") << "\n\n";
73+
74+
std::cout << "Example completed successfully!\n\n";
75+
std::cout << "This example demonstrated:\n";
76+
std::cout << " * AutoStart::IsSupported() - Check platform support\n";
77+
std::cout << " * AutoStart(id, name) - Construct with identifier and display name\n";
78+
std::cout << " * AutoStart::GetId() - Get identifier\n";
79+
std::cout << " * AutoStart::GetDisplayName() - Get display name\n";
80+
std::cout << " * AutoStart::SetDisplayName() - Update display name\n";
81+
std::cout << " * AutoStart::SetProgram() - Set executable path and arguments\n";
82+
std::cout << " * AutoStart::GetExecutablePath() - Get configured executable\n";
83+
std::cout << " * AutoStart::GetArguments() - Get configured arguments\n";
84+
std::cout << " * AutoStart::Enable() - Register auto-start with the OS\n";
85+
std::cout << " * AutoStart::Disable() - Remove auto-start from the OS\n";
86+
std::cout << " * AutoStart::IsEnabled() - Query current registration state\n";
87+
88+
return 0;
89+
}

0 commit comments

Comments
 (0)