Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ext-session-lock protocol #203

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 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 @@ -487,6 +487,16 @@ where
}
}

// If session locked, only show session lock surfaces
if let Some(session_lock) = &state.session_lock {
elements.extend(
session_lock_elements(renderer, output, session_lock)
.into_iter()
.map(|x| WorkspaceRenderElement::from(x).into()),
);
return Ok(elements);
}

let overview = state.shell.overview_mode();
let (resize_mode, resize_indicator) = state.shell.resize_mode();
let resize_indicator = resize_indicator.map(|indicator| (resize_mode, indicator));
Expand Down Expand Up @@ -783,6 +793,30 @@ where
(layer_elements, popup_elements)
}

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_output<R, Target, OffTarget, Source>(
gpu: Option<&DrmNode>,
renderer: &mut R,
Expand Down
30 changes: 28 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,14 @@ impl State {
pattern: KeyPattern,
direction: Option<Direction>,
) {
// TODO: Detect if started from login manager or tty, and only allow
// `Terminate` if it will return to login manager.
if self.common.session_lock.is_some()
&& !matches!(action, Action::Terminate | Action::Debug)
{
return;
}
Drakulix marked this conversation as resolved.
Show resolved Hide resolved

match action {
Action::Terminate => {
self.common.should_stop = true;
Expand Down Expand Up @@ -1718,10 +1734,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