Skip to content

Commit

Permalink
Add ext-session-lock protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Oct 26, 2023
1 parent a6de6b6 commit 6b2066f
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 88 deletions.
37 changes: 22 additions & 15 deletions src/backend/kms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(feature = "debug")]
use crate::backend::render::element::AsGlowRenderer;
use crate::{
backend::render::{workspace_elements, CLEAR_COLOR},
backend::render::{session_lock_elements, workspace_elements, CLEAR_COLOR},
config::OutputConfig,
shell::Shell,
state::{BackendData, ClientState, Common, Fps, SurfaceDmabufFeedback},
Expand Down Expand Up @@ -1223,20 +1223,27 @@ impl Surface {
.map(|((w, start), idx)| (w.handle, idx, start));
let workspace = (workspace.handle, idx);

let elements = workspace_elements(
Some(&render_node),
&mut renderer,
state,
&self.output,
previous_workspace,
workspace,
CursorMode::All,
&mut Some(&mut self.fps),
false,
)
.map_err(|err| {
anyhow::format_err!("Failed to accumulate elements for rendering: {:?}", err)
})?;
let elements = if let Some(session_lock) = &state.session_lock {
session_lock_elements(&mut renderer, &self.output, session_lock)
.into_iter()
.map(|x| crate::shell::WorkspaceRenderElement::from(x).into())
.collect()
} else {
workspace_elements(
Some(&render_node),
&mut renderer,
state,
&self.output,
previous_workspace,
workspace,
CursorMode::All,
&mut Some(&mut self.fps),
false,
)
.map_err(|err| {
anyhow::format_err!("Failed to accumulate elements for rendering: {:?}", err)
})?
};
self.fps.elements();

let res = compositor.render_frame::<_, _, GlesTexture>(
Expand Down
49 changes: 47 additions & 2 deletions src/backend/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
focus::target::WindowGroup, grabs::SeatMoveGrabState, layout::tiling::ANIMATION_DURATION,
CosmicMapped, CosmicMappedRenderElement, OverviewMode, Trigger, WorkspaceRenderElement,
},
state::{Common, Fps},
state::{Common, Fps, SessionLock},
utils::prelude::*,
wayland::{
handlers::{
Expand All @@ -42,7 +42,7 @@ use smithay::{
buffer_dimensions,
damage::{Error as RenderError, OutputDamageTracker, RenderOutputResult},
element::{
surface::render_elements_from_surface_tree,
surface::{render_elements_from_surface_tree, WaylandSurfaceRenderElement},
utils::{Relocate, RelocateRenderElement},
Element, Id, Kind, RenderElement,
},
Expand Down Expand Up @@ -812,6 +812,10 @@ where
WorkspaceRenderElement<R>: RenderElement<R>,
Source: Clone,
{
if let Some(session_lock) = &state.session_lock {
return render_session_lock(renderer, target, damage_tracker, age, output, session_lock);
}

let (previous_workspace, workspace) = state.shell.workspaces.active(output);
let (previous_idx, idx) = state.shell.workspaces.active_num(output);
let previous_workspace = previous_workspace
Expand All @@ -838,6 +842,47 @@ where
result
}

pub fn render_session_lock<R, Target>(
renderer: &mut R,
target: Target,
damage_tracker: &mut OutputDamageTracker,
age: usize,
output: &Output,
session_lock: &SessionLock,
) -> Result<RenderOutputResult, RenderError<R>>
where
R: Renderer + ImportAll + Bind<Target>,
<R as Renderer>::TextureId: Clone + 'static,
{
let elements = session_lock_elements(renderer, output, session_lock);
renderer.bind(target).map_err(RenderError::Rendering)?;
damage_tracker.render_output(renderer, age, &elements, CLEAR_COLOR)
}

pub fn session_lock_elements<R>(
renderer: &mut R,
output: &Output,
session_lock: &SessionLock,
) -> Vec<WaylandSurfaceRenderElement<R>>
where
R: Renderer + ImportAll,
<R as Renderer>::TextureId: Clone + 'static,
{
if let Some(surface) = session_lock.surfaces.get(output) {
let scale = Scale::from(output.current_scale().fractional_scale());
render_elements_from_surface_tree(
renderer,
surface.wl_surface(),
(0, 0),
scale,
1.0,
Kind::Unspecified,
)
} else {
Vec::new()
}
}

pub fn render_workspace<R, Target, OffTarget, Source>(
gpu: Option<&DrmNode>,
renderer: &mut R,
Expand Down
26 changes: 24 additions & 2 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
Direction, FocusResult, MoveResult, OverviewMode, ResizeDirection, ResizeMode, Trigger,
Workspace,
},
state::Common,
state::{Common, SessionLock},
utils::prelude::*,
wayland::{handlers::screencopy::ScreencopySessions, protocols::screencopy::Session},
};
Expand Down Expand Up @@ -565,6 +565,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0.clone(),
workspace,
self.common.session_lock.as_ref(),
)
.map(|(target, pos)| (target, pos.as_logical()));

Expand Down Expand Up @@ -646,6 +647,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0,
workspace,
self.common.session_lock.as_ref(),
)
.map(|(target, pos)| (target, pos.as_logical()));

Expand Down Expand Up @@ -776,6 +778,7 @@ impl State {
&self.common.shell.override_redirect_windows,
overview.0,
workspace,
self.common.session_lock.as_ref(),
)
.map(|(target, pos)| (target, pos.as_logical()));

Expand Down Expand Up @@ -852,7 +855,12 @@ impl State {
let workspace = self.common.shell.active_space_mut(&output);
let mut under = None;

if let Some(window) = workspace.get_fullscreen() {
if let Some(session_lock) = self.common.session_lock.as_ref() {
under = session_lock
.surfaces
.get(&output)
.map(|lock| lock.clone().into());
} else if let Some(window) = workspace.get_fullscreen() {
let layers = layer_map_for_output(&output);
if let Some(layer) =
layers.layer_under(WlrLayer::Overlay, relative_pos.as_logical())
Expand Down Expand Up @@ -1153,6 +1161,10 @@ impl State {
pattern: KeyPattern,
direction: Option<Direction>,
) {
if self.common.session_lock.is_some() {
return;
}

match action {
Action::Terminate => {
self.common.should_stop = true;
Expand Down Expand Up @@ -1718,10 +1730,20 @@ impl State {
override_redirect_windows: &[X11Surface],
overview: OverviewMode,
workspace: &mut Workspace,
session_lock: Option<&SessionLock>,
) -> Option<(PointerFocusTarget, Point<i32, Global>)> {
let relative_pos = global_pos.to_local(output);
let output_geo = output.geometry();

if let Some(session_lock) = session_lock {
return session_lock.surfaces.get(output).map(|surface| {
(
PointerFocusTarget::LockSurface(surface.clone()),
output_geo.loc,
)
});
}

if let Some(window) = workspace.get_fullscreen() {
let layers = layer_map_for_output(output);
if let Some(layer) = layers.layer_under(WlrLayer::Overlay, relative_pos.as_logical()) {
Expand Down
143 changes: 76 additions & 67 deletions src/shell/focus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use indexmap::IndexSet;
use smithay::{
desktop::{layer_map_for_output, PopupUngrabStrategy},
input::Seat,
output::Output,
utils::{IsAlive, Serial, SERIAL_COUNTER},
wayland::seat::WaylandFocus,
};
Expand Down Expand Up @@ -217,55 +218,11 @@ impl Common {

if let Some(target) = last_known_focus {
if target.alive() {
match target {
KeyboardFocusTarget::Element(mapped) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
if focus_stack.last().map(|m| m == &mapped).unwrap_or(false)
&& workspace.get_fullscreen().is_none()
{
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
}
KeyboardFocusTarget::LayerSurface(layer) => {
if layer_map_for_output(&output).layers().any(|l| l == &layer) {
continue; // Focus is valid
}
}
KeyboardFocusTarget::Group(WindowGroup { node, .. }) => {
if state
.common
.shell
.workspaces
.active(&output)
.1
.tiling_layer
.has_node(&node)
{
continue; // Focus is valid,
}
}
KeyboardFocusTarget::Fullscreen(window) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);

if focus_stack
.last()
.map(|m| m.has_active_window(&window))
.unwrap_or(false)
&& workspace.get_fullscreen().is_some()
{
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
}
KeyboardFocusTarget::Popup(_) => {
continue; // Focus is valid
}
};
if focus_target_is_valid(state, &seat, &output, target) {
continue; // Focus is valid
} else {
trace!("Wrong Window, focus fixup");
}
} else {
if let KeyboardFocusTarget::Popup(_) = target {
if let Some(popup_grab) = seat
Expand Down Expand Up @@ -319,24 +276,7 @@ impl Common {
}

// update keyboard focus
let target = state
.common
.shell
.active_space(&output)
.get_fullscreen()
.cloned()
.map(KeyboardFocusTarget::Fullscreen)
.or_else(|| {
state
.common
.shell
.active_space(&output)
.focus_stack
.get(&seat)
.last()
.cloned()
.map(KeyboardFocusTarget::from)
});
let target = update_focus_target(state, &seat, &output);
if let Some(keyboard) = seat.get_keyboard() {
debug!("Restoring focus to {:?}", target.as_ref());
keyboard.set_focus(state, target.clone(), SERIAL_COUNTER.next_serial());
Expand All @@ -349,3 +289,72 @@ impl Common {
state.common.shell.update_active(seats.iter())
}
}

fn focus_target_is_valid(
state: &mut State,
seat: &Seat<State>,
output: &Output,
target: KeyboardFocusTarget,
) -> bool {
if state.common.session_lock.is_some() {
return matches!(target, KeyboardFocusTarget::LockSurface(_));
}

match target {
KeyboardFocusTarget::Element(mapped) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);
focus_stack.last().map(|m| m == &mapped).unwrap_or(false)
&& workspace.get_fullscreen().is_none()
}
KeyboardFocusTarget::LayerSurface(layer) => {
layer_map_for_output(&output).layers().any(|l| l == &layer)
}
KeyboardFocusTarget::Group(WindowGroup { node, .. }) => state
.common
.shell
.workspaces
.active(&output)
.1
.tiling_layer
.has_node(&node),
KeyboardFocusTarget::Fullscreen(window) => {
let workspace = state.common.shell.active_space(&output);
let focus_stack = workspace.focus_stack.get(&seat);

focus_stack
.last()
.map(|m| m.has_active_window(&window))
.unwrap_or(false)
&& workspace.get_fullscreen().is_some()
}
KeyboardFocusTarget::Popup(_) => true,
KeyboardFocusTarget::LockSurface(_) => false,
}
}

fn update_focus_target(
state: &mut State,
seat: &Seat<State>,
output: &Output,
) -> Option<KeyboardFocusTarget> {
if let Some(session_lock) = &state.common.session_lock {
session_lock
.surfaces
.get(output)
.cloned()
.map(KeyboardFocusTarget::from)
} else if let Some(surface) = state.common.shell.active_space(&output).get_fullscreen() {
Some(KeyboardFocusTarget::Fullscreen(surface.clone()))
} else {
state
.common
.shell
.active_space(&output)
.focus_stack
.get(&seat)
.last()
.cloned()
.map(KeyboardFocusTarget::from)
}
}
Loading

0 comments on commit 6b2066f

Please sign in to comment.