obs_wrapper/
lib.rs

1//! # Rust OBS Wrapper
2//!
3//! A safe wrapper around the OBS API, useful for creating OBS sources, filters
4//! and effects.
5//!
6//! ## Usage
7//!
8//! In your `Cargo.toml` file add the following section, substituting
9//! `<module-name>` for the name of the module:
10//!
11//! ```toml
12//! [dependencies]
13//! obs-wrapper = "0.1"
14//!
15//! [lib]
16//! name = "<module-name>"
17//! crate-type = ["cdylib"]
18//! ```
19//!
20//! The process for creating a plugin is:
21//! 1. Create a struct that implements Module
22//! 1. Create a struct that will store the plugin state
23//! 1. Implement the required traits for the module
24//! 1. Enable the traits which have been enabled in the module `load` method
25//!
26//! ~~~
27//! use obs_wrapper::{
28//!     // Everything required for modules
29//!     prelude::*,
30//!     // Everything required for creating a source
31//!     source::*,
32//!     // Macro for registering modules
33//!     obs_register_module,
34//!     // Macro for creating strings
35//!     obs_string,
36//! };
37//!
38//! // The module that will handle creating the source.
39//! struct TestModule {
40//!     context: ModuleContext
41//! };
42//!
43//! // The source that will be shown inside OBS.
44//! struct TestSource;
45//!
46//! // Implement the Sourceable trait for TestSource, this is required for each
47//! // source.
48//! // It allows you to specify the source ID and type.
49//! impl Sourceable for TestSource {
50//!     fn get_id() -> ObsString {
51//!         obs_string!("test_source")
52//!     }
53//!
54//!     fn get_type() -> SourceType {
55//!         SourceType::FILTER
56//!     }
57//!
58//!     fn create(
59//!         create: &mut CreatableSourceContext<Self>,
60//!         _source: SourceContext
61//!     ) -> Self {
62//!         Self
63//!     }
64//! }
65//!
66//! // Allow OBS to show a name for the source
67//! impl GetNameSource for TestSource {
68//!     fn get_name() -> ObsString {
69//!         obs_string!("Test Source")
70//!     }
71//! }
72//!
73//! // Implement the Module trait for TestModule. This will handle the creation
74//! // of the source and has some methods for telling OBS a bit about itself.
75//! impl Module for TestModule {
76//!     fn new(context: ModuleContext) -> Self {
77//!         Self { context }
78//!     }
79//!
80//!     fn get_ctx(&self) -> &ModuleContext {
81//!         &self.context
82//!     }
83//!    
84//!     // Load the module - create all sources, returning true if all went
85//!     // well.
86//!     fn load(&mut self, load_context: &mut LoadContext) -> bool {
87//!         // Create the source
88//!         let source = load_context
89//!             .create_source_builder::<TestSource>()
90//!             // Since GetNameSource is implemented, this method needs to be
91//!             // called to enable it.
92//!             .enable_get_name()
93//!             .build();
94//!    
95//!         // Tell OBS about the source so that it will show it.
96//!         load_context.register_source(source);
97//!    
98//!         // Nothing could have gone wrong, so return true.
99//!         true
100//!     }
101//!    
102//!     fn description() -> ObsString {
103//!         obs_string!("A great test module.")
104//!     }
105//!
106//!     fn name() -> ObsString {
107//!         obs_string!("Test Module")
108//!     }
109//!
110//!     fn author() -> ObsString {
111//!         obs_string!("Bennett")
112//!     }
113//! }
114//! ~~~
115//!
116//! ### Installing
117//!
118//! 1. Run `cargo build --release`
119//! 2. Copy `/target/release/<module-name>.so` to your OBS plugins folder
120//! (`/usr/lib/obs-plugins/`) 3. The plugin should be available for use from
121//! inside OBS
122
123/// Raw bindings of OBS C API
124pub use obs_sys;
125
126/// `obs_data_t` handling
127pub mod data;
128/// Tools required for manipulating graphics in OBS
129pub mod graphics;
130mod hotkey;
131/// Logger for logging to OBS console
132pub mod log;
133/// Tools for creating modules
134pub mod module;
135/// Tools for creating outputs
136pub mod output;
137/// Tools for creating properties
138pub mod properties;
139/// Tools for creating sources
140pub mod source;
141/// String macros
142pub mod string;
143/// FFI pointer wrapper
144pub mod wrapper;
145
146mod native_enum;
147
148/// Re-exports of a bunch of popular tools
149pub mod prelude {
150    pub use crate::data::{DataArray, DataObj, FromDataItem};
151    pub use crate::module::*;
152    pub use crate::source::context::*;
153    pub use crate::string::*;
154}
155
156#[derive(Debug)]
157pub struct Error;
158
159impl std::error::Error for Error {}
160
161impl std::fmt::Display for Error {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        write!(f, "OBS Error")
164    }
165}
166
167pub type Result<T> = std::result::Result<T, Error>;