2020-04-21 16:56:59 +00:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
rc::Rc,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc, Mutex,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use smithay::{
|
|
|
|
reexports::{
|
|
|
|
calloop::{
|
2020-04-25 17:10:02 +00:00
|
|
|
generic::{Fd, Generic},
|
|
|
|
Interest, LoopHandle, Mode, Source,
|
2020-04-21 16:56:59 +00:00
|
|
|
},
|
|
|
|
wayland_server::{protocol::wl_surface::WlSurface, Display},
|
|
|
|
},
|
|
|
|
wayland::{
|
|
|
|
compositor::CompositorToken,
|
2020-05-04 13:04:30 +00:00
|
|
|
data_device::{default_action_chooser, init_data_device, set_data_device_focus, DataDeviceEvent},
|
|
|
|
seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig},
|
2020-04-21 16:56:59 +00:00
|
|
|
shm::init_shm_global,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-04 13:04:30 +00:00
|
|
|
#[cfg(feature = "udev")]
|
2020-06-27 22:07:48 +00:00
|
|
|
use smithay::backend::session::{auto::AutoSession, Session};
|
2021-01-03 15:21:22 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
use smithay::xwayland::XWayland;
|
2021-05-13 17:59:47 +00:00
|
|
|
#[cfg(feature = "egl")]
|
|
|
|
use smithay::backend::egl::display::EGLBufferReader;
|
2020-05-04 13:04:30 +00:00
|
|
|
|
2020-06-27 16:27:47 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
use crate::udev::MyOutput;
|
2021-01-03 15:21:22 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
use crate::xwayland::XWm;
|
2021-05-13 17:59:47 +00:00
|
|
|
use crate::shell::init_shell;
|
2020-04-21 16:56:59 +00:00
|
|
|
|
|
|
|
pub struct AnvilState {
|
|
|
|
pub socket_name: String,
|
|
|
|
pub running: Arc<AtomicBool>,
|
|
|
|
pub display: Rc<RefCell<Display>>,
|
|
|
|
pub handle: LoopHandle<AnvilState>,
|
|
|
|
pub ctoken: CompositorToken<crate::shell::Roles>,
|
|
|
|
pub window_map: Rc<RefCell<crate::window_map::WindowMap<crate::shell::Roles>>>,
|
|
|
|
pub dnd_icon: Arc<Mutex<Option<WlSurface>>>,
|
|
|
|
pub log: slog::Logger,
|
2020-05-04 13:04:30 +00:00
|
|
|
// input-related fields
|
|
|
|
pub pointer: PointerHandle,
|
|
|
|
pub keyboard: KeyboardHandle,
|
|
|
|
pub pointer_location: Rc<RefCell<(f64, f64)>>,
|
|
|
|
pub cursor_status: Arc<Mutex<CursorImageStatus>>,
|
2020-06-11 17:36:57 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
pub output_map: Option<Rc<RefCell<Vec<MyOutput>>>>,
|
2020-05-04 13:04:30 +00:00
|
|
|
pub seat_name: String,
|
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
pub session: Option<AutoSession>,
|
2020-04-21 16:56:59 +00:00
|
|
|
// things we must keep alive
|
2020-04-25 17:10:02 +00:00
|
|
|
_wayland_event_source: Source<Generic<Fd>>,
|
2021-01-03 15:21:22 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
_xwayland: XWayland<XWm>,
|
2020-04-21 16:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnvilState {
|
|
|
|
pub fn init(
|
|
|
|
display: Rc<RefCell<Display>>,
|
|
|
|
handle: LoopHandle<AnvilState>,
|
2021-05-13 17:59:47 +00:00
|
|
|
#[cfg(feature = "egl")] egl_reader: Rc<RefCell<Option<EGLBufferReader>>>,
|
2020-05-04 13:04:30 +00:00
|
|
|
#[cfg(feature = "udev")] session: Option<AutoSession>,
|
|
|
|
#[cfg(not(feature = "udev"))] _session: Option<()>,
|
2020-06-11 17:36:57 +00:00
|
|
|
#[cfg(feature = "udev")] output_map: Option<Rc<RefCell<Vec<MyOutput>>>>,
|
|
|
|
#[cfg(not(feature = "udev"))] _output_map: Option<()>,
|
2020-04-21 16:56:59 +00:00
|
|
|
log: slog::Logger,
|
|
|
|
) -> AnvilState {
|
|
|
|
// init the wayland connection
|
|
|
|
let _wayland_event_source = handle
|
|
|
|
.insert_source(
|
2020-04-25 17:10:02 +00:00
|
|
|
Generic::from_fd(display.borrow().get_poll_fd(), Interest::Readable, Mode::Level),
|
2020-04-21 16:56:59 +00:00
|
|
|
{
|
|
|
|
let display = display.clone();
|
|
|
|
let log = log.clone();
|
2020-04-25 17:10:02 +00:00
|
|
|
move |_, _, state: &mut AnvilState| {
|
2020-04-21 16:56:59 +00:00
|
|
|
let mut display = display.borrow_mut();
|
|
|
|
match display.dispatch(std::time::Duration::from_millis(0), state) {
|
2020-04-25 17:10:02 +00:00
|
|
|
Ok(_) => Ok(()),
|
2020-04-21 16:56:59 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!(log, "I/O error on the Wayland display: {}", e);
|
|
|
|
state.running.store(false, Ordering::SeqCst);
|
2020-04-25 17:10:02 +00:00
|
|
|
Err(e)
|
2020-04-21 16:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Failed to init the wayland event source.");
|
|
|
|
|
2020-04-21 17:42:03 +00:00
|
|
|
// Init the basic compositor globals
|
|
|
|
|
2020-04-21 16:56:59 +00:00
|
|
|
init_shm_global(&mut display.borrow_mut(), vec![], log.clone());
|
|
|
|
|
2021-05-13 17:59:47 +00:00
|
|
|
#[cfg(feature = "egl")]
|
|
|
|
let shell_handles = init_shell(&mut display.borrow_mut(), egl_reader, log.clone());
|
|
|
|
#[cfg(not(feature = "egl"))]
|
|
|
|
let shell_handles = init_shell(&mut display.borrow_mut(), log.clone());
|
2020-04-21 16:56:59 +00:00
|
|
|
|
|
|
|
let socket_name = display
|
|
|
|
.borrow_mut()
|
|
|
|
.add_socket_auto()
|
|
|
|
.unwrap()
|
|
|
|
.into_string()
|
|
|
|
.unwrap();
|
|
|
|
info!(log, "Listening on wayland socket"; "name" => socket_name.clone());
|
|
|
|
::std::env::set_var("WAYLAND_DISPLAY", &socket_name);
|
|
|
|
|
2020-04-21 17:42:03 +00:00
|
|
|
// init data device
|
|
|
|
|
2020-04-21 16:56:59 +00:00
|
|
|
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,
|
2020-05-02 10:42:02 +00:00
|
|
|
shell_handles.token,
|
2020-04-21 16:56:59 +00:00
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2020-05-04 13:04:30 +00:00
|
|
|
// init input
|
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
let seat_name = if let Some(ref session) = session {
|
|
|
|
session.seat()
|
|
|
|
} else {
|
|
|
|
"anvil".into()
|
|
|
|
};
|
|
|
|
#[cfg(not(feature = "udev"))]
|
2020-06-27 16:27:47 +00:00
|
|
|
let seat_name: String = "anvil".into();
|
2020-05-04 13:04:30 +00:00
|
|
|
|
|
|
|
let (mut seat, _) = Seat::new(
|
|
|
|
&mut display.borrow_mut(),
|
|
|
|
seat_name.clone(),
|
|
|
|
shell_handles.token,
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let cursor_status = Arc::new(Mutex::new(CursorImageStatus::Default));
|
|
|
|
|
|
|
|
let cursor_status2 = cursor_status.clone();
|
2020-06-07 13:29:37 +00:00
|
|
|
let pointer = seat.add_pointer(shell_handles.token, move |new_status| {
|
2020-05-04 13:04:30 +00:00
|
|
|
// TODO: hide winit system cursor when relevant
|
|
|
|
*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("Failed to initialize the keyboard");
|
|
|
|
|
2021-01-03 15:21:22 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
let _xwayland = {
|
2021-01-04 08:46:59 +00:00
|
|
|
let xwm = XWm::new(
|
|
|
|
handle.clone(),
|
|
|
|
shell_handles.token,
|
|
|
|
shell_handles.window_map.clone(),
|
|
|
|
log.clone(),
|
|
|
|
);
|
2021-01-03 15:21:22 +00:00
|
|
|
XWayland::init(xwm, handle.clone(), display.clone(), &mut (), log.clone()).unwrap()
|
|
|
|
};
|
|
|
|
|
2020-04-21 16:56:59 +00:00
|
|
|
AnvilState {
|
|
|
|
running: Arc::new(AtomicBool::new(true)),
|
|
|
|
display,
|
|
|
|
handle,
|
2020-05-02 10:42:02 +00:00
|
|
|
ctoken: shell_handles.token,
|
|
|
|
window_map: shell_handles.window_map,
|
2020-04-21 16:56:59 +00:00
|
|
|
dnd_icon,
|
|
|
|
log,
|
|
|
|
socket_name,
|
2020-05-04 13:04:30 +00:00
|
|
|
pointer,
|
|
|
|
keyboard,
|
|
|
|
cursor_status,
|
|
|
|
pointer_location: Rc::new(RefCell::new((0.0, 0.0))),
|
2020-06-11 17:36:57 +00:00
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
output_map,
|
2020-05-04 13:04:30 +00:00
|
|
|
seat_name,
|
|
|
|
#[cfg(feature = "udev")]
|
|
|
|
session,
|
2020-04-21 16:56:59 +00:00
|
|
|
_wayland_event_source,
|
2021-01-03 15:21:22 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
_xwayland,
|
2020-04-21 16:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|