VirtualKeyCode conversion traits
This commit is contained in:
parent
a175362a32
commit
ff27ec18d9
|
@ -4,299 +4,27 @@ pub mod keycodes;
|
||||||
pub mod window_event;
|
pub mod window_event;
|
||||||
pub mod xlib;
|
pub mod xlib;
|
||||||
|
|
||||||
pub trait WindowServerBackend<Window = u64> {
|
pub trait WindowServerBackend {
|
||||||
fn next_event(&self) -> window_event::WindowEvent;
|
type Window;
|
||||||
fn add_keybind(&mut self, keybind: KeyBind, window: Option<Window>);
|
|
||||||
fn remove_keybind(&mut self, keybind: KeyBind, window: Option<Window>);
|
fn next_event(&self) -> window_event::WindowEvent<Self::Window>;
|
||||||
fn add_mousebind(&mut self, keybind: KeyBind, window: Option<Window>);
|
fn add_keybind(&mut self, keybind: KeyBind, window: Option<Self::Window>);
|
||||||
fn remove_mousebind(&mut self, keybind: KeyBind, window: Option<Window>);
|
fn remove_keybind(
|
||||||
fn focus_window(&self, window: Window);
|
&mut self,
|
||||||
fn unfocus_window(&self, window: Window);
|
keybind: KeyBind,
|
||||||
fn move_window(&self, window: Window, pos: i32);
|
window: Option<Self::Window>,
|
||||||
fn resize_window(&self, window: Window, pos: i32);
|
);
|
||||||
fn hide_window(&self, window: Window);
|
fn add_mousebind(&mut self, keybind: KeyBind, window: Option<Self::Window>);
|
||||||
|
fn remove_mousebind(
|
||||||
|
&mut self,
|
||||||
|
keybind: KeyBind,
|
||||||
|
window: Option<Self::Window>,
|
||||||
|
);
|
||||||
|
fn focus_window(&self, window: Self::Window);
|
||||||
|
fn unfocus_window(&self, window: Self::Window);
|
||||||
|
fn move_window(&self, window: Self::Window, pos: i32);
|
||||||
|
fn resize_window(&self, window: Self::Window, pos: i32);
|
||||||
|
fn hide_window(&self, window: Self::Window);
|
||||||
fn screen_size(&self) -> (i32, i32);
|
fn screen_size(&self) -> (i32, i32);
|
||||||
fn kill_window(&self, window: Window);
|
fn kill_window(&self, window: Self::Window);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub mod xlib {
|
|
||||||
// use log::{error, warn};
|
|
||||||
// use std::{
|
|
||||||
// borrow::Borrow,
|
|
||||||
// convert::{TryFrom, TryInto},
|
|
||||||
// ffi::CString,
|
|
||||||
// rc::Rc,
|
|
||||||
// };
|
|
||||||
// use thiserror::Error;
|
|
||||||
|
|
||||||
// use x11::xlib::{self, Atom, Window, XEvent, XInternAtom};
|
|
||||||
|
|
||||||
// use super::{window_event::WindowEvent, WindowServerBackend};
|
|
||||||
|
|
||||||
// #[derive(Clone)]
|
|
||||||
// pub struct Display(Rc<*mut x11::xlib::Display>);
|
|
||||||
|
|
||||||
// #[derive(Debug, Error)]
|
|
||||||
// pub enum XlibError {
|
|
||||||
// #[error("BadAccess")]
|
|
||||||
// BadAccess,
|
|
||||||
// #[error("BadAlloc")]
|
|
||||||
// BadAlloc,
|
|
||||||
// #[error("BadAtom")]
|
|
||||||
// BadAtom,
|
|
||||||
// #[error("BadColor")]
|
|
||||||
// BadColor,
|
|
||||||
// #[error("BadCursor")]
|
|
||||||
// BadCursor,
|
|
||||||
// #[error("BadDrawable")]
|
|
||||||
// BadDrawable,
|
|
||||||
// #[error("BadFont")]
|
|
||||||
// BadFont,
|
|
||||||
// #[error("BadGC")]
|
|
||||||
// BadGC,
|
|
||||||
// #[error("BadIDChoice")]
|
|
||||||
// BadIDChoice,
|
|
||||||
// #[error("BadImplementation")]
|
|
||||||
// BadImplementation,
|
|
||||||
// #[error("BadLength")]
|
|
||||||
// BadLength,
|
|
||||||
// #[error("BadMatch")]
|
|
||||||
// BadMatch,
|
|
||||||
// #[error("BadName")]
|
|
||||||
// BadName,
|
|
||||||
// #[error("BadPixmap")]
|
|
||||||
// BadPixmap,
|
|
||||||
// #[error("BadRequest")]
|
|
||||||
// BadRequest,
|
|
||||||
// #[error("BadValue")]
|
|
||||||
// BadValue,
|
|
||||||
// #[error("BadWindow")]
|
|
||||||
// BadWindow,
|
|
||||||
// #[error("Invalid XError: {0}")]
|
|
||||||
// InvalidError(u8),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl From<u8> for XlibError {
|
|
||||||
// fn from(value: u8) -> Self {
|
|
||||||
// match value {
|
|
||||||
// xlib::BadAccess => XlibError::BadAccess,
|
|
||||||
// xlib::BadAlloc => XlibError::BadAlloc,
|
|
||||||
// xlib::BadAtom => XlibError::BadAtom,
|
|
||||||
// xlib::BadColor => XlibError::BadColor,
|
|
||||||
// xlib::BadCursor => XlibError::BadCursor,
|
|
||||||
// xlib::BadDrawable => XlibError::BadDrawable,
|
|
||||||
// xlib::BadFont => XlibError::BadFont,
|
|
||||||
// xlib::BadGC => XlibError::BadGC,
|
|
||||||
// xlib::BadIDChoice => XlibError::BadIDChoice,
|
|
||||||
// xlib::BadImplementation => XlibError::BadImplementation,
|
|
||||||
// xlib::BadLength => XlibError::BadLength,
|
|
||||||
// xlib::BadMatch => XlibError::BadMatch,
|
|
||||||
// xlib::BadName => XlibError::BadName,
|
|
||||||
// xlib::BadPixmap => XlibError::BadPixmap,
|
|
||||||
// xlib::BadRequest => XlibError::BadRequest,
|
|
||||||
// xlib::BadValue => XlibError::BadValue,
|
|
||||||
// xlib::BadWindow => XlibError::BadWindow,
|
|
||||||
// any => XlibError::InvalidError(any),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // impl Into<i32> for XlibError {
|
|
||||||
// // fn into(self) -> i32 {
|
|
||||||
// // match self {
|
|
||||||
// // XlibError::BadAccess => xlib::BadAccess.into(),
|
|
||||||
// // XlibError::BadAlloc => xlib::BadAlloc.into(),
|
|
||||||
// // XlibError::BadAtom => xlib::BadAtom.into(),
|
|
||||||
// // XlibError::BadColor => xlib::BadColor.into(),
|
|
||||||
// // XlibError::BadCursor => xlib::BadCursor.into(),
|
|
||||||
// // XlibError::BadDrawable => xlib::BadDrawable.into(),
|
|
||||||
// // XlibError::BadFont => xlib::BadFont.into(),
|
|
||||||
// // XlibError::BadGC => xlib::BadGC.into(),
|
|
||||||
// // XlibError::BadIDChoice => xlib::BadIDChoice.into(),
|
|
||||||
// // XlibError::BadImplementation => xlib::BadImplementation.into(),
|
|
||||||
// // XlibError::BadLength => xlib::BadLength.into(),
|
|
||||||
// // XlibError::BadMatch => xlib::BadMatch.into(),
|
|
||||||
// // XlibError::BadName => xlib::BadName.into(),
|
|
||||||
// // XlibError::BadPixmap => xlib::BadPixmap.into(),
|
|
||||||
// // XlibError::BadRequest => xlib::BadRequest.into(),
|
|
||||||
// // XlibError::BadValue => xlib::BadValue.into(),
|
|
||||||
// // XlibError::BadWindow => xlib::BadWindow.into(),
|
|
||||||
// // XlibError::InvalidError(err) => err,
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// impl Display {
|
|
||||||
// pub fn new(display: *mut x11::xlib::Display) -> Self {
|
|
||||||
// Self {
|
|
||||||
// 0: Rc::new(display),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub fn get(&self) -> *mut x11::xlib::Display {
|
|
||||||
// *self.0
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub struct XLib {
|
|
||||||
// display: Display,
|
|
||||||
// root: Window,
|
|
||||||
// screen: i32,
|
|
||||||
// atoms: XLibAtoms,
|
|
||||||
// keybinds: Vec<()>,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl XLib {
|
|
||||||
// fn dpy(&self) -> *mut xlib::Display {
|
|
||||||
// self.display.get()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn next_xevent(&self) -> XEvent {
|
|
||||||
// unsafe {
|
|
||||||
// let mut event = std::mem::MaybeUninit::<xlib::XEvent>::zeroed()
|
|
||||||
// .assume_init();
|
|
||||||
// xlib::XNextEvent(self.dpy(), &mut event);
|
|
||||||
|
|
||||||
// event
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl TryFrom<XEvent> for WindowEvent {
|
|
||||||
// type Error = crate::error::Error;
|
|
||||||
|
|
||||||
// fn try_from(event: XEvent) -> Result<Self, Self::Error> {
|
|
||||||
// match event.get_type() {
|
|
||||||
// xlib::MapRequest => Ok(Self::MapRequestEvent {
|
|
||||||
// window: event.map_request.window,
|
|
||||||
// event: todo!(),
|
|
||||||
// }),
|
|
||||||
// _ => Err(Self::Error::UnknownEvent),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl WindowServerBackend for XLib {
|
|
||||||
// fn next_event(&self) -> super::window_event::WindowEvent {
|
|
||||||
// self.next_xevent().try_into().unwrap()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn add_keybind(
|
|
||||||
// &mut self,
|
|
||||||
// keybind: super::window_event::KeyBind,
|
|
||||||
// window: Option<u64>,
|
|
||||||
// ) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn remove_keybind(
|
|
||||||
// &mut self,
|
|
||||||
// keybind: super::window_event::KeyBind,
|
|
||||||
// window: Option<u64>,
|
|
||||||
// ) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn add_mousebind(
|
|
||||||
// &mut self,
|
|
||||||
// keybind: super::window_event::KeyBind,
|
|
||||||
// window: Option<u64>,
|
|
||||||
// ) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn remove_mousebind(
|
|
||||||
// &mut self,
|
|
||||||
// keybind: super::window_event::KeyBind,
|
|
||||||
// window: Option<u64>,
|
|
||||||
// ) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn focus_window(&self, window: u64) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn unfocus_window(&self, window: u64) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn move_window(&self, window: u64, pos: i32) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn resize_window(&self, window: u64, pos: i32) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn hide_window(&self, window: u64) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn screen_size(&self) -> (i32, i32) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn kill_window(&self, window: u64) {
|
|
||||||
// todo!()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// struct XLibAtoms {
|
|
||||||
// protocols: Atom,
|
|
||||||
// delete_window: Atom,
|
|
||||||
// active_window: Atom,
|
|
||||||
// take_focus: Atom,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl XLibAtoms {
|
|
||||||
// fn init(display: Display) -> Self {
|
|
||||||
// unsafe {
|
|
||||||
// Self {
|
|
||||||
// protocols: {
|
|
||||||
// let name = CString::new("WM_PROTOCOLS").unwrap();
|
|
||||||
// XInternAtom(display.get(), name.as_c_str().as_ptr(), 0)
|
|
||||||
// },
|
|
||||||
// delete_window: {
|
|
||||||
// let name = CString::new("WM_DELETE_WINDOW").unwrap();
|
|
||||||
// XInternAtom(display.get(), name.as_c_str().as_ptr(), 0)
|
|
||||||
// },
|
|
||||||
// active_window: {
|
|
||||||
// let name = CString::new("WM_ACTIVE_WINDOW").unwrap();
|
|
||||||
// XInternAtom(display.get(), name.as_c_str().as_ptr(), 0)
|
|
||||||
// },
|
|
||||||
// take_focus: {
|
|
||||||
// let name = CString::new("WM_TAKE_FOCUS").unwrap();
|
|
||||||
// XInternAtom(display.get(), name.as_c_str().as_ptr(), 0)
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// #[allow(dead_code)]
|
|
||||||
// unsafe extern "C" fn xlib_error_handler(
|
|
||||||
// _dpy: *mut x11::xlib::Display,
|
|
||||||
// ee: *mut x11::xlib::XErrorEvent,
|
|
||||||
// ) -> std::os::raw::c_int {
|
|
||||||
// let err_event = ee.as_ref().unwrap();
|
|
||||||
// let err = XlibError::from(err_event.error_code);
|
|
||||||
|
|
||||||
// match err {
|
|
||||||
// err @ XlibError::BadAccess
|
|
||||||
// | err @ XlibError::BadMatch
|
|
||||||
// | err @ XlibError::BadWindow
|
|
||||||
// | err @ XlibError::BadDrawable => {
|
|
||||||
// warn!("{:?}", err);
|
|
||||||
// 0
|
|
||||||
// }
|
|
||||||
// _ => {
|
|
||||||
// error!(
|
|
||||||
// "wm: fatal error:\nrequest_code: {}\nerror_code: {}",
|
|
||||||
// err_event.request_code, err_event.error_code
|
|
||||||
// );
|
|
||||||
// std::process::exit(1)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
|
@ -4,51 +4,18 @@
|
||||||
use super::keycodes::{MouseButton, VirtualKeyCode};
|
use super::keycodes::{MouseButton, VirtualKeyCode};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum WindowEvent {
|
pub enum WindowEvent<Window> {
|
||||||
KeyEvent {
|
KeyEvent(KeyEvent<Window>),
|
||||||
window: Window,
|
ButtonEvent(ButtonEvent<Window>),
|
||||||
event: KeyEvent,
|
MotionEvent(MotionEvent<Window>),
|
||||||
},
|
MapRequestEvent(MapEvent<Window>),
|
||||||
ButtonEvent {
|
MapEvent(MapEvent<Window>),
|
||||||
window: Window,
|
UnmapEvent(UnmapEvent<Window>),
|
||||||
event: ButtonEvent,
|
CreateEvent(CreateEvent<Window>),
|
||||||
},
|
DestroyEvent(DestroyEvent<Window>),
|
||||||
MotionEvent {
|
EnterEvent(EnterEvent<Window>),
|
||||||
window: Window,
|
ConfigureEvent(ConfigureEvent<Window>),
|
||||||
event: MotionEvent,
|
FullscreenEvent(FullscreenEvent<Window>), //1 { window: Window, event: 1 },
|
||||||
},
|
|
||||||
MapRequestEvent {
|
|
||||||
window: Window,
|
|
||||||
event: MapEvent,
|
|
||||||
},
|
|
||||||
MapEvent {
|
|
||||||
window: Window,
|
|
||||||
event: MapEvent,
|
|
||||||
},
|
|
||||||
UnmapEvent {
|
|
||||||
window: Window,
|
|
||||||
event: UnmapEvent,
|
|
||||||
},
|
|
||||||
CreateEvent {
|
|
||||||
window: Window,
|
|
||||||
event: CreateEvent,
|
|
||||||
},
|
|
||||||
DestroyEvent {
|
|
||||||
window: Window,
|
|
||||||
event: DestroyEvent,
|
|
||||||
},
|
|
||||||
EnterEvent {
|
|
||||||
window: Window,
|
|
||||||
event: EnterEvent,
|
|
||||||
},
|
|
||||||
ConfigureEvent {
|
|
||||||
window: Window,
|
|
||||||
event: ConfigureEvent,
|
|
||||||
},
|
|
||||||
FullscreenEvent {
|
|
||||||
window: Window,
|
|
||||||
event: FullscreenEvent,
|
|
||||||
}, //1 { window: Window, event: 1 },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -94,19 +61,22 @@ impl ModifierState {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct KeyEvent {
|
pub struct KeyEvent<Window> {
|
||||||
|
pub window: Window,
|
||||||
pub state: KeyState,
|
pub state: KeyState,
|
||||||
pub keycode: VirtualKeyCode,
|
pub keycode: VirtualKeyCode,
|
||||||
pub modifierstate: ModifierState,
|
pub modifierstate: ModifierState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyEvent {
|
impl<Window> KeyEvent<Window> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
window: Window,
|
||||||
state: KeyState,
|
state: KeyState,
|
||||||
keycode: VirtualKeyCode,
|
keycode: VirtualKeyCode,
|
||||||
modifierstate: ModifierState,
|
modifierstate: ModifierState,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
window,
|
||||||
state,
|
state,
|
||||||
keycode,
|
keycode,
|
||||||
modifierstate,
|
modifierstate,
|
||||||
|
@ -115,19 +85,22 @@ impl KeyEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ButtonEvent {
|
pub struct ButtonEvent<Window> {
|
||||||
|
pub window: Window,
|
||||||
pub state: KeyState,
|
pub state: KeyState,
|
||||||
pub keycode: MouseButton,
|
pub keycode: MouseButton,
|
||||||
pub modifierstate: ModifierState,
|
pub modifierstate: ModifierState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ButtonEvent {
|
impl<Window> ButtonEvent<Window> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
window: Window,
|
||||||
state: KeyState,
|
state: KeyState,
|
||||||
keycode: MouseButton,
|
keycode: MouseButton,
|
||||||
modifierstate: ModifierState,
|
modifierstate: ModifierState,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
window,
|
||||||
state,
|
state,
|
||||||
keycode,
|
keycode,
|
||||||
modifierstate,
|
modifierstate,
|
||||||
|
@ -136,60 +109,51 @@ impl ButtonEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MotionEvent {
|
pub struct MotionEvent<Window> {
|
||||||
pub position: [i32; 2],
|
pub position: [i32; 2],
|
||||||
|
pub window: Window,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MotionEvent {
|
impl<Window> MotionEvent<Window> {
|
||||||
pub fn new(position: [i32; 2]) -> Self {
|
pub fn new(position: [i32; 2], window: Window) -> Self {
|
||||||
Self { position }
|
Self { position, window }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MapEvent {
|
pub struct MapEvent<Window> {
|
||||||
pub window: Window,
|
pub window: Window,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MapEvent {
|
#[derive(Debug)]
|
||||||
|
pub struct UnmapEvent<Window> {
|
||||||
|
pub window: Window,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct EnterEvent<Window> {
|
||||||
|
pub window: Window,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DestroyEvent<Window> {
|
||||||
|
pub window: Window,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Window> DestroyEvent<Window> {
|
||||||
pub fn new(window: Window) -> Self {
|
pub fn new(window: Window) -> Self {
|
||||||
Self { window }
|
Self { window }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UnmapEvent {
|
pub struct CreateEvent<Window> {
|
||||||
pub window: Window,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UnmapEvent {
|
|
||||||
pub fn new(window: Window) -> Self {
|
|
||||||
Self { window }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct EnterEvent {}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DestroyEvent {
|
|
||||||
pub window: Window,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DestroyEvent {
|
|
||||||
pub fn new(window: Window) -> Self {
|
|
||||||
Self { window }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct CreateEvent {
|
|
||||||
pub window: Window,
|
pub window: Window,
|
||||||
pub position: [i32; 2],
|
pub position: [i32; 2],
|
||||||
pub size: [i32; 2],
|
pub size: [i32; 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CreateEvent {
|
impl<Window> CreateEvent<Window> {
|
||||||
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
window,
|
window,
|
||||||
|
@ -200,13 +164,13 @@ impl CreateEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ConfigureEvent {
|
pub struct ConfigureEvent<Window> {
|
||||||
pub window: Window,
|
pub window: Window,
|
||||||
pub position: [i32; 2],
|
pub position: [i32; 2],
|
||||||
pub size: [i32; 2],
|
pub size: [i32; 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigureEvent {
|
impl<Window> ConfigureEvent<Window> {
|
||||||
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
window,
|
window,
|
||||||
|
@ -217,13 +181,17 @@ impl ConfigureEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FullscreenEvent {
|
pub struct FullscreenEvent<Window> {
|
||||||
|
window: Window,
|
||||||
new_fullscreen: bool,
|
new_fullscreen: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FullscreenEvent {
|
impl<Window> FullscreenEvent<Window> {
|
||||||
pub fn new(new_fullscreen: bool) -> Self {
|
pub fn new(window: Window, new_fullscreen: bool) -> Self {
|
||||||
Self { new_fullscreen }
|
Self {
|
||||||
|
window,
|
||||||
|
new_fullscreen,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::backends::{
|
use std::{borrow::Borrow, ops::Deref};
|
||||||
keycodes::{MouseButton, VirtualKeyCode},
|
|
||||||
window_event::ModifierKey,
|
use x11::xlib::XKeyReleasedEvent;
|
||||||
};
|
|
||||||
|
use crate::backends::keycodes::{MouseButton, VirtualKeyCode};
|
||||||
|
|
||||||
pub fn xev_to_mouse_button(
|
pub fn xev_to_mouse_button(
|
||||||
button: &x11::xlib::XButtonEvent,
|
button: &x11::xlib::XButtonEvent,
|
||||||
|
@ -34,10 +35,42 @@ pub fn mouse_button_to_xbutton(button: MouseButton) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||||
|
pub struct XKeysym(u32);
|
||||||
|
|
||||||
|
impl Borrow<u32> for XKeysym {
|
||||||
|
fn borrow(&self) -> &u32 {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<u32> for XKeysym {
|
||||||
|
fn as_ref(&self) -> &u32 {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for XKeysym {
|
||||||
|
type Target = u32;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<XKeysym> for VirtualKeyCode {
|
||||||
|
fn from(value: XKeysym) -> Self {
|
||||||
|
keysym_to_virtual_keycode(*value).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<VirtualKeyCode> for XKeysym {
|
||||||
|
fn from(value: VirtualKeyCode) -> Self {
|
||||||
|
Self(virtual_keycode_to_keysym(value).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// from winit
|
/// from winit
|
||||||
pub fn keysym_to_virtual_keycode(
|
pub fn keysym_to_virtual_keycode(keysym: u32) -> Option<VirtualKeyCode> {
|
||||||
keysym: u32,
|
|
||||||
) -> Option<VirtualKeyCode> {
|
|
||||||
Some(match keysym {
|
Some(match keysym {
|
||||||
x11::keysym::XK_BackSpace => VirtualKeyCode::Back,
|
x11::keysym::XK_BackSpace => VirtualKeyCode::Back,
|
||||||
x11::keysym::XK_Tab => VirtualKeyCode::Tab,
|
x11::keysym::XK_Tab => VirtualKeyCode::Tab,
|
||||||
|
@ -1034,9 +1067,7 @@ pub fn keysym_to_virtual_keycode(
|
||||||
//x11::keysym::XK_hebrew_taf => VirtualKeyCode::Hebrew_taf,
|
//x11::keysym::XK_hebrew_taf => VirtualKeyCode::Hebrew_taf,
|
||||||
//x11::keysym::XK_Hebrew_switch => VirtualKeyCode::Hebrew_switch,
|
//x11::keysym::XK_Hebrew_switch => VirtualKeyCode::Hebrew_switch,
|
||||||
x11::keysym::XF86XK_Back => VirtualKeyCode::NavigateBackward,
|
x11::keysym::XF86XK_Back => VirtualKeyCode::NavigateBackward,
|
||||||
x11::keysym::XF86XK_Forward => {
|
x11::keysym::XF86XK_Forward => VirtualKeyCode::NavigateForward,
|
||||||
VirtualKeyCode::NavigateForward
|
|
||||||
}
|
|
||||||
x11::keysym::XF86XK_Copy => VirtualKeyCode::Copy,
|
x11::keysym::XF86XK_Copy => VirtualKeyCode::Copy,
|
||||||
x11::keysym::XF86XK_Paste => VirtualKeyCode::Paste,
|
x11::keysym::XF86XK_Paste => VirtualKeyCode::Paste,
|
||||||
x11::keysym::XF86XK_Cut => VirtualKeyCode::Cut,
|
x11::keysym::XF86XK_Cut => VirtualKeyCode::Cut,
|
||||||
|
@ -1044,9 +1075,7 @@ pub fn keysym_to_virtual_keycode(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn virtual_keycode_to_keysym(
|
pub fn virtual_keycode_to_keysym(keycode: VirtualKeyCode) -> Option<u32> {
|
||||||
keycode: VirtualKeyCode,
|
|
||||||
) -> Option<u32> {
|
|
||||||
Some(match keycode {
|
Some(match keycode {
|
||||||
VirtualKeyCode::Back => x11::keysym::XK_BackSpace,
|
VirtualKeyCode::Back => x11::keysym::XK_BackSpace,
|
||||||
VirtualKeyCode::Tab => x11::keysym::XK_Tab,
|
VirtualKeyCode::Tab => x11::keysym::XK_Tab,
|
||||||
|
@ -2043,12 +2072,28 @@ pub fn virtual_keycode_to_keysym(
|
||||||
//VirtualKeyCode::Hebrew_taf => x11::keysym::XK_hebrew_taf,
|
//VirtualKeyCode::Hebrew_taf => x11::keysym::XK_hebrew_taf,
|
||||||
//VirtualKeyCode::Hebrew_switch => x11::keysym::XK_Hebrew_switch,
|
//VirtualKeyCode::Hebrew_switch => x11::keysym::XK_Hebrew_switch,
|
||||||
VirtualKeyCode::NavigateBackward => x11::keysym::XF86XK_Back,
|
VirtualKeyCode::NavigateBackward => x11::keysym::XF86XK_Back,
|
||||||
VirtualKeyCode::NavigateForward => {
|
VirtualKeyCode::NavigateForward => x11::keysym::XF86XK_Forward,
|
||||||
x11::keysym::XF86XK_Forward
|
|
||||||
}
|
|
||||||
VirtualKeyCode::Copy => x11::keysym::XF86XK_Copy,
|
VirtualKeyCode::Copy => x11::keysym::XF86XK_Copy,
|
||||||
VirtualKeyCode::Paste => x11::keysym::XF86XK_Paste,
|
VirtualKeyCode::Paste => x11::keysym::XF86XK_Paste,
|
||||||
VirtualKeyCode::Cut => x11::keysym::XF86XK_Cut,
|
VirtualKeyCode::Cut => x11::keysym::XF86XK_Cut,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_keysym_to_vkc() {
|
||||||
|
let keysym: XKeysym = VirtualKeyCode::W.into();
|
||||||
|
let keycode: VirtualKeyCode = keysym.into();
|
||||||
|
|
||||||
|
assert_eq!(keycode, VirtualKeyCode::W);
|
||||||
|
|
||||||
|
let keysym2: XKeysym = keycode.into();
|
||||||
|
|
||||||
|
assert_eq!(keysym2, keysym);
|
||||||
|
assert_eq!(&x11::keysym::XK_W, keysym.as_ref());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Borrow,
|
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
ffi::CString,
|
ffi::CString,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
@ -9,7 +8,16 @@ use thiserror::Error;
|
||||||
|
|
||||||
use x11::xlib::{self, Atom, Window, XEvent, XInternAtom};
|
use x11::xlib::{self, Atom, Window, XEvent, XInternAtom};
|
||||||
|
|
||||||
use super::{window_event::WindowEvent, WindowServerBackend};
|
use self::keysym::xev_to_mouse_button;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
keycodes::{MouseButton, VirtualKeyCode},
|
||||||
|
window_event::{
|
||||||
|
ButtonEvent, ConfigureEvent, DestroyEvent, EnterEvent, KeyState,
|
||||||
|
MapEvent, ModifierState, UnmapEvent, WindowEvent,
|
||||||
|
},
|
||||||
|
WindowServerBackend,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod keysym;
|
pub mod keysym;
|
||||||
|
|
||||||
|
@ -142,29 +150,69 @@ impl XLib {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<XEvent> for WindowEvent {
|
impl TryFrom<XEvent> for WindowEvent<xlib::Window> {
|
||||||
type Error = crate::error::Error;
|
type Error = crate::error::Error;
|
||||||
|
|
||||||
fn try_from(event: XEvent) -> Result<Self, Self::Error> {
|
fn try_from(event: XEvent) -> Result<Self, Self::Error> {
|
||||||
match event.get_type() {
|
match event.get_type() {
|
||||||
xlib::MapRequest => Ok(Self::MapRequestEvent {
|
xlib::MapRequest => {
|
||||||
window: event.map_request.window,
|
let ev = unsafe { &event.map_request };
|
||||||
event: todo!(),
|
Ok(Self::MapRequestEvent(MapEvent { window: ev.window }))
|
||||||
}),
|
}
|
||||||
|
xlib::UnmapNotify => {
|
||||||
|
let ev = unsafe { &event.unmap };
|
||||||
|
Ok(Self::UnmapEvent(UnmapEvent { window: ev.window }))
|
||||||
|
}
|
||||||
|
xlib::ConfigureRequest => {
|
||||||
|
let ev = unsafe { &event.configure_request };
|
||||||
|
Ok(Self::ConfigureEvent(ConfigureEvent {
|
||||||
|
window: ev.window,
|
||||||
|
position: [ev.x, ev.y],
|
||||||
|
size: [ev.width, ev.height],
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
xlib::EnterNotify => {
|
||||||
|
let ev = unsafe { &event.crossing };
|
||||||
|
Ok(Self::EnterEvent(EnterEvent { window: ev.window }))
|
||||||
|
}
|
||||||
|
xlib::DestroyNotify => {
|
||||||
|
let ev = unsafe { &event.destroy_window };
|
||||||
|
Ok(Self::DestroyEvent(DestroyEvent { window: ev.window }))
|
||||||
|
}
|
||||||
|
xlib::ButtonPress | xlib::ButtonRelease => {
|
||||||
|
let ev = unsafe { &event.button };
|
||||||
|
let keycode = xev_to_mouse_button(ev).unwrap();
|
||||||
|
let state = if ev.state as i32 == xlib::ButtonPress {
|
||||||
|
KeyState::Pressed
|
||||||
|
} else {
|
||||||
|
KeyState::Released
|
||||||
|
};
|
||||||
|
|
||||||
|
let modifierstate = ModifierState::new();
|
||||||
|
|
||||||
|
Ok(Self::ButtonEvent(ButtonEvent::new(
|
||||||
|
ev.window,
|
||||||
|
state,
|
||||||
|
keycode,
|
||||||
|
modifierstate,
|
||||||
|
)))
|
||||||
|
}
|
||||||
_ => Err(Self::Error::UnknownEvent),
|
_ => Err(Self::Error::UnknownEvent),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowServerBackend for XLib {
|
impl WindowServerBackend for XLib {
|
||||||
fn next_event(&self) -> super::window_event::WindowEvent {
|
type Window = xlib::Window;
|
||||||
|
|
||||||
|
fn next_event(&self) -> super::window_event::WindowEvent<Self::Window> {
|
||||||
self.next_xevent().try_into().unwrap()
|
self.next_xevent().try_into().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_keybind(
|
fn add_keybind(
|
||||||
&mut self,
|
&mut self,
|
||||||
keybind: super::window_event::KeyBind,
|
keybind: super::window_event::KeyBind,
|
||||||
window: Option<u64>,
|
window: Option<Self::Window>,
|
||||||
) {
|
) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
@ -172,7 +220,7 @@ impl WindowServerBackend for XLib {
|
||||||
fn remove_keybind(
|
fn remove_keybind(
|
||||||
&mut self,
|
&mut self,
|
||||||
keybind: super::window_event::KeyBind,
|
keybind: super::window_event::KeyBind,
|
||||||
window: Option<u64>,
|
window: Option<Self::Window>,
|
||||||
) {
|
) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
@ -180,7 +228,7 @@ impl WindowServerBackend for XLib {
|
||||||
fn add_mousebind(
|
fn add_mousebind(
|
||||||
&mut self,
|
&mut self,
|
||||||
keybind: super::window_event::KeyBind,
|
keybind: super::window_event::KeyBind,
|
||||||
window: Option<u64>,
|
window: Option<Self::Window>,
|
||||||
) {
|
) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
@ -188,28 +236,28 @@ impl WindowServerBackend for XLib {
|
||||||
fn remove_mousebind(
|
fn remove_mousebind(
|
||||||
&mut self,
|
&mut self,
|
||||||
keybind: super::window_event::KeyBind,
|
keybind: super::window_event::KeyBind,
|
||||||
window: Option<u64>,
|
window: Option<Self::Window>,
|
||||||
) {
|
) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn focus_window(&self, window: u64) {
|
fn focus_window(&self, window: Self::Window) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unfocus_window(&self, window: u64) {
|
fn unfocus_window(&self, window: Self::Window) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_window(&self, window: u64, pos: i32) {
|
fn move_window(&self, window: Self::Window, pos: i32) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize_window(&self, window: u64, pos: i32) {
|
fn resize_window(&self, window: Self::Window, pos: i32) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hide_window(&self, window: u64) {
|
fn hide_window(&self, window: Self::Window) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,7 +265,7 @@ impl WindowServerBackend for XLib {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kill_window(&self, window: u64) {
|
fn kill_window(&self, window: Self::Window) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,8 @@ pub mod error {
|
||||||
NonError,
|
NonError,
|
||||||
#[error("Unknown Event")]
|
#[error("Unknown Event")]
|
||||||
UnknownEvent,
|
UnknownEvent,
|
||||||
|
#[error("Unhandled VirtualKeyCode")]
|
||||||
|
UnhandledVirtualKeyCode,
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
IoError(#[from] std::io::Error),
|
IoError(#[from] std::io::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
|
|
Loading…
Reference in a new issue