92 lines
3.8 KiB
Rust
92 lines
3.8 KiB
Rust
pub const OUTPUT_NAME: &str = "winit";
|
|
use std::{cell::RefCell, rc::Rc, sync::{Arc, Mutex}, sync::atomic::{AtomicBool, Ordering}, time::Duration};
|
|
|
|
use smithay::{reexports::{calloop::{Mode, Interest, LoopHandle, PostAction, generic::Generic}, wayland_server::{Display, protocol::wl_surface::WlSurface}}, utils::{Logical, Point}, wayland::{data_device::{DataDeviceEvent, default_action_chooser, init_data_device, set_data_device_focus}, output::xdg::init_xdg_output_manager, seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig}, shm::init_shm_global}};
|
|
|
|
pub struct State<BackendData> {
|
|
pub backend_data: BackendData,
|
|
pub cursor_status: Arc<Mutex<CursorImageStatus>>,
|
|
pub display: Rc<RefCell<Display>>,
|
|
pub dnd_icon: Arc<Mutex<Option<WlSurface>>>,
|
|
pub keyboard: KeyboardHandle,
|
|
pub loop_handle: LoopHandle<'static, State<BackendData>>,
|
|
pub pointer_location: Point<f64, Logical>,
|
|
pub pointer: PointerHandle,
|
|
pub running: Arc<AtomicBool>,
|
|
pub seat_name: String,
|
|
pub seat: Seat,
|
|
}
|
|
|
|
impl<BackendData: Backend + 'static> State<BackendData> {
|
|
pub fn init(display: Rc<RefCell<Display>>, loop_handle: LoopHandle<'static, State<BackendData>>, backend_data: BackendData) -> Self {
|
|
loop_handle.insert_source(
|
|
Generic::from_fd(display.borrow().get_poll_fd(), Interest::READ, Mode::Level),
|
|
move |_,_, state: &mut State<BackendData>| {
|
|
let display = state.display.clone();
|
|
let mut display = display.borrow_mut();
|
|
match display.dispatch(Duration::from_millis(0), state) {
|
|
Ok(_) => Ok(PostAction::Continue),
|
|
Err(e) => {
|
|
dbg!("I/O Error on Display {}", &e);
|
|
state.running.store(false, Ordering::SeqCst);
|
|
Err(e)
|
|
}
|
|
}
|
|
}).expect("Wayland Eventsource init failed");
|
|
|
|
init_shm_global(&mut display.borrow_mut(), Vec::new(), None);
|
|
|
|
//let shell_handles = init_shell()
|
|
init_xdg_output_manager(&mut display.borrow_mut(), None);
|
|
|
|
let socket_name = display.borrow_mut().add_socket_auto().unwrap().into_string().unwrap();
|
|
std::env::set_var("WAYLAND_DISPLAY", &socket_name);
|
|
|
|
let dnd_icon = Arc::new(Mutex::new(None));
|
|
let dnd_icon2 = dnd_icon.clone();
|
|
init_data_device(
|
|
&mut display.borrow_mut(),
|
|
move |event| match event {
|
|
DataDeviceEvent::DnDStarted { icon, .. } => {
|
|
*dnd_icon2.lock().unwrap() = icon;
|
|
}
|
|
DataDeviceEvent::DnDDropped => {
|
|
*dnd_icon2.lock().unwrap() = None;
|
|
}
|
|
_ => {}
|
|
},
|
|
default_action_chooser,
|
|
None,
|
|
);
|
|
|
|
let seat_name = backend_data.seat_name();
|
|
|
|
let (mut seat, _) = Seat::new(&mut display.borrow_mut(), seat_name.clone(), None);
|
|
|
|
let cursor_status = Arc::new(Mutex::new(CursorImageStatus::Default));
|
|
let cursor_status2 = cursor_status.clone();
|
|
let pointer = seat.add_pointer(move |new_status| { *cursor_status2.lock().unwrap() = new_status});
|
|
let keyboard = seat.add_keyboard(XkbConfig::default(), 200, 25, |seat, focus| {
|
|
set_data_device_focus(seat, focus.and_then(|s| s.as_ref().client()))
|
|
}).expect("Could not initialize keyboard");
|
|
|
|
Self {
|
|
backend_data,
|
|
running: Arc::new(AtomicBool::new(true)),
|
|
display,
|
|
loop_handle,
|
|
cursor_status,
|
|
dnd_icon,
|
|
keyboard,
|
|
pointer_location: (0.0,0.0).into(),
|
|
pointer,
|
|
seat_name,
|
|
seat,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait Backend {
|
|
fn seat_name(&self) -> String;
|
|
}
|