Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ tracing = { version = "0.1", optional = true }
x11rb = { version = "0.13.2", features = ["cursor", "dri3", "present", "resource_manager", "allow-unsafe-code", "dl-libxcb"], default-features = false }
xkbcommon-dl = { version = "0.4.2", features = ["x11"] }
x11-dl = { version = "2.21.0" }
calloop = "=0.14.4" # 0.14.5 has MSRV of 1.86
polling = "3.11.0"
slotmap = "1.1.1"
percent-encoding = "2.3.2"
bytemuck = { version = "1.25.2", features = ["extern_crate_alloc"] }
libloading = "0.8.9" # Libloading 0.9 is out but its MSRV is 1.88
rustix = "1.1.4"

[target.'cfg(target_os="windows")'.dependencies]
windows = { version = "0.62.2", features = [
Expand Down
4 changes: 3 additions & 1 deletion examples/external-wakeup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ fn run_thread(sender: Sender<Message>, waker: WindowWaker) {
loop {
let interval: f32 = rand::random_range(0.5..2.5);
std::thread::sleep(Duration::from_secs_f32(interval));
sender.send(Message::Hello).unwrap();
if sender.send(Message::Hello).is_err() {
break; // Receiver has hung up, we can just shut this down.
}
waker.request_poll();
}
}
4 changes: 2 additions & 2 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ fn main() -> Result<(), baseview::Error> {
})
})?;

