input stub

This commit is contained in:
Patrick Michl 2021-09-01 16:38:06 +02:00
parent 28e53ff17a
commit a238cd1aa4
2 changed files with 124 additions and 30 deletions

View File

@ -37,6 +37,8 @@ fn main() {
#[cfg(feature = "winit")]
fn run_winit(log: Logger) {
use smithay::backend::input::InputBackend;
let mut event_loop = EventLoop::try_new().unwrap();
let display = Rc::new(RefCell::new(Display::new()));
@ -76,16 +78,21 @@ fn run_winit(log: Logger) {
);
}
let size = renderer.borrow().window_size().physical_size;
let data = WinitData {};
let mut state = State::init(display.clone(), event_loop.handle(), data, log.clone());
let size = renderer.borrow().window_size().physical_size;
let mode = Mode {
size,
refresh: 60_000,
};
loop {}
//TODO Initialize Output map and WindowMap
while state.running.load(std::sync::atomic::Ordering::SeqCst) {
if input.dispatch_new_events(|event| state.process_input_event(event)).is_err() {
state.running.store(false, std::sync::atomic::Ordering::SeqCst);
break;
}
}
}

View File

@ -1,8 +1,34 @@
pub const OUTPUT_NAME: &str = "winit";
use slog::*;
use std::{cell::RefCell, rc::Rc, sync::{Arc, Mutex}, sync::atomic::{AtomicBool, Ordering}, time::Duration};
use std::{
cell::RefCell,
rc::Rc,
sync::atomic::{AtomicBool, Ordering},
sync::{Arc, Mutex},
time::Duration,
};
use smithay::{reexports::{calloop::{Mode, Interest, LoopHandle, PostAction, generic::Generic}, wayland_server::{Display, protocol::wl_surface::WlSurface}}, utils::{Logical, Point}, wayland::{data_device::{DataDeviceEvent, default_action_chooser, init_data_device, set_data_device_focus}, output::xdg::init_xdg_output_manager, seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig}, shm::init_shm_global}};
use smithay::{
backend::input::{InputBackend, InputEvent},
reexports::{
calloop::{generic::Generic, Interest, LoopHandle, Mode, PostAction},
wayland_server::{protocol::wl_surface::WlSurface, Display},
},
utils::{Logical, Point},
wayland::{
data_device::{
default_action_chooser, init_data_device, set_data_device_focus, DataDeviceEvent,
},
output::xdg::init_xdg_output_manager,
seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig},
shm::init_shm_global,
},
};
pub const OUTPUT_NAME: &str = "winit";
pub trait Backend {
fn seat_name(&self) -> String;
}
pub struct State<BackendData> {
pub backend_data: BackendData,
@ -20,28 +46,41 @@ pub struct State<BackendData> {
}
impl<BackendData: Backend + 'static> State<BackendData> {
pub fn init(display: Rc<RefCell<Display>>, loop_handle: LoopHandle<'static, State<BackendData>>, backend_data: BackendData, log: Logger) -> Self {
loop_handle.insert_source(
Generic::from_fd(display.borrow().get_poll_fd(), Interest::READ, Mode::Level),
move |_,_, state: &mut State<BackendData>| {
let display = state.display.clone();
let mut display = display.borrow_mut();
match display.dispatch(Duration::from_millis(0), state) {
Ok(_) => Ok(PostAction::Continue),
Err(e) => {
dbg!("I/O Error on Display {}", &e);
state.running.store(false, Ordering::SeqCst);
Err(e)
pub fn init(
display: Rc<RefCell<Display>>,
loop_handle: LoopHandle<'static, State<BackendData>>,
backend_data: BackendData,
log: Logger,
) -> Self {
loop_handle
.insert_source(
Generic::from_fd(display.borrow().get_poll_fd(), Interest::READ, Mode::Level),
move |_, _, state: &mut State<BackendData>| {
let display = state.display.clone();
let mut display = display.borrow_mut();
match display.dispatch(Duration::from_millis(0), state) {
Ok(_) => Ok(PostAction::Continue),
Err(e) => {
dbg!("I/O Error on Display {}", &e);
state.running.store(false, Ordering::SeqCst);
Err(e)
}
}
}
}).expect("Wayland Eventsource init failed");
},
)
.expect("Wayland Eventsource init failed");
init_shm_global(&mut display.borrow_mut(), Vec::new(), None);
//let shell_handles = init_shell()
init_xdg_output_manager(&mut display.borrow_mut(), None);
let socket_name = display.borrow_mut().add_socket_auto().unwrap().into_string().unwrap();
let socket_name = display
.borrow_mut()
.add_socket_auto()
.unwrap()
.into_string()
.unwrap();
dbg!(&socket_name);
std::env::set_var("WAYLAND_DISPLAY", &socket_name);
@ -68,10 +107,13 @@ impl<BackendData: Backend + 'static> State<BackendData> {
let cursor_status = Arc::new(Mutex::new(CursorImageStatus::Default));
let cursor_status2 = cursor_status.clone();
let pointer = seat.add_pointer(move |new_status| { *cursor_status2.lock().unwrap() = new_status});
let keyboard = seat.add_keyboard(XkbConfig::default(), 200, 25, |seat, focus| {
set_data_device_focus(seat, focus.and_then(|s| s.as_ref().client()))
}).expect("Could not initialize keyboard");
let pointer =
seat.add_pointer(move |new_status| *cursor_status2.lock().unwrap() = new_status);
let keyboard = seat
.add_keyboard(XkbConfig::default(), 200, 25, |seat, focus| {
set_data_device_focus(seat, focus.and_then(|s| s.as_ref().client()))
})
.expect("Could not initialize keyboard");
Self {
backend_data,
@ -81,15 +123,60 @@ impl<BackendData: Backend + 'static> State<BackendData> {
cursor_status,
dnd_icon,
keyboard,
pointer_location: (0.0,0.0).into(),
pointer_location: (0.0, 0.0).into(),
pointer,
seat_name,
seat,
log: log.clone()
log,
}
}
}
pub trait Backend {
fn seat_name(&self) -> String;
#[derive(Debug)]
enum KeyAction {
Quit,
VtSwitch(i32),
Run(String),
Screen(usize),
Forward,
None,
}
impl<Backend> State<Backend> {
fn key_to_action<B: InputBackend>(&mut self, event: B::KeyboardKeyEvent) -> KeyAction {
warn!(self.log, "Key Action Not implemented yet");
KeyAction::None
}
}
#[cfg(feature = "winit")]
use crate::WinitData;
#[cfg(feature = "winit")]
impl State<WinitData> {
pub fn process_input_event<Evt>(&mut self, event: InputEvent<Evt>)
where
Evt: InputBackend<SpecialEvent = smithay::backend::winit::WinitEvent>,
{
use smithay::backend::winit::WinitEvent;
match event {
InputEvent::Keyboard { event, .. } => match self.key_to_action::<Evt>(event) {
_ => warn!(self.log, "key to action not yet implemented"),
},
InputEvent::PointerMotionAbsolute { event, .. } => {
warn!(self.log, "PointerMotionAbsolute Not implemented yet")
}
InputEvent::PointerButton { event, .. } => {
warn!(self.log, "PointerButton Not implemented yet")
}
InputEvent::PointerAxis { event, .. } => {
warn!(self.log, "PointerAxis Not implemented yet")
}
InputEvent::Special(WinitEvent::Resized { size, .. }) => {
warn!(self.log, "RESIZE Not implemented yet")
}
_ => warn!(self.log, "Not implemented yet"),
};
}
}