use crate::*;
use crate::aegp::*;
pub struct ErrReportState((ae_sys::AEGP_ErrReportState, bool));
impl Drop for ErrReportState {
fn drop(&mut self) {
if let Ok(util) = UtilitySuite::new() {
let _ = util.end_quiet_errors(self.0.0, self.0.1);
}
}
}
define_suite!(
UtilitySuite,
AEGP_UtilitySuite6,
kAEGPUtilitySuite,
kAEGPUtilitySuiteVersion6
);
impl UtilitySuite {
pub fn new() -> Result<Self, Error> {
crate::Suite::new()
}
pub fn report_info(&self, plugin_id: PluginId, info_string: &str) -> Result<(), Error> {
let info_string = CString::new(info_string).map_err(|_| Error::InvalidParms)?;
call_suite_fn!(self, AEGP_ReportInfo, plugin_id, info_string.as_ptr())
}
pub fn report_info_unicode(&self, plugin_id: PluginId, info_string: &str) -> Result<(), Error> {
let info_string = U16CString::from_str(info_string).map_err(|_| Error::InvalidParms)?;
call_suite_fn!(self, AEGP_ReportInfoUnicode, plugin_id, info_string.as_ptr())
}
pub fn start_quiet_errors(&self, report_quieted_errors_on_drop: bool) -> Result<ErrReportState, Error> {
Ok(ErrReportState((call_suite_fn_single!(self, AEGP_StartQuietErrors -> ae_sys::AEGP_ErrReportState)?, report_quieted_errors_on_drop)))
}
pub fn end_quiet_errors(&self, err_state: ae_sys::AEGP_ErrReportState, report_quieted_errors: bool) -> Result<(), Error> {
call_suite_fn!(self, AEGP_EndQuietErrors, report_quieted_errors as _, &err_state as *const _ as *mut _)
}
pub fn start_undo_group(&self, undo_name: &str) -> Result<(), Error> {
let undo_name = CString::new(undo_name).map_err(|_| Error::InvalidParms)?;
call_suite_fn!(self, AEGP_StartUndoGroup, undo_name.as_ptr())
}
pub fn end_undo_group(&self) -> Result<(), Error> {
call_suite_fn!(self, AEGP_EndUndoGroup,)
}
pub fn register_with_aegp_refcon<T: AegpSeal>(&self, global_refcon: T, plugin_name: &str) -> Result<PluginId, Error> {
let refcon = Box::into_raw(Box::new(global_refcon));
let plugin_name = CString::new(plugin_name).map_err(|_| Error::InvalidParms)?;
call_suite_fn_single!(self, AEGP_RegisterWithAEGP -> ae_sys::AEGP_PluginID, refcon as _, plugin_name.as_ptr())
}
pub fn register_with_aegp(&self, plugin_name: &str) -> Result<PluginId, Error> {
let plugin_name = CString::new(plugin_name).map_err(|_| Error::InvalidParms)?;
call_suite_fn_single!(self, AEGP_RegisterWithAEGP -> ae_sys::AEGP_PluginID, std::ptr::null_mut() as _, plugin_name.as_ptr())
}
pub fn main_hwnd(&self) -> Result<*mut std::ffi::c_void, Error> {
let mut hwnd = std::ptr::null_mut();
call_suite_fn!(self, AEGP_GetMainHWND, &mut hwnd as *mut _ as *mut _)?;
Ok(hwnd)
}
pub fn show_hide_all_floaters(&self, include_tool_pal: bool) -> Result<(), Error> {
call_suite_fn!(self, AEGP_ShowHideAllFloaters, include_tool_pal as _)
}
pub fn paint_palette_foreground_color(&self) -> Result<pf::PixelF64, Error> {
let color = call_suite_fn_single!(self, AEGP_PaintPalGetForeColor -> ae_sys::AEGP_ColorVal)?;
Ok(unsafe { std::mem::transmute(color) })
}
pub fn paint_palette_background_color(&self) -> Result<pf::PixelF64, Error> {
let color = call_suite_fn_single!(self, AEGP_PaintPalGetBackColor -> ae_sys::AEGP_ColorVal)?;
Ok(unsafe { std::mem::transmute(color) })
}
pub fn paint_palette_set_foreground_color(&self, color: pf::PixelF64) -> Result<(), Error> {
let color = unsafe { std::mem::transmute(color) };
call_suite_fn!(self, AEGP_PaintPalSetForeColor, &color as *const _)
}
pub fn paint_palette_set_background_color(&self, color: pf::PixelF64) -> Result<(), Error> {
let color = unsafe { std::mem::transmute(color) };
call_suite_fn!(self, AEGP_PaintPalSetBackColor, &color as *const _)
}
pub fn character_palette_fill_color(&self) -> Result<(bool, pf::PixelF64), Error> {
let (is_defined, color) = call_suite_fn_double!(self, AEGP_CharPalGetFillColor -> ae_sys::A_Boolean, ae_sys::AEGP_ColorVal)?;
Ok((is_defined != 0, unsafe { std::mem::transmute(color) }))
}
pub fn character_palette_stroke_color(&self) -> Result<(bool, pf::PixelF64), Error> {
let (is_defined, color) = call_suite_fn_double!(self, AEGP_CharPalGetStrokeColor -> ae_sys::A_Boolean, ae_sys::AEGP_ColorVal)?;
Ok((is_defined != 0, unsafe { std::mem::transmute(color) }))
}
pub fn character_palette_set_fill_color(&self, color: pf::PixelF64) -> Result<(), Error> {
let color = unsafe { std::mem::transmute(color) };
call_suite_fn!(self, AEGP_CharPalSetFillColor, &color as *const _)
}
pub fn character_palette_set_stroke_color(&self, color: pf::PixelF64) -> Result<(), Error> {
let color = unsafe { std::mem::transmute(color) };
call_suite_fn!(self, AEGP_CharPalSetStrokeColor, &color as *const _)
}
pub fn character_palette_is_fill_color_ui_frontmost(&self) -> Result<bool, Error> {
Ok(call_suite_fn_single!(self, AEGP_CharPalIsFillColorUIFrontmost -> ae_sys::A_Boolean)? != 0)
}
pub fn convert_fp_long_to_hsf_ratio(&self, number: f64) -> Result<Ratio, Error> {
Ok(call_suite_fn_single!(self, AEGP_ConvertFpLongToHSFRatio -> ae_sys::A_Ratio, number)?.into())
}
pub fn convert_hsf_ratio_to_fp_long(&self, ratio: Ratio) -> Result<f64, Error> {
Ok(call_suite_fn_single!(self, AEGP_ConvertHSFRatioToFpLong -> f64, ratio.into())?)
}
pub fn cause_idle_routines_to_be_called(&self) -> Result<(), Error> {
call_suite_fn!(self, AEGP_CauseIdleRoutinesToBeCalled,)
}
pub fn suppress_interactive_ui(&self) -> Result<bool, Error> {
Ok(call_suite_fn_single!(self, AEGP_GetSuppressInteractiveUI -> ae_sys::A_Boolean)? != 0)
}
pub fn write_to_os_console(&self, text: &str) -> Result<(), Error> {
let text = CString::new(text).map_err(|_| Error::InvalidParms)?;
call_suite_fn!(self, AEGP_WriteToOSConsole, text.as_ptr())
}
pub fn write_to_debug_log(&self, subsystem: &str, event_type: &str, info: &str) -> Result<(), Error> {
let subsystem = CString::new(subsystem).map_err(|_| Error::InvalidParms)?;
let event_type = CString::new(event_type).map_err(|_| Error::InvalidParms)?;
let info = CString::new(info).map_err(|_| Error::InvalidParms)?;
call_suite_fn!(self, AEGP_WriteToDebugLog, subsystem.as_ptr(), event_type.as_ptr(), info.as_ptr())
}
pub fn last_error_message(&self) -> Result<(String, Error), Error> {
let mut buffer = vec![0u8; 512];
let err = call_suite_fn_single!(self, AEGP_GetLastErrorMessage -> ae_sys::A_Err, buffer.len() as _, buffer.as_mut_ptr() as *mut _)?;
Ok((String::from_utf8_lossy(&buffer).to_string(), Error::from(err)))
}
pub fn is_scripting_available(&self) -> Result<bool, Error> {
Ok(call_suite_fn_single!(self, AEGP_IsScriptingAvailable -> ae_sys::A_Boolean)? != 0)
}
pub fn execute_script(&self, plugin_id: PluginId, script: &str, platform_encoding: bool) -> Result<(String, String), Error> {
let script = CString::new(script).map_err(|_| Error::InvalidParms)?;
let (result, error_string) = call_suite_fn_double!(self,
AEGP_ExecuteScript -> ae_sys::AEGP_MemHandle, ae_sys::AEGP_MemHandle,
plugin_id,
script.as_ptr(),
platform_encoding as _
)?;
let result = MemHandle::<u8>::from_raw(result)?;
let error_string = MemHandle::<u8>::from_raw(error_string)?;
let result_len = result.len()?;
let error_string_len = error_string.len()?;
let result = unsafe { std::slice::from_raw_parts(result .lock()?.as_ptr(), result_len) };
let error_string = unsafe { std::slice::from_raw_parts(error_string.lock()?.as_ptr(), error_string_len) };
Ok((
std::str::from_utf8(result) .map_err(|_| Error::InvalidParms)?.trim_end_matches('\0').to_owned(),
std::str::from_utf8(error_string).map_err(|_| Error::InvalidParms)?.trim_end_matches('\0').to_owned()
))
}
pub fn host_is_activated(&self) -> Result<bool, Error> {
Ok(call_suite_fn_single!(self, AEGP_HostIsActivated -> ae_sys::A_Boolean)? != 0)
}
pub fn plugin_platform_ref(&self, plugin_id: PluginId) -> Result<*mut std::ffi::c_void, Error> {
let mut plat_ref = std::ptr::null_mut();
call_suite_fn!(self, AEGP_GetPluginPlatformRef, plugin_id, &mut plat_ref as *mut _ as *mut _)?;
Ok(plat_ref)
}
pub fn update_font_list(&self) -> Result<(), Error> {
call_suite_fn!(self, AEGP_UpdateFontList,)
}
pub fn plugin_paths(&self, plugin_id: PluginId, path_type: GetPathTypes) -> Result<String, Error> {
let mem_handle = call_suite_fn_single!(self, AEGP_GetPluginPaths -> ae_sys::AEGP_MemHandle, plugin_id, path_type.into())?;
Ok(unsafe {
U16CString::from_ptr_str(
MemHandle::<u16>::from_raw(mem_handle)?.lock()?.as_ptr(),
).to_string_lossy()
})
}
}
define_enum! {
ae_sys::AEGP_GetPathTypes,
GetPathTypes {
Plugin = ae_sys::AEGP_GetPathTypes_PLUGIN,
UserPlugin = ae_sys::AEGP_GetPathTypes_USER_PLUGIN,
AllUserPlugin = ae_sys::AEGP_GetPathTypes_ALLUSER_PLUGIN,
App = ae_sys::AEGP_GetPathTypes_APP,
}
}