let waker = window.waker();
//let waker = window.waker();
std::thread::spawn(move || loop {
std::thread::sleep(Duration::from_secs(5));

if tx.push(Message::Hello).is_err() {
println!("Failed sending message");
} else {
waker.request_poll();
//waker.request_poll();
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/plugin_clack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ crate-type = ["cdylib"]

[dependencies]
clack-plugin = "0.2.0"
clack-extensions = { version = "0.2.0", features = ["gui", "state", "clack-plugin", "raw-window-handle_06"] }
clack-extensions = { version = "0.2.0", features = ["clack-plugin", "raw-window-handle_06", "gui", "state", "timer", "posix-fd"] }
baseview = { path = "../..", features = ["tracing"] }
softbuffer = "0.4.8"
100 changes: 80 additions & 20 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::window_handler::OpenWindowExample;
use crate::ExamplePluginMainThread;
use baseview::dpi::*;
use baseview::host::{Host, HostCallbacks, HostMainThreadCaller};
use baseview::host::{Host, HostCallbacks, HostTimerSupport, TimerId as BvTimerId};
use baseview::{Window, WindowSettings, WindowSize};
use clack_extensions::gui::{
AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, HostGui,
PluginGuiImpl, Window as ClapWindow,
};
use clack_extensions::timer::{HostTimer, PluginTimerImpl, TimerId};
use clack_plugin::plugin::PluginError;
use clack_plugin::prelude::{HostMainThreadHandle, HostSharedHandle};
use clack_plugin::prelude::HostMainThreadHandle;
use std::error::Error;
use std::time::Duration;

pub struct ExamplePluginGui {
pub handle: Window,
Expand All @@ -35,14 +37,23 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
.with_min_size(LogicalSize::new(200.0, 100.0))
.with_max_size(LogicalSize::new(600.0, 400.0));

let mut host = Host::new().with_main_thread(unsafe {
MainThreadHandler { host: self.host.shared().with_arbitrary_lifetime() }
});
// SAFETY: this handle is valid for the lifetime of the plugin, and baseview guarantees
// it releases all references to host callbacks on `drop` (which we do unconditionally)
let host_handle = unsafe { self.host.with_arbitrary_lifetime() };

let mut host = Host::new();

if let Some(gui) = self.host_gui {
host = host.with_callbacks(unsafe {
HostGuiCallbacks { ext: gui, host: self.host.with_arbitrary_lifetime() }
});
host = host.with_callbacks(HostGuiCallbacks { ext: gui, host: host_handle });
}

#[cfg(unix)]
if let Some(fd) = self.host_posix_fd {
host = host.with_fd(HostFdCallbacks { ext: fd, host: host_handle });
}

if let Some(timer) = self.host_timer {
host = host.with_timer(HostTimerCallbacks { ext: timer, host: host_handle })
}

let window = Window::create_with_host(options, OpenWindowExample::new, host)?;
Expand Down Expand Up @@ -153,29 +164,78 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
}
}

struct MainThreadHandler {
host: HostSharedHandle<'static>,
}

impl HostMainThreadCaller for MainThreadHandler {
fn call_main_thread(&mut self) {
self.host.request_callback();
}
}

struct HostGuiCallbacks {
ext: HostGui,
host: HostMainThreadHandle<'static>,
}

impl HostCallbacks for HostGuiCallbacks {
fn request_resize(&mut self, new_size: WindowSize) -> Result<(), Box<dyn Error>> {
fn request_resize(&self, new_size: WindowSize) -> Result<(), Box<dyn Error>> {
let new_size = new_size.to_native_size();
self.ext.request_resize(&self.host, new_size.width, new_size.height)?;
Ok(())
}

fn destroyed(&mut self) {
fn destroyed(&self) {
self.ext.closed(&self.host, true);
}
}

struct HostTimerCallbacks {
ext: HostTimer,
host: HostMainThreadHandle<'static>,
}

impl HostTimerSupport for HostTimerCallbacks {
fn register_timer(&self, period: Duration) -> Result<BvTimerId, Box<dyn Error>> {
let period_ms = period.as_millis().try_into().unwrap_or(u32::MAX);
let timer = self.ext.register_timer(&self.host, period_ms)?;
Ok(BvTimerId(timer.0))
}

fn unregister_timer(&self, timer: BvTimerId) -> Result<(), Box<dyn Error>> {
self.ext.unregister_timer(&self.host, TimerId(timer.0))?;
Ok(())
}
}

#[cfg(unix)]
struct HostFdCallbacks {
ext: clack_extensions::posix_fd::HostPosixFd,
host: HostMainThreadHandle<'static>,
}

#[cfg(unix)]
impl baseview::host::HostFdSupport for HostFdCallbacks {
fn register_fd(&self, fd: std::os::fd::BorrowedFd) -> Result<(), Box<dyn Error>> {
use std::os::fd::AsRawFd;
self.ext.register_fd(
&self.host,
fd.as_raw_fd(),
clack_extensions::posix_fd::FdFlags::READ,
)?;
Ok(())
}

fn unregister_fd(&self, fd: std::os::fd::RawFd) -> Result<(), Box<dyn Error>> {
self.ext.unregister_fd(&self.host, fd)?;
Ok(())
}
}

impl PluginTimerImpl for ExamplePluginMainThread<'_> {
fn on_timer(&self, timer_id: TimerId) {
if let Some(gui) = self.gui.borrow().as_ref() {
gui.handle.host_timer_callback(BvTimerId(timer_id.0));
}
}
}

#[cfg(unix)]
impl clack_extensions::posix_fd::PluginPosixFdImpl for ExamplePluginMainThread<'_> {
fn on_fd(&self, fd: std::os::fd::RawFd, _: clack_extensions::posix_fd::FdFlags) {
if let Some(gui) = self.gui.borrow().as_ref() {
gui.handle.host_fd_callback(fd)
}
}
}
32 changes: 22 additions & 10 deletions examples/plugin_clack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::audio::ExamplePluginAudioProcessor;
use crate::gui::ExamplePluginGui;
use clack_extensions::gui::{HostGui, PluginGui};
use clack_extensions::state::{PluginState, PluginStateImpl};
use clack_extensions::timer::{HostTimer, PluginTimer};
use clack_plugin::prelude::*;
use clack_plugin::stream::{InputStream, OutputStream};
use std::cell::{Ref, RefCell};
Expand All @@ -21,7 +22,12 @@ impl Plugin for ExamplePlugin {
type MainThread<'a> = ExamplePluginMainThread<'a>;

fn declare_extensions(builder: &mut PluginExtensions<Self>, _shared: Option<&()>) {
builder.register::<PluginGui>().register::<PluginState>();
builder.register::<PluginGui>().register::<PluginState>().register::<PluginTimer>();

#[cfg(unix)]
{
builder.register::<clack_extensions::posix_fd::PluginPosixFd>();
}
}
}

Expand All @@ -40,16 +46,28 @@ impl DefaultPluginFactory for ExamplePlugin {
fn new_main_thread<'a>(
host: HostMainThreadHandle<'a>, _shared: &'a Self::Shared<'a>,
) -> Result<Self::MainThread<'a>, PluginError> {
Ok(Self::MainThread { gui: None.into(), host_gui: host.get_extension(), host })
Ok(Self::MainThread {
gui: None.into(),
host_gui: host.get_extension(),
#[cfg(unix)]
host_posix_fd: host.get_extension(),
host_timer: host.get_extension(),
host,
})
}
}

