1
0
Fork 0
forked from wry/wry

all: implement screen locking

This commit is contained in:
Julian Orth 2022-05-19 23:39:11 +02:00
parent 9db389835d
commit d42add4d18
24 changed files with 618 additions and 6 deletions

View file

@ -0,0 +1,129 @@
use {
crate::{
client::{Client, ClientError},
globals::{Global, GlobalName},
ifs::ext_session_lock_v1::ExtSessionLockV1,
leaks::Tracker,
object::Object,
utils::buffd::{MsgParser, MsgParserError},
wire::{ext_session_lock_manager_v1::*, ExtSessionLockManagerV1Id},
},
std::{cell::Cell, rc::Rc},
thiserror::Error,
};
pub struct ExtSessionLockManagerV1Global {
pub name: GlobalName,
}
impl ExtSessionLockManagerV1Global {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
fn bind_(
self: Rc<Self>,
id: ExtSessionLockManagerV1Id,
client: &Rc<Client>,
_version: u32,
) -> Result<(), ExtSessionLockManagerV1Error> {
let obj = Rc::new(ExtSessionLockManagerV1 {
id,
client: client.clone(),
tracker: Default::default(),
});
track!(client, obj);
client.add_client_obj(&obj)?;
Ok(())
}
}
pub struct ExtSessionLockManagerV1 {
pub id: ExtSessionLockManagerV1Id,
pub client: Rc<Client>,
pub tracker: Tracker<Self>,
}
impl ExtSessionLockManagerV1 {
fn destroy(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockManagerV1Error> {
let _req: Destroy = self.client.parse(self, msg)?;
self.client.remove_obj(self)?;
Ok(())
}
fn lock(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockManagerV1Error> {
let req: Lock = self.client.parse(self, msg)?;
let did_lock = self.client.state.lock.locked.get() == false;
let new = Rc::new(ExtSessionLockV1 {
id: req.id,
client: self.client.clone(),
tracker: Default::default(),
did_lock,
finished: Cell::new(false),
});
track!(new.client, new);
self.client.add_client_obj(&new)?;
if did_lock {
log::info!("Client {} locks the screen", self.client.id);
let state = &self.client.state;
for seat in state.globals.seats.lock().values() {
seat.prepare_for_lock();
}
state.lock.locked.set(true);
state.lock.lock.set(Some(new.clone()));
state.tree_changed();
state.damage();
new.send_locked();
} else {
new.finish();
}
Ok(())
}
}
global_base!(
ExtSessionLockManagerV1Global,
ExtSessionLockManagerV1,
ExtSessionLockManagerV1Error
);
impl Global for ExtSessionLockManagerV1Global {
fn singleton(&self) -> bool {
true
}
fn version(&self) -> u32 {
1
}
fn secure(&self) -> bool {
true
}
}
simple_add_global!(ExtSessionLockManagerV1Global);
object_base! {
ExtSessionLockManagerV1;
DESTROY => destroy,
LOCK => lock,
}
impl Object for ExtSessionLockManagerV1 {
fn num_requests(&self) -> u32 {
LOCK + 1
}
}
simple_add_obj!(ExtSessionLockManagerV1);
#[derive(Debug, Error)]
pub enum ExtSessionLockManagerV1Error {
#[error("Parsing failed")]
MsgParserError(#[source] Box<MsgParserError>),
#[error(transparent)]
ClientError(Box<ClientError>),
}
efrom!(ExtSessionLockManagerV1Error, MsgParserError);
efrom!(ExtSessionLockManagerV1Error, ClientError);

View file

@ -0,0 +1,136 @@
use {
crate::{
client::{Client, ClientError},
ifs::wl_surface::ext_session_lock_surface_v1::{
ExtSessionLockSurfaceV1, ExtSessionLockSurfaceV1Error,
},
leaks::Tracker,
object::Object,
utils::buffd::{MsgParser, MsgParserError},
wire::{ext_session_lock_v1::*, ExtSessionLockV1Id},
},
std::{cell::Cell, rc::Rc},
thiserror::Error,
};
pub struct ExtSessionLockV1 {
pub id: ExtSessionLockV1Id,
pub client: Rc<Client>,
pub tracker: Tracker<Self>,
pub did_lock: bool,
pub finished: Cell<bool>,
}
impl ExtSessionLockV1 {
pub fn send_locked(&self) {
self.client.event(Locked { self_id: self.id })
}
fn send_finished(&self) {
self.client.event(Finished { self_id: self.id })
}
pub fn finish(&self) {
self.send_finished();
self.finished.set(true);
}
fn destroy(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockV1Error> {
let _req: Destroy = self.client.parse(self, msg)?;
if !self.finished.get() {
self.client.state.lock.lock.take();
}
self.client.remove_obj(self)?;
Ok(())
}
fn get_lock_surface(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockV1Error> {
let req: GetLockSurface = self.client.parse(self, msg)?;
let output = self.client.lookup(req.output)?;
let surface = self.client.lookup(req.surface)?;
let new = Rc::new(ExtSessionLockSurfaceV1 {
id: req.id,
node_id: self.client.state.node_ids.next(),
client: self.client.clone(),
surface,
tracker: Default::default(),
serial: Default::default(),
output: output.global.node.get(),
seat_state: Default::default(),
});
track!(new.client, new);
new.install()?;
self.client.add_client_obj(&new)?;
if !output.global.destroyed.get() && !self.finished.get() {
if let Some(node) = output.global.node.get() {
if node.lock_surface.get().is_some() {
return Err(ExtSessionLockV1Error::OutputAlreadyLocked);
}
node.lock_surface.set(Some(new.clone()));
let pos = output.global.pos.get();
new.change_extents(pos);
self.client.state.tree_changed();
}
}
Ok(())
}
fn unlock_and_destroy(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockV1Error> {
let _req: UnlockAndDestroy = self.client.parse(self, msg)?;
if !self.did_lock {
return Err(ExtSessionLockV1Error::NeverLocked);
}
if !self.finished.get() {
let state = &self.client.state;
state.lock.locked.set(false);
state.lock.lock.take();
for output in state.outputs.lock().values() {
if let Some(surface) = output.node.lock_surface.take() {
surface.destroy_node();
}
}
state.tree_changed();
state.damage();
}
self.client.remove_obj(self)?;
Ok(())
}
}
object_base! {
ExtSessionLockV1;
DESTROY => destroy,
GET_LOCK_SURFACE => get_lock_surface,
UNLOCK_AND_DESTROY => unlock_and_destroy,
}
impl Object for ExtSessionLockV1 {
fn num_requests(&self) -> u32 {
UNLOCK_AND_DESTROY + 1
}
fn break_loops(&self) {
if !self.finished.get() {
self.client.state.lock.lock.take();
}
}
}
simple_add_obj!(ExtSessionLockV1);
#[derive(Debug, Error)]
pub enum ExtSessionLockV1Error {
#[error("Parsing failed")]
MsgParserError(#[source] Box<MsgParserError>),
#[error(transparent)]
ClientError(Box<ClientError>),
#[error("The lock was not accepted")]
NeverLocked,
#[error("The output already has a lock surface attached")]
OutputAlreadyLocked,
#[error(transparent)]
ExtSessionLockSurfaceV1Error(#[from] ExtSessionLockSurfaceV1Error),
}
efrom!(ExtSessionLockV1Error, MsgParserError);
efrom!(ExtSessionLockV1Error, ClientError);

View file

@ -174,6 +174,25 @@ impl JayCompositor {
self.client.symmetric_delete.set(true);
Ok(())
}
fn unlock(&self, parser: MsgParser<'_, '_>) -> Result<(), JayCompositorError> {
let _req: Unlock = self.client.parse(self, parser)?;
let state = &self.client.state;
if state.lock.locked.replace(false) {
if let Some(lock) = state.lock.lock.take() {
lock.finish();
}
for output in state.outputs.lock().values() {
if let Some(surface) = output.node.lock_surface.take() {
surface.destroy_node();
}
}
state.tree_changed();
state.damage();
}
self.client.symmetric_delete.set(true);
Ok(())
}
}
object_base! {
@ -187,11 +206,12 @@ object_base! {
GET_IDLE => get_idle,
GET_CLIENT_ID => get_client_id,
ENABLE_SYMMETRIC_DELETE => enable_symmetric_delete,
UNLOCK => unlock,
}
impl Object for JayCompositor {
fn num_requests(&self) -> u32 {
GET_CLIENT_ID + 1
UNLOCK + 1
}
}

View file

@ -79,6 +79,7 @@ pub struct WlOutputGlobal {
pub bindings: RefCell<AHashMap<ClientId, AHashMap<WlOutputId, Rc<WlOutput>>>>,
pub unused_captures: LinkedList<Rc<ZwlrScreencopyFrameV1>>,
pub pending_captures: LinkedList<Rc<ZwlrScreencopyFrameV1>>,
pub destroyed: Cell<bool>,
}
impl WlOutputGlobal {
@ -112,6 +113,7 @@ impl WlOutputGlobal {
bindings: Default::default(),
unused_captures: Default::default(),
pending_captures: Default::default(),
destroyed: Cell::new(false),
}
}

View file

@ -118,6 +118,7 @@ pub struct WlSeatGlobal {
extents_start_pos: Cell<(i32, i32)>,
pos: Cell<(Fixed, Fixed)>,
pointer_stack: RefCell<Vec<Rc<dyn Node>>>,
pointer_stack_modified: Cell<bool>,
found_tree: RefCell<Vec<FoundNode>>,
keyboard_node: CloneCell<Rc<dyn Node>>,
pressed_keys: RefCell<AHashSet<u32>>,
@ -164,6 +165,7 @@ impl WlSeatGlobal {
extents_start_pos: Cell::new((0, 0)),
pos: Cell::new((Fixed(0), Fixed(0))),
pointer_stack: RefCell::new(vec![]),
pointer_stack_modified: Cell::new(false),
found_tree: RefCell::new(vec![]),
keyboard_node: CloneCell::new(state.root.clone()),
pressed_keys: RefCell::new(Default::default()),
@ -330,6 +332,11 @@ impl WlSeatGlobal {
}
}
pub fn prepare_for_lock(self: &Rc<Self>) {
self.pointer_owner.revert_to_default(self);
self.kb_owner.ungrab(self);
}
pub fn set_position(&self, x: i32, y: i32) {
self.pos.set((Fixed::from_int(x), Fixed::from_int(y)));
self.trigger_tree_changed();

View file

@ -134,6 +134,7 @@ impl NodeSeatState {
last.node_seat_state().leave(&seat);
last.node_on_leave(&seat);
}
seat.pointer_stack_modified.set(true);
seat.state.tree_changed();
}
self.release_kb_focus2(focus_last);
@ -273,7 +274,7 @@ impl WlSeatGlobal {
let new_mods;
{
let mut kb_state = self.kb_state.borrow_mut();
if state == wl_keyboard::PRESSED {
if !self.state.lock.locked.get() && state == wl_keyboard::PRESSED {
let old_mods = kb_state.mods();
let keysyms = kb_state.unmodified_keysyms(key);
for &sym in keysyms {

View file

@ -201,7 +201,8 @@ impl PointerOwner for DefaultPointerOwner {
break;
}
}
if (stack.len(), found_tree.len()) == (divergence, divergence) {
let psm = seat.pointer_stack_modified.replace(false);
if !psm && (stack.len(), found_tree.len()) == (divergence, divergence) {
if let Some(node) = found_tree.last() {
node.node.clone().node_on_pointer_motion(
seat,

View file

@ -1,4 +1,5 @@
pub mod cursor;
pub mod ext_session_lock_surface_v1;
pub mod wl_subsurface;
pub mod xdg_surface;
pub mod xwindow;
@ -64,6 +65,7 @@ pub enum SurfaceRole {
DndIcon,
ZwlrLayerSurface,
XSurface,
ExtSessionLockSurface,
}
impl SurfaceRole {
@ -76,6 +78,7 @@ impl SurfaceRole {
SurfaceRole::DndIcon => "dnd_icon",
SurfaceRole::ZwlrLayerSurface => "zwlr_layer_surface",
SurfaceRole::XSurface => "xwayland surface",
SurfaceRole::ExtSessionLockSurface => "ext_session_lock_surface",
}
}
}

View file

@ -0,0 +1,163 @@
use {
crate::{
client::{Client, ClientError},
fixed::Fixed,
ifs::{
wl_seat::{NodeSeatState, WlSeatGlobal},
wl_surface::{SurfaceExt, SurfaceRole, WlSurface, WlSurfaceError},
},
leaks::Tracker,
object::Object,
rect::Rect,
tree::{FindTreeResult, FoundNode, Node, NodeId, NodeVisitor, OutputNode},
utils::{
buffd::{MsgParser, MsgParserError},
numcell::NumCell,
},
wire::{ext_session_lock_surface_v1::*, ExtSessionLockSurfaceV1Id, WlSurfaceId},
},
std::rc::Rc,
thiserror::Error,
};
pub struct ExtSessionLockSurfaceV1 {
pub id: ExtSessionLockSurfaceV1Id,
pub node_id: ExtSessionLockSurfaceV1NodeId,
pub client: Rc<Client>,
pub surface: Rc<WlSurface>,
pub tracker: Tracker<Self>,
pub serial: NumCell<u32>,
pub output: Option<Rc<OutputNode>>,
pub seat_state: NodeSeatState,
}
impl ExtSessionLockSurfaceV1 {
pub fn install(self: &Rc<Self>) -> Result<(), ExtSessionLockSurfaceV1Error> {
self.surface.set_role(SurfaceRole::ExtSessionLockSurface)?;
if self.surface.ext.get().is_some() {
return Err(ExtSessionLockSurfaceV1Error::AlreadyAttached(
self.surface.id,
));
}
self.surface.ext.set(self.clone());
Ok(())
}
pub fn change_extents(&self, rect: Rect) {
self.send_configure(rect.width(), rect.height());
self.surface.set_absolute_position(rect.x1(), rect.x2());
}
fn send_configure(&self, width: i32, height: i32) {
self.client.event(Configure {
self_id: self.id,
serial: self.serial.fetch_add(1),
width: width as _,
height: height as _,
});
}
fn destroy(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockSurfaceV1Error> {
let _req: Destroy = self.client.parse(self, msg)?;
self.destroy_node();
self.surface.unset_ext();
self.client.remove_obj(self)?;
Ok(())
}
fn ack_configure(&self, msg: MsgParser<'_, '_>) -> Result<(), ExtSessionLockSurfaceV1Error> {
let _req: AckConfigure = self.client.parse(self, msg)?;
Ok(())
}
pub fn destroy_node(&self) {
if let Some(output) = &self.output {
if let Some(ls) = output.lock_surface.get() {
if ls.node_id == self.node_id {
output.lock_surface.take();
self.client.state.tree_changed();
}
}
}
self.surface.destroy_node();
self.seat_state.destroy_node(self);
}
}
impl SurfaceExt for ExtSessionLockSurfaceV1 {
fn extents_changed(&self) {
self.client.state.tree_changed();
}
fn focus_node(&self) -> Option<Rc<dyn Node>> {
Some(self.surface.clone())
}
}
tree_id!(ExtSessionLockSurfaceV1NodeId);
impl Node for ExtSessionLockSurfaceV1 {
fn node_id(&self) -> NodeId {
self.node_id.into()
}
fn node_seat_state(&self) -> &NodeSeatState {
&self.seat_state
}
fn node_visit(self: Rc<Self>, visitor: &mut dyn NodeVisitor) {
visitor.visit_lock_surface(&self);
}
fn node_visit_children(&self, visitor: &mut dyn NodeVisitor) {
visitor.visit_surface(&self.surface);
}
fn node_visible(&self) -> bool {
true
}
fn node_absolute_position(&self) -> Rect {
self.surface.node_absolute_position()
}
fn node_find_tree_at(&self, x: i32, y: i32, tree: &mut Vec<FoundNode>) -> FindTreeResult {
self.surface.find_tree_at_(x, y, tree)
}
fn node_on_pointer_enter(self: Rc<Self>, seat: &Rc<WlSeatGlobal>, _x: Fixed, _y: Fixed) {
seat.focus_node(self.surface.clone());
}
}
object_base! {
ExtSessionLockSurfaceV1;
DESTROY => destroy,
ACK_CONFIGURE => ack_configure,
}
impl Object for ExtSessionLockSurfaceV1 {
fn num_requests(&self) -> u32 {
ACK_CONFIGURE + 1
}
fn break_loops(&self) {
self.destroy_node();
}
}
simple_add_obj!(ExtSessionLockSurfaceV1);
#[derive(Debug, Error)]
pub enum ExtSessionLockSurfaceV1Error {
#[error("Parsing failed")]
MsgParserError(#[source] Box<MsgParserError>),
#[error(transparent)]
ClientError(Box<ClientError>),
#[error(transparent)]
WlSurfaceError(#[from] WlSurfaceError),
#[error("Surface {0} cannot be turned into an ext_session_lock_surface because it already has an attached ext_session_lock_surface")]
AlreadyAttached(WlSurfaceId),
}
efrom!(ExtSessionLockSurfaceV1Error, MsgParserError);
efrom!(ExtSessionLockSurfaceV1Error, ClientError);

View file

@ -270,7 +270,7 @@ simple_add_obj!(WlSubsurface);
impl SurfaceExt for WlSubsurface {
fn pre_commit(self: Rc<Self>, ctx: CommitContext) -> Result<CommitAction, WlSurfaceError> {
if ctx == CommitContext::RootCommit && self.sync() {
log::info!("Aborting commit due to sync");
log::debug!("Aborting commit due to sync");
return Ok(CommitAction::AbortCommit);
}
Ok(CommitAction::ContinueCommit)