wxdragon_sys/lib.rs
1#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]
2
3// Include the generated FFI bindings (from bindgen)
4include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
5
6// Conditionally include the pre-generated constants based on target OS
7// Assumes files are located in `rust/wxdragon-sys/src/generated_constants/`
8#[cfg(target_os = "macos")]
9include!("generated_constants/wx_osx_constants.rs");
10
11#[cfg(target_os = "windows")]
12include!("generated_constants/wx_msw_constants.rs");
13
14#[cfg(target_os = "linux")]
15include!("generated_constants/wx_gtk_constants.rs");
16
17// Fallback or error for unsupported OS for constants, if necessary.
18// Alternatively, you could have a `wx_common_constants.rs` if some constants are universal
19// and only OS-specific parts are in the files above.
20#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
21compile_error!("Target OS not supported by pre-generated constants. Please add a constants file for this OS.");
22
23mod logging4c;
24
25// Type alias for convenience maybe?
26// pub type wxWindow_t = self::wxd_Window_t; // Example
27
28// Additional constants or utility functions specific to the sys crate itself
29// (if any are ever needed, usually this file is minimal)
30
31// REMOVED redundant/incorrect manual definitions and extern block.
32// Bindgen now generates wxd_EVT_* constants directly from wxdragon.h
33// into the included bindings.rs file.
34
35// Need to find the actual values for these constants. // REMOVED Comment
36
37// Add necessary imports for the drop function
38use std::cell::RefCell;
39use std::os::raw::c_void;
40
41// Type placeholder for user data until a proper type is defined in the safe wrapper
42type WindowUserData = (); // Replace with actual user data type later if needed
43
44/// Function called by C++ (WxdRustClientData destructor) to drop the Rust `Box<RefCell<T>>`.
45/// # Safety
46/// The caller (C++) must ensure `user_data_ptr` is a valid pointer obtained
47/// from `Box::into_raw(Box::new(RefCell::new(data)))` and that it hasn't been
48/// dropped or invalidated since.
49#[unsafe(no_mangle)]
50#[allow(clippy::not_unsafe_ptr_arg_deref)]
51pub extern "C" fn drop_rust_refcell_box(user_data_ptr: *mut c_void) {
52 if !user_data_ptr.is_null() {
53 // Reconstitute the Box and let it drop, freeing the memory
54 // and dropping the RefCell<WindowUserData>.
55 let _boxed_refcell: Box<RefCell<WindowUserData>> =
56 unsafe { Box::from_raw(user_data_ptr as *mut RefCell<WindowUserData>) };
57 // Drop happens automatically when `_boxed_refcell` goes out of scope here.
58 } else {
59 // Optional: Log a warning or handle null pointer case if necessary
60 // eprintln!("Warning: drop_rust_refcell_box called with null pointer.");
61 }
62}
63
64/// Function to properly free a string that was allocated by Rust using CString::into_raw().
65/// This must be called instead of C's free() for strings allocated by Rust.
66/// # Safety
67/// The caller (C++) must ensure `str_ptr` is a valid pointer obtained from
68/// `CString::into_raw()` and that it hasn't been freed already.
69#[unsafe(no_mangle)]
70#[allow(clippy::not_unsafe_ptr_arg_deref)]
71pub extern "C" fn wxd_Variant_Free_Rust_String(str_ptr: *mut std::os::raw::c_char) {
72 if !str_ptr.is_null() {
73 // Reconstitute the CString and let it drop, properly freeing the memory
74 // that was allocated by Rust's allocator
75 unsafe {
76 let _cstring = std::ffi::CString::from_raw(str_ptr);
77 // Drop happens automatically when `_cstring` goes out of scope here.
78 }
79 }
80}