about to breka everything

This commit is contained in:
Janis 2022-05-07 16:14:54 +02:00 committed by Gitea
parent fb011ea23f
commit 85d3c3ce79
2 changed files with 135 additions and 48 deletions

View file

@ -31,6 +31,7 @@ pub trait WindowServerBackend {
fn screen_size(&self) -> Size<i32>; fn screen_size(&self) -> Size<i32>;
fn get_window_size(&self, window: Self::Window) -> Option<Size<i32>>; fn get_window_size(&self, window: Self::Window) -> Option<Size<i32>>;
fn get_window_name(&self, window: Self::Window) -> Option<String>;
fn grab_cursor(&self); fn grab_cursor(&self);
fn ungrab_cursor(&self); fn ungrab_cursor(&self);

View file

@ -14,9 +14,14 @@ use crate::backends::{
keycodes::KeyOrButton, xlib::keysym::mouse_button_to_xbutton, keycodes::KeyOrButton, xlib::keysym::mouse_button_to_xbutton,
}; };
use self::keysym::{ use self::{
keysym_to_virtual_keycode, virtual_keycode_to_keysym, xev_to_mouse_button, connection::XLibConnection,
XKeySym, ewmh::EWMHAtoms,
keysym::{
keysym_to_virtual_keycode, virtual_keycode_to_keysym,
xev_to_mouse_button, XKeySym,
},
wmh::ICCCMAtoms,
}; };
use super::{ use super::{
@ -103,6 +108,92 @@ impl From<u8> for XlibError {
} }
} }
pub mod wmh {
use std::{borrow::Borrow, ffi::CString, ops::Index, os::raw::c_long};
use strum::{EnumCount, EnumIter};
use x11::xlib::{Atom, XA_ATOM};
use super::{
connection::{PropMode, XLibConnection},
Display,
};
#[derive(Debug, PartialEq, Eq, EnumIter, EnumCount, Clone, Copy)]
pub enum ICCCMAtom {
WmName,
WmProtocols,
WmDeleteWindow,
WmActiveWindow,
WmTakeFocus,
WmState,
}
#[derive(Debug, Clone)]
pub struct ICCCMAtoms {
inner: Vec<Atom>,
}
impl Index<ICCCMAtom> for ICCCMAtoms {
type Output = Atom;
fn index(&self, index: ICCCMAtom) -> &Self::Output {
&self.inner[usize::from(index)]
}
}
impl ICCCMAtoms {
pub fn from_connection<C: Borrow<XLibConnection>>(
con: C,
) -> Option<Self> {
ICCCMAtom::try_get_atoms(con.borrow().display())
.map(|atoms| Self { inner: atoms })
}
}
impl ICCCMAtom {
pub fn try_get_atoms(display: Display) -> Option<Vec<Atom>> {
use strum::IntoEnumIterator;
Self::iter()
.map(|atom| atom.try_into_x_atom(&display))
.collect::<Option<Vec<_>>>()
}
fn try_into_x_atom(self, display: &Display) -> Option<Atom> {
let name = CString::new::<&str>(self.into()).ok()?;
match unsafe {
x11::xlib::XInternAtom(
display.get(),
name.as_c_str().as_ptr(),
0,
)
} {
0 => None,
atom => Some(atom),
}
}
}
impl From<ICCCMAtom> for usize {
fn from(atom: ICCCMAtom) -> Self {
atom as usize
}
}
impl From<ICCCMAtom> for &str {
fn from(atom: ICCCMAtom) -> Self {
match atom {
ICCCMAtom::WmName => "WM_NAME",
ICCCMAtom::WmProtocols => "WM_PROTOCOLS",
ICCCMAtom::WmDeleteWindow => "WM_DELETE_WINDOW",
ICCCMAtom::WmActiveWindow => "WM_ACTIVE_WINDOW",
ICCCMAtom::WmTakeFocus => "WM_TAKE_FOCUS",
ICCCMAtom::WmState => "WM_STATE",
}
}
}
}
pub mod ewmh { pub mod ewmh {
use std::{borrow::Borrow, ffi::CString, ops::Index, os::raw::c_long}; use std::{borrow::Borrow, ffi::CString, ops::Index, os::raw::c_long};
@ -500,38 +591,26 @@ impl Display {
} }
pub struct XLib { pub struct XLib {
display: Display, connection: Rc<XLibConnection>,
root: Window, //atoms: XLibAtoms,
screen: i32, icccm_atoms: ICCCMAtoms,
atoms: XLibAtoms, ewmh_atoms: EWMHAtoms,
keybinds: Vec<KeyOrMouseBind>, keybinds: Vec<KeyOrMouseBind>,
active_border_color: Option<color::XftColor>, active_border_color: Option<color::XftColor>,
inactive_border_color: Option<color::XftColor>, inactive_border_color: Option<color::XftColor>,
} }
impl Drop for XLib {
fn drop(&mut self) {
self.close_dpy();
}
}
impl XLib { impl XLib {
fn new() -> Self { fn new() -> Self {
let (display, screen, root) = { let con =
let display = Display::open().expect("failed to open x display"); Rc::new(XLibConnection::new().expect("failed to open x display"));
let screen = unsafe { xlib::XDefaultScreen(display.get()) };
let root = unsafe { xlib::XRootWindow(display.get(), screen) };
(display, screen, root)
};
let atoms = XLibAtoms::init(display.clone());
Self { Self {
display, connection: con.clone(),
screen, icccm_atoms: ICCCMAtoms::from_connection(con.clone())
root, .expect("atoms"),
atoms, ewmh_atoms: EWMHAtoms::from_connection(con.clone())
.expect("ewmh atoms"),
keybinds: Vec::new(), keybinds: Vec::new(),
active_border_color: None, active_border_color: None,
inactive_border_color: None, inactive_border_color: None,
@ -551,25 +630,24 @@ impl XLib {
| xlib::ButtonPressMask; | xlib::ButtonPressMask;
xlib::XChangeWindowAttributes( xlib::XChangeWindowAttributes(
self.dpy(), self.connection.dpy(),
self.root, self.connection.root(),
xlib::CWEventMask, xlib::CWEventMask,
&mut window_attributes, &mut window_attributes,
); );
xlib::XSelectInput(self.dpy(), self.root, window_attributes.event_mask); xlib::XSelectInput(
self.dpy(),
self.connection.root(),
window_attributes.event_mask,
);
xlib::XSetErrorHandler(Some(xlib_error_handler)); xlib::XSetErrorHandler(Some(xlib_error_handler));
xlib::XSync(self.dpy(), 0); xlib::XSync(self.dpy(), 0);
} }
#[deprecated = "use `self.connection.dpy()` instead"]
fn dpy(&self) -> *mut xlib::Display { fn dpy(&self) -> *mut xlib::Display {
self.display.get() self.connection.dpy()
}
fn close_dpy(&self) {
unsafe {
xlib::XCloseDisplay(self.dpy());
}
} }
fn next_xevent(&mut self) -> XEvent { fn next_xevent(&mut self) -> XEvent {
@ -1119,7 +1197,7 @@ impl WindowServerBackend for XLib {
} }
fn add_keybind(&mut self, keybind: super::window_event::KeyOrMouseBind) { fn add_keybind(&mut self, keybind: super::window_event::KeyOrMouseBind) {
self.grab_key_or_button(&keybind, self.root); self.grab_key_or_button(&keybind, self.connection.root());
self.keybinds.push(keybind); self.keybinds.push(keybind);
} }
@ -1154,7 +1232,7 @@ impl WindowServerBackend for XLib {
xlib::XChangeProperty( xlib::XChangeProperty(
self.dpy(), self.dpy(),
self.root, self.connection.root(),
self.atoms.wm_active_window, self.atoms.wm_active_window,
xlib::XA_WINDOW, xlib::XA_WINDOW,
32, 32,
@ -1171,7 +1249,7 @@ impl WindowServerBackend for XLib {
unsafe { unsafe {
xlib::XSetInputFocus( xlib::XSetInputFocus(
self.dpy(), self.dpy(),
self.root, self.connection.root(),
xlib::RevertToPointerRoot, xlib::RevertToPointerRoot,
xlib::CurrentTime, xlib::CurrentTime,
); );
@ -1193,7 +1271,7 @@ impl WindowServerBackend for XLib {
xlib::XDeleteProperty( xlib::XDeleteProperty(
self.dpy(), self.dpy(),
self.root, self.connection.root(),
self.atoms.wm_active_window, self.atoms.wm_active_window,
); );
} }
@ -1274,7 +1352,11 @@ impl WindowServerBackend for XLib {
let mut wa = let mut wa =
std::mem::MaybeUninit::<xlib::XWindowAttributes>::zeroed(); std::mem::MaybeUninit::<xlib::XWindowAttributes>::zeroed();
xlib::XGetWindowAttributes(self.dpy(), self.root, wa.as_mut_ptr()); xlib::XGetWindowAttributes(
self.dpy(),
self.connection.root(),
wa.as_mut_ptr(),
);
let wa = wa.assume_init(); let wa = wa.assume_init();
@ -1291,7 +1373,7 @@ impl WindowServerBackend for XLib {
unsafe { unsafe {
xlib::XGrabPointer( xlib::XGrabPointer(
self.dpy(), self.dpy(),
self.root, self.connection.root(),
0, 0,
(xlib::ButtonPressMask (xlib::ButtonPressMask
| xlib::ButtonReleaseMask | xlib::ButtonReleaseMask
@ -1316,7 +1398,7 @@ impl WindowServerBackend for XLib {
xlib::XWarpPointer( xlib::XWarpPointer(
self.dpy(), self.dpy(),
0, 0,
window.unwrap_or(self.root), window.unwrap_or(self.connection.root()),
0, 0,
0, 0,
0, 0,
@ -1336,7 +1418,7 @@ impl WindowServerBackend for XLib {
unsafe { unsafe {
xlib::XQueryTree( xlib::XQueryTree(
self.dpy(), self.dpy(),
self.root, self.connection.root(),
&mut root, &mut root,
&mut parent, &mut parent,
&mut children, &mut children,
@ -1357,8 +1439,8 @@ impl WindowServerBackend for XLib {
fn set_active_window_border_color(&mut self, color_name: &str) { fn set_active_window_border_color(&mut self, color_name: &str) {
self.active_border_color = color::XftColor::new( self.active_border_color = color::XftColor::new(
self.display.clone(), self.connection.display(),
self.screen, self.connection.screen(),
color_name.to_owned(), color_name.to_owned(),
) )
.ok(); .ok();
@ -1366,12 +1448,16 @@ impl WindowServerBackend for XLib {
fn set_inactive_window_border_color(&mut self, color_name: &str) { fn set_inactive_window_border_color(&mut self, color_name: &str) {
self.inactive_border_color = color::XftColor::new( self.inactive_border_color = color::XftColor::new(
self.display.clone(), self.connection.display(),
self.screen, self.connection.screen(),
color_name.to_owned(), color_name.to_owned(),
) )
.ok(); .ok();
} }
fn get_window_name(&self, window: Self::Window) -> Option<String> {
todo!()
}
} }
#[allow(dead_code)] #[allow(dead_code)]