seems to work now :^)
This commit is contained in:
parent
2b4ddc8b5a
commit
b47f245250
|
@ -18,7 +18,7 @@ pub enum WindowEvent<Window> {
|
||||||
FullscreenEvent(FullscreenEvent<Window>), //1 { window: Window, event: 1 },
|
FullscreenEvent(FullscreenEvent<Window>), //1 { window: Window, event: 1 },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||||
pub enum KeyState {
|
pub enum KeyState {
|
||||||
Pressed,
|
Pressed,
|
||||||
Released,
|
Released,
|
||||||
|
@ -55,7 +55,7 @@ impl<const N: usize> From<[ModifierKey; N]> for ModifierState {
|
||||||
fn from(slice: [ModifierKey; N]) -> Self {
|
fn from(slice: [ModifierKey; N]) -> Self {
|
||||||
let mut state = ModifierState::empty();
|
let mut state = ModifierState::empty();
|
||||||
for ele in slice {
|
for ele in slice {
|
||||||
state.set_mod(ele);
|
state.insert_mod(ele);
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
state
|
||||||
|
@ -69,32 +69,31 @@ impl ModifierState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mod(mut self, modifier: ModifierKey) -> Self {
|
pub fn with_mod(mut self, modifier: ModifierKey) -> Self {
|
||||||
self.set_mod(modifier);
|
self.insert_mod(modifier);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unset_mod(&mut self, modifier: ModifierKey) {
|
pub fn unset_mod(&mut self, modifier: ModifierKey) {
|
||||||
match modifier {
|
self.set_mod(modifier, false);
|
||||||
ModifierKey::Shift => self.remove(Self::SHIFT),
|
|
||||||
ModifierKey::ShiftLock => self.remove(Self::SHIFT_LOCK),
|
|
||||||
ModifierKey::Control => self.remove(Self::CONTROL),
|
|
||||||
ModifierKey::Alt => self.remove(Self::ALT),
|
|
||||||
ModifierKey::AltGr => self.remove(Self::ALT_GR),
|
|
||||||
ModifierKey::Super => self.remove(Self::SUPER),
|
|
||||||
ModifierKey::NumLock => self.remove(Self::NUM_LOCK),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_mod(&mut self, modifier: ModifierKey) {
|
pub fn set_mod(&mut self, modifier: ModifierKey, state: bool) {
|
||||||
match modifier {
|
self.set(
|
||||||
ModifierKey::Shift => self.insert(Self::SHIFT),
|
match modifier {
|
||||||
ModifierKey::ShiftLock => self.insert(Self::SHIFT_LOCK),
|
ModifierKey::Shift => Self::SHIFT,
|
||||||
ModifierKey::Control => self.insert(Self::CONTROL),
|
ModifierKey::ShiftLock => Self::SHIFT_LOCK,
|
||||||
ModifierKey::Alt => self.insert(Self::ALT),
|
ModifierKey::Control => Self::CONTROL,
|
||||||
ModifierKey::AltGr => self.insert(Self::ALT_GR),
|
ModifierKey::Alt => Self::ALT,
|
||||||
ModifierKey::Super => self.insert(Self::SUPER),
|
ModifierKey::AltGr => Self::ALT_GR,
|
||||||
ModifierKey::NumLock => self.insert(Self::NUM_LOCK),
|
ModifierKey::Super => Self::SUPER,
|
||||||
}
|
ModifierKey::NumLock => Self::NUM_LOCK,
|
||||||
|
},
|
||||||
|
state,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_mod(&mut self, modifier: ModifierKey) {
|
||||||
|
self.set_mod(modifier, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,7 +293,7 @@ impl KeyBind {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
||||||
self.modifiers.set_mod(modifier_key);
|
self.modifiers.insert_mod(modifier_key);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -314,7 +313,7 @@ impl MouseBind {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
||||||
self.modifiers.set_mod(modifier_key);
|
self.modifiers.insert_mod(modifier_key);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -334,7 +333,7 @@ impl KeyOrMouseBind {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
pub fn with_mod(mut self, modifier_key: ModifierKey) -> Self {
|
||||||
self.modifiers.set_mod(modifier_key);
|
self.modifiers.insert_mod(modifier_key);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#![allow(unused_variables, dead_code)]
|
#![allow(unused_variables, dead_code)]
|
||||||
use log::{error, warn};
|
use log::{debug, error, warn};
|
||||||
use std::{ffi::CString, rc::Rc};
|
use std::{ffi::CString, rc::Rc};
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -13,13 +13,17 @@ use crate::backends::{
|
||||||
xlib::keysym::mouse_button_to_xbutton,
|
xlib::keysym::mouse_button_to_xbutton,
|
||||||
};
|
};
|
||||||
|
|
||||||
use self::keysym::{virtual_keycode_to_keysym, xev_to_mouse_button, XKeySym};
|
use self::keysym::{
|
||||||
|
keysym_to_virtual_keycode, virtual_keycode_to_keysym, xev_to_mouse_button,
|
||||||
|
XKeySym,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
keycodes::VirtualKeyCode,
|
keycodes::VirtualKeyCode,
|
||||||
window_event::{
|
window_event::{
|
||||||
ButtonEvent, ConfigureEvent, DestroyEvent, EnterEvent, KeyOrMouseBind,
|
ButtonEvent, ConfigureEvent, DestroyEvent, EnterEvent, KeyEvent,
|
||||||
KeyState, MapEvent, ModifierState, Point, UnmapEvent, WindowEvent,
|
KeyOrMouseBind, KeyState, MapEvent, ModifierState, MotionEvent, Point,
|
||||||
|
UnmapEvent, WindowEvent,
|
||||||
},
|
},
|
||||||
WindowServerBackend,
|
WindowServerBackend,
|
||||||
};
|
};
|
||||||
|
@ -152,7 +156,7 @@ impl XLib {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
let (display, screen, root) = {
|
let (display, screen, root) = {
|
||||||
let display = unsafe { xlib::XOpenDisplay(std::ptr::null()) };
|
let display = unsafe { xlib::XOpenDisplay(std::ptr::null()) };
|
||||||
assert_eq!(display, std::ptr::null_mut());
|
assert_ne!(display, std::ptr::null_mut());
|
||||||
let screen = unsafe { xlib::XDefaultScreen(display) };
|
let screen = unsafe { xlib::XDefaultScreen(display) };
|
||||||
let root = unsafe { xlib::XRootWindow(display, screen) };
|
let root = unsafe { xlib::XRootWindow(display, screen) };
|
||||||
|
|
||||||
|
@ -215,14 +219,14 @@ impl XLib {
|
||||||
event.assume_init()
|
event.assume_init()
|
||||||
};
|
};
|
||||||
|
|
||||||
match event.get_type() {
|
// match event.get_type() {
|
||||||
xlib::KeyPress | xlib::KeyRelease => {
|
// xlib::KeyPress | xlib::KeyRelease => {
|
||||||
self.update_modifier_state(AsRef::<xlib::XKeyEvent>::as_ref(
|
// self.update_modifier_state(AsRef::<xlib::XKeyEvent>::as_ref(
|
||||||
&event,
|
// &event,
|
||||||
));
|
// ));
|
||||||
}
|
// }
|
||||||
_ => {}
|
// _ => {}
|
||||||
}
|
// }
|
||||||
|
|
||||||
event
|
event
|
||||||
}
|
}
|
||||||
|
@ -261,27 +265,51 @@ impl XLib {
|
||||||
window: ev.window,
|
window: ev.window,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
xlib::MotionNotify => {
|
||||||
|
let ev = unsafe { &event.motion };
|
||||||
|
Some(XLibWindowEvent::MotionEvent(MotionEvent {
|
||||||
|
position: (ev.x, ev.y).into(),
|
||||||
|
window: ev.window,
|
||||||
|
}))
|
||||||
|
}
|
||||||
// both ButtonPress and ButtonRelease use the XButtonEvent structure, aliased as either
|
// both ButtonPress and ButtonRelease use the XButtonEvent structure, aliased as either
|
||||||
// XButtonReleasedEvent or XButtonPressedEvent
|
// XButtonReleasedEvent or XButtonPressedEvent
|
||||||
xlib::ButtonPress | xlib::ButtonRelease => {
|
xlib::ButtonPress | xlib::ButtonRelease => {
|
||||||
let ev = unsafe { &event.button };
|
let ev = unsafe { &event.button };
|
||||||
let keycode = xev_to_mouse_button(ev).unwrap();
|
let keycode = xev_to_mouse_button(ev).unwrap();
|
||||||
let state = if ev.state as i32 == xlib::ButtonPress {
|
let state = if ev.type_ == xlib::ButtonPress {
|
||||||
KeyState::Pressed
|
KeyState::Pressed
|
||||||
} else {
|
} else {
|
||||||
KeyState::Released
|
KeyState::Released
|
||||||
};
|
};
|
||||||
|
|
||||||
let modifierstate = ModifierState::empty();
|
|
||||||
|
|
||||||
Some(XLibWindowEvent::ButtonEvent(ButtonEvent::new(
|
Some(XLibWindowEvent::ButtonEvent(ButtonEvent::new(
|
||||||
ev.subwindow,
|
ev.subwindow,
|
||||||
state,
|
state,
|
||||||
keycode,
|
keycode,
|
||||||
(ev.x, ev.y).into(),
|
(ev.x, ev.y).into(),
|
||||||
modifierstate,
|
ModifierState::from_modmask(ev.state),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
xlib::KeyPress | xlib::KeyRelease => {
|
||||||
|
let ev = unsafe { &event.key };
|
||||||
|
let keycode =
|
||||||
|
keysym_to_virtual_keycode(self.keyev_to_keysym(ev).get());
|
||||||
|
let state = if ev.type_ == xlib::KeyPress {
|
||||||
|
KeyState::Pressed
|
||||||
|
} else {
|
||||||
|
KeyState::Released
|
||||||
|
};
|
||||||
|
|
||||||
|
keycode.map(|keycode| {
|
||||||
|
XLibWindowEvent::KeyEvent(KeyEvent::new(
|
||||||
|
ev.subwindow,
|
||||||
|
state,
|
||||||
|
keycode,
|
||||||
|
ModifierState::from_modmask(ev.state),
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -387,7 +415,7 @@ impl XLib {
|
||||||
|
|
||||||
if let Some(modifier) = modifier {
|
if let Some(modifier) = modifier {
|
||||||
match keyevent.type_ {
|
match keyevent.type_ {
|
||||||
KeyPress => self.modifier_state.set_mod(modifier),
|
KeyPress => self.modifier_state.insert_mod(modifier),
|
||||||
KeyRelease => self.modifier_state.unset_mod(modifier),
|
KeyRelease => self.modifier_state.unset_mod(modifier),
|
||||||
_ => unreachable!("keyyevent != (KeyPress | KeyRelease)"),
|
_ => unreachable!("keyyevent != (KeyPress | KeyRelease)"),
|
||||||
}
|
}
|
||||||
|
@ -545,6 +573,7 @@ impl XLib {
|
||||||
|
|
||||||
trait ModifierStateExt {
|
trait ModifierStateExt {
|
||||||
fn as_modmask(&self, xlib: &XLib) -> u32;
|
fn as_modmask(&self, xlib: &XLib) -> u32;
|
||||||
|
fn from_modmask(modmask: u32) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModifierStateExt for ModifierState {
|
impl ModifierStateExt for ModifierState {
|
||||||
|
@ -555,15 +584,30 @@ impl ModifierStateExt for ModifierState {
|
||||||
.get_numlock_mask()
|
.get_numlock_mask()
|
||||||
.expect("failed to query numlock mask");
|
.expect("failed to query numlock mask");
|
||||||
|
|
||||||
mask &= xlib::ShiftMask & !u32::from(self.contains(Self::SHIFT));
|
mask |= xlib::ShiftMask * u32::from(self.contains(Self::SHIFT));
|
||||||
mask &= xlib::LockMask & !u32::from(self.contains(Self::SHIFT_LOCK));
|
//mask |= xlib::LockMask * u32::from(self.contains(Self::SHIFT_LOCK));
|
||||||
mask &= xlib::ControlMask & !u32::from(self.contains(Self::CONTROL));
|
mask |= xlib::ControlMask * u32::from(self.contains(Self::CONTROL));
|
||||||
mask &= xlib::Mod1Mask & !u32::from(self.contains(Self::ALT));
|
mask |= xlib::Mod1Mask * u32::from(self.contains(Self::ALT));
|
||||||
mask &= xlib::Mod4Mask & !u32::from(self.contains(Self::SUPER));
|
//mask |= xlib::Mod2Mask * u32::from(self.contains(Self::NUM_LOCK));
|
||||||
mask &= numlock_mask & !u32::from(self.contains(Self::NUM_LOCK));
|
//mask |= xlib::Mod3Mask * u32::from(self.contains(Self::ALT_GR));
|
||||||
|
mask |= xlib::Mod4Mask * u32::from(self.contains(Self::SUPER));
|
||||||
|
//mask |= numlock_mask * u32::from(self.contains(Self::NUM_LOCK));
|
||||||
|
|
||||||
mask
|
mask
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_modmask(modmask: u32) -> Self {
|
||||||
|
let mut state = Self::empty();
|
||||||
|
state.set(Self::SHIFT, (modmask & xlib::ShiftMask) != 0);
|
||||||
|
//state.set(Self::SHIFT_LOCK, (modmask & xlib::LockMask) != 0);
|
||||||
|
state.set(Self::CONTROL, (modmask & xlib::ControlMask) != 0);
|
||||||
|
state.set(Self::ALT, (modmask & xlib::Mod1Mask) != 0);
|
||||||
|
//state.set(Self::NUM_LOCK, (modmask & xlib::Mod2Mask) != 0);
|
||||||
|
state.set(Self::ALT_GR, (modmask & xlib::Mod3Mask) != 0);
|
||||||
|
state.set(Self::SUPER, (modmask & xlib::Mod4Mask) != 0);
|
||||||
|
|
||||||
|
state
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowServerBackend for XLib {
|
impl WindowServerBackend for XLib {
|
||||||
|
@ -576,12 +620,14 @@ impl WindowServerBackend for XLib {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_event(&mut self) -> super::window_event::WindowEvent<Self::Window> {
|
fn next_event(&mut self) -> super::window_event::WindowEvent<Self::Window> {
|
||||||
std::iter::from_fn(|| {
|
loop {
|
||||||
let ev = self.next_xevent();
|
let ev = self.next_xevent();
|
||||||
self.xevent_to_window_event(ev)
|
let ev = self.xevent_to_window_event(ev);
|
||||||
})
|
|
||||||
.next()
|
if let Some(ev) = ev {
|
||||||
.unwrap()
|
return ev;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_event(
|
fn handle_event(
|
||||||
|
@ -700,7 +746,6 @@ impl WindowServerBackend for XLib {
|
||||||
xlib::XKillClient(self.dpy(), window);
|
xlib::XKillClient(self.dpy(), window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_parent_window(&self, window: Self::Window) -> Option<Self::Window> {
|
fn get_parent_window(&self, window: Self::Window) -> Option<Self::Window> {
|
||||||
|
|
28
src/state.rs
28
src/state.rs
|
@ -58,12 +58,14 @@ enum MoveResizeInfo {
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct MoveInfoInner {
|
struct MoveInfoInner {
|
||||||
window: Window,
|
window: Window,
|
||||||
starting_cursor_pos: Point<i32>,
|
starting_cursor_pos: Point<i32>,
|
||||||
starting_window_pos: Point<i32>,
|
starting_window_pos: Point<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct ResizeInfoInner {
|
struct ResizeInfoInner {
|
||||||
window: Window,
|
window: Window,
|
||||||
starting_cursor_pos: Point<i32>,
|
starting_cursor_pos: Point<i32>,
|
||||||
|
@ -160,12 +162,12 @@ where
|
||||||
));
|
));
|
||||||
|
|
||||||
self.add_keybind(KeyBinding::new(
|
self.add_keybind(KeyBinding::new(
|
||||||
KeyBind::new(VirtualKeyCode::Snapshot),
|
KeyBind::new(VirtualKeyCode::Print),
|
||||||
|wm, _| wm.spawn("screenshot.sh", &[]),
|
|wm, _| wm.spawn("screenshot.sh", &[]),
|
||||||
));
|
));
|
||||||
|
|
||||||
self.add_keybind(KeyBinding::new(
|
self.add_keybind(KeyBinding::new(
|
||||||
KeyBind::new(VirtualKeyCode::Snapshot).with_mod(ModifierKey::Shift),
|
KeyBind::new(VirtualKeyCode::Print).with_mod(ModifierKey::Shift),
|
||||||
|wm, _| wm.spawn("screenshot.sh", &["-edit"]),
|
|wm, _| wm.spawn("screenshot.sh", &["-edit"]),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -250,8 +252,6 @@ where
|
||||||
|
|
||||||
self.add_vs_switch_keybinds();
|
self.add_vs_switch_keybinds();
|
||||||
|
|
||||||
//self.xlib.init();
|
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,14 +421,16 @@ where
|
||||||
|
|
||||||
// TODO: change this somehow cuz I'm not a big fan of this "hardcoded" keybind stuff
|
// TODO: change this somehow cuz I'm not a big fan of this "hardcoded" keybind stuff
|
||||||
fn handle_keybinds(&mut self, event: &KeyEvent<B::Window>) {
|
fn handle_keybinds(&mut self, event: &KeyEvent<B::Window>) {
|
||||||
// I'm not sure if this has to be a Rc<RefCell>> or if it would be better as a Cell<>
|
if event.state == KeyState::Released {
|
||||||
let keybinds = self.keybinds.clone();
|
// I'm not sure if this has to be a Rc<RefCell>> or if it would be better as a Cell<>
|
||||||
|
let keybinds = self.keybinds.clone();
|
||||||
|
|
||||||
for kb in keybinds.borrow().iter() {
|
for kb in keybinds.borrow().iter() {
|
||||||
if kb.key.key == event.keycode
|
if kb.key.key == event.keycode
|
||||||
&& kb.key.modifiers == event.modifierstate
|
&& kb.key.modifiers == event.modifierstate
|
||||||
{
|
{
|
||||||
kb.call(self, event);
|
kb.call(self, event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -761,6 +763,8 @@ where
|
||||||
fn do_move_resize_window(&mut self, event: &MotionEvent<B::Window>) {
|
fn do_move_resize_window(&mut self, event: &MotionEvent<B::Window>) {
|
||||||
match &self.move_resize_window {
|
match &self.move_resize_window {
|
||||||
MoveResizeInfo::Move(info) => {
|
MoveResizeInfo::Move(info) => {
|
||||||
|
info!("do_move: {:#?}", info);
|
||||||
|
|
||||||
let (x, y) = (
|
let (x, y) = (
|
||||||
event.position.x - info.starting_cursor_pos.x,
|
event.position.x - info.starting_cursor_pos.x,
|
||||||
event.position.y - info.starting_cursor_pos.y,
|
event.position.y - info.starting_cursor_pos.y,
|
||||||
|
@ -778,6 +782,8 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MoveResizeInfo::Resize(info) => {
|
MoveResizeInfo::Resize(info) => {
|
||||||
|
info!("do_resize: {:#?}", info);
|
||||||
|
|
||||||
let (x, y) = (
|
let (x, y) = (
|
||||||
event.position.x - info.starting_cursor_pos.x,
|
event.position.x - info.starting_cursor_pos.x,
|
||||||
event.position.y - info.starting_cursor_pos.y,
|
event.position.y - info.starting_cursor_pos.y,
|
||||||
|
|
Loading…
Reference in a new issue