2021-04-28 22:31:49 +00:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
use std::{cell::RefCell, sync::Mutex};
|
2021-04-06 23:15:03 +00:00
|
|
|
|
|
|
|
use slog::Logger;
|
|
|
|
use smithay::{
|
2021-05-13 17:59:47 +00:00
|
|
|
backend::{
|
2021-06-24 16:37:17 +00:00
|
|
|
renderer::{buffer_type, BufferType, Frame, ImportAll, Renderer, Texture, Transform},
|
2021-05-13 17:59:47 +00:00
|
|
|
SwapBuffersError,
|
|
|
|
},
|
2021-05-27 15:34:37 +00:00
|
|
|
reexports::wayland_server::protocol::{wl_buffer, wl_surface},
|
2021-04-06 23:15:03 +00:00
|
|
|
utils::Rectangle,
|
|
|
|
wayland::{
|
2021-06-23 07:43:53 +00:00
|
|
|
compositor::{
|
|
|
|
get_role, with_states, with_surface_tree_upward, Damage, SubsurfaceCachedState,
|
|
|
|
SurfaceAttributes, TraversalAction,
|
|
|
|
},
|
|
|
|
seat::CursorImageAttributes,
|
2021-04-06 23:15:03 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
use crate::{shell::SurfaceData, window_map::WindowMap};
|
2021-04-25 21:38:48 +00:00
|
|
|
|
2021-05-13 17:59:47 +00:00
|
|
|
struct BufferTextures<T> {
|
2021-05-16 18:08:10 +00:00
|
|
|
buffer: Option<wl_buffer::WlBuffer>,
|
2021-05-13 17:59:47 +00:00
|
|
|
texture: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for BufferTextures<T> {
|
|
|
|
fn drop(&mut self) {
|
2021-05-16 18:08:10 +00:00
|
|
|
if let Some(buffer) = self.buffer.take() {
|
|
|
|
buffer.release();
|
|
|
|
}
|
2021-05-13 17:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 21:03:43 +00:00
|
|
|
pub fn draw_cursor<R, E, F, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
2021-05-23 21:03:43 +00:00
|
|
|
frame: &mut F,
|
2021-04-06 23:15:03 +00:00
|
|
|
surface: &wl_surface::WlSurface,
|
|
|
|
(x, y): (i32, i32),
|
|
|
|
log: &Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
2021-06-10 16:13:20 +00:00
|
|
|
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll,
|
2021-05-23 21:03:43 +00:00
|
|
|
F: Frame<Error = E, TextureId = T>,
|
2021-04-28 22:32:47 +00:00
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-06-23 07:43:53 +00:00
|
|
|
let ret = with_states(surface, |states| {
|
|
|
|
Some(
|
|
|
|
states
|
|
|
|
.data_map
|
|
|
|
.get::<Mutex<CursorImageAttributes>>()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.hotspot,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or(None);
|
|
|
|
let (dx, dy) = match ret {
|
|
|
|
Some(h) => h,
|
|
|
|
None => {
|
2021-04-06 23:15:03 +00:00
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Trying to display as a cursor a surface that does not have the CursorImage role."
|
|
|
|
);
|
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
};
|
2021-06-23 07:43:53 +00:00
|
|
|
draw_surface_tree(renderer, frame, surface, (x - dx, y - dy), log)
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 21:03:43 +00:00
|
|
|
fn draw_surface_tree<R, E, F, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
2021-05-23 21:03:43 +00:00
|
|
|
frame: &mut F,
|
2021-04-06 23:15:03 +00:00
|
|
|
root: &wl_surface::WlSurface,
|
|
|
|
location: (i32, i32),
|
|
|
|
log: &Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
2021-06-10 16:13:20 +00:00
|
|
|
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll,
|
2021-05-23 21:03:43 +00:00
|
|
|
F: Frame<Error = E, TextureId = T>,
|
2021-04-28 22:32:47 +00:00
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-04-28 22:02:17 +00:00
|
|
|
let mut result = Ok(());
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
with_surface_tree_upward(
|
2021-04-06 23:15:03 +00:00
|
|
|
root,
|
2021-05-23 21:03:43 +00:00
|
|
|
location,
|
2021-06-23 07:43:53 +00:00
|
|
|
|_surface, states, &(mut x, mut y)| {
|
2021-04-06 23:15:03 +00:00
|
|
|
// Pull a new buffer if available
|
2021-06-23 07:43:53 +00:00
|
|
|
if let Some(data) = states.data_map.get::<RefCell<SurfaceData>>() {
|
2021-04-06 23:15:03 +00:00
|
|
|
let mut data = data.borrow_mut();
|
2021-06-23 07:43:53 +00:00
|
|
|
let attributes = states.cached_state.current::<SurfaceAttributes>();
|
2021-04-06 23:15:03 +00:00
|
|
|
if data.texture.is_none() {
|
2021-06-23 07:43:53 +00:00
|
|
|
if let Some(buffer) = data.buffer.take() {
|
2021-06-10 16:13:20 +00:00
|
|
|
let damage = attributes
|
|
|
|
.damage
|
|
|
|
.iter()
|
|
|
|
.map(|dmg| match dmg {
|
|
|
|
Damage::Buffer(rect) => *rect,
|
|
|
|
// TODO also apply transformations
|
|
|
|
Damage::Surface(rect) => rect.scale(attributes.buffer_scale),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-06-23 07:43:53 +00:00
|
|
|
match renderer.import_buffer(&buffer, Some(states), &damage) {
|
2021-06-10 16:13:20 +00:00
|
|
|
Some(Ok(m)) => {
|
2021-06-23 07:43:53 +00:00
|
|
|
let texture_buffer = if let Some(BufferType::Shm) = buffer_type(&buffer) {
|
2021-06-10 16:13:20 +00:00
|
|
|
buffer.release();
|
2021-06-23 07:43:53 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(buffer)
|
|
|
|
};
|
2021-06-10 16:13:20 +00:00
|
|
|
data.texture = Some(Box::new(BufferTextures {
|
2021-06-23 07:43:53 +00:00
|
|
|
buffer: texture_buffer,
|
2021-06-10 16:13:20 +00:00
|
|
|
texture: m,
|
2021-06-23 07:43:53 +00:00
|
|
|
}))
|
2021-06-09 20:12:35 +00:00
|
|
|
}
|
2021-06-10 16:13:20 +00:00
|
|
|
Some(Err(err)) => {
|
|
|
|
warn!(log, "Error loading buffer: {:?}", err);
|
|
|
|
buffer.release();
|
2021-06-09 20:12:35 +00:00
|
|
|
}
|
2021-06-10 16:13:20 +00:00
|
|
|
None => {
|
2021-06-05 17:58:51 +00:00
|
|
|
error!(log, "Unknown buffer format for: {:?}", buffer);
|
2021-06-09 19:02:33 +00:00
|
|
|
buffer.release();
|
2021-06-05 17:58:51 +00:00
|
|
|
}
|
2021-06-10 16:13:20 +00:00
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now, should we be drawn ?
|
2021-05-26 17:12:45 +00:00
|
|
|
if data.texture.is_some() {
|
|
|
|
// if yes, also process the children
|
2021-06-23 07:43:53 +00:00
|
|
|
if states.role == Some("subsurface") {
|
|
|
|
let current = states.cached_state.current::<SubsurfaceCachedState>();
|
|
|
|
x += current.location.0;
|
|
|
|
y += current.location.1;
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
TraversalAction::DoChildren((x, y))
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we are not displayed, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
|
|
|
}
|
|
|
|
},
|
2021-06-23 07:43:53 +00:00
|
|
|
|_surface, states, &(mut x, mut y)| {
|
|
|
|
if let Some(ref data) = states.data_map.get::<RefCell<SurfaceData>>() {
|
2021-04-06 23:15:03 +00:00
|
|
|
let mut data = data.borrow_mut();
|
2021-05-13 17:59:47 +00:00
|
|
|
if let Some(texture) = data
|
2021-04-28 22:32:47 +00:00
|
|
|
.texture
|
|
|
|
.as_mut()
|
|
|
|
.and_then(|x| x.downcast_mut::<BufferTextures<T>>())
|
|
|
|
{
|
2021-04-06 23:15:03 +00:00
|
|
|
// we need to re-extract the subsurface offset, as the previous closure
|
|
|
|
// only passes it to our children
|
2021-06-23 07:43:53 +00:00
|
|
|
if states.role == Some("subsurface") {
|
|
|
|
let current = states.cached_state.current::<SubsurfaceCachedState>();
|
|
|
|
x += current.location.0;
|
|
|
|
y += current.location.1;
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
2021-05-23 21:03:43 +00:00
|
|
|
if let Err(err) = frame.render_texture_at(
|
2021-05-15 16:17:43 +00:00
|
|
|
&texture.texture,
|
|
|
|
(x, y),
|
|
|
|
Transform::Normal, /* TODO */
|
|
|
|
1.0,
|
|
|
|
) {
|
2021-04-28 22:02:17 +00:00
|
|
|
result = Err(err.into());
|
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-06-23 07:43:53 +00:00
|
|
|
|_, _, _| true,
|
2021-04-06 23:15:03 +00:00
|
|
|
);
|
2021-04-28 22:02:17 +00:00
|
|
|
|
|
|
|
result
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 21:03:43 +00:00
|
|
|
pub fn draw_windows<R, E, F, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
2021-05-23 21:03:43 +00:00
|
|
|
frame: &mut F,
|
2021-06-23 07:43:53 +00:00
|
|
|
window_map: &WindowMap,
|
2021-06-21 19:29:40 +00:00
|
|
|
output_rect: Rectangle,
|
2021-04-06 23:15:03 +00:00
|
|
|
log: &::slog::Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
2021-06-10 16:13:20 +00:00
|
|
|
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll,
|
2021-05-23 21:03:43 +00:00
|
|
|
F: Frame<Error = E, TextureId = T>,
|
2021-04-28 22:32:47 +00:00
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-04-28 22:02:17 +00:00
|
|
|
let mut result = Ok(());
|
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
// redraw the frame, in a simple but inneficient way
|
2021-04-28 22:32:47 +00:00
|
|
|
window_map.with_windows_from_bottom_to_top(|toplevel_surface, mut initial_place, bounding_box| {
|
|
|
|
// skip windows that do not overlap with a given output
|
2021-06-21 19:29:40 +00:00
|
|
|
if !output_rect.overlaps(bounding_box) {
|
|
|
|
return;
|
2021-04-28 22:32:47 +00:00
|
|
|
}
|
2021-06-21 19:29:40 +00:00
|
|
|
initial_place.0 -= output_rect.x;
|
2021-04-28 22:32:47 +00:00
|
|
|
if let Some(wl_surface) = toplevel_surface.get_surface() {
|
|
|
|
// this surface is a root of a subsurface tree that needs to be drawn
|
2021-06-23 07:43:53 +00:00
|
|
|
if let Err(err) = draw_surface_tree(renderer, frame, &wl_surface, initial_place, log) {
|
2021-04-28 22:32:47 +00:00
|
|
|
result = Err(err);
|
2021-04-28 22:02:17 +00:00
|
|
|
}
|
2021-06-29 15:30:48 +00:00
|
|
|
// furthermore, draw its popups
|
2021-07-01 20:59:16 +00:00
|
|
|
let toplevel_geometry_offset = window_map
|
|
|
|
.geometry(toplevel_surface)
|
|
|
|
.map(|g| (g.x, g.y))
|
|
|
|
.unwrap_or_default();
|
2021-06-29 15:30:48 +00:00
|
|
|
window_map.with_child_popups(&wl_surface, |popup| {
|
|
|
|
let location = popup.location();
|
2021-07-01 20:59:16 +00:00
|
|
|
let draw_location = (
|
|
|
|
initial_place.0 + location.0 + toplevel_geometry_offset.0,
|
|
|
|
initial_place.1 + location.1 + toplevel_geometry_offset.1,
|
|
|
|
);
|
2021-06-29 15:30:48 +00:00
|
|
|
if let Some(wl_surface) = popup.get_surface() {
|
|
|
|
if let Err(err) = draw_surface_tree(renderer, frame, &wl_surface, draw_location, log) {
|
|
|
|
result = Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-04-28 22:32:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 22:02:17 +00:00
|
|
|
result
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 21:03:43 +00:00
|
|
|
pub fn draw_dnd_icon<R, E, F, T>(
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer: &mut R,
|
2021-05-23 21:03:43 +00:00
|
|
|
frame: &mut F,
|
2021-04-06 23:15:03 +00:00
|
|
|
surface: &wl_surface::WlSurface,
|
|
|
|
(x, y): (i32, i32),
|
|
|
|
log: &::slog::Logger,
|
2021-04-28 22:02:17 +00:00
|
|
|
) -> Result<(), SwapBuffersError>
|
2021-04-28 22:32:47 +00:00
|
|
|
where
|
2021-06-10 16:13:20 +00:00
|
|
|
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportAll,
|
2021-05-23 21:03:43 +00:00
|
|
|
F: Frame<Error = E, TextureId = T>,
|
2021-04-28 22:32:47 +00:00
|
|
|
E: std::error::Error + Into<SwapBuffersError>,
|
|
|
|
T: Texture + 'static,
|
2021-04-06 23:15:03 +00:00
|
|
|
{
|
2021-06-23 07:43:53 +00:00
|
|
|
if get_role(surface) != Some("dnd_icon") {
|
2021-04-06 23:15:03 +00:00
|
|
|
warn!(
|
|
|
|
log,
|
|
|
|
"Trying to display as a dnd icon a surface that does not have the DndIcon role."
|
|
|
|
);
|
|
|
|
}
|
2021-06-23 07:43:53 +00:00
|
|
|
draw_surface_tree(renderer, frame, surface, (x, y), log)
|
2021-04-06 23:15:03 +00:00
|
|
|
}
|