Skip to content

Commit 0859caf

Browse files
committed
Add cross-platform Application API and C/C++ examples
Introduces a new Application singleton class with lifecycle management, event handling, and platform-specific implementations for Linux, macOS, and Windows. Adds C and C++ API headers and C API bindings for application management. Provides example programs demonstrating usage of the new Application API in both C and C++. Updates the main CMakeLists.txt to include the new examples.
1 parent 6b5483f commit 0859caf

File tree

14 files changed

+1576
-1
lines changed

14 files changed

+1576
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ project(nativeapi_library VERSION 0.0.1 LANGUAGES CXX C)
66
add_subdirectory(src)
77

88
# Add example programs subdirectory
9+
add_subdirectory(examples/application_example)
10+
add_subdirectory(examples/application_c_example)
911
add_subdirectory(examples/display_example)
1012
add_subdirectory(examples/display_c_example)
1113
add_subdirectory(examples/id_allocator_example)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(application_c_example VERSION 0.0.1 LANGUAGES CXX)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add example program
10+
add_executable(application_c_example
11+
"main.c"
12+
)
13+
14+
# Link main library
15+
target_link_libraries(application_c_example PRIVATE nativeapi)
16+
17+
# Set example program properties
18+
set_target_properties(application_c_example PROPERTIES
19+
OUTPUT_NAME "application_c_example"
20+
)
21+
22+
# Set example program compile options (macOS only)
23+
if(APPLE)
24+
set_source_files_properties("main.cpp"
25+
PROPERTIES
26+
COMPILE_FLAGS "-x objective-c++"
27+
)
28+
endif()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(application_example VERSION 0.0.1 LANGUAGES CXX)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add example program
10+
add_executable(application_example
11+
"main.cpp"
12+
)
13+
14+
# Link main library
15+
target_link_libraries(application_example PRIVATE nativeapi)
16+
17+
# Set example program properties
18+
set_target_properties(application_example PROPERTIES
19+
OUTPUT_NAME "application_example"
20+
)
21+
22+
# Set example program compile options (macOS only)
23+
if(APPLE)
24+
set_source_files_properties("main.cpp"
25+
PROPERTIES
26+
COMPILE_FLAGS "-x objective-c++"
27+
)
28+
endif()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <iostream>
2+
#include <memory>
3+
4+
#include "nativeapi.h"
5+
6+
using namespace nativeapi;
7+
8+
int main() {
9+
std::cout << "Application Example" << std::endl;
10+
11+
// Get the Application singleton
12+
auto& app = Application::GetInstance();
13+
14+
// Initialize the application
15+
if (!app.Initialize()) {
16+
std::cerr << "Failed to initialize application" << std::endl;
17+
return 1;
18+
}
19+
20+
std::cout << "Application initialized successfully" << std::endl;
21+
std::cout << "Single instance: " << (app.IsSingleInstance() ? "Yes" : "No") << std::endl;
22+
23+
// Add event listeners
24+
auto started_listener = app.AddListener<ApplicationStartedEvent>(
25+
[](const ApplicationStartedEvent& event) {
26+
std::cout << "Application started event received" << std::endl;
27+
});
28+
29+
auto quit_listener = app.AddListener<ApplicationQuitRequestedEvent>(
30+
[](const ApplicationQuitRequestedEvent& event) {
31+
std::cout << "Application quit requested event received" << std::endl;
32+
});
33+
34+
auto activated_listener = app.AddListener<ApplicationActivatedEvent>(
35+
[](const ApplicationActivatedEvent& event) {
36+
std::cout << "Application activated event received" << std::endl;
37+
});
38+
39+
auto deactivated_listener = app.AddListener<ApplicationDeactivatedEvent>(
40+
[](const ApplicationDeactivatedEvent& event) {
41+
std::cout << "Application deactivated event received" << std::endl;
42+
});
43+
44+
45+
// Create a simple window
46+
auto& window_manager = WindowManager::GetInstance();
47+
48+
WindowOptions window_options;
49+
window_options.title = "Application Example Window";
50+
51+
auto window = window_manager.Create(window_options);
52+
if (!window) {
53+
std::cerr << "Failed to create window" << std::endl;
54+
return 1;
55+
}
56+
57+
// Set as primary window
58+
app.SetPrimaryWindow(window);
59+
60+
std::cout << "Window created successfully" << std::endl;
61+
std::cout << "Window ID: " << window->GetId() << std::endl;
62+
63+
// Show the window
64+
window->Show();
65+
66+
std::cout << "Starting application event loop..." << std::endl;
67+
std::cout << "Press Ctrl+C to quit" << std::endl;
68+
69+
// Run the application
70+
int exit_code = app.Run();
71+
72+
std::cout << "Application exited with code: " << exit_code << std::endl;
73+
74+
// Clean up listeners
75+
app.RemoveListener(started_listener);
76+
app.RemoveListener(quit_listener);
77+
app.RemoveListener(activated_listener);
78+
app.RemoveListener(deactivated_listener);
79+
80+
return exit_code;
81+
}

include/nativeapi.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#pragma once
22

3+
#ifdef __cplusplus
4+
// C++ API headers
35
#include "../src/accessibility_manager.h"
6+
#include "../src/application.h"
7+
#include "../src/application_event.h"
48
#include "../src/app_runner.h"
59
#include "../src/display.h"
610
#include "../src/display_event.h"
@@ -19,9 +23,11 @@
1923
#include "../src/window.h"
2024
#include "../src/window_event.h"
2125
#include "../src/window_manager.h"
26+
#endif
2227

23-
// Include necessary headers for C API
28+
// C API headers (available for both C and C++)
2429
#include "../src/capi/accessibility_manager_c.h"
30+
#include "../src/capi/application_c.h"
2531
#include "../src/capi/app_runner_c.h"
2632
#include "../src/capi/display_c.h"
2733
#include "../src/capi/display_manager_c.h"

src/application.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "application.h"
2+
3+
namespace nativeapi {
4+
5+
Application& Application::GetInstance() {
6+
static Application instance;
7+
return instance;
8+
}
9+
10+
} // namespace nativeapi

0 commit comments

Comments
 (0)