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

@ -19,11 +19,12 @@ use rand::Rng;
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::error::Error;
use std::ptr;
use std::{ptr, slice};
use std::rc::Rc;
use thiserror::Error;
use uapi::{c, OwnedFd};
use xcb_dl::{ffi, Xcb, XcbDri3, XcbPresent, XcbXinput, XcbXkb};
use xcb_dl::{ffi, Xcb, XcbDri3, XcbPresent, XcbRender, XcbXinput, XcbXkb};
use xcb_dl_util::cursor::{XcbCursorContext, XcbCursorImage};
use xcb_dl_util::error::{XcbError, XcbErrorParser};
use xcb_dl_util::xcb_box::XcbBox;
@ -75,6 +76,7 @@ struct XcbCon {
input: Box<XcbXinput>,
dri: Box<XcbDri3>,
present: Box<XcbPresent>,
render: Box<XcbRender>,
input_opcode: u8,
present_opcode: u8,
xkb: Box<XcbXkb>,
@ -91,6 +93,7 @@ impl XcbCon {
let xkb = Box::new(XcbXkb::load_loose()?);
let dri = Box::new(XcbDri3::load_loose()?);
let present = Box::new(XcbPresent::load_loose()?);
let render = Box::new(XcbRender::load_loose()?);
let c = xcb.xcb_connect(ptr::null(), ptr::null_mut());
let errors = XcbErrorParser::new(&xcb, c);
@ -101,6 +104,7 @@ impl XcbCon {
input,
dri,
present,
render,
input_opcode: 0,
present_opcode: 0,
xkb,
@ -139,6 +143,13 @@ impl XcbCon {
);
con.errors.check(&con.xcb, res, err)?;
let res = con.render.xcb_render_query_version_reply(
c,
con.render.xcb_render_query_version(c, 0, 8),
&mut err,
);
con.errors.check(&con.xcb, res, err)?;
let present_ex = con
.xcb
.xcb_get_extension_data(con.c, con.present.xcb_present_id());
@ -196,6 +207,7 @@ pub struct XorgBackend {
mouse_seats: CopyHashMap<ffi::xcb_input_device_id_t, Rc<XorgSeat>>,
ctx: Rc<RenderContext>,
gbm: GbmDevice,
cursor: ffi::xcb_cursor_t,
r: Cell<f32>,
g: Cell<f32>,
b: Cell<f32>,
@ -233,6 +245,28 @@ impl XorgBackend {
let wheel_id = state.wheel.id();
let cursor = {
let ctx = XcbCursorContext::new(&con.xcb, &con.render, con.c);
let image = XcbCursorImage {
width: 1,
height: 1,
xhot: 0,
yhot: 0,
delay: 0,
pixels: vec![0],
..Default::default()
};
let cursor =
ctx.create_cursor(&con.xcb, &con.render, slice::from_ref(&image));
match cursor {
Ok(c) => c,
Err(e) => {
log::error!("Could not create empty cursor: {}", e);
0
}
}
};
let slf = Rc::new(Self {
id: state.el.id(),
wheel_id,
@ -243,6 +277,7 @@ impl XorgBackend {
mouse_seats: Default::default(),
ctx: ctx.clone(),
gbm,
cursor,
r: Cell::new(0.0),
g: Cell::new(0.0),
b: Cell::new(0.0),
@ -394,11 +429,12 @@ impl XorgBackend {
let event_mask = ffi::XCB_EVENT_MASK_EXPOSURE
| ffi::XCB_EVENT_MASK_STRUCTURE_NOTIFY
| ffi::XCB_EVENT_MASK_VISIBILITY_CHANGE;
let args = [event_mask, self.cursor];
let cookie = con.xcb.xcb_change_window_attributes_checked(
con.c,
window_id,
ffi::XCB_CW_EVENT_MASK,
&event_mask as *const _ as _,
ffi::XCB_CW_EVENT_MASK | ffi::XCB_CW_CURSOR,
args.as_ptr() as _,
);
if let Err(e) = con.check_cookie(cookie) {
return Err(XorgBackendError::WindowEvents(e));
@ -649,7 +685,7 @@ impl XorgBackend {
if let Some(node) = self.state.root.outputs.get(&output.id) {
let fb = image.fb.get();
fb.render(&*node);
fb.render(&*node, &self.state, Some(node.position.get()));
}
unsafe {
@ -710,7 +746,8 @@ impl XorgBackend {
let event =
unsafe { (event as *const _ as *const ffi::xcb_input_button_press_event_t).deref() };
if let Some(seat) = self.mouse_seats.get(&event.deviceid) {
let button = seat.button_map.get(&event.detail).unwrap_or(event.detail);
let button = event.detail;
// let button = seat.button_map.get(&event.detail).unwrap_or(event.detail);
if matches!(button, 4..=7) {
if state == KeyState::Pressed {
let (axis, val) = match button {

View file

@ -12,6 +12,7 @@ pub struct Format {
pub gl_type: GLint,
pub drm: u32,
pub wl_id: Option<u32>,
pub has_alpha: bool,
}
static FORMATS_MAP: Lazy<AHashMap<u32, &'static Format>> = Lazy::new(|| {
@ -58,6 +59,7 @@ pub static FORMATS: &[Format] = &[
gl_type: GL_UNSIGNED_BYTE,
drm: ARGB8888_DRM,
wl_id: Some(ARGB8888_ID),
has_alpha: true,
},
Format {
name: "xrgb8888",
@ -67,6 +69,7 @@ pub static FORMATS: &[Format] = &[
gl_type: GL_UNSIGNED_BYTE,
drm: XRGB8888_DRM,
wl_id: Some(XRGB8888_ID),
has_alpha: false,
},
// Format {
// id: fourcc_code('C', '8', ' ', ' '),

View file

@ -1,3 +1,4 @@
use std::cell::{RefMut};
use crate::client::{Client, DynEventFormatter};
use crate::ifs::org_kde_kwin_server_decoration_manager::{
OrgKdeKwinServerDecorationManagerError, OrgKdeKwinServerDecorationManagerGlobal,
@ -20,6 +21,7 @@ use crate::{
};
use std::fmt::{Display, Formatter};
use std::rc::Rc;
use ahash::AHashMap;
use thiserror::Error;
#[derive(Debug, Error)]
@ -101,6 +103,7 @@ pub struct Globals {
next_name: NumCell<u32>,
registry: CopyHashMap<GlobalName, Rc<dyn Global>>,
outputs: CopyHashMap<GlobalName, Rc<WlOutputGlobal>>,
seats: CopyHashMap<GlobalName, Rc<WlSeatGlobal>>,
}
impl Globals {
@ -109,6 +112,7 @@ impl Globals {
next_name: NumCell::new(1),
registry: CopyHashMap::new(),
outputs: Default::default(),
seats: Default::default(),
}
}
@ -143,6 +147,10 @@ impl Globals {
Ok(())
}
pub fn lock_seats(&self) -> RefMut<AHashMap<GlobalName, Rc<WlSeatGlobal>>> {
self.seats.lock()
}
pub fn notify_all(&self, client: &Rc<Client>, registry: &Rc<WlRegistry>) {
let globals = self.registry.lock();
macro_rules! emit {
@ -215,7 +223,6 @@ macro_rules! simple_add_global {
};
}
simple_add_global!(WlSeatGlobal);
simple_add_global!(WlCompositorGlobal);
simple_add_global!(WlShmGlobal);
simple_add_global!(WlSubcompositorGlobal);
@ -247,3 +254,4 @@ macro_rules! dedicated_add_global {
}
dedicated_add_global!(WlOutputGlobal, outputs);
dedicated_add_global!(WlSeatGlobal, seats);

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);
}
}
}
}

View file

@ -76,7 +76,8 @@ mod xkbcommon;
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
.filter_level(LevelFilter::Inf)
// .filter_level(LevelFilter::Trace)
.init();
if let Err(e) = main_() {
log::error!("A fatal error occurred: {}", ErrorFmt(e));

View file

@ -40,6 +40,9 @@ pub const GL_TRIANGLES: GLenum = 0x0004;
pub const GL_UNPACK_ROW_LENGTH_EXT: GLenum = 0x0CF2;
pub const GL_UNSIGNED_BYTE: GLint = 0x1401;
pub const GL_VERTEX_SHADER: GLenum = 0x8B31;
pub const GL_BLEND : GLenum = 0x0BE2;
pub const GL_ONE : GLenum = 1;
pub const GL_ONE_MINUS_SRC_ALPHA : GLenum = 0x0303;
#[link(name = "GLESv2")]
extern "C" {
@ -66,6 +69,7 @@ extern "C" {
);
pub fn glCheckFramebufferStatus(target: GLenum) -> GLenum;
pub fn glClear(mask: GLbitfield);
pub fn glBlendFunc (sfactor: GLenum, dfactor: GLenum);
pub fn glClearColor(red: GLfloat, green: GLfloat, blue: GLfloat, alpha: GLfloat);
#[allow(dead_code)]
pub fn glFlush();

View file

@ -18,6 +18,24 @@ use std::ffi::CString;
use std::rc::Rc;
use uapi::ustr;
pub(super) struct TexProg {
pub(super) prog: GlProgram,
pub(super) pos: GLint,
pub(super) texcoord: GLint,
pub(super) tex: GLint,
}
impl TexProg {
unsafe fn from(prog: GlProgram) -> Self {
Self {
pos: prog.get_attrib_location(ustr!("pos")),
texcoord: prog.get_attrib_location(ustr!("texcoord")),
tex: prog.get_uniform_location(ustr!("tex")),
prog,
}
}
}
pub struct RenderContext {
pub(super) ctx: Rc<EglContext>,
@ -25,10 +43,8 @@ pub struct RenderContext {
pub(super) renderdoc: Option<RefCell<RenderDoc<V100>>>,
pub(super) tex_prog: GlProgram,
pub(super) tex_prog_pos: GLint,
pub(super) tex_prog_texcoord: GLint,
pub(super) tex_prog_tex: GLint,
pub(super) tex_prog: TexProg,
pub(super) tex_alpha_prog: TexProg,
pub(super) fill_prog: GlProgram,
pub(super) fill_prog_pos: GLint,
@ -60,6 +76,11 @@ impl RenderContext {
include_str!("../shaders/tex.vert.glsl"),
include_str!("../shaders/tex.frag.glsl"),
)?;
let tex_alpha_prog = GlProgram::from_shaders(
ctx,
include_str!("../shaders/tex.vert.glsl"),
include_str!("../shaders/tex-alpha.frag.glsl"),
)?;
let fill_prog = GlProgram::from_shaders(
ctx,
include_str!("../shaders/fill.vert.glsl"),
@ -70,10 +91,8 @@ impl RenderContext {
render_node: node.clone(),
tex_prog_pos: tex_prog.get_attrib_location(ustr!("pos")),
tex_prog_texcoord: tex_prog.get_attrib_location(ustr!("texcoord")),
tex_prog_tex: tex_prog.get_uniform_location(ustr!("tex")),
tex_prog,
tex_prog: TexProg::from(tex_prog),
tex_alpha_prog: TexProg::from(tex_alpha_prog),
fill_prog_pos: fill_prog.get_attrib_location(ustr!("pos")),
fill_prog_color: fill_prog.get_uniform_location(ustr!("color")),

View file

@ -7,6 +7,9 @@ use crate::render::renderer::renderer::Renderer;
use crate::tree::Node;
use std::ptr;
use std::rc::Rc;
use crate::rect::Rect;
use crate::render::sys::{GL_ONE, GL_ONE_MINUS_SRC_ALPHA, glBlendFunc};
use crate::State;
pub struct Framebuffer {
pub(super) ctx: Rc<RenderContext>,
@ -14,7 +17,7 @@ pub struct Framebuffer {
}
impl Framebuffer {
pub fn render(&self, node: &dyn Node) {
pub fn render(&self, node: &dyn Node, state: &State, cursor_rect: Option<Rect>) {
let _ = self.ctx.ctx.with_current(|| {
if let Some(rd) = &self.ctx.renderdoc {
rd.borrow_mut()
@ -25,12 +28,25 @@ impl Framebuffer {
glViewport(0, 0, self.gl.width, self.gl.height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
let mut renderer = Renderer {
ctx: &self.ctx,
fb: &self.gl,
};
node.render(&mut renderer, 0, 0);
if let Some(rect) = cursor_rect {
let seats = state.globals.lock_seats();
for seat in seats.values() {
if let Some(cursor) = seat.get_cursor() {
let extents = cursor.extents();
if extents.intersects(&rect) {
let (x, y) = rect.translate(extents.x1(), extents.y1());
renderer.render_surface(cursor.surface(), x, y);
}
}
}
}
if let Some(rd) = &self.ctx.renderdoc {
rd.borrow_mut().end_frame_capture(ptr::null(), ptr::null());
}

View file

@ -17,6 +17,7 @@ use crate::tree::{
use std::ops::Deref;
use std::rc::Rc;
use std::slice;
use crate::render::sys::{GL_BLEND, glDisable, glEnable};
const NON_COLOR: (f32, f32, f32) = (0.2, 0.2, 0.2);
const CHILD_COLOR: (f32, f32, f32) = (0.8, 0.8, 0.8);
@ -47,7 +48,7 @@ impl Renderer<'_> {
self.render_container(&node, x, y)
}
for stacked in workspace.stacked.iter() {
let pos = stacked.absolute_position();
let (pos, _) = stacked.absolute_position();
stacked.render(self, pos.x1(), pos.y1());
}
}
@ -186,7 +187,9 @@ impl Renderer<'_> {
let buffer = match surface.buffer.get() {
Some(b) => b,
_ => {
log::warn!("surface has no buffer attached");
if !surface.is_cursor() {
log::warn!("surface has no buffer attached");
}
return;
}
};
@ -226,9 +229,20 @@ impl Renderer<'_> {
glBindTexture(GL_TEXTURE_2D, texture.gl.tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glUseProgram(self.ctx.tex_prog.prog);
let prog = match buffer.format.has_alpha {
true => {
glEnable(GL_BLEND);
&self.ctx.tex_alpha_prog
},
false => {
glDisable(GL_BLEND);
&self.ctx.tex_prog
},
};
glUniform1i(self.ctx.tex_prog_tex, 0);
glUseProgram(prog.prog.prog);
glUniform1i(prog.tex, 0);
let texcoord: [f32; 8] = [1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0];
@ -248,7 +262,7 @@ impl Renderer<'_> {
];
glVertexAttribPointer(
self.ctx.tex_prog_texcoord as _,
prog.texcoord as _,
2,
GL_FLOAT,
GL_FALSE,
@ -256,7 +270,7 @@ impl Renderer<'_> {
texcoord.as_ptr() as _,
);
glVertexAttribPointer(
self.ctx.tex_prog_pos as _,
prog.pos as _,
2,
GL_FLOAT,
GL_FALSE,
@ -264,13 +278,13 @@ impl Renderer<'_> {
pos.as_ptr() as _,
);
glEnableVertexAttribArray(self.ctx.tex_prog_texcoord as _);
glEnableVertexAttribArray(self.ctx.tex_prog_pos as _);
glEnableVertexAttribArray(prog.texcoord as _);
glEnableVertexAttribArray(prog.pos as _);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(self.ctx.tex_prog_texcoord as _);
glDisableVertexAttribArray(self.ctx.tex_prog_pos as _);
glDisableVertexAttribArray(prog.texcoord as _);
glDisableVertexAttribArray(prog.pos as _);
glBindTexture(GL_TEXTURE_2D, 0);
}

View file

@ -0,0 +1,7 @@
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;
void main() {
gl_FragColor = texture2D(tex, v_texcoord);
}

View file

@ -20,14 +20,16 @@ impl OutputHandler {
let ae = ae.clone();
self.output.on_change(Rc::new(move || ae.trigger()));
}
let name = self.state.globals.name();
let global = Rc::new(WlOutputGlobal::new(name, &self.output));
let on = Rc::new(OutputNode {
display: self.state.root.clone(),
id: self.state.node_ids.next(),
backend: self.output.clone(),
workspaces: RefCell::new(vec![]),
position: Cell::new(Default::default()),
workspace: CloneCell::new(None),
seat_state: Default::default(),
global: global.clone(),
});
let workspace = Rc::new(WorkspaceNode {
id: self.state.node_ids.next(),
@ -38,8 +40,6 @@ impl OutputHandler {
});
on.workspace.set(Some(workspace));
self.state.root.outputs.set(self.output.id(), on.clone());
let name = self.state.globals.name();
let global = Rc::new(WlOutputGlobal::new(name, &self.output));
self.state.add_global(&global);
self.state.outputs.set(self.output.id(), global.clone());
let mut width = 0;

View file

@ -39,6 +39,7 @@ impl SeatHandler {
}
ae.triggered().await;
}
global.set_cursor(None);
let _ = self.state.globals.remove(&self.state, name);
self.state.seats.borrow_mut().remove(&self.seat.id());
}

View file

@ -343,7 +343,6 @@ impl Node for ContainerNode {
}
fn child_size_changed(&self, child: &dyn Node, width: i32, height: i32) {
log::info!("child size changed {}x{}", width, height);
let cn = self.child_nodes.borrow();
if let Some(node) = cn.get(&child.id()) {
let rect = Rect::new(0, 0, width, height).unwrap();

View file

@ -1,4 +1,4 @@
use crate::backend::{KeyState, Output, OutputId, ScrollAxis};
use crate::backend::{KeyState, OutputId, ScrollAxis};
use crate::fixed::Fixed;
use crate::ifs::wl_seat::{NodeSeatState, WlSeatGlobal};
use crate::rect::Rect;
@ -13,6 +13,9 @@ use std::fmt::Display;
use std::ops::Deref;
use std::rc::Rc;
pub use workspace::*;
use crate::client::ClientId;
use crate::ifs::wl_output::WlOutputGlobal;
use crate::xkbcommon::ModifierState;
mod container;
mod workspace;
@ -59,7 +62,7 @@ pub enum FindTreeResult {
pub trait AbsoluteNode: Node {
fn into_node(self: Rc<Self>) -> Rc<dyn Node>;
fn absolute_position(&self) -> Rect;
fn absolute_position(&self) -> (Rect, bool);
}
pub trait Node {
@ -67,6 +70,17 @@ pub trait Node {
fn seat_state(&self) -> &NodeSeatState;
fn destroy_node(&self, detach: bool);
fn active_changed(&self, active: bool) {
let _ = active;
}
fn key(&self, seat: &WlSeatGlobal, key: u32, state: u32, mods: Option<ModifierState>) {
let _ = seat;
let _ = key;
let _ = state;
let _ = mods;
}
fn button(self: Rc<Self>, seat: &Rc<WlSeatGlobal>, button: u32, state: KeyState) {
let _ = seat;
let _ = button;
@ -143,6 +157,10 @@ pub trait Node {
fn set_workspace(self: Rc<Self>, ws: &Rc<WorkspaceNode>) {
let _ = ws;
}
fn client_id(&self) -> Option<ClientId> {
None
}
}
pub struct FoundNode {
@ -193,9 +211,10 @@ impl Node for DisplayNode {
}
fn find_tree_at(&self, x: i32, y: i32, tree: &mut Vec<FoundNode>) -> FindTreeResult {
for stacked in self.stacked.iter() {
let ext = stacked.absolute_position();
if !ext.contains(x, y) {
for stacked in self.stacked.rev_iter() {
let (ext, constrain) = stacked.absolute_position();
if constrain && !ext.contains(x, y) {
// TODO: make constrain always true
continue;
}
let (x, y) = ext.translate(x, y);
@ -206,7 +225,9 @@ impl Node for DisplayNode {
y,
});
match stacked.find_tree_at(x, y, tree) {
FindTreeResult::AcceptsInput => return FindTreeResult::AcceptsInput,
FindTreeResult::AcceptsInput => {
return FindTreeResult::AcceptsInput;
},
FindTreeResult::Other => {
tree.drain(idx..);
}
@ -235,7 +256,7 @@ pub struct OutputNode {
pub display: Rc<DisplayNode>,
pub id: OutputNodeId,
pub position: Cell<Rect>,
pub backend: Rc<dyn Output>,
pub global: Rc<WlOutputGlobal>,
pub workspaces: RefCell<Vec<Rc<WorkspaceNode>>>,
pub workspace: CloneCell<Option<Rc<WorkspaceNode>>>,
pub seat_state: NodeSeatState,
@ -307,8 +328,8 @@ impl AbsoluteNode for FloatNode {
self
}
fn absolute_position(&self) -> Rect {
self.position.get()
fn absolute_position(&self) -> (Rect, bool) {
(self.position.get(), true)
}
}

View file

@ -28,4 +28,4 @@ pub enum BufFdError {
const BUF_SIZE: usize = 4096;
const CMSG_BUF_SIZE: usize = 4096;
const MAX_IN_FD: usize = 4;
const MAX_IN_FD: usize = 32;

View file

@ -21,9 +21,9 @@ impl<T: UnsafeCellCloneSafe> CloneCell<T> {
}
#[inline(always)]
pub fn set(&self, t: T) {
pub fn set(&self, t: T) -> T {
unsafe {
let _ = mem::replace(self.data.get().deref_mut(), t);
mem::replace(self.data.get().deref_mut(), t)
}
}

View file

@ -23,6 +23,12 @@ impl<K: Eq, V, const N: usize> SmallMap<K, V, N> {
}
}
pub fn len(&self) -> usize {
unsafe {
self.m.get().deref().len()
}
}
pub fn insert(&self, k: K, v: V) -> Option<V> {
unsafe {
let m = self.m.get().deref_mut();