From 8e92c9c9226709dc37136cc417c516c736789ec1 Mon Sep 17 00:00:00 2001 From: Drakulix Date: Sat, 18 Mar 2017 17:09:29 +0100 Subject: [PATCH] Add InputConfig --- src/backend/glutin.rs | 16 ++++++++++++---- src/backend/input.rs | 26 +++++++++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/backend/glutin.rs b/src/backend/glutin.rs index 18cf3f7..c63a8e7 100644 --- a/src/backend/glutin.rs +++ b/src/backend/glutin.rs @@ -198,12 +198,15 @@ pub struct GlutinInputBackend window: Rc, time_counter: u32, seat: Seat, - handler: Option>, + input_config: (), + handler: Option + 'static>>, } impl InputBackend for GlutinInputBackend { - fn set_handler(&mut self, mut handler: H) { + type InputConfig = (); + + fn set_handler + 'static>(&mut self, mut handler: H) { if self.handler.is_some() { self.clear_handler(); } @@ -211,9 +214,9 @@ impl InputBackend for GlutinInputBackend self.handler = Some(Box::new(handler)); } - fn get_handler(&mut self) -> Option<&mut InputHandler> + fn get_handler(&mut self) -> Option<&mut InputHandler> { - self.handler.as_mut().map(|handler| handler as &mut InputHandler) + self.handler.as_mut().map(|handler| handler as &mut InputHandler) } fn clear_handler(&mut self) { @@ -222,6 +225,10 @@ impl InputBackend for GlutinInputBackend } } + fn input_config(&mut self) -> &mut Self::InputConfig { + &mut self.input_config + } + fn set_cursor_position(&mut self, x: u32, y: u32) -> Result<(), ()> { if let Some((win_x, win_y)) = self.window.get_position() { self.window.set_cursor_position(win_x + x as i32, win_y + y as i32) @@ -239,6 +246,7 @@ impl GlutinInputBackend window: window, time_counter: 0, seat: Seat::new(0), + input_config: (), handler: None, } } diff --git a/src/backend/input.rs b/src/backend/input.rs index 9306e0d..5bd3164 100644 --- a/src/backend/input.rs +++ b/src/backend/input.rs @@ -158,13 +158,19 @@ pub enum TouchEvent /// Trait that describes objects providing a source of input events. All input backends /// need to implemenent this and provide the same base gurantees about the presicion of /// given events. -pub trait InputBackend { +pub trait InputBackend: Sized { + /// Type of input device associated with the backend + type InputConfig; /// Sets a new handler for this `InputBackend` - fn set_handler(&mut self, handler: H); + fn set_handler + 'static>(&mut self, handler: H); /// Get a reference to the currently set handler, if any - fn get_handler(&mut self) -> Option<&mut InputHandler>; + fn get_handler(&mut self) -> Option<&mut InputHandler>; /// Clears the currently handler, if one is set fn clear_handler(&mut self); + + /// Get current `InputConfig` + fn input_config(&mut self) -> &mut Self::InputConfig; + /// Sets the cursor position, useful for e.g. pointer wrapping. /// /// Not guaranteed to be supported on every backend. The result usually @@ -174,7 +180,7 @@ pub trait InputBackend { } /// Implement to receive input events from any `InputBackend`. -pub trait InputHandler { +pub trait InputHandler { /// Called when a new `Seat` has been created fn on_seat_created(&mut self, seat: &Seat); /// Called when an existing `Seat` has been destroyed. @@ -244,9 +250,15 @@ pub trait InputHandler { /// - check if events can arrive out of order. /// - Make stronger time guarantees fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent); + + /// Called when the `InputConfig` was changed through an external event. + /// + /// What kind of events can trigger this call is completely backend dependent. + /// E.g. an input devices was attached/detached or changed it's own configuration. + fn on_input_config_changed(&mut self, config: &mut B::InputConfig); } -impl InputHandler for Box { +impl InputHandler for Box> { fn on_seat_created(&mut self, seat: &Seat) { (**self).on_seat_created(seat) } @@ -274,4 +286,8 @@ impl InputHandler for Box { fn on_touch(&mut self, seat: &Seat, time: u32, event: TouchEvent) { (**self).on_touch(seat, time, event) } + + fn on_input_config_changed(&mut self, config: &mut B::InputConfig) { + (**self).on_input_config_changed(config) + } }