2017-10-20 18:46:42 +11:00
|
|
|
use std::sync::{Arc, Mutex, Weak};
|
2015-12-13 21:43:39 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
use {CreationError, CursorState, MouseCursor, WindowAttributes};
|
2016-10-08 23:51:29 +11:00
|
|
|
use platform::MonitorId as PlatformMonitorId;
|
2017-08-29 11:08:12 +10:00
|
|
|
use window::MonitorId as RootMonitorId;
|
2015-12-09 08:54:06 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
use sctk::window::{BasicFrame, Event as WEvent, Window as SWindow};
|
|
|
|
use sctk::reexports::client::{Display, Proxy};
|
|
|
|
use sctk::reexports::client::protocol::{wl_seat, wl_surface};
|
|
|
|
use sctk::reexports::client::protocol::wl_compositor::RequestsTrait as CompositorRequests;
|
|
|
|
use sctk::reexports::client::protocol::wl_surface::RequestsTrait as SurfaceRequests;
|
|
|
|
|
|
|
|
use super::{make_wid, EventsLoop, MonitorId, WindowId};
|
2015-12-09 08:54:06 +11:00
|
|
|
|
|
|
|
pub struct Window {
|
2018-05-06 03:36:34 +10:00
|
|
|
surface: Proxy<wl_surface::WlSurface>,
|
|
|
|
frame: Arc<Mutex<SWindow<BasicFrame>>>,
|
|
|
|
monitors: Arc<Mutex<Vec<MonitorId>>>,
|
2017-10-20 18:46:42 +11:00
|
|
|
size: Arc<Mutex<(u32, u32)>>,
|
|
|
|
kill_switch: (Arc<Mutex<bool>>, Arc<Mutex<bool>>),
|
2018-05-06 03:36:34 +10:00
|
|
|
display: Arc<Display>,
|
|
|
|
need_frame_refresh: Arc<Mutex<bool>>,
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2018-05-08 07:36:21 +10:00
|
|
|
pub fn new(evlp: &EventsLoop, attributes: WindowAttributes) -> Result<Window, CreationError> {
|
2018-05-06 03:36:34 +10:00
|
|
|
let (width, height) = attributes.dimensions.unwrap_or((800, 600));
|
|
|
|
// Create the window
|
2017-10-20 18:46:42 +11:00
|
|
|
let size = Arc::new(Mutex::new((width, height)));
|
2018-05-06 03:36:34 +10:00
|
|
|
|
|
|
|
// monitor tracking
|
|
|
|
let monitor_list = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
|
|
|
|
let surface = evlp.env.compositor.create_surface().unwrap().implement({
|
|
|
|
let list = monitor_list.clone();
|
|
|
|
let omgr = evlp.env.outputs.clone();
|
|
|
|
move |event, _| match event {
|
|
|
|
wl_surface::Event::Enter { output } => list.lock().unwrap().push(MonitorId {
|
|
|
|
proxy: output,
|
|
|
|
mgr: omgr.clone(),
|
|
|
|
}),
|
|
|
|
wl_surface::Event::Leave { output } => {
|
|
|
|
list.lock().unwrap().retain(|m| !m.proxy.equals(&output));
|
|
|
|
}
|
2017-10-20 18:46:42 +11:00
|
|
|
}
|
2018-05-06 03:36:34 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
let window_store = evlp.store.clone();
|
|
|
|
let my_surface = surface.clone();
|
|
|
|
let mut frame = SWindow::<BasicFrame>::init(
|
|
|
|
surface.clone(),
|
|
|
|
(width, height),
|
|
|
|
&evlp.env.compositor,
|
|
|
|
&evlp.env.subcompositor,
|
|
|
|
&evlp.env.shm,
|
|
|
|
&evlp.env.shell,
|
|
|
|
move |event, ()| match event {
|
|
|
|
WEvent::Configure { new_size, .. } => {
|
|
|
|
let mut store = window_store.lock().unwrap();
|
|
|
|
for window in &mut store.windows {
|
|
|
|
if window.surface.equals(&my_surface) {
|
|
|
|
window.newsize = new_size.map(|(w, h)| (w as i32, h as i32));
|
|
|
|
window.need_refresh = true;
|
|
|
|
*(window.need_frame_refresh.lock().unwrap()) = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WEvent::Refresh => {
|
|
|
|
let store = window_store.lock().unwrap();
|
|
|
|
for window in &store.windows {
|
|
|
|
if window.surface.equals(&my_surface) {
|
|
|
|
*(window.need_frame_refresh.lock().unwrap()) = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WEvent::Close => {
|
|
|
|
let mut store = window_store.lock().unwrap();
|
|
|
|
for window in &mut store.windows {
|
|
|
|
if window.surface.equals(&my_surface) {
|
|
|
|
window.closed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
for &(_, ref seat) in evlp.seats.lock().unwrap().iter() {
|
|
|
|
frame.new_seat(seat);
|
|
|
|
}
|
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
// Check for fullscreen requirements
|
2018-05-06 03:36:34 +10:00
|
|
|
if let Some(RootMonitorId {
|
|
|
|
inner: PlatformMonitorId::Wayland(ref monitor_id),
|
|
|
|
}) = attributes.fullscreen
|
|
|
|
{
|
|
|
|
frame.set_fullscreen(Some(&monitor_id.proxy));
|
2017-11-04 03:35:29 +11:00
|
|
|
} else if attributes.maximized {
|
2018-05-06 03:36:34 +10:00
|
|
|
frame.set_maximized();
|
2017-09-28 00:31:46 +10:00
|
|
|
}
|
2017-11-04 03:35:29 +11:00
|
|
|
|
|
|
|
// set decorations
|
|
|
|
frame.set_decorate(attributes.decorations);
|
|
|
|
|
|
|
|
// min-max dimensions
|
2018-05-06 03:36:34 +10:00
|
|
|
frame.set_min_size(attributes.min_dimensions);
|
|
|
|
frame.set_max_size(attributes.max_dimensions);
|
2016-10-08 23:51:29 +11:00
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
let kill_switch = Arc::new(Mutex::new(false));
|
2018-01-13 16:38:12 +11:00
|
|
|
let need_frame_refresh = Arc::new(Mutex::new(true));
|
2017-11-04 03:35:29 +11:00
|
|
|
let frame = Arc::new(Mutex::new(frame));
|
2017-10-20 18:46:42 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
evlp.store.lock().unwrap().windows.push(InternalWindow {
|
|
|
|
closed: false,
|
|
|
|
newsize: None,
|
|
|
|
need_refresh: false,
|
|
|
|
need_frame_refresh: need_frame_refresh.clone(),
|
|
|
|
surface: surface.clone(),
|
|
|
|
kill_switch: kill_switch.clone(),
|
|
|
|
frame: Arc::downgrade(&frame),
|
|
|
|
});
|
|
|
|
evlp.evq.borrow_mut().sync_roundtrip().unwrap();
|
2016-10-08 23:51:29 +11:00
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
Ok(Window {
|
|
|
|
display: evlp.display.clone(),
|
|
|
|
surface: surface,
|
2017-11-04 03:35:29 +11:00
|
|
|
frame: frame,
|
2017-10-20 18:46:42 +11:00
|
|
|
monitors: monitor_list,
|
|
|
|
size: size,
|
2018-01-13 16:38:12 +11:00
|
|
|
kill_switch: (kill_switch, evlp.cleanup_needed.clone()),
|
2018-05-06 03:36:34 +10:00
|
|
|
need_frame_refresh: need_frame_refresh,
|
2017-10-20 18:46:42 +11:00
|
|
|
})
|
2017-03-05 00:04:01 +11:00
|
|
|
}
|
2016-10-08 23:51:29 +11:00
|
|
|
|
2017-03-05 00:04:01 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> WindowId {
|
|
|
|
make_wid(&self.surface)
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_title(&self, title: &str) {
|
2017-11-04 03:35:29 +11:00
|
|
|
self.frame.lock().unwrap().set_title(title.into());
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn show(&self) {
|
2015-12-13 21:43:39 +11:00
|
|
|
// TODO
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn hide(&self) {
|
2015-12-13 21:43:39 +11:00
|
|
|
// TODO
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_position(&self) -> Option<(i32, i32)> {
|
2015-12-13 21:43:39 +11:00
|
|
|
// Not possible with wayland
|
|
|
|
None
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
2018-04-17 11:40:30 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn get_inner_position(&self) -> Option<(i32, i32)> {
|
|
|
|
// Not possible with wayland
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-12-09 08:54:06 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn set_position(&self, _x: i32, _y: i32) {
|
2015-12-13 21:43:39 +11:00
|
|
|
// Not possible with wayland
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
|
2016-10-10 01:19:06 +11:00
|
|
|
Some(self.size.lock().unwrap().clone())
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
|
2016-10-10 01:19:06 +11:00
|
|
|
let (w, h) = self.size.lock().unwrap().clone();
|
2018-05-06 03:36:34 +10:00
|
|
|
// let (w, h) = super::wayland_window::add_borders(w as i32, h as i32);
|
2016-10-10 01:19:06 +11:00
|
|
|
Some((w as u32, h as u32))
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-10-10 01:19:06 +11:00
|
|
|
// NOTE: This will only resize the borders, the contents must be updated by the user
|
2015-12-09 08:54:06 +11:00
|
|
|
pub fn set_inner_size(&self, x: u32, y: u32) {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().resize(x, y);
|
2017-07-06 00:23:10 +10:00
|
|
|
*(self.size.lock().unwrap()) = (x, y);
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:35:35 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn set_min_dimensions(&self, dimensions: Option<(u32, u32)>) {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().set_min_size(dimensions);
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_max_dimensions(&self, dimensions: Option<(u32, u32)>) {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().set_max_size(dimensions);
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
|
|
|
|
2015-12-09 08:54:06 +11:00
|
|
|
#[inline]
|
2015-12-13 23:13:20 +11:00
|
|
|
pub fn set_cursor(&self, _cursor: MouseCursor) {
|
2015-12-13 21:43:39 +11:00
|
|
|
// TODO
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-12-23 00:35:55 +11:00
|
|
|
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
|
2018-05-06 03:36:34 +10:00
|
|
|
use CursorState::{Grab, Hide, Normal};
|
2015-12-23 00:35:55 +11:00
|
|
|
// TODO : not yet possible on wayland to grab cursor
|
|
|
|
match state {
|
|
|
|
Grab => Err("Cursor cannot be grabbed on wayland yet.".to_string()),
|
|
|
|
Hide => Err("Cursor cannot be hidden on wayland yet.".to_string()),
|
2018-05-06 03:36:34 +10:00
|
|
|
Normal => Ok(()),
|
2015-12-23 00:35:55 +11:00
|
|
|
}
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn hidpi_factor(&self) -> f32 {
|
2018-05-06 03:36:34 +10:00
|
|
|
let mut factor: f32 = 1.0;
|
2017-10-20 18:46:42 +11:00
|
|
|
let guard = self.monitors.lock().unwrap();
|
2018-05-06 03:36:34 +10:00
|
|
|
for monitor_id in guard.iter() {
|
|
|
|
let hidpif = monitor_id.get_hidpi_factor();
|
|
|
|
factor = factor.max(hidpif);
|
2017-10-20 18:46:42 +11:00
|
|
|
}
|
|
|
|
factor
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
2018-01-13 16:38:12 +11:00
|
|
|
pub fn set_decorations(&self, decorate: bool) {
|
|
|
|
self.frame.lock().unwrap().set_decorate(decorate);
|
|
|
|
*(self.need_frame_refresh.lock().unwrap()) = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
|
|
|
if maximized {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().set_maximized();
|
2018-01-13 16:38:12 +11:00
|
|
|
} else {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().unset_maximized();
|
2018-01-13 16:38:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_fullscreen(&self, monitor: Option<RootMonitorId>) {
|
2018-05-06 03:36:34 +10:00
|
|
|
if let Some(RootMonitorId {
|
|
|
|
inner: PlatformMonitorId::Wayland(ref monitor_id),
|
|
|
|
}) = monitor
|
|
|
|
{
|
|
|
|
self.frame
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.set_fullscreen(Some(&monitor_id.proxy));
|
2018-01-13 16:38:12 +11:00
|
|
|
} else {
|
2018-05-06 03:36:34 +10:00
|
|
|
self.frame.lock().unwrap().unset_fullscreen();
|
2018-01-13 16:38:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 08:54:06 +11:00
|
|
|
#[inline]
|
2015-12-13 21:43:39 +11:00
|
|
|
pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> {
|
|
|
|
// TODO: not yet possible on wayland
|
2015-12-23 00:35:55 +11:00
|
|
|
Err(())
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
2018-03-23 20:35:35 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
pub fn get_display(&self) -> &Display {
|
2017-10-20 18:46:42 +11:00
|
|
|
&*self.display
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
2018-03-23 20:35:35 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
pub fn get_surface(&self) -> &Proxy<wl_surface::WlSurface> {
|
2016-10-08 23:51:29 +11:00
|
|
|
&self.surface
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
2017-09-07 18:33:46 +10:00
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
pub fn get_current_monitor(&self) -> MonitorId {
|
|
|
|
// we don't know how much each monitor sees us so...
|
|
|
|
// just return the most recent one ?
|
|
|
|
let guard = self.monitors.lock().unwrap();
|
2018-05-06 03:36:34 +10:00
|
|
|
guard.last().unwrap().clone()
|
2017-09-07 18:33:46 +10:00
|
|
|
}
|
2015-12-09 08:54:06 +11:00
|
|
|
}
|
|
|
|
|
2015-12-13 21:43:39 +11:00
|
|
|
impl Drop for Window {
|
|
|
|
fn drop(&mut self) {
|
2017-10-20 18:46:42 +11:00
|
|
|
*(self.kill_switch.0.lock().unwrap()) = true;
|
|
|
|
*(self.kill_switch.1.lock().unwrap()) = true;
|
2016-10-08 23:51:29 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
/*
|
|
|
|
* Internal store for windows
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct InternalWindow {
|
2018-05-06 03:36:34 +10:00
|
|
|
surface: Proxy<wl_surface::WlSurface>,
|
2017-10-20 18:46:42 +11:00
|
|
|
newsize: Option<(i32, i32)>,
|
|
|
|
need_refresh: bool,
|
2018-01-13 16:38:12 +11:00
|
|
|
need_frame_refresh: Arc<Mutex<bool>>,
|
2017-07-06 00:23:10 +10:00
|
|
|
closed: bool,
|
2017-10-20 18:46:42 +11:00
|
|
|
kill_switch: Arc<Mutex<bool>>,
|
2018-05-06 03:36:34 +10:00
|
|
|
frame: Weak<Mutex<SWindow<BasicFrame>>>,
|
2016-10-08 23:51:29 +11:00
|
|
|
}
|
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
pub struct WindowStore {
|
2018-05-06 03:36:34 +10:00
|
|
|
windows: Vec<InternalWindow>,
|
2017-10-20 18:46:42 +11:00
|
|
|
}
|
2017-03-05 00:04:01 +11:00
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
impl WindowStore {
|
|
|
|
pub fn new() -> WindowStore {
|
2018-05-06 03:36:34 +10:00
|
|
|
WindowStore {
|
|
|
|
windows: Vec::new(),
|
|
|
|
}
|
2016-10-08 23:51:29 +11:00
|
|
|
}
|
2017-07-06 00:23:10 +10:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
pub fn find_wid(&self, surface: &Proxy<wl_surface::WlSurface>) -> Option<WindowId> {
|
2017-10-20 18:46:42 +11:00
|
|
|
for window in &self.windows {
|
|
|
|
if surface.equals(&window.surface) {
|
|
|
|
return Some(make_wid(surface));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2017-09-28 00:31:46 +10:00
|
|
|
}
|
|
|
|
|
2018-04-25 06:20:40 +10:00
|
|
|
pub fn cleanup(&mut self) -> Vec<WindowId> {
|
|
|
|
let mut pruned = Vec::new();
|
2017-10-20 18:46:42 +11:00
|
|
|
self.windows.retain(|w| {
|
|
|
|
if *w.kill_switch.lock().unwrap() {
|
|
|
|
// window is dead, cleanup
|
2018-04-25 06:20:40 +10:00
|
|
|
pruned.push(make_wid(&w.surface));
|
2017-10-20 18:46:42 +11:00
|
|
|
w.surface.destroy();
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
});
|
2018-04-25 06:20:40 +10:00
|
|
|
pruned
|
2017-10-20 18:46:42 +11:00
|
|
|
}
|
2016-10-08 23:51:29 +11:00
|
|
|
|
2018-05-06 03:36:34 +10:00
|
|
|
pub fn new_seat(&self, seat: &Proxy<wl_seat::WlSeat>) {
|
|
|
|
for window in &self.windows {
|
|
|
|
if let Some(w) = window.frame.upgrade() {
|
|
|
|
w.lock().unwrap().new_seat(seat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 18:46:42 +11:00
|
|
|
pub fn for_each<F>(&mut self, mut f: F)
|
2018-05-06 03:36:34 +10:00
|
|
|
where
|
|
|
|
F: FnMut(Option<(i32, i32)>, bool, bool, bool, WindowId, Option<&mut SWindow<BasicFrame>>),
|
2016-10-08 23:51:29 +11:00
|
|
|
{
|
2017-10-20 18:46:42 +11:00
|
|
|
for window in &mut self.windows {
|
2017-11-04 03:35:29 +11:00
|
|
|
let opt_arc = window.frame.upgrade();
|
2017-10-20 18:46:42 +11:00
|
|
|
let mut opt_mutex_lock = opt_arc.as_ref().map(|m| m.lock().unwrap());
|
|
|
|
f(
|
|
|
|
window.newsize.take(),
|
|
|
|
window.need_refresh,
|
2018-01-13 16:38:12 +11:00
|
|
|
::std::mem::replace(&mut *window.need_frame_refresh.lock().unwrap(), false),
|
2017-10-20 18:46:42 +11:00
|
|
|
window.closed,
|
|
|
|
make_wid(&window.surface),
|
2018-05-06 03:36:34 +10:00
|
|
|
opt_mutex_lock.as_mut().map(|m| &mut **m),
|
2017-10-20 18:46:42 +11:00
|
|
|
);
|
|
|
|
window.need_refresh = false;
|
|
|
|
// avoid re-spamming the event
|
|
|
|
window.closed = false;
|
|
|
|
}
|
2016-10-08 23:51:29 +11:00
|
|
|
}
|
2017-10-20 18:46:42 +11:00
|
|
|
}
|