diff --git a/src/main.rs b/src/main.rs index 80d78c6..61deb50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; + } + } } diff --git a/src/state.rs b/src/state.rs index 0bf3fa5..fb8f042 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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 { pub backend_data: BackendData, @@ -20,28 +46,41 @@ pub struct State { } impl State { - pub fn init(display: Rc>, loop_handle: LoopHandle<'static, State>, 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| { - 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>, + loop_handle: LoopHandle<'static, State>, + 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| { + 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 State { 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 State { 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 State { + fn key_to_action(&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 { + pub fn process_input_event(&mut self, event: InputEvent) + where + Evt: InputBackend, + { + use smithay::backend::winit::WinitEvent; + + match event { + InputEvent::Keyboard { event, .. } => match self.key_to_action::(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"), + }; + } }