/// The data that belongs to the main thread of our plugin.
pub struct ExamplePluginMainThread<'a> {
/// The host handle
host: HostMainThreadHandle<'a>,
// The host GUI extension handle
/// The host GUI extension handle
host_gui: Option<HostGui>,
/// The host FD extension handle
#[cfg(unix)]
host_posix_fd: Option<clack_extensions::posix_fd::HostPosixFd>,
/// The host Timer extension handle
host_timer: Option<HostTimer>,
/// The plugin's GUI state and context
gui: RefCell<Option<ExamplePluginGui>>,
}
Expand All @@ -61,13 +79,7 @@ impl<'a> ExamplePluginMainThread<'a> {
}
}

impl<'a> PluginMainThread<'a, ()> for ExamplePluginMainThread<'a> {
fn on_main_thread(&self) {
if let Some(gui) = self.gui.borrow().as_ref() {
gui.handle.host_main_thread_callback();
}
}
}
impl<'a> PluginMainThread<'a, ()> for ExamplePluginMainThread<'a> {}

impl PluginStateImpl for ExamplePluginMainThread<'_> {
fn save(&self, _output: &mut OutputStream) -> Result<(), PluginError> {
Expand Down
22 changes: 5 additions & 17 deletions examples/plugin_clack_femtovg/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::window_handler::FemtovgExample;
use crate::ExamplePluginMainThread;
use baseview::dpi::*;
use baseview::gl::GlConfig;
use baseview::host::{Host, HostCallbacks, HostMainThreadCaller};
use baseview::host::{Host, HostCallbacks};
use baseview::{Window, WindowSettings, WindowSize};
use clack_extensions::gui::{
AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, HostGui,
PluginGuiImpl, Window as ClapWindow,
};
use clack_plugin::plugin::PluginError;
use clack_plugin::prelude::{HostMainThreadHandle, HostSharedHandle};
use clack_plugin::prelude::HostMainThreadHandle;
use std::error::Error;

pub struct ExamplePluginGui {
Expand All @@ -35,9 +35,7 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
.with_size(PhysicalSize::new(400, 200))
.with_gl_config(GlConfig::default());

let mut host = Host::new().with_main_thread(unsafe {
MainThreadHandler { host: self.host.shared().with_arbitrary_lifetime() }
});
let mut host = Host::new();

if let Some(gui) = self.host_gui {
host = host.with_callbacks(unsafe {
Expand Down Expand Up @@ -153,29 +151,19 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
}
}

struct MainThreadHandler {
host: HostSharedHandle<'static>,
}

impl HostMainThreadCaller for MainThreadHandler {
fn call_main_thread(&mut self) {
self.host.request_callback();
}
}

struct HostGuiCallbacks {
ext: HostGui,
host: HostMainThreadHandle<'static>,
}

impl HostCallbacks for HostGuiCallbacks {
fn request_resize(&mut self, new_size: WindowSize) -> Result<(), Box<dyn Error>> {
fn request_resize(&self, new_size: WindowSize) -> Result<(), Box<dyn Error>> {
let new_size = new_size.to_native_size();
self.ext.request_resize(&self.host, new_size.width, new_size.height)?;
Ok(())
}

fn destroyed(&mut self) {
fn destroyed(&self) {
self.ext.closed(&self.host, true);
}
}
6 changes: 1 addition & 5 deletions examples/plugin_clack_femtovg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ impl<'a> ExamplePluginMainThread<'a> {
}

impl<'a> PluginMainThread<'a, ()> for ExamplePluginMainThread<'a> {
fn on_main_thread(&self) {
if let Some(gui) = self.gui.borrow().as_ref() {
gui.handle.host_main_thread_callback();
}
}
fn on_main_thread(&self) {}
}

impl PluginStateImpl for ExamplePluginMainThread<'_> {
Expand Down
Loading
Loading