forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 15
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
11 changed files
with
540 additions
and
5 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,13 @@ | ||
[package] | ||
name = "sctk_subsurface" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sctk = { package = "smithay-client-toolkit", git = "https://github.com/smithay/client-toolkit", rev = "828b1eb" } | ||
iced = { path = "../..", default-features = false, features = ["wayland", "debug", "a11y"] } | ||
iced_runtime = { path = "../../runtime" } | ||
iced_sctk = { path = "../../sctk" } | ||
env_logger = "0.10" | ||
futures-channel = "0.3.29" | ||
calloop = "0.12.3" |
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,108 @@ | ||
// Shows a subsurface with a 1x1 px red buffer, stretch to window size | ||
|
||
use iced::{ | ||
event::wayland::Event as WaylandEvent, wayland::InitialSurface, | ||
widget::text, window, Application, Command, Element, Length, Subscription, | ||
Theme, | ||
}; | ||
use sctk::reexports::client::{ | ||
protocol::wl_buffer::WlBuffer, Connection, Proxy, | ||
}; | ||
|
||
mod wayland; | ||
|
||
fn main() { | ||
let mut settings = iced::Settings::default(); | ||
settings.initial_surface = InitialSurface::XdgWindow(Default::default()); | ||
SubsurfaceApp::run(settings).unwrap(); | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
struct SubsurfaceApp { | ||
connection: Option<Connection>, | ||
red_buffer: Option<WlBuffer>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Message { | ||
WaylandEvent(WaylandEvent), | ||
Wayland(wayland::Event), | ||
Ignore, | ||
} | ||
|
||
impl Application for SubsurfaceApp { | ||
type Executor = iced::executor::Default; | ||
type Message = Message; | ||
type Flags = (); | ||
type Theme = Theme; | ||
|
||
fn new(_flags: ()) -> (SubsurfaceApp, Command<Self::Message>) { | ||
( | ||
SubsurfaceApp { | ||
..SubsurfaceApp::default() | ||
}, | ||
Command::none(), | ||
) | ||
} | ||
|
||
fn title(&self) -> String { | ||
String::from("SubsurfaceApp") | ||
} | ||
|
||
fn update(&mut self, message: Self::Message) -> Command<Self::Message> { | ||
match message { | ||
Message::WaylandEvent(evt) => match evt { | ||
WaylandEvent::Output(_evt, output) => { | ||
if self.connection.is_none() { | ||
if let Some(backend) = output.backend().upgrade() { | ||
self.connection = | ||
Some(Connection::from_backend(backend)); | ||
} | ||
} | ||
} | ||
_ => {} | ||
}, | ||
Message::Wayland(evt) => match evt { | ||
wayland::Event::RedBuffer(buffer) => { | ||
self.red_buffer = Some(buffer); | ||
} | ||
}, | ||
Message::Ignore => {} | ||
} | ||
Command::none() | ||
} | ||
|
||
fn view(&self, _id: window::Id) -> Element<Self::Message> { | ||
if let Some(buffer) = &self.red_buffer { | ||
iced_sctk::subsurface_widget::Subsurface::new(1, 1, buffer) | ||
.width(Length::Fill) | ||
.height(Length::Fill) | ||
.into() | ||
} else { | ||
text("No subsurface").into() | ||
} | ||
} | ||
|
||
fn subscription(&self) -> Subscription<Self::Message> { | ||
let mut subscriptions = | ||
vec![iced::subscription::events_with(|evt, _| { | ||
if let iced::Event::PlatformSpecific( | ||
iced::event::PlatformSpecific::Wayland(evt), | ||
) = evt | ||
{ | ||
Some(Message::WaylandEvent(evt)) | ||
} else { | ||
None | ||
} | ||
})]; | ||
if let Some(connection) = &self.connection { | ||
subscriptions | ||
.push(wayland::subscription(connection).map(Message::Wayland)); | ||
} | ||
Subscription::batch(subscriptions) | ||
} | ||
|
||
fn close_requested(&self, _id: window::Id) -> Self::Message { | ||
Message::Ignore | ||
} | ||
} |
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,86 @@ | ||
use futures_channel::mpsc; | ||
use iced::futures::{FutureExt, SinkExt}; | ||
use sctk::{ | ||
reexports::{ | ||
calloop_wayland_source::WaylandSource, | ||
client::{ | ||
delegate_noop, | ||
globals::registry_queue_init, | ||
protocol::{wl_buffer::WlBuffer, wl_shm}, | ||
Connection, | ||
}, | ||
}, | ||
registry::{ProvidesRegistryState, RegistryState}, | ||
shm::{raw::RawPool, Shm, ShmHandler}, | ||
}; | ||
use std::thread; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Event { | ||
RedBuffer(WlBuffer), | ||
} | ||
|
||
struct AppData { | ||
registry_state: RegistryState, | ||
shm_state: Shm, | ||
} | ||
|
||
impl ProvidesRegistryState for AppData { | ||
fn registry(&mut self) -> &mut RegistryState { | ||
&mut self.registry_state | ||
} | ||
|
||
sctk::registry_handlers!(); | ||
} | ||
|
||
impl ShmHandler for AppData { | ||
fn shm_state(&mut self) -> &mut Shm { | ||
&mut self.shm_state | ||
} | ||
} | ||
|
||
pub fn subscription(connection: &Connection) -> iced::Subscription<Event> { | ||
let connection = connection.clone(); | ||
iced::subscription::run_with_id( | ||
"wayland-sub", | ||
async { start(connection).await }.flatten_stream(), | ||
) | ||
} | ||
|
||
async fn start(conn: Connection) -> mpsc::Receiver<Event> { | ||
let (mut sender, receiver) = mpsc::channel(20); | ||
|
||
let (globals, event_queue) = registry_queue_init(&conn).unwrap(); | ||
let qh = event_queue.handle(); | ||
|
||
let mut app_data = AppData { | ||
registry_state: RegistryState::new(&globals), | ||
shm_state: Shm::bind(&globals, &qh).unwrap(), | ||
}; | ||
|
||
let mut pool = RawPool::new(4, &app_data.shm_state).unwrap(); | ||
let buffer = | ||
pool.create_buffer(0, 1, 1, 4, wl_shm::Format::Xrgb8888, (), &qh); | ||
let data = pool.mmap(); | ||
data[0] = 0; | ||
data[1] = 0; | ||
data[2] = 255; | ||
data[3] = 255; | ||
let _ = sender.send(Event::RedBuffer(buffer)).await; | ||
|
||
thread::spawn(move || { | ||
let mut event_loop = calloop::EventLoop::try_new().unwrap(); | ||
WaylandSource::new(conn, event_queue) | ||
.insert(event_loop.handle()) | ||
.unwrap(); | ||
loop { | ||
event_loop.dispatch(None, &mut app_data).unwrap(); | ||
} | ||
}); | ||
|
||
receiver | ||
} | ||
|
||
delegate_noop!(AppData: ignore WlBuffer); | ||
sctk::delegate_registry!(AppData); | ||
sctk::delegate_shm!(AppData); |
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
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
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,5 @@ | ||
use crate::handlers::SctkState; | ||
use sctk::delegate_subcompositor; | ||
use std::fmt::Debug; | ||
|
||
delegate_subcompositor!(@<T: 'static + Debug> SctkState<T>); |
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
Oops, something went wrong.