-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
200 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use std::ffi::{c_char, CStr}; | ||
|
||
use clap_sys::ext::gui::*; | ||
use clap_sys::plugin::*; | ||
|
||
use super::instance::Instance; | ||
use crate::parent::{Parent, RawParent}; | ||
use crate::{Editor, Plugin}; | ||
|
||
impl<P: Plugin> Instance<P> { | ||
pub(super) const GUI: clap_plugin_gui = clap_plugin_gui { | ||
is_api_supported: Some(Self::gui_is_api_supported), | ||
get_preferred_api: Some(Self::gui_get_preferred_api), | ||
create: Some(Self::gui_create), | ||
destroy: Some(Self::gui_destroy), | ||
set_scale: Some(Self::gui_set_scale), | ||
get_size: Some(Self::gui_get_size), | ||
can_resize: Some(Self::gui_can_resize), | ||
get_resize_hints: Some(Self::gui_get_resize_hints), | ||
adjust_size: Some(Self::gui_adjust_size), | ||
set_size: Some(Self::gui_set_size), | ||
set_parent: Some(Self::gui_set_parent), | ||
set_transient: Some(Self::gui_set_transient), | ||
suggest_title: Some(Self::gui_suggest_title), | ||
show: Some(Self::gui_show), | ||
hide: Some(Self::gui_hide), | ||
}; | ||
|
||
#[cfg(target_os = "windows")] | ||
const API: &CStr = CLAP_WINDOW_API_WIN32; | ||
|
||
#[cfg(target_os = "macos")] | ||
const API: &CStr = CLAP_WINDOW_API_COCOA; | ||
|
||
#[cfg(target_os = "linux")] | ||
const API: &CStr = CLAP_WINDOW_API_X11; | ||
|
||
unsafe extern "C" fn gui_is_api_supported( | ||
_plugin: *const clap_plugin, | ||
api: *const c_char, | ||
is_floating: bool, | ||
) -> bool { | ||
if is_floating { | ||
return false; | ||
} | ||
|
||
CStr::from_ptr(api) == Self::API | ||
} | ||
|
||
unsafe extern "C" fn gui_get_preferred_api( | ||
_plugin: *const clap_plugin, | ||
api: *mut *const c_char, | ||
is_floating: *mut bool, | ||
) -> bool { | ||
*is_floating = false; | ||
|
||
*api = Self::API.as_ptr(); | ||
|
||
true | ||
} | ||
|
||
unsafe extern "C" fn gui_create( | ||
plugin: *const clap_plugin, | ||
api: *const c_char, | ||
is_floating: bool, | ||
) -> bool { | ||
if !Self::gui_is_api_supported(plugin, api, is_floating) { | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
unsafe extern "C" fn gui_destroy(plugin: *const clap_plugin) { | ||
let instance = &*(plugin as *const Self); | ||
let main_thread_state = &mut *instance.main_thread_state.get(); | ||
|
||
main_thread_state.editor = None; | ||
} | ||
|
||
unsafe extern "C" fn gui_set_scale(_plugin: *const clap_plugin, _scale: f64) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_get_size( | ||
plugin: *const clap_plugin, | ||
width: *mut u32, | ||
height: *mut u32, | ||
) -> bool { | ||
let instance = &*(plugin as *const Self); | ||
let main_thread_state = &mut *instance.main_thread_state.get(); | ||
|
||
if let Some(editor) = &main_thread_state.editor { | ||
let size = editor.size(); | ||
|
||
*width = size.width.round() as u32; | ||
*height = size.height.round() as u32; | ||
|
||
return true; | ||
} | ||
|
||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_can_resize(_plugin: *const clap_plugin) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_get_resize_hints( | ||
_plugin: *const clap_plugin, | ||
_hints: *mut clap_gui_resize_hints, | ||
) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_adjust_size( | ||
_plugin: *const clap_plugin, | ||
_width: *mut u32, | ||
_height: *mut u32, | ||
) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_set_size( | ||
_plugin: *const clap_plugin, | ||
_width: u32, | ||
_height: u32, | ||
) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_set_parent( | ||
plugin: *const clap_plugin, | ||
window: *const clap_window, | ||
) -> bool { | ||
let window = &*window; | ||
|
||
if CStr::from_ptr(window.api) != Self::API { | ||
return false; | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
let raw_parent = { RawParent::Win32(window.specific.win32) }; | ||
|
||
#[cfg(target_os = "macos")] | ||
let raw_parent = { RawParent::Cocoa(window.specific.cocoa) }; | ||
|
||
#[cfg(target_os = "linux")] | ||
let raw_parent = { RawParent::X11(window.specific.x11) }; | ||
|
||
let instance = &*(plugin as *const Self); | ||
let main_thread_state = &mut *instance.main_thread_state.get(); | ||
|
||
let editor = main_thread_state.plugin.editor(Parent::from_raw(raw_parent)); | ||
main_thread_state.editor = Some(editor); | ||
|
||
true | ||
} | ||
|
||
unsafe extern "C" fn gui_set_transient( | ||
_plugin: *const clap_plugin, | ||
_window: *const clap_window, | ||
) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_suggest_title(_plugin: *const clap_plugin, _title: *const c_char) {} | ||
|
||
unsafe extern "C" fn gui_show(_plugin: *const clap_plugin) -> bool { | ||
false | ||
} | ||
|
||
unsafe extern "C" fn gui_hide(_plugin: *const clap_plugin) -> bool { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters