dear_imgui_sys/lib.rs
1//! Low-level FFI bindings for Dear ImGui (via cimgui C API) with docking support
2//!
3//! This crate provides raw, unsafe bindings to Dear ImGui using the cimgui C API,
4//! specifically targeting the docking branch (multi-viewport capable).
5//!
6//! ## Features
7//!
8//! - **docking**: Always enabled in this crate
9//! - **freetype**: Enable FreeType font rasterizer support
10//! - **wasm**: Enable WebAssembly compatibility
11//! - **backend-shim-\***: Expose selected repository-owned backend shim modules
12//! for low-level integrations
13//!
14//! ## WebAssembly Support
15//!
16//! When the `wasm` feature is enabled, this crate provides full WASM compatibility:
17//! - Disables platform-specific functions (file I/O, shell functions, etc.)
18//! - Configures Dear ImGui for WASM environment
19//! - Compatible with wasm-bindgen and web targets
20//!
21//! ## Safety
22//!
23//! This crate provides raw FFI bindings and is inherently unsafe. Users should
24//! prefer the high-level `dear-imgui-rs` crate for safe Rust bindings.
25//!
26//! ## Usage
27//!
28//! This crate is typically not used directly. Instead, use the `dear-imgui-rs` crate
29//! which provides safe, idiomatic Rust bindings built on top of these FFI bindings.
30//!
31//! ## Backend Shim Modules
32//!
33//! For downstream backend crates, engine integrations, and platform-specific
34//! application glue, `dear-imgui-sys` can expose selected official backend
35//! pieces through `backend_shim::*`.
36//!
37//! Important boundary:
38//!
39//! - these modules expose the repository-owned C shim ABI
40//! - they do not expose upstream `imgui_impl_*` C++ symbol names as a stable
41//! Rust-facing contract
42//! - enabling `backend-shim-*` features does not imply that `dear-imgui-rs`
43//! already owns a safe wrapper for those backends
44//!
45//! Typical feature gates:
46//!
47//! - `backend-shim-opengl3`
48//! - `backend-shim-sdlrenderer3`
49//! - `backend-shim-android`
50//! - `backend-shim-win32`
51//! - `backend-shim-dx11`
52//!
53//! ## Android Direction
54//!
55//! The current Android story is intentionally low-level but supported.
56//!
57//! ```toml
58//! [dependencies]
59//! dear-imgui-rs = "0.10"
60//! dear-imgui-sys = { version = "0.10", features = ["backend-shim-android", "backend-shim-opengl3"] }
61//! ```
62//!
63//! Recommended split of responsibilities:
64//!
65//! - `dear-imgui-rs` owns the safe core `Context`, `Io`, frame lifecycle, and
66//! render snapshots
67//! - `dear-imgui-sys::backend_shim::{android, opengl3}` exposes the low-level
68//! official backend pieces
69//! - the Android application still owns lifecycle glue, input translation
70//! strategy, EGL / GLES context creation, and packaging
71//!
72//! The repository's concrete reference for this path is
73//! `examples-android/dear-imgui-android-smoke/`, which now carries a minimal
74//! NativeActivity + EGL / GLES3 render loop proving that downstream users can
75//! build Android support on top of `dear-imgui-rs` + `dear-imgui-sys` even
76//! before a dedicated first-party Android convenience crate exists.
77
78#![allow(non_upper_case_globals)]
79#![allow(non_camel_case_types)]
80#![allow(non_snake_case)]
81#![allow(dead_code)]
82#![allow(unnecessary_transmutes)]
83#![allow(clippy::all)]
84// Bindgen may derive Eq/Hash for structs containing function pointers.
85// New Clippy lint warns these comparisons are unpredictable; suppress for raw FFI types.
86#![allow(unpredictable_function_pointer_comparisons)]
87
88// Bindings are generated into OUT_DIR and included via a submodule so that
89// possible inner attributes in the generated file are accepted at module root.
90mod ffi;
91pub use ffi::*;
92
93/// Optional backend shim entry points for downstream integrations.
94///
95/// These modules expose the repository-owned C shim ABI for selected official
96/// Dear ImGui backends. They do not expose the upstream C++ symbols directly,
97/// and they do not imply that `dear-imgui-sys` or `dear-imgui-rs` owns full
98/// safe integration for those backends.
99pub mod backend_shim;
100
101// This project always builds Dear ImGui with `IMGUI_USE_WCHAR32`, so `ImWchar` must be 32-bit.
102const _: [(); 4] = [(); std::mem::size_of::<ImWchar>()];
103
104// Ensure common ImGui typedefs are available even if bindgen doesn't emit them explicitly
105
106// cimgui exposes typed vectors (e.g., ImVector_ImVec2) instead of a generic ImVector<T>.
107// The sys crate intentionally avoids adding higher-level helpers here.
108
109// cimgui C API avoids C++ ABI pitfalls; no MSVC-specific conversions are required.
110
111// Re-export commonly used types for convenience
112pub use ImColor as Color;
113pub use ImVec2 as Vector2;
114pub use ImVec4 as Vector4;
115
116/// Version information for the Dear ImGui library
117pub const IMGUI_VERSION: &str = env!("CARGO_PKG_VERSION");
118
119/// Docking features are always available in this crate
120pub const HAS_DOCKING: bool = true;
121
122/// Check if FreeType support is available
123#[cfg(feature = "freetype")]
124pub const HAS_FREETYPE: bool = true;
125
126#[cfg(not(feature = "freetype"))]
127pub const HAS_FREETYPE: bool = false;
128
129/// Check if WASM support is available
130#[cfg(feature = "wasm")]
131pub const HAS_WASM: bool = true;
132
133#[cfg(not(feature = "wasm"))]
134pub const HAS_WASM: bool = false;
135
136// (No wasm-specific shims are required when using shared memory import style.)
137
138impl ImVec2 {
139 #[inline]
140 pub const fn new(x: f32, y: f32) -> ImVec2 {
141 ImVec2 { x, y }
142 }
143
144 #[inline]
145 pub const fn zero() -> ImVec2 {
146 ImVec2 { x: 0.0, y: 0.0 }
147 }
148}
149
150impl From<[f32; 2]> for ImVec2 {
151 #[inline]
152 fn from(array: [f32; 2]) -> ImVec2 {
153 ImVec2::new(array[0], array[1])
154 }
155}
156
157impl From<(f32, f32)> for ImVec2 {
158 #[inline]
159 fn from((x, y): (f32, f32)) -> ImVec2 {
160 ImVec2::new(x, y)
161 }
162}
163
164impl From<ImVec2> for [f32; 2] {
165 #[inline]
166 fn from(v: ImVec2) -> [f32; 2] {
167 [v.x, v.y]
168 }
169}
170
171impl From<ImVec2> for (f32, f32) {
172 #[inline]
173 fn from(v: ImVec2) -> (f32, f32) {
174 (v.x, v.y)
175 }
176}
177
178impl From<mint::Vector2<f32>> for ImVec2 {
179 #[inline]
180 fn from(v: mint::Vector2<f32>) -> ImVec2 {
181 ImVec2::new(v.x, v.y)
182 }
183}
184
185#[cfg(feature = "glam")]
186impl From<glam::Vec2> for ImVec2 {
187 #[inline]
188 fn from(v: glam::Vec2) -> ImVec2 {
189 ImVec2::new(v.x, v.y)
190 }
191}
192
193impl ImVec4 {
194 #[inline]
195 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 {
196 ImVec4 { x, y, z, w }
197 }
198
199 #[inline]
200 pub const fn zero() -> ImVec4 {
201 ImVec4 {
202 x: 0.0,
203 y: 0.0,
204 z: 0.0,
205 w: 0.0,
206 }
207 }
208}
209
210impl From<[f32; 4]> for ImVec4 {
211 #[inline]
212 fn from(array: [f32; 4]) -> ImVec4 {
213 ImVec4::new(array[0], array[1], array[2], array[3])
214 }
215}
216
217impl From<(f32, f32, f32, f32)> for ImVec4 {
218 #[inline]
219 fn from((x, y, z, w): (f32, f32, f32, f32)) -> ImVec4 {
220 ImVec4::new(x, y, z, w)
221 }
222}
223
224impl From<ImVec4> for [f32; 4] {
225 #[inline]
226 fn from(v: ImVec4) -> [f32; 4] {
227 [v.x, v.y, v.z, v.w]
228 }
229}
230
231impl From<ImVec4> for (f32, f32, f32, f32) {
232 #[inline]
233 fn from(v: ImVec4) -> (f32, f32, f32, f32) {
234 (v.x, v.y, v.z, v.w)
235 }
236}
237
238impl From<mint::Vector4<f32>> for ImVec4 {
239 #[inline]
240 fn from(v: mint::Vector4<f32>) -> ImVec4 {
241 ImVec4::new(v.x, v.y, v.z, v.w)
242 }
243}
244
245#[cfg(feature = "glam")]
246impl From<glam::Vec4> for ImVec4 {
247 #[inline]
248 fn from(v: glam::Vec4) -> ImVec4 {
249 ImVec4::new(v.x, v.y, v.z, v.w)
250 }
251}