Skip to content

Commit b81454c

Browse files
committed
Add C API for window and app runner management
- Implement window_c and window_manager_c for window operations/events - Add app_runner_c for running the main event loop from C - Update nativeapi.h to include new C API headers - Remove unused nativeapi_c.h from display_example - Update build workflow formatting and artifact path
1 parent 5307fc4 commit b81454c

File tree

9 files changed

+1355
-38
lines changed

9 files changed

+1355
-38
lines changed

.github/workflows/build.yml

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,55 @@ name: Build
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
build:
1111
strategy:
1212
matrix:
13-
os: [ubuntu-latest, windows-latest, macos-latest]
13+
os:
14+
- ubuntu-latest
15+
windows-latest
16+
macos-latest
1417
include:
15-
- os: ubuntu-latest
16-
platform: linux
17-
- os: windows-latest
18-
platform: windows
18+
# - os: ubuntu-latest
19+
# platform: linux
20+
# - os: windows-latest
21+
# platform: windows
1922
- os: macos-latest
2023
platform: macos
2124

2225
runs-on: ${{ matrix.os }}
2326

2427
steps:
25-
- uses: actions/checkout@v4
26-
27-
- name: Set up CMake
28-
shell: bash
29-
run: |
30-
cmake --version
31-
if [ "${{ matrix.platform }}" = "linux" ]; then
32-
sudo apt-get update
33-
sudo apt-get install -y ninja-build libgtk-3-dev
34-
fi
35-
36-
- name: Configure CMake
37-
shell: bash
38-
run: |
39-
mkdir build
40-
cd build
41-
cmake .. -DCMAKE_BUILD_TYPE=Release
42-
43-
- name: Build Examples
44-
shell: bash
45-
run: |
46-
cd build
47-
cmake --build . --config Release
48-
49-
- name: Upload artifacts
50-
uses: actions/upload-artifact@v4
51-
with:
52-
name: examples-${{ matrix.platform }}
53-
path: build/examples/display_example/
28+
- uses: actions/checkout@v4
29+
30+
- name: Set up CMake
31+
shell: bash
32+
run: |
33+
cmake --version
34+
if [ "${{ matrix.platform }}" = "linux" ]; then
35+
sudo apt-get update
36+
sudo apt-get install -y ninja-build libgtk-3-dev
37+
fi
38+
39+
- name: Configure CMake
40+
shell: bash
41+
run: |
42+
mkdir build
43+
cd build
44+
cmake .. -DCMAKE_BUILD_TYPE=Release
45+
46+
- name: Build Examples
47+
shell: bash
48+
run: |
49+
cd build
50+
cmake --build . --config Release
51+
52+
- name: Upload artifacts
53+
uses: actions/upload-artifact@v4
54+
with:
55+
name: examples-${{ matrix.platform }}
56+
path: build/examples/display_example/

examples/display_example/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <sstream>
55
#include <thread>
66
#include "nativeapi.h"
7-
#include "nativeapi_c.h"
87

98
using nativeapi::Display;
109
using nativeapi::DisplayManager;
@@ -247,7 +246,7 @@ int main() {
247246
std::cout << " Primary: " << (display.is_primary ? "Yes" : "No") << std::endl;
248247
std::cout << std::endl;
249248
}
250-
249+
251250
// Clean up memory
252251
native_display_list_free(&display_list);
253252
} else {

include/nativeapi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
// Include necessary headers for C API
1818
#include "../src/capi/accessibility_manager_c.h"
19+
#include "../src/capi/app_runner_c.h"
1920
#include "../src/capi/display_c.h"
2021
#include "../src/capi/display_manager_c.h"
2122
#include "../src/capi/geometry_c.h"
2223
#include "../src/capi/keyboard_monitor_c.h"
24+
#include "../src/capi/window_c.h"
25+
#include "../src/capi/window_manager_c.h"

src/capi/app_runner_c.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "app_runner_c.h"
2+
#include <atomic>
3+
#include <memory>
4+
5+
#include "../app_runner.h"
6+
7+
using namespace nativeapi;
8+
9+
// Global state for the app runner
10+
static std::atomic<bool> g_is_running{false};
11+
static std::atomic<int> g_exit_code{0};
12+
13+
// Helper to convert native_window_t back to shared_ptr<Window>
14+
static std::shared_ptr<Window> GetWindowFromHandle(
15+
native_window_t window_handle) {
16+
if (!window_handle) {
17+
return nullptr;
18+
}
19+
20+
// Assuming native_window_handle structure from window_c.cpp
21+
struct native_window_handle {
22+
std::shared_ptr<Window> window;
23+
};
24+
25+
auto* handle = reinterpret_cast<native_window_handle*>(window_handle);
26+
return handle->window;
27+
}
28+
29+
extern "C" {
30+
31+
int native_app_runner_run(native_window_t window) {
32+
// Validate input
33+
if (!window) {
34+
return NATIVE_APP_EXIT_INVALID_WINDOW;
35+
}
36+
37+
auto window_ptr = GetWindowFromHandle(window);
38+
if (!window_ptr) {
39+
return NATIVE_APP_EXIT_INVALID_WINDOW;
40+
}
41+
42+
// Mark as running
43+
g_is_running.store(true);
44+
g_exit_code.store(0);
45+
46+
try {
47+
// Get the AppRunner instance and run the application
48+
AppRunner& runner = AppRunner::GetInstance();
49+
50+
// Run the main event loop
51+
int exit_code = runner.Run(window_ptr);
52+
53+
// Mark as no longer running
54+
g_is_running.store(false);
55+
56+
// Use the stored exit code if terminate was called, otherwise use the
57+
// returned code
58+
int final_exit_code = g_exit_code.load();
59+
if (final_exit_code == 0) {
60+
final_exit_code = exit_code;
61+
}
62+
63+
return final_exit_code;
64+
65+
} catch (const std::exception& e) {
66+
// Handle any exceptions that might occur
67+
g_is_running.store(false);
68+
69+
return NATIVE_APP_EXIT_FAILURE;
70+
}
71+
}
72+
73+
bool native_app_runner_is_running(void) {
74+
return g_is_running.load();
75+
}
76+
77+
} // extern "C"

src/capi/app_runner_c.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include <stdint.h>
5+
6+
#if _WIN32
7+
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
8+
#else
9+
#define FFI_PLUGIN_EXPORT
10+
#endif
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#include "window_c.h"
17+
18+
/**
19+
* App runner exit codes
20+
*/
21+
typedef enum {
22+
NATIVE_APP_EXIT_SUCCESS = 0,
23+
NATIVE_APP_EXIT_FAILURE = 1,
24+
NATIVE_APP_EXIT_INVALID_WINDOW = 2
25+
} native_app_exit_code_t;
26+
27+
/**
28+
* Run the application with the specified window.
29+
* This function starts the main event loop and blocks until the application
30+
* exits.
31+
*
32+
* @param window The window to run the application with (must not be NULL)
33+
* @return Exit code of the application (0 for success)
34+
*/
35+
FFI_PLUGIN_EXPORT
36+
int native_app_runner_run(native_window_t window);
37+
38+
/**
39+
* Check if the application is currently running.
40+
*
41+
* @return true if the app is running, false otherwise
42+
*/
43+
FFI_PLUGIN_EXPORT
44+
bool native_app_runner_is_running(void);
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif

0 commit comments

Comments
 (0)