2018-10-02 21:37:24 +00:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
rc::Rc,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
2018-05-07 17:56:38 +00:00
|
|
|
|
2018-10-02 21:37:24 +00:00
|
|
|
use smithay::{
|
2021-05-13 17:59:47 +00:00
|
|
|
backend::renderer::buffer_dimensions,
|
2020-02-03 13:31:15 +00:00
|
|
|
reexports::{
|
|
|
|
wayland_protocols::xdg_shell::server::xdg_toplevel,
|
|
|
|
wayland_server::{
|
2021-06-21 19:29:40 +00:00
|
|
|
protocol::{wl_buffer, wl_output, wl_pointer::ButtonState, wl_shell_surface, wl_surface},
|
2020-02-03 13:31:15 +00:00
|
|
|
Display,
|
|
|
|
},
|
2018-12-15 21:01:24 +00:00
|
|
|
},
|
2021-07-05 17:21:15 +00:00
|
|
|
utils::{Logical, Physical, Point, Rectangle, Size},
|
2018-10-02 21:37:24 +00:00
|
|
|
wayland::{
|
2020-04-21 18:53:55 +00:00
|
|
|
compositor::{
|
2021-06-23 07:43:53 +00:00
|
|
|
compositor_init, is_sync_subsurface, with_states, with_surface_tree_upward, BufferAssignment,
|
|
|
|
SurfaceAttributes, TraversalAction,
|
2020-04-21 18:53:55 +00:00
|
|
|
},
|
2021-06-23 07:43:53 +00:00
|
|
|
seat::{AxisFrame, GrabStartData, PointerGrab, PointerInnerHandle, Seat},
|
2018-10-02 21:37:24 +00:00
|
|
|
shell::{
|
2021-06-23 07:43:53 +00:00
|
|
|
legacy::{wl_shell_init, ShellRequest, ShellState as WlShellState, ShellSurfaceKind},
|
2021-07-30 16:15:15 +00:00
|
|
|
wlr_layer::{LayerShellRequest, LayerSurfaceAttributes},
|
2018-10-02 21:37:24 +00:00
|
|
|
xdg::{
|
2021-06-29 15:30:48 +00:00
|
|
|
xdg_shell_init, Configure, ShellState as XdgShellState, SurfaceCachedState,
|
|
|
|
XdgPopupSurfaceRoleAttributes, XdgRequest, XdgToplevelSurfaceRoleAttributes,
|
2018-10-02 21:37:24 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-08 04:37:35 +00:00
|
|
|
Serial,
|
2018-10-02 21:37:24 +00:00
|
|
|
},
|
2018-09-24 22:32:09 +00:00
|
|
|
};
|
2017-12-10 21:09:17 +00:00
|
|
|
|
2021-05-30 21:31:14 +00:00
|
|
|
use crate::{
|
2021-06-21 19:29:40 +00:00
|
|
|
output_map::OutputMap,
|
2021-06-24 16:37:17 +00:00
|
|
|
state::AnvilState,
|
2021-06-15 21:32:02 +00:00
|
|
|
window_map::{Kind as SurfaceKind, PopupKind, WindowMap},
|
2021-05-30 21:31:14 +00:00
|
|
|
};
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2020-02-01 15:42:08 +00:00
|
|
|
struct MoveSurfaceGrab {
|
2020-02-02 15:05:49 +00:00
|
|
|
start_data: GrabStartData,
|
2021-06-23 07:43:53 +00:00
|
|
|
window_map: Rc<RefCell<WindowMap>>,
|
|
|
|
toplevel: SurfaceKind,
|
2021-07-05 17:21:15 +00:00
|
|
|
initial_window_location: Point<i32, Logical>,
|
2020-02-01 15:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PointerGrab for MoveSurfaceGrab {
|
|
|
|
fn motion(
|
|
|
|
&mut self,
|
|
|
|
_handle: &mut PointerInnerHandle<'_>,
|
2021-07-05 17:21:15 +00:00
|
|
|
location: Point<f64, Logical>,
|
|
|
|
_focus: Option<(wl_surface::WlSurface, Point<i32, Logical>)>,
|
2020-08-08 04:37:35 +00:00
|
|
|
_serial: Serial,
|
2020-02-01 15:42:08 +00:00
|
|
|
_time: u32,
|
|
|
|
) {
|
2021-07-05 17:21:15 +00:00
|
|
|
let delta = location - self.start_data.location;
|
|
|
|
let new_location = self.initial_window_location.to_f64() + delta;
|
|
|
|
|
|
|
|
self.window_map.borrow_mut().set_location(
|
|
|
|
&self.toplevel,
|
|
|
|
(new_location.x as i32, new_location.y as i32).into(),
|
|
|
|
);
|
2020-02-01 15:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn button(
|
|
|
|
&mut self,
|
|
|
|
handle: &mut PointerInnerHandle<'_>,
|
|
|
|
button: u32,
|
|
|
|
state: ButtonState,
|
2020-08-08 04:37:35 +00:00
|
|
|
serial: Serial,
|
2020-02-01 15:42:08 +00:00
|
|
|
time: u32,
|
|
|
|
) {
|
|
|
|
handle.button(button, state, serial, time);
|
|
|
|
if handle.current_pressed().is_empty() {
|
|
|
|
// No more buttons are pressed, release the grab.
|
|
|
|
handle.unset_grab(serial, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn axis(&mut self, handle: &mut PointerInnerHandle<'_>, details: AxisFrame) {
|
|
|
|
handle.axis(details)
|
|
|
|
}
|
2020-02-02 15:05:49 +00:00
|
|
|
|
|
|
|
fn start_data(&self) -> &GrabStartData {
|
|
|
|
&self.start_data
|
|
|
|
}
|
2020-02-01 15:42:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 10:37:18 +00:00
|
|
|
bitflags::bitflags! {
|
2020-02-11 08:06:36 +00:00
|
|
|
struct ResizeEdge: u32 {
|
|
|
|
const NONE = 0;
|
|
|
|
const TOP = 1;
|
|
|
|
const BOTTOM = 2;
|
|
|
|
const LEFT = 4;
|
|
|
|
const TOP_LEFT = 5;
|
|
|
|
const BOTTOM_LEFT = 6;
|
|
|
|
const RIGHT = 8;
|
|
|
|
const TOP_RIGHT = 9;
|
|
|
|
const BOTTOM_RIGHT = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<wl_shell_surface::Resize> for ResizeEdge {
|
|
|
|
#[inline]
|
|
|
|
fn from(x: wl_shell_surface::Resize) -> Self {
|
|
|
|
Self::from_bits(x.bits()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ResizeEdge> for wl_shell_surface::Resize {
|
|
|
|
#[inline]
|
|
|
|
fn from(x: ResizeEdge) -> Self {
|
|
|
|
Self::from_bits(x.bits()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<xdg_toplevel::ResizeEdge> for ResizeEdge {
|
|
|
|
#[inline]
|
|
|
|
fn from(x: xdg_toplevel::ResizeEdge) -> Self {
|
|
|
|
Self::from_bits(x.to_raw()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ResizeEdge> for xdg_toplevel::ResizeEdge {
|
|
|
|
#[inline]
|
|
|
|
fn from(x: ResizeEdge) -> Self {
|
|
|
|
Self::from_raw(x.bits()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:31:15 +00:00
|
|
|
struct ResizeSurfaceGrab {
|
|
|
|
start_data: GrabStartData,
|
2021-06-23 07:43:53 +00:00
|
|
|
toplevel: SurfaceKind,
|
2020-02-11 08:06:36 +00:00
|
|
|
edges: ResizeEdge,
|
2021-07-05 17:21:15 +00:00
|
|
|
initial_window_size: Size<i32, Logical>,
|
|
|
|
last_window_size: Size<i32, Logical>,
|
2020-02-03 13:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PointerGrab for ResizeSurfaceGrab {
|
|
|
|
fn motion(
|
|
|
|
&mut self,
|
2021-06-17 20:41:53 +00:00
|
|
|
handle: &mut PointerInnerHandle<'_>,
|
2021-07-05 17:21:15 +00:00
|
|
|
location: Point<f64, Logical>,
|
|
|
|
_focus: Option<(wl_surface::WlSurface, Point<i32, Logical>)>,
|
2021-06-17 20:41:53 +00:00
|
|
|
serial: Serial,
|
|
|
|
time: u32,
|
2020-02-03 13:31:15 +00:00
|
|
|
) {
|
2021-06-17 20:41:53 +00:00
|
|
|
// It is impossible to get `min_size` and `max_size` of dead toplevel, so we return early.
|
|
|
|
if !self.toplevel.alive() | self.toplevel.get_surface().is_none() {
|
|
|
|
handle.unset_grab(serial, time);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
let (mut dx, mut dy) = (location - self.start_data.location).into();
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
let mut new_window_width = self.initial_window_size.w;
|
|
|
|
let mut new_window_height = self.initial_window_size.h;
|
2020-02-08 06:24:50 +00:00
|
|
|
|
2020-02-11 08:06:36 +00:00
|
|
|
let left_right = ResizeEdge::LEFT | ResizeEdge::RIGHT;
|
|
|
|
let top_bottom = ResizeEdge::TOP | ResizeEdge::BOTTOM;
|
2020-02-08 06:24:50 +00:00
|
|
|
|
|
|
|
if self.edges.intersects(left_right) {
|
2020-02-11 08:06:36 +00:00
|
|
|
if self.edges.intersects(ResizeEdge::LEFT) {
|
2020-02-03 13:31:15 +00:00
|
|
|
dx = -dx;
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
new_window_width = (self.initial_window_size.w as f64 + dx) as i32;
|
2020-02-08 06:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.edges.intersects(top_bottom) {
|
2020-02-11 08:06:36 +00:00
|
|
|
if self.edges.intersects(ResizeEdge::TOP) {
|
2020-02-03 13:31:15 +00:00
|
|
|
dy = -dy;
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
new_window_height = (self.initial_window_size.h as f64 + dy) as i32;
|
2020-02-08 06:24:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
let (min_size, max_size) = with_states(self.toplevel.get_surface().unwrap(), |states| {
|
|
|
|
let data = states.cached_state.current::<SurfaceCachedState>();
|
|
|
|
(data.min_size, data.max_size)
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-08 06:24:50 +00:00
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
let min_width = min_size.w.max(1);
|
|
|
|
let min_height = min_size.h.max(1);
|
|
|
|
let max_width = if max_size.w == 0 {
|
2020-02-08 06:24:50 +00:00
|
|
|
i32::max_value()
|
2020-02-03 13:31:15 +00:00
|
|
|
} else {
|
2021-07-05 17:21:15 +00:00
|
|
|
max_size.w
|
2020-02-03 13:31:15 +00:00
|
|
|
};
|
2021-07-05 17:21:15 +00:00
|
|
|
let max_height = if max_size.h == 0 {
|
2020-02-08 06:24:50 +00:00
|
|
|
i32::max_value()
|
|
|
|
} else {
|
2021-07-05 17:21:15 +00:00
|
|
|
max_size.h
|
2020-02-08 06:24:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
new_window_width = new_window_width.max(min_width).min(max_width);
|
|
|
|
new_window_height = new_window_height.max(min_height).min(max_height);
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
self.last_window_size = (new_window_width, new_window_height).into();
|
2020-02-03 13:31:15 +00:00
|
|
|
|
|
|
|
match &self.toplevel {
|
2021-06-15 21:32:02 +00:00
|
|
|
SurfaceKind::Xdg(xdg) => {
|
2021-06-23 07:43:53 +00:00
|
|
|
let ret = xdg.with_pending_state(|state| {
|
|
|
|
state.states.set(xdg_toplevel::State::Resizing);
|
|
|
|
state.size = Some(self.last_window_size);
|
|
|
|
});
|
|
|
|
if ret.is_ok() {
|
2021-06-15 21:32:02 +00:00
|
|
|
xdg.send_configure();
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 17:21:15 +00:00
|
|
|
SurfaceKind::Wl(wl) => wl.send_configure(self.last_window_size, self.edges.into()),
|
2021-01-04 08:46:59 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
SurfaceKind::X11(_) => {
|
|
|
|
// TODO: What to do here? Send the update via X11?
|
|
|
|
}
|
2020-02-03 13:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn button(
|
|
|
|
&mut self,
|
|
|
|
handle: &mut PointerInnerHandle<'_>,
|
|
|
|
button: u32,
|
|
|
|
state: ButtonState,
|
2020-08-08 04:37:35 +00:00
|
|
|
serial: Serial,
|
2020-02-03 13:31:15 +00:00
|
|
|
time: u32,
|
|
|
|
) {
|
|
|
|
handle.button(button, state, serial, time);
|
|
|
|
if handle.current_pressed().is_empty() {
|
|
|
|
// No more buttons are pressed, release the grab.
|
|
|
|
handle.unset_grab(serial, time);
|
|
|
|
|
2021-06-17 20:41:53 +00:00
|
|
|
// If toplevel is dead, we can't resize it, so we return early.
|
|
|
|
if !self.toplevel.alive() | self.toplevel.get_surface().is_none() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:31:15 +00:00
|
|
|
if let SurfaceKind::Xdg(xdg) = &self.toplevel {
|
2021-06-23 07:43:53 +00:00
|
|
|
let ret = xdg.with_pending_state(|state| {
|
|
|
|
state.states.unset(xdg_toplevel::State::Resizing);
|
|
|
|
state.size = Some(self.last_window_size);
|
|
|
|
});
|
|
|
|
if ret.is_ok() {
|
2021-06-15 21:32:02 +00:00
|
|
|
xdg.send_configure();
|
|
|
|
}
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
with_states(self.toplevel.get_surface().unwrap(), |states| {
|
|
|
|
let mut data = states
|
|
|
|
.data_map
|
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut();
|
|
|
|
if let ResizeState::Resizing(resize_data) = data.resize_state {
|
|
|
|
data.resize_state = ResizeState::WaitingForFinalAck(resize_data, serial);
|
|
|
|
} else {
|
|
|
|
panic!("invalid resize state: {:?}", data.resize_state);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-08 05:46:15 +00:00
|
|
|
} else {
|
2021-06-23 07:43:53 +00:00
|
|
|
with_states(self.toplevel.get_surface().unwrap(), |states| {
|
|
|
|
let mut data = states
|
|
|
|
.data_map
|
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut();
|
|
|
|
if let ResizeState::Resizing(resize_data) = data.resize_state {
|
|
|
|
data.resize_state = ResizeState::WaitingForCommit(resize_data);
|
|
|
|
} else {
|
|
|
|
panic!("invalid resize state: {:?}", data.resize_state);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-03 13:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn axis(&mut self, handle: &mut PointerInnerHandle<'_>, details: AxisFrame) {
|
|
|
|
handle.axis(details)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_data(&self) -> &GrabStartData {
|
|
|
|
&self.start_data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 10:42:02 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ShellHandles {
|
2021-06-23 07:43:53 +00:00
|
|
|
pub xdg_state: Arc<Mutex<XdgShellState>>,
|
|
|
|
pub wl_state: Arc<Mutex<WlShellState>>,
|
2020-05-02 10:42:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:58:13 +00:00
|
|
|
fn fullscreen_output_geometry(
|
|
|
|
wl_surface: &wl_surface::WlSurface,
|
|
|
|
wl_output: Option<&wl_output::WlOutput>,
|
|
|
|
window_map: &WindowMap,
|
|
|
|
output_map: &OutputMap,
|
2021-07-05 17:21:15 +00:00
|
|
|
) -> Option<Rectangle<i32, Logical>> {
|
2021-06-30 17:58:13 +00:00
|
|
|
// First test if a specific output has been requested
|
|
|
|
// if the requested output is not found ignore the request
|
|
|
|
if let Some(wl_output) = wl_output {
|
2021-07-30 16:03:19 +00:00
|
|
|
return output_map.find_by_output(wl_output).map(|o| o.geometry());
|
2021-06-30 17:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is no output preference, try to find the output
|
|
|
|
// where the window is currently active
|
|
|
|
let window_location = window_map
|
|
|
|
.find(wl_surface)
|
|
|
|
.and_then(|kind| window_map.location(&kind));
|
|
|
|
|
|
|
|
if let Some(location) = window_location {
|
2021-07-01 17:51:06 +00:00
|
|
|
let window_output = output_map.find_by_position(location).map(|o| o.geometry());
|
2021-06-30 17:58:13 +00:00
|
|
|
|
|
|
|
if let Some(result) = window_output {
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to primary output
|
2021-07-01 17:51:06 +00:00
|
|
|
output_map.with_primary().map(|o| o.geometry())
|
2021-06-30 17:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:29:40 +00:00
|
|
|
pub fn init_shell<BackendData: 'static>(display: Rc<RefCell<Display>>, log: ::slog::Logger) -> ShellHandles {
|
2018-04-22 09:58:39 +00:00
|
|
|
// Create the compositor
|
2021-06-23 07:43:53 +00:00
|
|
|
compositor_init(
|
2021-06-21 19:29:40 +00:00
|
|
|
&mut *display.borrow_mut(),
|
2021-06-23 07:43:53 +00:00
|
|
|
move |surface, mut ddata| {
|
|
|
|
let anvil_state = ddata.get::<AnvilState<BackendData>>().unwrap();
|
|
|
|
let window_map = anvil_state.window_map.as_ref();
|
2021-07-30 16:15:15 +00:00
|
|
|
let output_map = anvil_state.output_map.as_ref();
|
|
|
|
surface_commit(&surface, &*window_map, &*output_map)
|
2018-04-22 09:58:39 +00:00
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
// init the xdg_shell
|
2021-10-08 21:12:22 +00:00
|
|
|
let (xdg_shell_state, _) = xdg_shell_init(
|
2021-06-21 19:29:40 +00:00
|
|
|
&mut *display.borrow_mut(),
|
2021-09-02 22:34:55 +00:00
|
|
|
move |shell_event, mut ddata| {
|
|
|
|
let state = ddata.get::<AnvilState<BackendData>>().unwrap();
|
2021-08-03 13:43:40 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
match shell_event {
|
|
|
|
XdgRequest::NewToplevel { surface } => {
|
|
|
|
// place the window at a random location on the primary output
|
|
|
|
// or if there is not output in a [0;800]x[0;800] square
|
|
|
|
use rand::distributions::{Distribution, Uniform};
|
|
|
|
|
|
|
|
let output_geometry = state
|
|
|
|
.output_map
|
|
|
|
.borrow()
|
|
|
|
.with_primary()
|
|
|
|
.map(|o| o.geometry())
|
|
|
|
.unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800)));
|
2020-02-01 15:42:08 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let max_x =
|
|
|
|
output_geometry.loc.x + (((output_geometry.size.w as f32) / 3.0) * 2.0) as i32;
|
|
|
|
let max_y =
|
|
|
|
output_geometry.loc.y + (((output_geometry.size.h as f32) / 3.0) * 2.0) as i32;
|
|
|
|
let x_range = Uniform::new(output_geometry.loc.x, max_x);
|
|
|
|
let y_range = Uniform::new(output_geometry.loc.y, max_y);
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let x = x_range.sample(&mut rng);
|
|
|
|
let y = y_range.sample(&mut rng);
|
|
|
|
// Do not send a configure here, the initial configure
|
|
|
|
// of a xdg_surface has to be sent during the commit if
|
|
|
|
// the surface is not already configured
|
|
|
|
state
|
|
|
|
.window_map
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(SurfaceKind::Xdg(surface), (x, y).into());
|
2020-02-02 15:07:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::NewPopup { surface, positioner } => {
|
|
|
|
// Do not send a configure here, the initial configure
|
|
|
|
// of a xdg_surface has to be sent during the commit if
|
|
|
|
// the surface is not already configured
|
2020-02-02 15:05:49 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
// TODO: properly recompute the geometry with the whole of positioner state
|
|
|
|
surface
|
|
|
|
.with_pending_state(|state| {
|
|
|
|
// NOTE: This is not really necessary as the default geometry
|
|
|
|
// is already set the same way, but for demonstrating how
|
|
|
|
// to set the initial popup geometry this code is left as
|
|
|
|
// an example
|
|
|
|
state.geometry = positioner.get_geometry();
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
state
|
|
|
|
.window_map
|
|
|
|
.borrow_mut()
|
|
|
|
.insert_popup(PopupKind::Xdg(surface));
|
2020-02-02 15:09:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::RePosition {
|
|
|
|
surface,
|
|
|
|
positioner,
|
|
|
|
token,
|
|
|
|
} => {
|
|
|
|
let result = surface.with_pending_state(|state| {
|
|
|
|
// NOTE: This is again a simplification, a proper compositor would
|
|
|
|
// calculate the geometry of the popup here. For simplicity we just
|
|
|
|
// use the default implementation here that does not take the
|
|
|
|
// window position and output constraints into account.
|
|
|
|
let geometry = positioner.get_geometry();
|
|
|
|
state.geometry = geometry;
|
|
|
|
state.positioner = positioner;
|
|
|
|
});
|
2021-07-19 18:35:03 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
if result.is_ok() {
|
|
|
|
surface.send_repositioned(token);
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 17:12:38 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::Move {
|
|
|
|
surface,
|
|
|
|
seat,
|
|
|
|
serial,
|
|
|
|
} => {
|
|
|
|
let seat = Seat::from_resource(&seat).unwrap();
|
|
|
|
// TODO: touch move.
|
|
|
|
let pointer = seat.get_pointer().unwrap();
|
2021-07-19 18:35:03 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
// Check that this surface has a click grab.
|
|
|
|
if !pointer.has_grab(serial) {
|
|
|
|
return;
|
2021-07-19 17:12:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let start_data = pointer.grab_start_data().unwrap();
|
2020-02-01 15:42:08 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
// If the focus was for a different surface, ignore the request.
|
|
|
|
if start_data.focus.is_none()
|
|
|
|
|| !start_data
|
|
|
|
.focus
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.as_ref()
|
|
|
|
.same_client_as(surface.get_surface().unwrap().as_ref())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let toplevel = SurfaceKind::Xdg(surface.clone());
|
|
|
|
let mut initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
|
|
|
|
|
|
|
|
// If surface is maximized then unmaximize it
|
|
|
|
if let Some(current_state) = surface.current_state() {
|
|
|
|
if current_state.states.contains(xdg_toplevel::State::Maximized) {
|
|
|
|
let fs_changed = surface.with_pending_state(|state| {
|
|
|
|
state.states.unset(xdg_toplevel::State::Maximized);
|
|
|
|
state.size = None;
|
|
|
|
});
|
|
|
|
|
|
|
|
if fs_changed.is_ok() {
|
|
|
|
surface.send_configure();
|
|
|
|
|
|
|
|
// NOTE: In real compositor mouse location should be mapped to a new window size
|
|
|
|
// For example, you could:
|
|
|
|
// 1) transform mouse pointer position from compositor space to window space (location relative)
|
|
|
|
// 2) divide the x coordinate by width of the window to get the percentage
|
|
|
|
// - 0.0 would be on the far left of the window
|
|
|
|
// - 0.5 would be in middle of the window
|
|
|
|
// - 1.0 would be on the far right of the window
|
|
|
|
// 3) multiply the percentage by new window width
|
|
|
|
// 4) by doing that, drag will look a lot more natural
|
|
|
|
//
|
|
|
|
// but for anvil needs setting location to pointer location is fine
|
|
|
|
let pos = pointer.current_location();
|
|
|
|
initial_window_location = (pos.x as i32, pos.y as i32).into();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let grab = MoveSurfaceGrab {
|
|
|
|
start_data,
|
|
|
|
window_map: state.window_map.clone(),
|
|
|
|
toplevel,
|
|
|
|
initial_window_location,
|
|
|
|
};
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
pointer.set_grab(grab, serial);
|
2020-02-03 13:31:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::Resize {
|
|
|
|
surface,
|
|
|
|
seat,
|
|
|
|
serial,
|
|
|
|
edges,
|
|
|
|
} => {
|
|
|
|
let seat = Seat::from_resource(&seat).unwrap();
|
|
|
|
// TODO: touch resize.
|
|
|
|
let pointer = seat.get_pointer().unwrap();
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
// Check that this surface has a click grab.
|
|
|
|
if !pointer.has_grab(serial) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let start_data = pointer.grab_start_data().unwrap();
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
// If the focus was for a different surface, ignore the request.
|
|
|
|
if start_data.focus.is_none()
|
|
|
|
|| !start_data
|
|
|
|
.focus
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.as_ref()
|
|
|
|
.same_client_as(surface.get_surface().unwrap().as_ref())
|
|
|
|
{
|
|
|
|
return;
|
2021-06-23 07:43:53 +00:00
|
|
|
}
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let toplevel = SurfaceKind::Xdg(surface.clone());
|
|
|
|
let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
|
|
|
|
let geometry = state.window_map.borrow().geometry(&toplevel).unwrap();
|
|
|
|
let initial_window_size = geometry.size;
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
with_states(surface.get_surface().unwrap(), move |states| {
|
2021-06-30 18:57:29 +00:00
|
|
|
states
|
|
|
|
.data_map
|
2021-09-02 22:34:55 +00:00
|
|
|
.get::<RefCell<SurfaceData>>()
|
2021-06-30 18:57:29 +00:00
|
|
|
.unwrap()
|
2021-09-02 22:34:55 +00:00
|
|
|
.borrow_mut()
|
|
|
|
.resize_state = ResizeState::Resizing(ResizeData {
|
|
|
|
edges: edges.into(),
|
|
|
|
initial_window_location,
|
|
|
|
initial_window_size,
|
|
|
|
});
|
2021-06-30 18:57:29 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let grab = ResizeSurfaceGrab {
|
|
|
|
start_data,
|
|
|
|
toplevel,
|
|
|
|
edges: edges.into(),
|
|
|
|
initial_window_size,
|
|
|
|
last_window_size: initial_window_size,
|
|
|
|
};
|
|
|
|
|
|
|
|
pointer.set_grab(grab, serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
XdgRequest::AckConfigure {
|
|
|
|
surface,
|
|
|
|
configure: Configure::Toplevel(configure),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let waiting_for_serial = with_states(&surface, |states| {
|
|
|
|
if let Some(data) = states.data_map.get::<RefCell<SurfaceData>>() {
|
|
|
|
if let ResizeState::WaitingForFinalAck(_, serial) = data.borrow().resize_state {
|
|
|
|
return Some(serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if let Some(serial) = waiting_for_serial {
|
|
|
|
// When the resize grab is released the surface
|
|
|
|
// resize state will be set to WaitingForFinalAck
|
|
|
|
// and the client will receive a configure request
|
|
|
|
// without the resize state to inform the client
|
|
|
|
// resizing has finished. Here we will wait for
|
|
|
|
// the client to acknowledge the end of the
|
|
|
|
// resizing. To check if the surface was resizing
|
|
|
|
// before sending the configure we need to use
|
|
|
|
// the current state as the received acknowledge
|
|
|
|
// will no longer have the resize state set
|
|
|
|
let is_resizing = with_states(&surface, |states| {
|
|
|
|
states
|
2021-06-23 07:43:53 +00:00
|
|
|
.data_map
|
2021-09-02 22:34:55 +00:00
|
|
|
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
|
2021-06-23 07:43:53 +00:00
|
|
|
.unwrap()
|
2021-09-02 22:34:55 +00:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.current
|
|
|
|
.states
|
|
|
|
.contains(xdg_toplevel::State::Resizing)
|
2021-06-23 07:43:53 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2021-09-02 22:34:55 +00:00
|
|
|
|
|
|
|
if configure.serial >= serial && is_resizing {
|
|
|
|
with_states(&surface, |states| {
|
|
|
|
let mut data = states
|
|
|
|
.data_map
|
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut();
|
|
|
|
if let ResizeState::WaitingForFinalAck(resize_data, _) = data.resize_state {
|
|
|
|
data.resize_state = ResizeState::WaitingForCommit(resize_data);
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-02-08 05:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
|
|
|
|
XdgRequest::Fullscreen { surface, output, .. } => {
|
|
|
|
// NOTE: This is only one part of the solution. We can set the
|
|
|
|
// location and configure size here, but the surface should be rendered fullscreen
|
|
|
|
// independently from its buffer size
|
|
|
|
let wl_surface = if let Some(surface) = surface.get_surface() {
|
|
|
|
surface
|
|
|
|
} else {
|
|
|
|
// If there is no underlying surface just ignore the request
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let output_geometry = fullscreen_output_geometry(
|
|
|
|
wl_surface,
|
|
|
|
output.as_ref(),
|
|
|
|
&state.window_map.borrow(),
|
|
|
|
&state.output_map.borrow(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(geometry) = output_geometry {
|
|
|
|
if let Some(surface) = surface.get_surface() {
|
|
|
|
let mut xdg_window_map = state.window_map.borrow_mut();
|
|
|
|
|
|
|
|
if let Some(kind) = xdg_window_map.find(surface) {
|
|
|
|
xdg_window_map.set_location(&kind, geometry.loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = surface.with_pending_state(|state| {
|
|
|
|
state.states.set(xdg_toplevel::State::Fullscreen);
|
|
|
|
state.size = Some(geometry.size);
|
|
|
|
state.fullscreen_output = output;
|
|
|
|
});
|
|
|
|
if ret.is_ok() {
|
|
|
|
surface.send_configure();
|
2021-06-21 19:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
}
|
2021-06-21 19:29:40 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::UnFullscreen { surface } => {
|
2021-06-21 19:29:40 +00:00
|
|
|
let ret = surface.with_pending_state(|state| {
|
2021-09-02 22:34:55 +00:00
|
|
|
state.states.unset(xdg_toplevel::State::Fullscreen);
|
|
|
|
state.size = None;
|
|
|
|
state.fullscreen_output = None;
|
2021-06-21 19:29:40 +00:00
|
|
|
});
|
|
|
|
if ret.is_ok() {
|
|
|
|
surface.send_configure();
|
|
|
|
}
|
2021-06-15 21:32:02 +00:00
|
|
|
}
|
2021-06-21 19:29:40 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
XdgRequest::Maximize { surface } => {
|
|
|
|
// NOTE: This should use layer-shell when it is implemented to
|
|
|
|
// get the correct maximum size
|
|
|
|
let output_geometry = {
|
|
|
|
let xdg_window_map = state.window_map.borrow();
|
|
|
|
surface
|
|
|
|
.get_surface()
|
|
|
|
.and_then(|s| xdg_window_map.find(s))
|
|
|
|
.and_then(|k| xdg_window_map.location(&k))
|
|
|
|
.and_then(|position| {
|
|
|
|
state
|
|
|
|
.output_map
|
|
|
|
.borrow()
|
|
|
|
.find_by_position(position)
|
|
|
|
.map(|o| o.geometry())
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(geometry) = output_geometry {
|
|
|
|
if let Some(surface) = surface.get_surface() {
|
|
|
|
let mut xdg_window_map = state.window_map.borrow_mut();
|
|
|
|
|
|
|
|
if let Some(kind) = xdg_window_map.find(surface) {
|
|
|
|
xdg_window_map.set_location(&kind, geometry.loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = surface.with_pending_state(|state| {
|
|
|
|
state.states.set(xdg_toplevel::State::Maximized);
|
|
|
|
state.size = Some(geometry.size);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ret.is_ok() {
|
|
|
|
surface.send_configure();
|
2021-06-21 19:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
}
|
|
|
|
XdgRequest::UnMaximize { surface } => {
|
2021-06-21 19:29:40 +00:00
|
|
|
let ret = surface.with_pending_state(|state| {
|
2021-09-02 22:34:55 +00:00
|
|
|
state.states.unset(xdg_toplevel::State::Maximized);
|
|
|
|
state.size = None;
|
2021-06-21 19:29:40 +00:00
|
|
|
});
|
2021-09-02 22:34:55 +00:00
|
|
|
|
2021-06-21 19:29:40 +00:00
|
|
|
if ret.is_ok() {
|
|
|
|
surface.send_configure();
|
|
|
|
}
|
2021-06-15 21:32:02 +00:00
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
_ => (),
|
2021-06-15 21:32:02 +00:00
|
|
|
}
|
2017-09-22 12:56:59 +00:00
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2018-04-23 09:40:41 +00:00
|
|
|
// init the wl_shell
|
|
|
|
let (wl_shell_state, _) = wl_shell_init(
|
2021-06-21 19:29:40 +00:00
|
|
|
&mut *display.borrow_mut(),
|
2021-09-02 22:34:55 +00:00
|
|
|
move |req: ShellRequest, mut ddata| {
|
|
|
|
let state = ddata.get::<AnvilState<BackendData>>().unwrap();
|
|
|
|
|
2020-02-01 15:42:08 +00:00
|
|
|
match req {
|
|
|
|
ShellRequest::SetKind {
|
|
|
|
surface,
|
|
|
|
kind: ShellSurfaceKind::Toplevel,
|
|
|
|
} => {
|
2021-06-21 19:29:40 +00:00
|
|
|
// place the window at a random location on the primary output
|
|
|
|
// or if there is not output in a [0;800]x[0;800] square
|
2020-02-01 15:42:08 +00:00
|
|
|
use rand::distributions::{Distribution, Uniform};
|
2021-06-21 19:29:40 +00:00
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
let output_geometry = state
|
|
|
|
.output_map
|
2021-06-21 19:29:40 +00:00
|
|
|
.borrow()
|
2021-07-01 17:51:06 +00:00
|
|
|
.with_primary()
|
|
|
|
.map(|o| o.geometry())
|
2021-07-05 17:21:15 +00:00
|
|
|
.unwrap_or_else(|| Rectangle::from_loc_and_size((0, 0), (800, 800)));
|
|
|
|
let max_x =
|
|
|
|
output_geometry.loc.x + (((output_geometry.size.w as f32) / 3.0) * 2.0) as i32;
|
|
|
|
let max_y =
|
|
|
|
output_geometry.loc.y + (((output_geometry.size.h as f32) / 3.0) * 2.0) as i32;
|
|
|
|
let x_range = Uniform::new(output_geometry.loc.x, max_x);
|
|
|
|
let y_range = Uniform::new(output_geometry.loc.y, max_y);
|
2020-02-01 15:42:08 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
2021-06-21 19:29:40 +00:00
|
|
|
let x = x_range.sample(&mut rng);
|
|
|
|
let y = y_range.sample(&mut rng);
|
2021-09-02 22:34:55 +00:00
|
|
|
state
|
|
|
|
.window_map
|
2020-02-01 15:42:08 +00:00
|
|
|
.borrow_mut()
|
2021-07-05 17:21:15 +00:00
|
|
|
.insert(SurfaceKind::Wl(surface), (x, y).into());
|
2020-02-01 15:42:08 +00:00
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
|
2021-06-21 19:29:40 +00:00
|
|
|
ShellRequest::SetKind {
|
|
|
|
surface,
|
|
|
|
kind: ShellSurfaceKind::Fullscreen { output, .. },
|
|
|
|
} => {
|
|
|
|
// NOTE: This is only one part of the solution. We can set the
|
|
|
|
// location and configure size here, but the surface should be rendered fullscreen
|
|
|
|
// independently from its buffer size
|
|
|
|
let wl_surface = if let Some(surface) = surface.get_surface() {
|
|
|
|
surface
|
|
|
|
} else {
|
|
|
|
// If there is no underlying surface just ignore the request
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2021-06-30 17:58:13 +00:00
|
|
|
let output_geometry = fullscreen_output_geometry(
|
|
|
|
wl_surface,
|
|
|
|
output.as_ref(),
|
2021-09-02 22:34:55 +00:00
|
|
|
&state.window_map.borrow(),
|
|
|
|
&state.output_map.borrow(),
|
2021-06-30 17:58:13 +00:00
|
|
|
);
|
2021-06-21 19:29:40 +00:00
|
|
|
|
2021-06-30 17:58:13 +00:00
|
|
|
if let Some(geometry) = output_geometry {
|
2021-09-02 22:34:55 +00:00
|
|
|
state
|
|
|
|
.window_map
|
2021-06-28 10:07:33 +00:00
|
|
|
.borrow_mut()
|
2021-07-05 17:21:15 +00:00
|
|
|
.insert(SurfaceKind::Wl(surface), geometry.loc);
|
2021-06-28 10:07:33 +00:00
|
|
|
}
|
2021-06-21 19:29:40 +00:00
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
|
2020-02-01 15:42:08 +00:00
|
|
|
ShellRequest::Move {
|
|
|
|
surface,
|
|
|
|
seat,
|
|
|
|
serial,
|
|
|
|
} => {
|
|
|
|
let seat = Seat::from_resource(&seat).unwrap();
|
|
|
|
// TODO: touch move.
|
|
|
|
let pointer = seat.get_pointer().unwrap();
|
|
|
|
|
2020-02-02 15:07:43 +00:00
|
|
|
// Check that this surface has a click grab.
|
|
|
|
if !pointer.has_grab(serial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:05:49 +00:00
|
|
|
let start_data = pointer.grab_start_data().unwrap();
|
|
|
|
|
2020-02-02 15:09:21 +00:00
|
|
|
// If the focus was for a different surface, ignore the request.
|
|
|
|
if start_data.focus.is_none()
|
|
|
|
|| !start_data
|
|
|
|
.focus
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.as_ref()
|
|
|
|
.same_client_as(surface.get_surface().unwrap().as_ref())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:42:08 +00:00
|
|
|
let toplevel = SurfaceKind::Wl(surface);
|
2021-09-02 22:34:55 +00:00
|
|
|
let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
|
2020-02-01 15:42:08 +00:00
|
|
|
|
|
|
|
let grab = MoveSurfaceGrab {
|
2020-02-02 15:05:49 +00:00
|
|
|
start_data,
|
2021-09-02 22:34:55 +00:00
|
|
|
window_map: state.window_map.clone(),
|
2020-02-01 15:42:08 +00:00
|
|
|
toplevel,
|
|
|
|
initial_window_location,
|
|
|
|
};
|
|
|
|
|
|
|
|
pointer.set_grab(grab, serial);
|
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
|
2020-02-03 13:31:15 +00:00
|
|
|
ShellRequest::Resize {
|
|
|
|
surface,
|
|
|
|
seat,
|
|
|
|
serial,
|
|
|
|
edges,
|
|
|
|
} => {
|
|
|
|
let seat = Seat::from_resource(&seat).unwrap();
|
|
|
|
// TODO: touch resize.
|
|
|
|
let pointer = seat.get_pointer().unwrap();
|
|
|
|
|
|
|
|
// Check that this surface has a click grab.
|
|
|
|
if !pointer.has_grab(serial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start_data = pointer.grab_start_data().unwrap();
|
|
|
|
|
|
|
|
// If the focus was for a different surface, ignore the request.
|
|
|
|
if start_data.focus.is_none()
|
|
|
|
|| !start_data
|
|
|
|
.focus
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.as_ref()
|
|
|
|
.same_client_as(surface.get_surface().unwrap().as_ref())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:46:15 +00:00
|
|
|
let toplevel = SurfaceKind::Wl(surface.clone());
|
2021-09-02 22:34:55 +00:00
|
|
|
let initial_window_location = state.window_map.borrow().location(&toplevel).unwrap();
|
|
|
|
let geometry = state.window_map.borrow().geometry(&toplevel).unwrap();
|
2021-07-05 17:21:15 +00:00
|
|
|
let initial_window_size = geometry.size;
|
2020-02-03 13:31:15 +00:00
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
with_states(surface.get_surface().unwrap(), move |states| {
|
|
|
|
states
|
|
|
|
.data_map
|
2020-04-10 15:01:49 +00:00
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut()
|
|
|
|
.resize_state = ResizeState::Resizing(ResizeData {
|
|
|
|
edges: edges.into(),
|
|
|
|
initial_window_location,
|
|
|
|
initial_window_size,
|
|
|
|
});
|
2021-06-23 07:43:53 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2020-02-03 13:31:15 +00:00
|
|
|
let grab = ResizeSurfaceGrab {
|
|
|
|
start_data,
|
|
|
|
toplevel,
|
2020-02-11 08:06:36 +00:00
|
|
|
edges: edges.into(),
|
2020-02-03 13:31:15 +00:00
|
|
|
initial_window_size,
|
|
|
|
last_window_size: initial_window_size,
|
|
|
|
};
|
|
|
|
|
|
|
|
pointer.set_grab(grab, serial);
|
|
|
|
}
|
2020-02-01 15:42:08 +00:00
|
|
|
_ => (),
|
2018-09-24 22:32:09 +00:00
|
|
|
}
|
|
|
|
},
|
2018-04-23 09:40:41 +00:00
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2021-07-30 16:15:15 +00:00
|
|
|
smithay::wayland::shell::wlr_layer::wlr_layer_shell_init(
|
|
|
|
&mut *display.borrow_mut(),
|
|
|
|
move |event, mut ddata| match event {
|
|
|
|
LayerShellRequest::NewLayerSurface {
|
|
|
|
surface,
|
|
|
|
output,
|
|
|
|
layer,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let anvil_state = ddata.get::<AnvilState<BackendData>>().unwrap();
|
2021-09-02 22:34:55 +00:00
|
|
|
let output_map = anvil_state.output_map.borrow();
|
2021-07-30 16:15:15 +00:00
|
|
|
|
|
|
|
let output = output.and_then(|output| output_map.find_by_output(&output));
|
|
|
|
let output = output.unwrap_or_else(|| {
|
|
|
|
output_map
|
|
|
|
.find_by_position(anvil_state.pointer_location.to_i32_round())
|
|
|
|
.unwrap_or_else(|| output_map.with_primary().unwrap())
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(wl_surface) = surface.get_surface() {
|
|
|
|
output.add_layer_surface(wl_surface.clone());
|
|
|
|
|
2021-09-02 22:34:55 +00:00
|
|
|
anvil_state.window_map.borrow_mut().layers.insert(surface, layer);
|
2021-07-30 16:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 22:34:55 +00:00
|
|
|
|
2021-07-30 16:15:15 +00:00
|
|
|
LayerShellRequest::AckConfigure { .. } => {}
|
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2020-05-02 10:42:02 +00:00
|
|
|
ShellHandles {
|
|
|
|
xdg_state: xdg_shell_state,
|
|
|
|
wl_state: wl_shell_state,
|
|
|
|
}
|
2017-09-22 12:56:59 +00:00
|
|
|
}
|
2018-05-07 17:56:38 +00:00
|
|
|
|
2020-02-08 05:46:15 +00:00
|
|
|
/// Information about the resize operation.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct ResizeData {
|
|
|
|
/// The edges the surface is being resized with.
|
2020-02-11 08:06:36 +00:00
|
|
|
edges: ResizeEdge,
|
2020-02-08 05:46:15 +00:00
|
|
|
/// The initial window location.
|
2021-07-05 17:21:15 +00:00
|
|
|
initial_window_location: Point<i32, Logical>,
|
2020-02-08 05:46:15 +00:00
|
|
|
/// The initial window size (geometry width and height).
|
2021-07-05 17:21:15 +00:00
|
|
|
initial_window_size: Size<i32, Logical>,
|
2020-02-08 05:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// State of the resize operation.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub enum ResizeState {
|
|
|
|
/// The surface is not being resized.
|
|
|
|
NotResizing,
|
|
|
|
/// The surface is currently being resized.
|
|
|
|
Resizing(ResizeData),
|
|
|
|
/// The resize has finished, and the surface needs to ack the final configure.
|
2020-08-08 04:37:35 +00:00
|
|
|
WaitingForFinalAck(ResizeData, Serial),
|
2020-02-08 05:46:15 +00:00
|
|
|
/// The resize has finished, and the surface needs to commit its final state.
|
|
|
|
WaitingForCommit(ResizeData),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ResizeState {
|
|
|
|
fn default() -> Self {
|
|
|
|
ResizeState::NotResizing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 17:56:38 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SurfaceData {
|
2021-06-23 07:43:53 +00:00
|
|
|
pub buffer: Option<wl_buffer::WlBuffer>,
|
2021-04-06 23:15:03 +00:00
|
|
|
pub texture: Option<Box<dyn std::any::Any + 'static>>,
|
2021-07-05 17:21:15 +00:00
|
|
|
pub geometry: Option<Rectangle<i32, Logical>>,
|
2020-02-08 05:46:15 +00:00
|
|
|
pub resize_state: ResizeState,
|
2021-07-05 17:21:15 +00:00
|
|
|
pub buffer_dimensions: Option<Size<i32, Physical>>,
|
|
|
|
pub buffer_scale: i32,
|
2020-04-21 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SurfaceData {
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn update_buffer(&mut self, attrs: &mut SurfaceAttributes) {
|
|
|
|
match attrs.buffer.take() {
|
|
|
|
Some(BufferAssignment::NewBuffer { buffer, .. }) => {
|
|
|
|
// new contents
|
2021-07-05 17:21:15 +00:00
|
|
|
self.buffer_dimensions = buffer_dimensions(&buffer);
|
|
|
|
self.buffer_scale = attrs.buffer_scale;
|
2021-06-23 07:43:53 +00:00
|
|
|
if let Some(old_buffer) = std::mem::replace(&mut self.buffer, Some(buffer)) {
|
|
|
|
old_buffer.release();
|
|
|
|
}
|
|
|
|
self.texture = None;
|
2020-04-21 18:53:55 +00:00
|
|
|
}
|
2021-06-23 07:43:53 +00:00
|
|
|
Some(BufferAssignment::Removed) => {
|
|
|
|
// remove the contents
|
|
|
|
self.buffer = None;
|
2021-07-05 17:21:15 +00:00
|
|
|
self.buffer_dimensions = None;
|
2021-06-23 07:43:53 +00:00
|
|
|
self.texture = None;
|
2020-04-21 18:53:55 +00:00
|
|
|
}
|
2021-06-23 07:43:53 +00:00
|
|
|
None => {}
|
2020-04-21 18:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 17:56:38 +00:00
|
|
|
|
2020-02-03 12:55:48 +00:00
|
|
|
/// Returns the size of the surface.
|
2021-07-05 17:21:15 +00:00
|
|
|
pub fn size(&self) -> Option<Size<i32, Logical>> {
|
|
|
|
self.buffer_dimensions
|
|
|
|
.map(|dims| dims.to_logical(self.buffer_scale))
|
2020-02-03 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the surface's input region contains the point.
|
2021-07-05 17:21:15 +00:00
|
|
|
pub fn contains_point(&self, attrs: &SurfaceAttributes, point: Point<f64, Logical>) -> bool {
|
|
|
|
let size = match self.size() {
|
2020-02-03 12:55:48 +00:00
|
|
|
None => return false, // If the surface has no size, it can't have an input region.
|
2021-07-05 17:21:15 +00:00
|
|
|
Some(size) => size,
|
2020-02-03 12:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let rect = Rectangle {
|
2021-07-05 17:21:15 +00:00
|
|
|
loc: (0, 0).into(),
|
|
|
|
size,
|
|
|
|
}
|
|
|
|
.to_f64();
|
2020-02-03 12:55:48 +00:00
|
|
|
|
|
|
|
// The input region is always within the surface itself, so if the surface itself doesn't contain the
|
|
|
|
// point we can return false.
|
|
|
|
if !rect.contains(point) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no input region, we're done.
|
2021-06-23 07:43:53 +00:00
|
|
|
if attrs.input_region.is_none() {
|
2020-02-03 12:55:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
attrs
|
|
|
|
.input_region
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.contains(point.to_i32_floor())
|
2020-02-03 12:55:48 +00:00
|
|
|
}
|
2020-04-21 17:42:03 +00:00
|
|
|
|
|
|
|
/// Send the frame callback if it had been requested
|
2021-06-23 07:43:53 +00:00
|
|
|
pub fn send_frame(attrs: &mut SurfaceAttributes, time: u32) {
|
|
|
|
for callback in attrs.frame_callbacks.drain(..) {
|
2020-10-10 18:01:18 +00:00
|
|
|
callback.done(time);
|
2020-04-21 17:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 16:15:15 +00:00
|
|
|
fn surface_commit(
|
|
|
|
surface: &wl_surface::WlSurface,
|
|
|
|
window_map: &RefCell<WindowMap>,
|
|
|
|
output_map: &RefCell<OutputMap>,
|
|
|
|
) {
|
2021-01-04 08:18:35 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
super::xwayland::commit_hook(surface);
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
let mut window_map = window_map.borrow_mut();
|
2021-06-15 21:32:02 +00:00
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
if !is_sync_subsurface(surface) {
|
|
|
|
// Update the buffer of all child surfaces
|
|
|
|
with_surface_tree_upward(
|
|
|
|
surface,
|
|
|
|
(),
|
|
|
|
|_, _, _| TraversalAction::DoChildren(()),
|
|
|
|
|_, states, _| {
|
|
|
|
states
|
|
|
|
.data_map
|
|
|
|
.insert_if_missing(|| RefCell::new(SurfaceData::default()));
|
|
|
|
let mut data = states
|
|
|
|
.data_map
|
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut();
|
|
|
|
data.update_buffer(&mut *states.cached_state.current::<SurfaceAttributes>());
|
|
|
|
},
|
|
|
|
|_, _, _| true,
|
|
|
|
);
|
2021-06-15 21:32:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
if let Some(toplevel) = window_map.find(surface) {
|
|
|
|
// send the initial configure if relevant
|
|
|
|
if let SurfaceKind::Xdg(ref toplevel) = toplevel {
|
|
|
|
let initial_configure_sent = with_states(surface, |states| {
|
|
|
|
states
|
|
|
|
.data_map
|
|
|
|
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.initial_configure_sent
|
2021-06-15 21:32:02 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
if !initial_configure_sent {
|
|
|
|
toplevel.send_configure();
|
2018-05-07 17:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 05:44:39 +00:00
|
|
|
|
|
|
|
window_map.refresh_toplevel(&toplevel);
|
2020-02-08 05:46:15 +00:00
|
|
|
|
2021-07-05 17:21:15 +00:00
|
|
|
let geometry = window_map.geometry(&toplevel).unwrap();
|
2021-06-23 07:43:53 +00:00
|
|
|
let new_location = with_states(surface, |states| {
|
|
|
|
let mut data = states
|
|
|
|
.data_map
|
2020-04-10 15:01:49 +00:00
|
|
|
.get::<RefCell<SurfaceData>>()
|
|
|
|
.unwrap()
|
|
|
|
.borrow_mut();
|
2020-02-08 05:46:15 +00:00
|
|
|
|
|
|
|
let mut new_location = None;
|
|
|
|
|
|
|
|
// If the window is being resized by top or left, its location must be adjusted
|
|
|
|
// accordingly.
|
|
|
|
match data.resize_state {
|
|
|
|
ResizeState::Resizing(resize_data)
|
|
|
|
| ResizeState::WaitingForFinalAck(resize_data, _)
|
|
|
|
| ResizeState::WaitingForCommit(resize_data) => {
|
|
|
|
let ResizeData {
|
|
|
|
edges,
|
|
|
|
initial_window_location,
|
|
|
|
initial_window_size,
|
|
|
|
} = resize_data;
|
2020-01-22 04:00:37 +00:00
|
|
|
|
2020-02-11 08:06:36 +00:00
|
|
|
if edges.intersects(ResizeEdge::TOP_LEFT) {
|
2020-02-08 05:46:15 +00:00
|
|
|
let mut location = window_map.location(&toplevel).unwrap();
|
2020-01-22 04:45:25 +00:00
|
|
|
|
2020-02-11 08:06:36 +00:00
|
|
|
if edges.intersects(ResizeEdge::LEFT) {
|
2021-07-05 17:21:15 +00:00
|
|
|
location.x =
|
|
|
|
initial_window_location.x + (initial_window_size.w - geometry.size.w);
|
2020-02-08 05:46:15 +00:00
|
|
|
}
|
2020-02-11 08:06:36 +00:00
|
|
|
if edges.intersects(ResizeEdge::TOP) {
|
2021-07-05 17:21:15 +00:00
|
|
|
location.y =
|
|
|
|
initial_window_location.y + (initial_window_size.h - geometry.size.h);
|
2020-02-08 05:46:15 +00:00
|
|
|
}
|
2020-02-02 09:42:23 +00:00
|
|
|
|
2020-02-08 05:46:15 +00:00
|
|
|
new_location = Some(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResizeState::NotResizing => (),
|
|
|
|
}
|
2020-01-22 04:45:25 +00:00
|
|
|
|
2020-02-08 05:46:15 +00:00
|
|
|
// Finish resizing.
|
|
|
|
if let ResizeState::WaitingForCommit(_) = data.resize_state {
|
|
|
|
data.resize_state = ResizeState::NotResizing;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_location
|
2021-06-23 07:43:53 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-02-08 05:46:15 +00:00
|
|
|
|
|
|
|
if let Some(location) = new_location {
|
|
|
|
window_map.set_location(&toplevel, location);
|
|
|
|
}
|
2020-02-08 05:44:39 +00:00
|
|
|
}
|
2021-06-29 15:30:48 +00:00
|
|
|
|
|
|
|
if let Some(popup) = window_map.find_popup(surface) {
|
|
|
|
let PopupKind::Xdg(ref popup) = popup;
|
|
|
|
let initial_configure_sent = with_states(surface, |states| {
|
|
|
|
states
|
|
|
|
.data_map
|
|
|
|
.get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.initial_configure_sent
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
if !initial_configure_sent {
|
2021-08-03 17:32:28 +00:00
|
|
|
// NOTE: This should never fail as the initial configure is always
|
|
|
|
// allowed.
|
|
|
|
popup.send_configure().expect("initial configure failed");
|
2021-06-29 15:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-30 16:15:15 +00:00
|
|
|
|
|
|
|
if let Some(layer) = window_map.layers.find(surface) {
|
|
|
|
// send the initial configure if relevant
|
|
|
|
let initial_configure_sent = with_states(surface, |states| {
|
|
|
|
states
|
|
|
|
.data_map
|
|
|
|
.get::<Mutex<LayerSurfaceAttributes>>()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.initial_configure_sent
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
if !initial_configure_sent {
|
|
|
|
layer.surface.send_configure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(output) = output_map.borrow().find_by_layer_surface(surface) {
|
|
|
|
window_map.layers.arange_layers(output);
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 04:00:37 +00:00
|
|
|
}
|