asdf_overlay/lib.rs
1//! ## Asdf Overlay
2//! Asdf overlay let you put overlay infront of existing windows gpu framebuffer.
3//!
4//! It hooks various graphics API call to detect graphical windows in the process.
5//! Asdf overlay automatically decides which graphics API the window is using,
6//! chooses suitable renderer.
7//!
8//! It can also capture inputs going through the target window.
9//! You can listen them or even block them from reaching application handlers.
10//!
11//! ## Example
12//! ```no_run
13//! use asdf_overlay::initialize;
14//! use asdf_overlay::event_sink::OverlayEventSink;
15//!
16//! let module_handle, window_hwnd;
17//! // Initialize asdf-overlay.
18//! initialize(module_handle).expect("initialization failed");
19//!
20//! // Initialize Event sink.
21//! // Without setting it, the overlay will not render.
22//! // This is intended because windows state will be out of sync if you miss any events.
23//! OverlayEventSink::set(move |event| {
24//! // Do something with events.
25//! });
26//!
27//! Backends::with_backend(window_hwnd, |backend| {
28//! // Do something with overlay window backend.
29//! });
30//! ```
31
32#[allow(unsafe_op_in_unsafe_fn, clippy::all)]
33/// Generated OpenGL bindings and global function tables.
34mod gl {
35 include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
36}
37
38#[allow(unsafe_op_in_unsafe_fn, clippy::all)]
39/// Generated WGL bindings and global function tables.
40mod wgl {
41 include!(concat!(env!("OUT_DIR"), "/wgl_bindings.rs"));
42}
43
44pub mod backend;
45pub mod event_sink;
46pub mod layout;
47pub mod surface;
48
49mod hook;
50mod interop;
51mod renderer;
52mod resources;
53mod texture;
54mod types;
55mod util;
56
57use anyhow::{Context, bail};
58use once_cell::sync::OnceCell;
59use windows::Win32::Foundation::HINSTANCE;
60
61/// Module handle of the overlay.
62static INSTANCE: OnceCell<usize> = OnceCell::new();
63
64#[inline]
65/// Get overlay [`HINSTANCE`]
66pub(crate) fn instance() -> HINSTANCE {
67 HINSTANCE(*INSTANCE.get().unwrap() as _)
68}
69
70/// Initialize overlay, hooks.
71///
72/// * Calling more than once will fail.
73/// * Calling with holding loader lock (DllMain) will fail.
74/// * If given `hinstance` is invalid, some resources may not appear correctly.
75pub fn initialize(hinstance: usize) -> anyhow::Result<()> {
76 if INSTANCE.set(hinstance).is_err() {
77 bail!("Already initialized");
78 }
79
80 hook::install(HINSTANCE(hinstance as _)).context("hook initialization failed")?;
81 Ok(())
82}