1
0
Fork 0
forked from wry/wry

autocommit 2022-01-31 23:45:42 CET

This commit is contained in:
Julian Orth 2022-01-31 23:45:42 +01:00
parent 865d5f295d
commit f2117256b9
33 changed files with 784 additions and 178 deletions

View file

@ -1,15 +1,14 @@
mod types;
use std::cell::Cell;
use crate::client::{Client, DynEventFormatter};
use crate::clientmem::{ClientMem, ClientMemOffset};
use crate::format::Format;
use crate::ifs::wl_surface::{WlSurface, WlSurfaceId};
use crate::object::{Interface, Object, ObjectId};
use crate::rect::Rect;
use crate::render::{Image, Texture};
use crate::utils::buffd::MsgParser;
use crate::utils::clonecell::CloneCell;
use crate::utils::copyhashmap::CopyHashMap;
use std::rc::Rc;
pub use types::*;
@ -25,18 +24,22 @@ pub enum WlBufferStorage {
}
pub struct WlBuffer {
id: WlBufferId,
pub id: WlBufferId,
destroyed: Cell<bool>,
pub client: Rc<Client>,
pub rect: Rect,
format: &'static Format,
pub format: &'static Format,
storage: WlBufferStorage,
pub texture: CloneCell<Option<Rc<Texture>>>,
pub(super) surfaces: CopyHashMap<WlSurfaceId, Rc<WlSurface>>,
width: i32,
height: i32,
}
impl WlBuffer {
pub fn destroyed(&self) -> bool {
self.destroyed.get()
}
#[allow(clippy::too_many_arguments)]
pub fn new_dmabuf(
id: WlBufferId,
@ -48,13 +51,13 @@ impl WlBuffer {
let height = img.height();
Self {
id,
destroyed: Cell::new(false),
client: client.clone(),
rect: Rect::new_sized(0, 0, width, height).unwrap(),
format,
width,
height,
texture: CloneCell::new(None),
surfaces: Default::default(),
storage: WlBufferStorage::Dmabuf(img.clone()),
}
}
@ -82,6 +85,7 @@ impl WlBuffer {
}
Ok(Self {
id,
destroyed: Cell::new(false),
client: client.clone(),
rect: Rect::new_sized(0, 0, width, height).unwrap(),
format,
@ -89,7 +93,6 @@ impl WlBuffer {
width,
height,
texture: CloneCell::new(None),
surfaces: Default::default(),
})
}
@ -114,13 +117,8 @@ impl WlBuffer {
fn destroy(&self, parser: MsgParser<'_, '_>) -> Result<(), DestroyError> {
let _req: Destroy = self.client.parse(self, parser)?;
{
let surfaces = self.surfaces.lock();
for surface in surfaces.values() {
surface.buffer.set(None);
}
}
self.client.remove_obj(self)?;
self.destroyed.set(true);
Ok(())
}
@ -155,8 +153,4 @@ impl Object for WlBuffer {
fn num_requests(&self) -> u32 {
DESTROY + 1
}
fn break_loops(&self) {
self.surfaces.clear();
}
}

View file

@ -1,10 +1,13 @@
mod types;
use crate::client::Client;
use crate::client::{Client, DynEventFormatter};
use crate::object::{Interface, Object, ObjectId};
use crate::utils::buffd::MsgParser;
use std::rc::Rc;
pub use types::*;
use crate::ifs::wl_data_device_manager::WlDataDeviceManagerObj;
use crate::ifs::wl_data_offer::WlDataOfferId;
use crate::ifs::wl_seat::WlSeatObj;
const START_DRAG: u32 = 0;
const SET_SELECTION: u32 = 1;
@ -23,18 +26,29 @@ const ROLE: u32 = 0;
id!(WlDataDeviceId);
pub struct WlDataDevice {
id: WlDataDeviceId,
pub id: WlDataDeviceId,
pub manager: Rc<WlDataDeviceManagerObj>,
client: Rc<Client>,
seat: Rc<WlSeatObj>,
}
impl WlDataDevice {
pub fn new(id: WlDataDeviceId, client: &Rc<Client>) -> Self {
pub fn new(id: WlDataDeviceId, manager: &Rc<WlDataDeviceManagerObj>, seat: &Rc<WlSeatObj>) -> Self {
Self {
id,
client: client.clone(),
manager: manager.clone(),
client: seat.client().clone(),
seat: seat.clone(),
}
}
pub fn selection(self: &Rc<Self>, id: WlDataOfferId) -> DynEventFormatter {
Box::new(Selection {
obj: self.clone(),
id,
})
}
fn start_drag(&self, parser: MsgParser<'_, '_>) -> Result<(), StartDragError> {
let _req: StartDrag = self.client.parse(self, parser)?;
Ok(())
@ -47,6 +61,7 @@ impl WlDataDevice {
fn release(&self, parser: MsgParser<'_, '_>) -> Result<(), ReleaseError> {
let _req: Release = self.client.parse(self, parser)?;
self.seat.remove_data_device(self);
self.client.remove_obj(self)?;
Ok(())
}
@ -80,4 +95,8 @@ impl Object for WlDataDevice {
fn num_requests(&self) -> u32 {
RELEASE + 1
}
fn break_loops(&self) {
self.seat.remove_data_device(self);
}
}

View file

@ -30,6 +30,7 @@ pub struct WlDataDeviceManagerGlobal {
pub struct WlDataDeviceManagerObj {
id: WlDataDeviceManagerId,
client: Rc<Client>,
pub version: u32,
}
impl WlDataDeviceManagerGlobal {
@ -41,11 +42,12 @@ impl WlDataDeviceManagerGlobal {
self: Rc<Self>,
id: WlDataDeviceManagerId,
client: &Rc<Client>,
_version: u32,
version: u32,
) -> Result<(), WlDataDeviceManagerError> {
let obj = Rc::new(WlDataDeviceManagerObj {
id,
client: client.clone(),
version
});
client.add_client_obj(&obj)?;
Ok(())
@ -60,10 +62,12 @@ impl WlDataDeviceManagerObj {
Ok(())
}
fn get_data_device(&self, parser: MsgParser<'_, '_>) -> Result<(), GetDataDeviceError> {
let req: GetDataDevice = self.client.parse(self, parser)?;
let res = Rc::new(WlDataDevice::new(req.id, &self.client));
self.client.add_client_obj(&res)?;
fn get_data_device(self: &Rc<Self>, parser: MsgParser<'_, '_>) -> Result<(), GetDataDeviceError> {
let req: GetDataDevice = self.client.parse(&**self, parser)?;
let seat = self.client.get_wl_seat(req.seat)?;
let dev = Rc::new(WlDataDevice::new(req.id, self, &seat));
seat.add_data_device(&dev);
self.client.add_client_obj(&dev)?;
Ok(())
}

View file

@ -5,10 +5,11 @@ use crate::client::{Client, ClientId, DynEventFormatter, WlEvent};
use crate::globals::{Global, GlobalName};
use crate::object::{Interface, Object, ObjectId};
use crate::utils::buffd::MsgParser;
use crate::utils::copyhashmap::CopyHashMap;
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::collections::hash_map::Entry;
use std::iter;
use std::rc::Rc;
use ahash::AHashMap;
pub use types::*;
id!(WlOutputId);
@ -59,7 +60,7 @@ pub struct WlOutputGlobal {
pub y: Cell<i32>,
width: Cell<i32>,
height: Cell<i32>,
bindings: CopyHashMap<(ClientId, WlOutputId), Rc<WlOutputObj>>,
pub bindings: RefCell<AHashMap<ClientId, AHashMap<WlOutputId, Rc<WlOutputObj>>>>,
}
impl WlOutputGlobal {
@ -84,20 +85,22 @@ impl WlOutputGlobal {
changed |= self.height.replace(height) != height;
if changed {
let bindings = self.bindings.lock();
let bindings = self.bindings.borrow_mut();
for binding in bindings.values() {
let events = [
binding.geometry(),
binding.mode(),
binding.scale(),
binding.done(),
];
let events = events
.into_iter()
.map(|e| WlEvent::Event(e))
.chain(iter::once(WlEvent::Flush));
for event in events {
binding.client.event2(event);
for binding in binding.values() {
let events = [
binding.geometry(),
binding.mode(),
binding.scale(),
binding.done(),
];
let events = events
.into_iter()
.map(|e| WlEvent::Event(e))
.chain(iter::once(WlEvent::Flush));
for event in events {
binding.client.event2(event);
}
}
}
}
@ -116,7 +119,7 @@ impl WlOutputGlobal {
version,
});
client.add_client_obj(&obj)?;
self.bindings.set((client.id, id), obj.clone());
self.bindings.borrow_mut().entry(client.id).or_default().insert(id, obj.clone());
client.event(obj.geometry());
client.event(obj.mode());
if obj.send_scale() {
@ -149,13 +152,13 @@ impl Global for WlOutputGlobal {
}
fn break_loops(&self) {
self.bindings.clear();
self.bindings.borrow_mut().clear();
}
}
pub struct WlOutputObj {
global: Rc<WlOutputGlobal>,
id: WlOutputId,
pub id: WlOutputId,
client: Rc<Client>,
version: u32,
}
@ -177,8 +180,8 @@ impl WlOutputObj {
physical_width: self.global.width.get() as _,
physical_height: self.global.height.get() as _,
subpixel: SP_UNKNOWN,
make: String::new(),
model: String::new(),
make: "i4".to_string(),
model: "i4".to_string(),
transform: TF_NORMAL,
})
}
@ -204,9 +207,18 @@ impl WlOutputObj {
Box::new(Done { obj: self.clone() })
}
fn remove_binding(&self) {
if let Entry::Occupied(mut e) = self.global.bindings.borrow_mut().entry(self.client.id) {
e.get_mut().remove(&self.id);
if e.get().is_empty() {
e.remove();
}
}
}
fn release(&self, parser: MsgParser<'_, '_>) -> Result<(), ReleaseError> {
let _req: Release = self.client.parse(self, parser)?;
self.global.bindings.remove(&(self.client.id, self.id));
self.remove_binding();
self.client.remove_obj(self)?;
Ok(())
}
@ -244,6 +256,6 @@ impl Object for WlOutputObj {
}
fn break_loops(&self) {
self.global.bindings.remove(&(self.client.id, self.id));
self.remove_binding();
}
}

View file

@ -3,7 +3,9 @@ use std::rc::Rc;
use crate::backend::{KeyState, OutputId, ScrollAxis, SeatEvent, SeatId};
use crate::client::{ClientId, DynEventFormatter};
use crate::fixed::Fixed;
use crate::ifs::wl_seat::{wl_pointer, WlSeatGlobal, WlSeatObj};
use crate::ifs::wl_data_device::WlDataDevice;
use crate::ifs::wl_data_offer::WlDataOfferId;
use crate::ifs::wl_seat::{wl_keyboard, wl_pointer, WlSeatGlobal, WlSeatObj};
use crate::ifs::wl_seat::wl_keyboard::WlKeyboard;
use crate::ifs::wl_seat::wl_pointer::{POINTER_FRAME_SINCE_VERSION, WlPointer};
use crate::ifs::wl_surface::WlSurface;
@ -12,7 +14,7 @@ use crate::ifs::wl_surface::xdg_surface::xdg_toplevel::XdgToplevel;
use crate::ifs::wl_surface::xdg_surface::XdgSurface;
use crate::tree::{FloatNode, FoundNode, Node};
use crate::utils::smallmap::SmallMap;
use crate::xkbcommon::ModifierState;
use crate::xkbcommon::{ModifierState, XKB_KEY_DOWN, XKB_KEY_UP};
#[derive(Default)]
pub struct NodeSeatState {
@ -29,12 +31,18 @@ impl NodeSeatState {
self.pointer_foci.remove(&seat.seat.id());
}
fn focus(&self, seat: &Rc<WlSeatGlobal>) {
fn focus(&self, seat: &Rc<WlSeatGlobal>) -> bool {
self.kb_foci.insert(seat.seat.id(), seat.clone());
self.kb_foci.len() == 1
}
fn unfocus(&self, seat: &WlSeatGlobal) {
fn unfocus(&self, seat: &WlSeatGlobal) -> bool {
self.kb_foci.remove(&seat.seat.id());
self.kb_foci.len() == 0
}
pub fn is_active(&self) -> bool {
self.kb_foci.len() > 0
}
pub fn destroy_node(&self, node: &dyn Node) {
@ -98,41 +106,27 @@ impl WlSeatGlobal {
}
}
fn key_event(&self, _key: u32, _state: KeyState) {
// let (state, xkb_dir) = {
// let mut pk = self.pressed_keys.borrow_mut();
// match state {
// KeyState::Released => {
// if !pk.remove(&key) {
// return;
// }
// (wl_keyboard::RELEASED, XKB_KEY_UP)
// }
// KeyState::Pressed => {
// if !pk.insert(key) {
// return;
// }
// (wl_keyboard::PRESSED, XKB_KEY_DOWN)
// }
// }
// };
// let mods = self.kb_state.borrow_mut().update(key, xkb_dir);
// let node = self.keyboard_node.get().into_kind();
// if let NodeKind::Toplevel(node) = node {
// self.tl_kb_event(&node, |k| k.key(0, 0, key, state)).await;
// if let Some(mods) = mods {
// self.tl_kb_event(&node, |k| {
// k.modifiers(
// 0,
// mods.mods_depressed,
// mods.mods_latched,
// mods.mods_locked,
// mods.group,
// )
// })
// .await;
// }
// }
fn key_event(&self, key: u32, state: KeyState) {
let (state, xkb_dir) = {
let mut pk = self.pressed_keys.borrow_mut();
match state {
KeyState::Released => {
if !pk.remove(&key) {
return;
}
(wl_keyboard::RELEASED, XKB_KEY_UP)
}
KeyState::Pressed => {
if !pk.insert(key) {
return;
}
(wl_keyboard::PRESSED, XKB_KEY_DOWN)
}
}
};
let mods = self.kb_state.borrow_mut().update(key, xkb_dir);
let node = self.keyboard_node.get();
node.key(self, key, state, mods);
}
}
@ -157,6 +151,12 @@ impl WlSeatGlobal {
self.extents_start_pos.set((ex.x1(), ex.y1()));
}
pub fn focus_toplevel(self: &Rc<Self>, n: &Rc<XdgToplevel>) {
let node = self.toplevel_focus_history.add_last(n.clone());
n.toplevel_history.insert(self.id(), node);
self.focus_xdg_surface(&n.xdg);
}
fn focus_xdg_surface(self: &Rc<Self>, xdg: &Rc<XdgSurface>) {
self.focus_surface(&xdg.focus_surface(self));
}
@ -167,16 +167,20 @@ impl WlSeatGlobal {
return;
}
old.unfocus(self);
old.seat_state().unfocus(self);
if old.seat_state().unfocus(self) {
old.active_changed(false);
}
surface.seat_state().focus(self);
if surface.seat_state().focus(self) {
surface.active_changed(true);
}
surface.clone().focus(self);
self.keyboard_node.set(surface.clone());
let pressed_keys: Vec<_> = self.pressed_keys.borrow().iter().copied().collect();
log::info!("enter");
let serial = self.serial.fetch_add(1);
self.surface_kb_event(0, &surface, |k| {
k.enter(0, surface.id, pressed_keys.clone())
k.enter(serial, surface.id, pressed_keys.clone())
});
let ModifierState {
mods_depressed,
@ -184,9 +188,12 @@ impl WlSeatGlobal {
mods_locked,
group,
} = self.kb_state.borrow().mods();
let serial = self.serial.fetch_add(1);
self.surface_kb_event(0, &surface, |k| {
k.modifiers(0, mods_depressed, mods_latched, mods_locked, group)
k.modifiers(serial, mods_depressed, mods_latched, mods_locked, group)
});
self.surface_data_device_event(0, &surface, |dd| dd.selection(WlDataOfferId::NONE));
}
fn for_each_seat<C>(&self, ver: u32, client: ClientId, mut f: C)
@ -227,6 +234,20 @@ impl WlSeatGlobal {
})
}
fn for_each_data_device<C>(&self, ver: u32, client: ClientId, mut f: C)
where
C: FnMut(&Rc<WlDataDevice>),
{
let dd = self.data_devices.borrow_mut();
if let Some(dd) = dd.get(&client) {
for dd in dd.values() {
if dd.manager.version >= ver {
f(dd);
}
}
}
}
fn surface_pointer_frame(&self, surface: &WlSurface) {
self.surface_pointer_event(POINTER_FRAME_SINCE_VERSION, surface, |p| p.frame());
}
@ -253,6 +274,17 @@ impl WlSeatGlobal {
client.flush();
}
fn surface_data_device_event<F>(&self, ver: u32, surface: &WlSurface, mut f: F)
where
F: FnMut(&Rc<WlDataDevice>) -> DynEventFormatter,
{
let client = &surface.client;
self.for_each_data_device(ver, client.id, |p| {
client.event(f(p));
});
client.flush();
}
fn set_new_position(self: &Rc<Self>, x: Fixed, y: Fixed) {
self.pos.set((x, y));
self.handle_new_position(true);
@ -264,6 +296,11 @@ impl WlSeatGlobal {
fn handle_new_position(self: &Rc<Self>, changed: bool) {
let (x, y) = self.pos.get();
if changed {
if let Some(cursor) = self.cursor.get() {
cursor.set_position(x.round_down(), y.round_down());
}
}
let mut found_tree = self.found_tree.borrow_mut();
let mut stack = self.pointer_stack.borrow_mut();
// if self.move_.get() {
@ -322,9 +359,10 @@ impl WlSeatGlobal {
KeyState::Released => (wl_pointer::RELEASED, false),
KeyState::Pressed => (wl_pointer::PRESSED, true),
};
self.surface_pointer_event(0, surface, |p| p.button(0, 0, button, state));
let serial = self.serial.fetch_add(1);
self.surface_pointer_event(0, surface, |p| p.button(serial, 0, button, state));
self.surface_pointer_frame(surface);
if pressed {
if pressed && surface.belongs_to_toplevel() {
self.focus_surface(surface);
}
}
@ -353,17 +391,16 @@ impl WlSeatGlobal {
// Enter callbacks
impl WlSeatGlobal {
pub fn enter_toplevel(self: &Rc<Self>, n: &Rc<XdgToplevel>) {
let node = self.toplevel_focus_history.add_last(n.clone());
n.toplevel_history.insert(self.id(), node);
self.focus_xdg_surface(&n.xdg);
self.focus_toplevel(n);
}
pub fn enter_popup(self: &Rc<Self>, n: &Rc<XdgPopup>) {
self.focus_xdg_surface(&n.xdg);
// self.focus_xdg_surface(&n.xdg);
}
pub fn enter_surface(&self, n: &WlSurface, x: Fixed, y: Fixed) {
self.surface_pointer_event(0, n, |p| p.enter(0, n.id, x, y));
let serial = self.serial.fetch_add(1);
self.surface_pointer_event(0, n, |p| p.enter(serial, n.id, x, y));
self.surface_pointer_frame(n);
}
}
@ -371,7 +408,8 @@ impl WlSeatGlobal {
// Leave callbacks
impl WlSeatGlobal {
pub fn leave_surface(&self, n: &WlSurface) {
self.surface_pointer_event(0, n, |p| p.leave(0, n.id));
let serial = self.serial.fetch_add(1);
self.surface_pointer_event(0, n, |p| p.leave(serial, n.id));
self.surface_pointer_frame(n);
}
}
@ -382,3 +420,23 @@ impl WlSeatGlobal {
self.surface_kb_event(0, surface, |k| k.leave(0, surface.id))
}
}
// Key callbacks
impl WlSeatGlobal {
pub fn key_surface(&self, surface: &WlSurface, key: u32, state: u32, mods: Option<ModifierState>) {
let serial = self.serial.fetch_add(1);
self.surface_kb_event(0, surface, |k| k.key(serial, 0, key, state));
let serial = self.serial.fetch_add(1);
if let Some(mods) = mods {
self.surface_kb_event(0, surface, |k| {
k.modifiers(
serial,
mods.mods_depressed,
mods.mods_latched,
mods.mods_locked,
mods.group,
)
});
}
}
}

View file

@ -19,15 +19,18 @@ use crate::utils::clonecell::CloneCell;
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::linkedlist::{LinkedList};
use crate::xkbcommon::{XkbContext, XkbState};
use crate::State;
use crate::{NumCell, State};
use ahash::{AHashMap, AHashSet};
use bstr::ByteSlice;
use std::cell::{Cell, RefCell};
use std::collections::hash_map::Entry;
use std::io::Write;
use std::rc::Rc;
pub use types::*;
use uapi::{c, OwnedFd};
pub use handling::NodeSeatState;
use crate::ifs::wl_data_device::{WlDataDevice, WlDataDeviceId};
use crate::ifs::wl_surface::cursor::CursorSurface;
id!(WlSeatId);
@ -54,6 +57,7 @@ pub struct WlSeatGlobal {
name: GlobalName,
state: Rc<State>,
seat: Rc<dyn Seat>,
seat_name: Rc<String>,
move_: Cell<bool>,
move_start_pos: Cell<(Fixed, Fixed)>,
extents_start_pos: Cell<(i32, i32)>,
@ -64,9 +68,12 @@ pub struct WlSeatGlobal {
keyboard_node: CloneCell<Rc<dyn Node>>,
pressed_keys: RefCell<AHashSet<u32>>,
bindings: RefCell<AHashMap<ClientId, AHashMap<WlSeatId, Rc<WlSeatObj>>>>,
data_devices: RefCell<AHashMap<ClientId, AHashMap<WlDataDeviceId, Rc<WlDataDevice>>>>,
kb_state: RefCell<XkbState>,
layout: Rc<OwnedFd>,
layout_size: u32,
cursor: CloneCell<Option<Rc<CursorSurface>>>,
serial: NumCell<u32>,
}
impl WlSeatGlobal {
@ -92,6 +99,7 @@ impl WlSeatGlobal {
name,
state: state.clone(),
seat: seat.clone(),
seat_name: Rc::new(format!("seat-{}", seat.id())),
move_: Cell::new(false),
move_start_pos: Cell::new((Fixed(0), Fixed(0))),
extents_start_pos: Cell::new((0, 0)),
@ -102,12 +110,31 @@ impl WlSeatGlobal {
keyboard_node: CloneCell::new(state.root.clone()),
pressed_keys: RefCell::new(Default::default()),
bindings: Default::default(),
data_devices: RefCell::new(Default::default()),
kb_state: RefCell::new(kb_state),
layout,
layout_size,
cursor: Default::default(),
serial: Default::default(),
}
}
pub fn set_cursor(&self, cursor: Option<Rc<CursorSurface>>) {
if let Some(old) = self.cursor.get() {
if let Some(new) = cursor.as_ref() {
if Rc::ptr_eq(&old, new) {
return;
}
}
old.handle_unset();
}
self.cursor.set(cursor);
}
pub fn get_cursor(&self) -> Option<Rc<CursorSurface>> {
self.cursor.get()
}
pub fn id(&self) -> SeatId {
self.seat.id()
}
@ -128,6 +155,7 @@ impl WlSeatGlobal {
});
client.add_client_obj(&obj)?;
client.event(obj.capabilities());
client.event(obj.name(&self.seat_name));
{
let mut bindings = self.bindings.borrow_mut();
let bindings = bindings.entry(client.id).or_insert_with(Default::default);
@ -162,7 +190,7 @@ impl Global for WlSeatGlobal {
}
pub struct WlSeatObj {
global: Rc<WlSeatGlobal>,
pub global: Rc<WlSeatGlobal>,
id: WlSeatId,
client: Rc<Client>,
pointers: CopyHashMap<WlPointerId, Rc<WlPointer>>,
@ -178,6 +206,32 @@ impl WlSeatObj {
})
}
fn name(self: &Rc<Self>, name: &Rc<String>) -> DynEventFormatter {
Box::new(Name {
obj: self.clone(),
name: name.clone(),
})
}
pub fn add_data_device(&self, device: &Rc<WlDataDevice>) {
let mut dd = self.global.data_devices.borrow_mut();
dd.entry(self.client.id).or_default().insert(device.id, device.clone());
}
pub fn remove_data_device(&self, device: &WlDataDevice) {
let mut dd = self.global.data_devices.borrow_mut();
if let Entry::Occupied(mut e) = dd.entry(self.client.id) {
e.get_mut().remove(&device.id);
if e.get().is_empty() {
e.remove();
}
}
}
pub fn client(&self) -> &Rc<Client> {
&self.client
}
pub fn move_(&self, node: &Rc<FloatNode>) {
self.global.move_(node);
}

View file

@ -148,11 +148,11 @@ impl Debug for Capabilities {
pub(super) struct Name {
pub obj: Rc<WlSeatObj>,
pub name: String,
pub name: Rc<String>,
}
impl EventFormatter for Name {
fn format(self: Box<Self>, fmt: &mut MsgFormatter<'_>) {
fmt.header(self.obj.id, NAME).string(&self.name);
fmt.header(self.obj.id, NAME).string(self.name.as_bytes());
}
fn obj(&self) -> &dyn Object {
&*self.obj

View file

@ -147,7 +147,25 @@ impl WlPointer {
}
fn set_cursor(&self, parser: MsgParser<'_, '_>) -> Result<(), SetCursorError> {
let _req: SetCursor = self.seat.client.parse(self, parser)?;
let req: SetCursor = self.seat.client.parse(self, parser)?;
let mut cursor_opt = None;
if req.surface.is_some() {
let surface = self.seat.client.get_surface(req.surface)?;
let cursor = surface.get_cursor(&self.seat.global)?;
cursor.set_hotspot(req.hotspot_x, req.hotspot_y);
cursor_opt = Some(cursor);
}
let pointer_node = match self.seat.global.pointer_stack.borrow().last().cloned() {
Some(n) => n,
_ => {
// cannot happen
return Ok(());
},
};
if pointer_node.client_id() != Some(self.seat.client.id) {
return Ok(());
}
self.seat.global.set_cursor(cursor_opt);
Ok(())
}

View file

@ -3,7 +3,7 @@ use crate::fixed::Fixed;
use crate::ifs::wl_seat::wl_pointer::{
WlPointer, AXIS, AXIS_DISCRETE, AXIS_SOURCE, AXIS_STOP, BUTTON, ENTER, FRAME, LEAVE, MOTION,
};
use crate::ifs::wl_surface::WlSurfaceId;
use crate::ifs::wl_surface::{WlSurfaceError, WlSurfaceId};
use crate::object::Object;
use crate::utils::buffd::{MsgFormatter, MsgParser, MsgParserError};
use std::fmt::{Debug, Formatter};
@ -28,9 +28,12 @@ pub enum SetCursorError {
ParseError(#[source] Box<MsgParserError>),
#[error(transparent)]
ClientError(Box<ClientError>),
#[error(transparent)]
WlSurfaceError(Box<WlSurfaceError>),
}
efrom!(SetCursorError, ParseError, MsgParserError);
efrom!(SetCursorError, ClientError);
efrom!(SetCursorError, WlSurfaceError);
#[derive(Debug, Error)]
pub enum ReleaseError {
@ -150,6 +153,9 @@ impl EventFormatter for Motion {
fn obj(&self) -> &dyn Object {
self.obj.deref()
}
fn should_log(&self) -> bool {
false
}
}
impl Debug for Motion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@ -227,6 +233,9 @@ impl EventFormatter for Frame {
fn obj(&self) -> &dyn Object {
self.obj.deref()
}
fn should_log(&self) -> bool {
false
}
}
impl Debug for Frame {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {

View file

@ -0,0 +1,73 @@
use std::cell::Cell;
use std::rc::Rc;
use crate::ifs::wl_seat::{WlSeatGlobal};
use crate::ifs::wl_surface::{WlSurface};
use crate::rect::Rect;
pub struct CursorSurface {
seat: Rc<WlSeatGlobal>,
surface: Rc<WlSurface>,
hotspot: Cell<(i32, i32)>,
pos: Cell<(i32, i32)>,
extents: Cell<Rect>,
}
impl CursorSurface {
pub fn new(seat: &Rc<WlSeatGlobal>, surface: &Rc<WlSurface>) -> Self {
Self {
seat: seat.clone(),
surface: surface.clone(),
hotspot: Cell::new((0, 0)),
pos: Cell::new((0, 0)),
extents: Cell::new(Default::default())
}
}
fn update_extents(&self) {
let (pos_x, pos_y) = self.pos.get();
let extents = self.extents.get();
let (hot_x, hot_y) = self.hotspot.get();
self.extents.set(Rect::new_sized(pos_x - hot_x, pos_y - hot_y, extents.width(), extents.height()).unwrap());
}
pub fn set_position(&self, x: i32, y: i32) {
self.pos.set((x, y));
self.update_extents();
}
pub fn handle_unset(&self) {
self.surface.cursors.remove(&self.seat.id());
}
pub fn handle_surface_destroy(&self) {
self.seat.set_cursor(None);
}
pub fn handle_buffer_change(&self) {
let (width, height) = match self.surface.buffer.get() {
Some(b) => (b.rect.width(), b.rect.height()),
_ => (0, 0),
};
self.extents.set(Rect::new_sized(0, 0, width, height).unwrap());
self.update_extents();
}
pub fn set_hotspot(&self, x: i32, y: i32) {
self.hotspot.set((x, y));
self.update_extents();
}
pub fn dec_hotspot(&self, hotspot_dx: i32, hotspot_dy: i32) {
let (hot_x, hot_y) = self.hotspot.get();
self.hotspot.set((hot_x - hotspot_dx, hot_y - hotspot_dy));
self.update_extents();
}
pub fn surface(&self) -> &Rc<WlSurface> {
&self.surface
}
pub fn extents(&self) -> Rect {
self.extents.get()
}
}

View file

@ -1,9 +1,10 @@
mod types;
pub mod wl_subsurface;
pub mod xdg_surface;
pub mod cursor;
use crate::backend::{KeyState, ScrollAxis};
use crate::client::{Client, RequestParser};
use crate::backend::{KeyState, ScrollAxis, SeatId};
use crate::client::{Client, ClientId, DynEventFormatter, RequestParser};
use crate::fixed::Fixed;
use crate::ifs::wl_buffer::WlBuffer;
use crate::ifs::wl_callback::WlCallback;
@ -23,7 +24,12 @@ use std::mem;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
pub use types::*;
use crate::ifs::wl_surface::xdg_surface::XdgSurface;
use crate::ifs::wl_output::WlOutputId;
use crate::ifs::wl_surface::cursor::CursorSurface;
use crate::ifs::wl_surface::xdg_surface::{XdgSurface, XdgSurfaceRole};
use crate::render::Renderer;
use crate::utils::smallmap::SmallMap;
use crate::xkbcommon::ModifierState;
const DESTROY: u32 = 0;
const ATTACH: u32 = 1;
@ -55,6 +61,7 @@ pub enum SurfaceRole {
None,
Subsurface,
XdgSurface,
Cursor,
}
impl SurfaceRole {
@ -63,6 +70,7 @@ impl SurfaceRole {
SurfaceRole::None => "none",
SurfaceRole::Subsurface => "subsurface",
SurfaceRole::XdgSurface => "xdg_surface",
SurfaceRole::Cursor => "cursor",
}
}
}
@ -86,6 +94,7 @@ pub struct WlSurface {
pub frame_requests: RefCell<Vec<Rc<WlCallback>>>,
seat_state: NodeSeatState,
xdg: CloneCell<Option<Rc<XdgSurface>>>,
cursors: SmallMap<SeatId, Rc<CursorSurface>, 1>,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@ -180,9 +189,38 @@ impl WlSurface {
frame_requests: RefCell::new(vec![]),
seat_state: Default::default(),
xdg: Default::default(),
cursors: Default::default(),
}
}
pub fn is_cursor(&self) -> bool {
self.role.get() == SurfaceRole::Cursor
}
pub fn get_cursor(self: &Rc<Self>, seat: &Rc<WlSeatGlobal>) -> Result<Rc<CursorSurface>, WlSurfaceError> {
if let Some(cursor) = self.cursors.get(&seat.id()) {
return Ok(cursor);
}
self.set_role(SurfaceRole::Cursor)?;
let cursor = Rc::new(CursorSurface::new(seat, self));
self.cursors.insert(seat.id(), cursor.clone());
Ok(cursor)
}
pub fn belongs_to_toplevel(&self) -> bool {
if let Some(xdg) = self.xdg.get() {
return xdg.role() == XdgSurfaceRole::XdgToplevel;
}
false
}
fn enter_event(self: &Rc<Self>, output: WlOutputId) -> DynEventFormatter {
Box::new(Enter {
obj: self.clone(),
output,
})
}
fn set_xdg_surface(&self, xdg: Option<Rc<XdgSurface>>) {
let ch = self.children.borrow();
if let Some(ch) = &*ch {
@ -190,6 +228,11 @@ impl WlSurface {
ss.surface.set_xdg_surface(xdg.clone());
}
}
if self.seat_state.is_active() {
if let Some(xdg) = &xdg {
xdg.surface_active_changed(true);
}
}
self.xdg.set(xdg);
}
@ -261,8 +304,15 @@ impl WlSurface {
self.client.parse(self, parser)
}
fn unset_cursors(&self) {
while let Some((_, cursor)) = self.cursors.pop() {
cursor.handle_surface_destroy();
}
}
fn destroy(&self, parser: MsgParser<'_, '_>) -> Result<(), DestroyError> {
let _req: Destroy = self.parse(parser)?;
self.unset_cursors();
self.destroy_node(true);
if self.ext.get().is_some() {
return Err(DestroyError::ReloObjectStillExists);
@ -276,17 +326,11 @@ impl WlSurface {
}
*children = None;
}
{
let buffer = self.buffer.get();
if let Some(buffer) = &buffer {
buffer.surfaces.remove(&self.id);
}
}
self.client.remove_obj(self)?;
Ok(())
}
fn attach(&self, parser: MsgParser<'_, '_>) -> Result<(), AttachError> {
fn attach(self: &Rc<Self>, parser: MsgParser<'_, '_>) -> Result<(), AttachError> {
let req: Attach = self.parse(parser)?;
let buf = if req.buffer.is_some() {
Some((req.x, req.y, self.client.get_buffer(req.buffer)?))
@ -332,7 +376,7 @@ impl WlSurface {
Ok(())
}
fn do_commit(&self, ctx: CommitContext) -> Result<(), WlSurfaceError> {
fn do_commit(self: &Rc<Self>, ctx: CommitContext) -> Result<(), WlSurfaceError> {
let ext = self.ext.get();
if ext.clone().pre_commit(ctx)? == CommitAction::AbortCommit {
return Ok(());
@ -350,8 +394,9 @@ impl WlSurface {
let mut new_size = None;
if let Some(buffer) = self.buffer.take() {
old_size = Some(buffer.rect);
self.client.event(buffer.release());
buffer.surfaces.remove(&self.id);
if !buffer.destroyed() {
self.client.event(buffer.release());
}
}
if let Some((dx, dy, buffer)) = buffer_change {
let _ = buffer.update_texture();
@ -361,14 +406,23 @@ impl WlSurface {
self.buf_y.fetch_add(dy);
if (dx, dy) != (0, 0) {
self.need_extents_update.set(true);
for (_, cursor) in &self.cursors {
cursor.dec_hotspot(dx, dy);
}
}
} else {
self.buf_x.set(0);
self.buf_y.set(0);
for (_, cursor) in &self.cursors {
cursor.set_hotspot(0, 0);
}
}
if old_size != new_size {
self.need_extents_update.set(true);
}
for (_, cursor) in &self.cursors {
cursor.handle_buffer_change();
}
}
{
let mut pfr = self.pending.frame_request.borrow_mut();
@ -389,7 +443,7 @@ impl WlSurface {
Ok(())
}
fn commit(&self, parser: MsgParser<'_, '_>) -> Result<(), CommitError> {
fn commit(self: &Rc<Self>, parser: MsgParser<'_, '_>) -> Result<(), CommitError> {
let _req: Commit = self.parse(parser)?;
self.do_commit(CommitContext::RootCommit)?;
Ok(())
@ -414,7 +468,7 @@ impl WlSurface {
}
fn handle_request_(
&self,
self: &Rc<Self>,
request: u32,
parser: MsgParser<'_, '_>,
) -> Result<(), WlSurfaceError> {
@ -494,6 +548,7 @@ impl Object for WlSurface {
}
fn break_loops(&self) {
self.unset_cursors();
self.destroy_node(true);
*self.children.borrow_mut() = None;
self.unset_ext();
@ -530,10 +585,23 @@ impl Node for WlSurface {
for seat in remove {
xdg.focus_surface.remove(&seat);
}
if self.seat_state.is_active() {
xdg.surface_active_changed(false);
}
}
self.seat_state.destroy_node(self);
}
fn active_changed(&self, active: bool) {
if let Some(xdg) = self.xdg.get() {
xdg.surface_active_changed(active);
}
}
fn key(&self, seat: &WlSeatGlobal, key: u32, state: u32, mods: Option<ModifierState>) {
seat.key_surface(self, key, state, mods);
}
fn button(self: Rc<Self>, seat: &Rc<WlSeatGlobal>, button: u32, state: KeyState) {
seat.button_surface(&self, button, state);
}
@ -563,4 +631,12 @@ impl Node for WlSurface {
fn motion(&self, seat: &WlSeatGlobal, x: Fixed, y: Fixed) {
seat.motion_surface(self, x, y)
}
fn render(&self, renderer: &mut Renderer, x: i32, y: i32) {
renderer.render_surface(self, x, y);
}
fn client_id(&self) -> Option<ClientId> {
Some(self.client.id)
}
}

View file

@ -1,11 +1,15 @@
use crate::client::{ClientError, RequestParser};
use crate::client::{ClientError, EventFormatter, RequestParser};
use crate::ifs::wl_callback::WlCallbackId;
use crate::ifs::wl_region::WlRegionId;
use crate::ifs::wl_surface::xdg_surface::XdgSurfaceError;
use crate::ifs::wl_surface::{SurfaceRole, WlSurfaceId};
use crate::utils::buffd::{MsgParser, MsgParserError};
use crate::ifs::wl_surface::{ENTER, SurfaceRole, WlSurface, WlSurfaceId};
use crate::utils::buffd::{MsgFormatter, MsgParser, MsgParserError};
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::rc::Rc;
use thiserror::Error;
use crate::ifs::wl_output::WlOutputId;
use crate::object::Object;
#[derive(Debug, Error)]
pub enum WlSurfaceError {
@ -329,3 +333,21 @@ impl Debug for DamageBuffer {
)
}
}
pub(super) struct Enter {
pub obj: Rc<WlSurface>,
pub output: WlOutputId,
}
impl EventFormatter for Enter {
fn format(self: Box<Self>, fmt: &mut MsgFormatter<'_>) {
fmt.header(self.obj.id, ENTER).object(self.output);
}
fn obj(&self) -> &dyn Object {
self.obj.deref()
}
}
impl Debug for Enter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "enter(output: {})", self.output)
}
}

View file

@ -95,6 +95,10 @@ trait XdgSurfaceExt {
fn extents_changed(&self) {
// nothing
}
fn surface_active_changed(self: Rc<Self>, active: bool) {
let _ = active;
}
}
impl XdgSurface {
@ -118,6 +122,16 @@ impl XdgSurface {
}
}
pub fn surface_active_changed(&self, active: bool) {
if let Some(ext) = self.ext.get() {
ext.surface_active_changed(active);
}
}
pub fn role(&self) -> XdgSurfaceRole {
self.role.get()
}
fn set_workspace(&self, ws: &Rc<WorkspaceNode>) {
self.workspace.set(Some(ws.clone()));
let pu = self.popups.lock();
@ -311,7 +325,9 @@ impl XdgSurface {
});
FindTreeResult::AcceptsInput
},
_ => FindTreeResult::Other
_ => {
FindTreeResult::Other
}
}
}

View file

@ -1,6 +1,6 @@
mod types;
use crate::client::DynEventFormatter;
use crate::client::{ClientId, DynEventFormatter};
use crate::fixed::Fixed;
use crate::ifs::wl_seat::{NodeSeatState, WlSeatGlobal};
use crate::ifs::wl_surface::xdg_surface::{XdgSurface, XdgSurfaceError, XdgSurfaceExt};
@ -199,8 +199,8 @@ impl AbsoluteNode for XdgPopup {
self
}
fn absolute_position(&self) -> Rect {
self.xdg.absolute_desired_extents.get()
fn absolute_position(&self) -> (Rect, bool) {
(self.xdg.absolute_desired_extents.get(), false)
}
}
@ -235,6 +235,10 @@ impl Node for XdgPopup {
fn set_workspace(self: Rc<Self>, ws: &Rc<WorkspaceNode>) {
self.xdg.set_workspace(ws);
}
fn client_id(&self) -> Option<ClientId> {
Some(self.xdg.surface.client.id)
}
}
impl XdgSurfaceExt for XdgPopup {

View file

@ -1,6 +1,6 @@
mod types;
use crate::client::DynEventFormatter;
use crate::client::{ClientId, DynEventFormatter};
use crate::fixed::Fixed;
use crate::ifs::wl_seat::{NodeSeatState, WlSeatGlobal};
use crate::ifs::wl_surface::xdg_surface::{XdgSurface, XdgSurfaceError, XdgSurfaceExt};
@ -17,7 +17,8 @@ use std::cell::{Cell, RefCell};
use std::mem;
use std::rc::Rc;
pub use types::*;
use crate::backend::SeatId;
use crate::backend::{SeatId};
use crate::NumCell;
use crate::utils::linkedlist::LinkedNode;
use crate::utils::smallmap::SmallMap;
@ -80,6 +81,7 @@ pub struct XdgToplevel {
pub children: RefCell<AHashMap<XdgToplevelId, Rc<XdgToplevel>>>,
states: RefCell<AHashSet<u32>>,
pub toplevel_history: SmallMap<SeatId, LinkedNode<Rc<XdgToplevel>>, 1>,
active_surfaces: NumCell<u32>,
}
impl XdgToplevel {
@ -98,6 +100,22 @@ impl XdgToplevel {
children: RefCell::new(Default::default()),
states: RefCell::new(states),
toplevel_history: Default::default(),
active_surfaces: Default::default(),
}
}
pub fn set_active(self: &Rc<Self>, active: bool) {
let changed = {
let mut states = self.states.borrow_mut();
match active {
true => states.insert(STATE_ACTIVATED),
false => states.remove(&STATE_ACTIVATED),
}
};
if changed {
let rect = self.xdg.absolute_desired_extents.get();
self.xdg.surface.client.event(self.configure(rect.width(), rect.height()));
self.xdg.send_configure();
}
}
@ -278,7 +296,6 @@ impl XdgToplevel {
}
fn map_tiled(self: &Rc<Self>) {
log::info!("mapping tiled");
let state = &self.xdg.surface.client.state;
let seat = state.seat_queue.last();
if let Some(seat) = seat {
@ -389,6 +406,10 @@ impl Node for XdgToplevel {
fn set_workspace(self: Rc<Self>, ws: &Rc<WorkspaceNode>) {
self.xdg.set_workspace(ws);
}
fn client_id(&self) -> Option<ClientId> {
Some(self.xdg.surface.client.id)
}
}
impl XdgSurfaceExt for XdgToplevel {
@ -418,10 +439,29 @@ impl XdgSurfaceExt for XdgToplevel {
self.map_tiled();
}
self.extents_changed();
if let Some(workspace) = self.xdg.workspace.get() {
let output = workspace.output.get();
let bindings = output.global.bindings.borrow_mut();
for binding in bindings.get(&self.xdg.surface.client.id) {
for binding in binding.values() {
self.xdg.surface.client.event(self.xdg.surface.enter_event(binding.id));
}
}
}
{
let seats = surface.client.state.globals.lock_seats();
for seat in seats.values() {
seat.focus_toplevel(&self);
}
}
surface.client.state.tree_changed();
}
}
fn into_node(self: Rc<Self>) -> Option<Rc<dyn Node>> {
Some(self)
}
fn extents_changed(&self) {
if let Some(parent) = self.parent_node.get() {
let extents = self.xdg.extents.get();
@ -430,7 +470,15 @@ impl XdgSurfaceExt for XdgToplevel {
}
}
fn into_node(self: Rc<Self>) -> Option<Rc<dyn Node>> {
Some(self)
fn surface_active_changed(self: Rc<Self>, active: bool) {
if active {
if self.active_surfaces.fetch_add(1) == 0 {
self.set_active(true);
}
} else {
if self.active_surfaces.fetch_sub(1) == 1 {
self.set_active(false);
}
}
}
}